Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(27)

Delta Between Two Patch Sets: gears/database2/statement.cc

Issue 717: Database2Values, argument conversion implemented (Closed) SVN Base: http://google-gears.googlecode.com/svn/contrib/dimitri.glazkov/database2/
Left Patch Set: Removed post-increment Created 3 months ago
Right Patch Set: Variant, multiple-row capability added Created 3 months, 2 weeks ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
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 JsArray *sql_arguments,
47 JsRootedCallback *callback, 48 JsRootedCallback *callback,
48 JsRootedCallback *error_callback, 49 JsRootedCallback *error_callback,
49 Database2Statement **instance) { 50 Database2Statement **instance) {
51 assert(instance && *instance);
52
50 scoped_ptr<Database2Statement> statement(new Database2Statement()); 53 scoped_ptr<Database2Statement> statement(new Database2Statement());
51 54
52 // NULL should be passed if no arguments are specified 55 // NULL should be passed if no arguments are specified
53 assert(!sql_arguments || !JsTokenIsNullOrUndefined(sql_arguments->token())); 56 assert(!sql_arguments || !JsTokenIsNullOrUndefined(sql_arguments->token()));
54 // NULL should be passed if a callback is not specified 57 // NULL should be passed if a callback is not specified
55 assert(!callback || !JsTokenIsNullOrUndefined(callback->token())); 58 assert(!callback || !JsTokenIsNullOrUndefined(callback->token()));
56 assert(!error_callback || !JsTokenIsNullOrUndefined(error_callback->token())); 59 assert(!error_callback || !JsTokenIsNullOrUndefined(error_callback->token()));
57 60
58 statement->sql_statement_.assign(sql_statement); 61 statement->sql_statement_.assign(sql_statement);
59 statement->callback_.reset(callback); 62 statement->callback_.reset(callback);
60 statement->error_callback_.reset(error_callback); 63 statement->error_callback_.reset(error_callback);
61 64
62 Database2Values *arguments; 65 Database2Values *arguments;
63 if (!Database2Values::CreateFromJsArray(sql_arguments, &arguments)) { 66 if (!Database2Values::CreateFromJsArray(sql_arguments, &arguments)) {
64 return false; 67 return false;
65 } 68 }
66 69
67 statement->arguments_.reset(arguments); 70 statement->arguments_.reset(arguments);
68 71
69 *instance = statement.release(); 72 *instance = statement.release();
70 return true; 73 return true;
71 } 74 }
72 75
73 // static 76 // static
74 bool Database2Values::CreateFromJsArray(const JsArray *js_array, 77 bool Database2Values::CreateFromJsArray(const JsArray *js_array,
75 Database2Values **instance) { 78 Database2Values **instance) {
76 assert(instance && *instance); 79 assert(instance && *instance);
77 80
78 scoped_ptr<Database2Values> result(new Database2Values()); 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 (js_array == NULL) { 84 if (js_array == NULL) {
82 result->length_ = 0; 85 result->length_ = 0;
83 *instance = result.release(); 86 *instance = result.release();
84 return true; 87 return true;
85 } 88 }
86 89
87 int len; 90 int len;
88 if (!js_array->GetLength(&len)) { 91 if (!js_array->GetLength(&len)) {
89 // unable to query JsArray, someting's gone horribly wrong 92 // unable to query JsArray, someting's gone horribly wrong
90 // returning with failure will trigger an internal error 93 // returning with failure will trigger an internal error
91 assert(false); 94 assert(false);
92 return false; 95 return false;
93 } 96 }
94 97
95 result->length_ = len; 98 result->length_ = len;
96 // a JsArray produces one row of values 99 // a JsArray produces one row of values
97 result->StartNewRow(); 100 result->StartNewRow();
98 Variant *row = result->rows_.back(); 101 Variant *row = result->rows_.back();
99 for(int i = 0; i < len; i++) { 102 for(int i = 0; i < len; i++) {
100 switch(js_array->GetElementType(i)) { 103 switch(js_array->GetElementType(i)) {
101 case JSPARAM_INT: { 104 case JSPARAM_INT: {
102 int value; 105 int value;
103 if (!js_array->GetElementAsInt(i, &value)) { 106 if (!js_array->GetElementAsInt(i, &value)) {
104 return false; 107 return false;
105 } 108 }
106 row[i].type = JSPARAM_INT; 109 row[i].type = JSPARAM_INT;
107 row[i].int_value = value; 110 row[i].int_value = value;
108 break; 111 break;
109 } 112 }
110 case JSPARAM_DOUBLE: { 113 case JSPARAM_DOUBLE: {
111 double value; 114 double value;
112 if (!js_array->GetElementAsDouble(i, &value)) { 115 if (!js_array->GetElementAsDouble(i, &value)) {
113 return false; 116 return false;
114 } 117 }
115 row[i].type = JSPARAM_DOUBLE; 118 row[i].type = JSPARAM_DOUBLE;
116 row[i].double_value = value; 119 row[i].double_value = value;
117 break; 120 break;
118 } 121 }
119 case JSPARAM_STRING16: { 122 case JSPARAM_STRING16: {
120 std::string16 value; 123 std::string16 value;
121 if (!js_array->GetElementAsString(i, &value)) { 124 if (!js_array->GetElementAsString(i, &value)) {
122 return false; 125 return false;
123 } 126 }
124 row[i].type = JSPARAM_STRING16; 127 row[i].type = JSPARAM_STRING16;
125 row[i].string_value = new std::string16(value); 128 row[i].string_value = new std::string16(value);
126 break; 129 break;
127 } 130 }
128 case JSPARAM_NULL: { 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 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld r305