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