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.
35 #include "V8SQLTransaction.h"
38 #include "ExceptionCode.h"
40 #include "V8Binding.h"
41 #include "V8BindingMacros.h"
42 #include "V8SQLStatementCallback.h"
43 #include "V8SQLStatementErrorCallback.h"
45 #include <wtf/Vector.h>
51 v8::Handle<v8::Value> V8SQLTransaction::executeSqlCallback(const v8::Arguments& args)
53 INC_STATS("DOM.SQLTransaction.executeSql()");
55 if (args.Length() == 0)
56 return throwError(SYNTAX_ERR);
58 STRING_TO_V8PARAMETER_EXCEPTION_BLOCK(V8Parameter<>, statement, args[0]);
60 Vector<SQLValue> sqlValues;
62 if (args.Length() > 1 && !isUndefinedOrNull(args[1])) {
63 if (!args[1]->IsObject())
64 return throwError(TYPE_MISMATCH_ERR);
66 uint32_t sqlArgsLength = 0;
67 v8::Local<v8::Object> sqlArgsObject = args[1]->ToObject();
68 EXCEPTION_BLOCK(v8::Local<v8::Value>, length, sqlArgsObject->Get(v8::String::New("length")));
70 if (isUndefinedOrNull(length))
71 sqlArgsLength = sqlArgsObject->GetPropertyNames()->Length();
73 sqlArgsLength = length->Uint32Value();
75 for (unsigned int i = 0; i < sqlArgsLength; ++i) {
76 v8::Local<v8::Integer> key = v8::Integer::New(i);
77 EXCEPTION_BLOCK(v8::Local<v8::Value>, value, sqlArgsObject->Get(key));
79 if (value.IsEmpty() || value->IsNull())
80 sqlValues.append(SQLValue());
81 else if (value->IsNumber()) {
82 EXCEPTION_BLOCK(double, sqlValue, value->NumberValue());
83 sqlValues.append(SQLValue(sqlValue));
85 STRING_TO_V8PARAMETER_EXCEPTION_BLOCK(V8Parameter<>, sqlValue, value);
86 sqlValues.append(SQLValue(sqlValue));
91 SQLTransaction* transaction = V8SQLTransaction::toNative(args.Holder());
93 ScriptExecutionContext* scriptExecutionContext = getScriptExecutionContext();
94 if (!scriptExecutionContext)
95 return v8::Undefined();
97 RefPtr<SQLStatementCallback> callback;
98 if (args.Length() > 2 && !isUndefinedOrNull(args[2])) {
99 if (!args[2]->IsObject())
100 return throwError(TYPE_MISMATCH_ERR);
101 callback = V8SQLStatementCallback::create(args[2], scriptExecutionContext);
104 RefPtr<SQLStatementErrorCallback> errorCallback;
105 if (args.Length() > 3 && !isUndefinedOrNull(args[3])) {
106 if (!args[3]->IsObject())
107 return throwError(TYPE_MISMATCH_ERR);
108 errorCallback = V8SQLStatementErrorCallback::create(args[3], scriptExecutionContext);
111 ExceptionCode ec = 0;
112 transaction->executeSQL(statement, sqlValues, callback, errorCallback, ec);
113 V8Proxy::setDOMException(ec);
115 return v8::Undefined();
118 } // namespace WebCore