Skip to content

Support ALTER TABLE for SQLite #414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions internal/sqlite/catalog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package sqlite

import "github.com/kyleconroy/sqlc/internal/sql/catalog"

func NewCatalog() *catalog.Catalog {
c := catalog.New("main")
return c
}
269 changes: 269 additions & 0 deletions internal/sqlite/catalog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
package sqlite

import (
"strconv"
"strings"
"testing"

"github.com/kyleconroy/sqlc/internal/sql/ast"
"github.com/kyleconroy/sqlc/internal/sql/catalog"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

func TestUpdate(t *testing.T) {
p := NewParser()

for i, tc := range []struct {
stmt string
s *catalog.Schema
}{
{
`
CREATE TABLE foo (bar text);
`,
&catalog.Schema{
Name: "main",
Tables: []*catalog.Table{
{
Rel: &ast.TableName{Name: "foo"},
Columns: []*catalog.Column{
{
Name: "bar",
Type: ast.TypeName{Name: "text"},
},
},
},
},
},
},
{
`
CREATE TABLE foo (bar text);
ALTER TABLE foo RENAME TO baz;
`,
&catalog.Schema{
Name: "main",
Tables: []*catalog.Table{
{
Rel: &ast.TableName{Name: "baz"},
Columns: []*catalog.Column{
{
Name: "bar",
Type: ast.TypeName{Name: "text"},
},
},
},
},
},
},
{
`
CREATE TABLE foo (bar text);
ALTER TABLE foo ADD COLUMN baz bool;
`,
&catalog.Schema{
Name: "main",
Tables: []*catalog.Table{
{
Rel: &ast.TableName{Name: "foo"},
Columns: []*catalog.Column{
{
Name: "bar",
Type: ast.TypeName{Name: "text"},
},
{
Name: "baz",
Type: ast.TypeName{Name: "bool"},
},
},
},
},
},
},
{
`
CREATE TABLE foo (bar text);
ALTER TABLE foo RENAME COLUMN bar TO baz;
`,
&catalog.Schema{
Name: "main",
Tables: []*catalog.Table{
{
Rel: &ast.TableName{Name: "foo"},
Columns: []*catalog.Column{
{
Name: "baz",
Type: ast.TypeName{Name: "text"},
},
},
},
},
},
},
{
`
CREATE TABLE foo (bar text);
ALTER TABLE foo RENAME bar TO baz;
`,
&catalog.Schema{
Name: "main",
Tables: []*catalog.Table{
{
Rel: &ast.TableName{Name: "foo"},
Columns: []*catalog.Column{
{
Name: "baz",
Type: ast.TypeName{Name: "text"},
},
},
},
},
},
},
{
`
ATTACH ':memory:' as ns;
CREATE TABLE ns.foo (bar text);
`,
&catalog.Schema{
Name: "ns",
Tables: []*catalog.Table{
{
Rel: &ast.TableName{Schema: "ns", Name: "foo"},
Columns: []*catalog.Column{
{
Name: "bar",
Type: ast.TypeName{Name: "text"},
},
},
},
},
},
},
{
`
ATTACH ':memory:' as ns;
CREATE TABLE ns.foo (bar text);
ALTER TABLE ns.foo RENAME TO baz;
`,
&catalog.Schema{
Name: "ns",
Tables: []*catalog.Table{
{
Rel: &ast.TableName{Schema: "ns", Name: "baz"},
Columns: []*catalog.Column{
{
Name: "bar",
Type: ast.TypeName{Name: "text"},
},
},
},
},
},
},
{
`
ATTACH ':memory:' as ns;
CREATE TABLE ns.foo (bar text);
ALTER TABLE ns.foo ADD COLUMN baz bool;
`,
&catalog.Schema{
Name: "ns",
Tables: []*catalog.Table{
{
Rel: &ast.TableName{Schema: "ns", Name: "foo"},
Columns: []*catalog.Column{
{
Name: "bar",
Type: ast.TypeName{Name: "text"},
},
{
Name: "baz",
Type: ast.TypeName{Name: "bool"},
},
},
},
},
},
},
{
`
ATTACH ':memory:' as ns;
CREATE TABLE ns.foo (bar text);
ALTER TABLE ns.foo RENAME COLUMN bar TO baz;
`,
&catalog.Schema{
Name: "ns",
Tables: []*catalog.Table{
{
Rel: &ast.TableName{Schema: "ns", Name: "foo"},
Columns: []*catalog.Column{
{
Name: "baz",
Type: ast.TypeName{Name: "text"},
},
},
},
},
},
},
{
`
ATTACH ':memory:' as ns;
CREATE TABLE ns.foo (bar text);
ALTER TABLE ns.foo RENAME bar TO baz;
`,
&catalog.Schema{
Name: "ns",
Tables: []*catalog.Table{
{
Rel: &ast.TableName{Schema: "ns", Name: "foo"},
Columns: []*catalog.Column{
{
Name: "baz",
Type: ast.TypeName{Name: "text"},
},
},
},
},
},
},
} {
test := tc
t.Run(strconv.Itoa(i), func(t *testing.T) {
stmts, err := p.Parse(strings.NewReader(test.stmt))
if err != nil {
t.Log(test.stmt)
t.Fatal(err)
}

c := NewCatalog()
if err := c.Build(stmts); err != nil {
t.Log(test.stmt)
t.Fatal(err)
}

e := NewCatalog()
if test.s != nil {
var replaced bool
for i := range e.Schemas {
if e.Schemas[i].Name == test.s.Name {
e.Schemas[i] = test.s
replaced = true
break
}
}
if !replaced {
e.Schemas = append(e.Schemas, test.s)
}
}

if diff := cmp.Diff(e, c, cmpopts.EquateEmpty()); diff != "" {
t.Log(test.stmt)
t.Errorf("catalog mismatch:\n%s", diff)
}
})
}
}
45 changes: 41 additions & 4 deletions internal/sqlite/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,34 @@ func (l *listener) EnterAlter_table_stmt(c *parser.Alter_table_stmtContext) {
return
}

stmt := &ast.AlterTableStmt{
Table: parseTableName(c),
Cmds: &ast.List{},
if newTable, ok := c.New_table_name().(*parser.New_table_nameContext); ok {
name := newTable.Any_name().GetText()
stmt := &ast.RenameTableStmt{
Table: parseTableName(c),
NewName: &name,
}
l.stmt = &ast.RawStmt{Stmt: stmt}
return
}

if newCol, ok := c.New_column_name().(*parser.New_column_nameContext); ok {
name := newCol.Any_name().GetText()
stmt := &ast.RenameColumnStmt{
Table: parseTableName(c),
Col: &ast.ColumnRef{
Name: c.Column_name().GetText(),
},
NewName: &name,
}
l.stmt = &ast.RawStmt{Stmt: stmt}
return
}

if def, ok := c.Column_def().(*parser.Column_defContext); ok {
stmt := &ast.AlterTableStmt{
Table: parseTableName(c),
Cmds: &ast.List{},
}
name := def.Column_name().GetText()
stmt.Cmds.Items = append(stmt.Cmds.Items, &ast.AlterTableCmd{
Name: &name,
Expand All @@ -56,10 +78,23 @@ func (l *listener) EnterAlter_table_stmt(c *parser.Alter_table_stmtContext) {
},
},
})
l.stmt = &ast.RawStmt{Stmt: stmt}
return
}

l.stmt = &ast.RawStmt{Stmt: stmt}
}

