Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
aacebo committed Nov 21, 2024
1 parent 09a114c commit 19372e9
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 23 deletions.
4 changes: 4 additions & 0 deletions sqlx/as.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ func (self AsClause) Sql() string {
func (self AsClause) SqlPretty(indent string) string {
return fmt.Sprintf(`%s as "%s"`, self.stmt.SqlPretty(indent), self.alias)
}

func (self *AsClause) setDepth(depth uint) {
self.stmt.setDepth(depth)
}
6 changes: 6 additions & 0 deletions sqlx/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ func (self Columns) SqlPretty(indent string) string {

return strings.Join(parts, "\n")
}

func (self Columns) setDepth(depth uint) {
for _, column := range self {
column.setDepth(depth)
}
}
55 changes: 55 additions & 0 deletions sqlx/expression.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package sqlx

import "strings"

type Expression struct {
left Sqlizer
op Sqlizer
right Sqlizer
}

func Expr(left any, op any, right any) *Expression {
return &Expression{&Sql{left}, &Sql{op}, &Sql{right}}
}

func (self Expression) Sql() string {
parts := []string{}

if self.left != nil {
parts = append(parts, self.left.Sql())
}

if self.op != nil {
parts = append(parts, self.op.Sql())
}

if self.right != nil {
parts = append(parts, self.right.Sql())
}

return strings.Join(parts, " ")
}

func (self Expression) SqlPretty(indent string) string {
parts := []string{}

if self.left != nil {
parts = append(parts, self.left.SqlPretty(indent))
}

if self.op != nil {
parts = append(parts, self.op.SqlPretty(indent))
}

if self.right != nil {
parts = append(parts, self.right.SqlPretty(indent))
}

return strings.Join(parts, " ")
}

func (self *Expression) setDepth(depth uint) {
self.left.setDepth(depth)
self.op.setDepth(depth)
self.right.setDepth(depth)
}
42 changes: 26 additions & 16 deletions sqlx/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ func (self *SelectStatement) Column(column string) *SelectStatement {
func (self *SelectStatement) ColumnAs(column any, alias string) *SelectStatement {
switch v := column.(type) {
case string:
self.columns = append(self.columns, As(Sql{v}, alias))
self.columns = append(self.columns, As(&Sql{v}, alias))
break
case *SelectStatement:
v.depth = self.depth + 1
self.columns = append(self.columns, As(Sql{v}, alias))
self.columns = append(self.columns, As(&Sql{v}, alias))
break
}

Expand All @@ -49,7 +49,7 @@ func (self *SelectStatement) ColumnAs(column any, alias string) *SelectStatement
func (self *SelectStatement) From(from any) *SelectStatement {
switch v := from.(type) {
case string:
self.from = Sql{from}
self.from = &Sql{from}
break
case *SelectStatement:
v.depth = self.depth + 1
Expand All @@ -70,19 +70,20 @@ func (self *SelectStatement) From(from any) *SelectStatement {
}

func (self *SelectStatement) Where(predicate any) *SelectStatement {
self.where = Where(Sql{predicate})
switch v := predicate.(type) {
case Sqlizer:
v.setDepth(self.depth + 1)
}

self.where = Where(predicate)
return self
}

func (self *SelectStatement) And(predicates ...any) *SelectStatement {
for _, predicate := range predicates {
switch v := predicate.(type) {
case *SelectStatement:
v.depth = self.depth + 1
break
case *WhereClause:
v.depth = self.depth + 1
break
case Sqlizer:
v.setDepth(self.depth + 1)
}

self.where.And(predicate)
Expand All @@ -94,12 +95,8 @@ func (self *SelectStatement) And(predicates ...any) *SelectStatement {
func (self *SelectStatement) Or(predicates ...any) *SelectStatement {
for _, predicate := range predicates {
switch v := predicate.(type) {
case *SelectStatement:
v.depth = self.depth + 1
break
case *WhereClause:
v.depth = self.depth + 1
break
case Sqlizer:
v.setDepth(self.depth + 1)
}

self.where.Or(predicate)
Expand Down Expand Up @@ -172,3 +169,16 @@ func (self SelectStatement) SqlPretty(indent string) string {

return sql
}

func (self *SelectStatement) setDepth(depth uint) {
self.depth = depth
self.columns.setDepth(depth + 1)

if self.from != nil {
self.from.setDepth(depth + 1)
}

if self.where != nil {
self.where.setDepth(depth)
}
}
12 changes: 10 additions & 2 deletions sqlx/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ func TestSelect(t *testing.T) {
).From("test").Where(
"a = b",
).And(
"b = c",
sqlx.Expr(
sqlx.Select("*").From("tester"),
"IS",
"NULL",
),
).Sql()

if sql != strings.TrimSuffix(string(expected), "\n") {
Expand Down Expand Up @@ -257,7 +261,11 @@ func TestSelect(t *testing.T) {
).From("test").Where(
"a = b",
).And(
"b = c",
sqlx.Expr(
sqlx.Select("*").From("tester"),
"IS",
"NULL",
),
).SqlPretty(" ")

if sql != strings.TrimSuffix(string(expected), "\n") {
Expand Down
9 changes: 9 additions & 0 deletions sqlx/sqlizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package sqlx
type Sqlizer interface {
Sql() string
SqlPretty(indent string) string

setDepth(depth uint)
}

type Sql struct {
Expand Down Expand Up @@ -34,3 +36,10 @@ func (self Sql) SqlPretty(indent string) string {

panic("invalid type")
}

func (self *Sql) setDepth(depth uint) {
switch v := self.Value.(type) {
case Sqlizer:
v.setDepth(depth)
}
}
2 changes: 1 addition & 1 deletion sqlx/testcases/select/where_and.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SELECT a, b, c FROM test WHERE a = b AND b = c;
SELECT a, b, c FROM test WHERE a = b AND (SELECT * FROM tester) IS NULL;
6 changes: 5 additions & 1 deletion sqlx/testcases/select/where_and_pretty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ SELECT
c
FROM test
WHERE a = b
AND b = c;
AND (
SELECT
*
FROM tester
) IS NULL;
15 changes: 12 additions & 3 deletions sqlx/where.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ type WhereClause struct {
}

func Where(predicate any) *WhereClause {
return &WhereClause{0, Sql{predicate}, []_Condition{}}
return &WhereClause{0, &Sql{predicate}, []_Condition{}}
}

func (self *WhereClause) And(predicate any) *WhereClause {
self.conditions = append(self.conditions, _Condition{
kind: and,
value: Sql{predicate},
value: &Sql{predicate},
})

return self
Expand All @@ -39,7 +39,7 @@ func (self *WhereClause) And(predicate any) *WhereClause {
func (self *WhereClause) Or(predicate any) *WhereClause {
self.conditions = append(self.conditions, _Condition{
kind: or,
value: Sql{predicate},
value: &Sql{predicate},
})

return self
Expand Down Expand Up @@ -79,3 +79,12 @@ func (self WhereClause) SqlPretty(indent string) string {

return strings.Join(parts, "\n")
}

func (self *WhereClause) setDepth(depth uint) {
self.depth = depth
self.predicate.setDepth(depth + 1)

for _, condition := range self.conditions {
condition.value.setDepth(depth + 1)
}
}

0 comments on commit 19372e9

Please sign in to comment.