• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
33 #if ENABLE(DATABASE)
34 
35 #include "Database.h"
36 #include "SQLValue.h"
37 #include "V8Binding.h"
38 #include "V8CustomBinding.h"
39 #include "V8CustomSQLStatementCallback.h"
40 #include "V8CustomSQLStatementErrorCallback.h"
41 #include "V8Proxy.h"
42 #include <wtf/Vector.h>
43 
44 using namespace WTF;
45 
46 namespace WebCore {
47 
CALLBACK_FUNC_DECL(SQLTransactionExecuteSql)48 CALLBACK_FUNC_DECL(SQLTransactionExecuteSql)
49 {
50     INC_STATS("DOM.SQLTransaction.executeSql()");
51 
52     if (args.Length() == 0)
53         return throwError("SQL statement is required.", V8Proxy::SyntaxError);
54 
55     String statement = toWebCoreString(args[0]);
56 
57     Vector<SQLValue> sqlValues;
58 
59     if (args.Length() > 1 && !isUndefinedOrNull(args[1])) {
60         if (args[1]->IsObject()) {
61             uint32_t sqlArgsLength = 0;
62             v8::Local<v8::Object> sqlArgsObject = args[1]->ToObject();
63             v8::Local<v8::Value> lengthGetter;
64             {
65                 v8::TryCatch block;
66                 lengthGetter = sqlArgsObject->Get(v8::String::New("length"));
67                 if (block.HasCaught())
68                     return throwError(block.Exception());
69             }
70 
71             if (isUndefinedOrNull(lengthGetter))
72                 sqlArgsLength = sqlArgsObject->GetPropertyNames()->Length();
73             else
74                 sqlArgsLength = lengthGetter->Uint32Value();
75 
76             for (unsigned int i = 0; i < sqlArgsLength; ++i) {
77                 v8::Local<v8::Integer> key = v8::Integer::New(i);
78                 v8::Local<v8::Value> value;
79                 {
80                     v8::TryCatch block;
81                     value = sqlArgsObject->Get(key);
82                     if (block.HasCaught())
83                         return throwError(block.Exception());
84                 }
85 
86                 if (value.IsEmpty() || value->IsNull())
87                     sqlValues.append(SQLValue());
88                 else if (value->IsNumber())
89                     sqlValues.append(SQLValue(value->NumberValue()));
90                 else
91                     sqlValues.append(SQLValue(toWebCoreString(value)));
92             }
93         } else
94             return throwError("sqlArgs should be array or object!", V8Proxy::TypeError);
95     }
96 
97     SQLTransaction* transaction = V8DOMWrapper::convertToNativeObject<SQLTransaction>(V8ClassIndex::SQLTRANSACTION, args.Holder());
98 
99     Frame* frame = V8Proxy::retrieveFrameForCurrentContext();
100 
101     RefPtr<SQLStatementCallback> callback;
102     if (args.Length() > 2 && !isUndefinedOrNull(args[2])) {
103         if (!args[2]->IsObject())
104             return throwError("Statement callback must be of valid type.", V8Proxy::TypeError);
105 
106         if (frame)
107             callback = V8CustomSQLStatementCallback::create(args[2], frame);
108     }
109 
110     RefPtr<SQLStatementErrorCallback> errorCallback;
111     if (args.Length() > 3 && !isUndefinedOrNull(args[3])) {
112         if (!args[3]->IsObject())
113             return throwError("Statement error callback must be of valid type.", V8Proxy::TypeError);
114 
115         if (frame)
116             errorCallback = V8CustomSQLStatementErrorCallback::create(args[3], frame);
117     }
118 
119     ExceptionCode ec = 0;
120     transaction->executeSQL(statement, sqlValues, callback, errorCallback, ec);
121     V8Proxy::setDOMException(ec);
122 
123     return v8::Undefined();
124 }
125 
126 } // namespace WebCore
127 
128 #endif
129 
130