good morning!!!!

Skip to content
Snippets Groups Projects
Commit 1ad38040 authored by José Carlos Nieto's avatar José Carlos Nieto
Browse files

Refactor.

parent 70035a30
Branches
No related tags found
No related merge requests found
...@@ -12,7 +12,7 @@ func (self Column) String() string { ...@@ -12,7 +12,7 @@ func (self Column) String() string {
chunks := strings.Split(self.v, sqlColumnSeparator) chunks := strings.Split(self.v, sqlColumnSeparator)
for i := range chunks { for i := range chunks {
chunks[i] = mustParse(sqlEscape, Raw{chunks[i]}) chunks[i] = mustParse(sqlIdentifierQuote, Raw{chunks[i]})
} }
return strings.Join(chunks, sqlColumnSeparator) return strings.Join(chunks, sqlColumnSeparator)
......
...@@ -25,5 +25,5 @@ func (self ColumnValues) String() string { ...@@ -25,5 +25,5 @@ func (self ColumnValues) String() string {
out[i] = self[i].String() out[i] = self[i].String()
} }
return strings.Join(out, sqlColumnComma) return strings.Join(out, sqlIdentifierSeparator)
} }
...@@ -11,7 +11,7 @@ func TestColumnValue(t *testing.T) { ...@@ -11,7 +11,7 @@ func TestColumnValue(t *testing.T) {
cv = ColumnValue{Column{"id"}, "=", Value{1}} cv = ColumnValue{Column{"id"}, "=", Value{1}}
s = cv.String() s = cv.String()
e = `"id" = "1"` e = `"id" = '1'`
if s != e { if s != e {
t.Fatalf("Got: %s, Expecting: %s", s, e) t.Fatalf("Got: %s, Expecting: %s", s, e)
...@@ -40,7 +40,7 @@ func TestColumnValues(t *testing.T) { ...@@ -40,7 +40,7 @@ func TestColumnValues(t *testing.T) {
} }
s = cvs.String() s = cvs.String()
e = `"id" > "8", "other"."id" < 100, "name" = "Haruki Murakami", "created" >= NOW(), "modified" <= NOW()` e = `"id" > '8', "other"."id" < 100, "name" = 'Haruki Murakami', "created" >= NOW(), "modified" <= NOW()`
if s != e { if s != e {
t.Fatalf("Got: %s, Expecting: %s", s, e) t.Fatalf("Got: %s, Expecting: %s", s, e)
......
...@@ -16,7 +16,7 @@ func (self Columns) String() string { ...@@ -16,7 +16,7 @@ func (self Columns) String() string {
out[i] = self[i].String() out[i] = self[i].String()
} }
return strings.Join(out, sqlColumnComma) return strings.Join(out, sqlIdentifierSeparator)
} }
return "" return ""
} }
......
...@@ -9,5 +9,5 @@ type Database struct { ...@@ -9,5 +9,5 @@ type Database struct {
} }
func (self Database) String() string { func (self Database) String() string {
return mustParse(sqlEscape, Raw{fmt.Sprintf(`%v`, self.v)}) return mustParse(sqlIdentifierQuote, Raw{fmt.Sprintf(`%v`, self.v)})
} }
...@@ -2,21 +2,38 @@ package sqlgen ...@@ -2,21 +2,38 @@ package sqlgen
import ( import (
"bytes" "bytes"
"fmt"
"text/template" "text/template"
) )
const ( const (
sqlColumnSeparator = `.` sqlColumnSeparator = `.`
sqlColumnComma = `, ` sqlIdentifierSeparator = `, `
sqlValueComma = `, ` sqlIdentifierQuote = `"{{.Raw}}"`
sqlEscape = `"{{.Raw}}"` sqlValueSeparator = `, `
sqlValueQuote = `'{{.}}'`
sqlAndKeyword = `AND`
sqlOrKeyword = `OR`
sqlNotKeyword = `NOT`
sqlDescKeyword = `DESC`
sqlAscKeyword = `ASC`
sqlDefaultOperator = `=`
sqlClauseGroup = `({{.}})`
sqlClauseOperator = ` {{.}} `
sqlColumnValue = `{{.Column}} {{.Operator}} {{.Value}}`
sqlOrderByLayout = ` sqlOrderByLayout = `
{{if .Columns}} {{if .Columns}}
ORDER BY {{.Columns}} {{.Sort}} ORDER BY {{.Columns}} {{.Sort}}
{{end}} {{end}}
` `
sqlWhereLayout = `
{{if .Conds}}
WHERE {{.Conds}}
{{end}}
`
sqlSelectLayout = ` sqlSelectLayout = `
SELECT SELECT
...@@ -28,9 +45,7 @@ const ( ...@@ -28,9 +45,7 @@ const (
FROM {{.Table}} FROM {{.Table}}
{{if .Where}} {{.Where}}
WHERE {{.Where}}
{{end}}
{{.OrderBy}} {{.OrderBy}}
...@@ -45,26 +60,20 @@ const ( ...@@ -45,26 +60,20 @@ const (
sqlDeleteLayout = ` sqlDeleteLayout = `
DELETE DELETE
FROM {{.Table}} FROM {{.Table}}
{{if .Where}} {{.Where}}
WHERE {{.Where}}
{{end}}
` `
sqlUpdateLayout = ` sqlUpdateLayout = `
UPDATE UPDATE
{{.Table}} {{.Table}}
SET {{.ColumnValues}} SET {{.ColumnValues}}
{{if .Where}} {{ .Where }}
WHERE {{.Where}}
{{end}}
` `
sqlSelectCountLayout = ` sqlSelectCountLayout = `
SELECT SELECT
COUNT(1) AS _t COUNT(1) AS _t
FROM {{.Table}} FROM {{.Table}}
{{if .Where}} {{.Where}}
WHERE {{.Where}}
{{end}}
` `
sqlInsertLayout = ` sqlInsertLayout = `
...@@ -85,15 +94,6 @@ const ( ...@@ -85,15 +94,6 @@ const (
sqlDropTableLayout = ` sqlDropTableLayout = `
DROP TABLE {{.Table}} DROP TABLE {{.Table}}
` `
sqlAndKeyword = `AND`
sqlOrKeyword = `OR`
sqlDescKeyword = `DESC`
sqlAscKeyword = `ASC`
sqlDefaultOperator = `=`
sqlConditionGroup = `({{.}})`
sqlColumnValue = `{{.Column}} {{.Operator}} {{.Value}}`
) )
type Type uint type Type uint
...@@ -120,7 +120,6 @@ func mustParse(text string, data interface{}) string { ...@@ -120,7 +120,6 @@ func mustParse(text string, data interface{}) string {
t := template.Must(template.New("").Parse(text)) t := template.Must(template.New("").Parse(text))
if err := t.Execute(&b, data); err != nil { if err := t.Execute(&b, data); err != nil {
fmt.Printf("data: %v\n", data)
panic("t.Execute: " + err.Error()) panic("t.Execute: " + err.Error())
} }
......
...@@ -110,6 +110,26 @@ func TestSelectStarFrom(t *testing.T) { ...@@ -110,6 +110,26 @@ func TestSelectStarFrom(t *testing.T) {
} }
} }
func TestSelectArtistNameFrom(t *testing.T) {
var s, e string
var stmt Statement
stmt = Statement{
Type: SqlSelect,
Table: Table{"artist"},
Columns: Columns{
Column{"artist.name"},
},
}
s = trim(stmt.Compile())
e = `SELECT "artist"."name" FROM "artist"`
if s != e {
t.Fatalf("Got: %s, Expecting: %s", s, e)
}
}
func TestSelectFieldsFrom(t *testing.T) { func TestSelectFieldsFrom(t *testing.T) {
var s, e string var s, e string
var stmt Statement var stmt Statement
...@@ -289,7 +309,7 @@ func TestSelectFieldsFromWhere(t *testing.T) { ...@@ -289,7 +309,7 @@ func TestSelectFieldsFromWhere(t *testing.T) {
} }
s = trim(stmt.Compile()) s = trim(stmt.Compile())
e = `SELECT "foo", "bar", "baz" FROM "table name" WHERE ("baz" = "99")` e = `SELECT "foo", "bar", "baz" FROM "table name" WHERE ("baz" = '99')`
if s != e { if s != e {
t.Fatalf("Got: %s, Expecting: %s", s, e) t.Fatalf("Got: %s, Expecting: %s", s, e)
...@@ -316,7 +336,7 @@ func TestSelectFieldsFromWhereLimitOffset(t *testing.T) { ...@@ -316,7 +336,7 @@ func TestSelectFieldsFromWhereLimitOffset(t *testing.T) {
} }
s = trim(stmt.Compile()) s = trim(stmt.Compile())
e = `SELECT "foo", "bar", "baz" FROM "table name" WHERE ("baz" = "99") LIMIT 10 OFFSET 23` e = `SELECT "foo", "bar", "baz" FROM "table name" WHERE ("baz" = '99') LIMIT 10 OFFSET 23`
if s != e { if s != e {
t.Fatalf("Got: %s, Expecting: %s", s, e) t.Fatalf("Got: %s, Expecting: %s", s, e)
...@@ -336,7 +356,7 @@ func TestDelete(t *testing.T) { ...@@ -336,7 +356,7 @@ func TestDelete(t *testing.T) {
} }
s = trim(stmt.Compile()) s = trim(stmt.Compile())
e = `DELETE FROM "table name" WHERE ("baz" = "99")` e = `DELETE FROM "table name" WHERE ("baz" = '99')`
if s != e { if s != e {
t.Fatalf("Got: %s, Expecting: %s", s, e) t.Fatalf("Got: %s, Expecting: %s", s, e)
...@@ -359,7 +379,7 @@ func TestUpdate(t *testing.T) { ...@@ -359,7 +379,7 @@ func TestUpdate(t *testing.T) {
} }
s = trim(stmt.Compile()) s = trim(stmt.Compile())
e = `UPDATE "table name" SET "foo" = "76" WHERE ("baz" = "99")` e = `UPDATE "table name" SET "foo" = '76' WHERE ("baz" = '99')`
if s != e { if s != e {
t.Fatalf("Got: %s, Expecting: %s", s, e) t.Fatalf("Got: %s, Expecting: %s", s, e)
...@@ -378,7 +398,7 @@ func TestUpdate(t *testing.T) { ...@@ -378,7 +398,7 @@ func TestUpdate(t *testing.T) {
} }
s = trim(stmt.Compile()) s = trim(stmt.Compile())
e = `UPDATE "table name" SET "foo" = "76", "bar" = 88 WHERE ("baz" = "99")` e = `UPDATE "table name" SET "foo" = '76', "bar" = 88 WHERE ("baz" = '99')`
if s != e { if s != e {
t.Fatalf("Got: %s, Expecting: %s", s, e) t.Fatalf("Got: %s, Expecting: %s", s, e)
...@@ -405,7 +425,7 @@ func TestInsert(t *testing.T) { ...@@ -405,7 +425,7 @@ func TestInsert(t *testing.T) {
} }
s = trim(stmt.Compile()) s = trim(stmt.Compile())
e = `INSERT INTO "table name" ("foo", "bar", "baz") VALUES ("1", "2", 3)` e = `INSERT INTO "table name" ("foo", "bar", "baz") VALUES ('1', '2', 3)`
if s != e { if s != e {
t.Fatalf("Got: %s, Expecting: %s", s, e) t.Fatalf("Got: %s, Expecting: %s", s, e)
......
...@@ -9,5 +9,5 @@ type Table struct { ...@@ -9,5 +9,5 @@ type Table struct {
} }
func (self Table) String() string { func (self Table) String() string {
return mustParse(sqlEscape, Raw{fmt.Sprintf(`%v`, self.v)}) return mustParse(sqlIdentifierQuote, Raw{fmt.Sprintf(`%v`, self.v)})
} }
...@@ -15,8 +15,7 @@ func (self Value) String() string { ...@@ -15,8 +15,7 @@ func (self Value) String() string {
if raw, ok := self.v.(Raw); ok { if raw, ok := self.v.(Raw); ok {
return raw.Raw return raw.Raw
} }
return mustParse(sqlValueQuote, Raw{fmt.Sprintf(`%v`, self.v)})
return mustParse(sqlEscape, Raw{fmt.Sprintf(`%v`, self.v)})
} }
func (self Values) String() string { func (self Values) String() string {
...@@ -29,7 +28,7 @@ func (self Values) String() string { ...@@ -29,7 +28,7 @@ func (self Values) String() string {
chunks = append(chunks, self[i].String()) chunks = append(chunks, self[i].String())
} }
return strings.Join(chunks, sqlValueComma) return strings.Join(chunks, sqlValueSeparator)
} }
return "" return ""
......
...@@ -11,7 +11,7 @@ func TestValue(t *testing.T) { ...@@ -11,7 +11,7 @@ func TestValue(t *testing.T) {
val = Value{1} val = Value{1}
s = val.String() s = val.String()
e = `"1"` e = `'1'`
if s != e { if s != e {
t.Fatalf("Got: %s, Expecting: %s", s, e) t.Fatalf("Got: %s, Expecting: %s", s, e)
...@@ -38,7 +38,7 @@ func TestValues(t *testing.T) { ...@@ -38,7 +38,7 @@ func TestValues(t *testing.T) {
} }
s = val.String() s = val.String()
e = `1, 2, "3"` e = `1, 2, '3'`
if s != e { if s != e {
t.Fatalf("Got: %s, Expecting: %s", s, e) t.Fatalf("Got: %s, Expecting: %s", s, e)
......
...@@ -10,16 +10,24 @@ type ( ...@@ -10,16 +10,24 @@ type (
Where []interface{} Where []interface{}
) )
type conds struct {
Conds string
}
func (self Or) String() string { func (self Or) String() string {
return groupCondition(self, sqlOrKeyword) return groupCondition(self, mustParse(sqlClauseOperator, sqlOrKeyword))
} }
func (self And) String() string { func (self And) String() string {
return groupCondition(self, sqlAndKeyword) return groupCondition(self, mustParse(sqlClauseOperator, sqlAndKeyword))
} }
func (self Where) String() string { func (self Where) String() string {
return groupCondition(self, sqlAndKeyword) grouped := groupCondition(self, mustParse(sqlClauseOperator, sqlAndKeyword))
if grouped != "" {
return mustParse(sqlWhereLayout, conds{grouped})
}
return ""
} }
func groupCondition(terms []interface{}, joinKeyword string) string { func groupCondition(terms []interface{}, joinKeyword string) string {
...@@ -44,7 +52,7 @@ func groupCondition(terms []interface{}, joinKeyword string) string { ...@@ -44,7 +52,7 @@ func groupCondition(terms []interface{}, joinKeyword string) string {
} }
if len(chunks) > 0 { if len(chunks) > 0 {
return mustParse(sqlConditionGroup, strings.Join(chunks, " "+joinKeyword+" ")) return mustParse(sqlClauseGroup, strings.Join(chunks, joinKeyword))
} }
return "" return ""
......
...@@ -15,7 +15,7 @@ func TestWhereAnd(t *testing.T) { ...@@ -15,7 +15,7 @@ func TestWhereAnd(t *testing.T) {
} }
s = and.String() s = and.String()
e = `("id" > 8 AND "id" < 99 AND "name" = "John")` e = `("id" > 8 AND "id" < 99 AND "name" = 'John')`
if s != e { if s != e {
t.Fatalf("Got: %s, Expecting: %s", s, e) t.Fatalf("Got: %s, Expecting: %s", s, e)
...@@ -54,7 +54,7 @@ func TestWhereAndOr(t *testing.T) { ...@@ -54,7 +54,7 @@ func TestWhereAndOr(t *testing.T) {
} }
s = and.String() s = and.String()
e = `("id" > 8 AND "id" < 99 AND "name" = "John" AND ("last_name" = "Smith" OR "last_name" = "Reyes"))` e = `("id" > 8 AND "id" < 99 AND "name" = 'John' AND ("last_name" = 'Smith' OR "last_name" = 'Reyes'))`
if s != e { if s != e {
t.Fatalf("Got: %s, Expecting: %s", s, e) t.Fatalf("Got: %s, Expecting: %s", s, e)
...@@ -82,8 +82,8 @@ func TestWhereAndRawOrAnd(t *testing.T) { ...@@ -82,8 +82,8 @@ func TestWhereAndRawOrAnd(t *testing.T) {
}, },
} }
s = where.String() s = trim(where.String())
e = `(("id" > 8 AND "id" < 99) AND "name" = "John" AND city_id = 728 AND ("last_name" = "Smith" OR "last_name" = "Reyes") AND ("age" > 18 AND "age" < 41))` e = `WHERE (("id" > 8 AND "id" < 99) AND "name" = 'John' AND city_id = 728 AND ("last_name" = 'Smith' OR "last_name" = 'Reyes') AND ("age" > 18 AND "age" < 41))`
if s != e { if s != e {
t.Fatalf("Got: %s, Expecting: %s", s, e) t.Fatalf("Got: %s, Expecting: %s", s, e)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment