private String getDeleteSql(final RecordDefinition type) {
    final PathName typePath = type.getPathName();
    final String tableName = this.recordStore.getDatabaseQualifiedTableName(typePath);
    String sql = this.typeDeleteSqlMap.get(typePath);
    if (sql == null) {
      final StringBuilder sqlBuffer = new StringBuilder();
      if (this.sqlPrefix != null) {
        sqlBuffer.append(this.sqlPrefix);
      }
      sqlBuffer.append("delete ");
      if (this.hints != null) {
        sqlBuffer.append(this.hints);
      }
      sqlBuffer.append(" from ");
      sqlBuffer.append(tableName);
      sqlBuffer.append(" where ");
      final JdbcFieldDefinition idField = (JdbcFieldDefinition) type.getIdField();
      if (idField == null) {
        throw new RuntimeException("No primary key found for " + type);
      }
      addSqlColumEqualsPlaceholder(sqlBuffer, idField);

      sqlBuffer.append(" ");
      if (this.sqlSuffix != null) {
        sqlBuffer.append(this.sqlSuffix);
      }
      sql = sqlBuffer.toString();

      this.typeDeleteSqlMap.put(typePath, sql);
    }
    return sql;
  }
 private void insertSequence(
     final Record object, final PathName typePath, final RecordDefinition recordDefinition)
     throws SQLException {
   PreparedStatement statement = this.typeInsertSequenceStatementMap.get(typePath);
   if (statement == null) {
     final String sql = getInsertSql(recordDefinition, true);
     try {
       statement = this.connection.prepareStatement(sql);
       this.typeInsertSequenceStatementMap.put(typePath, statement);
     } catch (final SQLException e) {
       LOG.error(sql, e);
     }
   }
   int parameterIndex = 1;
   final FieldDefinition idField = recordDefinition.getIdField();
   for (final FieldDefinition field : recordDefinition.getFields()) {
     if (field != idField) {
       final JdbcFieldDefinition jdbcAttribute = (JdbcFieldDefinition) field;
       parameterIndex =
           jdbcAttribute.setInsertPreparedStatementValue(statement, parameterIndex, object);
     }
   }
   statement.addBatch();
   Integer batchCount = this.typeInsertSequenceBatchCountMap.get(typePath);
   if (batchCount == null) {
     batchCount = 1;
     this.typeInsertSequenceBatchCountMap.put(typePath, 1);
   } else {
     batchCount += 1;
     this.typeInsertSequenceBatchCountMap.put(typePath, batchCount);
   }
   if (batchCount >= this.batchSize) {
     final String sql = getInsertSql(recordDefinition, true);
     processCurrentBatch(typePath, sql, statement, this.typeInsertSequenceBatchCountMap);
   }
 }
  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());
    // }
  }