@Override public VirtualTable as(String newAlias) { return (VirtualTable) super.as(newAlias); }
/** * Deprecated alias for {@link #getRowIdProperty()} */ @Deprecated public LongProperty getIdProperty() { return getRowIdProperty(); } }
@Override public Table as(String newAlias) { Table result = (Table) super.as(newAlias); result.rowidProperty = rowidProperty == null ? null : result.qualifyField(rowidProperty); return result; }
public Table qualifiedFromDatabase(String databaseName) { Table result = new Table(modelClass, properties, getExpression(), databaseName, tableConstraint, alias); result.rowidProperty = rowidProperty; return result; }
/** * 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(')'); }
public void testQueryAsFunction() { Table qualifiedTable = Employee.TABLE.as("e1"); Query subquery = Query.select(Function.add(qualifiedTable.qualifyField(Employee.ID), 1)) .from(qualifiedTable).where(Employee.ID.eq(qualifiedTable.qualifyField(Employee.ID))); Function<Long> fromQuery = subquery.asFunction(); LongProperty idPlus1 = LongProperty.fromFunction(fromQuery, "idPlus1"); Query baseQuery = Query.select(Employee.ID, idPlus1); SquidCursor<Employee> cursor = database.query(Employee.class, baseQuery); try { assertEquals(database.countAll(Employee.class), cursor.getCount()); while (cursor.moveToNext()) { assertEquals(cursor.get(Employee.ID) + 1, cursor.get(idPlus1).longValue()); } } finally { cursor.close(); } }
/** * @return the property representing the table's rowid column (or a integer primary key rowid alias if one exists) */ public LongProperty getRowIdProperty() { if (rowidProperty == null) { throw new UnsupportedOperationException("Table " + getExpression() + " has no id property defined"); } return rowidProperty; }
private ISQLitePreparedStatement prepareInsert(SquidDatabase db, Table table, TableStatement.ConflictAlgorithm conflictAlgorithm) { Object[] placeholders = new Object[table.getProperties().length]; Arrays.fill(placeholders, new Object()); Insert insert = Insert.into(table).columns(table.getProperties()) .values(placeholders).onConflict(conflictAlgorithm); CompiledStatement compiled = insert.compile(db.getCompileContext()); ISQLitePreparedStatement statement = db.prepareStatement(compiled.sql); dbStatementTracking.add(statement); return statement; } }
/** * Create a new {@link Table} or {@link VirtualTable} in the database * * @param table the Table or VirtualTable to create * @return true if the statement executed without error, false otherwise */ protected boolean tryCreateTable(Table table) { SqlConstructorVisitor sqlVisitor = new SqlConstructorVisitor(); StringBuilder sql = new StringBuilder(STRING_BUILDER_INITIAL_CAPACITY); table.appendCreateTableSql(getCompileContext(), sql, sqlVisitor); return tryExecSql(sql.toString()); }
@Override public String toString() { return super.toString() + " ModelClass=" + modelClass.getSimpleName() + " module=" + moduleName; }
ISQLitePreparedStatement getPreparedInsert(SquidDatabase db, Table table, TableStatement.ConflictAlgorithm conflictAlgorithm) { Class<? extends TableModel> modelClass = table.getModelClass(); ISQLitePreparedStatement[] preparedStatements = preparedStatementCache.get(modelClass); if (preparedStatements == null) { preparedStatements = new ISQLitePreparedStatement[TableStatement.ConflictAlgorithm.values().length]; preparedStatementCache.put(modelClass, preparedStatements); } if (conflictAlgorithm == null) { conflictAlgorithm = TableStatement.ConflictAlgorithm.NONE; } ISQLitePreparedStatement toReturn = preparedStatements[conflictAlgorithm.ordinal()]; if (toReturn == null) { toReturn = prepareInsert(db, table, conflictAlgorithm); preparedStatements[conflictAlgorithm.ordinal()] = toReturn; } return toReturn; }
Table managerTable = Employee.TABLE.as("managers"); StringProperty employeeName = Employee.NAME.as("employeeName"); StringProperty managerName = Employee.NAME.as(managerTable, "managerName"); LongProperty managerId = managerTable.qualifyField(Employee.ID); Join join = Join.inner(managerTable, Employee.MANAGER_ID.eq(managerId)); Query query = Query.select(employeeName, managerName).from(Employee.TABLE).join(join).orderBy(managerId.asc());
@Override protected Table asNewAliasWithPropertiesArray(String newAlias, Property<?>[] newProperties) { return new Table(modelClass, newProperties, getExpression(), qualifier, tableConstraint, newAlias); }
/** * 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(')'); }
/** * Drop a {@link Table} or {@link VirtualTable} in the database if it exists * * @param table the Table or VirtualTable to drop * @return true if the statement executed without error, false otherwise */ protected boolean tryDropTable(Table table) { return tryExecSql("DROP TABLE IF EXISTS " + table.getExpression()); }
void bindValuesForInsert(Table table, ISQLitePreparedStatement preparedInsert) { LongProperty rowidProperty = getRowIdProperty(); Property<?>[] allProperties = table.getProperties(); ModelAndIndex modelAndIndex = new ModelAndIndex(this); for (Property<?> property : allProperties) { if (property == rowidProperty) { long rowid = getRowId(); if (rowid == TableModel.NO_ID) { preparedInsert.bindNull(modelAndIndex.index); } else { preparedInsert.bindLong(modelAndIndex.index, rowid); } } else { property.accept(valueBindingVisitor, preparedInsert, modelAndIndex); } modelAndIndex.index++; } }
if (tables != null) { for (Table table : tables) { table.appendCreateTableSql(getCompileContext(), sql, sqlVisitor); db.execSQL(sql.toString()); sql.setLength(0);
@Override public String toString() { return super.toString() + " ModelClass=" + modelClass.getSimpleName() + " module=" + moduleName; }
ISQLitePreparedStatement getPreparedInsert(SquidDatabase db, Table table, TableStatement.ConflictAlgorithm conflictAlgorithm) { Class<? extends TableModel> modelClass = table.getModelClass(); ISQLitePreparedStatement[] preparedStatements = preparedStatementCache.get(modelClass); if (preparedStatements == null) { preparedStatements = new ISQLitePreparedStatement[TableStatement.ConflictAlgorithm.values().length]; preparedStatementCache.put(modelClass, preparedStatements); } if (conflictAlgorithm == null) { conflictAlgorithm = TableStatement.ConflictAlgorithm.NONE; } ISQLitePreparedStatement toReturn = preparedStatements[conflictAlgorithm.ordinal()]; if (toReturn == null) { toReturn = prepareInsert(db, table, conflictAlgorithm); preparedStatements[conflictAlgorithm.ordinal()] = toReturn; } return toReturn; }
Table employeesAlias = Employee.TABLE.as("e"); LongProperty aliasedId = employeesAlias.qualifyField(Employee.ID); LongProperty aliasedManagerId = employeesAlias.qualifyField(Employee.MANAGER_ID); StringProperty subordinates = StringProperty.fromFunction(Function.groupConcat(aliasedId, separator), "subordinates");