Add jet generator support for SQLite
This commit is contained in:
parent
3f7efb33eb
commit
51cad22809
8 changed files with 154 additions and 31 deletions
|
|
@ -8,9 +8,10 @@ import (
|
|||
// TableType is type of database table(view or base)
|
||||
type TableType string
|
||||
|
||||
// SQL table types
|
||||
const (
|
||||
baseTable TableType = "BASE TABLE"
|
||||
viewTable TableType = "VIEW"
|
||||
BaseTable TableType = "BASE TABLE"
|
||||
ViewTable TableType = "VIEW"
|
||||
)
|
||||
|
||||
// DialectQuerySet is set of methods necessary to retrieve dialect meta data information
|
||||
|
|
@ -23,8 +24,8 @@ type DialectQuerySet interface {
|
|||
func GetSchema(db *sql.DB, querySet DialectQuerySet, schemaName string) Schema {
|
||||
ret := Schema{
|
||||
Name: schemaName,
|
||||
TablesMetaData: querySet.GetTablesMetaData(db, schemaName, baseTable),
|
||||
ViewsMetaData: querySet.GetTablesMetaData(db, schemaName, viewTable),
|
||||
TablesMetaData: querySet.GetTablesMetaData(db, schemaName, BaseTable),
|
||||
ViewsMetaData: querySet.GetTablesMetaData(db, schemaName, ViewTable),
|
||||
EnumsMetaData: querySet.GetEnumsMetaData(db, schemaName),
|
||||
}
|
||||
|
||||
|
|
|
|||
80
generator/sqlite/query_set.go
Normal file
80
generator/sqlite/query_set.go
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/go-jet/jet/v2/generator/metadata"
|
||||
"github.com/go-jet/jet/v2/internal/utils/throw"
|
||||
"github.com/go-jet/jet/v2/qrm"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// sqliteQuerySet is dialect query set for SQLite
|
||||
type sqliteQuerySet struct{}
|
||||
|
||||
func (p sqliteQuerySet) GetTablesMetaData(db *sql.DB, schemaName string, tableType metadata.TableType) []metadata.Table {
|
||||
query := `
|
||||
SELECT name as "table.name"
|
||||
FROM sqlite_master
|
||||
WHERE type=? AND name != 'sqlite_sequence'
|
||||
ORDER BY name;
|
||||
`
|
||||
sqlTableType := "table"
|
||||
|
||||
if tableType == metadata.ViewTable {
|
||||
sqlTableType = "view"
|
||||
}
|
||||
|
||||
var tables []metadata.Table
|
||||
|
||||
err := qrm.Query(context.Background(), db, query, []interface{}{sqlTableType}, &tables)
|
||||
throw.OnError(err)
|
||||
|
||||
for i := range tables {
|
||||
tables[i].Columns = p.GetTableColumnsMetaData(db, schemaName, tables[i].Name)
|
||||
}
|
||||
|
||||
return tables
|
||||
}
|
||||
|
||||
func (p sqliteQuerySet) GetTableColumnsMetaData(db *sql.DB, schemaName string, tableName string) []metadata.Column {
|
||||
query := fmt.Sprintf(`select * from pragma_table_info(?);`)
|
||||
var columnInfos []struct {
|
||||
Name string
|
||||
Type string
|
||||
NotNull int32
|
||||
Pk int32
|
||||
}
|
||||
|
||||
err := qrm.Query(context.Background(), db, query, []interface{}{tableName}, &columnInfos)
|
||||
throw.OnError(err)
|
||||
|
||||
var columns []metadata.Column
|
||||
|
||||
for _, columnInfo := range columnInfos {
|
||||
columnType := getColumnType(columnInfo.Type)
|
||||
|
||||
columns = append(columns, metadata.Column{
|
||||
Name: columnInfo.Name,
|
||||
IsPrimaryKey: columnInfo.Pk != 0,
|
||||
IsNullable: columnInfo.NotNull != 1,
|
||||
DataType: metadata.DataType{
|
||||
Name: columnType,
|
||||
Kind: metadata.BaseType,
|
||||
IsUnsigned: false,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return columns
|
||||
}
|
||||
|
||||
// will convert VARCHAR(10) -> VARCHAR, etc...
|
||||
func getColumnType(columnType string) string {
|
||||
return strings.TrimSpace(strings.Split(columnType, "(")[0])
|
||||
}
|
||||
|
||||
func (p sqliteQuerySet) GetEnumsMetaData(db *sql.DB, schemaName string) []metadata.Enum {
|
||||
return nil
|
||||
}
|
||||
32
generator/sqlite/sqlite_generator.go
Normal file
32
generator/sqlite/sqlite_generator.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package sqlite
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/go-jet/jet/v2/generator/metadata"
|
||||
"github.com/go-jet/jet/v2/generator/template"
|
||||
"github.com/go-jet/jet/v2/internal/utils"
|
||||
"github.com/go-jet/jet/v2/internal/utils/throw"
|
||||
"github.com/go-jet/jet/v2/sqlite"
|
||||
)
|
||||
|
||||
// GenerateDSN generates jet files using dsn connection string
|
||||
func GenerateDSN(dsn, destDir string, templates ...template.Template) (err error) {
|
||||
defer utils.ErrorCatch(&err)
|
||||
|
||||
db, err := sql.Open("sqlite3", dsn)
|
||||
throw.OnError(err)
|
||||
defer utils.DBClose(db)
|
||||
|
||||
fmt.Println("Retrieving schema information...")
|
||||
|
||||
generatorTemplate := template.Default(sqlite.Dialect)
|
||||
if len(templates) > 0 {
|
||||
generatorTemplate = templates[0]
|
||||
}
|
||||
|
||||
schemaMetadata := metadata.GetSchema(db, &sqliteQuerySet{}, "")
|
||||
|
||||
template.ProcessSchema(destDir, schemaMetadata, generatorTemplate)
|
||||
return
|
||||
}
|
||||
|
|
@ -74,7 +74,7 @@ func new{{tableTemplate.TypeName}}(schemaName, tableName, alias string) {{tableT
|
|||
}
|
||||
`
|
||||
|
||||
var tablePostgreSQLBuilderTemplate = `
|
||||
var tableSQLBuilderTemplateWithEXCLUDED = `
|
||||
{{define "column-list" -}}
|
||||
{{- range $i, $c := . }}
|
||||
{{- $field := columnField $c}}
|
||||
|
|
|
|||
|
|
@ -267,8 +267,8 @@ func getGoType(column metadata.Column) interface{} {
|
|||
|
||||
// toGoType returns model type for column info.
|
||||
func toGoType(column metadata.Column) interface{} {
|
||||
switch column.DataType.Name {
|
||||
case "USER-DEFINED", "enum":
|
||||
switch strings.ToLower(column.DataType.Name) {
|
||||
case "user-defined", "enum":
|
||||
return ""
|
||||
case "boolean", "bool":
|
||||
return false
|
||||
|
|
@ -306,10 +306,10 @@ func toGoType(column metadata.Column) interface{} {
|
|||
return []byte("")
|
||||
case "text",
|
||||
"character", "bpchar",
|
||||
"character varying", "varchar",
|
||||
"character varying", "varchar", "nvarchar",
|
||||
"tsvector", "bit", "bit varying", "varbit",
|
||||
"money", "json", "jsonb",
|
||||
"xml", "point", "interval", "line", "ARRAY",
|
||||
"xml", "point", "interval", "line", "array",
|
||||
"char", "tinytext", "mediumtext", "longtext": // MySQL
|
||||
return ""
|
||||
case "real", "float4":
|
||||
|
|
|
|||
|
|
@ -169,8 +169,8 @@ func processTableSQLBuilder(fileTypes, dirPath string,
|
|||
}
|
||||
|
||||
func getTableSQLBuilderTemplate(dialect jet.Dialect) string {
|
||||
if dialect.Name() == "PostgreSQL" {
|
||||
return tablePostgreSQLBuilderTemplate
|
||||
if dialect.Name() == "PostgreSQL" || dialect.Name() == "SQLite" {
|
||||
return tableSQLBuilderTemplateWithEXCLUDED
|
||||
}
|
||||
|
||||
return tableSQLBuilderTemplate
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
"github.com/go-jet/jet/v2/generator/metadata"
|
||||
"github.com/go-jet/jet/v2/internal/utils"
|
||||
"path"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
|
|
@ -137,7 +138,7 @@ func getSqlBuilderColumnType(columnMetaData metadata.Column) string {
|
|||
return "String"
|
||||
}
|
||||
|
||||
switch columnMetaData.DataType.Name {
|
||||
switch strings.ToLower(columnMetaData.DataType.Name) {
|
||||
case "boolean":
|
||||
return "Bool"
|
||||
case "smallint", "integer", "bigint",
|
||||
|
|
@ -157,9 +158,9 @@ func getSqlBuilderColumnType(columnMetaData metadata.Column) string {
|
|||
return "Timez"
|
||||
case "interval":
|
||||
return "Interval"
|
||||
case "USER-DEFINED", "enum", "text", "character", "character varying", "bytea", "uuid",
|
||||
case "user-defined", "enum", "text", "character", "character varying", "bytea", "uuid",
|
||||
"tsvector", "bit", "bit varying", "money", "json", "jsonb", "xml", "point", "line", "ARRAY",
|
||||
"char", "varchar", "binary", "varbinary",
|
||||
"char", "varchar", "nvarchar", "binary", "varbinary",
|
||||
"tinyblob", "blob", "mediumblob", "longblob", "tinytext", "mediumtext", "longtext": // MySQL
|
||||
return "String"
|
||||
case "real", "numeric", "decimal", "double precision", "float",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue