Home | History | Annotate | Download | only in custom
      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 "ExceptionCode.h"
     39 #include "SQLValue.h"
     40 #include "V8Binding.h"
     41 #include "V8BindingMacros.h"
     42 #include "V8SQLStatementCallback.h"
     43 #include "V8SQLStatementErrorCallback.h"
     44 #include "V8Proxy.h"
     45 #include <wtf/Vector.h>
     46 
     47 using namespace WTF;
     48 
     49 namespace WebCore {
     50 
     51 v8::Handle<v8::Value> V8SQLTransaction::executeSqlCallback(const v8::Arguments& args)
     52 {
     53     INC_STATS("DOM.SQLTransaction.executeSql()");
     54 
     55     if (args.Length() == 0)
     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     SQLTransaction* transaction = V8SQLTransaction::toNative(args.Holder());
     92 
     93     ScriptExecutionContext* scriptExecutionContext = getScriptExecutionContext();
     94     if (!scriptExecutionContext)
     95         return v8::Undefined();
     96 
     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);
    102     }
    103 
    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);
    109     }
    110 
    111     ExceptionCode ec = 0;
    112     transaction->executeSQL(statement, sqlValues, callback, errorCallback, ec);
    113     V8Proxy::setDOMException(ec);
    114 
    115     return v8::Undefined();
    116 }
    117 
    118 } // namespace WebCore
    119 
    120 #endif
    121