|
| 1 | +/* |
| 2 | + * Copyright (C) 2006 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 net.sqlcipher; |
| 18 | + |
| 19 | +import android.database.CharArrayBuffer; |
| 20 | + |
| 21 | +/** |
| 22 | + * A base class for Cursors that store their data in {@link CursorWindow}s. |
| 23 | + */ |
| 24 | +public abstract class AbstractWindowedCursor extends AbstractCursor |
| 25 | +{ |
| 26 | + @Override |
| 27 | + public byte[] getBlob(int columnIndex) |
| 28 | + { |
| 29 | + checkPosition(); |
| 30 | + |
| 31 | + synchronized(mUpdatedRows) { |
| 32 | + if (isFieldUpdated(columnIndex)) { |
| 33 | + return (byte[])getUpdatedField(columnIndex); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return mWindow.getBlob(mPos, columnIndex); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public String getString(int columnIndex) |
| 42 | + { |
| 43 | + checkPosition(); |
| 44 | + |
| 45 | + synchronized(mUpdatedRows) { |
| 46 | + if (isFieldUpdated(columnIndex)) { |
| 47 | + return (String)getUpdatedField(columnIndex); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return mWindow.getString(mPos, columnIndex); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) |
| 56 | + { |
| 57 | + checkPosition(); |
| 58 | + |
| 59 | + synchronized(mUpdatedRows) { |
| 60 | + if (isFieldUpdated(columnIndex)) { |
| 61 | + super.copyStringToBuffer(columnIndex, buffer); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + mWindow.copyStringToBuffer(mPos, columnIndex, buffer); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public short getShort(int columnIndex) |
| 70 | + { |
| 71 | + checkPosition(); |
| 72 | + |
| 73 | + synchronized(mUpdatedRows) { |
| 74 | + if (isFieldUpdated(columnIndex)) { |
| 75 | + Number value = (Number)getUpdatedField(columnIndex); |
| 76 | + return value.shortValue(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return mWindow.getShort(mPos, columnIndex); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public int getInt(int columnIndex) |
| 85 | + { |
| 86 | + checkPosition(); |
| 87 | + |
| 88 | + synchronized(mUpdatedRows) { |
| 89 | + if (isFieldUpdated(columnIndex)) { |
| 90 | + Number value = (Number)getUpdatedField(columnIndex); |
| 91 | + return value.intValue(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return mWindow.getInt(mPos, columnIndex); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public long getLong(int columnIndex) |
| 100 | + { |
| 101 | + checkPosition(); |
| 102 | + |
| 103 | + synchronized(mUpdatedRows) { |
| 104 | + if (isFieldUpdated(columnIndex)) { |
| 105 | + Number value = (Number)getUpdatedField(columnIndex); |
| 106 | + return value.longValue(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return mWindow.getLong(mPos, columnIndex); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public float getFloat(int columnIndex) |
| 115 | + { |
| 116 | + checkPosition(); |
| 117 | + |
| 118 | + synchronized(mUpdatedRows) { |
| 119 | + if (isFieldUpdated(columnIndex)) { |
| 120 | + Number value = (Number)getUpdatedField(columnIndex); |
| 121 | + return value.floatValue(); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return mWindow.getFloat(mPos, columnIndex); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public double getDouble(int columnIndex) |
| 130 | + { |
| 131 | + checkPosition(); |
| 132 | + |
| 133 | + synchronized(mUpdatedRows) { |
| 134 | + if (isFieldUpdated(columnIndex)) { |
| 135 | + Number value = (Number)getUpdatedField(columnIndex); |
| 136 | + return value.doubleValue(); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return mWindow.getDouble(mPos, columnIndex); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public boolean isNull(int columnIndex) |
| 145 | + { |
| 146 | + checkPosition(); |
| 147 | + |
| 148 | + synchronized(mUpdatedRows) { |
| 149 | + if (isFieldUpdated(columnIndex)) { |
| 150 | + return getUpdatedField(columnIndex) == null; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return mWindow.isNull(mPos, columnIndex); |
| 155 | + } |
| 156 | + |
| 157 | + public boolean isBlob(int columnIndex) |
| 158 | + { |
| 159 | + checkPosition(); |
| 160 | + |
| 161 | + synchronized(mUpdatedRows) { |
| 162 | + if (isFieldUpdated(columnIndex)) { |
| 163 | + Object object = getUpdatedField(columnIndex); |
| 164 | + return object == null || object instanceof byte[]; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + return mWindow.isBlob(mPos, columnIndex); |
| 169 | + } |
| 170 | + |
| 171 | + public boolean isString(int columnIndex) |
| 172 | + { |
| 173 | + checkPosition(); |
| 174 | + |
| 175 | + synchronized(mUpdatedRows) { |
| 176 | + if (isFieldUpdated(columnIndex)) { |
| 177 | + Object object = getUpdatedField(columnIndex); |
| 178 | + return object == null || object instanceof String; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + return mWindow.isString(mPos, columnIndex); |
| 183 | + } |
| 184 | + |
| 185 | + public boolean isLong(int columnIndex) |
| 186 | + { |
| 187 | + checkPosition(); |
| 188 | + |
| 189 | + synchronized(mUpdatedRows) { |
| 190 | + if (isFieldUpdated(columnIndex)) { |
| 191 | + Object object = getUpdatedField(columnIndex); |
| 192 | + return object != null && (object instanceof Integer || object instanceof Long); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + return mWindow.isLong(mPos, columnIndex); |
| 197 | + } |
| 198 | + |
| 199 | + public boolean isFloat(int columnIndex) |
| 200 | + { |
| 201 | + checkPosition(); |
| 202 | + |
| 203 | + synchronized(mUpdatedRows) { |
| 204 | + if (isFieldUpdated(columnIndex)) { |
| 205 | + Object object = getUpdatedField(columnIndex); |
| 206 | + return object != null && (object instanceof Float || object instanceof Double); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + return mWindow.isFloat(mPos, columnIndex); |
| 211 | + } |
| 212 | + |
| 213 | + @Override |
| 214 | + public int getType(int columnIndex) { |
| 215 | + checkPosition(); |
| 216 | + return mWindow.getType(mPos, columnIndex); |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + protected void checkPosition() |
| 221 | + { |
| 222 | + super.checkPosition(); |
| 223 | + |
| 224 | + if (mWindow == null) { |
| 225 | + throw new StaleDataException("Access closed cursor"); |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + @Override |
| 230 | + public CursorWindow getWindow() { |
| 231 | + return mWindow; |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * Set a new cursor window to cursor, usually set a remote cursor window |
| 236 | + * @param window cursor window |
| 237 | + */ |
| 238 | + public void setWindow(CursorWindow window) { |
| 239 | + if (mWindow != null) { |
| 240 | + mWindow.close(); |
| 241 | + } |
| 242 | + mWindow = window; |
| 243 | + } |
| 244 | + |
| 245 | + public boolean hasWindow() { |
| 246 | + return mWindow != null; |
| 247 | + } |
| 248 | + |
| 249 | + /** |
| 250 | + * This needs be updated in {@link #onMove} by subclasses, and |
| 251 | + * needs to be set to NULL when the contents of the cursor change. |
| 252 | + */ |
| 253 | + protected CursorWindow mWindow; |
| 254 | +} |
0 commit comments