Search in sources :

Example 1 with ColumnConverter

use of org.xutils.db.converter.ColumnConverter in project xUtils3 by wyouflf.

the class ColumnUtils method convert2DbValueIfNeeded.

@SuppressWarnings("unchecked")
public static Object convert2DbValueIfNeeded(final Object value) {
    Object result = value;
    if (value != null) {
        Class<?> valueType = value.getClass();
        ColumnConverter converter = ColumnConverterFactory.getColumnConverter(valueType);
        result = converter.fieldValue2DbValue(value);
    }
    return result;
}
Also used : ColumnConverter(org.xutils.db.converter.ColumnConverter)

Example 2 with ColumnConverter

use of org.xutils.db.converter.ColumnConverter in project xUtils3 by wyouflf.

the class SqlInfo method buildStatement.

public SQLiteStatement buildStatement(SQLiteDatabase database) {
    SQLiteStatement result = database.compileStatement(sql);
    if (bindArgs != null) {
        for (int i = 1; i < bindArgs.size() + 1; i++) {
            KeyValue kv = bindArgs.get(i - 1);
            Object value = ColumnUtils.convert2DbValueIfNeeded(kv.value);
            if (value == null) {
                result.bindNull(i);
            } else {
                ColumnConverter converter = ColumnConverterFactory.getColumnConverter(value.getClass());
                ColumnDbType type = converter.getColumnDbType();
                switch(type) {
                    case INTEGER:
                        result.bindLong(i, ((Number) value).longValue());
                        break;
                    case REAL:
                        result.bindDouble(i, ((Number) value).doubleValue());
                        break;
                    case TEXT:
                        result.bindString(i, value.toString());
                        break;
                    case BLOB:
                        result.bindBlob(i, (byte[]) value);
                        break;
                    default:
                        result.bindNull(i);
                        break;
                }
            // end switch
            }
        }
    }
    return result;
}
Also used : KeyValue(org.xutils.common.util.KeyValue) SQLiteStatement(android.database.sqlite.SQLiteStatement) ColumnConverter(org.xutils.db.converter.ColumnConverter)

Aggregations

ColumnConverter (org.xutils.db.converter.ColumnConverter)2 SQLiteStatement (android.database.sqlite.SQLiteStatement)1 KeyValue (org.xutils.common.util.KeyValue)1