The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | #undef LOG_TAG |
| 18 | #define LOG_TAG "CursorWindow" |
| 19 | |
| 20 | #include <jni.h> |
| 21 | #include <JNIHelp.h> |
| 22 | #include <android_runtime/AndroidRuntime.h> |
| 23 | |
| 24 | #include <utils/Log.h> |
| 25 | #include <utils/String8.h> |
| 26 | #include <utils/String16.h> |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 27 | #include <utils/Unicode.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
| 29 | #include <stdio.h> |
| 30 | #include <string.h> |
| 31 | #include <unistd.h> |
| 32 | |
Mathias Agopian | 49d2b18 | 2012-02-27 18:11:20 -0800 | [diff] [blame] | 33 | #include <androidfw/CursorWindow.h> |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 34 | #include "android_os_Parcel.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | #include "android_util_Binder.h" |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 36 | #include "android_database_SQLiteCommon.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | namespace android { |
| 39 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 40 | static struct { |
| 41 | jfieldID data; |
| 42 | jfieldID sizeCopied; |
| 43 | } gCharArrayBufferClassInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 45 | static jstring gEmptyString; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 47 | static void throwExceptionWithRowCol(JNIEnv* env, jint row, jint column) { |
| 48 | String8 msg; |
| 49 | msg.appendFormat("Couldn't read row %d, col %d from CursorWindow. " |
| 50 | "Make sure the Cursor is initialized correctly before accessing data from it.", |
| 51 | row, column); |
| 52 | jniThrowException(env, "java/lang/IllegalStateException", msg.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 55 | static void throwUnknownTypeException(JNIEnv * env, jint type) { |
| 56 | String8 msg; |
| 57 | msg.appendFormat("UNKNOWN type %d", type); |
| 58 | jniThrowException(env, "java/lang/IllegalStateException", msg.string()); |
| 59 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
Jeff Brown | 5e5d6d8 | 2011-10-12 15:41:34 -0700 | [diff] [blame] | 61 | static jint nativeCreate(JNIEnv* env, jclass clazz, jstring nameObj, jint cursorWindowSize) { |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 62 | String8 name; |
Jeff Brown | 5a05c23 | 2012-01-12 12:04:22 -0800 | [diff] [blame] | 63 | const char* nameStr = env->GetStringUTFChars(nameObj, NULL); |
| 64 | name.setTo(nameStr); |
| 65 | env->ReleaseStringUTFChars(nameObj, nameStr); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 66 | |
| 67 | CursorWindow* window; |
Jeff Brown | 5e5d6d8 | 2011-10-12 15:41:34 -0700 | [diff] [blame] | 68 | status_t status = CursorWindow::create(name, cursorWindowSize, &window); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 69 | if (status || !window) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 70 | ALOGE("Could not allocate CursorWindow '%s' of size %d due to error %d.", |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 71 | name.string(), cursorWindowSize, status); |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 72 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 75 | LOG_WINDOW("nativeInitializeEmpty: window = %p", window); |
| 76 | return reinterpret_cast<jint>(window); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 79 | static jint nativeCreateFromParcel(JNIEnv* env, jclass clazz, jobject parcelObj) { |
| 80 | Parcel* parcel = parcelForJavaObject(env, parcelObj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 82 | CursorWindow* window; |
| 83 | status_t status = CursorWindow::createFromParcel(parcel, &window); |
| 84 | if (status || !window) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 85 | ALOGE("Could not create CursorWindow from Parcel due to error %d.", status); |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 86 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 89 | LOG_WINDOW("nativeInitializeFromBinder: numRows = %d, numColumns = %d, window = %p", |
| 90 | window->getNumRows(), window->getNumColumns(), window); |
| 91 | return reinterpret_cast<jint>(window); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 94 | static void nativeDispose(JNIEnv* env, jclass clazz, jint windowPtr) { |
| 95 | CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); |
| 96 | if (window) { |
| 97 | LOG_WINDOW("Closing window %p", window); |
| 98 | delete window; |
| 99 | } |
| 100 | } |
| 101 | |
Jeff Brown | 650de3d | 2011-10-27 14:52:28 -0700 | [diff] [blame] | 102 | static jstring nativeGetName(JNIEnv* env, jclass clazz, jint windowPtr) { |
| 103 | CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); |
| 104 | return env->NewStringUTF(window->name().string()); |
| 105 | } |
| 106 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 107 | static void nativeWriteToParcel(JNIEnv * env, jclass clazz, jint windowPtr, |
| 108 | jobject parcelObj) { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 109 | CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 110 | Parcel* parcel = parcelForJavaObject(env, parcelObj); |
| 111 | |
| 112 | status_t status = window->writeToParcel(parcel); |
| 113 | if (status) { |
| 114 | String8 msg; |
| 115 | msg.appendFormat("Could not write CursorWindow to Parcel due to error %d.", status); |
| 116 | jniThrowRuntimeException(env, msg.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 120 | static void nativeClear(JNIEnv * env, jclass clazz, jint windowPtr) { |
| 121 | CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); |
| 122 | LOG_WINDOW("Clearing window %p", window); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 123 | status_t status = window->clear(); |
| 124 | if (status) { |
| 125 | LOG_WINDOW("Could not clear window. error=%d", status); |
| 126 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 129 | static jint nativeGetNumRows(JNIEnv* env, jclass clazz, jint windowPtr) { |
| 130 | CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); |
| 131 | return window->getNumRows(); |
| 132 | } |
| 133 | |
| 134 | static jboolean nativeSetNumColumns(JNIEnv* env, jclass clazz, jint windowPtr, |
| 135 | jint columnNum) { |
| 136 | CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 137 | status_t status = window->setNumColumns(columnNum); |
| 138 | return status == OK; |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static jboolean nativeAllocRow(JNIEnv* env, jclass clazz, jint windowPtr) { |
| 142 | CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 143 | status_t status = window->allocRow(); |
| 144 | return status == OK; |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static void nativeFreeLastRow(JNIEnv* env, jclass clazz, jint windowPtr) { |
| 148 | CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); |
| 149 | window->freeLastRow(); |
| 150 | } |
| 151 | |
| 152 | static jint nativeGetType(JNIEnv* env, jclass clazz, jint windowPtr, |
| 153 | jint row, jint column) { |
| 154 | CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); |
| 155 | LOG_WINDOW("returning column type affinity for %d,%d from %p", row, column, window); |
| 156 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 157 | CursorWindow::FieldSlot* fieldSlot = window->getFieldSlot(row, column); |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 158 | if (!fieldSlot) { |
Jeff Brown | aa32c30 | 2011-10-07 13:15:59 -0700 | [diff] [blame] | 159 | // FIXME: This is really broken but we have CTS tests that depend |
| 160 | // on this legacy behavior. |
| 161 | //throwExceptionWithRowCol(env, row, column); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 162 | return CursorWindow::FIELD_TYPE_NULL; |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 163 | } |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 164 | return window->getFieldSlotType(fieldSlot); |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static jbyteArray nativeGetBlob(JNIEnv* env, jclass clazz, jint windowPtr, |
| 168 | jint row, jint column) { |
| 169 | CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); |
| 170 | LOG_WINDOW("Getting blob for %d,%d from %p", row, column, window); |
| 171 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 172 | CursorWindow::FieldSlot* fieldSlot = window->getFieldSlot(row, column); |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 173 | if (!fieldSlot) { |
| 174 | throwExceptionWithRowCol(env, row, column); |
| 175 | return NULL; |
| 176 | } |
| 177 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 178 | int32_t type = window->getFieldSlotType(fieldSlot); |
| 179 | if (type == CursorWindow::FIELD_TYPE_BLOB || type == CursorWindow::FIELD_TYPE_STRING) { |
| 180 | size_t size; |
| 181 | const void* value = window->getFieldSlotValueBlob(fieldSlot, &size); |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 182 | jbyteArray byteArray = env->NewByteArray(size); |
| 183 | if (!byteArray) { |
| 184 | env->ExceptionClear(); |
| 185 | throw_sqlite3_exception(env, "Native could not create new byte[]"); |
| 186 | return NULL; |
| 187 | } |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 188 | env->SetByteArrayRegion(byteArray, 0, size, static_cast<const jbyte*>(value)); |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 189 | return byteArray; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 190 | } else if (type == CursorWindow::FIELD_TYPE_INTEGER) { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 191 | throw_sqlite3_exception(env, "INTEGER data in nativeGetBlob "); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 192 | } else if (type == CursorWindow::FIELD_TYPE_FLOAT) { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 193 | throw_sqlite3_exception(env, "FLOAT data in nativeGetBlob "); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 194 | } else if (type == CursorWindow::FIELD_TYPE_NULL) { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 195 | // do nothing |
| 196 | } else { |
| 197 | throwUnknownTypeException(env, type); |
| 198 | } |
| 199 | return NULL; |
| 200 | } |
| 201 | |
| 202 | static jstring nativeGetString(JNIEnv* env, jclass clazz, jint windowPtr, |
| 203 | jint row, jint column) { |
| 204 | CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); |
| 205 | LOG_WINDOW("Getting string for %d,%d from %p", row, column, window); |
| 206 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 207 | CursorWindow::FieldSlot* fieldSlot = window->getFieldSlot(row, column); |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 208 | if (!fieldSlot) { |
| 209 | throwExceptionWithRowCol(env, row, column); |
| 210 | return NULL; |
| 211 | } |
| 212 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 213 | int32_t type = window->getFieldSlotType(fieldSlot); |
| 214 | if (type == CursorWindow::FIELD_TYPE_STRING) { |
| 215 | size_t sizeIncludingNull; |
| 216 | const char* value = window->getFieldSlotValueString(fieldSlot, &sizeIncludingNull); |
| 217 | if (sizeIncludingNull <= 1) { |
Jeff Brown | 715311f | 2011-10-07 14:17:09 -0700 | [diff] [blame] | 218 | return gEmptyString; |
| 219 | } |
| 220 | // Convert to UTF-16 here instead of calling NewStringUTF. NewStringUTF |
| 221 | // doesn't like UTF-8 strings with high codepoints. It actually expects |
| 222 | // Modified UTF-8 with encoded surrogate pairs. |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 223 | String16 utf16(value, sizeIncludingNull - 1); |
Jeff Brown | 715311f | 2011-10-07 14:17:09 -0700 | [diff] [blame] | 224 | return env->NewString(reinterpret_cast<const jchar*>(utf16.string()), utf16.size()); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 225 | } else if (type == CursorWindow::FIELD_TYPE_INTEGER) { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 226 | int64_t value = window->getFieldSlotValueLong(fieldSlot); |
| 227 | char buf[32]; |
| 228 | snprintf(buf, sizeof(buf), "%lld", value); |
| 229 | return env->NewStringUTF(buf); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 230 | } else if (type == CursorWindow::FIELD_TYPE_FLOAT) { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 231 | double value = window->getFieldSlotValueDouble(fieldSlot); |
| 232 | char buf[32]; |
| 233 | snprintf(buf, sizeof(buf), "%g", value); |
| 234 | return env->NewStringUTF(buf); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 235 | } else if (type == CursorWindow::FIELD_TYPE_NULL) { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 236 | return NULL; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 237 | } else if (type == CursorWindow::FIELD_TYPE_BLOB) { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 238 | throw_sqlite3_exception(env, "Unable to convert BLOB to string"); |
| 239 | return NULL; |
| 240 | } else { |
| 241 | throwUnknownTypeException(env, type); |
| 242 | return NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 246 | static jcharArray allocCharArrayBuffer(JNIEnv* env, jobject bufferObj, size_t size) { |
| 247 | jcharArray dataObj = jcharArray(env->GetObjectField(bufferObj, |
| 248 | gCharArrayBufferClassInfo.data)); |
| 249 | if (dataObj && size) { |
| 250 | jsize capacity = env->GetArrayLength(dataObj); |
| 251 | if (size_t(capacity) < size) { |
| 252 | env->DeleteLocalRef(dataObj); |
| 253 | dataObj = NULL; |
| 254 | } |
| 255 | } |
| 256 | if (!dataObj) { |
| 257 | jsize capacity = size; |
| 258 | if (capacity < 64) { |
| 259 | capacity = 64; |
| 260 | } |
| 261 | dataObj = env->NewCharArray(capacity); // might throw OOM |
| 262 | if (dataObj) { |
| 263 | env->SetObjectField(bufferObj, gCharArrayBufferClassInfo.data, dataObj); |
| 264 | } |
| 265 | } |
| 266 | return dataObj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 269 | static void fillCharArrayBufferUTF(JNIEnv* env, jobject bufferObj, |
| 270 | const char* str, size_t len) { |
| 271 | ssize_t size = utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str), len); |
| 272 | if (size < 0) { |
| 273 | size = 0; // invalid UTF8 string |
| 274 | } |
| 275 | jcharArray dataObj = allocCharArrayBuffer(env, bufferObj, size); |
| 276 | if (dataObj) { |
| 277 | if (size) { |
| 278 | jchar* data = static_cast<jchar*>(env->GetPrimitiveArrayCritical(dataObj, NULL)); |
Jeff Brown | d0ff68d | 2011-10-07 13:28:18 -0700 | [diff] [blame] | 279 | utf8_to_utf16_no_null_terminator(reinterpret_cast<const uint8_t*>(str), len, |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 280 | reinterpret_cast<char16_t*>(data)); |
| 281 | env->ReleasePrimitiveArrayCritical(dataObj, data, 0); |
| 282 | } |
| 283 | env->SetIntField(bufferObj, gCharArrayBufferClassInfo.sizeCopied, size); |
| 284 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 287 | static void clearCharArrayBuffer(JNIEnv* env, jobject bufferObj) { |
| 288 | jcharArray dataObj = allocCharArrayBuffer(env, bufferObj, 0); |
| 289 | if (dataObj) { |
| 290 | env->SetIntField(bufferObj, gCharArrayBufferClassInfo.sizeCopied, 0); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | static void nativeCopyStringToBuffer(JNIEnv* env, jclass clazz, jint windowPtr, |
| 295 | jint row, jint column, jobject bufferObj) { |
| 296 | CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); |
| 297 | LOG_WINDOW("Copying string for %d,%d from %p", row, column, window); |
| 298 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 299 | CursorWindow::FieldSlot* fieldSlot = window->getFieldSlot(row, column); |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 300 | if (!fieldSlot) { |
| 301 | throwExceptionWithRowCol(env, row, column); |
| 302 | return; |
| 303 | } |
| 304 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 305 | int32_t type = window->getFieldSlotType(fieldSlot); |
| 306 | if (type == CursorWindow::FIELD_TYPE_STRING) { |
| 307 | size_t sizeIncludingNull; |
| 308 | const char* value = window->getFieldSlotValueString(fieldSlot, &sizeIncludingNull); |
| 309 | if (sizeIncludingNull > 1) { |
| 310 | fillCharArrayBufferUTF(env, bufferObj, value, sizeIncludingNull - 1); |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 311 | } else { |
| 312 | clearCharArrayBuffer(env, bufferObj); |
| 313 | } |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 314 | } else if (type == CursorWindow::FIELD_TYPE_INTEGER) { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 315 | int64_t value = window->getFieldSlotValueLong(fieldSlot); |
| 316 | char buf[32]; |
| 317 | snprintf(buf, sizeof(buf), "%lld", value); |
| 318 | fillCharArrayBufferUTF(env, bufferObj, buf, strlen(buf)); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 319 | } else if (type == CursorWindow::FIELD_TYPE_FLOAT) { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 320 | double value = window->getFieldSlotValueDouble(fieldSlot); |
| 321 | char buf[32]; |
| 322 | snprintf(buf, sizeof(buf), "%g", value); |
| 323 | fillCharArrayBufferUTF(env, bufferObj, buf, strlen(buf)); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 324 | } else if (type == CursorWindow::FIELD_TYPE_NULL) { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 325 | clearCharArrayBuffer(env, bufferObj); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 326 | } else if (type == CursorWindow::FIELD_TYPE_BLOB) { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 327 | throw_sqlite3_exception(env, "Unable to convert BLOB to string"); |
| 328 | } else { |
| 329 | throwUnknownTypeException(env, type); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | static jlong nativeGetLong(JNIEnv* env, jclass clazz, jint windowPtr, |
| 334 | jint row, jint column) { |
| 335 | CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); |
| 336 | LOG_WINDOW("Getting long for %d,%d from %p", row, column, window); |
| 337 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 338 | CursorWindow::FieldSlot* fieldSlot = window->getFieldSlot(row, column); |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 339 | if (!fieldSlot) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | throwExceptionWithRowCol(env, row, column); |
| 341 | return 0; |
| 342 | } |
| 343 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 344 | int32_t type = window->getFieldSlotType(fieldSlot); |
| 345 | if (type == CursorWindow::FIELD_TYPE_INTEGER) { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 346 | return window->getFieldSlotValueLong(fieldSlot); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 347 | } else if (type == CursorWindow::FIELD_TYPE_STRING) { |
| 348 | size_t sizeIncludingNull; |
| 349 | const char* value = window->getFieldSlotValueString(fieldSlot, &sizeIncludingNull); |
| 350 | return sizeIncludingNull > 1 ? strtoll(value, NULL, 0) : 0L; |
| 351 | } else if (type == CursorWindow::FIELD_TYPE_FLOAT) { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 352 | return jlong(window->getFieldSlotValueDouble(fieldSlot)); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 353 | } else if (type == CursorWindow::FIELD_TYPE_NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | return 0; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 355 | } else if (type == CursorWindow::FIELD_TYPE_BLOB) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | throw_sqlite3_exception(env, "Unable to convert BLOB to long"); |
| 357 | return 0; |
| 358 | } else { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 359 | throwUnknownTypeException(env, type); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | return 0; |
| 361 | } |
| 362 | } |
| 363 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 364 | static jdouble nativeGetDouble(JNIEnv* env, jclass clazz, jint windowPtr, |
| 365 | jint row, jint column) { |
| 366 | CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); |
| 367 | LOG_WINDOW("Getting double for %d,%d from %p", row, column, window); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 369 | CursorWindow::FieldSlot* fieldSlot = window->getFieldSlot(row, column); |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 370 | if (!fieldSlot) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | throwExceptionWithRowCol(env, row, column); |
| 372 | return 0.0; |
| 373 | } |
| 374 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 375 | int32_t type = window->getFieldSlotType(fieldSlot); |
| 376 | if (type == CursorWindow::FIELD_TYPE_FLOAT) { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 377 | return window->getFieldSlotValueDouble(fieldSlot); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 378 | } else if (type == CursorWindow::FIELD_TYPE_STRING) { |
| 379 | size_t sizeIncludingNull; |
| 380 | const char* value = window->getFieldSlotValueString(fieldSlot, &sizeIncludingNull); |
| 381 | return sizeIncludingNull > 1 ? strtod(value, NULL) : 0.0; |
| 382 | } else if (type == CursorWindow::FIELD_TYPE_INTEGER) { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 383 | return jdouble(window->getFieldSlotValueLong(fieldSlot)); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 384 | } else if (type == CursorWindow::FIELD_TYPE_NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | return 0.0; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 386 | } else if (type == CursorWindow::FIELD_TYPE_BLOB) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | throw_sqlite3_exception(env, "Unable to convert BLOB to double"); |
| 388 | return 0.0; |
| 389 | } else { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 390 | throwUnknownTypeException(env, type); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | return 0.0; |
| 392 | } |
| 393 | } |
| 394 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 395 | static jboolean nativePutBlob(JNIEnv* env, jclass clazz, jint windowPtr, |
| 396 | jbyteArray valueObj, jint row, jint column) { |
| 397 | CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 398 | jsize len = env->GetArrayLength(valueObj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 400 | void* value = env->GetPrimitiveArrayCritical(valueObj, NULL); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 401 | status_t status = window->putBlob(row, column, value, len); |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 402 | env->ReleasePrimitiveArrayCritical(valueObj, value, JNI_ABORT); |
| 403 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 404 | if (status) { |
| 405 | LOG_WINDOW("Failed to put blob. error=%d", status); |
| 406 | return false; |
| 407 | } |
| 408 | |
| 409 | LOG_WINDOW("%d,%d is BLOB with %u bytes", row, column, len); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | return true; |
| 411 | } |
| 412 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 413 | static jboolean nativePutString(JNIEnv* env, jclass clazz, jint windowPtr, |
| 414 | jstring valueObj, jint row, jint column) { |
| 415 | CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 417 | size_t sizeIncludingNull = env->GetStringUTFLength(valueObj) + 1; |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 418 | const char* valueStr = env->GetStringUTFChars(valueObj, NULL); |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 419 | if (!valueStr) { |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 420 | LOG_WINDOW("value can't be transferred to UTFChars"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | return false; |
| 422 | } |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 423 | status_t status = window->putString(row, column, valueStr, sizeIncludingNull); |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 424 | env->ReleaseStringUTFChars(valueObj, valueStr); |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 425 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 426 | if (status) { |
| 427 | LOG_WINDOW("Failed to put string. error=%d", status); |
| 428 | return false; |
| 429 | } |
| 430 | |
| 431 | LOG_WINDOW("%d,%d is TEXT with %u bytes", row, column, sizeIncludingNull); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | return true; |
| 433 | } |
| 434 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 435 | static jboolean nativePutLong(JNIEnv* env, jclass clazz, jint windowPtr, |
| 436 | jlong value, jint row, jint column) { |
| 437 | CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 438 | status_t status = window->putLong(row, column, value); |
| 439 | |
| 440 | if (status) { |
| 441 | LOG_WINDOW("Failed to put long. error=%d", status); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | return false; |
| 443 | } |
| 444 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 445 | LOG_WINDOW("%d,%d is INTEGER 0x%016llx", row, column, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 | return true; |
| 447 | } |
| 448 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 449 | static jboolean nativePutDouble(JNIEnv* env, jclass clazz, jint windowPtr, |
| 450 | jdouble value, jint row, jint column) { |
| 451 | CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 452 | status_t status = window->putDouble(row, column, value); |
| 453 | |
| 454 | if (status) { |
| 455 | LOG_WINDOW("Failed to put double. error=%d", status); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | return false; |
| 457 | } |
| 458 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 459 | LOG_WINDOW("%d,%d is FLOAT %lf", row, column, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | return true; |
| 461 | } |
| 462 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 463 | static jboolean nativePutNull(JNIEnv* env, jclass clazz, jint windowPtr, |
| 464 | jint row, jint column) { |
| 465 | CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 466 | status_t status = window->putNull(row, column); |
| 467 | |
| 468 | if (status) { |
| 469 | LOG_WINDOW("Failed to put null. error=%d", status); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | return false; |
| 471 | } |
| 472 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 473 | LOG_WINDOW("%d,%d is NULL", row, column); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | return true; |
| 475 | } |
| 476 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | static JNINativeMethod sMethods[] = |
| 478 | { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 479 | /* name, signature, funcPtr */ |
Jeff Brown | 5e5d6d8 | 2011-10-12 15:41:34 -0700 | [diff] [blame] | 480 | { "nativeCreate", "(Ljava/lang/String;I)I", |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 481 | (void*)nativeCreate }, |
| 482 | { "nativeCreateFromParcel", "(Landroid/os/Parcel;)I", |
| 483 | (void*)nativeCreateFromParcel }, |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 484 | { "nativeDispose", "(I)V", |
| 485 | (void*)nativeDispose }, |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 486 | { "nativeWriteToParcel", "(ILandroid/os/Parcel;)V", |
| 487 | (void*)nativeWriteToParcel }, |
Jeff Brown | 650de3d | 2011-10-27 14:52:28 -0700 | [diff] [blame] | 488 | { "nativeGetName", "(I)Ljava/lang/String;", |
| 489 | (void*)nativeGetName }, |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 490 | { "nativeClear", "(I)V", |
| 491 | (void*)nativeClear }, |
| 492 | { "nativeGetNumRows", "(I)I", |
| 493 | (void*)nativeGetNumRows }, |
| 494 | { "nativeSetNumColumns", "(II)Z", |
| 495 | (void*)nativeSetNumColumns }, |
| 496 | { "nativeAllocRow", "(I)Z", |
| 497 | (void*)nativeAllocRow }, |
| 498 | { "nativeFreeLastRow", "(I)V", |
| 499 | (void*)nativeFreeLastRow }, |
| 500 | { "nativeGetType", "(III)I", |
| 501 | (void*)nativeGetType }, |
| 502 | { "nativeGetBlob", "(III)[B", |
| 503 | (void*)nativeGetBlob }, |
| 504 | { "nativeGetString", "(III)Ljava/lang/String;", |
| 505 | (void*)nativeGetString }, |
| 506 | { "nativeGetLong", "(III)J", |
| 507 | (void*)nativeGetLong }, |
| 508 | { "nativeGetDouble", "(III)D", |
| 509 | (void*)nativeGetDouble }, |
| 510 | { "nativeCopyStringToBuffer", "(IIILandroid/database/CharArrayBuffer;)V", |
| 511 | (void*)nativeCopyStringToBuffer }, |
| 512 | { "nativePutBlob", "(I[BII)Z", |
| 513 | (void*)nativePutBlob }, |
| 514 | { "nativePutString", "(ILjava/lang/String;II)Z", |
| 515 | (void*)nativePutString }, |
| 516 | { "nativePutLong", "(IJII)Z", |
| 517 | (void*)nativePutLong }, |
| 518 | { "nativePutDouble", "(IDII)Z", |
| 519 | (void*)nativePutDouble }, |
| 520 | { "nativePutNull", "(III)Z", |
| 521 | (void*)nativePutNull }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 522 | }; |
| 523 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 524 | #define FIND_CLASS(var, className) \ |
| 525 | var = env->FindClass(className); \ |
| 526 | LOG_FATAL_IF(! var, "Unable to find class " className); |
| 527 | |
| 528 | #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \ |
| 529 | var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \ |
| 530 | LOG_FATAL_IF(! var, "Unable to find field " fieldName); |
| 531 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 532 | int register_android_database_CursorWindow(JNIEnv * env) |
| 533 | { |
| 534 | jclass clazz; |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 535 | FIND_CLASS(clazz, "android/database/CharArrayBuffer"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 537 | GET_FIELD_ID(gCharArrayBufferClassInfo.data, clazz, |
| 538 | "data", "[C"); |
| 539 | GET_FIELD_ID(gCharArrayBufferClassInfo.sizeCopied, clazz, |
| 540 | "sizeCopied", "I"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 542 | gEmptyString = jstring(env->NewGlobalRef(env->NewStringUTF(""))); |
| 543 | LOG_FATAL_IF(!gEmptyString, "Unable to create empty string"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 544 | |
| 545 | return AndroidRuntime::registerNativeMethods(env, "android/database/CursorWindow", |
| 546 | sMethods, NELEM(sMethods)); |
| 547 | } |
| 548 | |
| 549 | } // namespace android |