@Test public void testGeneratedIdSequence() throws Exception { DatabaseType databaseType = new NeedsSequenceDatabaseType(); connectionSource.setDatabaseType(databaseType); TableInfo<GeneratedId, Integer> tableInfo = new TableInfo<GeneratedId, Integer>(connectionSource, null, GeneratedId.class); StatementExecutor<GeneratedId, Integer> se = new StatementExecutor<GeneratedId, Integer>(databaseType, tableInfo, null); DatabaseConnection databaseConnection = createMock(DatabaseConnection.class); expect(databaseConnection.queryForLong(isA(String.class))).andReturn(1L); expect( databaseConnection.insert(isA(String.class), isA(Object[].class), isA(FieldType[].class), (GeneratedKeyHolder) isNull())).andReturn(1); replay(databaseConnection); GeneratedId genIdSeq = new GeneratedId(); se.create(databaseConnection, genIdSeq, null); verify(databaseConnection); }
public int insert(DatabaseType databaseType, DatabaseConnection databaseConnection, T data, ObjectCache objectCache) throws SQLException { KeyHolder keyHolder = null; if (idField != null) { boolean assignId; if (idField.isAllowGeneratedIdInsert() && !idField.isObjectsFieldValueDefault(data)) { assignId = false; } else { assignId = true; } if (idField.isSelfGeneratedId() && idField.isGeneratedId()) { if (assignId) { idField.assignField(data, idField.generateId(), false, objectCache); } } else if (idField.isGeneratedIdSequence() && databaseType.isSelectSequenceBeforeInsert()) { if (assignId) { assignSequenceId(databaseConnection, data, objectCache); } } else if (idField.isGeneratedId()) { if (assignId) { keyHolder = new KeyHolder(); } } else { } } try { if (tableInfo.isForeignAutoCreate()) { for (FieldType fieldType : tableInfo.getFieldTypes()) { if (!fieldType.isForeignAutoCreate()) { continue; } Object foreignObj = fieldType.extractRawJavaFieldValue(data); if (foreignObj != null && fieldType.getForeignIdField().isObjectsFieldValueDefault(foreignObj)) { fieldType.createWithForeignDao(foreignObj); } } } Object[] args = getFieldObjects(data); Object versionDefaultValue = null; if (versionFieldTypeIndex >= 0 && args[versionFieldTypeIndex] == null) { FieldType versionFieldType = argFieldTypes[versionFieldTypeIndex]; versionDefaultValue = versionFieldType.moveToNextValue(null); args[versionFieldTypeIndex] = versionFieldType.convertJavaFieldToSqlArgValue(versionDefaultValue); } int rowC; try { rowC = databaseConnection.insert(statement, args, argFieldTypes, keyHolder); } catch (SQLException e) { logger.debug("insert data with statement '{}' and {} args, threw exception: {}", statement, args.length, e); if (args.length > 0) { logger.trace("insert arguments: {}", (Object) args); } throw e; } logger.debug("insert data with statement '{}' and {} args, changed {} rows", statement, args.length, rowC); if (args.length > 0) { logger.trace("insert arguments: {}", (Object) args); } if (rowC > 0) { if (versionDefaultValue != null) { argFieldTypes[versionFieldTypeIndex].assignField(data, versionDefaultValue, false, null); } if (keyHolder != null) { Number key = keyHolder.getKey(); if (key == null) { throw new SQLException( "generated-id key was not set by the update call, maybe a schema mismatch between entity and database table?"); } if (key.longValue() == 0L) { throw new SQLException( "generated-id key must not be 0 value, maybe a schema mismatch between entity and database table?"); } assignIdValue(data, key, "keyholder", objectCache); } if (objectCache != null && foreignCollectionsAreAssigned(tableInfo.getForeignCollections(), data)) { Object id = idField.extractJavaFieldValue(data); objectCache.put(clazz, id, data); } } return rowC; } catch (SQLException e) { throw SqlExceptionUtil.create("Unable to run insert stmt on object " + data + ": " + statement, e); } }
MappedCreate extends BaseMappedStatement<T, ID> { public int insert(DatabaseType databaseType, DatabaseConnection databaseConnection, T data, ObjectCache objectCache) throws SQLException { KeyHolder keyHolder = null; if (idField != null) { boolean assignId; if (idField.isAllowGeneratedIdInsert() && !idField.isObjectsFieldValueDefault(data)) { assignId = false; } else { assignId = true; } if (idField.isSelfGeneratedId() && idField.isGeneratedId()) { if (assignId) { idField.assignField(data, idField.generateId(), false, objectCache); } } else if (idField.isGeneratedIdSequence() && databaseType.isSelectSequenceBeforeInsert()) { if (assignId) { assignSequenceId(databaseConnection, data, objectCache); } } else if (idField.isGeneratedId()) { if (assignId) { keyHolder = new KeyHolder(); } } else { } } try { if (tableInfo.isForeignAutoCreate()) { for (FieldType fieldType : tableInfo.getFieldTypes()) { if (!fieldType.isForeignAutoCreate()) { continue; } Object foreignObj = fieldType.extractRawJavaFieldValue(data); if (foreignObj != null && fieldType.getForeignIdField().isObjectsFieldValueDefault(foreignObj)) { fieldType.createWithForeignDao(foreignObj); } } } Object[] args = getFieldObjects(data); Object versionDefaultValue = null; if (versionFieldTypeIndex >= 0 && args[versionFieldTypeIndex] == null) { FieldType versionFieldType = argFieldTypes[versionFieldTypeIndex]; versionDefaultValue = versionFieldType.moveToNextValue(null); args[versionFieldTypeIndex] = versionFieldType.convertJavaFieldToSqlArgValue(versionDefaultValue); } int rowC; try { rowC = databaseConnection.insert(statement, args, argFieldTypes, keyHolder); } catch (SQLException e) { logger.debug("insert data with statement '{}' and {} args, threw exception: {}", statement, args.length, e); if (args.length > 0) { logger.trace("insert arguments: {}", (Object) args); } throw e; } logger.debug("insert data with statement '{}' and {} args, changed {} rows", statement, args.length, rowC); if (args.length > 0) { logger.trace("insert arguments: {}", (Object) args); } if (rowC > 0) { if (versionDefaultValue != null) { argFieldTypes[versionFieldTypeIndex].assignField(data, versionDefaultValue, false, null); } if (keyHolder != null) { Number key = keyHolder.getKey(); if (key == null) { throw new SQLException( "generated-id key was not set by the update call, maybe a schema mismatch between entity and database table?"); } if (key.longValue() == 0L) { throw new SQLException( "generated-id key must not be 0 value, maybe a schema mismatch between entity and database table?"); } assignIdValue(data, key, "keyholder", objectCache); } if (objectCache != null && foreignCollectionsAreAssigned(tableInfo.getForeignCollections(), data)) { Object id = idField.extractJavaFieldValue(data); objectCache.put(clazz, id, data); } } return rowC; } catch (SQLException e) { throw SqlExceptionUtil.create("Unable to run insert stmt on object " + data + ": " + statement, e); } } }
MappedCreate extends BaseMappedStatement<T, ID> { public int insert(DatabaseType databaseType, DatabaseConnection databaseConnection, T data, ObjectCache objectCache) throws SQLException { KeyHolder keyHolder = null; if (idField != null) { boolean assignId; if (idField.isAllowGeneratedIdInsert() && !idField.isObjectsFieldValueDefault(data)) { assignId = false; } else { assignId = true; } if (idField.isSelfGeneratedId() && idField.isGeneratedId()) { if (assignId) { idField.assignField(data, idField.generateId(), false, objectCache); } } else if (idField.isGeneratedIdSequence() && databaseType.isSelectSequenceBeforeInsert()) { if (assignId) { assignSequenceId(databaseConnection, data, objectCache); } } else if (idField.isGeneratedId()) { if (assignId) { keyHolder = new KeyHolder(); } } else { } } try { if (tableInfo.isForeignAutoCreate()) { for (FieldType fieldType : tableInfo.getFieldTypes()) { if (!fieldType.isForeignAutoCreate()) { continue; } Object foreignObj = fieldType.extractRawJavaFieldValue(data); if (foreignObj != null && fieldType.getForeignIdField().isObjectsFieldValueDefault(foreignObj)) { fieldType.createWithForeignDao(foreignObj); } } } Object[] args = getFieldObjects(data); Object versionDefaultValue = null; if (versionFieldTypeIndex >= 0 && args[versionFieldTypeIndex] == null) { FieldType versionFieldType = argFieldTypes[versionFieldTypeIndex]; versionDefaultValue = versionFieldType.moveToNextValue(null); args[versionFieldTypeIndex] = versionFieldType.convertJavaFieldToSqlArgValue(versionDefaultValue); } int rowC; try { rowC = databaseConnection.insert(statement, args, argFieldTypes, keyHolder); } catch (SQLException e) { logger.debug("insert data with statement '{}' and {} args, threw exception: {}", statement, args.length, e); if (args.length > 0) { logger.trace("insert arguments: {}", (Object) args); } throw e; } logger.debug("insert data with statement '{}' and {} args, changed {} rows", statement, args.length, rowC); if (args.length > 0) { logger.trace("insert arguments: {}", (Object) args); } if (rowC > 0) { if (versionDefaultValue != null) { argFieldTypes[versionFieldTypeIndex].assignField(data, versionDefaultValue, false, null); } if (keyHolder != null) { Number key = keyHolder.getKey(); if (key == null) { throw new SQLException( "generated-id key was not set by the update call, maybe a schema mismatch between entity and database table?"); } if (key.longValue() == 0L) { throw new SQLException( "generated-id key must not be 0 value, maybe a schema mismatch between entity and database table?"); } assignIdValue(data, key, "keyholder", objectCache); } if (objectCache != null && foreignCollectionsAreAssigned(tableInfo.getForeignCollections(), data)) { Object id = idField.extractJavaFieldValue(data); objectCache.put(clazz, id, data); } } return rowC; } catch (SQLException e) { throw SqlExceptionUtil.create("Unable to run insert stmt on object " + data + ": " + statement, e); } } private  MappedCreate(TableInfo<T, ID> tableInfo, String statement, FieldType[] argFieldTypes,
			String queryNextSequenceStmt, int versionFieldTypeIndex); }
