-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package sqlx | ||
|
||
import "strings" | ||
|
||
type AndClause []Sqlizer | ||
|
||
func And(conditions ...Sqlizer) AndClause { | ||
return conditions | ||
} | ||
|
||
func (self AndClause) Sql() string { | ||
parts := make([]string, len(self)) | ||
|
||
for i, cond := range self { | ||
parts[i] = cond.Sql() | ||
} | ||
|
||
sql := strings.Join(parts, "") | ||
return sql | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package sqlx | ||
|
||
import "fmt" | ||
|
||
type AsClause struct { | ||
stmt Sqlizer | ||
alias string | ||
} | ||
|
||
func As(stmt Sqlizer, alias string) *AsClause { | ||
return &AsClause{stmt, alias} | ||
} | ||
|
||
func (self AsClause) Sql() string { | ||
return fmt.Sprintf("%s as \"%s\"", self.stmt.Sql(), self.alias) | ||
} | ||
|
||
func (self AsClause) SqlPretty() string { | ||
return fmt.Sprintf("%s as \"%s\"", self.stmt.SqlPretty(), self.alias) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package sqlx | ||
|
||
type RawStatement struct { | ||
stmt string | ||
} | ||
|
||
func Raw(stmt string) *RawStatement { | ||
return &RawStatement{stmt} | ||
} | ||
|
||
func (self RawStatement) Sql() string { | ||
return self.stmt | ||
} | ||
|
||
func (self RawStatement) SqlPretty() string { | ||
return self.stmt | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package sqlx | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type SelectStatement struct { | ||
depth uint | ||
columns []Sqlizer | ||
from Sqlizer | ||
} | ||
|
||
func Select(columns ...string) *SelectStatement { | ||
cols := make([]Sqlizer, len(columns)) | ||
|
||
for i, col := range columns { | ||
cols[i] = Raw(col) | ||
} | ||
|
||
return &SelectStatement{ | ||
depth: 0, | ||
columns: cols, | ||
from: nil, | ||
} | ||
} | ||
|
||
func (self *SelectStatement) Column(column string) *SelectStatement { | ||
self.columns = append(self.columns, Raw(column)) | ||
return self | ||
} | ||
|
||
func (self *SelectStatement) ColumnAs(column string, alias string) *SelectStatement { | ||
self.columns = append(self.columns, As(Raw(column), alias)) | ||
return self | ||
} | ||
|
||
func (self *SelectStatement) From(from string) *SelectStatement { | ||
self.from = Raw(from) | ||
return self | ||
} | ||
|
||
func (self *SelectStatement) FromSelect(stmt *SelectStatement, alias string) *SelectStatement { | ||
stmt.depth = self.depth + 1 | ||
self.from = As(stmt, alias) | ||
return self | ||
} | ||
|
||
func (self SelectStatement) Sql() string { | ||
parts := []string{"SELECT"} | ||
columns := []string{} | ||
|
||
for _, column := range self.columns { | ||
columns = append(columns, column.Sql()) | ||
} | ||
|
||
parts = append(parts, strings.Join(columns, ", ")) | ||
parts = append(parts, "FROM", self.from.Sql()) | ||
sql := strings.Join(parts, " ") | ||
|
||
if self.depth == 0 { | ||
sql += ";" | ||
} else { | ||
sql = fmt.Sprintf("(%s)", sql) | ||
} | ||
|
||
return sql | ||
} | ||
|
||
func (self SelectStatement) SqlPretty() string { | ||
return strings.Join([]string{}, "\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package sqlx_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/thegogod/rum/sqlx" | ||
) | ||
|
||
func TestSelect(t *testing.T) { | ||
t.Run("should build basic", func(t *testing.T) { | ||
sql := sqlx.Select("a", "b", "c").From("test").Sql() | ||
|
||
if sql != "SELECT a, b, c FROM test;" { | ||
t.Fatalf(sql) | ||
} | ||
}) | ||
|
||
t.Run("should build with nested from", func(t *testing.T) { | ||
sql := sqlx.Select("a", "b", "c").FromSelect( | ||
sqlx.Select("d", "e", "f").From("test"), | ||
"test", | ||
).Sql() | ||
|
||
if sql != "SELECT a, b, c FROM (SELECT d, e, f FROM test) as \"test\";" { | ||
t.Fatalf(sql) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package sqlx | ||
|
||
type Sqlizer interface { | ||
Sql() string | ||
SqlPretty() string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package sqlx | ||
|
||
type Eq map[string]any | ||
|
||
type WhereClause struct { | ||
} |