mirror of
				https://gitee.com/dromara/mayfly-go
				synced 2025-11-04 00:10:25 +08:00 
			
		
		
		
	fix: show create table for postgres
This commit is contained in:
		@@ -319,28 +319,21 @@ func (d *Db) DumpSql(rc *req.Ctx) {
 | 
				
			|||||||
		writer.Close()
 | 
							writer.Close()
 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
	for _, dbName := range dbNames {
 | 
						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)
 | 
						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)
 | 
						dbConn := d.DbApp.GetDbConnection(dbId, dbName)
 | 
				
			||||||
	writer.WriteString("\n-- ----------------------------")
 | 
						writer.WriteString("\n-- ----------------------------")
 | 
				
			||||||
	writer.WriteString("\n-- 导出平台: mayfly-go")
 | 
						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 ", time.Now().Format("2006-01-02 15:04:05")))
 | 
				
			||||||
	writer.WriteString(fmt.Sprintf("\n-- 导出数据库: %s ", dbName))
 | 
						writer.WriteString(fmt.Sprintf("\n-- 导出数据库: %s ", dbName))
 | 
				
			||||||
	writer.WriteString("\n-- ----------------------------\n")
 | 
						writer.WriteString("\n-- ----------------------------\n\n")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if switchDb {
 | 
						writer.WriteString(dbConn.Info.Type.StmtUseDatabase(dbName))
 | 
				
			||||||
		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.StmtSetForeignKeyChecks(false))
 | 
						writer.WriteString(dbConn.Info.Type.StmtSetForeignKeyChecks(false))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	dbMeta := dbConn.GetMeta()
 | 
						dbMeta := dbConn.GetMeta()
 | 
				
			||||||
@@ -358,7 +351,7 @@ func (d *Db) dumpDb(writer *gzipWriter, dbId uint64, dbName string, tables []str
 | 
				
			|||||||
		if needStruct {
 | 
							if needStruct {
 | 
				
			||||||
			writer.WriteString(fmt.Sprintf("\n-- ----------------------------\n-- 表结构: %s \n-- ----------------------------\n", table))
 | 
								writer.WriteString(fmt.Sprintf("\n-- ----------------------------\n-- 表结构: %s \n-- ----------------------------\n", table))
 | 
				
			||||||
			writer.WriteString(fmt.Sprintf("DROP TABLE IF EXISTS %s;\n", quotedTable))
 | 
								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 {
 | 
							if !needData {
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -75,7 +75,7 @@ ORDER BY
 | 
				
			|||||||
        -- columns
 | 
					        -- columns
 | 
				
			||||||
        tableScript:=tableScript || ' CREATE TABLE '|| tablename|| ' ( '|| chr(13)||chr(10) || array_to_string(
 | 
					        tableScript:=tableScript || ' CREATE TABLE '|| tablename|| ' ( '|| chr(13)||chr(10) || array_to_string(
 | 
				
			||||||
        array(
 | 
					        array(
 | 
				
			||||||
        select ' ' || concat_ws(' ',fieldName, fieldType, fieldLen, indexType, isNullStr, fieldComment ) as column_line
 | 
					        select ' ' || concat_ws(' ',fieldName, fieldType, isNullStr ) as column_line
 | 
				
			||||||
        from (
 | 
					        from (
 | 
				
			||||||
        select a.attname as fieldName,format_type(a.atttypid,a.atttypmod) as fieldType,(case when atttypmod-4>0 then
 | 
					        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,
 | 
					        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
 | 
					        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)
 | 
					        c.relnamespace=n.oid and n.nspname =namespace and relname =tablename)
 | 
				
			||||||
        ) as string_columns
 | 
					        ) 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(
 | 
					        array(
 | 
				
			||||||
 | 
					        select '' union all
 | 
				
			||||||
        select concat(' CONSTRAINT ',conname ,c ,u,p,f) from (
 | 
					        select concat(' CONSTRAINT ',conname ,c ,u,p,f) from (
 | 
				
			||||||
        select conname,
 | 
					        select conname,
 | 
				
			||||||
        case when contype='c' then ' CHECK('|| ( select findattname(namespace,tablename,'c') ) ||')' end as c ,
 | 
					        case when contype='c' then ' CHECK('|| ( select findattname(namespace,tablename,'c') ) ||')' end as c ,
 | 
				
			||||||
@@ -117,7 +118,7 @@ ORDER BY
 | 
				
			|||||||
        /** **/
 | 
					        /** **/
 | 
				
			||||||
        --- 获取非约束索引 column
 | 
					        --- 获取非约束索引 column
 | 
				
			||||||
        -- CREATE UNIQUE INDEX pg_language_oid_index ON pg_language USING btree (oid); -- table pg_language
 | 
					        -- 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(
 | 
					        array(
 | 
				
			||||||
        select 'CREATE INDEX ' || indexrelname || ' ON ' || tablename || ' USING btree '|| '(' || attname || ');' from (
 | 
					        select 'CREATE INDEX ' || indexrelname || ' ON ' || tablename || ' USING btree '|| '(' || attname || ');' from (
 | 
				
			||||||
        SELECT
 | 
					        SELECT
 | 
				
			||||||
@@ -134,16 +135,16 @@ ORDER BY
 | 
				
			|||||||
        WHERE c.relname=tablename and i.relname not in
 | 
					        WHERE c.relname=tablename and i.relname not in
 | 
				
			||||||
        ( select constraint_name from information_schema.key_column_usage where table_name=tablename )
 | 
					        ( select constraint_name from information_schema.key_column_usage where table_name=tablename )
 | 
				
			||||||
        )as t
 | 
					        )as t
 | 
				
			||||||
        ) ,','|| chr(13)||chr(10));
 | 
					        ) , chr(13)||chr(10));
 | 
				
			||||||
        -- COMMENT COMMENT ON COLUMN sys_activity.id IS '主键';
 | 
					        -- COMMENT ON COLUMN sys_activity.id IS '主键';
 | 
				
			||||||
        tableScript:= tableScript || chr(13)||chr(10) || chr(13)||chr(10) || array_to_string(
 | 
					        tableScript:= tableScript || chr(13)||chr(10) || array_to_string(
 | 
				
			||||||
        array(
 | 
					        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
 | 
					        FROM pg_class c
 | 
				
			||||||
        JOIN pg_description d ON c.oid=d.objoid
 | 
					        JOIN pg_description d ON c.oid=d.objoid
 | 
				
			||||||
        JOIN pg_attribute a ON c.oid = a.attrelid
 | 
					        JOIN pg_attribute a ON c.oid = a.attrelid
 | 
				
			||||||
        WHERE c.relname=tablename
 | 
					        WHERE c.relname=tablename
 | 
				
			||||||
        AND a.attnum = d.objsubid),','|| chr(13)||chr(10)) ;
 | 
					        AND a.attnum = d.objsubid), chr(13)||chr(10)) ;
 | 
				
			||||||
        return tableScript;
 | 
					        return tableScript;
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
        $BODY$ LANGUAGE plpgsql;
 | 
					        $BODY$ LANGUAGE plpgsql;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -154,7 +154,7 @@ func (mm *MysqlMetadata) GetTableIndex(tableName string) []Index {
 | 
				
			|||||||
func (mm *MysqlMetadata) GetCreateTableDdl(tableName string) string {
 | 
					func (mm *MysqlMetadata) GetCreateTableDdl(tableName string) string {
 | 
				
			||||||
	_, res, err := mm.di.SelectData(fmt.Sprintf("show create table `%s` ", tableName))
 | 
						_, res, err := mm.di.SelectData(fmt.Sprintf("show create table `%s` ", tableName))
 | 
				
			||||||
	biz.ErrIsNilAppendErr(err, "获取表结构失败: %s")
 | 
						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) {
 | 
					func (mm *MysqlMetadata) GetTableRecord(tableName string, pageNum, pageSize int) ([]string, []map[string]any, error) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -94,9 +94,9 @@ func (dbType DbType) StmtSetForeignKeyChecks(check bool) string {
 | 
				
			|||||||
	switch dbType {
 | 
						switch dbType {
 | 
				
			||||||
	case DbTypeMysql:
 | 
						case DbTypeMysql:
 | 
				
			||||||
		if check {
 | 
							if check {
 | 
				
			||||||
			return "\nSET FOREIGN_KEY_CHECKS = 1;\n"
 | 
								return "SET FOREIGN_KEY_CHECKS = 1;\n"
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			return "\nSET FOREIGN_KEY_CHECKS = 0;\n"
 | 
								return "SET FOREIGN_KEY_CHECKS = 0;\n"
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	case DbTypePostgres:
 | 
						case DbTypePostgres:
 | 
				
			||||||
		// not currently supported postgres
 | 
							// not currently supported postgres
 | 
				
			||||||
@@ -104,5 +104,16 @@ func (dbType DbType) StmtSetForeignKeyChecks(check bool) string {
 | 
				
			|||||||
	default:
 | 
						default:
 | 
				
			||||||
		panic(fmt.Sprintf("invalid database type: %s", dbType))
 | 
							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))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user