mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-05 05:07:58 +08:00
commit
ea8cfd6b54
@ -0,0 +1,37 @@
|
|||||||
|
package cn.hutool.db.meta;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 索引中的列信息
|
||||||
|
*
|
||||||
|
* @author huzhongying
|
||||||
|
*/
|
||||||
|
public class ColumnIndexInfo implements Serializable, Cloneable{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 列名
|
||||||
|
*/
|
||||||
|
private String columnName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 列排序顺序,“A”: 升序,“D” : 降序,如果不支持排序顺序,可能为空
|
||||||
|
*/
|
||||||
|
private String ascOrDesc;
|
||||||
|
|
||||||
|
public String getColumnName() {
|
||||||
|
return columnName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setColumnName(String columnName) {
|
||||||
|
this.columnName = columnName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAscOrDesc() {
|
||||||
|
return ascOrDesc;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAscOrDesc(String ascOrDesc) {
|
||||||
|
this.ascOrDesc = ascOrDesc;
|
||||||
|
}
|
||||||
|
}
|
109
hutool-db/src/main/java/cn/hutool/db/meta/IndexInfo.java
Normal file
109
hutool-db/src/main/java/cn/hutool/db/meta/IndexInfo.java
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
package cn.hutool.db.meta;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库表的索引信息
|
||||||
|
*
|
||||||
|
* @author huzhongying
|
||||||
|
*/
|
||||||
|
public class IndexInfo implements Serializable, Cloneable{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 索引值是否可以不唯一
|
||||||
|
*/
|
||||||
|
private boolean nonUnique;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 索引名称
|
||||||
|
*/
|
||||||
|
private String indexName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 表名
|
||||||
|
*/
|
||||||
|
private String tableName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* table所在的schema
|
||||||
|
*/
|
||||||
|
private String schema;
|
||||||
|
/**
|
||||||
|
* table所在的catalog
|
||||||
|
*/
|
||||||
|
private String catalog;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 索引中的列信息,按索引顺序排列
|
||||||
|
*/
|
||||||
|
private List<ColumnIndexInfo> columnIndexInfoList;
|
||||||
|
|
||||||
|
public boolean isNonUnique() {
|
||||||
|
return nonUnique;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNonUnique(boolean nonUnique) {
|
||||||
|
this.nonUnique = nonUnique;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getIndexName() {
|
||||||
|
return indexName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIndexName(String indexName) {
|
||||||
|
this.indexName = indexName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTableName() {
|
||||||
|
return tableName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTableName(String tableName) {
|
||||||
|
this.tableName = tableName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSchema() {
|
||||||
|
return schema;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSchema(String schema) {
|
||||||
|
this.schema = schema;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCatalog() {
|
||||||
|
return catalog;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCatalog(String catalog) {
|
||||||
|
this.catalog = catalog;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ColumnIndexInfo> getColumnIndexInfoList() {
|
||||||
|
return columnIndexInfoList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setColumnIndexInfoList(List<ColumnIndexInfo> columnIndexInfoList) {
|
||||||
|
this.columnIndexInfoList = columnIndexInfoList;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param nonUnique 索引值是否可以不唯一
|
||||||
|
* @param indexName 索引名称
|
||||||
|
* @param tableName 表名
|
||||||
|
* @param schema table所在的schema
|
||||||
|
* @param catalog table所在的catalog
|
||||||
|
*/
|
||||||
|
public IndexInfo(boolean nonUnique, String indexName, String tableName, String schema, String catalog) {
|
||||||
|
this.nonUnique = nonUnique;
|
||||||
|
this.indexName = indexName;
|
||||||
|
this.tableName = tableName;
|
||||||
|
this.schema = schema;
|
||||||
|
this.catalog = catalog;
|
||||||
|
this.setColumnIndexInfoList(new ArrayList<>());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,6 +1,8 @@
|
|||||||
package cn.hutool.db.meta;
|
package cn.hutool.db.meta;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.ListUtil;
|
||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.convert.Convert;
|
||||||
|
import cn.hutool.core.map.MapUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.db.DbRuntimeException;
|
import cn.hutool.db.DbRuntimeException;
|
||||||
import cn.hutool.db.DbUtil;
|
import cn.hutool.db.DbUtil;
|
||||||
@ -13,7 +15,9 @@ import java.sql.ResultSet;
|
|||||||
import java.sql.ResultSetMetaData;
|
import java.sql.ResultSetMetaData;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据库元数据信息工具类
|
* 数据库元数据信息工具类
|
||||||
@ -247,6 +251,33 @@ public class MetaUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 获得索引信息
|
||||||
|
|
||||||
|
try (ResultSet rs = metaData.getIndexInfo(catalog, schema, tableName, false,false)) {
|
||||||
|
Map<String, IndexInfo> indexInfoMap = MapUtil.createMap(LinkedHashMap.class);
|
||||||
|
if (null != rs) {
|
||||||
|
while (rs.next()) {
|
||||||
|
//排除tableIndexStatistic类型索引
|
||||||
|
if (rs.getShort("TYPE") != 0) {
|
||||||
|
String indexName = rs.getString("INDEX_NAME");
|
||||||
|
String key = StrUtil.join("&", tableName, indexName);
|
||||||
|
IndexInfo indexInfo = indexInfoMap.getOrDefault(key
|
||||||
|
, new IndexInfo(rs.getBoolean("NON_UNIQUE"),indexName,tableName,schema,catalog));
|
||||||
|
ColumnIndexInfo columnIndexInfo = new ColumnIndexInfo();
|
||||||
|
columnIndexInfo.setColumnName(rs.getString("COLUMN_NAME"));
|
||||||
|
columnIndexInfo.setAscOrDesc(rs.getString("ASC_OR_DESC"));
|
||||||
|
indexInfo.getColumnIndexInfoList().add(columnIndexInfo);
|
||||||
|
if (!indexInfoMap.containsKey(key)) {
|
||||||
|
indexInfoMap.put(key,indexInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
table.setIndexInfoList(ListUtil.toList(indexInfoMap.values()));
|
||||||
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new DbRuntimeException("Get columns error!", e);
|
throw new DbRuntimeException("Get columns error!", e);
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -1,11 +1,7 @@
|
|||||||
package cn.hutool.db.meta;
|
package cn.hutool.db.meta;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Collection;
|
import java.util.*;
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.LinkedHashSet;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据库表信息
|
* 数据库表信息
|
||||||
@ -35,6 +31,11 @@ public class Table implements Serializable, Cloneable {
|
|||||||
* 主键字段名列表
|
* 主键字段名列表
|
||||||
*/
|
*/
|
||||||
private Set<String> pkNames = new LinkedHashSet<>();
|
private Set<String> pkNames = new LinkedHashSet<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 索引信息
|
||||||
|
*/
|
||||||
|
private List<IndexInfo> indexInfoList;
|
||||||
private final Map<String, Column> columns = new LinkedHashMap<>();
|
private final Map<String, Column> columns = new LinkedHashMap<>();
|
||||||
|
|
||||||
public static Table create(String tableName) {
|
public static Table create(String tableName) {
|
||||||
@ -209,4 +210,16 @@ public class Table implements Serializable, Cloneable {
|
|||||||
this.pkNames.add(pkColumnName);
|
this.pkNames.add(pkColumnName);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取索引信息
|
||||||
|
* @return 索引信息
|
||||||
|
*/
|
||||||
|
public List<IndexInfo> getIndexInfoList() {
|
||||||
|
return indexInfoList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIndexInfoList(List<IndexInfo> indexInfoList) {
|
||||||
|
this.indexInfoList = indexInfoList;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,4 +37,10 @@ public class MetaUtilTest {
|
|||||||
String[] names = MetaUtil.getColumnNames(ds, "user");
|
String[] names = MetaUtil.getColumnNames(ds, "user");
|
||||||
Assert.assertArrayEquals(StrUtil.splitToArray("id,name,age,birthday,gender", ','), names);
|
Assert.assertArrayEquals(StrUtil.splitToArray("id,name,age,birthday,gender", ','), names);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getTableIndexInfoTest() {
|
||||||
|
Table table = MetaUtil.getTableMeta(ds, "user_1");
|
||||||
|
Assert.assertEquals(table.getIndexInfoList().size(), 2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user