use of org.simpleflatmapper.map.property.AutoGeneratedProperty in project SimpleFlatMapper by arnaudroger.
the class CrudMeta method of.
public static CrudMeta of(Connection connection, String table, ColumnDefinitionProvider<FieldMapperColumnDefinition<JdbcColumnKey>, JdbcColumnKey> columnDefinitionProvider) throws SQLException {
Statement statement = connection.createStatement();
try {
ResultSet resultSet = statement.executeQuery("SELECT * FROM " + table + " WHERE 1 = 2");
try {
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
DatabaseMetaData metaData = connection.getMetaData();
DatabaseMeta databaseMeta = new DatabaseMeta(metaData.getDatabaseProductName(), metaData.getDatabaseMajorVersion(), metaData.getDatabaseMinorVersion());
ColumnMeta[] columnMetas = new ColumnMeta[resultSetMetaData.getColumnCount()];
List<String> primaryKeys = getPrimaryKeys(connection, resultSetMetaData, columnDefinitionProvider);
for (int i = 0; i < columnMetas.length; i++) {
String columnName = resultSetMetaData.getColumnName(i + 1);
FieldMapperColumnDefinition<JdbcColumnKey> columnDefinition = columnDefinitionProvider.getColumnDefinition(JdbcColumnKey.of(resultSetMetaData, i + 1));
AutoGeneratedProperty autoGeneratedProperty = columnDefinition.lookFor(AutoGeneratedProperty.class);
if (autoGeneratedProperty == null && resultSetMetaData.isAutoIncrement(i + 1) && !columnDefinition.has(IgnoreAutoGeneratedProperty.class)) {
autoGeneratedProperty = AutoGeneratedProperty.DEFAULT;
}
columnMetas[i] = new ColumnMeta(columnName, resultSetMetaData.getColumnType(i + 1), primaryKeys.contains(columnName), autoGeneratedProperty);
}
return new CrudMeta(databaseMeta, table, columnMetas);
} finally {
resultSet.close();
}
} finally {
statement.close();
}
}
Aggregations