static bool _setBindArgs(JNIEnv * env, sqlite3_stmt * stmt, jobjectArray bindArgs)
{
    // Nothing to do if we don't have any bind args.
    if (!bindArgs) {
        return true;
    }

    // Value Methods
    static jmethodID intValueMethod = 0;
    static jmethodID longValueMethod = 0;
    static jmethodID booleanValueMethod = 0;

    if (!intValueMethod) {
        intValueMethod = (*env)->GetMethodID(env, IntegerClass, "intValue", "()I");
    }

    if (!longValueMethod) {
        longValueMethod = (*env)->GetMethodID(env, LongClass, "longValue", "()J");
    }

    if (!booleanValueMethod) {
        booleanValueMethod = (*env)->GetMethodID(env, BooleanClass, "booleanValue", "()Z");
    }

    int length = (*env)->GetArrayLength(env, bindArgs);
    int i;
    for(i=0; i<length; i++)
    {
        jobject bindArg = (*env)->GetObjectArrayElement(env, bindArgs, i);
        if (!bindArg) {
            sqlite3_bind_null(stmt, i+1);
        } else {
            jclass bindArgClass = (*env)->GetObjectClass(env, bindArg);
            if((*env)->IsSameObject(env, bindArgClass, StringClass) == JNI_TRUE)
            {
                const char * chars = (*env)->GetStringUTFChars(env, bindArg, 0);
                int status = sqlite3_bind_text(stmt, i+1, chars, -1, SQLITE_TRANSIENT);
                (*env)->ReleaseStringUTFChars(env, bindArg, chars);

                if (status != SQLITE_OK) {
                    log_e(env, "BindArgs: Error (%d) binding string arg: %d", status, i+1);
                    _throwException(env, "BindArgs: Error (%d) binding string arg: %d", status, i+1);

                    return false;
                }
            } else if((*env)->IsSameObject(env, bindArgClass, IntegerClass) == JNI_TRUE) {
                jint value = (*env)->CallIntMethod(env, bindArg, intValueMethod);

                int status = sqlite3_bind_int(stmt, i+1, value);

                if (status != SQLITE_OK) {
                    log_e(env, "BindArgs: Error (%d) binding int arg: %d", status, i+1);
                    _throwException(env, "BindArgs: Error (%d) binding int arg: %d", status, i+1);

                    return false;
                }
            } else if((*env)->IsSameObject(env, bindArgClass, LongClass) == JNI_TRUE) {
                jlong value = (*env)->CallLongMethod(env, bindArg, longValueMethod);
                int status = sqlite3_bind_int64(stmt, i+1, value);

                if (status != SQLITE_OK) {
                    log_e(env, "BindArgs: Error (%d) binding long arg: %d", status, i+1);
                    _throwException(env, "BindArgs: Error (%d) binding long arg: %d", status, i+1);

                    return false;
                }
            } else if((*env)->IsSameObject(env, bindArgClass, BooleanClass) == JNI_TRUE) {
                jboolean value = (*env)->CallBooleanMethod(env, bindArg, booleanValueMethod);

                int status = sqlite3_bind_int(stmt, i+1, (value == true ? 1 : 0));

                if (status != SQLITE_OK) {
                    log_e(env, "BindArgs: Error (%d) binding boolean arg: %d", status, i+1);
                    _throwException(env, "BindArgs: Error (%d) binding boolean arg: %d", status, i+1);

                    return false;
                }
            } else if((*env)->IsSameObject(env, bindArgClass, ByteArrayClass) == JNI_TRUE) {
                jbyteArray byteArray = (jbyteArray)bindArg;
                jsize length = (*env)->GetArrayLength(env, byteArray);
                void * bytes = (*env)->GetPrimitiveArrayCritical(env, byteArray, 0);
                int status = sqlite3_bind_blob(stmt, i+1, bytes, length, SQLITE_TRANSIENT);
                (*env)->ReleasePrimitiveArrayCritical(env, byteArray, bytes, JNI_ABORT);

                if (status != SQLITE_OK) {
                    log_e(env, "BindArgs: Error (%d) binding blob arg: %d", status, i+1);
                    _throwException(env, "BindArgs: Error (%d) binding blob arg: %d", status, i+1);

                    return false;
                }
            } else {
                log_e(env, "BindArgs: Error binding arg %d.  Unsupported type", i+1);
            }
        }
    }

    return true;
}
Beispiel #2
0
inline void _throwException(const std::string &file, int line,\
                            int errorCode, ErrorType::ErrorType type = ErrorType::DEFAULT) {
    _throwException(file, line, errorCode, getMessage(errorCode, type), type);
}