From a8a962bb090869cfb6c91abeff770e0d95c3adf8 Mon Sep 17 00:00:00 2001 From: sunary Date: Wed, 2 Oct 2024 17:51:04 +0700 Subject: [PATCH] enable parse sqlite --- sql-parser/parser.go | 6 +++++- sql-parser/sqlite.go | 5 +++-- sql-templates/option.go | 3 ++- sql-templates/type.go | 10 ++++++++-- sqlize_test.go | 20 +++++++++----------- utils/file.go | 3 +-- utils/slc.go | 1 - 7 files changed, 28 insertions(+), 20 deletions(-) diff --git a/sql-parser/parser.go b/sql-parser/parser.go index 5da335c..ec410d8 100644 --- a/sql-parser/parser.go +++ b/sql-parser/parser.go @@ -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) } } diff --git a/sql-parser/sqlite.go b/sql-parser/sqlite.go index 1fa6f58..e05a8df 100644 --- a/sql-parser/sqlite.go +++ b/sql-parser/sqlite.go @@ -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{ diff --git a/sql-templates/option.go b/sql-templates/option.go index c183426..25d909b 100644 --- a/sql-templates/option.go +++ b/sql-templates/option.go @@ -77,7 +77,8 @@ func (s Sql) EscapeSqlName(name string) string { } escapeChar := "`" - if s.dialect == PostgresDialect { + switch s.dialect { + case PostgresDialect, SqliteDialect: escapeChar = "\"" } diff --git a/sql-templates/type.go b/sql-templates/type.go index 6a4f7a7..c2bd8f5 100644 --- a/sql-templates/type.go +++ b/sql-templates/type.go @@ -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 ... @@ -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") diff --git a/sqlize_test.go b/sqlize_test.go index 446e4bb..b0c6759 100644 --- a/sqlize_test.go +++ b/sqlize_test.go @@ -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 { @@ -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;` diff --git a/utils/file.go b/utils/file.go index 144211c..e0b76cf 100644 --- a/utils/file.go +++ b/utils/file.go @@ -1,7 +1,6 @@ package utils import ( - "io/ioutil" "os" "path/filepath" "strings" @@ -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 } diff --git a/utils/slc.go b/utils/slc.go index 0c7fa7e..e66c141 100644 --- a/utils/slc.go +++ b/utils/slc.go @@ -22,6 +22,5 @@ func ContainStr(ss []string, s string) bool { return true } } - return false }