Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / custom / V8SQLTransactionCustom.cpp
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 "V8SQLTransaction.h"
33
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"
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     V8TRYCATCH_FOR_V8STRINGRESOURCE_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         V8TRYCATCH_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 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));
80
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));
86             } else {
87                 V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<>, sqlValue, value);
88                 sqlValues.append(SQLValue(sqlValue));
89             }
90         }
91     }
92
93     SQLTransaction* transaction = V8SQLTransaction::toNative(info.Holder());
94
95     ExecutionContext* executionContext = currentExecutionContext(info.GetIsolate());
96
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();
102             return;
103         }
104         callback = V8SQLStatementCallback::create(v8::Handle<v8::Function>::Cast(info[2]), executionContext);
105     }
106
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();
112             return;
113         }
114         errorCallback = V8SQLStatementErrorCallback::create(v8::Handle<v8::Function>::Cast(info[3]), executionContext);
115     }
116
117     transaction->executeSQL(statement, sqlValues, callback.release(), errorCallback.release(), exceptionState);
118     exceptionState.throwIfNeeded();
119 }
120
121 } // namespace WebCore