| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.database.sqlite; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.database.DatabaseUtils; |
| 21 |
import android.os.Build; |
| 22 |
import android.os.CancellationSignal; |
| 23 |
|
| 24 |
import java.util.Arrays; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public abstract class SQLiteProgram extends SQLiteClosable { |
| 33 |
private static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 34 |
|
| 35 |
private final SQLiteDatabase mDatabase; |
| 36 |
@UnsupportedAppUsage |
| 37 |
private final String mSql; |
| 38 |
private final boolean mReadOnly; |
| 39 |
private final String[] mColumnNames; |
| 40 |
private final int mNumParameters; |
| 41 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 42 |
private final Object[] mBindArgs; |
| 43 |
|
| 44 |
SQLiteProgram(SQLiteDatabase db, String sql, Object[] bindArgs, |
| 45 |
CancellationSignal cancellationSignalForPrepare) { |
| 46 |
mDatabase = db; |
| 47 |
mSql = sql.trim(); |
| 48 |
|
| 49 |
int n = DatabaseUtils.getSqlStatementType(mSql); |
| 50 |
switch (n) { |
| 51 |
case DatabaseUtils.STATEMENT_BEGIN: |
| 52 |
case DatabaseUtils.STATEMENT_COMMIT: |
| 53 |
case DatabaseUtils.STATEMENT_ABORT: |
| 54 |
mReadOnly = false; |
| 55 |
mColumnNames = EMPTY_STRING_ARRAY; |
| 56 |
mNumParameters = 0; |
| 57 |
break; |
| 58 |
|
| 59 |
default: |
| 60 |
boolean assumeReadOnly = (n == DatabaseUtils.STATEMENT_SELECT); |
| 61 |
SQLiteStatementInfo info = new SQLiteStatementInfo(); |
| 62 |
db.getThreadSession().prepare(mSql, |
| 63 |
db.getThreadDefaultConnectionFlags(assumeReadOnly), |
| 64 |
cancellationSignalForPrepare, info); |
| 65 |
mReadOnly = info.readOnly; |
| 66 |
mColumnNames = info.columnNames; |
| 67 |
mNumParameters = info.numParameters; |
| 68 |
break; |
| 69 |
} |
| 70 |
|
| 71 |
if (bindArgs != null && bindArgs.length > mNumParameters) { |
| 72 |
throw new IllegalArgumentException("Too many bind arguments. " |
| 73 |
+ bindArgs.length + " arguments were provided but the statement needs " |
| 74 |
+ mNumParameters + " arguments."); |
| 75 |
} |
| 76 |
|
| 77 |
if (mNumParameters != 0) { |
| 78 |
mBindArgs = new Object[mNumParameters]; |
| 79 |
if (bindArgs != null) { |
| 80 |
System.arraycopy(bindArgs, 0, mBindArgs, 0, bindArgs.length); |
| 81 |
} |
| 82 |
} else { |
| 83 |
mBindArgs = null; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
final SQLiteDatabase getDatabase() { |
| 88 |
return mDatabase; |
| 89 |
} |
| 90 |
|
| 91 |
final String getSql() { |
| 92 |
return mSql; |
| 93 |
} |
| 94 |
|
| 95 |
final Object[] getBindArgs() { |
| 96 |
return mBindArgs; |
| 97 |
} |
| 98 |
|
| 99 |
final String[] getColumnNames() { |
| 100 |
return mColumnNames; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
protected final SQLiteSession getSession() { |
| 105 |
return mDatabase.getThreadSession(); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
protected final int getConnectionFlags() { |
| 110 |
return mDatabase.getThreadDefaultConnectionFlags(mReadOnly); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
protected final void onCorruption() { |
| 115 |
mDatabase.onCorruption(); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
@Deprecated |
| 123 |
public final int getUniqueId() { |
| 124 |
return -1; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public void bindNull(int index) { |
| 134 |
bind(index, null); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public void bindLong(int index, long value) { |
| 145 |
bind(index, value); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public void bindDouble(int index, double value) { |
| 156 |
bind(index, value); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public void bindString(int index, String value) { |
| 167 |
if (value == null) { |
| 168 |
throw new IllegalArgumentException("the bind value at index " + index + " is null"); |
| 169 |
} |
| 170 |
bind(index, value); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public void bindBlob(int index, byte[] value) { |
| 181 |
if (value == null) { |
| 182 |
throw new IllegalArgumentException("the bind value at index " + index + " is null"); |
| 183 |
} |
| 184 |
bind(index, value); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public void clearBindings() { |
| 191 |
if (mBindArgs != null) { |
| 192 |
Arrays.fill(mBindArgs, null); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public void bindAllArgsAsStrings(String[] bindArgs) { |
| 202 |
if (bindArgs != null) { |
| 203 |
for (int i = bindArgs.length; i != 0; i--) { |
| 204 |
bindString(i, bindArgs[i - 1]); |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
@Override |
| 210 |
protected void onAllReferencesReleased() { |
| 211 |
clearBindings(); |
| 212 |
} |
| 213 |
|
| 214 |
private void bind(int index, Object value) { |
| 215 |
if (index < 1 || index > mNumParameters) { |
| 216 |
throw new IllegalArgumentException("Cannot bind argument at index " |
| 217 |
+ index + " because the index is out of range. " |
| 218 |
+ "The statement has " + mNumParameters + " parameters."); |
| 219 |
} |
| 220 |
mBindArgs[index - 1] = value; |
| 221 |
} |
| 222 |
} |