private List<String> getPrimaryColumns(Storable storable, final String formatter) { return storable.getPrimaryKey().getFieldsToVal().keySet().stream().map( colField -> String.format(formatter, colField.getName())).collect(Collectors.toList()); }
private List<String> getPrimaryColumns(Storable storable, final String formatter) { return storable.getPrimaryKey().getFieldsToVal().keySet().stream().map( colField -> String.format(formatter, colField.getName())).collect(Collectors.toList()); }
public AbstractStorableUpdateQuery(Storable storable) { super(storable); Map<String, Object> columnsToValues = storable.toMap(); columns.forEach(col -> bindings.add(Pair.of(col, columnsToValues.get(col.getName())))); primaryKey.getFieldsToVal().forEach((f, o) -> { bindings.add(Pair.of(f, o)); whereFields.add(f); }); try { Optional<Pair<Field, Long>> versionFieldValue = StorageUtils.getVersionFieldValue(storable); if (versionFieldValue.isPresent()) { Pair<Field, Long> fv = versionFieldValue.get(); Schema.Field versionField = Schema.Field.of(fv.getKey().getName(), Schema.fromJavaType(fv.getValue().getClass())); whereFields.add(versionField); // update only if its the previous bindings.add(Pair.of(versionField, fv.getValue() - 1)); } } catch (Exception ex) { LOG.error("Got exception", ex); } }
public AbstractStorableKeyQuery(StorableKey storableKey) { tableName = storableKey.getNameSpace(); primaryKey = storableKey.getPrimaryKey(); columns = new LinkedList<>(storableKey.getPrimaryKey().getFieldsToVal().keySet()); } }
public AbstractStorableKeyQuery(StorableKey storableKey) { tableName = storableKey.getNameSpace(); primaryKey = storableKey.getPrimaryKey(); columns = new LinkedList<>(storableKey.getPrimaryKey().getFieldsToVal().keySet()); } }
public AbstractStorableUpdateQuery(Storable storable) { super(storable); Map<String, Object> columnsToValues = storable.toMap(); columns.forEach(col -> bindings.add(Pair.of(col, columnsToValues.get(col.getName())))); primaryKey.getFieldsToVal().forEach((f, o) -> { bindings.add(Pair.of(f, o)); whereFields.add(f); }); try { Optional<Pair<Field, Long>> versionFieldValue = StorageUtils.getVersionFieldValue(storable); if (versionFieldValue.isPresent()) { Pair<Field, Long> fv = versionFieldValue.get(); Schema.Field versionField = Schema.Field.of(fv.getKey().getName(), Schema.fromJavaType(fv.getValue().getClass())); whereFields.add(versionField); // update only if its the previous bindings.add(Pair.of(versionField, fv.getValue() - 1)); } } catch (Exception ex) { LOG.error("Got exception", ex); } }
private void setStorableKeyPreparedStatement(SqlQuery sqlBuilder) throws SQLException { final List<Schema.Field> columns = sqlBuilder.getColumns(); if (columns != null) { final int len = columns.size(); Map<Schema.Field, Object> columnsToValues = sqlBuilder.getPrimaryKey().getFieldsToVal(); for (int j = 0; j < numPrepStmtParams; j++) { Schema.Field column = columns.get(j % len); Schema.Type javaType = column.getType(); storageDataTypeContext.setPreparedStatementParams(preparedStatement, javaType, j + 1, columnsToValues.get(column)); } } }
private void setStorableKeyPreparedStatement(SqlQuery sqlBuilder) throws SQLException { final List<Schema.Field> columns = sqlBuilder.getColumns(); if (columns != null) { final int len = columns.size(); Map<Schema.Field, Object> columnsToValues = sqlBuilder.getPrimaryKey().getFieldsToVal(); for (int j = 0; j < numPrepStmtParams; j++) { Schema.Field column = columns.get(j % len); Schema.Type javaType = column.getType(); storageDataTypeContext.setPreparedStatementParams(preparedStatement, javaType, j + 1, columnsToValues.get(column)); } } }
@Override protected String getParameterizedSql() { String sql = "SELECT * FROM \"" + tableName + "\""; if (columns != null) { List<String> whereClauseColumns = new LinkedList<>(); for (Map.Entry<Schema.Field, Object> columnKeyValue : primaryKey.getFieldsToVal().entrySet()) { if (columnKeyValue.getKey().getType() == Schema.Type.STRING) { String stringValue = (String) columnKeyValue.getValue(); if ((stringValue).length() > 4000) { throw new OracleQueryException(String.format("Column \"%s\" of the table \"%s\" is compared against a value \"%s\", " + "which is greater than 4k characters", columnKeyValue.getKey().getName(), tableName, stringValue)); } else whereClauseColumns.add(String.format(" to_char(\"%s\") = ?", columnKeyValue.getKey().getName())); } else { whereClauseColumns.add(String.format(" \"%s\" = ?", columnKeyValue.getKey().getName())); } } sql += " WHERE " + join(whereClauseColumns, " AND "); } LOG.debug(sql); return sql; }
@Override protected String getParameterizedSql() { String sql = "SELECT * FROM \"" + tableName + "\""; if (columns != null) { List<String> whereClauseColumns = new LinkedList<>(); for (Map.Entry<Schema.Field, Object> columnKeyValue : primaryKey.getFieldsToVal().entrySet()) { if (columnKeyValue.getKey().getType() == Schema.Type.STRING) { String stringValue = (String) columnKeyValue.getValue(); if ((stringValue).length() > 4000) { throw new OracleQueryException(String.format("Column \"%s\" of the table \"%s\" is compared against a value \"%s\", " + "which is greater than 4k characters", columnKeyValue.getKey().getName(), tableName, stringValue)); } else whereClauseColumns.add(String.format(" to_char(\"%s\") = ?", columnKeyValue.getKey().getName())); } else { whereClauseColumns.add(String.format(" \"%s\" = ?", columnKeyValue.getKey().getName())); } } sql += " WHERE " + join(whereClauseColumns, " AND "); } LOG.debug(sql); return sql; }
protected List<QueryParam> buildQueryParamsForPrimaryKey(Storable storable) { final Map<Schema.Field, Object> fieldsToVal = storable.getPrimaryKey().getFieldsToVal(); final List<QueryParam> queryParams = new ArrayList<>(fieldsToVal.size()); for (Schema.Field field : fieldsToVal.keySet()) { QueryParam qp = new QueryParam(field.getName(), fieldsToVal.get(field).toString()); queryParams.add(qp); } return queryParams; }