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); }