forked from p81075629/datagear
[meta]完善模块功能
This commit is contained in:
parent
5f9389ecef
commit
31bf09fb3b
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* Copyright 2018 datagear.tech. All Rights Reserved.
|
||||
*/
|
||||
|
||||
package org.datagear.meta;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 数据类型。
|
||||
*
|
||||
* @author datagear@163.com
|
||||
*
|
||||
*/
|
||||
public class DataType implements Serializable
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 类型名 */
|
||||
private String name;
|
||||
|
||||
/** SQL类型,对应java.sql.Types中的值 */
|
||||
private int type;
|
||||
|
||||
/** 可搜索类型 */
|
||||
private SearchableType searchableType;
|
||||
|
||||
public DataType(String name, int type)
|
||||
{
|
||||
super();
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getType()
|
||||
{
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(int type)
|
||||
{
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public boolean hasSearchableType()
|
||||
{
|
||||
return (this.searchableType != null);
|
||||
}
|
||||
|
||||
public SearchableType getSearchableType()
|
||||
{
|
||||
return searchableType;
|
||||
}
|
||||
|
||||
public void setSearchableType(SearchableType searchableType)
|
||||
{
|
||||
this.searchableType = searchableType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return getClass().getSimpleName() + " [name=" + name + ", type=" + type + ", searchableType=" + searchableType
|
||||
+ "]";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,127 @@
|
|||
/*
|
||||
* Copyright 2018 datagear.tech. All Rights Reserved.
|
||||
*/
|
||||
|
||||
package org.datagear.meta;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 数据库信息。
|
||||
*
|
||||
* @author datagear@163.com
|
||||
*
|
||||
*/
|
||||
public class Database implements Serializable
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String url;
|
||||
|
||||
private String user;
|
||||
|
||||
private String productName;
|
||||
|
||||
private String productVersion;
|
||||
|
||||
private String catalog;
|
||||
|
||||
private String schema;
|
||||
|
||||
private String driverName;
|
||||
|
||||
private String driverVersion;
|
||||
|
||||
public Database()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public String getUrl()
|
||||
{
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url)
|
||||
{
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getUser()
|
||||
{
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(String user)
|
||||
{
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public String getProductName()
|
||||
{
|
||||
return productName;
|
||||
}
|
||||
|
||||
public void setProductName(String productName)
|
||||
{
|
||||
this.productName = productName;
|
||||
}
|
||||
|
||||
public String getProductVersion()
|
||||
{
|
||||
return productVersion;
|
||||
}
|
||||
|
||||
public void setProductVersion(String productVersion)
|
||||
{
|
||||
this.productVersion = productVersion;
|
||||
}
|
||||
|
||||
public String getCatalog()
|
||||
{
|
||||
return catalog;
|
||||
}
|
||||
|
||||
public void setCatalog(String catalog)
|
||||
{
|
||||
this.catalog = catalog;
|
||||
}
|
||||
|
||||
public String getSchema()
|
||||
{
|
||||
return schema;
|
||||
}
|
||||
|
||||
public void setSchema(String schema)
|
||||
{
|
||||
this.schema = schema;
|
||||
}
|
||||
|
||||
public String getDriverName()
|
||||
{
|
||||
return driverName;
|
||||
}
|
||||
|
||||
public void setDriverName(String driverName)
|
||||
{
|
||||
this.driverName = driverName;
|
||||
}
|
||||
|
||||
public String getDriverVersion()
|
||||
{
|
||||
return driverVersion;
|
||||
}
|
||||
|
||||
public void setDriverVersion(String driverVersion)
|
||||
{
|
||||
this.driverVersion = driverVersion;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return getClass().getSimpleName() + " [url=" + url + ", user=" + user + ", productName=" + productName
|
||||
+ ", productVersion=" + productVersion + ", catalog=" + catalog + ", schema=" + schema + ", driverName="
|
||||
+ driverName + ", driverVersion=" + driverVersion + "]";
|
||||
}
|
||||
}
|
|
@ -1,64 +0,0 @@
|
|||
/*
|
||||
* Copyright 2018 datagear.tech. All Rights Reserved.
|
||||
*/
|
||||
|
||||
package org.datagear.meta;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 外键。
|
||||
*
|
||||
* @author datagear@163.com
|
||||
*
|
||||
*/
|
||||
public class ForeignKey extends AbstractKey
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 主表名 */
|
||||
private String primaryTableName;
|
||||
|
||||
/** 主表键列名 */
|
||||
private String[] primaryColumnNames;
|
||||
|
||||
public ForeignKey()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public ForeignKey(String[] columnNames, String primaryTableName, String[] primaryColumnNames)
|
||||
{
|
||||
super(columnNames);
|
||||
this.primaryTableName = primaryTableName;
|
||||
this.primaryColumnNames = primaryColumnNames;
|
||||
}
|
||||
|
||||
public String getPrimaryTableName()
|
||||
{
|
||||
return primaryTableName;
|
||||
}
|
||||
|
||||
public void setPrimaryTableName(String primaryTableName)
|
||||
{
|
||||
this.primaryTableName = primaryTableName;
|
||||
}
|
||||
|
||||
public String[] getPrimaryColumnNames()
|
||||
{
|
||||
return primaryColumnNames;
|
||||
}
|
||||
|
||||
public void setPrimaryColumnNames(String[] primaryColumnNames)
|
||||
{
|
||||
this.primaryColumnNames = primaryColumnNames;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return getClass().getSimpleName() + " [columnNames=" + Arrays.toString(getColumnNames())
|
||||
+ ", primaryTableName=" + primaryTableName + ", primaryColumnNames="
|
||||
+ Arrays.toString(primaryColumnNames) + "]";
|
||||
}
|
||||
}
|
|
@ -15,6 +15,8 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
|
||||
import org.datagear.meta.Column;
|
||||
import org.datagear.meta.DataType;
|
||||
import org.datagear.meta.Database;
|
||||
import org.datagear.meta.ImportKey;
|
||||
import org.datagear.meta.PrimaryKey;
|
||||
import org.datagear.meta.SimpleTable;
|
||||
|
@ -50,6 +52,33 @@ public abstract class AbstractDevotedDBMetaResolver implements DevotedDBMetaReso
|
|||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Database getDatabase(Connection cn) throws DBMetaResolverException
|
||||
{
|
||||
try
|
||||
{
|
||||
DatabaseMetaData metaData = cn.getMetaData();
|
||||
|
||||
Database databaseInfo = new Database();
|
||||
|
||||
databaseInfo.setCatalog(cn.getCatalog());
|
||||
databaseInfo.setSchema(getSchema(cn, metaData));
|
||||
|
||||
databaseInfo.setUrl(metaData.getURL());
|
||||
databaseInfo.setUser(metaData.getUserName());
|
||||
databaseInfo.setProductName(metaData.getDatabaseProductName());
|
||||
databaseInfo.setProductVersion(metaData.getDatabaseProductVersion());
|
||||
databaseInfo.setDriverName(metaData.getDriverName());
|
||||
databaseInfo.setDriverVersion(metaData.getDriverVersion());
|
||||
|
||||
return databaseInfo;
|
||||
}
|
||||
catch(SQLException e)
|
||||
{
|
||||
throw new DBMetaResolverException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SimpleTable> getSimpleTables(Connection cn) throws DBMetaResolverException
|
||||
{
|
||||
|
@ -124,6 +153,43 @@ public abstract class AbstractDevotedDBMetaResolver implements DevotedDBMetaReso
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DataType> getDataTypes(Connection cn) throws DBMetaResolverException
|
||||
{
|
||||
DatabaseMetaData metaData = getDatabaseMetaData(cn);
|
||||
return getDataTypes(cn, metaData);
|
||||
}
|
||||
|
||||
protected List<DataType> getDataTypes(Connection cn, DatabaseMetaData metaData) throws DBMetaResolverException
|
||||
{
|
||||
List<DataType> dataTypes = new ArrayList<DataType>();
|
||||
|
||||
ResultSet rs = null;
|
||||
|
||||
try
|
||||
{
|
||||
rs = getDataTypeResultSet(cn, metaData);
|
||||
|
||||
while (rs.next())
|
||||
{
|
||||
DataType dataType = readDataType(rs);
|
||||
|
||||
if (dataType != null)
|
||||
dataTypes.add(dataType);
|
||||
}
|
||||
|
||||
return dataTypes;
|
||||
}
|
||||
catch(SQLException e)
|
||||
{
|
||||
throw new DBMetaResolverException(e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
JdbcUtil.closeResultSet(rs);
|
||||
}
|
||||
}
|
||||
|
||||
protected Table getTable(Connection cn, DatabaseMetaData metaData, String schema, String tableName)
|
||||
throws DBMetaResolverException
|
||||
{
|
||||
|
@ -447,6 +513,27 @@ public abstract class AbstractDevotedDBMetaResolver implements DevotedDBMetaReso
|
|||
return false;
|
||||
}
|
||||
|
||||
protected DataType readDataType(ResultSet rs)
|
||||
{
|
||||
try
|
||||
{
|
||||
String name = rs.getString("TYPE_NAME");
|
||||
if (StringUtil.isEmpty(name))
|
||||
{
|
||||
LOGGER.warn("Invalid data type row : name={}", name);
|
||||
return null;
|
||||
}
|
||||
|
||||
DataType column = new DataType(name, rs.getInt("DATA_TYPE"));
|
||||
|
||||
return column;
|
||||
}
|
||||
catch(SQLException e)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param rs
|
||||
|
@ -511,6 +598,11 @@ public abstract class AbstractDevotedDBMetaResolver implements DevotedDBMetaReso
|
|||
return databaseMetaData.getImportedKeys(cn.getCatalog(), schema, tableName);
|
||||
}
|
||||
|
||||
protected ResultSet getDataTypeResultSet(Connection cn, DatabaseMetaData databaseMetaData) throws SQLException
|
||||
{
|
||||
return databaseMetaData.getTypeInfo();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param cn
|
||||
* @param metaData
|
||||
|
|
|
@ -9,6 +9,8 @@ import java.sql.ResultSetMetaData;
|
|||
import java.util.List;
|
||||
|
||||
import org.datagear.meta.Column;
|
||||
import org.datagear.meta.DataType;
|
||||
import org.datagear.meta.Database;
|
||||
import org.datagear.meta.SimpleTable;
|
||||
import org.datagear.meta.Table;
|
||||
|
||||
|
@ -20,6 +22,15 @@ import org.datagear.meta.Table;
|
|||
*/
|
||||
public interface DBMetaResolver
|
||||
{
|
||||
/**
|
||||
* 获取{@linkplain Database}。
|
||||
*
|
||||
* @param cn
|
||||
* @return
|
||||
* @throws DBMetaResolverException
|
||||
*/
|
||||
Database getDatabase(Connection cn) throws DBMetaResolverException;
|
||||
|
||||
/**
|
||||
* 获取所有{@linkplain SimpleTable}。
|
||||
*
|
||||
|
@ -67,4 +78,13 @@ public interface DBMetaResolver
|
|||
* @throws DBMetaResolverException
|
||||
*/
|
||||
Column[] getColumns(Connection cn, ResultSetMetaData resultSetMetaData) throws DBMetaResolverException;
|
||||
|
||||
/**
|
||||
* 获取所有{@linkplain DataType}。
|
||||
*
|
||||
* @param cn
|
||||
* @return
|
||||
* @throws DBMetaResolverException
|
||||
*/
|
||||
List<DataType> getDataTypes(Connection cn) throws DBMetaResolverException;
|
||||
}
|
||||
|
|
|
@ -1,39 +0,0 @@
|
|||
/*
|
||||
* Copyright 2018 datagear.tech. All Rights Reserved.
|
||||
*/
|
||||
|
||||
package org.datagear.meta.resolver;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.List;
|
||||
|
||||
import org.datagear.meta.SimpleTable;
|
||||
import org.datagear.meta.Table;
|
||||
|
||||
/**
|
||||
* 数据库元信息解析类。
|
||||
*
|
||||
* @author datagear@163.com
|
||||
*
|
||||
*/
|
||||
public interface DatabaseMetaResolver
|
||||
{
|
||||
/**
|
||||
* 获取所有{@linkplain SimpleTable}。
|
||||
*
|
||||
* @param cn
|
||||
* @return
|
||||
* @throws DatabaseMetaResolverException
|
||||
*/
|
||||
List<SimpleTable> getSimpleTables(Connection cn) throws DatabaseMetaResolverException;
|
||||
|
||||
/**
|
||||
* 获取指定名称的{@linkplain Table}。
|
||||
*
|
||||
* @param cn
|
||||
* @param tableName
|
||||
* @return
|
||||
* @throws DatabaseMetaResolverException
|
||||
*/
|
||||
Table getTable(Connection cn, String tableName) throws DatabaseMetaResolverException;
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
/*
|
||||
* Copyright 2018 datagear.tech. All Rights Reserved.
|
||||
*/
|
||||
|
||||
package org.datagear.meta.resolver;
|
||||
|
||||
/**
|
||||
* 数据库元信息解析异常。
|
||||
*
|
||||
* @author datagear@163.com
|
||||
*
|
||||
*/
|
||||
public class DatabaseMetaResolverException extends RuntimeException
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public DatabaseMetaResolverException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public DatabaseMetaResolverException(String message)
|
||||
{
|
||||
super(message);
|
||||
}
|
||||
|
||||
public DatabaseMetaResolverException(Throwable cause)
|
||||
{
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public DatabaseMetaResolverException(String message, Throwable cause)
|
||||
{
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public DatabaseMetaResolverException(String message, Throwable cause, boolean enableSuppression,
|
||||
boolean writableStackTrace)
|
||||
{
|
||||
super(message, cause, enableSuppression, writableStackTrace);
|
||||
}
|
||||
}
|
|
@ -10,6 +10,8 @@ import java.util.List;
|
|||
|
||||
import org.datagear.connection.ConnectionOption;
|
||||
import org.datagear.meta.Column;
|
||||
import org.datagear.meta.DataType;
|
||||
import org.datagear.meta.Database;
|
||||
import org.datagear.meta.SimpleTable;
|
||||
import org.datagear.meta.Table;
|
||||
|
||||
|
@ -53,6 +55,13 @@ public class GenericDBMetaResolver implements DBMetaResolver
|
|||
this.devotedDBMetaResolvers = devotedDBMetaResolvers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Database getDatabase(Connection cn) throws DBMetaResolverException
|
||||
{
|
||||
DevotedDBMetaResolver resolver = doGetDevotedDBMetaResolverNotNull(cn);
|
||||
return resolver.getDatabase(cn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SimpleTable> getSimpleTables(Connection cn) throws DBMetaResolverException
|
||||
{
|
||||
|
@ -88,6 +97,13 @@ public class GenericDBMetaResolver implements DBMetaResolver
|
|||
return resolver.getColumns(cn, resultSetMetaData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DataType> getDataTypes(Connection cn) throws DBMetaResolverException
|
||||
{
|
||||
DevotedDBMetaResolver resolver = doGetDevotedDBMetaResolverNotNull(cn);
|
||||
return resolver.getDataTypes(cn);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取支持指定{@linkplain Connection}的{@linkplain DevotedDBMetaResolver}。
|
||||
*
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
package org.datagear.meta;
|
||||
|
||||
import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
|
||||
import static org.hamcrest.collection.ArrayMatching.arrayContaining;
|
||||
import static org.hamcrest.collection.ArrayMatching.hasItemInArray;
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.hamcrest.core.IsIterableContaining.hasItem;
|
||||
|
@ -20,6 +21,8 @@ import org.datagear.meta.resolver.WildcardDevotedDBMetaResolver;
|
|||
import org.datagear.meta.resolver.support.MySqlDevotedDBMetaResolver;
|
||||
import org.datagear.util.JdbcUtil;
|
||||
import org.datagear.util.test.DBTestSupport;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
|
@ -32,6 +35,8 @@ public class GenericDBMetaResolverTest extends DBTestSupport
|
|||
{
|
||||
private GenericDBMetaResolver genericDBMetaResolver;
|
||||
|
||||
private Connection connection;
|
||||
|
||||
public GenericDBMetaResolverTest()
|
||||
{
|
||||
super();
|
||||
|
@ -43,23 +48,22 @@ public class GenericDBMetaResolverTest extends DBTestSupport
|
|||
this.genericDBMetaResolver = new GenericDBMetaResolver(devotedDBMetaResolvers);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void init() throws Exception
|
||||
{
|
||||
this.connection = getConnection();
|
||||
}
|
||||
|
||||
@After
|
||||
public void destroy()
|
||||
{
|
||||
JdbcUtil.closeConnection(this.connection);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSimpleTablesTest() throws Exception
|
||||
{
|
||||
Connection cn = null;
|
||||
|
||||
List<SimpleTable> simpleTables = null;
|
||||
|
||||
try
|
||||
{
|
||||
cn = getConnection();
|
||||
|
||||
simpleTables = this.genericDBMetaResolver.getSimpleTables(cn);
|
||||
}
|
||||
finally
|
||||
{
|
||||
JdbcUtil.closeConnection(cn);
|
||||
}
|
||||
List<SimpleTable> simpleTables = this.genericDBMetaResolver.getSimpleTables(this.connection);
|
||||
|
||||
assertThat(simpleTables, hasItem(hasProperty("name", equalTo("T_ACCOUNT"))));
|
||||
assertThat(simpleTables, hasItem(hasProperty("name", equalTo("T_ADDRESS"))));
|
||||
|
@ -68,28 +72,19 @@ public class GenericDBMetaResolverTest extends DBTestSupport
|
|||
@Test
|
||||
public void getTableTest() throws Exception
|
||||
{
|
||||
Connection cn = null;
|
||||
|
||||
Table table0 = null;
|
||||
Table table1 = null;
|
||||
|
||||
try
|
||||
{
|
||||
cn = getConnection();
|
||||
|
||||
table0 = this.genericDBMetaResolver.getTable(cn, "T_ACCOUNT");
|
||||
table1 = this.genericDBMetaResolver.getTable(cn, "T_ADDRESS");
|
||||
}
|
||||
finally
|
||||
{
|
||||
JdbcUtil.closeConnection(cn);
|
||||
Table table = this.genericDBMetaResolver.getTable(this.connection, "T_ACCOUNT");
|
||||
assertThat(table, hasProperty("name", equalTo("T_ACCOUNT")));
|
||||
assertThat(table.getColumns(), hasItemInArray(hasProperty("name", equalTo("ID"))));
|
||||
assertThat(table.getPrimaryKey(), hasProperty("columnNames", hasItemInArray(equalTo("ID"))));
|
||||
}
|
||||
|
||||
assertThat(table0, hasProperty("name", equalTo("T_ACCOUNT")));
|
||||
assertThat(table0, hasProperty("columns", hasItemInArray(hasProperty("name", equalTo("ID")))));
|
||||
assertThat(table0, hasProperty("primaryKey", hasProperty("columnNames", hasItemInArray(equalTo("ID")))));
|
||||
|
||||
assertThat(table1, hasProperty("name", equalTo("T_ADDRESS")));
|
||||
assertThat(table1, hasProperty("columns", hasItemInArray(hasProperty("name", equalTo("ACCOUNT_ID")))));
|
||||
{
|
||||
Table table = this.genericDBMetaResolver.getTable(this.connection, "T_ADDRESS");
|
||||
assertThat(table, hasProperty("name", equalTo("T_ADDRESS")));
|
||||
assertThat(table.getColumns(), hasItemInArray(hasProperty("name", equalTo("ACCOUNT_ID"))));
|
||||
assertThat(table.getUniqueKeys(),
|
||||
hasItemInArray(hasProperty("columnNames", arrayContaining("ACCOUNT_ID"))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
8
pom.xml
8
pom.xml
|
@ -14,7 +14,7 @@
|
|||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compile.source>1.8</maven.compile.source>
|
||||
<maven.compile.target>1.8</maven.compile.target>
|
||||
<junit.version>4.11</junit.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
<hamcrest.version>2.2</hamcrest.version>
|
||||
<spring.version>4.3.26.RELEASE</spring.version>
|
||||
<spring.security.version>3.2.10.RELEASE</spring.security.version>
|
||||
|
@ -57,6 +57,12 @@
|
|||
<version>${hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-library</artifactId>
|
||||
<version>${hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
|
|
Loading…
Reference in New Issue