1/* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.arch.persistence.db; 18 19/** 20 * A basic implemtation of {@link SupportSQLiteQuery} which receives a query and its args and binds 21 * args based on the passed in Object type. 22 */ 23public final class SimpleSQLiteQuery implements SupportSQLiteQuery { 24 private final String mQuery; 25 private final Object[] mBindArgs; 26 27 /** 28 * Creates an SQL query with the sql string and the bind arguments. 29 * 30 * @param query The query string, can include bind arguments (.e.g ?). 31 * @param bindArgs The bind argument value that will replace the placeholders in the query. 32 */ 33 public SimpleSQLiteQuery(String query, Object[] bindArgs) { 34 mQuery = query; 35 mBindArgs = bindArgs; 36 } 37 38 /** 39 * Creates an SQL query without any bind arguments. 40 * 41 * @param query The SQL query to execute. Cannot include bind parameters. 42 */ 43 public SimpleSQLiteQuery(String query) { 44 this(query, null); 45 } 46 47 @Override 48 public String getSql() { 49 return mQuery; 50 } 51 52 @Override 53 public void bindTo(SupportSQLiteProgram statement) { 54 bind(statement, mBindArgs); 55 } 56 57 /** 58 * Binds the given arguments into the given sqlite statement. 59 * 60 * @param statement The sqlite statement 61 * @param bindArgs The list of bind arguments 62 */ 63 public static void bind(SupportSQLiteProgram statement, Object[] bindArgs) { 64 if (bindArgs == null) { 65 return; 66 } 67 final int limit = bindArgs.length; 68 for (int i = 0; i < limit; i++) { 69 final Object arg = bindArgs[i]; 70 bind(statement, i + 1, arg); 71 } 72 } 73 74 private static void bind(SupportSQLiteProgram statement, int index, Object arg) { 75 // extracted from android.database.sqlite.SQLiteConnection 76 if (arg == null) { 77 statement.bindNull(index); 78 } else if (arg instanceof byte[]) { 79 statement.bindBlob(index, (byte[]) arg); 80 } else if (arg instanceof Float) { 81 statement.bindDouble(index, (Float) arg); 82 } else if (arg instanceof Double) { 83 statement.bindDouble(index, (Double) arg); 84 } else if (arg instanceof Long) { 85 statement.bindLong(index, (Long) arg); 86 } else if (arg instanceof Integer) { 87 statement.bindLong(index, (Integer) arg); 88 } else if (arg instanceof Short) { 89 statement.bindLong(index, (Short) arg); 90 } else if (arg instanceof Byte) { 91 statement.bindLong(index, (Byte) arg); 92 } else if (arg instanceof String) { 93 statement.bindString(index, (String) arg); 94 } else { 95 throw new IllegalArgumentException("Cannot bind " + arg + " at index " + index 96 + " Supported types: null, byte[], float, double, long, int, short, byte," 97 + " string"); 98 } 99 } 100} 101