Home | History | Annotate | Download | only in custom
      1 /*
      2  * Copyright (C) 2009 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "bindings/modules/v8/V8SQLTransaction.h"
     33 
     34 #include "bindings/modules/v8/V8SQLStatementCallback.h"
     35 #include "bindings/modules/v8/V8SQLStatementErrorCallback.h"
     36 #include "bindings/v8/ExceptionMessages.h"
     37 #include "bindings/v8/ExceptionState.h"
     38 #include "bindings/v8/V8Binding.h"
     39 #include "core/dom/ExceptionCode.h"
     40 #include "modules/webdatabase/sqlite/SQLValue.h"
     41 #include "modules/webdatabase/Database.h"
     42 #include "wtf/Vector.h"
     43 
     44 using namespace WTF;
     45 
     46 namespace WebCore {
     47 
     48 void V8SQLTransaction::executeSqlMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
     49 {
     50     ExceptionState exceptionState(ExceptionState::ExecutionContext, "executeSql", "SQLTransaction", info.Holder(), info.GetIsolate());
     51     if (!info.Length()) {
     52         exceptionState.throwDOMException(SyntaxError, ExceptionMessages::notEnoughArguments(1, 0));
     53         exceptionState.throwIfNeeded();
     54         return;
     55     }
     56 
     57     TOSTRING_VOID(V8StringResource<>, statement, info[0]);
     58 
     59     Vector<SQLValue> sqlValues;
     60 
     61     if (info.Length() > 1 && !isUndefinedOrNull(info[1])) {
     62         if (!info[1]->IsObject()) {
     63             exceptionState.throwDOMException(TypeMismatchError, "The 'arguments' (2nd) argument provided is not an object.");
     64             exceptionState.throwIfNeeded();
     65             return;
     66         }
     67 
     68         uint32_t sqlArgsLength = 0;
     69         v8::Local<v8::Object> sqlArgsObject = info[1]->ToObject();
     70         TONATIVE_VOID(v8::Local<v8::Value>, length, sqlArgsObject->Get(v8AtomicString(info.GetIsolate(), "length")));
     71 
     72         if (isUndefinedOrNull(length))
     73             sqlArgsLength = sqlArgsObject->GetPropertyNames()->Length();
     74         else
     75             sqlArgsLength = length->Uint32Value();
     76 
     77         for (unsigned i = 0; i < sqlArgsLength; ++i) {
     78             v8::Handle<v8::Integer> key = v8::Integer::New(info.GetIsolate(), i);
     79             TONATIVE_VOID(v8::Local<v8::Value>, value, sqlArgsObject->Get(key));
     80 
     81             if (value.IsEmpty() || value->IsNull())
     82                 sqlValues.append(SQLValue());
     83             else if (value->IsNumber()) {
     84                 TONATIVE_VOID(double, sqlValue, value->NumberValue());
     85                 sqlValues.append(SQLValue(sqlValue));
     86             } else {
     87                 TOSTRING_VOID(V8StringResource<>, sqlValue, value);
     88                 sqlValues.append(SQLValue(sqlValue));
     89             }
     90         }
     91     }
     92 
     93     SQLTransaction* transaction = V8SQLTransaction::toNative(info.Holder());
     94     OwnPtr<SQLStatementCallback> callback;
     95     if (info.Length() > 2 && !isUndefinedOrNull(info[2])) {
     96         if (!info[2]->IsFunction()) {
     97             exceptionState.throwDOMException(TypeMismatchError, "The 'callback' (2nd) argument provided is not a function.");
     98             exceptionState.throwIfNeeded();
     99             return;
    100         }
    101         callback = V8SQLStatementCallback::create(v8::Handle<v8::Function>::Cast(info[2]), ScriptState::current(info.GetIsolate()));
    102     }
    103 
    104     OwnPtr<SQLStatementErrorCallback> errorCallback;
    105     if (info.Length() > 3 && !isUndefinedOrNull(info[3])) {
    106         if (!info[3]->IsFunction()) {
    107             exceptionState.throwDOMException(TypeMismatchError, "The 'errorCallback' (3rd) argument provided is not a function.");
    108             exceptionState.throwIfNeeded();
    109             return;
    110         }
    111         errorCallback = V8SQLStatementErrorCallback::create(v8::Handle<v8::Function>::Cast(info[3]), ScriptState::current(info.GetIsolate()));
    112     }
    113 
    114     transaction->executeSQL(statement, sqlValues, callback.release(), errorCallback.release(), exceptionState);
    115     exceptionState.throwIfNeeded();
    116 }
    117 
    118 } // namespace WebCore
    119