Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: generated code, mainly postgres #94

Merged
merged 6 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 55 additions & 57 deletions codegen/templates/db.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
{{- reserveImport "github.com/si3nloong/sqlgen/sequel" }}
{{- reserveImport "github.com/si3nloong/sqlgen/sequel/strpool" }}

const _getTableSQL = "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = {{ quoteVar 1 }} LIMIT 1;"

func InsertOne[T sequel.TableColumnValuer[T], Ptr interface {
sequel.TableColumnValuer[T]
sequel.Scanner[T]
Expand Down Expand Up @@ -39,7 +37,7 @@ func InsertOne[T sequel.TableColumnValuer[T], Ptr interface {
{{ if not isStaticVar -}}
stmt := strpool.AcquireString()
defer strpool.ReleaseString(stmt)
stmt.WriteString("INSERT INTO " + model.TableName() + " (" + strings.Join(columns, ",") + ") VALUES (")
stmt.WriteString("INSERT INTO " + dbName(model) + model.TableName() + " (" + strings.Join(columns, ",") + ") VALUES (")
for i := range args {
if i > 0 {
stmt.WriteByte(',')
Expand All @@ -53,13 +51,13 @@ func InsertOne[T sequel.TableColumnValuer[T], Ptr interface {
if err := sqlConn.QueryRowContext(ctx, stmt.String(), args...).Scan(model.Addrs()...); err != nil {
return nil, err
}
return new(sequel.EmptyResult), nil
return sequel.NewRowsAffectedResult(1), nil
{{ else }}
stmt.WriteString(");")
return sqlConn.ExecContext(ctx, stmt.String(), args...)
{{ end }}
{{ end -}}
{{ else -}}
return sqlConn.ExecContext(ctx, "INSERT INTO "+ model.TableName() +" ("+ strings.Join(columns, ",") +") VALUES ("+ strings.Repeat({{ quote (print "," varRune) }}, len(columns))[1:]+");", args...)
return sqlConn.ExecContext(ctx, "INSERT INTO "+ dbName(model) + model.TableName() +" ("+ strings.Join(columns, ",") +") VALUES ("+ strings.Repeat({{ quote (print "," varRune) }}, len(columns))[1:]+");", args...)
{{ end -}}
}

Expand All @@ -81,7 +79,7 @@ func Insert[T sequel.TableColumnValuer[T]](ctx context.Context, sqlConn sequel.D
switch vi := any(model).(type) {
case sequel.Inserter:
query := strings.Repeat(vi.InsertVarQuery()+",", len(data))
return sqlConn.ExecContext(ctx, "INSERT INTO "+model.TableName()+" ("+strings.Join(columns, ",")+") VALUES "+query[:len(query)-1]+";", args...)
return sqlConn.ExecContext(ctx, "INSERT INTO "+dbName(model)+model.TableName()+" ("+strings.Join(columns, ",")+") VALUES "+query[:len(query)-1]+";", args...)

case sequel.AutoIncrKeyer:
_, idx, _ = vi.PK()
Expand All @@ -91,7 +89,7 @@ func Insert[T sequel.TableColumnValuer[T]](ctx context.Context, sqlConn sequel.D

var stmt = strpool.AcquireString()
defer strpool.ReleaseString(stmt)
stmt.WriteString("INSERT INTO " + model.TableName() + " (" + strings.Join(columns, ",") + ") VALUES ")
stmt.WriteString("INSERT INTO " + dbName(model) + model.TableName() + " (" + strings.Join(columns, ",") + ") VALUES ")
for i := range data {
if i > 0 {
stmt.WriteString(",(")
Expand Down Expand Up @@ -153,13 +151,13 @@ func UpsertOne[T sequel.KeyValuer[T], Ptr sequel.KeyValueScanner[T]](ctx context
defer strpool.ReleaseString(stmt)
{{ if eq driver "mysql" -}}
if !override {
stmt.WriteString("INSERT IGNORE INTO " + model.TableName() + " (" + strings.Join(columns, ",") + ") VALUES (" + strings.Repeat(",?", len(columns))[1:] + ");")
stmt.WriteString("INSERT IGNORE INTO " + dbName(model) + model.TableName() + " (" + strings.Join(columns, ",") + ") VALUES (" + strings.Repeat(",?", len(columns))[1:] + ");")
} else {
dict := make(map[string]struct{})
for i := range omittedFields {
dict[omittedFields[i]] = struct{}{}
}
stmt.WriteString("INSERT INTO " + model.TableName() + " (" + strings.Join(columns, ",") + ") VALUES (" + strings.Repeat(",?", len(columns))[1:] + ") ON DUPLICATE KEY UPDATE ")
stmt.WriteString("INSERT INTO " + dbName(model) + model.TableName() + " (" + strings.Join(columns, ",") + ") VALUES (" + strings.Repeat(",?", len(columns))[1:] + ") ON DUPLICATE KEY UPDATE ")
columns = append(columns[:idx], columns[idx+1:]...)
args = append(args[:idx], args[idx+1:]...)
noOfCols := len(columns)
Expand All @@ -177,7 +175,7 @@ func UpsertOne[T sequel.KeyValuer[T], Ptr sequel.KeyValueScanner[T]](ctx context
}
{{ else -}}
noOfCols := len(columns)
stmt.WriteString("INSERT INTO " + model.TableName() + " (" + strings.Join(columns, ",") + ") VALUES ")
stmt.WriteString("INSERT INTO " + dbName(model) + model.TableName() + " (" + strings.Join(columns, ",") + ") VALUES ")
for j := 0; j < noOfCols; j++ {
if j > 0 {
stmt.WriteByte(',')
Expand All @@ -201,8 +199,15 @@ func UpsertOne[T sequel.KeyValuer[T], Ptr sequel.KeyValueScanner[T]](ctx context
stmt.WriteString(columns[i] + " = EXCLUDED." + columns[i])
}
}
stmt.WriteByte(';')
clear(dict)
{{- /* postgres */ -}}
{{ if eq driver "postgres" }}
stmt.WriteString(" RETURNING "+ strings.Join(model.Columns(), ",") +";")
if err := sqlConn.QueryRowContext(ctx, stmt.String(), args...).Scan(model.Addrs()...); err != nil {
return nil, err
}
return sequel.NewRowsAffectedResult(1), nil
{{ end -}}
} else {
stmt.WriteString(" DO NOTHING;")
}
Expand All @@ -211,7 +216,7 @@ func UpsertOne[T sequel.KeyValuer[T], Ptr sequel.KeyValueScanner[T]](ctx context
}

// Upsert is a helper function to upsert multiple records.
func Upsert[T sequel.KeyValuer[T]](ctx context.Context, sqlConn sequel.DB, data []T, override bool, omittedFields ...string) (sql.Result, error) {
func Upsert[T sequel.KeyValuer[T], Ptr sequel.Scanner[T]](ctx context.Context, sqlConn sequel.DB, data []T, override bool, omittedFields ...string) (sql.Result, error) {
n := len(data)
if n == 0 {
return new(sequel.EmptyResult), nil
Expand All @@ -235,12 +240,12 @@ func Upsert[T sequel.KeyValuer[T]](ctx context.Context, sqlConn sequel.DB, data
defer strpool.ReleaseString(stmt)
{{ if eq driver "mysql" -}}
if override {
stmt.WriteString("INSERT INTO " + model.TableName() + " (" + strings.Join(columns, ",") + ") VALUES ")
stmt.WriteString("INSERT INTO " + dbName(model) + model.TableName() + " (" + strings.Join(columns, ",") + ") VALUES ")
} else {
stmt.WriteString("INSERT IGNORE INTO " + model.TableName() + " (" + strings.Join(columns, ",") + ") VALUES ")
stmt.WriteString("INSERT IGNORE INTO " + dbName(model) + model.TableName() + " (" + strings.Join(columns, ",") + ") VALUES ")
}
{{ else -}}
stmt.WriteString("INSERT INTO " + model.TableName() + " (" + strings.Join(columns, ",") + ") VALUES ")
stmt.WriteString("INSERT INTO " + dbName(model) + model.TableName() + " (" + strings.Join(columns, ",") + ") VALUES ")
{{ end -}}
for i := range data {
if i > 0 {
Expand Down Expand Up @@ -291,8 +296,9 @@ func Upsert[T sequel.KeyValuer[T]](ctx context.Context, sqlConn sequel.DB, data
}
stmt.WriteByte(';')
{{ else -}}
{{- /* postgres */ -}}
if dupKey, ok := any(model).(sequel.DuplicateKeyer); ok {
stmt.WriteString(" ON CONFLICT(" + dupKey.OnDuplicateKey() + ")")
stmt.WriteString(" ON CONFLICT(" + strings.Join(dupKey.OnDuplicateKey(), ",") + ")")
} else {
stmt.WriteString(" ON CONFLICT(" + pkName + ")")
}
Expand All @@ -313,7 +319,20 @@ func Upsert[T sequel.KeyValuer[T]](ctx context.Context, sqlConn sequel.DB, data
}
}
clear(dict)
stmt.WriteByte(';')
stmt.WriteString(" RETURNING " + strings.Join(model.Columns(), ",") + ";")
rows, err := sqlConn.QueryContext(ctx, stmt.String(), args...)
if err != nil {
return nil, err
}
defer rows.Close()
var i int64
for rows.Next() {
if err := rows.Scan(Ptr(&data[i]).Addrs()...); err != nil {
return nil, err
}
i++
}
return sequel.NewRowsAffectedResult(i + 1), nil
} else {
stmt.WriteString(" DO NOTHING;")
}
Expand All @@ -333,7 +352,7 @@ func FindByPK[T sequel.KeyValuer[T], Ptr sequel.KeyValueScanner[T]](ctx context.
pkName, _, pk = model.PK()
columns = model.Columns()
)
return sqlConn.QueryRowContext(ctx, "SELECT "+strings.Join(columns, ",")+" FROM "+model.TableName()+" WHERE "+pkName+" = {{ quoteVar 1 }} LIMIT 1;", pk).Scan(model.Addrs()...)
return sqlConn.QueryRowContext(ctx, "SELECT "+strings.Join(columns, ",")+" FROM "+dbName(model)+model.TableName()+" WHERE "+pkName+" = {{ quoteVar 1 }} LIMIT 1;", pk).Scan(model.Addrs()...)
}
}

Expand All @@ -352,11 +371,11 @@ func UpdateByPK[T sequel.KeyValuer[T]](ctx context.Context, sqlConn sequel.DB, m
columns = append(columns[:idx], columns[idx+1:]...)
values = append(values[:idx], values[idx+1:]...)
{{ if isStaticVar -}}
return sqlConn.ExecContext(ctx, "UPDATE "+model.TableName()+" SET "+strings.Join(columns, " = {{ quoteVar 1 }},")+" = {{ quoteVar 1 }} WHERE "+pkName+" = {{ quoteVar 1 }};", append(values, pk)...)
return sqlConn.ExecContext(ctx, "UPDATE "+dbName(model)+model.TableName()+" SET "+strings.Join(columns, " = {{ quoteVar 1 }},")+" = {{ quoteVar 1 }} WHERE "+pkName+" = {{ quoteVar 1 }};", append(values, pk)...)
{{ else -}}
stmt := strpool.AcquireString()
defer strpool.ReleaseString(stmt)
stmt.WriteString("UPDATE "+ model.TableName()+ " SET ")
stmt.WriteString("UPDATE "+dbName(model)+model.TableName()+" SET ")
for idx := range columns {
if idx > 0 {
stmt.WriteByte(',')
Expand All @@ -372,32 +391,7 @@ func UpdateByPK[T sequel.KeyValuer[T]](ctx context.Context, sqlConn sequel.DB, m
// DeleteByPK is to update single record using primary key.
func DeleteByPK[T sequel.KeyValuer[T]](ctx context.Context, sqlConn sequel.DB, model T) (sql.Result, error) {
pkName, _, pk := model.PK()
return sqlConn.ExecContext(ctx, "DELETE FROM "+model.TableName()+" WHERE "+pkName+" = {{ quoteVar 1 }};", pk)
}

// Migrate is to create or alter the table based on the defined schemas.
func Migrate[T sequel.Migrator](ctx context.Context, sqlConn sequel.DB) error {
var (
v T
table string
tableExists bool
)
tableName, _ := strconv.Unquote(v.TableName())
if err := sqlConn.QueryRowContext(ctx, _getTableSQL, tableName).Scan(&table); err != nil {
tableExists = false
} else {
tableExists = true
}
if tableExists {
if _, err := sqlConn.ExecContext(ctx, v.AlterTableStmt()); err != nil {
return err
}
return nil
}
if _, err := sqlConn.ExecContext(ctx, v.CreateTableStmt()); err != nil {
return err
}
return nil
return sqlConn.ExecContext(ctx, "DELETE FROM "+ dbName(model) + model.TableName() +" WHERE "+ pkName +" = {{ quoteVar 1 }};", pk)
}

type SelectStmt struct {
Expand All @@ -419,7 +413,7 @@ func QueryStmt[T any, Ptr interface {
*T
sequel.Scanner[T]
}, Stmt interface{
SelectStmt | SQLStatement | string
SelectStmt | SQLStatement
}](ctx context.Context, sqlConn sequel.DB, stmt Stmt) ([]T, error) {
var (
rows *sql.Rows
Expand All @@ -445,11 +439,11 @@ func QueryStmt[T any, Ptr interface {
}
}
if vi.FromTable != "" {
blr.WriteString(" FROM " + vi.FromTable)
blr.WriteString(" FROM " + dbName(v) + vi.FromTable)
} else {
switch vj := any(v).(type) {
case sequel.Tabler:
blr.WriteString(" FROM " + vj.TableName())
blr.WriteString(" FROM " + dbName(v) + vj.TableName())
default:
return nil, fmt.Errorf("missing table name for model %T", v)
}
Expand Down Expand Up @@ -488,9 +482,6 @@ func QueryStmt[T any, Ptr interface {

case SQLStatement:
rows, err = sqlConn.QueryContext(ctx, vi.Query, vi.Arguments...)

case string:
rows, err = sqlConn.QueryContext(ctx, vi)
}
if err != nil {
return nil, err
Expand Down Expand Up @@ -536,9 +527,9 @@ func ExecStmt[T any, Stmt interface {
switch vi := any(stmt).(type) {
case UpdateStmt:
if vt, ok := any(v).(sequel.Tabler); ok {
blr.WriteString("UPDATE " + vt.TableName())
blr.WriteString("UPDATE " + dbName(v) + vt.TableName())
} else {
blr.WriteString("UPDATE " + vi.Table)
blr.WriteString("UPDATE " + dbName(v) + vi.Table)
}
if vi.Where != nil {
blr.WriteString(" WHERE ")
Expand Down Expand Up @@ -568,9 +559,9 @@ func ExecStmt[T any, Stmt interface {

case DeleteStmt:
if vt, ok := any(v).(sequel.Tabler); ok {
blr.WriteString("DELETE FROM " + vt.TableName())
blr.WriteString("DELETE FROM " + dbName(v) + vt.TableName())
} else {
blr.WriteString("DELETE FROM " + vi.FromTable)
blr.WriteString("DELETE FROM " + dbName(v) + vi.FromTable)
}
if vi.Where != nil {
blr.WriteString(" WHERE ")
Expand Down Expand Up @@ -655,6 +646,13 @@ func (s *sqlStmt) Reset() {
s.Builder.Reset()
}

func dbName(model any) string {
if v, ok := model.(sequel.DatabaseNamer); ok {
return v.DatabaseName() + "."
}
return ""
}

{{ if not isStaticVar -}}
func wrapVar(i int) string {
return {{ quote varRune }}+ strconv.Itoa(i)
Expand Down
19 changes: 7 additions & 12 deletions codegen/templates/init.yml.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,31 @@ src:
- {{ . }}
{{- end }}

# Optional:
# Possibly values : `mysql`, `postgres` or `sqlite`
# Optional: possibly values : `mysql`, `postgres` or `sqlite`
driver: {{ .Driver }}

# Optional:
# Possibly values : `snake_case`, `camelCase` or `PascalCase`
# Optional: possibly values : `snake_case`, `camelCase` or `PascalCase`
naming_convention: {{ .NamingConvention }}

# Optional:
# The struct tag for "sqlgen" to read from. Default is `sql`
# Optional: the struct tag for "sqlgen" to read from. Default is `sql`
struct_tag: {{ .Tag }}

# Optional:
# Whether to omit the quote on table name and column name
# Optional: whether to omit the quote on table name and column name
omit_quote_identifier: {{ .OmitQuoteIdentifier }}

# Optional:
# Whether to omit the getters
# Optional: whether to omit the getters
omit_getters: {{ .OmitGetters }}

# Optional: to add prefix to getter
getter:
prefix: {{ .Getter.Prefix }}

# Optional: Where should the generated model code go?
# Optional: where should the generated model code go?
exec:
skip_empty: {{ .Exec.SkipEmpty }}
filename: {{ .Exec.Filename }}

# Optional: Where should the generated database code go?
# Optional: where should the generated database code go?
database:
dir: {{ .Database.Dir }}
package: {{ .Database.Package }}
Expand Down
2 changes: 1 addition & 1 deletion codegen/templates/model.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (v {{ $structName }}) PK() (columnName string, pos int, value driver.Value)
return {{ quote (quoteIdentifier .PK.Field.ColumnName) }}, {{ .PK.Field.Index }}, {{ castAs .PK.Field }}
}
{{ if (and (not $hasCustomTabler) ($hasNotOnlyPK)) -}}
func (v {{ $structName }}) FindByPKStmt() string {
func ({{ $structName }}) FindByPKStmt() string {
return {{ findByPKStmt . }}
}
{{ end -}}
Expand Down
Loading