1/*
2 * Copyright (C) 2010 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
33#if ENABLE(DATABASE)
34
35#include "V8SQLTransactionSync.h"
36
37#include "DatabaseSync.h"
38#include "ExceptionCode.h"
39#include "SQLResultSet.h"
40#include "SQLValue.h"
41#include "V8Binding.h"
42#include "V8BindingMacros.h"
43#include "V8Proxy.h"
44#include "V8SQLResultSet.h"
45#include <wtf/Vector.h>
46
47using namespace WTF;
48
49namespace WebCore {
50
51v8::Handle<v8::Value> V8SQLTransactionSync::executeSqlCallback(const v8::Arguments& args)
52{
53    INC_STATS("DOM.SQLTransactionSync.executeSql()");
54
55    if (!args.Length())
56        return throwError(SYNTAX_ERR);
57
58    STRING_TO_V8PARAMETER_EXCEPTION_BLOCK(V8Parameter<>, statement, args[0]);
59
60    Vector<SQLValue> sqlValues;
61
62    if (args.Length() > 1 && !isUndefinedOrNull(args[1])) {
63        if (!args[1]->IsObject())
64            return throwError(TYPE_MISMATCH_ERR);
65
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")));
69
70        if (isUndefinedOrNull(length))
71            sqlArgsLength = sqlArgsObject->GetPropertyNames()->Length();
72        else
73            sqlArgsLength = length->Uint32Value();
74
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));
78
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));
84            } else {
85                STRING_TO_V8PARAMETER_EXCEPTION_BLOCK(V8Parameter<>, sqlValue, value);
86                sqlValues.append(SQLValue(sqlValue));
87            }
88        }
89    }
90
91    SQLTransactionSync* transaction = V8SQLTransactionSync::toNative(args.Holder());
92
93    ExceptionCode ec = 0;
94    v8::Handle<v8::Value> result = toV8(transaction->executeSQL(statement, sqlValues, ec));
95    V8Proxy::setDOMException(ec);
96
97    return result;
98}
99
100} // namespace WebCore
101
102#endif
103