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

feat: support native SQL as custom fields #1213

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions field/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func NewField(table, column string, opts ...Option) Field {
return Field{expr: expr{col: toColumn(table, column, opts...)}}
}

// NewFieldRaw create new field by native sql
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
Loading