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 String getUpdateSql(final RecordDefinition type) { final PathName typePath = type.getPathName(); final String tableName = this.recordStore.getDatabaseQualifiedTableName(typePath); String sql = this.typeUpdateSqlMap.get(typePath); if (sql == null) { final StringBuilder sqlBuffer = new StringBuilder(); if (this.sqlPrefix != null) { sqlBuffer.append(this.sqlPrefix); } sqlBuffer.append("update "); if (this.hints != null) { sqlBuffer.append(this.hints); } sqlBuffer.append(tableName); sqlBuffer.append(" set "); final List<FieldDefinition> idFields = type.getIdFields(); boolean first = true; for (final FieldDefinition attribute : type.getFields()) { if (!idFields.contains(attribute)) { final JdbcFieldDefinition jdbcAttribute = (JdbcFieldDefinition) attribute; if (first) { first = false; } else { sqlBuffer.append(", "); } addSqlColumEqualsPlaceholder(sqlBuffer, jdbcAttribute); } } sqlBuffer.append(" where "); first = true; for (final FieldDefinition idField : idFields) { if (first) { first = false; } else { sqlBuffer.append(" AND "); } final JdbcFieldDefinition idJdbcAttribute = (JdbcFieldDefinition) idField; addSqlColumEqualsPlaceholder(sqlBuffer, idJdbcAttribute); } sqlBuffer.append(" "); if (this.sqlSuffix != null) { sqlBuffer.append(this.sqlSuffix); } sql = sqlBuffer.toString(); this.typeUpdateSqlMap.put(typePath, sql); } return sql; }
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; }