Add jet generator support for SQLite
This commit is contained in:
parent
3f7efb33eb
commit
51cad22809
8 changed files with 154 additions and 31 deletions
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue