mirror of
				https://gitee.com/dromara/mayfly-go
				synced 2025-11-04 00:10:25 +08:00 
			
		
		
		
	!111 refactor:获取表索引,默认过滤主键索引
Merge pull request !111 from zongyangleo/dev_0327_fix
This commit is contained in:
		@@ -294,18 +294,10 @@ func (d *dbAppImpl) DumpDb(ctx context.Context, reqParam *DumpDbReq) error {
 | 
				
			|||||||
		indexs, err := dbMeta.GetTableIndex(tableName)
 | 
							indexs, err := dbMeta.GetTableIndex(tableName)
 | 
				
			||||||
		biz.ErrIsNil(err)
 | 
							biz.ErrIsNil(err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 过滤主键索引
 | 
							if len(indexs) > 0 {
 | 
				
			||||||
		idxs := make([]dbi.Index, 0)
 | 
					 | 
				
			||||||
		for _, idx := range indexs {
 | 
					 | 
				
			||||||
			if !idx.IsPrimaryKey {
 | 
					 | 
				
			||||||
				idxs = append(idxs, idx)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if len(idxs) > 0 {
 | 
					 | 
				
			||||||
			// 最后添加索引
 | 
								// 最后添加索引
 | 
				
			||||||
			writer.WriteString(fmt.Sprintf("\n-- ----------------------------\n-- 表索引: %s \n-- ----------------------------\n", tableName))
 | 
								writer.WriteString(fmt.Sprintf("\n-- ----------------------------\n-- 表索引: %s \n-- ----------------------------\n", tableName))
 | 
				
			||||||
			sqlArr := dbMeta.GenerateIndexDDL(idxs, tabInfo)
 | 
								sqlArr := dbMeta.GenerateIndexDDL(indexs, tabInfo)
 | 
				
			||||||
			for _, sqlStr := range sqlArr {
 | 
								for _, sqlStr := range sqlArr {
 | 
				
			||||||
				writer.WriteString(sqlStr + ";\n")
 | 
									writer.WriteString(sqlStr + ";\n")
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -324,18 +324,10 @@ func (app *dbTransferAppImpl) transferIndex(_ context.Context, tableInfo dbi.Tab
 | 
				
			|||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 过滤主键索引
 | 
						if len(indexs) == 0 {
 | 
				
			||||||
	idxs := make([]dbi.Index, 0)
 | 
					 | 
				
			||||||
	for _, idx := range indexs {
 | 
					 | 
				
			||||||
		if !idx.IsPrimaryKey {
 | 
					 | 
				
			||||||
			idxs = append(idxs, idx)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if len(idxs) == 0 {
 | 
					 | 
				
			||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 通过表名、索引信息生成建索引语句,并执行到目标表
 | 
						// 通过表名、索引信息生成建索引语句,并执行到目标表
 | 
				
			||||||
	return targetDialect.CreateIndex(tableInfo, idxs)
 | 
						return targetDialect.CreateIndex(tableInfo, indexs)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -44,6 +44,7 @@ WHERE
 | 
				
			|||||||
    SELECT
 | 
					    SELECT
 | 
				
			||||||
      database ()
 | 
					      database ()
 | 
				
			||||||
  )
 | 
					  )
 | 
				
			||||||
 | 
					  AND index_name != 'PRIMARY'
 | 
				
			||||||
  AND table_name = ?
 | 
					  AND table_name = ?
 | 
				
			||||||
ORDER BY
 | 
					ORDER BY
 | 
				
			||||||
  index_name asc,
 | 
					  index_name asc,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -39,14 +39,12 @@ SELECT ai.INDEX_NAME                          AS INDEX_NAME,
 | 
				
			|||||||
          AND aic.INDEX_NAME = ai.INDEX_NAME
 | 
					          AND aic.INDEX_NAME = ai.INDEX_NAME
 | 
				
			||||||
          AND aic.TABLE_NAME = ai.TABLE_NAME
 | 
					          AND aic.TABLE_NAME = ai.TABLE_NAME
 | 
				
			||||||
          AND ROWNUM = 1)                     AS INDEX_COMMENT,
 | 
					          AND ROWNUM = 1)                     AS INDEX_COMMENT,
 | 
				
			||||||
       CASE
 | 
					       0 AS IS_PRIMARY
 | 
				
			||||||
           WHEN ai.INDEX_NAME like 'PK_%%' THEN 1
 | 
					 | 
				
			||||||
           WHEN ai.INDEX_NAME like 'SYS_%%' THEN 1
 | 
					 | 
				
			||||||
           ELSE 0
 | 
					 | 
				
			||||||
           END AS IS_PRIMARY
 | 
					 | 
				
			||||||
FROM ALL_INDEXES ai
 | 
					FROM ALL_INDEXES ai
 | 
				
			||||||
WHERE ai.OWNER = (SELECT sys_context('USERENV', 'CURRENT_SCHEMA') FROM DUAL)
 | 
					WHERE ai.OWNER = (SELECT sys_context('USERENV', 'CURRENT_SCHEMA') FROM DUAL)
 | 
				
			||||||
  AND ai.table_name = '%s'
 | 
					  AND ai.table_name = '%s'
 | 
				
			||||||
 | 
					  AND ai.INDEX_NAME not like 'PK_%%'
 | 
				
			||||||
 | 
					  AND ai.INDEX_NAME not like 'SYS_%%'
 | 
				
			||||||
---------------------------------------
 | 
					---------------------------------------
 | 
				
			||||||
--ORACLE_COLUMN_MA 表列信息
 | 
					--ORACLE_COLUMN_MA 表列信息
 | 
				
			||||||
SELECT a.TABLE_NAME                                              as TABLE_NAME,
 | 
					SELECT a.TABLE_NAME                                              as TABLE_NAME,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -35,19 +35,20 @@ where
 | 
				
			|||||||
order by c.relname
 | 
					order by c.relname
 | 
				
			||||||
---------------------------------------
 | 
					---------------------------------------
 | 
				
			||||||
--PGSQL_INDEX_INFO 表索引信息
 | 
					--PGSQL_INDEX_INFO 表索引信息
 | 
				
			||||||
SELECT indexname                                                         AS "indexName",
 | 
					SELECT a.indexname                                                         AS "indexName",
 | 
				
			||||||
       'BTREE'                                                           AS "IndexType",
 | 
					       'BTREE'                                                           AS "IndexType",
 | 
				
			||||||
       case when indexdef like 'CREATE UNIQUE INDEX%%' then 1 else 0 end as "isUnique",
 | 
					       case when a.indexdef like 'CREATE UNIQUE INDEX%%' then 1 else 0 end as "isUnique",
 | 
				
			||||||
       obj_description(b.oid, 'pg_class')                                AS "indexComment",
 | 
					       obj_description(b.oid, 'pg_class')                                AS "indexComment",
 | 
				
			||||||
       indexdef                                                          AS "indexDef",
 | 
					       indexdef                                                          AS "indexDef",
 | 
				
			||||||
       c.attname                                                         AS "columnName",
 | 
					       c.attname                                                         AS "columnName",
 | 
				
			||||||
       c.attnum                                                          AS "seqInIndex",
 | 
					       c.attnum                                                          AS "seqInIndex",
 | 
				
			||||||
       case when indexname like '%_pkey' then 1 else 0 end               as "isPrimaryKey"
 | 
					       case when a.indexname like '%_pkey' then 1 else 0 end             AS "isPrimaryKey"
 | 
				
			||||||
FROM pg_indexes a
 | 
					FROM pg_indexes a
 | 
				
			||||||
         join pg_class b on a.indexname = b.relname
 | 
					         join pg_class b on a.indexname = b.relname
 | 
				
			||||||
         join pg_attribute c on b.oid = c.attrelid
 | 
					         join pg_attribute c on b.oid = c.attrelid
 | 
				
			||||||
WHERE a.schemaname = (select current_schema())
 | 
					WHERE a.schemaname = (select current_schema())
 | 
				
			||||||
  AND a.tablename = '%s';
 | 
					  AND a.tablename = '%s'
 | 
				
			||||||
 | 
					  AND a.indexname not like '%_pkey'
 | 
				
			||||||
---------------------------------------
 | 
					---------------------------------------
 | 
				
			||||||
--PGSQL_COLUMN_MA 表列信息
 | 
					--PGSQL_COLUMN_MA 表列信息
 | 
				
			||||||
SELECT a.table_name                                                                            AS "tableName",
 | 
					SELECT a.table_name                                                                            AS "tableName",
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -144,6 +144,7 @@ func (md *MssqlMetaData) GetPrimaryKey(tablename string) (string, error) {
 | 
				
			|||||||
	return columns[0].ColumnName, nil
 | 
						return columns[0].ColumnName, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 需要收集唯一键涉及的字段,所以需要查询出带主键的索引
 | 
				
			||||||
func (md *MssqlMetaData) getTableIndexWithPK(tableName string) ([]dbi.Index, error) {
 | 
					func (md *MssqlMetaData) getTableIndexWithPK(tableName string) ([]dbi.Index, error) {
 | 
				
			||||||
	_, res, err := md.dc.Query(dbi.GetLocalSql(MSSQL_META_FILE, MSSQL_INDEX_INFO_KEY), md.dc.Info.CurrentSchema(), tableName)
 | 
						_, res, err := md.dc.Query(dbi.GetLocalSql(MSSQL_META_FILE, MSSQL_INDEX_INFO_KEY), md.dc.Info.CurrentSchema(), tableName)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user