Java Code Examples for java.sql.ResultSetMetaData#isAutoIncrement()
The following examples show how to use
java.sql.ResultSetMetaData#isAutoIncrement() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResultSetUtil.java From Mycat2 with GNU General Public License v3.0 | 6 votes |
public static int toFlag(ResultSetMetaData metaData, int column) throws SQLException { int flags = 0; if (metaData.isNullable(column) == 1) { flags |= 1; } if (metaData.isSigned(column)) { flags |= 16; } if (metaData.isAutoIncrement(column)) { flags |= 128; } return flags; }
Example 2
Source File: PreparedStatementTest.java From dremio-oss with Apache License 2.0 | 6 votes |
boolean isEqualsTo(ResultSetMetaData metadata, int colNum) throws SQLException { return metadata.getCatalogName(colNum).equals(InfoSchemaConstants.IS_CATALOG_NAME) && metadata.getSchemaName(colNum).isEmpty() && metadata.getTableName(colNum).isEmpty() && metadata.getColumnName(colNum).equals(columnName) && metadata.getColumnLabel(colNum).equals(columnName) && metadata.getColumnType(colNum) == type && metadata.isNullable(colNum) == nullable && // There is an existing bug where query results doesn't contain the precision for VARCHAR field. //metadata.getPrecision(colNum) == precision && metadata.getScale(colNum) == scale && metadata.isSigned(colNum) == signed && metadata.getColumnDisplaySize(colNum) == displaySize && metadata.getColumnClassName(colNum).equals(className) && metadata.isSearchable(colNum) && metadata.isAutoIncrement(colNum) == false && metadata.isCaseSensitive(colNum) == false && metadata.isReadOnly(colNum) && metadata.isWritable(colNum) == false && metadata.isDefinitelyWritable(colNum) == false && metadata.isCurrency(colNum) == false; }
Example 3
Source File: ImportFromDBManagerBase.java From ermasterr with Apache License 2.0 | 6 votes |
private String getAutoIncrementColumnName(final Connection con, final String tableNameWithSchema) throws SQLException { String autoIncrementColumnName = null; Statement stmt = null; ResultSet rs = null; try { stmt = con.createStatement(); rs = stmt.executeQuery("SELECT * FROM " + tableNameWithSchema); final ResultSetMetaData md = rs.getMetaData(); for (int i = 0; i < md.getColumnCount(); i++) { if (md.isAutoIncrement(i + 1)) { autoIncrementColumnName = md.getColumnName(i + 1); break; } } } finally { this.close(rs); this.close(stmt); } return autoIncrementColumnName; }
Example 4
Source File: QueryHeaderBuilder.java From shardingsphere with Apache License 2.0 | 6 votes |
private static QueryHeader build(final ResultSetMetaData resultSetMetaData, final SchemaContext schema, final String columnName, final int columnIndex) throws SQLException { String schemaName = schema.getName(); String actualTableName = resultSetMetaData.getTableName(columnIndex); Optional<DataNodeRoutedRule> dataNodeRoutedRule = schema.getSchema().getRules().stream().filter(each -> each instanceof DataNodeRoutedRule).findFirst().map(rule -> (DataNodeRoutedRule) rule); String tableName; boolean primaryKey; if (null != actualTableName && dataNodeRoutedRule.isPresent()) { tableName = dataNodeRoutedRule.get().findLogicTableByActualTable(actualTableName).orElse(""); TableMetaData tableMetaData = schema.getSchema().getMetaData().getSchema().getConfiguredSchemaMetaData().get(tableName); primaryKey = null != tableMetaData && tableMetaData.getColumns().get(columnName.toLowerCase()).isPrimaryKey(); } else { tableName = actualTableName; primaryKey = false; } String columnLabel = resultSetMetaData.getColumnLabel(columnIndex); int columnLength = resultSetMetaData.getColumnDisplaySize(columnIndex); Integer columnType = resultSetMetaData.getColumnType(columnIndex); int decimals = resultSetMetaData.getScale(columnIndex); boolean signed = resultSetMetaData.isSigned(columnIndex); boolean notNull = resultSetMetaData.isNullable(columnIndex) == ResultSetMetaData.columnNoNulls; boolean autoIncrement = resultSetMetaData.isAutoIncrement(columnIndex); return new QueryHeader(schemaName, tableName, columnLabel, columnName, columnLength, columnType, decimals, signed, primaryKey, notNull, autoIncrement); }
Example 5
Source File: SelectStatementsTester.java From spanner-jdbc with MIT License | 5 votes |
private void testSelect(String sql, Object... parameters) throws SQLException { PreparedStatement ps = connection.prepareStatement(sql); for (int i = 1; i <= parameters.length; i++) { ps.setObject(i, parameters[i - 1]); } try (ResultSet rs = ps.executeQuery()) { ResultSetMetaData metadata = rs.getMetaData(); for (int i = 1; i <= metadata.getColumnCount(); i++) { metadata.getColumnClassName(i); metadata.getColumnDisplaySize(i); metadata.getColumnLabel(i); metadata.getColumnName(i); metadata.getColumnType(i); metadata.getColumnTypeName(i); metadata.getPrecision(i); metadata.getScale(i); metadata.getCatalogName(i); metadata.getSchemaName(i); metadata.getTableName(i); metadata.isNullable(i); metadata.isAutoIncrement(i); metadata.isCaseSensitive(i); metadata.isCurrency(i); metadata.isDefinitelyWritable(i); metadata.isReadOnly(i); metadata.isSearchable(i); metadata.isSigned(i); metadata.isWritable(i); } while (rs.next()) { // do nothing } } }
Example 6
Source File: ColumnInfo.java From ymate-platform-v2 with Apache License 2.0 | 5 votes |
public static Map<String, ColumnInfo> create(ConfigInfo configInfo, String dbType, String tableName, List<String> primaryKeys, DatabaseMetaData databaseMetaData, ResultSetMetaData metaData) throws SQLException { Map<String, ColumnInfo> _returnValue = new LinkedHashMap<String, ColumnInfo>(metaData.getColumnCount()); // for (int _idx = 1; _idx <= metaData.getColumnCount(); _idx++) { // 获取字段元数据对象 ResultSet _column = databaseMetaData.getColumns(configInfo.getDbName(), "oracle".equalsIgnoreCase(dbType) ? configInfo.getDbUserName().toUpperCase() : configInfo.getDbUserName(), tableName, metaData.getColumnName(_idx)); if (_column.next()) { // 提取字段定义及字段默认值 String _name = metaData.getColumnName(_idx).toLowerCase(); ColumnInfo _columnInfo = new ColumnInfo( configInfo.getNamedFilter(), _name, metaData.getColumnClassName(_idx), metaData.isAutoIncrement(_idx), primaryKeys.contains(_name), metaData.isSigned(_idx), metaData.getPrecision(_idx), metaData.getScale(_idx), metaData.isNullable(_idx), _column.getString("COLUMN_DEF"), _column.getString("REMARKS")); _returnValue.put(_name, _columnInfo); } _column.close(); } return _returnValue; }
Example 7
Source File: ImportFromDBManagerBase.java From erflute with Apache License 2.0 | 5 votes |
private String getAutoIncrementColumnName(Connection con, String tableNameWithSchema) throws SQLException { String autoIncrementColumnName = null; try (Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableNameWithSchema)) { final ResultSetMetaData md = rs.getMetaData(); for (int i = 0; i < md.getColumnCount(); i++) { if (md.isAutoIncrement(i + 1)) { autoIncrementColumnName = md.getColumnName(i + 1); break; } } } return autoIncrementColumnName; }
Example 8
Source File: CrudMeta.java From SimpleFlatMapper with MIT License | 5 votes |
private static CrudMeta getCrudMeta(Connection connection, String table, ColumnDefinitionProvider<JdbcColumnKey> columnDefinitionProvider, ResultSet resultSet, DatabaseMeta databaseMeta) throws SQLException { ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); ColumnMeta[] columnMetas = new ColumnMeta[resultSetMetaData.getColumnCount()]; List<String> primaryKeys = getPrimaryKeys(connection, resultSetMetaData, columnDefinitionProvider); int length = columnMetas.length; for(int i = 0; i < length; i++) { String columnName = resultSetMetaData.getColumnName(i + 1); ColumnDefinition<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); }
Example 9
Source File: StandardGenDialect.java From doma-gen with Apache License 2.0 | 5 votes |
@Override public boolean isAutoIncrement( Connection connection, String catalogName, String schemaName, String tableName, String columnName) throws SQLException { if (connection == null) { throw new GenNullPointerException("connection"); } if (tableName == null) { throw new GenNullPointerException("tableName"); } if (columnName == null) { throw new GenNullPointerException("columnName"); } String fullTableName = TableUtil.getQualifiedTableName(catalogName, schemaName, tableName); String sql = "select " + columnName + " from " + fullTableName + " where 1 = 0"; PreparedStatement preparedStatement = connection.prepareStatement(sql); try { ResultSet resultSet = preparedStatement.executeQuery(); try { ResultSetMetaData rsMetaData = resultSet.getMetaData(); return rsMetaData.isAutoIncrement(1); } finally { JdbcUtil.close(resultSet); } } finally { JdbcUtil.close(preparedStatement); } }
Example 10
Source File: ResultSetLiveTest.java From tutorials with MIT License | 5 votes |
@Test public void givenDbConnectionF_whenRSMetaInfo_thenCorrect() throws SQLException { int columnCount = 0; try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) { ResultSetMetaData metaData = rs.getMetaData(); columnCount = metaData.getColumnCount(); for (int i = 1; i <= columnCount; i++) { String catalogName = metaData.getCatalogName(i); String className = metaData.getColumnClassName(i); String label = metaData.getColumnLabel(i); String name = metaData.getColumnName(i); String typeName = metaData.getColumnTypeName(i); Integer type = metaData.getColumnType(i); String tableName = metaData.getTableName(i); String schemaName = metaData.getSchemaName(i); boolean isAutoIncrement = metaData.isAutoIncrement(i); boolean isCaseSensitive = metaData.isCaseSensitive(i); boolean isCurrency = metaData.isCurrency(i); boolean isDefiniteWritable = metaData.isDefinitelyWritable(i); boolean isReadOnly = metaData.isReadOnly(i); boolean isSearchable = metaData.isSearchable(i); boolean isReadable = metaData.isReadOnly(i); boolean isSigned = metaData.isSigned(i); boolean isWritable = metaData.isWritable(i); int nullable = metaData.isNullable(i); } } assertEquals("column count", 4, columnCount); }
Example 11
Source File: ImportFromDBManagerBase.java From ermaster-b with Apache License 2.0 | 5 votes |
private String getAutoIncrementColumnName(Connection con, String tableNameWithSchema) throws SQLException { String autoIncrementColumnName = null; Statement stmt = null; ResultSet rs = null; try { stmt = con.createStatement(); rs = stmt.executeQuery("SELECT * FROM " + tableNameWithSchema); ResultSetMetaData md = rs.getMetaData(); for (int i = 0; i < md.getColumnCount(); i++) { if (md.isAutoIncrement(i + 1)) { autoIncrementColumnName = md.getColumnName(i + 1); break; } } } finally { this.close(rs); this.close(stmt); } return autoIncrementColumnName; }
Example 12
Source File: JdbcModelReader.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** * Helper method that determines the auto increment status for the given columns via the * {@link ResultSetMetaData#isAutoIncrement(int)} method. * * @param table The table * @param columnsToCheck The columns to check (e.g. the primary key columns) */ protected void determineAutoIncrementFromResultSetMetaData(Table table, Column[] columnsToCheck) throws SQLException { if ((columnsToCheck == null) || (columnsToCheck.length == 0)) { return; } StringBuilder query = new StringBuilder(); query.append("SELECT "); for (int idx = 0; idx < columnsToCheck.length; idx++) { if (idx > 0) { query.append(","); } if (getPlatform().isDelimitedIdentifierModeOn()) { query.append(getPlatformInfo().getDelimiterToken()); } query.append(columnsToCheck[idx].getName()); if (getPlatform().isDelimitedIdentifierModeOn()) { query.append(getPlatformInfo().getDelimiterToken()); } } query.append(" FROM "); if (getPlatform().isDelimitedIdentifierModeOn()) { // GemStone changes BEGIN table.getDelimitedName(query, getPlatformInfo().getDelimiterToken()); } else { query.append(table.getQualifiedName()); /* (original code) query.append(getPlatformInfo().getDelimiterToken()); } query.append(table.getName()); if (getPlatform().isDelimitedIdentifierModeOn()) { query.append(getPlatformInfo().getDelimiterToken()); */ // GemStone changes END } query.append(" WHERE 1 = 0"); Statement stmt = null; try { stmt = getConnection().createStatement(); ResultSet rs = stmt.executeQuery(query.toString()); ResultSetMetaData rsMetaData = rs.getMetaData(); for (int idx = 0; idx < columnsToCheck.length; idx++) { if (rsMetaData.isAutoIncrement(idx + 1)) { columnsToCheck[idx].setAutoIncrement(true); } } } finally { closeStatement(stmt); } }
Example 13
Source File: JdbcModelReader.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** * Helper method that determines the auto increment status for the given columns via the * {@link ResultSetMetaData#isAutoIncrement(int)} method. * * @param table The table * @param columnsToCheck The columns to check (e.g. the primary key columns) */ protected void determineAutoIncrementFromResultSetMetaData(Table table, Column[] columnsToCheck) throws SQLException { if ((columnsToCheck == null) || (columnsToCheck.length == 0)) { return; } StringBuilder query = new StringBuilder(); query.append("SELECT "); for (int idx = 0; idx < columnsToCheck.length; idx++) { if (idx > 0) { query.append(","); } if (getPlatform().isDelimitedIdentifierModeOn()) { query.append(getPlatformInfo().getDelimiterToken()); } query.append(columnsToCheck[idx].getName()); if (getPlatform().isDelimitedIdentifierModeOn()) { query.append(getPlatformInfo().getDelimiterToken()); } } query.append(" FROM "); if (getPlatform().isDelimitedIdentifierModeOn()) { // GemStone changes BEGIN table.getDelimitedName(query, getPlatformInfo().getDelimiterToken()); } else { query.append(table.getQualifiedName()); /* (original code) query.append(getPlatformInfo().getDelimiterToken()); } query.append(table.getName()); if (getPlatform().isDelimitedIdentifierModeOn()) { query.append(getPlatformInfo().getDelimiterToken()); */ // GemStone changes END } query.append(" WHERE 1 = 0"); Statement stmt = null; try { stmt = getConnection().createStatement(); ResultSet rs = stmt.executeQuery(query.toString()); ResultSetMetaData rsMetaData = rs.getMetaData(); for (int idx = 0; idx < columnsToCheck.length; idx++) { if (rsMetaData.isAutoIncrement(idx + 1)) { columnsToCheck[idx].setAutoIncrement(true); } } } finally { closeStatement(stmt); } }
Example 14
Source File: Foorm.java From sakai with Educational Community License v2.0 | 4 votes |
/** * * @param table * @param formDefinition * @param vendor * @param md * @return */ public String[] formAdjustTable(String table, String[] formDefinition, String vendor, ResultSetMetaData md) { ArrayList<String> rv = new ArrayList<String>(); for (String formField : formDefinition) { Properties info = parseFormString(formField); String field = info.getProperty("field", null); String type = info.getProperty("type", null); if ( "header".equals(type) ) continue; String maxs = adjustMax(info.getProperty("maxlength", null)); int maxlength = 0; if (maxs != null) maxlength = (new Integer(maxs)).intValue(); if (maxlength < 1) maxlength = 80; String sqlType = null; boolean autoIncrement = false; int sqlLength = -1; boolean isNullable = false; try { for( int i = 1; i <= md.getColumnCount(); i++ ) { if ( field.equalsIgnoreCase(md.getColumnLabel(i)) ) { sqlLength = md.getColumnDisplaySize(i); autoIncrement = md.isAutoIncrement(i); sqlType = getSuperType(md.getColumnClassName(i)); isNullable = (md.isNullable(i) == ResultSetMetaData.columnNullable); break; } } } catch(Exception e) { // ignore } log.debug("{} ({}) type={}", field, maxlength, type); log.debug("{} ({}) auto={} type={} null={}", field, sqlLength, autoIncrement, sqlType, isNullable); // If the field is not there... if ( sqlType == null ) { if ( "oracle".equals(vendor) ) { rv.add("ALTER TABLE "+table+" ADD ( " + formSql(formField, vendor) + " )"); } else if ( "mysql".equals(vendor) ) { rv.add("ALTER TABLE "+table+" ADD " + formSql(formField, vendor)); } else { rv.add("ALTER TABLE "+table+" ADD COLUMN " + formSql(formField, vendor)); } continue; } String ff = formSql(formField, vendor); // BLTI-220, BLTI-238 - Required will be enforced in software - not the DB boolean shouldAlter = false; if ("key".equals(type)) { if ( ! NUMBER_TYPE.equals(sqlType) ) log.warn("{} must be Integer and Auto Increment", field); } else if ("autodate".equals(type)) { } else if ("url".equals(type) || "text".equals(type) || "textarea".equals(type)) { if ( "oracle.sql.CLOB".equals(sqlType) || "oracle.jdbc.OracleClob".equals(sqlType) ) continue; // CLOBS large enough :) if ( ! STRING_TYPE.equals(sqlType)) { log.warn("{} must be String field", field); continue; } if ( sqlLength < maxlength ) shouldAlter = true; if ( ! isNullable ) shouldAlter = true; // BLTI-220, BLTI-238 // shouldAlter = true; // Temporary SAK-31695 to force ALTER statements to be emitted } else if ("radio".equals(type) || "checkbox".equals(type) || "integer".equals(type) ) { if ( NUMBER_TYPE.equals(sqlType)) continue; log.warn("{} must be Integer field", field); } if ( shouldAlter ) { if ( "oracle".equals(vendor) ) { rv.add("ALTER TABLE "+table+" MODIFY ( " + ff + " )"); } else if ( "mysql".equals(vendor) ) { rv.add("ALTER TABLE "+table+" MODIFY " + ff); } else { rv.add("ALTER TABLE "+table+" ALTER COLUMN " + ff); } } } return rv.toArray(new String[rv.size()]); }
Example 15
Source File: Foorm.java From sakai with Educational Community License v2.0 | 4 votes |
/** * * @param table * @param formDefinition * @param vendor * @param md * @return */ public String[] formAdjustTable(String table, String[] formDefinition, String vendor, ResultSetMetaData md) { ArrayList<String> rv = new ArrayList<String>(); for (String formField : formDefinition) { Properties info = parseFormString(formField); String field = info.getProperty("field", null); String type = info.getProperty("type", null); if ( "header".equals(type) ) continue; String maxs = adjustMax(info.getProperty("maxlength", null)); int maxlength = 0; if (maxs != null) maxlength = (new Integer(maxs)).intValue(); if (maxlength < 1) maxlength = 80; String sqlType = null; boolean autoIncrement = false; int sqlLength = -1; boolean isNullable = false; try { for( int i = 1; i <= md.getColumnCount(); i++ ) { if ( field.equalsIgnoreCase(md.getColumnLabel(i)) ) { sqlLength = md.getColumnDisplaySize(i); autoIncrement = md.isAutoIncrement(i); sqlType = getSuperType(md.getColumnClassName(i)); isNullable = (md.isNullable(i) == ResultSetMetaData.columnNullable); break; } } } catch(Exception e) { // ignore } log.debug("{} ({}) type={}", field, maxlength, type); log.debug("{} ({}) auto={} type={} null={}", field, sqlLength, autoIncrement, sqlType, isNullable); // If the field is not there... if ( sqlType == null ) { if ( "oracle".equals(vendor) ) { rv.add("ALTER TABLE "+table+" ADD ( " + formSql(formField, vendor) + " )"); } else if ( "mysql".equals(vendor) ) { rv.add("ALTER TABLE "+table+" ADD " + formSql(formField, vendor)); } else { rv.add("ALTER TABLE "+table+" ADD COLUMN " + formSql(formField, vendor)); } continue; } String ff = formSql(formField, vendor); // BLTI-220, BLTI-238 - Required will be enforced in software - not the DB boolean shouldAlter = false; if ("key".equals(type)) { if ( ! NUMBER_TYPE.equals(sqlType) ) log.warn("{} must be Integer and Auto Increment", field); } else if ("autodate".equals(type)) { } else if ("url".equals(type) || "text".equals(type) || "textarea".equals(type)) { if ( "oracle.sql.CLOB".equals(sqlType) || "oracle.jdbc.OracleClob".equals(sqlType) ) continue; // CLOBS large enough :) if ( ! STRING_TYPE.equals(sqlType)) { log.warn("{} must be String field", field); continue; } if ( sqlLength < maxlength ) shouldAlter = true; if ( ! isNullable ) shouldAlter = true; // BLTI-220, BLTI-238 // shouldAlter = true; // Temporary SAK-31695 to force ALTER statements to be emitted } else if ("radio".equals(type) || "checkbox".equals(type) || "integer".equals(type) ) { if ( NUMBER_TYPE.equals(sqlType)) continue; log.warn("{} must be Integer field", field); } if ( shouldAlter ) { if ( "oracle".equals(vendor) ) { rv.add("ALTER TABLE "+table+" MODIFY ( " + ff + " )"); } else if ( "mysql".equals(vendor) ) { rv.add("ALTER TABLE "+table+" MODIFY " + ff); } else { rv.add("ALTER TABLE "+table+" ALTER COLUMN " + ff); } } } return rv.toArray(new String[rv.size()]); }