support pgsql model generation
This commit is contained in:
23
cmd/model.go
23
cmd/model.go
@@ -5,6 +5,8 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"atom/container"
|
"atom/container"
|
||||||
|
"atom/providers/config"
|
||||||
|
"atom/providers/database"
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
@@ -18,7 +20,8 @@ import (
|
|||||||
type GenQueryGenerator struct {
|
type GenQueryGenerator struct {
|
||||||
dig.In
|
dig.In
|
||||||
|
|
||||||
DB *gorm.DB
|
Config *config.Config
|
||||||
|
DB *gorm.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
// // Dynamic SQL
|
// // Dynamic SQL
|
||||||
@@ -35,10 +38,22 @@ var modelCmd = &cobra.Command{
|
|||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return container.Container.Invoke(func(gq GenQueryGenerator) error {
|
return container.Container.Invoke(func(gq GenQueryGenerator) error {
|
||||||
var tables []string
|
var tables []string
|
||||||
err := gq.DB.Raw("show tables").Scan(&tables).Error
|
|
||||||
if err != nil {
|
switch gq.Config.Database.Driver {
|
||||||
log.Fatal(err)
|
case database.DriverMySQL:
|
||||||
|
err := gq.DB.Raw("show tables").Scan(&tables).Error
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
case database.DriverPostgres:
|
||||||
|
err := gq.DB.Raw("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'").Scan(&tables).Error
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
case database.DriverSQLite:
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(tables) == 0 {
|
if len(tables) == 0 {
|
||||||
return errors.New("no tables in database, run migrate first")
|
return errors.New("no tables in database, run migrate first")
|
||||||
}
|
}
|
||||||
|
|||||||
15
config.toml
15
config.toml
@@ -88,7 +88,7 @@ ShowLine = true
|
|||||||
LogInConsole = true
|
LogInConsole = true
|
||||||
|
|
||||||
[Database]
|
[Database]
|
||||||
Driver = "mysql"
|
Driver = "postgres"
|
||||||
|
|
||||||
[Database.MySQL]
|
[Database.MySQL]
|
||||||
Host = "10.47.119.226"
|
Host = "10.47.119.226"
|
||||||
@@ -102,6 +102,19 @@ MaxIdleConns = 10
|
|||||||
MaxOpenConns = 200
|
MaxOpenConns = 200
|
||||||
Engine = "InnoDB"
|
Engine = "InnoDB"
|
||||||
|
|
||||||
|
[Database.PostgreSQL]
|
||||||
|
Host = "10.95.31.212"
|
||||||
|
Port = 5442
|
||||||
|
Database = "cspm"
|
||||||
|
Username = "postgres"
|
||||||
|
Password = "zHTz7H6S9WJQK7Y0"
|
||||||
|
Prefix = ""
|
||||||
|
TimeZone = "Asia/Shanghai"
|
||||||
|
SslMode = "disable"
|
||||||
|
Singular = false
|
||||||
|
MaxIdleConns = 10
|
||||||
|
MaxOpenConns = 200
|
||||||
|
|
||||||
[Database.SQLite]
|
[Database.SQLite]
|
||||||
File = "sqlite.db"
|
File = "sqlite.db"
|
||||||
|
|
||||||
|
|||||||
@@ -58,25 +58,29 @@ func (m *MySQL) DSN() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type PostgreSQL struct {
|
type PostgreSQL struct {
|
||||||
User string
|
Username string
|
||||||
Password string
|
Password string
|
||||||
Database string
|
Database string
|
||||||
Host string
|
Host string
|
||||||
Port uint
|
Port uint
|
||||||
SslMode string
|
SslMode string
|
||||||
TimeZone string
|
TimeZone string
|
||||||
|
Prefix string // 表前缀
|
||||||
|
Singular bool //是否开启全局禁用复数,true表示开启
|
||||||
|
MaxIdleConns int // 空闲中的最大连接数
|
||||||
|
MaxOpenConns int // 打开到数据库的最大连接数
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *PostgreSQL) EmptyDsn() string {
|
func (m *PostgreSQL) EmptyDsn() string {
|
||||||
dsnTpl := "host=%s user=%s password=%s port=%d dbname=postgres sslmode=disable TimeZone=Asia/Shanghai"
|
dsnTpl := "host=%s user=%s password=%s port=%d dbname=postgres sslmode=disable TimeZone=Asia/Shanghai"
|
||||||
|
|
||||||
return fmt.Sprintf(dsnTpl, m.Host, m.User, m.Password, m.Port)
|
return fmt.Sprintf(dsnTpl, m.Host, m.Username, m.Password, m.Port)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DSN connection dsn
|
// DSN connection dsn
|
||||||
func (m *PostgreSQL) DSN() string {
|
func (m *PostgreSQL) DSN() string {
|
||||||
dsnTpl := "host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s"
|
dsnTpl := "host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s"
|
||||||
return fmt.Sprintf(dsnTpl, m.Host, m.User, m.Password, m.Database, m.Port, m.SslMode, m.TimeZone)
|
return fmt.Sprintf(dsnTpl, m.Host, m.Username, m.Password, m.Database, m.Port, m.SslMode, m.TimeZone)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Redis struct {
|
type Redis struct {
|
||||||
|
|||||||
@@ -28,6 +28,8 @@ func NewDatabase(config *config.Config) (*gorm.DB, error) {
|
|||||||
return NewMySQL(config.Database.MySQL)
|
return NewMySQL(config.Database.MySQL)
|
||||||
case DriverSQLite:
|
case DriverSQLite:
|
||||||
return NewSQLite(config.Database.SQLite)
|
return NewSQLite(config.Database.SQLite)
|
||||||
|
case DriverPostgres:
|
||||||
|
return NewPostgres(config.Database.PostgreSQL)
|
||||||
}
|
}
|
||||||
return nil, errors.New("failed to connect to db")
|
return nil, errors.New("failed to connect to db")
|
||||||
}
|
}
|
||||||
|
|||||||
36
providers/database/postgres.go
Normal file
36
providers/database/postgres.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"atom/providers/config"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"gorm.io/driver/postgres"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"gorm.io/gorm/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewPostgres(conf *config.PostgreSQL) (*gorm.DB, error) {
|
||||||
|
dbConfig := postgres.Config{
|
||||||
|
DSN: conf.DSN(), // DSN data source name
|
||||||
|
}
|
||||||
|
log.Println("PostgreSQL DSN: ", dbConfig.DSN)
|
||||||
|
|
||||||
|
gormConfig := gorm.Config{
|
||||||
|
NamingStrategy: schema.NamingStrategy{
|
||||||
|
TablePrefix: conf.Prefix,
|
||||||
|
SingularTable: conf.Singular,
|
||||||
|
},
|
||||||
|
DisableForeignKeyConstraintWhenMigrating: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := gorm.Open(postgres.New(dbConfig), &gormConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlDB, _ := db.DB()
|
||||||
|
sqlDB.SetMaxIdleConns(conf.MaxIdleConns)
|
||||||
|
sqlDB.SetMaxOpenConns(conf.MaxOpenConns)
|
||||||
|
|
||||||
|
return db, err
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user