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

Add As() starter function to dialects #117

Merged
merged 1 commit into from
Oct 19, 2023
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
30 changes: 18 additions & 12 deletions dialect/mysql/starters.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var bmod = expr.Builder[Expression, Expression]{}
// F creates a function expression with the given name and args
//
// SQL: generate_series(1, 3)
// Go: psql.F("generate_series", 1, 3)
// Go: mysql.F("generate_series", 1, 3)
func F(name string, args ...any) *dialect.Function {
f := dialect.NewFunction(name, args...)

Expand All @@ -27,67 +27,73 @@ func F(name string, args ...any) *dialect.Function {

// S creates a string literal
// SQL: 'a string'
// Go: psql.S("a string")
// Go: mysql.S("a string")
func S(s string) Expression {
return bmod.S(s)
}

// SQL: NOT true
// Go: psql.Not("true")
// Go: mysql.Not("true")
func Not(exp bob.Expression) Expression {
return bmod.Not(exp)
}

// SQL: a OR b OR c
// Go: psql.Or("a", "b", "c")
// Go: mysql.Or("a", "b", "c")
func Or(args ...bob.Expression) Expression {
return bmod.Or(args...)
}

// SQL: a AND b AND c
// Go: psql.And("a", "b", "c")
// Go: mysql.And("a", "b", "c")
func And(args ...bob.Expression) Expression {
return bmod.And(args...)
}

// SQL: a || b || c
// Go: psql.Concat("a", "b", "c")
// Go: mysql.Concat("a", "b", "c")
func Concat(args ...bob.Expression) Expression {
return expr.X[Expression, Expression](expr.Join{Exprs: args, Sep: " || "})
}

// SQL: $1, $2, $3
// Go: psql.Args("a", "b", "c")
// Go: mysql.Args("a", "b", "c")
func Arg(args ...any) Expression {
return bmod.Arg(args...)
}

// SQL: ($1, $2, $3)
// Go: psql.ArgGroup("a", "b", "c")
// Go: mysql.ArgGroup("a", "b", "c")
func ArgGroup(args ...any) Expression {
return bmod.ArgGroup(args...)
}

// SQL: $1, $2, $3
// Go: psql.Placeholder(3)
// Go: mysql.Placeholder(3)
func Placeholder(n uint) Expression {
return bmod.Placeholder(n)
}

// SQL: (a, b)
// Go: psql.Group("a", "b")
// Go: mysql.Group("a", "b")
func Group(exps ...bob.Expression) Expression {
return bmod.Group(exps...)
}

// SQL: "table"."column"
// Go: psql.Quote("table", "column")
// Go: mysql.Quote("table", "column")
func Quote(ss ...string) Expression {
return bmod.Quote(ss...)
}

// SQL: where a = $1
// Go: psql.Raw("where a = ?", "something")
// Go: mysql.Raw("where a = ?", "something")
func Raw(query string, args ...any) Expression {
return bmod.Raw(query, args...)
}

// SQL: a as "alias"
// Go: mysql.As("a", "alias")
func As(e Expression, alias string) bob.Expression {
return expr.OP("AS", e, expr.Quote(alias))
}
6 changes: 6 additions & 0 deletions dialect/psql/starters.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,9 @@ func Quote(ss ...string) Expression {
func Raw(query string, args ...any) Expression {
return bmod.Raw(query, args...)
}

// SQL: a as "alias"
// Go: psql.As("a", "alias")
func As(e Expression, alias string) bob.Expression {
return expr.OP("AS", e, expr.Quote(alias))
}
30 changes: 18 additions & 12 deletions dialect/sqlite/starters.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var bmod = expr.Builder[Expression, Expression]{}
// F creates a function expression with the given name and args
//
// SQL: generate_series(1, 3)
// Go: psql.F("generate_series", 1, 3)
// Go: sqlite.F("generate_series", 1, 3)
func F(name string, args ...any) *dialect.Function {
f := dialect.NewFunction(name, args...)

Expand All @@ -27,67 +27,73 @@ func F(name string, args ...any) *dialect.Function {

// S creates a string literal
// SQL: 'a string'
// Go: psql.S("a string")
// Go: sqlite.S("a string")
func S(s string) Expression {
return bmod.S(s)
}

// SQL: NOT true
// Go: psql.Not("true")
// Go: sqlite.Not("true")
func Not(exp bob.Expression) Expression {
return bmod.Not(exp)
}

// SQL: a OR b OR c
// Go: psql.Or("a", "b", "c")
// Go: sqlite.Or("a", "b", "c")
func Or(args ...bob.Expression) Expression {
return bmod.Or(args...)
}

// SQL: a AND b AND c
// Go: psql.And("a", "b", "c")
// Go: sqlite.And("a", "b", "c")
func And(args ...bob.Expression) Expression {
return bmod.And(args...)
}

// SQL: a || b || c
// Go: psql.Concat("a", "b", "c")
// Go: sqlite.Concat("a", "b", "c")
func Concat(args ...bob.Expression) Expression {
return expr.X[Expression, Expression](expr.Join{Exprs: args, Sep: " || "})
}

// SQL: $1, $2, $3
// Go: psql.Args("a", "b", "c")
// Go: sqlite.Args("a", "b", "c")
func Arg(args ...any) Expression {
return bmod.Arg(args...)
}

// SQL: ($1, $2, $3)
// Go: psql.ArgGroup("a", "b", "c")
// Go: sqlite.ArgGroup("a", "b", "c")
func ArgGroup(args ...any) Expression {
return bmod.ArgGroup(args...)
}

// SQL: $1, $2, $3
// Go: psql.Placeholder(3)
// Go: sqlite.Placeholder(3)
func Placeholder(n uint) Expression {
return bmod.Placeholder(n)
}

// SQL: (a, b)
// Go: psql.Group("a", "b")
// Go: sqlite.Group("a", "b")
func Group(exps ...bob.Expression) Expression {
return bmod.Group(exps...)
}

// SQL: "table"."column"
// Go: psql.Quote("table", "column")
// Go: sqlite.Quote("table", "column")
func Quote(ss ...string) Expression {
return bmod.Quote(ss...)
}

// SQL: where a = $1
// Go: psql.Raw("where a = ?", "something")
// Go: sqlite.Raw("where a = ?", "something")
func Raw(query string, args ...any) Expression {
return bmod.Raw(query, args...)
}

// SQL: a as "alias"
// Go: sqlite.As("a", "alias")
func As(e Expression, alias string) bob.Expression {
return expr.OP("AS", e, expr.Quote(alias))
}