MappedCreate extends BaseMappedStatement<T, ID> { public int insert(DatabaseType databaseType, DatabaseConnection databaseConnection, T data, ObjectCache objectCache) throws SQLException { KeyHolder keyHolder = null; if (idField != null) { boolean assignId; if (idField.isAllowGeneratedIdInsert() && !idField.isObjectsFieldValueDefault(data)) { assignId = false; } else { assignId = true; } if (idField.isSelfGeneratedId() && idField.isGeneratedId()) { if (assignId) { idField.assignField(data, idField.generateId(), false, objectCache); } } else if (idField.isGeneratedIdSequence() && databaseType.isSelectSequenceBeforeInsert()) { if (assignId) { assignSequenceId(databaseConnection, data, objectCache); } } else if (idField.isGeneratedId()) { if (assignId) { keyHolder = new KeyHolder(); } } else { } } try { if (tableInfo.isForeignAutoCreate()) { for (FieldType fieldType : tableInfo.getFieldTypes()) { if (!fieldType.isForeignAutoCreate()) { continue; } Object foreignObj = fieldType.extractRawJavaFieldValue(data); if (foreignObj != null && fieldType.getForeignIdField().isObjectsFieldValueDefault(foreignObj)) { fieldType.createWithForeignDao(foreignObj); } } } Object[] args = getFieldObjects(data); Object versionDefaultValue = null; if (versionFieldTypeIndex >= 0 && args[versionFieldTypeIndex] == null) { FieldType versionFieldType = argFieldTypes[versionFieldTypeIndex]; versionDefaultValue = versionFieldType.moveToNextValue(null); args[versionFieldTypeIndex] = versionFieldType.convertJavaFieldToSqlArgValue(versionDefaultValue); } int rowC; try { rowC = databaseConnection.insert(statement, args, argFieldTypes, keyHolder); } catch (SQLException e) { logger.debug("insert data with statement '{}' and {} args, threw exception: {}", statement, args.length, e); if (args.length > 0) { logger.trace("insert arguments: {}", (Object) args); } throw e; } logger.debug("insert data with statement '{}' and {} args, changed {} rows", statement, args.length, rowC); if (args.length > 0) { logger.trace("insert arguments: {}", (Object) args); } if (rowC > 0) { if (versionDefaultValue != null) { argFieldTypes[versionFieldTypeIndex].assignField(data, versionDefaultValue, false, null); } if (keyHolder != null) { Number key = keyHolder.getKey(); if (key == null) { throw new SQLException( "generated-id key was not set by the update call, maybe a schema mismatch between entity and database table?"); } if (key.longValue() == 0L) { throw new SQLException( "generated-id key must not be 0 value, maybe a schema mismatch between entity and database table?"); } assignIdValue(data, key, "keyholder", objectCache); } if (objectCache != null && foreignCollectionsAreAssigned(tableInfo.getForeignCollections(), data)) { Object id = idField.extractJavaFieldValue(data); objectCache.put(clazz, id, data); } } return rowC; } catch (SQLException e) { throw SqlExceptionUtil.create("Unable to run insert stmt on object " + data + ": " + statement, e); } } private  MappedCreate(TableInfo<T, ID> tableInfo, String statement, FieldType[] argFieldTypes,
			String queryNextSequenceStmt, int versionFieldTypeIndex); int insert(DatabaseType databaseType, DatabaseConnection databaseConnection, T data, ObjectCache objectCache); static MappedCreate<T, ID> build(DatabaseType databaseType, TableInfo<T, ID> tableInfo); }
