2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "V8SQLTransaction.h"
34 #include "V8SQLStatementCallback.h"
35 #include "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"
48 void V8SQLTransaction::executeSqlMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
50 ExceptionState exceptionState(ExceptionState::ExecutionContext, "executeSql", "SQLTransaction", info.Holder(), info.GetIsolate());
52 exceptionState.throwDOMException(SyntaxError, ExceptionMessages::notEnoughArguments(1, 0));
53 exceptionState.throwIfNeeded();
57 V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<>, statement, info[0]);
59 Vector<SQLValue> sqlValues;
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();
68 uint32_t sqlArgsLength = 0;
69 v8::Local<v8::Object> sqlArgsObject = info[1]->ToObject();
70 V8TRYCATCH_VOID(v8::Local<v8::Value>, length, sqlArgsObject->Get(v8AtomicString(info.GetIsolate(), "length")));
72 if (isUndefinedOrNull(length))
73 sqlArgsLength = sqlArgsObject->GetPropertyNames()->Length();
75 sqlArgsLength = length->Uint32Value();
77 for (unsigned int i = 0; i < sqlArgsLength; ++i) {
78 v8::Handle<v8::Integer> key = v8::Integer::New(info.GetIsolate(), i);
79 V8TRYCATCH_VOID(v8::Local<v8::Value>, value, sqlArgsObject->Get(key));
81 if (value.IsEmpty() || value->IsNull())
82 sqlValues.append(SQLValue());
83 else if (value->IsNumber()) {
84 V8TRYCATCH_VOID(double, sqlValue, value->NumberValue());
85 sqlValues.append(SQLValue(sqlValue));
87 V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<>, sqlValue, value);
88 sqlValues.append(SQLValue(sqlValue));
93 SQLTransaction* transaction = V8SQLTransaction::toNative(info.Holder());
95 ExecutionContext* executionContext = currentExecutionContext(info.GetIsolate());
97 OwnPtr<SQLStatementCallback> callback;
98 if (info.Length() > 2 && !isUndefinedOrNull(info[2])) {
99 if (!info[2]->IsFunction()) {
100 exceptionState.throwDOMException(TypeMismatchError, "The 'callback' (2nd) argument provided is not a function.");
101 exceptionState.throwIfNeeded();
104 callback = V8SQLStatementCallback::create(v8::Handle<v8::Function>::Cast(info[2]), executionContext);
107 OwnPtr<SQLStatementErrorCallback> errorCallback;
108 if (info.Length() > 3 && !isUndefinedOrNull(info[3])) {
109 if (!info[3]->IsFunction()) {
110 exceptionState.throwDOMException(TypeMismatchError, "The 'errorCallback' (3rd) argument provided is not a function.");
111 exceptionState.throwIfNeeded();
114 errorCallback = V8SQLStatementErrorCallback::create(v8::Handle<v8::Function>::Cast(info[3]), executionContext);
117 transaction->executeSQL(statement, sqlValues, callback.release(), errorCallback.release(), exceptionState);
118 exceptionState.throwIfNeeded();
121 } // namespace WebCore