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