Skip to content

Commit

Permalink
feat: support native SQL as custom fields
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangleitao committed Sep 13, 2024
1 parent ebb6364 commit 776d993
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
4 changes: 4 additions & 0 deletions field/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func NewField(table, column string, opts ...Option) Field {
return Field{expr: expr{col: toColumn(table, column, opts...)}}
}

func NewFieldRaw(rawSql string, vars ...interface{}) Field {

Check failure on line 35 in field/export.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 exported: exported function NewFieldRaw should have comment or be unexported (revive) Raw Output: field/export.go:35:1: exported: exported function NewFieldRaw should have comment or be unexported (revive) func NewFieldRaw(rawSql string, vars ...interface{}) Field { ^
return Field{expr: expr{e: clause.Expr{SQL: rawSql, Vars: vars}}}
}

// NewSerializer create new field2
func NewSerializer(table, column string, opts ...Option) Serializer {
return Serializer{expr: expr{col: toColumn(table, column, opts...)}}
Expand Down
50 changes: 50 additions & 0 deletions field/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,56 @@ func TestExpr_Build(t *testing.T) {
Expr: field.NewField("", "id").GroupConcat(),
Result: "GROUP_CONCAT(`id`)",
},
{
Expr: field.NewFieldRaw("if(column1=?,column2,column3)", "1"),
Result: "if(column1=?,column2,column3)",
ExpectedVars: []interface{}{"1"},
},
{
Expr: field.NewFieldRaw("if(column1=?,column2,column3)", "1").Eq(p),
Result: "if(column1=?,column2,column3) = ?",
ExpectedVars: []interface{}{"1", p},
},
{
Expr: field.NewFieldRaw("if(column1=?,column2,column3)", field.NewField("", "new_id")).Eq(p),
Result: "if(column1=`new_id`,column2,column3) = ?",
ExpectedVars: []interface{}{p},
},
{
Expr: field.NewFieldRaw("if(column1=?,column2,column3)", "1").EqCol(field.NewField("", "new_id")),
Result: "if(column1=?,column2,column3) = `new_id`",
ExpectedVars: []interface{}{"1"},
},
{
Expr: field.NewFieldRaw("if(column1=?,column2,column3)", "1").EqCol(field.NewField("", "new_id").WithTable("tableB")),
Result: "if(column1=?,column2,column3) = `tableB`.`new_id`",
ExpectedVars: []interface{}{"1"},
},
{
Expr: field.NewFieldRaw("if(column1=?,column2,column3)", "1").IsNull(),
Result: "if(column1=?,column2,column3) IS NULL",
ExpectedVars: []interface{}{"1"},
},
{
Expr: field.NewFieldRaw("if(column1=?,column2,column3)", "1").GroupConcat(),
Result: "GROUP_CONCAT(if(column1=?,column2,column3))",
ExpectedVars: []interface{}{"1"},
},
{
Expr: field.NewFieldRaw("if(column1=?,column2,column3)", "1").Desc(),
Result: "if(column1=?,column2,column3) DESC",
ExpectedVars: []interface{}{"1"},
},
{
Expr: field.NewFieldRaw("if(column1=?,column2,column3)", "1").IfNull(p),
Result: "IFNULL(if(column1=?,column2,column3),?)",
ExpectedVars: []interface{}{"1", p},
},
{
Expr: field.NewFieldRaw("if(column1=?,column2,column3)", "1").As("column4"),
Result: "if(column1=?,column2,column3) AS `column4`",
ExpectedVars: []interface{}{"1"},
},
{
Expr: field.Func.UnixTimestamp(),
Result: "UNIX_TIMESTAMP()",
Expand Down

0 comments on commit 776d993

Please sign in to comment.