| LEFT | RIGHT |
| 1 // Copyright 2008, Google Inc. | 1 // Copyright 2008, Google Inc. |
| 2 // | 2 // |
| 3 // Redistribution and use in source and binary forms, with or without | 3 // Redistribution and use in source and binary forms, with or without |
| 4 // modification, are permitted provided that the following conditions are met: | 4 // modification, are permitted provided that the following conditions are met: |
| 5 // | 5 // |
| 6 // 1. Redistributions of source code must retain the above copyright notice, | 6 // 1. Redistributions of source code must retain the above copyright notice, |
| 7 // this list of conditions and the following disclaimer. | 7 // this list of conditions and the following disclaimer. |
| 8 // 2. Redistributions in binary form must reproduce the above copyright notice, | 8 // 2. Redistributions in binary form must reproduce the above copyright notice, |
| 9 // this list of conditions and the following disclaimer in the documentation | 9 // this list of conditions and the following disclaimer in the documentation |
| 10 // and/or other materials provided with the distribution. | 10 // and/or other materials provided with the distribution. |
| 11 // 3. Neither the name of Google Inc. nor the names of its contributors may be | 11 // 3. Neither the name of Google Inc. nor the names of its contributors may be |
| 12 // used to endorse or promote products derived from this software without | 12 // used to endorse or promote products derived from this software without |
| 13 // specific prior written permission. | 13 // specific prior written permission. |
| 14 // | 14 // |
| 15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | 15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | 16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | 17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | 20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | 21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | 22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | 23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | 25 |
| 26 #include "gears/database2/statement.h" | 26 #include "gears/database2/statement.h" |
| 27 | 27 |
| 28 #include "gears/database2/transaction.h" | 28 #include "gears/database2/transaction.h" |
| 29 | 29 |
| 30 void Database2Statement::InvokeCallback(Database2Transaction *tx) { | 30 void Database2Statement::InvokeCallback(Database2Transaction *tx) { |
| 31 // for now, just return the Database2Statement | 31 // for now, just return the Database2Statement |
| 32 JsParamToSend send_argv[] = { | 32 JsParamToSend send_argv[] = { |
| 33 { JSPARAM_STRING16, &sql_statement_ } | 33 { JSPARAM_STRING16, &sql_statement_ } |
| 34 }; | 34 }; |
| 35 | 35 |
| 36 if (HasCallback()) { | 36 if (HasCallback()) { |
| 37 tx->GetJsRunner()->InvokeCallback(callback_.get(), ARRAYSIZE(send_argv), | 37 tx->GetJsRunner()->InvokeCallback(callback_.get(), ARRAYSIZE(send_argv), |
| 38 send_argv, NULL); | 38 send_argv, NULL); |
| 39 } | 39 } |
| 40 } | 40 } |
| 41 | 41 |
| 42 void Database2Statement::InvokeErrorCallback(Database2Transaction *tx, | 42 void Database2Statement::InvokeErrorCallback(Database2Transaction *tx, |
| 43 JsObject *error) { | 43 JsObject *error) { |
| 44 } | 44 } |
| 45 | 45 |
| 46 bool Database2Statement::Create(const std::string16 &sql_statement, | 46 bool Database2Statement::Create(const std::string16 &sql_statement, |
| 47 JsArray *sql_arguments, | 47 JsArray *sql_arguments, |
| 48 JsRootedCallback *callback, | 48 JsRootedCallback *callback, |
| 49 JsRootedCallback *error_callback, | 49 JsRootedCallback *error_callback, |
| 50 Database2Statement **instance) { | 50 Database2Statement **instance) { |
| 51 assert(instance && *instance); |
| 52 |
| 51 scoped_ptr<Database2Statement> statement(new Database2Statement()); | 53 scoped_ptr<Database2Statement> statement(new Database2Statement()); |
| 52 | 54 |
| 53 // NULL should be passed if no arguments are specified | 55 // NULL should be passed if no arguments are specified |
| 54 assert(!sql_arguments || !JsTokenIsNullOrUndefined(sql_arguments->token())); | 56 assert(!sql_arguments || !JsTokenIsNullOrUndefined(sql_arguments->token())); |
| 55 // NULL should be passed if a callback is not specified | 57 // NULL should be passed if a callback is not specified |
| 56 assert(!callback || !JsTokenIsNullOrUndefined(callback->token())); | 58 assert(!callback || !JsTokenIsNullOrUndefined(callback->token())); |
| 57 assert(!error_callback || !JsTokenIsNullOrUndefined(error_callback->token())); | 59 assert(!error_callback || !JsTokenIsNullOrUndefined(error_callback->token())); |
| 58 | 60 |
| 59 statement->sql_statement_.assign(sql_statement); | 61 statement->sql_statement_.assign(sql_statement); |
| 60 statement->callback_.reset(callback); | 62 statement->callback_.reset(callback); |
| 61 statement->error_callback_.reset(error_callback); | 63 statement->error_callback_.reset(error_callback); |
| 62 | 64 |
| 63 Database2Values *arguments; | 65 Database2Values *arguments; |
| 64 if (!Database2Values::CreateFromJsArray(sql_arguments, &arguments)) { | 66 if (!Database2Values::CreateFromJsArray(sql_arguments, &arguments)) { |
| 65 return false; | 67 return false; |
| 66 } | 68 } |
| 67 | 69 |
| 68 statement->arguments_.reset(arguments); | 70 statement->arguments_.reset(arguments); |
| 69 | 71 |
| 70 *instance = statement.release(); | 72 *instance = statement.release(); |
| 71 return true; | 73 return true; |
| 72 } | 74 } |
| 73 | 75 |
| 74 // static | 76 // static |
| 75 bool Database2Values::CreateFromJsArray(const JsArray *sql_arguments, | 77 bool Database2Values::CreateFromJsArray(const JsArray *js_array, |
| 76 Database2Values **instance) { | 78 Database2Values **instance) { |
| 77 scoped_ptr<Database2Values> result; | 79 assert(instance && *instance); |
| 78 result.reset(new Database2Values()); | 80 |
| 81 scoped_ptr<Database2Values> result(new Database2Values()); |
| 79 | 82 |
| 80 // since the arguments are optional, they could be passed as NULL | 83 // since the arguments are optional, they could be passed as NULL |
| 81 if (sql_arguments == NULL) { | 84 if (js_array == NULL) { |
| 82 result->arguments_.reset(NULL); | |
| 83 result->length_ = 0; | 85 result->length_ = 0; |
| 84 *instance = result.release(); | 86 *instance = result.release(); |
| 85 return true; | 87 return true; |
| 86 } | 88 } |
| 87 | 89 |
| 88 int len; | 90 int len; |
| 89 if (!sql_arguments->GetLength(&len)) { | 91 if (!js_array->GetLength(&len)) { |
| 90 // unable to query JsArray, someting's gone horribly wrong | 92 // unable to query JsArray, someting's gone horribly wrong |
| 91 // returning with failure will trigger an internal error | 93 // returning with failure will trigger an internal error |
| 92 assert(false); | 94 assert(false); |
| 93 return false; | 95 return false; |
| 94 } | 96 } |
| 95 | 97 |
| 96 result->length_ = len; | 98 result->length_ = len; |
| 97 | 99 // a JsArray produces one row of values |
| 98 result->arguments_.reset(new JsParamToSend[len]); | 100 result->StartNewRow(); |
| 101 Variant *row = result->rows_.back(); |
| 99 for(int i = 0; i < len; i++) { | 102 for(int i = 0; i < len; i++) { |
| 100 if (!SetJsParamToSend(sql_arguments, i, result->arguments_.get())) { | 103 switch(js_array->GetElementType(i)) { |
| 101 // one invalid argument make the whole statement bogus | 104 case JSPARAM_INT: { |
| 102 // no need to process any more parameters | 105 int value; |
| 103 // set length to only include last processed parameter | 106 if (!js_array->GetElementAsInt(i, &value)) { |
| 104 result->length_ = i; | 107 return false; |
| 105 return false; | 108 } |
| 109 row[i].type = JSPARAM_INT; |
| 110 row[i].int_value = value; |
| 111 break; |
| 112 } |
| 113 case JSPARAM_DOUBLE: { |
| 114 double value; |
| 115 if (!js_array->GetElementAsDouble(i, &value)) { |
| 116 return false; |
| 117 } |
| 118 row[i].type = JSPARAM_DOUBLE; |
| 119 row[i].double_value = value; |
| 120 break; |
| 121 } |
| 122 case JSPARAM_STRING16: { |
| 123 std::string16 value; |
| 124 if (!js_array->GetElementAsString(i, &value)) { |
| 125 return false; |
| 126 } |
| 127 row[i].type = JSPARAM_STRING16; |
| 128 row[i].string_value = new std::string16(value); |
| 129 break; |
| 130 } |
| 131 case JSPARAM_NULL: { |
| 132 // Variant's type is set to JSPARAM_NULL by default, no need to do |
| 133 // anything here |
| 134 break; |
| 135 } |
| 136 default: { |
| 137 // invalid type |
| 138 return false; |
| 139 } |
| 106 } | 140 } |
| 107 } | 141 } |
| 108 | 142 |
| 109 *instance = result.release(); | 143 *instance = result.release(); |
| 110 return true; | 144 return true; |
| 111 } | 145 } |
| 112 | 146 |
| 113 // static | 147 void Database2Values::StartNewRow() { |
| 114 bool Database2Values::SetJsParamToSend(const JsArray *js_array, | 148 rows_.push_back(new Variant[length_]); |
| 115 int index, | 149 } |
| 116 JsParamToSend *arguments) { | 150 |
| 117 assert(index >= 0 && arguments); | 151 bool Database2Values::Next() { |
| 118 JsParamToSend *param = &arguments[index]; | 152 return ++current_row_ >= length_; |
| 119 param->type = js_array->GetElementType(index); | |
| 120 switch(param->type) { | |
| 121 case JSPARAM_INT: { | |
| 122 scoped_ptr<int> value; | |
| 123 value.reset(new int); | |
| 124 if (js_array->GetElementAsInt(index, value.get())) { | |
| 125 param->value_ptr = value.release(); | |
| 126 return true; | |
| 127 } | |
| 128 return false; | |
| 129 } | |
| 130 case JSPARAM_DOUBLE: { | |
| 131 scoped_ptr<double> value; | |
| 132 value.reset(new double); | |
| 133 if (js_array->GetElementAsDouble(index, value.get())) { | |
| 134 param->value_ptr = value.release(); | |
| 135 return true; | |
| 136 } | |
| 137 return false; | |
| 138 } | |
| 139 case JSPARAM_STRING16: { | |
| 140 scoped_ptr<std::string16> value; | |
| 141 value.reset(new std::string16()); | |
| 142 if (js_array->GetElementAsString(index, value.get())) { | |
| 143 param->value_ptr = value.release(); | |
| 144 return true; | |
| 145 } | |
| 146 return false; | |
| 147 } | |
| 148 case JSPARAM_NULL: { | |
| 149 param->value_ptr = NULL; | |
| 150 return true; | |
| 151 } | |
| 152 } | |
| 153 // all other types are considered invalid | |
| 154 return false; | |
| 155 } | 153 } |
| 156 | 154 |
| 157 JsParamType Database2Values::GetType(int index) const { | 155 JsParamType Database2Values::GetType(int index) const { |
| 158 assert(index >= 0 && index < length_); | 156 return current(index).type; |
| 159 return arguments_[index].type; | |
| 160 } | 157 } |
| 161 | 158 |
| 162 int Database2Values::GetAsInt(int index) const { | 159 bool Database2Values::GetElementAsInt(int index, int *value) { |
| 163 assert(index >= 0 && index < length_); | 160 assert(current(index).type == JSPARAM_INT); |
| 164 JsParamToSend param = arguments_[index]; | 161 assert(value); |
| 165 assert(param.type == JSPARAM_INT); | 162 *value = current(index).int_value; |
| 166 return *static_cast<int*>(const_cast<void*>(param.value_ptr)); | 163 return true; |
| 167 } | 164 } |
| 168 | 165 |
| 169 double Database2Values::GetAsDouble(int index) const { | 166 bool Database2Values::GetElementAsDouble(int index, double *value) { |
| 170 assert(index >= 0 && index < length_); | 167 assert(current(index).type = JSPARAM_DOUBLE); |
| 171 JsParamToSend param = arguments_[index]; | 168 assert(value); |
| 172 assert(param.type == JSPARAM_DOUBLE); | 169 *value = current(index).double_value; |
| 173 return *static_cast<int*>(const_cast<void*>(param.value_ptr)); | 170 return true; |
| 174 } | 171 } |
| 175 | 172 |
| 176 std::string16 &Database2Values::GetAsString(int index) const { | 173 bool Database2Values::GetElementAsString(int index, std::string16 *value) { |
| 177 assert(index >= 0 && index < length_); | 174 assert(current(index).type = JSPARAM_STRING16); |
| 178 JsParamToSend param = arguments_[index]; | 175 assert(value); |
| 179 assert(param.type == JSPARAM_STRING16); | 176 *value = *(current(index).string_value); |
| 180 return *static_cast<std::string16*>(const_cast<void*>(param.value_ptr)); | 177 return true; |
| 181 } | 178 } |
| 182 | |
| 183 Database2Values::~Database2Values() { | |
| 184 // dispose of the data, pointed to the value_ptrs | |
| 185 for(int i = 0; i < length_; i++) { | |
| 186 JsParamToSend param = arguments_[i]; | |
| 187 if (param.value_ptr) delete param.value_ptr; | |
| 188 } | |
| 189 } | |
| LEFT | RIGHT |