func (l *listener) EnterAttach_stmt(c *parser.Attach_stmtContext) {
if l.busy() {
return
}

name := c.Database_name().GetText()
stmt := &ast.CreateSchemaStmt{
Name: &name,
}

l.stmt = &ast.RawStmt{Stmt: stmt}
}

func (l *listener) EnterCreate_table_stmt(c *parser.Create_table_stmtContext) {
Expand Down Expand Up @@ -149,3 +184,5 @@ func (l *listener) EnterFactored_select_stmt(c *parser.Factored_select_stmtConte
}
l.stmt = &ast.RawStmt{Stmt: sel}
}

var _ parser.SQLiteListener = (*listener)(nil)
2 changes: 1 addition & 1 deletion internal/sqlite/parser/Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
sqlite_parser.go: SQLite.g4
antlr -Dlanguage=Go -visitor SQLite.g4
antlr -Dlanguage=Go SQLite.g4
5 changes: 5 additions & 0 deletions internal/sqlite/parser/SQLite.g4
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ sql_stmt
alter_table_stmt
: K_ALTER K_TABLE ( database_name '.' )? table_name
( K_RENAME K_TO new_table_name
| K_RENAME K_COLUMN? column_name K_TO new_column_name
| K_ADD K_COLUMN? column_def
)
;
Expand Down Expand Up @@ -633,6 +634,10 @@ column_name
: any_name
;

new_column_name
: any_name
;

collation_name
: any_name
;
Expand Down
3 changes: 2 additions & 1 deletion internal/sqlite/parser/SQLite.interp

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions internal/sqlite/parser/sqlite_base_listener.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading