Skip to content

Commit c33cb23

Browse files
committed
Added joins to lang and refactors
1 parent dd5c3ba commit c33cb23

22 files changed

+340
-354
lines changed

pkg/indexer/schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func Delete(schema string, evt *QueueEvent) (SQStatement, []interface{}) {
155155
[]interface{}{evt.Name, evt.Path}
156156
}
157157

158-
func Query(schema string) SQSelect {
158+
func Query(schema string, indexes []string) SQSelect {
159159
return S(N(searchTableName).WithSchema(schema)).
160160
To(
161161
N("rowid"),

pkg/lang/alter_test.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
11
package lang_test
22

33
import (
4-
"fmt"
54
"testing"
65

6+
// Namespace imports
7+
. "github.com/mutablelogic/go-sqlite"
78
. "github.com/mutablelogic/go-sqlite/pkg/lang"
89
)
910

1011
func Test_Alter_000(t *testing.T) {
1112
tests := []struct {
12-
In Statement
13-
String string
14-
Query string
13+
In SQStatement
14+
Query string
1515
}{
16-
{N("foo").AlterTable(), `ALTER TABLE foo`, ``},
17-
{N("foo").WithSchema("main").AlterTable(), `ALTER TABLE main.foo`, ``},
18-
{N("foo").WithSchema("main").AlterTable().DropColumn(C("a")), `ALTER TABLE main.foo DROP COLUMN a`, ``},
19-
{N("foo").WithSchema("main").AlterTable().AddColumn(C("a")), `ALTER TABLE main.foo ADD COLUMN a TEXT`, ``},
20-
{N("foo").WithSchema("main").AlterTable().AddColumn(C("a").NotNull()), `ALTER TABLE main.foo ADD COLUMN a TEXT NOT NULL`, ``},
21-
{N("foo").WithSchema("main").AlterTable().AddColumn(C("a").WithPrimary()), `ALTER TABLE main.foo ADD COLUMN a TEXT NOT NULL PRIMARY KEY`, ``},
16+
{N("foo").AlterTable(), `ALTER TABLE foo`},
17+
{N("foo").WithSchema("main").AlterTable(), `ALTER TABLE main.foo`},
18+
{N("foo").WithSchema("main").AlterTable().DropColumn(C("a")), `ALTER TABLE main.foo DROP COLUMN a`},
19+
{N("foo").WithSchema("main").AlterTable().AddColumn(C("a")), `ALTER TABLE main.foo ADD COLUMN a TEXT`},
20+
{N("foo").WithSchema("main").AlterTable().AddColumn(C("a").NotNull()), `ALTER TABLE main.foo ADD COLUMN a TEXT NOT NULL`},
21+
{N("foo").WithSchema("main").AlterTable().AddColumn(C("a").WithPrimary()), `ALTER TABLE main.foo ADD COLUMN a TEXT NOT NULL PRIMARY KEY`},
2222
}
2323

2424
for _, test := range tests {
25-
if v := fmt.Sprint(test.In); v != test.String {
26-
t.Errorf("Unexpected return from String(): %q, wanted %q", v, test.String)
27-
}
28-
if test.Query != "" {
29-
if v := test.In.Query(); v != test.Query {
30-
t.Errorf("Unexpected return from Query(): %q, wanted %q", v, test.Query)
31-
}
25+
if test.In == nil {
26+
t.Errorf("Unexpected nil return for %q", test.Query)
27+
} else if v := test.In.Query(); v != test.Query {
28+
t.Errorf("Unexpected return from Query(): %q, wanted %q", v, test.Query)
3229
}
3330
}
3431
}

pkg/lang/column.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,6 @@ func (this *column) WithDefaultNow() SQColumn {
9292
///////////////////////////////////////////////////////////////////////////////
9393
// STRINGIFY
9494

95-
func (this *column) Query() string {
96-
return this.String()
97-
}
98-
9995
func (this *column) String() string {
10096
tokens := []string{QuoteIdentifier(this.Name())}
10197
if this.decltype != "" {

pkg/lang/column_test.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,28 @@
11
package lang_test
22

33
import (
4-
"fmt"
54
"testing"
65

6+
. "github.com/mutablelogic/go-sqlite"
77
. "github.com/mutablelogic/go-sqlite/pkg/lang"
88
)
99

1010
func Test_Column_000(t *testing.T) {
1111
// Check db.C (column)
1212
tests := []struct {
13-
In Statement
13+
In SQExpr
1414
String string
15-
Query string
1615
}{
17-
{C("a"), `a TEXT`, ``},
18-
{C("a").WithType("BLOB"), `a BLOB`, ``},
19-
{C("a").NotNull(), `a TEXT NOT NULL`, ``},
20-
{C("a").WithType("VARCHAR"), `a "VARCHAR"`, ``},
21-
{C("a").WithAlias("b"), `a AS b`, ``},
16+
{C("a"), `a TEXT`},
17+
{C("a").WithType("BLOB"), `a BLOB`},
18+
{C("a").NotNull(), `a TEXT NOT NULL`},
19+
{C("a").WithType("VARCHAR"), `a VARCHAR`},
20+
{C("a").WithAlias("b"), `a AS b`},
2221
}
2322

2423
for _, test := range tests {
25-
if v := fmt.Sprint(test.In); v != test.String {
24+
if v := test.In.String(); v != test.String {
2625
t.Errorf("db.N = %v, wanted %v", v, test.String)
2726
}
28-
if test.Query != "" {
29-
if v := test.In.Query(); v != test.Query {
30-
t.Errorf("db.N = %v, wanted %v", v, test.Query)
31-
}
32-
}
3327
}
3428
}

pkg/lang/create_test.go

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,62 @@
11
package lang_test
22

33
import (
4-
"fmt"
54
"testing"
65

6+
// Namespace imports
7+
. "github.com/mutablelogic/go-sqlite"
78
. "github.com/mutablelogic/go-sqlite/pkg/lang"
89
)
910

1011
func Test_Create_000(t *testing.T) {
1112
tests := []struct {
12-
In Statement
13-
String string
14-
Query string
13+
In SQStatement
14+
Query string
1515
}{
16-
{N("foo").CreateTable(), `CREATE TABLE foo ()`, ``},
17-
{N("foo").CreateTable().WithTemporary(), `CREATE TEMPORARY TABLE foo ()`, ``},
18-
{N("foo").CreateTable().IfNotExists(), `CREATE TABLE IF NOT EXISTS foo ()`, ``},
19-
{N("foo").CreateTable(C("a"), C("b")), `CREATE TABLE foo (a TEXT,b TEXT)`, ``},
20-
{N("test").CreateTable(), "CREATE TABLE test ()", ""},
21-
{N("test").WithSchema("main").CreateTable(), "CREATE TABLE main.test ()", ""},
22-
{N("test").CreateTable().WithTemporary(), "CREATE TEMPORARY TABLE test ()", ""},
23-
{N("test").CreateTable().IfNotExists(), "CREATE TABLE IF NOT EXISTS test ()", ""},
24-
{N("test").CreateTable().WithoutRowID(), "CREATE TABLE test () WITHOUT ROWID", ""},
25-
{N("foo").CreateTable(C("a").NotNull(), C("b").NotNull()), `CREATE TABLE foo (a TEXT NOT NULL,b TEXT NOT NULL)`, ``},
26-
{N("foo").CreateTable(C("a").WithPrimary(), C("b")), `CREATE TABLE foo (a TEXT NOT NULL PRIMARY KEY,b TEXT)`, ``},
27-
{N("test").CreateTable(N("a").WithType("TEXT"), N("b").WithType("TEXT")), "CREATE TABLE test (a TEXT,b TEXT)", ""},
28-
{N("test").CreateTable(N("a").WithType("TEXT").NotNull(), N("b").WithType("TEXT").NotNull()), "CREATE TABLE test (a TEXT NOT NULL,b TEXT NOT NULL)", ""},
29-
{N("test").CreateTable(N("a").WithType("CUSTOM TYPE")), "CREATE TABLE test (a \"CUSTOM TYPE\")", ""},
30-
{N("test").CreateTable(N("a").WithType("CUSTOM TYPE").NotNull()), "CREATE TABLE test (a \"CUSTOM TYPE\" NOT NULL)", ""},
31-
{N("test").CreateTable(N("a").WithType("TEXT").WithPrimary()), "CREATE TABLE test (a TEXT NOT NULL PRIMARY KEY)", ""},
32-
{N("test").CreateTable(N("a").WithType("TEXT").WithPrimary(), N("b").WithType("TEXT").WithPrimary()), "CREATE TABLE test (a TEXT NOT NULL,b TEXT NOT NULL,PRIMARY KEY (a,b))", ""},
33-
{N("test").CreateTable(N("a").WithType("TEXT")).WithIndex("a"), "CREATE TABLE test (a TEXT,INDEX (a))", ""},
34-
{N("test").CreateTable(N("a").WithType("TEXT"), N("b").WithType("TEXT")).WithIndex("a", "b"), "CREATE TABLE test (a TEXT,b TEXT,INDEX (a,b))", ""},
35-
{N("test").CreateTable(N("a").WithType("TEXT"), N("b").WithType("TEXT")).WithUnique("a").WithUnique("b"), "CREATE TABLE test (a TEXT,b TEXT,UNIQUE (a),UNIQUE (b))", ""},
36-
{N("test").CreateTable(N("a").WithType("TEXT").WithPrimary(), N("b").WithType("TEXT")).WithUnique("b"), "CREATE TABLE test (a TEXT NOT NULL PRIMARY KEY,b TEXT,UNIQUE (b))", ""},
37-
{N("test").CreateTable(N("a").WithType("TEXT").WithAutoIncrement(), N("b").WithType("TEXT")).WithUnique("b"), "CREATE TABLE test (a TEXT NOT NULL PRIMARY KEY AUTOINCREMENT,b TEXT,UNIQUE (b))", ""},
16+
{N("foo").CreateTable(), `CREATE TABLE foo ()`},
17+
{N("foo").CreateTable().WithTemporary(), `CREATE TEMPORARY TABLE foo ()`},
18+
{N("foo").CreateTable().IfNotExists(), `CREATE TABLE IF NOT EXISTS foo ()`},
19+
{N("foo").CreateTable(C("a"), C("b")), `CREATE TABLE foo (a TEXT,b TEXT)`},
20+
{N("test").CreateTable(), "CREATE TABLE test ()"},
21+
{N("test").WithSchema("main").CreateTable(), "CREATE TABLE main.test ()"},
22+
{N("test").CreateTable().WithTemporary(), "CREATE TEMPORARY TABLE test ()"},
23+
{N("test").CreateTable().IfNotExists(), "CREATE TABLE IF NOT EXISTS test ()"},
24+
{N("test").CreateTable().WithoutRowID(), "CREATE TABLE test () WITHOUT ROWID"},
25+
{N("foo").CreateTable(C("a").NotNull(), C("b").NotNull()), `CREATE TABLE foo (a TEXT NOT NULL,b TEXT NOT NULL)`},
26+
{N("foo").CreateTable(C("a").WithPrimary(), C("b")), `CREATE TABLE foo (a TEXT NOT NULL PRIMARY KEY,b TEXT)`},
27+
{N("test").CreateTable(N("a").WithType("TEXT"), N("b").WithType("TEXT")), "CREATE TABLE test (a TEXT,b TEXT)"},
28+
{N("test").CreateTable(N("a").WithType("TEXT").NotNull(), N("b").WithType("TEXT").NotNull()), "CREATE TABLE test (a TEXT NOT NULL,b TEXT NOT NULL)"},
29+
{N("test").CreateTable(N("a").WithType("CUSTOM TYPE")), "CREATE TABLE test (a \"CUSTOM TYPE\")"},
30+
{N("test").CreateTable(N("a").WithType("CUSTOM TYPE").NotNull()), "CREATE TABLE test (a \"CUSTOM TYPE\" NOT NULL)"},
31+
{N("test").CreateTable(N("a").WithType("TEXT").WithPrimary()), "CREATE TABLE test (a TEXT NOT NULL PRIMARY KEY)"},
32+
{N("test").CreateTable(N("a").WithType("TEXT").WithPrimary(), N("b").WithType("TEXT").WithPrimary()), "CREATE TABLE test (a TEXT NOT NULL,b TEXT NOT NULL,PRIMARY KEY (a,b))"},
33+
{N("test").CreateTable(N("a").WithType("TEXT")).WithIndex("a"), "CREATE TABLE test (a TEXT,INDEX (a))"},
34+
{N("test").CreateTable(N("a").WithType("TEXT"), N("b").WithType("TEXT")).WithIndex("a", "b"), "CREATE TABLE test (a TEXT,b TEXT,INDEX (a,b))"},
35+
{N("test").CreateTable(N("a").WithType("TEXT"), N("b").WithType("TEXT")).WithUnique("a").WithUnique("b"), "CREATE TABLE test (a TEXT,b TEXT,UNIQUE (a),UNIQUE (b))"},
36+
{N("test").CreateTable(N("a").WithType("TEXT").WithPrimary(), N("b").WithType("TEXT")).WithUnique("b"), "CREATE TABLE test (a TEXT NOT NULL PRIMARY KEY,b TEXT,UNIQUE (b))"},
37+
{N("test").CreateTable(N("a").WithType("TEXT").WithAutoIncrement(), N("b").WithType("TEXT")).WithUnique("b"), "CREATE TABLE test (a TEXT NOT NULL PRIMARY KEY AUTOINCREMENT,b TEXT,UNIQUE (b))"},
3838
}
3939

4040
for _, test := range tests {
41-
if v := fmt.Sprint(test.In); v != test.String {
42-
t.Errorf("Unexpected return from String(): %q, wanted %q", v, test.String)
43-
} else {
44-
t.Log(v)
45-
}
46-
if test.Query != "" {
47-
if v := test.In.Query(); v != test.Query {
48-
t.Errorf("Unexpected return from Query(): %q, wanted %q", v, test.Query)
49-
}
41+
if v := test.In.Query(); v != test.Query {
42+
t.Errorf("Unexpected return from Query(): %q, wanted %q", v, test.Query)
5043
}
5144
}
5245
}
5346

5447
func Test_Create_001(t *testing.T) {
5548
tests := []struct {
56-
In Statement
57-
String string
58-
Query string
49+
In SQStatement
50+
Query string
5951
}{
60-
{N("foo").CreateTable(N("a").WithType("TEXT")).WithForeignKey(N("bar").ForeignKey(), "a"), `CREATE TABLE foo (a TEXT,FOREIGN KEY (a) REFERENCES bar)`, ``},
61-
{N("foo").CreateTable(N("a").WithType("TEXT")).WithForeignKey(N("bar").ForeignKey("x", "y"), "a"), `CREATE TABLE foo (a TEXT,FOREIGN KEY (a) REFERENCES bar (x,y))`, ``},
62-
{N("foo").CreateTable(N("a").WithType("TEXT")).WithForeignKey(N("bar").ForeignKey(), "a"), `CREATE TABLE foo (a TEXT,FOREIGN KEY (a) REFERENCES bar)`, ``},
52+
{N("foo").CreateTable(N("a").WithType("TEXT")).WithForeignKey(N("bar").ForeignKey(), "a"), `CREATE TABLE foo (a TEXT,FOREIGN KEY (a) REFERENCES bar)`},
53+
{N("foo").CreateTable(N("a").WithType("TEXT")).WithForeignKey(N("bar").ForeignKey("x", "y"), "a"), `CREATE TABLE foo (a TEXT,FOREIGN KEY (a) REFERENCES bar (x,y))`},
54+
{N("foo").CreateTable(N("a").WithType("TEXT")).WithForeignKey(N("bar").ForeignKey(), "a"), `CREATE TABLE foo (a TEXT,FOREIGN KEY (a) REFERENCES bar)`},
6355
}
6456

6557
for _, test := range tests {
66-
if v := fmt.Sprint(test.In); v != test.String {
67-
t.Errorf("Unexpected return from String(): %q, wanted %q", v, test.String)
68-
} else {
69-
t.Log(v)
70-
}
71-
if test.Query != "" {
72-
if v := test.In.Query(); v != test.Query {
73-
t.Errorf("Unexpected return from Query(): %q, wanted %q", v, test.Query)
74-
}
58+
if v := test.In.Query(); v != test.Query {
59+
t.Errorf("Unexpected return from Query(): %q, wanted %q", v, test.Query)
7560
}
7661
}
7762
}

pkg/lang/delete.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@ import (
44
"fmt"
55
"strings"
66

7-
sqlite "github.com/mutablelogic/go-sqlite"
7+
// Namespace imports
8+
. "github.com/mutablelogic/go-sqlite"
89
)
910

1011
///////////////////////////////////////////////////////////////////////////////
1112
// TYPES
1213

1314
type delete struct {
14-
source sqlite.SQSource
15+
source SQSource
1516
where []interface{}
1617
}
1718

1819
///////////////////////////////////////////////////////////////////////////////
1920
// LIFECYCLE
2021

2122
// Update values in a table with a name and defined column names
22-
func (this *source) Delete(expr ...interface{}) sqlite.SQStatement {
23+
func (this *source) Delete(expr ...interface{}) SQStatement {
2324
if len(expr) == 0 {
2425
return nil
2526
} else {
@@ -35,7 +36,7 @@ func (this *delete) String() string {
3536
}
3637

3738
func (this *delete) Query() string {
38-
tokens := []string{"DELETE FROM", fmt.Sprint(this.source)}
39+
tokens := []string{"DELETE FROM", this.source.String()}
3940

4041
// Where clause
4142
if len(this.where) > 0 {
@@ -44,7 +45,7 @@ func (this *delete) Query() string {
4445
if i > 0 {
4546
tokens = append(tokens, "AND")
4647
}
47-
tokens = append(tokens, fmt.Sprint(expr))
48+
tokens = append(tokens, fmt.Sprint(V(expr)))
4849
}
4950
}
5051

pkg/lang/delete_test.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
11
package lang_test
22

33
import (
4-
"fmt"
54
"testing"
65

6+
// Namespace imports
7+
. "github.com/mutablelogic/go-sqlite"
78
. "github.com/mutablelogic/go-sqlite/pkg/lang"
89
)
910

1011
func Test_Delete_000(t *testing.T) {
1112
tests := []struct {
12-
In Statement
13-
String string
14-
Query string
13+
In SQStatement
14+
Query string
1515
}{
16-
{N("foo").Delete(nil), `DELETE FROM foo WHERE NULL`, ``},
16+
{N("foo").Delete(nil), `DELETE FROM foo WHERE NULL`},
1717
}
1818

1919
for i, test := range tests {
20-
if v := fmt.Sprint(test.In); v != test.String {
21-
t.Errorf("Test %d, Unexpected return from String(): %q, wanted %q", i, v, test.String)
22-
} else {
23-
t.Log(v)
24-
}
25-
if test.Query != "" {
26-
if v := test.In.Query(); v != test.Query {
27-
t.Errorf("Test %d, Unexpected return from Query(): %q, wanted %q", i, v, test.Query)
28-
}
20+
if v := test.In.Query(); v != test.Query {
21+
t.Errorf("Test %d, Unexpected return from Query(): %q, wanted %q", i, v, test.Query)
2922
}
3023
}
3124
}

pkg/lang/drop_test.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,29 @@
11
package lang_test
22

33
import (
4-
"fmt"
54
"testing"
65

6+
// Namespace imports
7+
. "github.com/mutablelogic/go-sqlite"
78
. "github.com/mutablelogic/go-sqlite/pkg/lang"
89
)
910

1011
func Test_Drop_000(t *testing.T) {
1112
tests := []struct {
12-
In Statement
13-
String string
14-
Query string
13+
In SQStatement
14+
Query string
1515
}{
16-
{N("foo").DropTable(), `DROP TABLE foo`, ``},
17-
{N("foo").WithSchema("main").DropTable(), `DROP TABLE main.foo`, ``},
18-
{N("foo").WithSchema("main").DropTable().IfExists(), `DROP TABLE IF EXISTS main.foo`, ``},
19-
{N("foo").DropView().IfExists(), `DROP VIEW IF EXISTS foo`, ``},
20-
{N("foo").DropIndex().IfExists(), `DROP INDEX IF EXISTS foo`, ``},
21-
{N("foo").DropTrigger().IfExists(), `DROP TRIGGER IF EXISTS foo`, ``},
16+
{N("foo").DropTable(), `DROP TABLE foo`},
17+
{N("foo").WithSchema("main").DropTable(), `DROP TABLE main.foo`},
18+
{N("foo").WithSchema("main").DropTable().IfExists(), `DROP TABLE IF EXISTS main.foo`},
19+
{N("foo").DropView().IfExists(), `DROP VIEW IF EXISTS foo`},
20+
{N("foo").DropIndex().IfExists(), `DROP INDEX IF EXISTS foo`},
21+
{N("foo").DropTrigger().IfExists(), `DROP TRIGGER IF EXISTS foo`},
2222
}
2323

2424
for _, test := range tests {
25-
if v := fmt.Sprint(test.In); v != test.String {
26-
t.Errorf("Unexpected return from String(): %q, wanted %q", v, test.String)
27-
}
28-
if test.Query != "" {
29-
if v := test.In.Query(); v != test.Query {
30-
t.Errorf("Unexpected return from Query(): %q, wanted %q", v, test.Query)
31-
}
25+
if v := test.In.Query(); v != test.Query {
26+
t.Errorf("Unexpected return from Query(): %q, wanted %q", v, test.Query)
3227
}
3328
}
3429
}

pkg/lang/expr.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ func V(v interface{}) SQExpr {
4646
return &e{v, nil, ""}
4747
case SQSource, SQStatement:
4848
return &e{v, nil, ""}
49+
case SQExpr:
50+
return &e{v, nil, ""}
4951
}
5052
// Unsupported value
51-
panic(fmt.Sprintf("V unsupported value %q", v))
53+
panic(fmt.Sprintf("V unsupported type %T", v))
5254
}
5355

5456
///////////////////////////////////////////////////////////////////////////////
@@ -72,7 +74,7 @@ func (this *e) Or(v interface{}) SQExpr {
7274
return &e{this.v, v, "OR"}
7375
}
7476
// Unsupported value
75-
panic(fmt.Sprintf("V unsupported value %q", v))
77+
panic(fmt.Sprintf("V unsupported type %T", v))
7678
}
7779

7880
///////////////////////////////////////////////////////////////////////////////
@@ -89,10 +91,6 @@ func (this *e) String() string {
8991
}
9092
}
9193

92-
func (this *e) Query() string {
93-
return "SELECT " + fmt.Sprint(this)
94-
}
95-
9694
///////////////////////////////////////////////////////////////////////////////
9795
// PRIVATE METHODS
9896

@@ -121,6 +119,8 @@ func lhs(v interface{}) string {
121119
return fmt.Sprint(e.WithAlias(""))
122120
case SQStatement:
123121
return e.Query()
122+
case SQExpr:
123+
return e.String()
124124
default:
125125
return Quote(fmt.Sprint(v))
126126
}

0 commit comments

Comments
 (0)