/** * @return true if this object has an alias */ public boolean hasAlias() { return !SqlUtils.isEmpty(alias); }
/** * @return true if this object has a qualifier */ public boolean hasQualifier() { return !SqlUtils.isEmpty(qualifier); }
private boolean propertyBelongsToTable(Property<?> property) { return property.tableModelName.modelClass != null && TableModel.class.isAssignableFrom(property.tableModelName.modelClass) && !SqlUtils.isEmpty(property.tableModelName.tableName); }
private static void appendClause(StringBuilder s, String name, String clause) { if (!SqlUtils.isEmpty(clause)) { s.append(name); s.append(clause); } }
if (SqlUtils.isEmpty(selection)) { return null;
/** * Add a {@link Field} represented by the given expression to the map * * @param expression the expression to add * @return the value of any previous mapping with the specified key, or null if there was no mapping */ public Field<?> put(String expression) { if (SqlUtils.isEmpty(expression)) { throw new IllegalArgumentException("Expression cannot be empty"); } return map.put(expression, Field.field(expression)); }
@Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append(Integer.toString(majorVersion)) .append('.').append(Integer.toString(minorVersion)) .append('.').append(Integer.toString(microVersion)); if (nanoVersion > 0) { builder.append('.').append(nanoVersion); } if (!SqlUtils.isEmpty(trailing)) { builder.append(trailing); } return builder.toString(); }
/** * Finds the name of the first table, which is editable. * * @param tables a list of tables * @return the first table listed */ public static String findEditTable(String tables) { if (!SqlUtils.isEmpty(tables)) { // find the first word terminated by either a space or a comma int spacepos = tables.indexOf(' '); int commapos = tables.indexOf(','); if (spacepos > 0 && (spacepos < commapos || commapos < 0)) { return tables.substring(0, spacepos); } else if (commapos > 0 && (commapos < spacepos || spacepos < 0)) { return tables.substring(0, commapos); } return tables; } else { throw new IllegalStateException("Invalid tables"); } }
/** * Escape a pattern for use in LIKE clauses. LIKE clauses support the meta-characters '_' (matching a single * character) and '%' (matching a string of any length, including the empty string). Use this method to escape * those meta-characters in the input string so that LIKE will match on them normally. This method throws an * exception if the caller passes '_' or '%' as the escape character. * * @param pattern the LIKE clause pattern * @param escape the character to use to escape meta-characters and itself in the pattern * @return an escaped pattern for LIKE clauses * @throws IllegalArgumentException if the escape character is '_' or '%' * @see com.yahoo.squidb.sql.Field#like(Object, char) */ public static String escapeLikePattern(String pattern, char escape) { if (escape == '%' || escape == '_') { throw new IllegalArgumentException("Invalid escape character: " + escape); } if (isEmpty(pattern)) { return ""; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < pattern.length(); i++) { char c = pattern.charAt(i); if (c == '%' || c == '_' || c == escape) { sb.append(escape); } sb.append(c); } return sb.toString(); } }
private Void appendColumnDefinition(String type, Property<?> property, StringBuilder sql) { sql.append(property.getName()).append(" ").append(type); if (!SqlUtils.isEmpty(property.getColumnDefinition())) { sql.append(" ").append(property.getColumnDefinition()); } return null; }
if (SqlUtils.isEmpty(versionString)) { throw new IllegalArgumentException("Empty versionString");
/** * 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); }
/** * Append a CREATE TABLE statement that would create this table and its columns. Users should not call * this method and instead let {@link com.yahoo.squidb.data.SquidDatabase} build tables automatically. */ public void appendCreateTableSql(CompileContext compileContext, StringBuilder sql, PropertyVisitor<Void, StringBuilder> propertyVisitor) { sql.append("CREATE TABLE IF NOT EXISTS ").append(getExpression()).append('('); boolean needsComma = false; for (Property<?> property : properties) { if (TableModel.ROWID.equals(property.getExpression())) { continue; } if (needsComma) { sql.append(", "); } property.accept(propertyVisitor, sql); needsComma = true; } if (!SqlUtils.isEmpty(getTableConstraint())) { sql.append(", ").append(getTableConstraint()); } sql.append(')'); }
boolean distinct, String tables, String[] columns, String where, String groupBy, String having, String orderBy, String limit) { if (SqlUtils.isEmpty(groupBy) && !SqlUtils.isEmpty(having)) { throw new IllegalArgumentException( "HAVING clauses are only permitted when using a groupBy clause"); if (!SqlUtils.isEmpty(limit) && !sLimitPattern.matcher(limit).matches()) { throw new IllegalArgumentException("invalid LIMIT clauses:" + limit);
if (!SqlUtils.isEmpty(whereClause)) { sql.append(" WHERE "); sql.append(whereClause);
/** * Clone this property with the given alias and qualified with the given table name. For example, when called on a * property represented by "table.property as property", returns a property represented by * "tableAlias.property as columnAlias". * * @param tableAlias the table name to use as the qualifier * @param columnAlias the new alias for the column * @return a clone of this property */ public Property<TYPE> as(String tableAlias, String columnAlias) { if (SqlUtils.isEmpty(tableAlias) || function != null) { return as(columnAlias); } return cloneWithExpressionAndAlias(new TableModelName(tableModelName.modelClass, tableAlias), getExpression(), columnAlias); }
/** * Build a {@link Query} combining this object's internal state with the arguments passed. If a * {@link ProjectionMap} is set, the projection elements will be evaluated and transformed accordingly. If the * sortOrder is null or empty, the default order will be used (if one was set). * * @param projection the raw column names to be selected * @param selection a raw selection string * @param selectionArgs array of strings which substitute replaceable arguments in the selection string * @param sortOrder a raw ordering clause * @return a {@link Query} using the projection, selection, selection args, and sort order */ public Query build(String[] projection, String selection, String[] selectionArgs, String sortOrder) { Query query = Query.select(computeProjection(projection)).from(dataSource); boolean hasUserSelection = !SqlUtils.isEmpty(selection); if (hasUserSelection) { query.where(Criterion.fromRawSelection(selection, selectionArgs)); } if (!SqlUtils.isEmpty(sortOrder)) { query.orderBy(Order.fromExpression(sortOrder)); } else if (defaultOrder != null && defaultOrder.length > 0) { query.orderBy(defaultOrder); } if (strictMode && hasUserSelection) { query.requestValidation(); } return query; }
/** * Convenience method for deleting rows in the database. * * @param table the table to delete from * @param whereClause the optional WHERE clause to apply when deleting. * Passing null will delete all rows. * @param whereArgs You may include ?s in the where clause, which * will be replaced by the values from whereArgs. The values * will be bound as Strings. * @return the number of rows affected if a whereClause is passed in, 0 * otherwise. To remove all rows and get a count pass "1" as the * whereClause. */ public int delete(String table, String whereClause, String[] whereArgs) { acquireReference(); try { SQLiteStatement statement = new SQLiteStatement(this, "DELETE FROM " + table + (!SqlUtils.isEmpty(whereClause) ? " WHERE " + whereClause : ""), whereArgs); try { return statement.executeUpdateDelete(); } finally { statement.close(); } } finally { releaseReference(); } }
/** * @return true if this object has an alias */ public boolean hasAlias() { return !SqlUtils.isEmpty(alias); }
private boolean propertyBelongsToTable(Property<?> property) { return property.tableModelName.modelClass != null && TableModel.class.isAssignableFrom(property.tableModelName.modelClass) && !SqlUtils.isEmpty(property.tableModelName.tableName); }