private void insert(
     final Record object, final PathName typePath, final RecordDefinition recordDefinition)
     throws SQLException {
   PreparedStatement statement = this.typeInsertStatementMap.get(typePath);
   if (statement == null) {
     final String sql = getInsertSql(recordDefinition, false);
     try {
       statement = this.connection.prepareStatement(sql);
       this.typeInsertStatementMap.put(typePath, statement);
     } catch (final SQLException e) {
       LOG.error(sql, e);
     }
   }
   int parameterIndex = 1;
   for (final FieldDefinition fieldDefinition : recordDefinition.getFields()) {
     final JdbcFieldDefinition jdbcFieldDefinition = (JdbcFieldDefinition) fieldDefinition;
     parameterIndex =
         jdbcFieldDefinition.setInsertPreparedStatementValue(statement, parameterIndex, object);
   }
   statement.addBatch();
   Integer batchCount = this.typeInsertBatchCountMap.get(typePath);
   if (batchCount == null) {
     batchCount = 1;
     this.typeInsertBatchCountMap.put(typePath, 1);
   } else {
     batchCount += 1;
     this.typeInsertBatchCountMap.put(typePath, batchCount);
   }
   if (batchCount >= this.batchSize) {
     final String sql = getInsertSql(recordDefinition, false);
     processCurrentBatch(typePath, sql, statement, this.typeInsertBatchCountMap);
   }
 }
 private void addSqlColumEqualsPlaceholder(
     final StringBuilder sqlBuffer, final JdbcFieldDefinition attribute) {
   final String fieldName = attribute.getName();
   if (this.quoteColumnNames) {
     sqlBuffer.append('"').append(fieldName).append('"');
   } else {
     sqlBuffer.append(fieldName);
   }
   sqlBuffer.append(" = ");
   attribute.addInsertStatementPlaceHolder(sqlBuffer, false);
 }
 private void update(final Record object) throws SQLException {
   final RecordDefinition objectType = object.getRecordDefinition();
   final PathName typePath = objectType.getPathName();
   final RecordDefinition recordDefinition = getRecordDefinition(typePath);
   flushIfRequired(recordDefinition);
   PreparedStatement statement = this.typeUpdateStatementMap.get(typePath);
   if (statement == null) {
     final String sql = getUpdateSql(recordDefinition);
     try {
       statement = this.connection.prepareStatement(sql);
       this.typeUpdateStatementMap.put(typePath, statement);
     } catch (final SQLException e) {
       LOG.error(sql, e);
     }
   }
   int parameterIndex = 1;
   final List<FieldDefinition> idFields = recordDefinition.getIdFields();
   for (final FieldDefinition fieldDefinition : recordDefinition.getFields()) {
     if (!idFields.contains(fieldDefinition)) {
       final JdbcFieldDefinition jdbcFieldDefinition = (JdbcFieldDefinition) fieldDefinition;
       parameterIndex =
           jdbcFieldDefinition.setInsertPreparedStatementValue(statement, parameterIndex, object);
     }
   }
   for (final FieldDefinition idField : idFields) {
     final JdbcFieldDefinition jdbcFieldDefinition = (JdbcFieldDefinition) idField;
     parameterIndex =
         jdbcFieldDefinition.setInsertPreparedStatementValue(statement, parameterIndex, object);
   }
   statement.addBatch();
   Integer batchCount = this.typeUpdateBatchCountMap.get(typePath);
   if (batchCount == null) {
     batchCount = 1;
     this.typeUpdateBatchCountMap.put(typePath, 1);
   } else {
     batchCount += 1;
     this.typeUpdateBatchCountMap.put(typePath, batchCount);
   }
   if (batchCount >= this.batchSize) {
     final String sql = getUpdateSql(recordDefinition);
     processCurrentBatch(typePath, sql, statement, this.typeUpdateBatchCountMap);
   }
   this.recordStore.addStatistic("Update", object);
 }
  private void delete(final Record object) throws SQLException {
    final RecordDefinition objectType = object.getRecordDefinition();
    final PathName typePath = objectType.getPathName();
    final RecordDefinition recordDefinition = getRecordDefinition(typePath);
    flushIfRequired(recordDefinition);
    PreparedStatement statement = this.typeDeleteStatementMap.get(typePath);
    if (statement == null) {
      final String sql = getDeleteSql(recordDefinition);
      try {
        statement = this.connection.prepareStatement(sql);
        this.typeDeleteStatementMap.put(typePath, statement);
      } catch (final SQLException e) {
        LOG.error(sql, e);
      }
    }
    int parameterIndex = 1;
    final JdbcFieldDefinition idField = (JdbcFieldDefinition) recordDefinition.getIdField();
    parameterIndex = idField.setInsertPreparedStatementValue(statement, parameterIndex, object);
    statement.addBatch();
    Integer batchCount = this.typeDeleteBatchCountMap.get(typePath);
    if (batchCount == null) {
      batchCount = 1;
      this.typeDeleteBatchCountMap.put(typePath, 1);
    } else {
      batchCount += 1;
      this.typeDeleteBatchCountMap.put(typePath, batchCount);
    }
    this.recordStore.addStatistic("Delete", object);

    // TODO this locks code tables which prevents insert
    // if (batchCount >= batchSize) {
    // final String sql = getDeleteSql(recordDefinition);
    // processCurrentBatch(typePath, sql, statement, typeDeleteBatchCountMap,
    // getDeleteStatistics());
    // }
  }
 private String getInsertSql(final RecordDefinition type, final boolean generatePrimaryKey) {
   final PathName typePath = type.getPathName();
   final String tableName = this.recordStore.getDatabaseQualifiedTableName(typePath);
   String sql;
   if (generatePrimaryKey) {
     sql = this.typeInsertSequenceSqlMap.get(typePath);
   } else {
     sql = this.typeInsertSqlMap.get(typePath);
   }
   if (sql == null) {
     final StringBuilder sqlBuffer = new StringBuilder();
     if (this.sqlPrefix != null) {
       sqlBuffer.append(this.sqlPrefix);
     }
     sqlBuffer.append("insert ");
     if (this.hints != null) {
       sqlBuffer.append(this.hints);
     }
     sqlBuffer.append(" into ");
     sqlBuffer.append(tableName);
     sqlBuffer.append(" (");
     if (generatePrimaryKey) {
       final String idFieldName = type.getIdFieldName();
       if (this.quoteColumnNames) {
         sqlBuffer.append('"').append(idFieldName).append('"');
       } else {
         sqlBuffer.append(idFieldName);
       }
       sqlBuffer.append(",");
     }
     for (int i = 0; i < type.getFieldCount(); i++) {
       if (!generatePrimaryKey || i != type.getIdFieldIndex()) {
         final String fieldName = type.getFieldName(i);
         if (this.quoteColumnNames) {
           sqlBuffer.append('"').append(fieldName).append('"');
         } else {
           sqlBuffer.append(fieldName);
         }
         if (i < type.getFieldCount() - 1) {
           sqlBuffer.append(", ");
         }
       }
     }
     sqlBuffer.append(") VALUES (");
     if (generatePrimaryKey) {
       sqlBuffer.append(getGeneratePrimaryKeySql(type));
       sqlBuffer.append(",");
     }
     for (int i = 0; i < type.getFieldCount(); i++) {
       if (!generatePrimaryKey || i != type.getIdFieldIndex()) {
         final JdbcFieldDefinition attribute = (JdbcFieldDefinition) type.getField(i);
         attribute.addInsertStatementPlaceHolder(sqlBuffer, generatePrimaryKey);
         if (i < type.getFieldCount() - 1) {
           sqlBuffer.append(", ");
         }
       }
     }
     sqlBuffer.append(")");
     if (this.sqlSuffix != null) {
       sqlBuffer.append(this.sqlSuffix);
     }
     sql = sqlBuffer.toString();
     if (generatePrimaryKey) {
       this.typeInsertSequenceSqlMap.put(typePath, sql);
     } else {
       this.typeInsertSqlMap.put(typePath, sql);
     }
   }
   return sql;
 }