| 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 #include "gears/database2/transaction.h" | 28 #include "gears/database2/transaction.h" |
| 28 | 29 |
| 29 void Database2Statement::InvokeCallback(Database2Transaction *tx) { | 30 void Database2Statement::InvokeCallback(Database2Transaction *tx) { |
| 30 // for now, just return the Database2Statement | 31 // for now, just return the Database2Statement |
| 31 JsParamToSend send_argv[] = { | 32 JsParamToSend send_argv[] = { |
| 32 { JSPARAM_STRING16, &sql_statement_ } | 33 { JSPARAM_STRING16, &sql_statement_ } |
| 33 }; | 34 }; |
| 34 | 35 |
| 35 if (HasCallback()) { | 36 if (HasCallback()) { |
| 36 tx->GetJsRunner()->InvokeCallback(callback_.get(), ARRAYSIZE(send_argv), | 37 tx->GetJsRunner()->InvokeCallback(callback_.get(), ARRAYSIZE(send_argv), |
| 37 send_argv, NULL); | 38 send_argv, NULL); |
| 38 } | 39 } |
| 39 } | 40 } |
| 40 | 41 |
| 41 void Database2Statement::InvokeErrorCallback(Database2Transaction *tx, | 42 void Database2Statement::InvokeErrorCallback(Database2Transaction *tx, |
| 42 JsObject *error) { | 43 JsObject *error) { |
| 43 } | 44 } |
| 44 | 45 |
| 45 bool Database2Statement::Create(const std::string16 &sql_statement, | 46 bool Database2Statement::Create(const std::string16 &sql_statement, |
| 46 JsArray *sql_arguments, | 47 const JsArray &sql_arguments, |
| 47 JsRootedCallback *callback, | 48 JsRootedCallback *callback, |
| 48 JsRootedCallback *error_callback, | 49 JsRootedCallback *error_callback, |
| 49 Database2Statement **instance) { | 50 Database2Statement **instance) { |
| 50 scoped_ptr<Database2Statement> statement(new Database2Statement()); | 51 scoped_ptr<Database2Statement> statement; |
| 51 | 52 statement.reset(new Database2Statement()); |
| 52 // NULL should be passed if no arguments are specified | |
| 53 assert(!sql_arguments || !JsTokenIsNullOrUndefined(sql_arguments->token())); | |
| 54 // NULL should be passed if a callback is not specified | |
| 55 assert(!callback || !JsTokenIsNullOrUndefined(callback->token())); | |
| 56 assert(!error_callback || !JsTokenIsNullOrUndefined(error_callback->token())); | |
| 57 | |
| 58 statement->sql_statement_.assign(sql_statement); | 53 statement->sql_statement_.assign(sql_statement); |
| 59 statement->callback_.reset(callback); | 54 statement->callback_.reset(callback); |
| 60 statement->error_callback_.reset(error_callback); | 55 statement->error_callback_.reset(error_callback); |
| 61 | 56 |
| 62 Database2Values *arguments; | 57 Database2Values *arguments; |
| 63 if (!Database2Values::CreateFromJsArray(sql_arguments, &arguments)) { | 58 if (!Database2Values::CreateFromJsArray(sql_arguments, &arguments)) { |
| 64 return false; | 59 return false; |
| 65 } | 60 } |
| 66 | 61 |
| 67 statement->arguments_.reset(arguments); | 62 statement->arguments_.reset(arguments); |
| 68 | 63 |
| 69 *instance = statement.release(); | 64 *instance = statement.release(); |
| 70 return true; | 65 return true; |
| 71 } | 66 } |
| 72 | 67 |
| 73 // static | 68 // static |
| 74 bool Database2Values::CreateFromJsArray(const JsArray *js_array, | 69 bool Database2Values::CreateFromJsArray(const JsArray &sql_arguments, |
| 75 Database2Values **instance) { | 70 Database2Values **instance) { |
| 76 assert(instance && *instance); | 71 scoped_ptr<Database2Values> result; |
| 72 result.reset(new Database2Values()); | |
| 77 | 73 |
| 78 scoped_ptr<Database2Values> result(new Database2Values()); | 74 // since the arguments are optional, their token may be NULL |
| 79 | 75 if (sql_arguments.token() == NULL) { |
|
Aaron
2008/05/11 18:11:21
It's not a good idea to compare token() to NULL. I
Dimitri
2008/05/13 22:52:03
On 2008/05/11 18:11:21, Aaron wrote:
> We have a s
| |
| 80 // since the arguments are optional, they could be passed as NULL | 76 result->arguments_.reset(NULL); |
| 81 if (js_array == NULL) { | |
| 82 result->length_ = 0; | 77 result->length_ = 0; |
| 83 *instance = result.release(); | 78 *instance = result.release(); |
| 84 return true; | 79 return true; |
| 85 } | 80 } |
| 86 | 81 |
| 87 int len; | 82 int len; |
| 88 if (!js_array->GetLength(&len)) { | 83 if (!sql_arguments.GetLength(&len)) { |
| 89 // unable to query JsArray, someting's gone horribly wrong | 84 // unable to query JsArray, someting's gone horribly wrong |
| 90 // returning with failure will trigger an internal error | 85 // returning with failure will trigger an internal error |
|
Aaron
2008/05/11 18:11:21
Silent failures look weird to me. Maybe assert(fal
Dimitri
2008/05/13 22:52:03
On 2008/05/11 18:11:21, Aaron wrote:
> Silent fail
| |
| 91 assert(false); | |
| 92 return false; | 86 return false; |
| 93 } | 87 } |
| 94 | 88 |
| 95 result->length_ = len; | 89 result->length_ = len; |
| 96 // a JsArray produces one row of values | 90 |
| 97 result->StartNewRow(); | 91 result->arguments_.reset(new JsParamToSend[len]); |
| 98 Variant *row = result->rows_.back(); | |
| 99 for(int i = 0; i < len; i++) { | 92 for(int i = 0; i < len; i++) { |
| 100 switch(js_array->GetElementType(i)) { | 93 if (!SetJsParamToSend(sql_arguments, i, result->arguments_.get())) { |
| 101 case JSPARAM_INT: { | 94 // one invalid argument make the whole statement bogus |
| 102 int value; | 95 // no need to process any more parameters |
| 103 if (!js_array->GetElementAsInt(i, &value)) { | 96 // set length to only include last processed parameter |
| 104 return false; | 97 result->length_ = i; |
| 105 } | 98 return false; |
| 106 row[i].type = JSPARAM_INT; | 99 } |
| 107 row[i].int_value = value; | 100 } |
| 108 break; | 101 |
| 102 *instance = result.release(); | |
| 103 return true; | |
| 104 } | |
| 105 | |
| 106 // static | |
| 107 bool Database2Values::SetJsParamToSend(JsArray js_array, | |
| 108 int index, | |
| 109 JsParamToSend *arguments) { | |
| 110 assert(index >= 0 && arguments); | |
| 111 JsParamToSend *param = arguments + index; | |
|
Aaron
2008/05/11 18:11:21
Does arguments[index] work? I think it's easier to
Dimitri
2008/05/13 22:52:03
On 2008/05/11 18:11:21, Aaron wrote:
> Does argume
| |
| 112 param->type = js_array.GetElementType(index); | |
| 113 switch(param->type) { | |
| 114 case JSPARAM_INT: { | |
| 115 scoped_ptr<int> value; | |
| 116 value.reset(new int); | |
|
Aaron
2008/05/11 18:11:21
The heap allocated integers are unfortuante :-/. I
Dimitri
2008/05/13 22:52:03
On 2008/05/11 18:11:21, Aaron wrote:
> The heap al
| |
| 117 if (js_array.GetElementAsInt(index, value.get())) { | |
| 118 param->value_ptr = value.release(); | |
| 119 return true; | |
| 109 } | 120 } |
| 110 case JSPARAM_DOUBLE: { | 121 return false; |
| 111 double value; | 122 } |
| 112 if (!js_array->GetElementAsDouble(i, &value)) { | 123 case JSPARAM_DOUBLE: { |
| 113 return false; | 124 scoped_ptr<double> value; |
| 114 } | 125 value.reset(new double); |
| 115 row[i].type = JSPARAM_DOUBLE; | 126 if (js_array.GetElementAsDouble(index, value.get())) { |
| 116 row[i].double_value = value; | 127 param->value_ptr = value.release(); |
| 117 break; | 128 return true; |
| 118 } | 129 } |
| 119 case JSPARAM_STRING16: { | 130 return false; |
| 120 std::string16 value; | 131 } |
| 121 if (!js_array->GetElementAsString(i, &value)) { | 132 case JSPARAM_STRING16: { |
| 122 return false; | 133 scoped_ptr<std::string16> value; |
| 123 } | 134 value.reset(new std::string16()); |
| 124 row[i].type = JSPARAM_STRING16; | 135 if (js_array.GetElementAsString(index, value.get())) { |
| 125 row[i].string_value = new std::string16(value); | 136 param->value_ptr = value.release(); |
| 126 break; | 137 return true; |
| 127 } | 138 } |
| 128 case JSPARAM_NULL: { | 139 return false; |
| 140 } | |
| 141 case JSPARAM_NULL: { | |
| 142 param->value_ptr = NULL; | |
| 143 return true; | |
| 144 } | |
| 145 } | |
| 146 // all other types are considered invalid | |
| 147 return false; | |
| 148 } | |
| 149 | |
| 150 JsParamType Database2Values::GetType(int index) const { | |
| 151 assert(index >= 0 && index < length_); | |
| 152 return arguments_[index].type; | |
| 153 } | |
| 154 | |
| 155 int Database2Values::GetAsInt(int index) const { | |
| 156 assert(index >= 0 && index < length_); | |
| 157 JsParamToSend param = arguments_[index]; | |
| 158 assert(param.type == JSPARAM_INT); | |
| 159 return *static_cast<int*>(const_cast<void*>(param.value_ptr)); | |
| 160 } | |
| 161 | |
| 162 double Database2Values::GetAsDouble(int index) const { | |
| 163 assert(index >= 0 && index < length_); | |
| 164 JsParamToSend param = arguments_[index]; | |
| 165 assert(param.type == JSPARAM_DOUBLE); | |
| 166 return *static_cast<int*>(const_cast<void*>(param.value_ptr)); | |
| 167 } | |
| 168 | |
| 169 std::string16 &Database2Values::GetAsString(int index) const { | |
| 170 assert(index >= 0 && index < length_); | |
| 171 JsParamToSend param = arguments_[index]; | |
| 172 assert(param.type == JSPARAM_STRING16); | |
| 173 return *static_cast<std::string16*>(const_cast<void*>(param.value_ptr)); | |
| 174 } | |
| 175 | |
| 176 Database2Values::~Database2Values() { | |
| 177 // dispose of the data, pointed to the value_ptrs | |
| 178 for(int i = 0; i < length_; i++) { | |
| 179 JsParamToSend param = arguments_[i]; | |
| 180 if (param.value_ptr) delete param.value_ptr; | |
| 181 } | |
| 182 } | |
| LEFT | RIGHT |