Skip to content

Commit

Permalink
select cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
aacebo committed Nov 22, 2024
1 parent d7210d8 commit ce77001
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
24 changes: 13 additions & 11 deletions sqlx/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,30 @@ type SelectStatement struct {
offset Sqlizer
}

func Select(columns ...string) *SelectStatement {
func Select(columns ...any) *SelectStatement {
cols := make([]Sqlizer, len(columns))

for i, col := range columns {
cols[i] = Raw(col)
cols[i] = &Sql{col}
}

return &SelectStatement{
self := &SelectStatement{
depth: 0,
columns: cols,
}
}

func (self *SelectStatement) Column(column string) *SelectStatement {
self.columns = append(self.columns, Raw(column))
self.setDepth(self.depth)
return self
}

func (self *SelectStatement) ColumnAs(column any, alias string) *SelectStatement {
func (self *SelectStatement) Column(column any) *SelectStatement {
switch v := column.(type) {
case string:
self.columns = append(self.columns, As(&Sql{v}, alias))
self.columns = append(self.columns, &Sql{v})
break
case *SelectStatement:
v.depth = self.depth + 1
self.columns = append(self.columns, As(&Sql{v}, alias))
case Sqlizer:
v.setDepth(self.depth + 1)
self.columns = append(self.columns, v)
break
}

Expand Down Expand Up @@ -127,6 +125,10 @@ func (self *SelectStatement) Offset(offset string) *SelectStatement {
return self
}

func (self *SelectStatement) As(alias string) Sqlizer {
return As(self, alias)
}

func (self SelectStatement) Sql() string {
parts := []string{"SELECT"}
parts = append(parts, self.columns.Sql())
Expand Down
32 changes: 14 additions & 18 deletions sqlx/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ func TestSelect(t *testing.T) {
t.Fatal(err)
}

sql := sqlx.Select().ColumnAs(
sqlx.Select("a", "b", "c").From("test"),
"results",
sql := sqlx.Select(
sqlx.Select("a", "b", "c").From("test").As("results"),
).Sql()

if sql != strings.TrimSuffix(string(expected), "\n") {
Expand All @@ -52,9 +51,10 @@ func TestSelect(t *testing.T) {
t.Fatal(err)
}

sql := sqlx.Select("1", "2").ColumnAs(
sqlx.Select("a", "b", "c").From("test"),
"results",
sql := sqlx.Select(
"1",
"2",
sqlx.Select("a", "b", "c").From("test").As("results"),
).Sql()

if sql != strings.TrimSuffix(string(expected), "\n") {
Expand Down Expand Up @@ -227,9 +227,7 @@ func TestSelect(t *testing.T) {
sql := sqlx.Select(
"a",
"b",
).ColumnAs(
sqlx.Select("*").From("test").Limit("1"),
"tester",
sqlx.Select("*").From("test").Limit("1").As("tester"),
).From(
"test",
).Where(
Expand Down Expand Up @@ -270,9 +268,8 @@ func TestSelect(t *testing.T) {
t.Fatal(err)
}

sql := sqlx.Select().ColumnAs(
sqlx.Select("a", "b", "c").From("test"),
"results",
sql := sqlx.Select(
sqlx.Select("a", "b", "c").From("test").As("results"),
).SqlPretty(" ")

if sql != strings.TrimSuffix(string(expected), "\n") {
Expand All @@ -287,9 +284,10 @@ func TestSelect(t *testing.T) {
t.Fatal(err)
}

sql := sqlx.Select("1", "2").ColumnAs(
sqlx.Select("a", "b", "c").From("test"),
"results",
sql := sqlx.Select(
"1",
"2",
sqlx.Select("a", "b", "c").From("test").As("results"),
).SqlPretty(" ")

if sql != strings.TrimSuffix(string(expected), "\n") {
Expand Down Expand Up @@ -462,9 +460,7 @@ func TestSelect(t *testing.T) {
sql := sqlx.Select(
"a",
"b",
).ColumnAs(
sqlx.Select("*").From("test").Limit("1"),
"tester",
sqlx.Select("*").From("test").Limit("1").As("tester"),
).From(
"test",
).Where(
Expand Down

0 comments on commit ce77001

Please sign in to comment.