Skip to content

Commit

Permalink
enable parse sqlite (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunary authored Oct 2, 2024
1 parent c8b77db commit a6deebe
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 20 deletions.
6 changes: 5 additions & 1 deletion sql-parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ func (p *Parser) Parser(sql string) error {
switch p.dialect {
case sql_templates.PostgresDialect:
return p.ParserPostgresql(sql)

case sql_templates.SqliteDialect:
return p.ParserSqlite(sql)

default:
// TODO: mysql parser is default for other dialects
// TODO: mysql parser is default for remaining dialects
return p.ParserMysql(sql)
}
}
Expand Down
5 changes: 3 additions & 2 deletions sql-parser/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ AlterTableStatement
sqlite does not support drop column
*/
func (p Parser) Visit(node sqlite.Node) (w sqlite.Visitor, err error) {
func (p *Parser) Visit(node sqlite.Node) (w sqlite.Visitor, err error) {
switch n := node.(type) {
case *sqlite.CreateTableStatement:
tbName := n.Table.String()
tbName := n.Name.String()
tb := element.NewTableWithAction(tbName, element.MigrateAddAction)
p.Migration.AddTable(*tb)
p.Migration.Using(tbName)

// TODO: rqlite/sql doesn't support parse constraints
for i := range n.Columns {
col := element.Column{
Node: element.Node{
Expand Down
3 changes: 2 additions & 1 deletion sql-templates/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ func (s Sql) EscapeSqlName(name string) string {
}

escapeChar := "`"
if s.dialect == PostgresDialect {
switch s.dialect {
case PostgresDialect, SqliteDialect:
escapeChar = "\""
}

Expand Down
10 changes: 8 additions & 2 deletions sql-templates/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ func (s Sql) DoubleType() string {

// TextType ...
func (s Sql) TextType() string {
return s.apply("TEXT")
switch s.dialect {
case SqliteDialect:
return "TEXT"

default:
return s.apply("TEXT")
}
}

// DatetimeType ...
Expand All @@ -67,7 +73,7 @@ func (s Sql) DatetimeType() string {
return s.apply("TIMESTAMP")

case SqliteDialect:
return s.apply("TEXT") // TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS")
return "TEXT" // TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS")

default:
return s.apply("DATETIME")
Expand Down
20 changes: 9 additions & 11 deletions sqlize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ func (tpl) TableName() string {

type tpl_sqlite struct {
B1 Base `sql:"embedded"`
ClientID string `sql:"type:text;primary_key;index_columns:client_id,country"`
Country string `sql:"type:text"`
Email string `sql:"type:text;unique"`
ClientID string `sql:"type:TEXT;primary_key;index_columns:client_id,country"`
Country string `sql:"type:TEXT"`
Email string `sql:"type:TEXT;unique"`
}

func (tpl_sqlite) TableName() string {
Expand Down Expand Up @@ -231,14 +231,12 @@ ALTER TABLE three_pl ADD CONSTRAINT fk_user_three_pl FOREIGN KEY (email) REFEREN
DROP TABLE IF EXISTS three_pl;`
expectCreateTplSqliteUp = `
CREATE TABLE three_pl_sqlite (
client_id text,
country text,
email text,
created_at text,
updated_at text
);
ALTER TABLE three_pl_sqlite ADD PRIMARY KEY(client_id, country);
CREATE UNIQUE INDEX idx_email ON three_pl_sqlite(email);`
client_id TEXT,
country TEXT,
email TEXT,
created_at TEXT,
updated_at TEXT
);`
expectCreateTplSqliteDown = `
DROP TABLE IF EXISTS three_pl_sqlite;`

Expand Down
3 changes: 1 addition & 2 deletions utils/file.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utils

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -44,7 +43,7 @@ func glob(path string, suffix string) ([]string, error) {
files := []string{}
// listing
if f.IsDir() {
listing, err := ioutil.ReadDir(path)
listing, err := os.ReadDir(path)
if err != nil {
return nil, err
}
Expand Down
1 change: 0 additions & 1 deletion utils/slc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@ func ContainStr(ss []string, s string) bool {
return true
}
}

return false
}

0 comments on commit a6deebe

Please sign in to comment.