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 17 package androidx.sqlite.db; 18 19 import androidx.annotation.Nullable; 20 21 /** 22 * A basic implementation of {@link SupportSQLiteQuery} which receives a query and its args and 23 * binds args based on the passed in Object type. 24 */ 25 public final class SimpleSQLiteQuery implements SupportSQLiteQuery { 26 private final String mQuery; 27 @Nullable 28 private final Object[] mBindArgs; 29 30 /** 31 * Creates an SQL query with the sql string and the bind arguments. 32 * 33 * @param query The query string, can include bind arguments (.e.g ?). 34 * @param bindArgs The bind argument value that will replace the placeholders in the query. 35 */ 36 public SimpleSQLiteQuery(String query, @Nullable Object[] bindArgs) { 37 mQuery = query; 38 mBindArgs = bindArgs; 39 } 40 41 /** 42 * Creates an SQL query without any bind arguments. 43 * 44 * @param query The SQL query to execute. Cannot include bind parameters. 45 */ 46 public SimpleSQLiteQuery(String query) { 47 this(query, null); 48 } 49 50 @Override 51 public String getSql() { 52 return mQuery; 53 } 54 55 @Override 56 public void bindTo(SupportSQLiteProgram statement) { 57 bind(statement, mBindArgs); 58 } 59 60 @Override 61 public int getArgCount() { 62 return mBindArgs == null ? 0 : mBindArgs.length; 63 } 64 65 /** 66 * Binds the given arguments into the given sqlite statement. 67 * 68 * @param statement The sqlite statement 69 * @param bindArgs The list of bind arguments 70 */ 71 public static void bind(SupportSQLiteProgram statement, Object[] bindArgs) { 72 if (bindArgs == null) { 73 return; 74 } 75 final int limit = bindArgs.length; 76 for (int i = 0; i < limit; i++) { 77 final Object arg = bindArgs[i]; 78 bind(statement, i + 1, arg); 79 } 80 } 81 82 private static void bind(SupportSQLiteProgram statement, int index, Object arg) { 83 // extracted from android.database.sqlite.SQLiteConnection 84 if (arg == null) { 85 statement.bindNull(index); 86 } else if (arg instanceof byte[]) { 87 statement.bindBlob(index, (byte[]) arg); 88 } else if (arg instanceof Float) { 89 statement.bindDouble(index, (Float) arg); 90 } else if (arg instanceof Double) { 91 statement.bindDouble(index, (Double) arg); 92 } else if (arg instanceof Long) { 93 statement.bindLong(index, (Long) arg); 94 } else if (arg instanceof Integer) { 95 statement.bindLong(index, (Integer) arg); 96 } else if (arg instanceof Short) { 97 statement.bindLong(index, (Short) arg); 98 } else if (arg instanceof Byte) { 99 statement.bindLong(index, (Byte) arg); 100 } else if (arg instanceof String) { 101 statement.bindString(index, (String) arg); 102 } else if (arg instanceof Boolean) { 103 statement.bindLong(index, ((Boolean) arg) ? 1 : 0); 104 } else { 105 throw new IllegalArgumentException("Cannot bind " + arg + " at index " + index 106 + " Supported types: null, byte[], float, double, long, int, short, byte," 107 + " string"); 108 } 109 } 110 } 111