| OLD | NEW |
| 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. |
| (...skipping 25 matching lines...) Show 10 above Show 10 below |
| 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 const 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 Database2Statement *statement = new Database2Statement(); | 51 assert(instance && *instance); |
| 52 |
| 53 scoped_ptr<Database2Statement> statement(new Database2Statement()); |
| 54 |
| 55 // NULL should be passed if no arguments are specified |
| 56 assert(!sql_arguments || !JsTokenIsNullOrUndefined(sql_arguments->token())); |
| 57 // NULL should be passed if a callback is not specified |
| 58 assert(!callback || !JsTokenIsNullOrUndefined(callback->token())); |
| 59 assert(!error_callback || !JsTokenIsNullOrUndefined(error_callback->token())); |
| 60 |
| 52 statement->sql_statement_.assign(sql_statement); | 61 statement->sql_statement_.assign(sql_statement); |
| 53 statement->callback_.reset(callback); | 62 statement->callback_.reset(callback); |
| 54 statement->error_callback_.reset(error_callback); | 63 statement->error_callback_.reset(error_callback); |
| 55 // TODO(dimitri.glazkov): convert sql_arguments to JsParamToSend[]. Accepted | 64 |
| 56 // types are: double, int32, int64, string. All other types, are considered | 65 Database2Values *arguments; |
| 57 // invalid | 66 if (!Database2Values::CreateFromJsArray(sql_arguments, &arguments)) { |
| 58 *instance = statement; | 67 return false; |
| 59 // TODO(dimitri.glazkov): decide whether ever return false (so that creator | 68 } |
| 60 // could throw an exception, for instance) or mark statement as bogus | 69 |
| 70 statement->arguments_.reset(arguments); |
| 71 |
| 72 *instance = statement.release(); |
| 61 return true; | 73 return true; |
| 62 } | 74 } |
| 75 |
| 76 // static |
| 77 bool Database2Values::CreateFromJsArray(const JsArray *js_array, |
| 78 Database2Values **instance) { |
| 79 assert(instance && *instance); |
| 80 |
| 81 scoped_ptr<Database2Values> result(new Database2Values()); |
| 82 |
| 83 // since the arguments are optional, they could be passed as NULL |
| 84 if (js_array == NULL) { |
| 85 result->length_ = 0; |
| 86 *instance = result.release(); |
| 87 return true; |
| 88 } |
| 89 |
| 90 int len; |
| 91 if (!js_array->GetLength(&len)) { |
| 92 // unable to query JsArray, someting's gone horribly wrong |
| 93 // returning with failure will trigger an internal error |
| 94 assert(false); |
| 95 return false; |
| 96 } |
| 97 |
| 98 result->length_ = len; |
| 99 // a JsArray produces one row of values |
| 100 result->StartNewRow(); |
| 101 Variant *row = result->rows_.back(); |
| 102 for(int i = 0; i < len; i++) { |
| 103 switch(js_array->GetElementType(i)) { |
| 104 case JSPARAM_INT: { |
| 105 int value; |
| 106 if (!js_array->GetElementAsInt(i, &value)) { |
| 107 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 } |
| 140 } |
| 141 } |
| 142 |
| 143 *instance = result.release(); |
| 144 return true; |
| 145 } |
| 146 |
| 147 void Database2Values::StartNewRow() { |
| 148 rows_.push_back(new Variant[length_]); |
| 149 } |
| 150 |
| 151 bool Database2Values::Next() { |
| 152 return ++current_row_ >= length_; |
| 153 } |
| 154 |
| 155 JsParamType Database2Values::GetType(int index) const { |
| 156 return current(index).type; |
| 157 } |
| 158 |
| 159 bool Database2Values::GetElementAsInt(int index, int *value) { |
| 160 assert(current(index).type == JSPARAM_INT); |
| 161 assert(value); |
| 162 *value = current(index).int_value; |
| 163 return true; |
| 164 } |
| 165 |
| 166 bool Database2Values::GetElementAsDouble(int index, double *value) { |
| 167 assert(current(index).type = JSPARAM_DOUBLE); |
| 168 assert(value); |
| 169 *value = current(index).double_value; |
| 170 return true; |
| 171 } |
| 172 |
| 173 bool Database2Values::GetElementAsString(int index, std::string16 *value) { |
| 174 assert(current(index).type = JSPARAM_STRING16); |
| 175 assert(value); |
| 176 *value = *(current(index).string_value); |
| 177 return true; |
| 178 } |
| OLD | NEW |