Search in sources :

Example 1 with AutoGeneratedProperty

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();
    }
}
Also used : Statement(java.sql.Statement) DatabaseMetaData(java.sql.DatabaseMetaData) AutoGeneratedProperty(org.simpleflatmapper.map.property.AutoGeneratedProperty) IgnoreAutoGeneratedProperty(org.simpleflatmapper.map.property.IgnoreAutoGeneratedProperty) ResultSetMetaData(java.sql.ResultSetMetaData) JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) ResultSet(java.sql.ResultSet)

Aggregations

DatabaseMetaData (java.sql.DatabaseMetaData)1 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 Statement (java.sql.Statement)1 JdbcColumnKey (org.simpleflatmapper.jdbc.JdbcColumnKey)1 AutoGeneratedProperty (org.simpleflatmapper.map.property.AutoGeneratedProperty)1 IgnoreAutoGeneratedProperty (org.simpleflatmapper.map.property.IgnoreAutoGeneratedProperty)1