github.com/octohelm/storage@v0.0.0-20240516030302-1ac2cc1ea347/pkg/sqlbuilder/def_table_test.go (about) 1 package sqlbuilder_test 2 3 import ( 4 "testing" 5 6 //"github.com/octohelm/storage/internal/connectors/postgresql" 7 "github.com/octohelm/storage/internal/testutil" 8 . "github.com/octohelm/storage/pkg/sqlbuilder" 9 ) 10 11 func TestTable_Expr(t *testing.T) { 12 tUser := T("t_user", 13 Col("f_id", ColField("ID"), ColTypeOf(uint64(0), ",autoincrement")), 14 Col("f_name", ColField("Name"), ColTypeOf("", ",size=128,default=''")), 15 ) 16 17 tUserRole := T("t_user_role", 18 Col("f_id", ColField("ID"), ColTypeOf(uint64(0), ",autoincrement")), 19 Col("f_user_id", ColField("UserID"), ColTypeOf(uint64(0), "")), 20 ) 21 22 t.Run("replace table", func(t *testing.T) { 23 testutil.ShouldBeExpr(t, tUser.(TableExprParse).Expr("#.*"), "t_user.*") 24 }) 25 26 t.Run("replace table col by field", func(t *testing.T) { 27 testutil.ShouldBeExpr(t, tUser.(TableExprParse).Expr("#ID = #ID + 1"), "f_id = f_id + 1") 28 }) 29 30 t.Run("replace table col by field for function", func(t *testing.T) { 31 testutil.ShouldBeExpr(t, tUser.(TableExprParse).Expr("COUNT(#ID)"), "COUNT(f_id)") 32 }) 33 34 t.Run("could handle context", func(t *testing.T) { 35 testutil.ShouldBeExpr(t, 36 Select(nil). 37 From( 38 tUser, 39 Where( 40 AsCond(tUser.(TableExprParse).Expr("#ID > 1")), 41 ), 42 Join(tUserRole).On(AsCond(tUser.(TableExprParse).Expr("#ID = ?", tUserRole.(TableExprParse).Expr("#UserID")))), 43 ), 44 ` 45 SELECT * FROM t_user 46 JOIN t_user_role ON t_user.f_id = t_user_role.f_user_id 47 WHERE t_user.f_id > 1 48 `) 49 }) 50 }