Expand typed SQL DSL

This commit is contained in:
pavel 2026-08-28 00:02:32 +02:00
commit 314c8fb207
17 changed files with 1082 additions and 498 deletions

View file

@ -17,6 +17,11 @@ type sqlLowered struct {
hasResult bool
}
type sqlSourceRef struct {
class ClassDecl
alias string
}
func splitSQLChain(expr Expr) (CallExpr, string, []sqlCallStep, bool) {
var reversed []sqlCallStep
current := expr
@ -31,7 +36,7 @@ func splitSQLChain(expr Expr) (CallExpr, string, []sqlCallStep, bool) {
}
if receiver, ok := selector.Receiver.(IdentExpr); ok && receiver.Name == "sql" {
switch selector.Name {
case "from", "insert", "update", "delete":
case "from", "insert", "insertAll", "update", "delete":
default:
return CallExpr{}, "", nil, false
}
@ -78,9 +83,11 @@ func (g *goGenerator) lowerSQLQuery(expr Expr) (string, bool, error) {
var err error
switch operation {
case "from":
lowered, err = g.lowerSQLSelect(root, steps)
lowered, err = g.lowerSQLSelectExpanded(root, steps)
case "insert":
lowered, err = g.lowerSQLInsert(root, steps)
case "insertAll":
lowered, err = g.lowerSQLBulkInsert(root, steps)
case "update":
lowered, err = g.lowerSQLUpdate(root, steps)
case "delete":
@ -188,7 +195,7 @@ func sqlProjectionType(call CallExpr, rowType string) (string, bool) {
}
rowName := "it"
if !lambda.ImplicitIt {
if len(lambda.Params) != 1 {
if len(lambda.Params) == 0 {
return "", false
}
rowName = lambda.Params[0].Name
@ -337,6 +344,170 @@ func (g *goGenerator) lowerSQLSelect(root CallExpr, steps []sqlCallStep) (sqlLow
return sqlLowered{value: sqlQueryValue(query, args), result: result, hasResult: true}, nil
}
func (g *goGenerator) lowerSQLSelectExpanded(root CallExpr, steps []sqlCallStep) (sqlLowered, error) {
if len(root.Args) != 0 || len(root.NamedArgs) != 0 {
return sqlLowered{}, fmt.Errorf("sql.from does not accept value arguments")
}
class, err := g.sqlClass(root, "from")
if err != nil {
return sqlLowered{}, err
}
sources := []sqlSourceRef{{class: class}}
result := class
columns := sqlScopedAllColumns(sources[0])
var joins []string
var where, group, having, order, limit, offset string
var args []string
seenAlias, seenSelect, seenWhere, seenGroup, seenHaving, seenOrder, seenLimit, seenOffset := false, false, false, false, false, false, false, false
seenForUpdate, seenSkipLocked := false, false
stage := 0
for _, step := range steps {
switch step.name {
case "alias":
if seenAlias || stage != 0 {
return sqlLowered{}, fmt.Errorf("alias() may appear once before joins")
}
alias, err := sqlAliasArgument(step.call, "alias")
if err != nil {
return sqlLowered{}, err
}
sources[0].alias = alias
columns = sqlScopedAllColumns(sources[0])
seenAlias = true
case "join", "leftJoin", "rightJoin":
if stage > 0 {
return sqlLowered{}, fmt.Errorf("joins must appear before select() and where()")
}
joined, clause, joinArgs, err := g.sqlJoin(step, sources, len(args)+1)
if err != nil {
return sqlLowered{}, err
}
sources = append(sources, joined)
joins = append(joins, clause)
args = append(args, joinArgs...)
columns = sqlScopedAllColumns(sources[0])
case "select":
if seenSelect || stage > 0 {
return sqlLowered{}, fmt.Errorf("select() may appear once after joins and before filtering")
}
result, columns, err = g.sqlProjectionScoped(step.call, sources, "select")
if err != nil {
return sqlLowered{}, err
}
seenSelect, stage = true, 1
case "where":
if seenWhere || stage > 2 {
return sqlLowered{}, fmt.Errorf("where() may appear once before grouping")
}
where, args, err = g.sqlWhereScoped(step.call, sources, len(args)+1, args)
if err != nil {
return sqlLowered{}, err
}
seenWhere, stage = true, 2
case "groupBy":
if seenGroup || stage > 3 {
return sqlLowered{}, fmt.Errorf("groupBy() may appear once before having() and ordering")
}
group, err = g.sqlGroupBy(step.call, sources)
if err != nil {
return sqlLowered{}, err
}
seenGroup, stage = true, 3
case "having":
if seenHaving || !seenGroup || stage > 4 {
return sqlLowered{}, fmt.Errorf("having() may appear once after groupBy()")
}
var havingArgs []string
having, havingArgs, err = g.sqlPredicateLambda(step.call, sources, "having", len(args)+1)
if err != nil {
return sqlLowered{}, err
}
args = append(args, havingArgs...)
seenHaving, stage = true, 4
case "orderBy", "orderByDescending":
if seenOrder || stage > 5 {
return sqlLowered{}, fmt.Errorf("ordering may appear once before limit() and offset()")
}
order, err = g.sqlOrderScoped(step, sources)
if err != nil {
return sqlLowered{}, err
}
seenOrder, stage = true, 5
case "limit", "offset":
if seenForUpdate || seenSkipLocked {
return sqlLowered{}, fmt.Errorf("%s() may appear once before forUpdate()", step.name)
}
if step.name == "limit" && seenLimit {
return sqlLowered{}, fmt.Errorf("limit() may appear once")
}
if step.name == "offset" && seenOffset {
return sqlLowered{}, fmt.Errorf("offset() may appear once")
}
value, valueArgs, err := g.sqlNonNegativeInt(step.call, step.name)
if err != nil {
return sqlLowered{}, err
}
if value == "?" {
value = "$" + strconv.Itoa(len(args)+1)
}
args = append(args, valueArgs...)
if step.name == "limit" {
limit, seenLimit = value, true
} else {
offset, seenOffset = value, true
}
stage = 6
case "forUpdate":
if seenForUpdate || !sqlNoArgs(step.call) {
return sqlLowered{}, fmt.Errorf("forUpdate() accepts no arguments and may appear once after offset()")
}
seenForUpdate = true
stage = 7
case "skipLocked":
if seenSkipLocked || !seenForUpdate || !sqlNoArgs(step.call) {
return sqlLowered{}, fmt.Errorf("skipLocked() accepts no arguments and requires a preceding forUpdate()")
}
seenSkipLocked = true
stage = 8
default:
return sqlLowered{}, fmt.Errorf("unsupported sql.from method %q", step.name)
}
}
from := class.Table
if sources[0].alias != "" {
from += " AS " + sources[0].alias
}
query := "SELECT " + strings.Join(columns, ", ") + " FROM " + from
if len(joins) > 0 {
query += " " + strings.Join(joins, " ")
}
if where != "" {
query += " WHERE " + where
}
if group != "" {
query += " GROUP BY " + group
}
if having != "" {
query += " HAVING " + having
}
if order != "" {
query += " ORDER BY " + order
}
if limit != "" {
query += " LIMIT " + limit
}
if offset != "" {
query += " OFFSET " + offset
}
if seenForUpdate {
query += " FOR UPDATE"
}
if seenSkipLocked {
query += " SKIP LOCKED"
}
return sqlLowered{value: sqlQueryValue(query, args), result: result, hasResult: true}, nil
}
func (g *goGenerator) lowerSQLInsert(root CallExpr, steps []sqlCallStep) (sqlLowered, error) {
if len(root.Args) != 1 || len(root.NamedArgs) != 0 {
return sqlLowered{}, fmt.Errorf("sql.insert expects exactly one row argument")
@ -437,6 +608,41 @@ func (g *goGenerator) lowerSQLInsert(root CallExpr, steps []sqlCallStep) (sqlLow
return lowered, nil
}
func (g *goGenerator) lowerSQLBulkInsert(root CallExpr, steps []sqlCallStep) (sqlLowered, error) {
if len(root.Args) != 1 || len(root.NamedArgs) != 0 {
return sqlLowered{}, fmt.Errorf("sql.insertAll expects exactly one row list")
}
if len(steps) != 0 {
return sqlLowered{}, fmt.Errorf("sql.insertAll currently ends directly with build()")
}
class, err := g.sqlClass(root, "insertAll")
if err != nil {
return sqlLowered{}, err
}
typ := g.exprType(root.Args[0])
base, args, ok := parseGenericType(typ)
if !ok || (base != "List" && base != "MutableList") || len(args) != 1 || strings.TrimPrefix(args[0], "*") != class.Name {
return sqlLowered{}, fmt.Errorf("sql.insertAll<%s> requires List<%s>, got %s", class.Name, class.Name, typ)
}
rows, err := g.expr(root.Args[0], "List<"+class.Name+">")
if err != nil {
return sqlLowered{}, err
}
var columns, values []string
for _, field := range class.Fields {
if field.Generated {
continue
}
columns = append(columns, strconv.Quote(sqlColumn(field)))
values = append(values, "row."+mappingFieldName(class, field))
}
if len(columns) == 0 {
return sqlLowered{}, fmt.Errorf("sql.insertAll does not support rows containing only generated fields")
}
value := fmt.Sprintf("gotlinSQLBulkInsert[%s](%s, %q, []string{%s}, func(row *%s) []any { return []any{%s} })", class.Name, rows, class.Table, strings.Join(columns, ", "), class.Name, strings.Join(values, ", "))
return sqlLowered{value: value}, nil
}
func (g *goGenerator) lowerSQLUpdate(root CallExpr, steps []sqlCallStep) (sqlLowered, error) {
if len(root.Args) != 0 || len(root.NamedArgs) != 0 {
return sqlLowered{}, fmt.Errorf("sql.update does not accept value arguments")
@ -541,6 +747,370 @@ func sqlAllColumns(class ClassDecl) []string {
return columns
}
func sqlScopedAllColumns(source sqlSourceRef) []string {
columns := make([]string, 0, len(source.class.Fields))
for _, field := range source.class.Fields {
columns = append(columns, sqlQualifiedColumn(source, field))
}
return columns
}
func sqlQualifiedColumn(source sqlSourceRef, field FieldDecl) string {
column := sqlColumn(field)
if source.alias != "" {
return source.alias + "." + column
}
return column
}
func sqlAliasArgument(call CallExpr, method string) (string, error) {
if len(call.Args) != 1 || len(call.TypeArgs) != 0 || len(call.NamedArgs) != 0 {
return "", fmt.Errorf("%s() expects one alias string", method)
}
literal, ok := call.Args[0].(StringExpr)
if !ok {
return "", fmt.Errorf("%s() alias must be a string literal", method)
}
value, err := strconv.Unquote(literal.Value)
if err != nil || !validSQLName(value) {
return "", fmt.Errorf("invalid SQL alias %q", value)
}
return value, nil
}
func (g *goGenerator) sqlJoin(step sqlCallStep, existing []sqlSourceRef, placeholderStart int) (sqlSourceRef, string, []string, error) {
if len(step.call.TypeArgs) != 1 || len(step.call.NamedArgs) != 0 {
return sqlSourceRef{}, "", nil, fmt.Errorf("%s expects one joined row type", step.name)
}
joined, ok := g.semantic.Classes[step.call.TypeArgs[0]]
if !ok || !joined.Data || joined.Table == "" {
return sqlSourceRef{}, "", nil, fmt.Errorf("joined row %s must be a @table data class", step.call.TypeArgs[0])
}
if len(step.call.Args) < 1 || len(step.call.Args) > 2 {
return sqlSourceRef{}, "", nil, fmt.Errorf("%s expects an optional alias and a predicate lambda", step.name)
}
lambda, ok := step.call.Args[len(step.call.Args)-1].(LambdaExpr)
if !ok {
return sqlSourceRef{}, "", nil, fmt.Errorf("%s expects a predicate lambda", step.name)
}
alias := joined.Table
if len(step.call.Args) == 2 {
literal, ok := step.call.Args[0].(StringExpr)
if !ok {
return sqlSourceRef{}, "", nil, fmt.Errorf("%s alias must be a string literal", step.name)
}
var err error
alias, err = strconv.Unquote(literal.Value)
if err != nil || !validSQLName(alias) {
return sqlSourceRef{}, "", nil, fmt.Errorf("invalid SQL alias %q", alias)
}
}
for _, current := range existing {
currentAlias := current.alias
if currentAlias == "" {
currentAlias = current.class.Table
}
if currentAlias == alias {
return sqlSourceRef{}, "", nil, fmt.Errorf("duplicate SQL alias %q", alias)
}
}
source := sqlSourceRef{class: joined, alias: alias}
sources := append(append([]sqlSourceRef{}, existing...), source)
scope, body, err := sqlScopedLambda(lambda, sources, step.name)
if err != nil {
return sqlSourceRef{}, "", nil, err
}
predicate, args, err := g.sqlPredicateScoped(body, scope)
if err != nil {
return sqlSourceRef{}, "", nil, err
}
joinType := map[string]string{"join": "INNER JOIN", "leftJoin": "LEFT JOIN", "rightJoin": "RIGHT JOIN"}[step.name]
clause := joinType + " " + joined.Table
if alias != joined.Table {
clause += " AS " + alias
}
clause += " ON " + postgresPlaceholders(predicate, placeholderStart)
return source, clause, args, nil
}
func sqlScopedLambda(lambda LambdaExpr, sources []sqlSourceRef, method string) (map[string]sqlSourceRef, Expr, error) {
scope := map[string]sqlSourceRef{}
if lambda.ImplicitIt {
if len(sources) != 1 {
return nil, nil, fmt.Errorf("%s lambda requires %d row parameters", method, len(sources))
}
scope["it"] = sources[0]
} else {
if len(lambda.Params) != len(sources) {
return nil, nil, fmt.Errorf("%s lambda requires %d row parameters", method, len(sources))
}
for index, param := range lambda.Params {
if param.Type != "" && param.Type != sources[index].class.Name {
return nil, nil, fmt.Errorf("%s parameter %s must have type %s", method, param.Name, sources[index].class.Name)
}
scope[param.Name] = sources[index]
}
}
if len(lambda.Body) != 1 {
return nil, nil, fmt.Errorf("%s lambda must contain one expression", method)
}
statement, ok := lambda.Body[0].(ExprStmt)
if !ok {
return nil, nil, fmt.Errorf("%s lambda must contain one expression", method)
}
return scope, statement.Value, nil
}
func (g *goGenerator) sqlProjectionScoped(call CallExpr, sources []sqlSourceRef, method string) (ClassDecl, []string, error) {
lambda, err := sqlLambdaArg(call, method)
if err != nil {
return ClassDecl{}, nil, err
}
scope, body, err := sqlScopedLambda(lambda, sources, method)
if err != nil {
return ClassDecl{}, nil, err
}
if ident, ok := body.(IdentExpr); ok {
if source, found := scope[ident.Name]; found {
return source.class, sqlScopedAllColumns(source), nil
}
}
constructor, ok := body.(CallExpr)
if !ok || len(constructor.TypeArgs) != 0 || len(constructor.NamedArgs) != 0 {
return ClassDecl{}, nil, fmt.Errorf("%s expects Projection(row.field, aggregate(...))", method)
}
callee, ok := constructor.Callee.(IdentExpr)
if !ok {
return ClassDecl{}, nil, fmt.Errorf("%s projection must construct a local data class", method)
}
projection, ok := g.semantic.Classes[callee.Name]
if !ok || !projection.Data {
return ClassDecl{}, nil, fmt.Errorf("%s projection type %s must be a data class", method, callee.Name)
}
if len(constructor.Args) != len(projection.Fields) {
return ClassDecl{}, nil, fmt.Errorf("%s projection %s expects %d fields, got %d", method, projection.Name, len(projection.Fields), len(constructor.Args))
}
columns := make([]string, len(constructor.Args))
for index, argument := range constructor.Args {
operand, err := g.sqlOperandScoped(argument, scope)
if err != nil {
return ClassDecl{}, nil, fmt.Errorf("%s projection argument %d: %w", method, index+1, err)
}
if len(operand.args) != 0 {
return ClassDecl{}, nil, fmt.Errorf("%s projection arguments must be fields or aggregates", method)
}
if !sqlProjectionTypesCompatible(projection.Fields[index].Type, operand.typ) {
return ClassDecl{}, nil, fmt.Errorf("%s projection field %s has type %s but expression has type %s", method, projection.Fields[index].Name, projection.Fields[index].Type, operand.typ)
}
columns[index] = operand.sql
}
return projection, columns, nil
}
func (g *goGenerator) sqlWhereScoped(call CallExpr, sources []sqlSourceRef, placeholderStart int, existing []string) (string, []string, error) {
predicate, args, err := g.sqlPredicateLambda(call, sources, "where", placeholderStart)
return predicate, append(existing, args...), err
}
func (g *goGenerator) sqlPredicateLambda(call CallExpr, sources []sqlSourceRef, method string, placeholderStart int) (string, []string, error) {
lambda, err := sqlLambdaArg(call, method)
if err != nil {
return "", nil, err
}
scope, body, err := sqlScopedLambda(lambda, sources, method)
if err != nil {
return "", nil, err
}
predicate, args, err := g.sqlPredicateScoped(body, scope)
if err != nil {
return "", nil, err
}
return postgresPlaceholders(predicate, placeholderStart), args, nil
}
func (g *goGenerator) sqlPredicateScoped(expr Expr, scope map[string]sqlSourceRef) (string, []string, error) {
switch value := expr.(type) {
case BinaryExpr:
if value.Op == "&&" || value.Op == "||" {
left, leftArgs, err := g.sqlPredicateScoped(value.Left, scope)
if err != nil {
return "", nil, err
}
right, rightArgs, err := g.sqlPredicateScoped(value.Right, scope)
if err != nil {
return "", nil, err
}
op := "AND"
if value.Op == "||" {
op = "OR"
}
return "(" + left + " " + op + " " + right + ")", append(leftArgs, rightArgs...), nil
}
if !isSQLComparison(value.Op) {
return "", nil, fmt.Errorf("unsupported SQL predicate operator %q", value.Op)
}
left, err := g.sqlOperandScoped(value.Left, scope)
if err != nil {
return "", nil, err
}
right, err := g.sqlOperandScoped(value.Right, scope)
if err != nil {
return "", nil, err
}
if left.typ == "Null" || right.typ == "Null" {
if value.Op != "==" && value.Op != "!=" {
return "", nil, fmt.Errorf("null only supports == and != in SQL predicates")
}
operand := left
if operand.typ == "Null" {
operand = right
}
if !sqlNullableType(operand.typ) {
return "", nil, fmt.Errorf("SQL null comparison requires a nullable operand, got %s", operand.typ)
}
op := "IS NULL"
if value.Op == "!=" {
op = "IS NOT NULL"
}
return operand.sql + " " + op, operand.args, nil
}
if !sqlTypesCompatible(left.typ, right.typ) {
return "", nil, fmt.Errorf("SQL predicate compares incompatible types %s and %s", left.typ, right.typ)
}
if value.Op == ">" || value.Op == ">=" || value.Op == "<" || value.Op == "<=" {
if !(sqlNumericType(left.typ) && sqlNumericType(right.typ)) && !(sqlTimestampType(left.typ) && sqlTimestampType(right.typ)) {
return "", nil, fmt.Errorf("SQL ordering comparison requires numeric operands or timestamp operands, got %s and %s", left.typ, right.typ)
}
}
op := map[string]string{"==": "=", "!=": "<>", ">": ">", ">=": ">=", "<": "<", "<=": "<="}[value.Op]
return left.sql + " " + op + " " + right.sql, append(left.args, right.args...), nil
case UnaryExpr:
if value.Op != "!" {
return "", nil, fmt.Errorf("unsupported SQL predicate unary operator %q", value.Op)
}
inner, args, err := g.sqlPredicateScoped(value.Value, scope)
return "(NOT " + inner + ")", args, err
case SelectorExpr:
operand, err := g.sqlOperandScoped(value, scope)
if err != nil {
return "", nil, err
}
if operand.typ != "Boolean" {
return "", nil, fmt.Errorf("SQL predicate field has type %s, not Boolean", operand.typ)
}
return operand.sql, nil, nil
default:
return "", nil, fmt.Errorf("SQL %s lambda must produce a Boolean predicate", "where/having")
}
}
func (g *goGenerator) sqlOperandScoped(expr Expr, scope map[string]sqlSourceRef) (sqlOperandValue, error) {
if selector, ok := expr.(SelectorExpr); ok {
if receiver, ok := selector.Receiver.(IdentExpr); ok {
if source, found := scope[receiver.Name]; found {
field, err := sqlField(source.class, selector.Name)
if err != nil {
return sqlOperandValue{}, err
}
return sqlOperandValue{sql: sqlQualifiedColumn(source, field), typ: field.Type}, nil
}
}
}
if call, ok := expr.(CallExpr); ok {
if ident, ok := call.Callee.(IdentExpr); ok {
if ident.Name == "count" && len(call.Args) == 0 {
return sqlOperandValue{sql: "COUNT(*)", typ: "Long"}, nil
}
if (ident.Name == "sum" || ident.Name == "avg" || ident.Name == "min" || ident.Name == "max" || ident.Name == "countDistinct") && len(call.Args) == 1 {
inner, err := g.sqlOperandScoped(call.Args[0], scope)
if err != nil {
return sqlOperandValue{}, err
}
if len(inner.args) != 0 {
return sqlOperandValue{}, fmt.Errorf("%s() requires a row field", ident.Name)
}
name := map[string]string{"sum": "SUM", "avg": "AVG", "min": "MIN", "max": "MAX", "countDistinct": "COUNT"}[ident.Name]
typ := inner.typ
sql := name + "(" + inner.sql + ")"
if ident.Name == "avg" {
typ = "Double"
}
if ident.Name == "countDistinct" {
typ = "Long"
sql = "COUNT(DISTINCT " + inner.sql + ")"
}
return sqlOperandValue{sql: sql, typ: typ}, nil
}
if ident.Name == "now" && sqlNoArgs(call) {
return sqlOperandValue{sql: "CURRENT_TIMESTAMP", typ: "time.Time"}, nil
}
}
}
if binary, ok := expr.(BinaryExpr); ok && (binary.Op == "+" || binary.Op == "-") {
left, err := g.sqlOperandScoped(binary.Left, scope)
if err != nil {
return sqlOperandValue{}, err
}
right, err := g.sqlOperandScoped(binary.Right, scope)
if err != nil {
return sqlOperandValue{}, err
}
return sqlOperandValue{sql: "(" + left.sql + " " + binary.Op + " " + right.sql + ")", typ: left.typ, args: append(left.args, right.args...)}, nil
}
return g.sqlOperand(expr, ClassDecl{}, "")
}
func (g *goGenerator) sqlGroupBy(call CallExpr, sources []sqlSourceRef) (string, error) {
lambda, err := sqlLambdaArg(call, "groupBy")
if err != nil {
return "", err
}
scope, body, err := sqlScopedLambda(lambda, sources, "groupBy")
if err != nil {
return "", err
}
expressions := []Expr{body}
if list, ok := body.(CallExpr); ok {
if ident, ok := list.Callee.(IdentExpr); ok && (ident.Name == "listOf" || ident.Name == "mutableListOf") {
expressions = list.Args
}
}
columns := make([]string, len(expressions))
for index, expr := range expressions {
operand, err := g.sqlOperandScoped(expr, scope)
if err != nil {
return "", err
}
if len(operand.args) != 0 {
return "", fmt.Errorf("groupBy() requires row fields")
}
columns[index] = operand.sql
}
return strings.Join(columns, ", "), nil
}
func (g *goGenerator) sqlOrderScoped(step sqlCallStep, sources []sqlSourceRef) (string, error) {
lambda, err := sqlLambdaArg(step.call, step.name)
if err != nil {
return "", err
}
scope, body, err := sqlScopedLambda(lambda, sources, step.name)
if err != nil {
return "", err
}
operand, err := g.sqlOperandScoped(body, scope)
if err != nil {
return "", err
}
if len(operand.args) != 0 {
return "", fmt.Errorf("ordering requires a row field or aggregate")
}
if step.name == "orderByDescending" {
return operand.sql + " DESC", nil
}
return operand.sql, nil
}
func (g *goGenerator) sqlProjection(call CallExpr, rowClass ClassDecl, method string) (ClassDecl, []string, error) {
lambda, err := sqlLambdaArg(call, method)
if err != nil {
@ -620,23 +1190,27 @@ func sqlOrder(step sqlCallStep, class ClassDecl) (string, error) {
}
func (g *goGenerator) sqlLimit(call CallExpr) (string, []string, error) {
return g.sqlNonNegativeInt(call, "limit")
}
func (g *goGenerator) sqlNonNegativeInt(call CallExpr, method string) (string, []string, error) {
if len(call.Args) != 1 || len(call.NamedArgs) != 0 || len(call.TypeArgs) != 0 {
return "", nil, fmt.Errorf("limit() expects exactly one Int argument")
return "", nil, fmt.Errorf("%s() expects exactly one Int argument", method)
}
if literal, ok := call.Args[0].(IntExpr); ok {
value, err := strconv.Atoi(literal.Value)
if err != nil || value < 0 {
return "", nil, fmt.Errorf("limit() requires a non-negative Int")
return "", nil, fmt.Errorf("%s() requires a non-negative Int", method)
}
return literal.Value, nil, nil
}
if unary, ok := call.Args[0].(UnaryExpr); ok && unary.Op == "-" {
if _, ok := unary.Value.(IntExpr); ok {
return "", nil, fmt.Errorf("limit() requires a non-negative Int")
return "", nil, fmt.Errorf("%s() requires a non-negative Int", method)
}
}
if typ := g.exprType(call.Args[0]); typ != "Int" {
return "", nil, fmt.Errorf("limit() argument must have type Int, got %s", typ)
return "", nil, fmt.Errorf("%s() argument must have type Int, got %s", method, typ)
}
value, err := g.expr(call.Args[0], "Int")
if err != nil {
@ -869,24 +1443,16 @@ func (g *goGenerator) sqlTypedUpdateAssignments(call CallExpr, class ClassDecl)
rowName = lambda.Params[0].Name
}
if len(lambda.Body) == 0 {
return nil, nil, fmt.Errorf("set lambda requires at least one set(row.field, value) expression")
return nil, nil, fmt.Errorf("set lambda requires at least one row.field = value assignment")
}
var assignments, args []string
seen := map[string]bool{}
for _, stmt := range lambda.Body {
exprStmt, ok := stmt.(ExprStmt)
assignment, ok := stmt.(FieldAssignStmt)
if !ok {
return nil, nil, fmt.Errorf("set lambda only supports set(row.field, value) expressions")
return nil, nil, fmt.Errorf("set lambda only supports %s.field = value assignments", rowName)
}
setCall, ok := exprStmt.Value.(CallExpr)
if !ok {
return nil, nil, fmt.Errorf("set lambda only supports set(row.field, value) expressions")
}
callee, ok := setCall.Callee.(IdentExpr)
if !ok || callee.Name != "set" || len(setCall.Args) != 2 || len(setCall.NamedArgs) != 0 || len(setCall.TypeArgs) != 0 {
return nil, nil, fmt.Errorf("set lambda expects set(%s.field, value)", rowName)
}
target, err := sqlRowField(setCall.Args[0], class, rowName)
target, err := sqlRowField(assignment.Target, class, rowName)
if err != nil {
return nil, nil, fmt.Errorf("set target: %w", err)
}
@ -896,7 +1462,7 @@ func (g *goGenerator) sqlTypedUpdateAssignments(call CallExpr, class ClassDecl)
if seen[target.Name] {
return nil, nil, fmt.Errorf("duplicate set target %s", target.Name)
}
value, err := g.sqlOperand(setCall.Args[1], class, rowName)
value, err := g.sqlOperand(assignment.Value, class, rowName)
if err != nil {
return nil, nil, fmt.Errorf("set value for %s: %w", target.Name, err)
}
@ -926,27 +1492,16 @@ func sqlConflictUpdateAssignments(lambda LambdaExpr, class ClassDecl) ([]string,
excludedName = lambda.Params[0].Name
}
if len(lambda.Body) == 0 {
return nil, fmt.Errorf("doUpdate lambda requires at least one set() expression")
return nil, fmt.Errorf("doUpdate lambda requires at least one Row.field = excluded.field assignment")
}
assignments := make([]string, 0, len(lambda.Body))
seen := map[string]bool{}
for _, stmt := range lambda.Body {
exprStmt, ok := stmt.(ExprStmt)
assignment, ok := stmt.(FieldAssignStmt)
if !ok {
return nil, fmt.Errorf("doUpdate only supports set() expressions")
}
call, ok := exprStmt.Value.(CallExpr)
if !ok {
return nil, fmt.Errorf("doUpdate only supports set() expressions")
}
callee, ok := call.Callee.(IdentExpr)
if !ok || callee.Name != "set" || len(call.Args) != 2 || len(call.NamedArgs) != 0 || len(call.TypeArgs) != 0 {
return nil, fmt.Errorf("doUpdate expects set(%s.field, %s.field)", class.Name, excludedName)
}
targetRef, ok := call.Args[0].(SelectorExpr)
if !ok {
return nil, fmt.Errorf("set target must be %s.field", class.Name)
return nil, fmt.Errorf("doUpdate only supports %s.field = %s.field assignments", class.Name, excludedName)
}
targetRef := assignment.Target
targetType, ok := targetRef.Receiver.(IdentExpr)
if !ok || targetType.Name != class.Name {
return nil, fmt.Errorf("set target must be %s.field", class.Name)
@ -961,7 +1516,7 @@ func sqlConflictUpdateAssignments(lambda LambdaExpr, class ClassDecl) ([]string,
if seen[target.Name] {
return nil, fmt.Errorf("duplicate set target %s", target.Name)
}
sourceRef, ok := call.Args[1].(SelectorExpr)
sourceRef, ok := assignment.Value.(SelectorExpr)
if !ok {
return nil, fmt.Errorf("set value must be %s.field", excludedName)
}
@ -1117,6 +1672,13 @@ func programContainsSQLExecution(program *Program) bool {
})
}
func programContainsSQLBulk(program *Program) bool {
return programExprMatches(program, func(expr Expr) bool {
_, operation, _, ok := splitSQLChain(expr)
return ok && operation == "insertAll"
})
}
func exprMatches(expr Expr, match func(Expr) bool) bool {
if match(expr) {
return true