Search in sources :

Example 1 with QueryBuilder

use of com.raizlabs.android.dbflow.sql.QueryBuilder in project DBFlow by Raizlabs.

the class CaseCondition method getQuery.

@Override
public String getQuery() {
    QueryBuilder queryBuilder = new QueryBuilder(" WHEN ");
    if (caze.isEfficientCase()) {
        queryBuilder.append(convertValueToString(property != null ? property : whenValue, false));
    } else {
        sqlCondition.appendConditionToQuery(queryBuilder);
    }
    queryBuilder.append(" THEN ").append(convertValueToString(isThenPropertySet ? thenProperty : thenValue, false));
    return queryBuilder.getQuery();
}
Also used : QueryBuilder(com.raizlabs.android.dbflow.sql.QueryBuilder)

Example 2 with QueryBuilder

use of com.raizlabs.android.dbflow.sql.QueryBuilder in project DBFlow by Raizlabs.

the class AlterTableMigration method getColumnDefinitions.

/**
     * @return A List of column definitions that add column to a table in the DB.
     */
public List<String> getColumnDefinitions() {
    String sql = new QueryBuilder(getAlterTableQueryBuilder()).append(FlowManager.getTableName(table)).toString();
    List<String> columnDefinitions = new ArrayList<>();
    if (this.columnDefinitions != null) {
        for (QueryBuilder columnDefinition : this.columnDefinitions) {
            QueryBuilder queryBuilder = new QueryBuilder(sql).appendSpaceSeparated("ADD COLUMN").append(columnDefinition.getQuery());
            columnDefinitions.add(queryBuilder.getQuery());
        }
    }
    return columnDefinitions;
}
Also used : ArrayList(java.util.ArrayList) QueryBuilder(com.raizlabs.android.dbflow.sql.QueryBuilder)

Example 3 with QueryBuilder

use of com.raizlabs.android.dbflow.sql.QueryBuilder in project DBFlow by Raizlabs.

the class AlterTableMigration method migrate.

@Override
public final void migrate(DatabaseWrapper database) {
    // "ALTER TABLE "
    String sql = getAlterTableQueryBuilder().getQuery();
    String tableName = FlowManager.getTableName(table);
    // Since the structure has been updated already, the manager knows only the new name.
    if (renameQuery != null) {
        String renameQuery = new QueryBuilder(sql).appendQuotedIfNeeded(oldTableName).append(this.renameQuery.getQuery()).append(tableName).toString();
        database.execSQL(renameQuery);
    }
    // ADD COLUMN columnName {type}
    if (columnDefinitions != null) {
        Cursor cursorToCheckColumnFor = SQLite.select().from(table).limit(0).query(database);
        if (cursorToCheckColumnFor != null) {
            try {
                sql = new QueryBuilder(sql).append(tableName).toString();
                for (int i = 0; i < columnDefinitions.size(); i++) {
                    QueryBuilder columnDefinition = columnDefinitions.get(i);
                    String columnName = QueryBuilder.stripQuotes(columnNames.get(i));
                    if (cursorToCheckColumnFor.getColumnIndex(columnName) == -1) {
                        database.execSQL(sql + " ADD COLUMN " + columnDefinition.getQuery());
                    }
                }
            } finally {
                cursorToCheckColumnFor.close();
            }
        }
    }
}
Also used : QueryBuilder(com.raizlabs.android.dbflow.sql.QueryBuilder) Cursor(android.database.Cursor)

Example 4 with QueryBuilder

use of com.raizlabs.android.dbflow.sql.QueryBuilder in project DBFlow by Raizlabs.

the class AlterTableMigration method addColumn.

/**
     * Add a column to the DB. This does not necessarily need to be reflected in the {@link TModel},
     * but it is recommended.
     *
     * @param sqLiteType The type of column represented in the DB.
     * @param columnName The name of the column to add. Use the "_Table" class for the specified table.
     * @return This instance
     */
public AlterTableMigration<TModel> addColumn(@NonNull SQLiteType sqLiteType, @NonNull String columnName) {
    if (columnDefinitions == null) {
        columnDefinitions = new ArrayList<>();
        columnNames = new ArrayList<>();
    }
    QueryBuilder queryBuilder = new QueryBuilder().append(QueryBuilder.quoteIfNeeded(columnName)).appendSpace().appendSQLiteType(sqLiteType);
    columnDefinitions.add(queryBuilder);
    columnNames.add(columnName);
    return this;
}
Also used : QueryBuilder(com.raizlabs.android.dbflow.sql.QueryBuilder)

Example 5 with QueryBuilder

use of com.raizlabs.android.dbflow.sql.QueryBuilder in project DBFlow by Raizlabs.

the class Trigger method getQuery.

@Override
public String getQuery() {
    QueryBuilder queryBuilder = new QueryBuilder("CREATE ");
    if (temporary) {
        queryBuilder.append("TEMP ");
    }
    queryBuilder.append("TRIGGER IF NOT EXISTS ").appendQuotedIfNeeded(triggerName).appendSpace().appendOptional(" " + beforeOrAfter + " ");
    return queryBuilder.getQuery();
}
Also used : QueryBuilder(com.raizlabs.android.dbflow.sql.QueryBuilder)

Aggregations

QueryBuilder (com.raizlabs.android.dbflow.sql.QueryBuilder)19 Cursor (android.database.Cursor)1 SQLiteException (android.database.sqlite.SQLiteException)1 TypeConverter (com.raizlabs.android.dbflow.converter.TypeConverter)1 ModelAdapter (com.raizlabs.android.dbflow.structure.ModelAdapter)1 ModelViewAdapter (com.raizlabs.android.dbflow.structure.ModelViewAdapter)1 ArrayList (java.util.ArrayList)1