Add the ability to fully customize jet generated files.
This commit is contained in:
parent
caa81930dc
commit
8864667f47
40 changed files with 2274 additions and 882 deletions
|
|
@ -1,81 +1,91 @@
|
|||
package mysql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"github.com/go-jet/jet/v2/generator/internal/metadata"
|
||||
"github.com/go-jet/jet/v2/internal/utils"
|
||||
"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"
|
||||
)
|
||||
|
||||
// mySqlQuerySet is dialect query set for MySQL
|
||||
type mySqlQuerySet struct{}
|
||||
|
||||
func (m *mySqlQuerySet) ListOfTablesQuery() string {
|
||||
return `
|
||||
SELECT table_name
|
||||
func (m mySqlQuerySet) GetTablesMetaData(db *sql.DB, schemaName string, tableType metadata.TableType) []metadata.Table {
|
||||
query := `
|
||||
SELECT table_name as "table.name"
|
||||
FROM INFORMATION_SCHEMA.tables
|
||||
WHERE table_schema = ? and table_type = ?;
|
||||
`
|
||||
var tables []metadata.Table
|
||||
|
||||
err := qrm.Query(context.Background(), db, query, []interface{}{schemaName, tableType}, &tables)
|
||||
throw.OnError(err)
|
||||
|
||||
for i := range tables {
|
||||
tables[i].Columns = m.GetTableColumnsMetaData(db, schemaName, tables[i].Name)
|
||||
}
|
||||
|
||||
return tables
|
||||
}
|
||||
|
||||
func (m *mySqlQuerySet) PrimaryKeysQuery() string {
|
||||
return `
|
||||
SELECT k.column_name
|
||||
FROM information_schema.table_constraints t
|
||||
JOIN information_schema.key_column_usage k
|
||||
USING(constraint_name,table_schema,table_name)
|
||||
WHERE t.constraint_type='PRIMARY KEY'
|
||||
AND t.table_schema= ?
|
||||
AND t.table_name= ?;
|
||||
`
|
||||
}
|
||||
|
||||
func (m *mySqlQuerySet) ListOfColumnsQuery() string {
|
||||
return `
|
||||
SELECT COLUMN_NAME,
|
||||
IS_NULLABLE, IF(COLUMN_TYPE = 'tinyint(1)', 'boolean', DATA_TYPE),
|
||||
IF(DATA_TYPE = 'enum', CONCAT(TABLE_NAME, '_', COLUMN_NAME), ''),
|
||||
COLUMN_TYPE LIKE '%unsigned%'
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = ? and table_name = ?
|
||||
func (m mySqlQuerySet) GetTableColumnsMetaData(db *sql.DB, schemaName string, tableName string) []metadata.Column {
|
||||
query := `
|
||||
WITH primaryKeys AS (
|
||||
SELECT k.column_name
|
||||
FROM information_schema.table_constraints t
|
||||
JOIN information_schema.key_column_usage k USING(constraint_name,table_schema,table_name)
|
||||
WHERE table_schema = ? AND table_name = ? AND t.constraint_type='PRIMARY KEY'
|
||||
)
|
||||
SELECT COLUMN_NAME AS "column.Name",
|
||||
IS_NULLABLE = "YES" AS "column.IsNullable",
|
||||
(EXISTS(SELECT 1 FROM primaryKeys AS pk WHERE pk.column_name = columns.column_name)) AS "column.IsPrimaryKey",
|
||||
IF (COLUMN_TYPE = 'tinyint(1)',
|
||||
'boolean',
|
||||
IF (DATA_TYPE='enum',
|
||||
CONCAT(TABLE_NAME, '_', COLUMN_NAME),
|
||||
DATA_TYPE)
|
||||
) AS "dataType.Name",
|
||||
IF (DATA_TYPE = 'enum', 'enum', 'base') AS "dataType.Kind",
|
||||
COLUMN_TYPE LIKE '%unsigned%' AS "dataType.IsUnsigned"
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = ? AND table_name = ?
|
||||
ORDER BY ordinal_position;
|
||||
`
|
||||
var columns []metadata.Column
|
||||
err := qrm.Query(context.Background(), db, query, []interface{}{schemaName, tableName, schemaName, tableName}, &columns)
|
||||
throw.OnError(err)
|
||||
|
||||
return columns
|
||||
}
|
||||
|
||||
func (m *mySqlQuerySet) ListOfEnumsQuery() string {
|
||||
return `
|
||||
SELECT (CASE c.DATA_TYPE WHEN 'enum' then CONCAT(c.TABLE_NAME, '_', c.COLUMN_NAME) ELSE '' END ), SUBSTRING(c.COLUMN_TYPE,5)
|
||||
func (m *mySqlQuerySet) GetEnumsMetaData(db *sql.DB, schemaName string) []metadata.Enum {
|
||||
query := `
|
||||
SELECT (CASE c.DATA_TYPE WHEN 'enum' then CONCAT(c.TABLE_NAME, '_', c.COLUMN_NAME) ELSE '' END ) as "name",
|
||||
SUBSTRING(c.COLUMN_TYPE,5) as "values"
|
||||
FROM information_schema.columns as c
|
||||
INNER JOIN information_schema.tables as t on (t.table_schema = c.table_schema AND t.table_name = c.table_name)
|
||||
WHERE c.table_schema = ? AND DATA_TYPE = 'enum';
|
||||
`
|
||||
}
|
||||
var queryResult []struct {
|
||||
Name string
|
||||
Values string
|
||||
}
|
||||
|
||||
func (m *mySqlQuerySet) GetEnumsMetaData(db *sql.DB, schemaName string) []metadata.MetaData {
|
||||
err := qrm.Query(context.Background(), db, query, []interface{}{schemaName}, &queryResult)
|
||||
throw.OnError(err)
|
||||
|
||||
rows, err := db.Query(m.ListOfEnumsQuery(), schemaName)
|
||||
utils.PanicOnError(err)
|
||||
defer rows.Close()
|
||||
var ret []metadata.Enum
|
||||
|
||||
ret := []metadata.MetaData{}
|
||||
for _, result := range queryResult {
|
||||
enumValues := strings.Replace(result.Values[1:len(result.Values)-1], "'", "", -1)
|
||||
|
||||
for rows.Next() {
|
||||
var enumName string
|
||||
var enumValues string
|
||||
err = rows.Scan(&enumName, &enumValues)
|
||||
utils.PanicOnError(err)
|
||||
|
||||
enumValues = strings.Replace(enumValues[1:len(enumValues)-1], "'", "", -1)
|
||||
|
||||
ret = append(ret, metadata.EnumMetaData{
|
||||
EnumName: enumName,
|
||||
Values: strings.Split(enumValues, ","),
|
||||
ret = append(ret, metadata.Enum{
|
||||
Name: result.Name,
|
||||
Values: strings.Split(enumValues, ","),
|
||||
})
|
||||
}
|
||||
|
||||
err = rows.Err()
|
||||
utils.PanicOnError(err)
|
||||
|
||||
return ret
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue