github.com/eden-framework/sqlx@v0.0.2/builder/stmt_with_test.go (about)

     1  package builder_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/eden-framework/sqlx/builder"
     7  	"github.com/eden-framework/sqlx/builder/buidertestingutils"
     8  	"github.com/onsi/gomega"
     9  )
    10  
    11  func TestWithStmt(t *testing.T) {
    12  	gr := &GroupRelation{}
    13  	g := &Group{}
    14  
    15  	t.Run("simple with", func(t *testing.T) {
    16  		gomega.NewWithT(t).Expect(
    17  			With((&GroupWithParent{}).T(), func(tmpTableGroupWithParent *Table) SqlExpr {
    18  				s := Select(MultiMayAutoAlias(
    19  					g.T().Col("f_group_id"),
    20  					gr.T().Col("f_group_id"),
    21  				)).
    22  					From(gr.T(),
    23  						RightJoin(g.T()).On(g.T().Col("f_group_id").Eq(gr.T().Col("f_group_id"))),
    24  					)
    25  				return s
    26  			}).With((&GroupWithParent{}).T(), func(tmpTableGroupWithParent *Table) SqlExpr {
    27  				s := Select(MultiMayAutoAlias(
    28  					g.T().Col("f_group_id"),
    29  					gr.T().Col("f_group_id"),
    30  				)).
    31  					From(gr.T(),
    32  						RightJoin(g.T()).On(g.T().Col("f_group_id").Eq(gr.T().Col("f_group_id"))),
    33  					)
    34  				return s
    35  			}).
    36  				Exec(func(tables ...*Table) SqlExpr {
    37  					return Select(nil).From(tables[0])
    38  				}),
    39  		).To(buidertestingutils.BeExpr(`
    40  WITH t_group_with_parent(f_group_id,f_parent_group_id) AS (
    41  SELECT (t_group.f_group_id) AS f_group_id, (t_group_relation.f_group_id) AS f_group_id FROM t_group_relation
    42  RIGHT JOIN t_group ON t_group.f_group_id = t_group_relation.f_group_id
    43  ), t_group_with_parent(f_group_id,f_parent_group_id) AS (
    44  SELECT (t_group.f_group_id) AS f_group_id, (t_group_relation.f_group_id) AS f_group_id FROM t_group_relation
    45  RIGHT JOIN t_group ON t_group.f_group_id = t_group_relation.f_group_id
    46  )
    47  SELECT * FROM t_group_with_parent
    48  `))
    49  	})
    50  	t.Run("WithRecursive", func(t *testing.T) {
    51  		gomega.NewWithT(t).Expect(
    52  			WithRecursive((&GroupWithParentAndChildren{}).T(), func(tmpTableGroupWithParentAndChildren *Table) SqlExpr {
    53  				return With((&GroupWithParent{}).T(), func(tmpTableGroupWithParent *Table) SqlExpr {
    54  					s := Select(MultiMayAutoAlias(
    55  						g.T().Col("f_group_id"),
    56  						gr.T().Col("f_parent_group_id"),
    57  					)).
    58  						From(gr.T(), RightJoin(g.T()).On(g.T().Col("f_group_id").Eq(gr.T().Col("f_group_id"))))
    59  					return s
    60  				}).Exec(func(tables ...*Table) SqlExpr {
    61  					tmpTableGroupWithParent := tables[0]
    62  					return Select(
    63  						MultiMayAutoAlias(
    64  							tmpTableGroupWithParent.Col("f_group_id"),
    65  							tmpTableGroupWithParent.Col("f_parent_group_id"),
    66  							Alias(Expr("0"), "f_depth"),
    67  						),
    68  					).From(
    69  						tmpTableGroupWithParent,
    70  						Where(tmpTableGroupWithParent.Col("f_group_id").Eq(1201375536060956676)),
    71  						Union().All(
    72  							Select(MultiMayAutoAlias(
    73  								tmpTableGroupWithParent.Col("f_group_id"),
    74  								tmpTableGroupWithParent.Col("f_parent_group_id"),
    75  								Alias(tmpTableGroupWithParentAndChildren.Col("f_depth").Expr("# + 1"), "f_depth"),
    76  							)).From(
    77  								tmpTableGroupWithParent,
    78  								CrossJoin(tmpTableGroupWithParentAndChildren),
    79  								Where(
    80  									And(
    81  										tmpTableGroupWithParent.Col("f_group_id").Neq(tmpTableGroupWithParentAndChildren.Col("f_group_id")),
    82  										tmpTableGroupWithParent.Col("f_parent_group_id").Eq(tmpTableGroupWithParentAndChildren.Col("f_group_id")),
    83  									)),
    84  							),
    85  						),
    86  					)
    87  				})
    88  			}).Exec(func(tables ...*Table) SqlExpr {
    89  				return Select(nil).From(tables[0])
    90  			}),
    91  		).To(buidertestingutils.BeExpr(`
    92  WITH RECURSIVE t_group_with_parent_and_children(f_group_id,f_parent_group_id,f_depth) AS (
    93  WITH t_group_with_parent(f_group_id,f_parent_group_id) AS (
    94  SELECT (t_group.f_group_id) AS f_group_id, (t_group_relation.f_parent_group_id) AS f_parent_group_id FROM t_group_relation
    95  RIGHT JOIN t_group ON t_group.f_group_id = t_group_relation.f_group_id
    96  )
    97  SELECT f_group_id, f_parent_group_id, (0) AS f_depth FROM t_group_with_parent
    98  WHERE f_group_id = ?
    99  UNION ALL SELECT (t_group_with_parent.f_group_id) AS f_group_id, (t_group_with_parent.f_parent_group_id) AS f_parent_group_id, (t_group_with_parent_and_children.f_depth + 1) AS f_depth FROM t_group_with_parent
   100  CROSS JOIN t_group_with_parent_and_children
   101  WHERE (t_group_with_parent.f_group_id <> t_group_with_parent_and_children.f_group_id) AND (t_group_with_parent.f_parent_group_id = t_group_with_parent_and_children.f_group_id)
   102  )
   103  SELECT * FROM t_group_with_parent_and_children
   104  `, 1201375536060956676))
   105  	})
   106  }
   107  
   108  var tableGroup = TableFromModel(&Group{})
   109  
   110  type Group struct {
   111  	GroupID int `db:"f_group_id"`
   112  }
   113  
   114  func (g *Group) TableName() string {
   115  	return "t_group"
   116  }
   117  
   118  func (g *Group) T() *Table {
   119  	return tableGroup
   120  }
   121  
   122  var tableGroupRelation = TableFromModel(&GroupRelation{})
   123  
   124  type GroupRelation struct {
   125  	GroupID       int `db:"f_group_id"`
   126  	ParentGroupID int `db:"f_parent_group_id"`
   127  }
   128  
   129  func (g *GroupRelation) TableName() string {
   130  	return "t_group_relation"
   131  }
   132  
   133  func (g *GroupRelation) T() *Table {
   134  	return tableGroupRelation
   135  }
   136  
   137  var tableGroupWithParent = TableFromModel(&GroupWithParent{})
   138  
   139  type GroupWithParent struct {
   140  	GroupID       int `db:"f_group_id"`
   141  	ParentGroupID int `db:"f_parent_group_id"`
   142  }
   143  
   144  func (g *GroupWithParent) TableName() string {
   145  	return "t_group_with_parent"
   146  }
   147  
   148  func (g *GroupWithParent) T() *Table {
   149  	return tableGroupWithParent
   150  }
   151  
   152  var tableGroupWithParentAndChildren = TableFromModel(&GroupWithParentAndChildren{})
   153  
   154  type GroupWithParentAndChildren struct {
   155  	GroupWithParent
   156  	Depth int `db:"f_depth"`
   157  }
   158  
   159  func (g *GroupWithParentAndChildren) TableName() string {
   160  	return "t_group_with_parent_and_children"
   161  }
   162  
   163  func (g *GroupWithParentAndChildren) T() *Table {
   164  	return tableGroupWithParentAndChildren
   165  }