MappedCreate extends BaseMappedStatement<T, ID> { public int insert(DatabaseType databaseType, DatabaseConnection databaseConnection, T data, ObjectCache objectCache) throws SQLException { KeyHolder keyHolder = null; if (idField != null) { boolean assignId; if (idField.isAllowGeneratedIdInsert() && !idField.isObjectsFieldValueDefault(data)) { assignId = false; } else { assignId = true; } if (idField.isSelfGeneratedId() && idField.isGeneratedId()) { if (assignId) { idField.assignField(data, idField.generateId(), false, objectCache); } } else if (idField.isGeneratedIdSequence() && databaseType.isSelectSequenceBeforeInsert()) { if (assignId) { assignSequenceId(databaseConnection, data, objectCache); } } else if (idField.isGeneratedId()) { if (assignId) { keyHolder = new KeyHolder(); } } else { } } try { if (tableInfo.isForeignAutoCreate()) { for (FieldType fieldType : tableInfo.getFieldTypes()) { if (!fieldType.isForeignAutoCreate()) { continue; } Object foreignObj = fieldType.extractRawJavaFieldValue(data); if (foreignObj != null && fieldType.getForeignIdField().isObjectsFieldValueDefault(foreignObj)) { fieldType.createWithForeignDao(foreignObj); } } } Object[] args = getFieldObjects(data); Object versionDefaultValue = null; if (versionFieldTypeIndex >= 0 && args[versionFieldTypeIndex] == null) { FieldType versionFieldType = argFieldTypes[versionFieldTypeIndex]; versionDefaultValue = versionFieldType.moveToNextValue(null); args[versionFieldTypeIndex] = versionFieldType.convertJavaFieldToSqlArgValue(versionDefaultValue); } int rowC; try { rowC = databaseConnection.insert(statement, args, argFieldTypes, keyHolder); } catch (SQLException e) { logger.debug("insert data with statement '{}' and {} args, threw exception: {}", statement, args.length, e); if (args.length > 0) { logger.trace("insert arguments: {}", (Object) args); } throw e; } logger.debug("insert data with statement '{}' and {} args, changed {} rows", statement, args.length, rowC); if (args.length > 0) { logger.trace("insert arguments: {}", (Object) args); } if (rowC > 0) { if (versionDefaultValue != null) { argFieldTypes[versionFieldTypeIndex].assignField(data, versionDefaultValue, false, null); } if (keyHolder != null) { Number key = keyHolder.getKey(); if (key == null) { throw new SQLException( "generated-id key was not set by the update call, maybe a schema mismatch between entity and database table?"); } if (key.longValue() == 0L) { throw new SQLException( "generated-id key must not be 0 value, maybe a schema mismatch between entity and database table?"); } assignIdValue(data, key, "keyholder", objectCache); } if (objectCache != null && foreignCollectionsAreAssigned(tableInfo.getForeignCollections(), data)) { Object id = idField.extractJavaFieldValue(data); objectCache.put(clazz, id, data); } } return rowC; } catch (SQLException e) { throw SqlExceptionUtil.create("Unable to run insert stmt on object " + data + ": " + statement, e); } } private  MappedCreate(TableInfo<T, ID> tableInfo, String statement, FieldType[] argFieldTypes,
			String queryNextSequenceStmt, int versionFieldTypeIndex); int insert(DatabaseType databaseType, DatabaseConnection databaseConnection, T data, ObjectCache objectCache); static MappedCreate<T, ID> build(DatabaseType databaseType, TableInfo<T, ID> tableInfo);  }