Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.github.freegeese.maven.plugin.autocode.DatabaseMetadataUtils Maven / Gradle / Ivy
package com.github.freegeese.maven.plugin.autocode;
import com.github.freegeese.maven.plugin.autocode.metadata.Column;
import com.github.freegeese.maven.plugin.autocode.metadata.PrimaryKey;
import com.github.freegeese.maven.plugin.autocode.metadata.Table;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class DatabaseMetadataUtils {
/**
* 获取所有列元数据
*
* @param conn
* @return
* @throws SQLException
*/
public static List getAllColumnMetadata(Connection conn) throws SQLException {
DatabaseMetaData metaData = conn.getMetaData();
ResultSet rs = metaData.getTables(null, null, null, new String[]{"TABLE"});
List columnList = new ArrayList<>();
while (rs.next()) {
String catalog = rs.getString(1);
String tableName = rs.getString(3);
columnList.addAll(getColumnMetadatas(conn, catalog, tableName));
}
return columnList;
}
/**
* 获取指定Table的所有列元数据
*
* @param conn
* @param schema
* @param tableName
* @return
* @throws SQLException
*/
public static List getColumnMetadatas(Connection conn, String schema, String tableName) throws SQLException {
DatabaseMetaData metaData = conn.getMetaData();
List columnList = new ArrayList<>();
ResultSet rs = metaData.getColumns(schema, null, tableName, null);
while (rs.next()) {
columnList.add(
new Column(
null == rs.getObject(1) ? null : rs.getObject(1).toString(),
null == rs.getObject(2) ? null : rs.getObject(2).toString(),
null == rs.getObject(3) ? null : rs.getObject(3).toString(),
null == rs.getObject(4) ? null : rs.getObject(4).toString(),
null == rs.getObject(5) ? null : rs.getObject(5).toString(),
null == rs.getObject(6) ? null : rs.getObject(6).toString(),
null == rs.getObject(7) ? null : rs.getObject(7).toString(),
null == rs.getObject(8) ? null : rs.getObject(8).toString(),
null == rs.getObject(9) ? null : rs.getObject(9).toString(),
null == rs.getObject(10) ? null : rs.getObject(10).toString(),
null == rs.getObject(11) ? null : rs.getObject(11).toString(),
null == rs.getObject(12) ? null : rs.getObject(12).toString(),
null == rs.getObject(13) ? null : rs.getObject(13).toString(),
null == rs.getObject(14) ? null : rs.getObject(14).toString(),
null == rs.getObject(15) ? null : rs.getObject(15).toString(),
null == rs.getObject(16) ? null : rs.getObject(16).toString(),
null == rs.getObject(17) ? null : rs.getObject(17).toString(),
null == rs.getObject(18) ? null : rs.getObject(18).toString(),
null == rs.getObject(19) ? null : rs.getObject(19).toString(),
null == rs.getObject(20) ? null : rs.getObject(20).toString(),
null == rs.getObject(21) ? null : rs.getObject(21).toString(),
null == rs.getObject(22) ? null : rs.getObject(22).toString(),
null == rs.getObject(23) ? null : rs.getObject(23).toString(),
null == rs.getObject(24) ? null : rs.getObject(24).toString()
)
);
}
return columnList;
}
/**
* 获取所有Table元数据
*
* @param conn
* @return
* @throws SQLException
*/
public static List getAllTableMetadata(Connection conn) throws SQLException {
DatabaseMetaData metaData = conn.getMetaData();
ResultSet rs = metaData.getTables(null, null, null, new String[]{"TABLE"});
List tableList = new ArrayList<>();
while (rs.next()) {
Table table = new Table(
null == rs.getObject(1) ? null : rs.getObject(1).toString(),
null == rs.getObject(2) ? null : rs.getObject(2).toString(),
null == rs.getObject(3) ? null : rs.getObject(3).toString(),
null == rs.getObject(4) ? null : rs.getObject(4).toString(),
null == rs.getObject(5) ? null : rs.getObject(5).toString(),
null == rs.getObject(6) ? null : rs.getObject(6).toString(),
null == rs.getObject(7) ? null : rs.getObject(7).toString(),
null == rs.getObject(8) ? null : rs.getObject(8).toString(),
null == rs.getObject(9) ? null : rs.getObject(9).toString()
);
// columns
List columns = getColumnMetadatas(conn, table.getTableSchem(), table.getTableName());
// primary key
List primaryKeys = getPrimaryKeyMetadatas(conn, table.getTableCat(), table.getTableSchem(), table.getTableName());
if (!primaryKeys.isEmpty()) {
for (PrimaryKey primaryKey : primaryKeys) {
for (Column column : columns) {
if (primaryKey.getColumnName().equals(column.getColumnName())) {
column.setPrimaryKey(true);
break;
}
}
}
}
table.setColumns(columns);
table.setPrimaryKeys(primaryKeys);
tableList.add(table);
}
return tableList;
}
public static List getPrimaryKeyMetadatas(Connection connection, String catalog, String schema, String tableName) throws SQLException {
DatabaseMetaData metaData = connection.getMetaData();
ResultSet rs = metaData.getPrimaryKeys(catalog, schema, tableName);
List primaryKeys = new ArrayList<>();
while (rs.next()) {
PrimaryKey primaryKey = new PrimaryKey(
null == rs.getObject(1) ? null : rs.getObject(1).toString(),
null == rs.getObject(2) ? null : rs.getObject(2).toString(),
null == rs.getObject(3) ? null : rs.getObject(3).toString(),
null == rs.getObject(4) ? null : rs.getObject(4).toString(),
null == rs.getObject(5) ? null : rs.getObject(5).toString(),
null == rs.getObject(6) ? null : rs.getObject(6).toString()
);
primaryKeys.add(primaryKey);
}
return primaryKeys;
}
/**
* 下划线转驼峰
*
* @param value
* @return
*/
public static String underlineToCamelHump(String value) {
Matcher matcher = Pattern.compile("_[a-zA-Z]").matcher(value);
StringBuilder builder = new StringBuilder(value.toLowerCase());
for (int i = 0; matcher.find(); i++) {
builder.replace(matcher.start() - i, matcher.end() - i, matcher.group().substring(1).toUpperCase());
}
if (Character.isUpperCase(builder.charAt(0))) {
builder.replace(0, 1, String.valueOf(Character.toLowerCase(builder.charAt(0))));
}
return builder.toString();
}
}