Skip to content

Commit

Permalink
add sqlx
Browse files Browse the repository at this point in the history
  • Loading branch information
aacebo committed Nov 16, 2024
1 parent 6cc9553 commit 50e7934
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 0 deletions.
20 changes: 20 additions & 0 deletions sqlx/and.go
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
}
20 changes: 20 additions & 0 deletions sqlx/as.go
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)
}
17 changes: 17 additions & 0 deletions sqlx/raw.go
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
}
72 changes: 72 additions & 0 deletions sqlx/select.go
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")
}
28 changes: 28 additions & 0 deletions sqlx/select_test.go
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)
}
})
}
6 changes: 6 additions & 0 deletions sqlx/sqlizer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package sqlx

type Sqlizer interface {
Sql() string
SqlPretty() string
}
6 changes: 6 additions & 0 deletions sqlx/where.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package sqlx

type Eq map[string]any

type WhereClause struct {
}

0 comments on commit 50e7934

Please sign in to comment.