Data model generator for postgres database.
This commit is contained in:
parent
92edc96c9a
commit
319c9f757d
9 changed files with 505 additions and 213 deletions
|
|
@ -3,8 +3,8 @@ package generator
|
|||
import (
|
||||
"database/sql"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/serenize/snaker"
|
||||
"os"
|
||||
"github.com/sub0Zero/go-sqlbuilder/generator/metadata"
|
||||
"path"
|
||||
)
|
||||
|
||||
type DbConnectInfo struct {
|
||||
|
|
@ -16,12 +16,10 @@ type DbConnectInfo struct {
|
|||
}
|
||||
|
||||
func Generate(folderPath string, connectString string, databaseName, schemaName string) error {
|
||||
if _, err := os.Stat(folderPath); os.IsNotExist(err) {
|
||||
err := os.Mkdir(folderPath, os.ModePerm)
|
||||
err := cleanUpGeneratedFiles(path.Join(folderPath, databaseName, schemaName))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
db, err := sql.Open("postgres", connectString)
|
||||
|
|
@ -36,128 +34,23 @@ func Generate(folderPath string, connectString string, databaseName, schemaName
|
|||
return err
|
||||
}
|
||||
|
||||
tables, err := getTablesInfo(db, schemaName)
|
||||
databaseInfo, err := metadata.GetDatabaseInfo(db, databaseName, schemaName)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, table := range tables {
|
||||
err = generateSqlBuilderModel(databaseName, schemaName, table, folderPath)
|
||||
err = generateSqlBuilderModel(databaseInfo, folderPath)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = generateDataModel(databaseInfo, folderPath)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type TableInfo struct {
|
||||
Name string
|
||||
Columns []ColumnInfo
|
||||
}
|
||||
|
||||
func getTablesInfo(db *sql.DB, schemaName string) ([]TableInfo, error) {
|
||||
tableNames, err := getListOfTables(db, schemaName)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tables := []TableInfo{}
|
||||
for _, tableName := range tableNames {
|
||||
columns, err := getColumnInfos(db, tableName)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tables = append(tables, TableInfo{tableName, columns})
|
||||
}
|
||||
|
||||
return tables, nil
|
||||
}
|
||||
|
||||
func getListOfTables(db *sql.DB, schemaName string) ([]string, error) {
|
||||
|
||||
rows, err := db.Query(`
|
||||
SELECT table_name FROM information_schema.tables
|
||||
where table_schema = $1 and table_type = 'BASE TABLE';`, schemaName)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
tables := []string{}
|
||||
for rows.Next() {
|
||||
var table string
|
||||
err = rows.Scan(&table)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tables = append(tables, table)
|
||||
}
|
||||
|
||||
err = rows.Err()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return tables, nil
|
||||
}
|
||||
|
||||
type ColumnInfo struct {
|
||||
Name string
|
||||
IsNullable bool
|
||||
DataType string
|
||||
}
|
||||
|
||||
func (c *ColumnInfo) CamelCaseName() string {
|
||||
return snaker.SnakeToCamel(c.Name)
|
||||
}
|
||||
|
||||
func getColumnInfos(db *sql.DB, tableName string) ([]ColumnInfo, error) {
|
||||
|
||||
query := `
|
||||
SELECT column_name, is_nullable, data_type
|
||||
FROM information_schema.columns
|
||||
where table_name = $1
|
||||
order by ordinal_position;`
|
||||
|
||||
//fmt.Println(query)
|
||||
|
||||
rows, err := db.Query(query, &tableName)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
ret := []ColumnInfo{}
|
||||
|
||||
for rows.Next() {
|
||||
columnInfo := ColumnInfo{}
|
||||
var isNullable string
|
||||
err := rows.Scan(&columnInfo.Name, &isNullable, &columnInfo.DataType)
|
||||
|
||||
columnInfo.IsNullable = isNullable == "YES"
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ret = append(ret, columnInfo)
|
||||
}
|
||||
|
||||
err = rows.Err()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue