fix: show create table for postgres

This commit is contained in:
wanli
2023-10-20 17:37:09 +08:00
parent 2b91bbe185
commit 9e5f146e05
4 changed files with 30 additions and 25 deletions

View File

@@ -319,28 +319,21 @@ func (d *Db) DumpSql(rc *req.Ctx) {
writer.Close()
}()
for _, dbName := range dbNames {
d.dumpDb(writer, dbId, dbName, tables, needStruct, needData, len(dbNames) > 1)
d.dumpDb(writer, dbId, dbName, tables, needStruct, needData)
}
rc.ReqParam = collx.Kvs("db", db, "databases", dbNamesStr, "tables", tablesStr, "dumpType", dumpType)
}
func (d *Db) dumpDb(writer *gzipWriter, dbId uint64, dbName string, tables []string, needStruct bool, needData bool, switchDb bool) {
func (d *Db) dumpDb(writer *gzipWriter, dbId uint64, dbName string, tables []string, needStruct bool, needData bool) {
dbConn := d.DbApp.GetDbConnection(dbId, dbName)
writer.WriteString("\n-- ----------------------------")
writer.WriteString("\n-- 导出平台: mayfly-go")
writer.WriteString(fmt.Sprintf("\n-- 导出时间: %s ", time.Now().Format("2006-01-02 15:04:05")))
writer.WriteString(fmt.Sprintf("\n-- 导出数据库: %s ", dbName))
writer.WriteString("\n-- ----------------------------\n")
writer.WriteString("\n-- ----------------------------\n\n")
if switchDb {
switch dbConn.Info.Type {
case entity.DbTypeMysql:
writer.WriteString(fmt.Sprintf("USE %s;\n", entity.DbTypeMysql.QuoteIdentifier(dbName)))
default:
biz.IsTrue(false, "同时导出多个数据库,数据库类型必须为 %s", entity.DbTypeMysql)
}
}
writer.WriteString(dbConn.Info.Type.StmtUseDatabase(dbName))
writer.WriteString(dbConn.Info.Type.StmtSetForeignKeyChecks(false))
dbMeta := dbConn.GetMeta()
@@ -358,7 +351,7 @@ func (d *Db) dumpDb(writer *gzipWriter, dbId uint64, dbName string, tables []str
if needStruct {
writer.WriteString(fmt.Sprintf("\n-- ----------------------------\n-- 表结构: %s \n-- ----------------------------\n", table))
writer.WriteString(fmt.Sprintf("DROP TABLE IF EXISTS %s;\n", quotedTable))
writer.WriteString(dbMeta.GetCreateTableDdl(table) + ";\n")
writer.WriteString(dbMeta.GetCreateTableDdl(table) + "\n")
}
if !needData {
continue

View File

@@ -75,7 +75,7 @@ ORDER BY
-- columns
tableScript:=tableScript || ' CREATE TABLE '|| tablename|| ' ( '|| chr(13)||chr(10) || array_to_string(
array(
select ' ' || concat_ws(' ',fieldName, fieldType, fieldLen, indexType, isNullStr, fieldComment ) as column_line
select ' ' || concat_ws(' ',fieldName, fieldType, isNullStr ) as column_line
from (
select a.attname as fieldName,format_type(a.atttypid,a.atttypmod) as fieldType,(case when atttypmod-4>0 then
atttypmod-4 else 0 end) as fieldLen,
@@ -91,10 +91,11 @@ ORDER BY
from pg_attribute a where attstattarget=-1 and attrelid = (select c.oid from pg_class c,pg_namespace n where
c.relnamespace=n.oid and n.nspname =namespace and relname =tablename)
) as string_columns
),','||chr(13)||chr(10)) || ',';
),','||chr(13)||chr(10));
-- 约束
tableScript:= tableScript || chr(13)||chr(10) || array_to_string(
tableScript:= tableScript || array_to_string(
array(
select '' union all
select concat(' CONSTRAINT ',conname ,c ,u,p,f) from (
select conname,
case when contype='c' then ' CHECK('|| ( select findattname(namespace,tablename,'c') ) ||')' end as c ,
@@ -117,7 +118,7 @@ ORDER BY
/** **/
--- 获取非约束索引 column
-- CREATE UNIQUE INDEX pg_language_oid_index ON pg_language USING btree (oid); -- table pg_language
tableScript:= tableScript || chr(13)||chr(10) || chr(13)||chr(10) || array_to_string(
tableScript:= tableScript || chr(13)||chr(10) || array_to_string(
array(
select 'CREATE INDEX ' || indexrelname || ' ON ' || tablename || ' USING btree '|| '(' || attname || ');' from (
SELECT
@@ -134,16 +135,16 @@ ORDER BY
WHERE c.relname=tablename and i.relname not in
( select constraint_name from information_schema.key_column_usage where table_name=tablename )
)as t
) ,','|| chr(13)||chr(10));
-- COMMENT COMMENT ON COLUMN sys_activity.id IS '主键';
tableScript:= tableScript || chr(13)||chr(10) || chr(13)||chr(10) || array_to_string(
) , chr(13)||chr(10));
-- COMMENT ON COLUMN sys_activity.id IS '主键';
tableScript:= tableScript || chr(13)||chr(10) || array_to_string(
array(
SELECT 'COMMENT ON COLUMN' || tablename || '.' || a.attname ||' IS '|| ''''|| d.description ||''''
SELECT 'COMMENT ON COLUMN ' || tablename || '.' || a.attname ||' IS '|| ''''|| d.description ||''';'
FROM pg_class c
JOIN pg_description d ON c.oid=d.objoid
JOIN pg_attribute a ON c.oid = a.attrelid
WHERE c.relname=tablename
AND a.attnum = d.objsubid),','|| chr(13)||chr(10)) ;
AND a.attnum = d.objsubid), chr(13)||chr(10)) ;
return tableScript;
end
$BODY$ LANGUAGE plpgsql;

View File

@@ -154,7 +154,7 @@ func (mm *MysqlMetadata) GetTableIndex(tableName string) []Index {
func (mm *MysqlMetadata) GetCreateTableDdl(tableName string) string {
_, res, err := mm.di.SelectData(fmt.Sprintf("show create table `%s` ", tableName))
biz.ErrIsNilAppendErr(err, "获取表结构失败: %s")
return res[0]["Create Table"].(string)
return res[0]["Create Table"].(string) + ";"
}
func (mm *MysqlMetadata) GetTableRecord(tableName string, pageNum, pageSize int) ([]string, []map[string]any, error) {

View File

@@ -94,9 +94,9 @@ func (dbType DbType) StmtSetForeignKeyChecks(check bool) string {
switch dbType {
case DbTypeMysql:
if check {
return "\nSET FOREIGN_KEY_CHECKS = 1;\n"
return "SET FOREIGN_KEY_CHECKS = 1;\n"
} else {
return "\nSET FOREIGN_KEY_CHECKS = 0;\n"
return "SET FOREIGN_KEY_CHECKS = 0;\n"
}
case DbTypePostgres:
// not currently supported postgres
@@ -104,5 +104,16 @@ func (dbType DbType) StmtSetForeignKeyChecks(check bool) string {
default:
panic(fmt.Sprintf("invalid database type: %s", dbType))
}
}
func (dbType DbType) StmtUseDatabase(dbName string) string {
switch dbType {
case DbTypeMysql:
return fmt.Sprintf("USE %s;\n", dbType.QuoteIdentifier(dbName))
case DbTypePostgres:
// not currently supported postgres
return ""
default:
panic(fmt.Sprintf("invalid database type: %s", dbType))
}
}