public void executeSql(Connection connection) throws SQLException { if (fields != null) { PreparedStatement pstInsert = null; PreparedStatement pstCreate = null; try { pstCreate = connection.prepareStatement(getCreateStatement()); pstCreate.executeUpdate(); } finally { Utils.close(pstCreate); } for (SqlTableRow row : rows) { StringBuilder insert = (new StringBuilder("insert into ")).append(tableName).append("("); StringBuilder values = new StringBuilder(); boolean first = true; for (FieldData fieldData : row.getValueList()) { if (!first) { insert.append(", "); values.append(", "); } else { first = false; } insert.append(fieldData.getName()); values.append("?"); } insert.append(") values (").append(values).append(")"); try { pstInsert = connection.prepareStatement(insert.toString()); for (int i = 0; i < row.getValueList().size(); i++) { FieldData fieldData = row.getValueList().get(i); KeyValuePair<String, SQLType> fieldDefinition = getFields().get(fieldData.getName()); pstInsert.setObject( i + 1, fieldData.getValueData(), fieldDefinition.getValue().getVendorTypeNumber()); } pstInsert.executeUpdate(); } finally { Utils.close(pstInsert); } } } }
public void merge(JsonSqlValueMap source) throws InvalidActionException { if (source == null) { throw new NullPointerException("Source object is NULL!"); } else { if (source.getRows().size() > 0) { SqlTableRow row = source.getRows().get(source.getRows().size() - 1); if (!row.isLocked()) { throw new InvalidActionException("Source is not completed!"); } if (rows.size() > 0 && !rows.get(rows.size() - 1).isLocked()) { throw new InvalidActionException("Target is not completed!"); } if (fields == null) { generateFieldsList(row); } else { for (FieldData fieldData : row.getValueList()) { if (!fields.containsKey(fieldData.getName())) { KeyValuePair<String, SQLType> newField = new KeyValuePair<>( fieldData.getName(), getFieldType(fieldData.getValueType(), fieldData.getValueData())); fields.put(fieldData.getName(), newField); } else { SQLType newFieldType = getFieldType(fieldData.getValueType(), fieldData.getValueData()); KeyValuePair<String, SQLType> existingField = fields.get(fieldData.getName()); if (!existingField.getValue().equals(newFieldType)) { SQLType targetType = JDBCType.VARCHAR; if (existingField.getValue().equals(JDBCType.INTEGER) && (newFieldType.equals(JDBCType.DOUBLE))) { targetType = newFieldType; } else if (existingField.getValue().equals(JDBCType.NULL)) { targetType = newFieldType; } existingField.setValue(targetType); } } } } rows.addAll(source.rows); } } }
private void generateFieldsList(SqlTableRow row) { if (row != null && row.isLocked()) { if (fields == null) { fields = new LinkedHashMap<>(row.size()); } for (FieldData fieldData : row.getValueList()) { String fieldName = fieldData.getName(); if (fieldName != null && !fields.containsKey(fieldName)) { KeyValuePair<String, SQLType> newField = new KeyValuePair<>( fieldName, getFieldType(fieldData.getValueType(), fieldData.getValueData())); fields.put(fieldData.getName(), newField); } } } }