Skip to content

Conversation

@zhouyusd
Copy link
Contributor

@zhouyusd zhouyusd commented Oct 5, 2025

让动态SQL支持泛型参数

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

有的时候参数作为查询条件,零值是有意义的,需要实现nil的时候忽略该查询条件,尝试了泛型
原先gen版本只能解析到类型名,不带泛型参数

User Case Description

wrapper.go

package wrapper

type Wrapper[T any] struct {
	Value T
}
type UserQuerier interface {
  // SELECT * FROM users
  // {{ where }}
  //   {{ if status != nil }}status = @status.Value{{ end }}
  // {{ end }}
  GetUsers(status *wrapper.Wrapper[int]) ([]*gen.T, error)
}

原先生成效果

type IUserDo interface {
    ...

    GetUsers(status *Wrapper) ([]*model.User, error)
}

现在生成效果

type IUserDo interface {
    ...

    GetUsers(status *wrapper.Wrapper[int]) ([]*model.User, error)
}

@propel-code-bot
Copy link

propel-code-bot bot commented Oct 5, 2025

Add generic-type awareness and pagination/count support to dynamic SQL generator

This PR is a substantial extension of gorm/gen that enables the code-generator to understand Go generic type parameters when parsing interfaces and to enrich dynamic SQL with SELECT / ORDER BY blocks plus built-in pagination & total-count handling. Core parsing, clause building, template generation, helper functions and public API are all updated while keeping existing APIs backward-compatible.

Key Changes

• Parser: new function astGetGenericType, extended handling for ast.IndexExpr / ast.IndexListExpr / ast.SelectorExpr; helper flags IsGenPage, IsCount to detect gen.Page and int64 count returns.
• Model: status enum now includes SELECT and ORDERBY; Page struct added to public API (generator.go) with GetLimit/GetOffset helpers.
• Clause system: deprecated SQLClause.Finish() in favour of SQLClause.Finishes(...bool) returning a slice of builder lines; new clause types SelectClause and OrderByClause with corresponding Create/Finishes implementations.
• Section builder: major refactor to parse new blocks, track order-by state, and emit multiple template lines via variadic appendTmpl; validation now forbids SELECT/ORDERBY without matching page/count usage.
• Templates: internal/template/method.go updated to emit pagination/record/count SQL builders using new helpers; supports conditional param slice and switched exec SQL var (recordSQL vs generateSQL).
• Helper utilities: added JoinRecordBuilder, JoinCountBuilder; modified JoinWhereBuilder and others for trimming.
• Generator: detects Page param and count result, wires pagination into generated code; propagates import paths and enhances error checks.
• Bug-fix: commit includes fallback to interface{} when astGetType returns empty, preventing malformed generic strings.
• Misc: extended validation in export.go, new enum constants, many small safety tweaks.

Affected Areas

internal/parser/ – type parsing
internal/generate/ – clause, section, interface processing, export
internal/template/ – DIY and CRUD method templates
helper/clause.go – SQL builder helpers
generator.go public API
internal/model enums & helpers

This summary was automatically generated by @propel-code-bot

Co-authored-by: propel-code-bot[bot] <203372662+propel-code-bot[bot]@users.noreply.github.com>
Copy link

@propel-code-bot propel-code-bot bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, ship it! 🚢


Why was this auto-approved?
AI analysis completed with no actionable comments or suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant