/** * Add a {@link Field} to the map. If the name of the Field being added does not match the name, it will be aliased * to that name before being added to the map. * * @param name the key to use * @param column the Field to add * @return the value of any previous mapping with the specified key, or null if there was no mapping */ public Field<?> put(String name, Field<?> column) { if (column == null) { throw new IllegalArgumentException("Cannot use null column in ProjectionMap"); } if (SqlUtils.isEmpty(name)) { throw new IllegalArgumentException("Cannot use empty string as a key"); } if (!SqlUtils.equals(name, column.getName())) { column = column.as(name); } return map.put(name, column); }
static void addInlineCollectionToSqlString(StringBuilder sql, ArgumentResolver argResolver, Collection<?> values) { if (values != null && !values.isEmpty()) { for (Object value : values) { sql.append(toSanitizedString(value, argResolver)); sql.append(","); } sql.deleteCharAt(sql.length() - 1); } }
void addCollectionArg(Collection<?> value) { if (value != null) { if (args == null) { SqlUtils.addInlineCollectionToSqlString(sql, compileContext.getArgumentResolver(), value); } else { sql.append(SqlStatement.REPLACEABLE_ARRAY_PARAMETER); args.add(value); } } }
/** * Convert an arbitrary object to a string. If the object itself is a {@link String}, it will be sanitized. */ static String toSanitizedString(Object value, ArgumentResolver argResolver) { value = argResolver.resolveArgument(value); if (value == null) { return "NULL"; } else if (value instanceof Double || value instanceof Float) { return Double.toString(((Number) value).doubleValue()); } else if (value instanceof Number) { return Long.toString(((Number) value).longValue()); } else if (value instanceof Boolean) { return ((Boolean) value) ? "1" : "0"; } else if (value instanceof byte[]) { return byteArrayToBlobLiteral((byte[]) value); } else { return sanitizeStringAsLiteral(String.valueOf(value)); } }
/** * @return true if this object has an alias */ public boolean hasAlias() { return !SqlUtils.isEmpty(alias); }
@Override protected void afterPopulateOperator(SqlBuilder builder, boolean forSqlValidation) { super.afterPopulateOperator(builder, forSqlValidation); if (escape != '\0') { builder.sql.append(" ESCAPE ").append(SqlUtils.sanitizeStringAsLiteral(Character.toString(escape))); } }
private static void checkCacheIntegrity(AbstractModel model, JSONProperty<?> property, String transitoryKey) { if (!model.hasTransitory(transitoryKey)) { return; } JSONObjectHolder<?> holder = getJSONTransitory(model, transitoryKey); if (model.containsValue(property) || model.getDefaultValues().containsKey(property.getName())) { String jsonValue = model.get(property); if (SqlUtils.equals(holder.jsonString, jsonValue)) { return; } } model.clearTransitory(transitoryKey); }
@Override public void run() { SqlUtils.escapeLikePattern("foo", '%'); } }, IllegalArgumentException.class);
/** * @return true if this object has a qualifier */ public boolean hasQualifier() { return !SqlUtils.isEmpty(qualifier); }
/** * Convert an arbitrary object to a string. If the object itself is a {@link String}, it will be sanitized. */ static String toSanitizedString(Object value, ArgumentResolver argResolver) { value = argResolver.resolveArgument(value); if (value == null) { return "NULL"; } else if (value instanceof Double || value instanceof Float) { return Double.toString(((Number) value).doubleValue()); } else if (value instanceof Number) { return Long.toString(((Number) value).longValue()); } else if (value instanceof Boolean) { return ((Boolean) value) ? "1" : "0"; } else if (value instanceof byte[]) { return byteArrayToBlobLiteral((byte[]) value); } else { return sanitizeStringAsLiteral(String.valueOf(value)); } }
/** * Construct a StringProperty from a literal value and with the given alias, e.g. "'hello' AS greeting" * * @param literal the literal value * @param selectAs the alias to use. May be null. */ public static StringProperty literal(String literal, String selectAs) { return new StringProperty(null, SqlUtils.sanitizeStringAsLiteral(literal), selectAs, null); }
@Override public void run() { SqlUtils.escapeLikePattern("bar", '_'); } }, IllegalArgumentException.class);
private boolean propertyBelongsToTable(Property<?> property) { return property.tableModelName.modelClass != null && TableModel.class.isAssignableFrom(property.tableModelName.modelClass) && !SqlUtils.isEmpty(property.tableModelName.tableName); }
addCollectionArg((Collection<?>) value); } else if (args == null) { sql.append(SqlUtils.toSanitizedString(value, compileContext.getArgumentResolver())); } else { if (value != null) {
/** * Add a {@link Field} to the map. If the name of the Field being added does not match the name, it will be aliased * to that name before being added to the map. * * @param name the key to use * @param column the Field to add * @return the value of any previous mapping with the specified key, or null if there was no mapping */ public Field<?> put(String name, Field<?> column) { if (column == null) { throw new IllegalArgumentException("Cannot use null column in ProjectionMap"); } if (SqlUtils.isEmpty(name)) { throw new IllegalArgumentException("Cannot use empty string as a key"); } if (!SqlUtils.equals(name, column.getName())) { column = column.as(name); } return map.put(name, column); }
/** * Construct an EnumProperty from a literal enum value and with the given alias, e.g. "'ENUM_VAL_1' AS * greeting" * * @param literal the literal value * @param selectAs the alias to use. May be null. */ public static <T extends Enum<T>> EnumProperty<T> literal(T literal, String selectAs) { return new EnumProperty<>(null, literal == null ? "null" : SqlUtils.sanitizeStringAsLiteral(literal.name()), selectAs, null); }
Collection<?> values = collectionArgs.get(index); if (largeArgMode) { SqlUtils.addInlineCollectionToSqlString(result, compileContext.getArgumentResolver(), values); } else { appendCollectionVariableStringForSize(result, values.size());
public void testEscapeLikePattern() { assertEquals("", SqlUtils.escapeLikePattern(null, '^')); assertEquals("", SqlUtils.escapeLikePattern("", '^')); assertEquals("abc", SqlUtils.escapeLikePattern("abc", '^')); assertEquals("a^%c", SqlUtils.escapeLikePattern("a%c", '^')); assertEquals("a^%^%c", SqlUtils.escapeLikePattern("a%%c", '^')); assertEquals("a^_c", SqlUtils.escapeLikePattern("a_c", '^')); assertEquals("a^_^_^_c", SqlUtils.escapeLikePattern("a___c", '^')); assertEquals("a^^b^^c", SqlUtils.escapeLikePattern("a^b^c", '^')); assertEquals("", SqlUtils.escapeLikePattern(null, '\\')); assertEquals("", SqlUtils.escapeLikePattern("", '\\')); assertEquals("123", SqlUtils.escapeLikePattern("123", '\\')); assertEquals("1\\%3", SqlUtils.escapeLikePattern("1%3", '\\')); assertEquals("1\\%\\%3", SqlUtils.escapeLikePattern("1%%3", '\\')); assertEquals("1\\_3", SqlUtils.escapeLikePattern("1_3", '\\')); assertEquals("1\\_\\_\\_3", SqlUtils.escapeLikePattern("1___3", '\\')); assertEquals("1\\\\2\\\\3", SqlUtils.escapeLikePattern("1\\2\\3", '\\'));
private static void appendClause(StringBuilder s, String name, String clause) { if (!SqlUtils.isEmpty(clause)) { s.append(name); s.append(clause); } }
public void testBlobLiterals() { ArgumentResolver defaultArgumentResolver = new DefaultArgumentResolver(); assertEquals("X''", SqlUtils.toSanitizedString(new byte[0], defaultArgumentResolver)); assertEquals("X'94'", SqlUtils.toSanitizedString(new byte[]{(byte) 0x94}, defaultArgumentResolver)); assertEquals("X'5daa'", SqlUtils.toSanitizedString(new byte[]{(byte) 0x5d, (byte) 0xaa}, defaultArgumentResolver)); assertEquals("X'08312f'", SqlUtils.toSanitizedString( new byte[]{(byte) 0x08, (byte) 0x31, (byte) 0x2f}, defaultArgumentResolver)); assertEquals("X'7be6cd4b'", SqlUtils.toSanitizedString( new byte[]{(byte) 0x7b, (byte) 0xe6, (byte) 0xcd, (byte) 0x4b}, defaultArgumentResolver)); }