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

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

Issue 717: Database2Values, argument conversion implemented (Closed) SVN Base: http://google-gears.googlecode.com/svn/contrib/dimitri.glazkov/database2/
Left Patch Set: Variant, multiple-row capability added Created 4 months, 2 weeks ago
Right Patch Set: Created 4 months, 4 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 #ifndef GEARS_DATABASE2_STATEMENT_H__ 26 #ifndef GEARS_DATABASE2_STATEMENT_H__
27 #define GEARS_DATABASE2_STATEMENT_H__ 27 #define GEARS_DATABASE2_STATEMENT_H__
28 28
29 #include <vector>
30
31 #include "gears/base/common/common.h" 29 #include "gears/base/common/common.h"
32 #include "gears/base/common/js_types.h" 30 #include "gears/base/common/js_types.h"
33 #include "gears/third_party/scoped_ptr/scoped_ptr.h" 31 #include "gears/third_party/scoped_ptr/scoped_ptr.h"
34 32
35 // forward declarations 33 // forward declarations
36 class Database2; 34 class Database2;
37 class Database2Transaction; 35 class Database2Transaction;
38 class Database2Values; 36 class Database2Values;
39 37
40 // represents statement, for both synchronous and asynchronous operations 38 // represents statement, for both synchronous and asynchronous operations
41 class Database2Statement { 39 class Database2Statement {
42 public: 40 public:
43 bool HasCallback() const { 41 bool HasCallback() const {
44 return callback_.get() != NULL; 42 assert(callback_.get());
43 return !JsTokenIsNullOrUndefined(callback_->token());
45 } 44 }
46 45
47 bool HasErrorCallback() const { 46 bool HasErrorCallback() const {
48 return error_callback_.get() != NULL; 47 assert(error_callback_.get());
48 return !JsTokenIsNullOrUndefined(error_callback_->token());
49 } 49 }
50 50
51 void InvokeCallback(Database2Transaction *tx); 51 void InvokeCallback(Database2Transaction *tx);
52 void InvokeErrorCallback(Database2Transaction *tx, JsObject *error); 52 void InvokeErrorCallback(Database2Transaction *tx, JsObject *error);
53 53
54 // create a statement instance
55 // must passs NULL for arguments or callbacks if they are not specified
56 static bool Create(const std::string16 &sql_statement, 54 static bool Create(const std::string16 &sql_statement,
57 JsArray *sql_arguments, 55 const JsArray &sql_arguments,
58 JsRootedCallback *callback, 56 JsRootedCallback *callback,
59 JsRootedCallback *error_callback, 57 JsRootedCallback *error_callback,
60 Database2Statement **instance); 58 Database2Statement **instance);
61 59
62 std::string16 sql() const { return sql_statement_; } 60 std::string16 sql() const { return sql_statement_; }
63 Database2Values *arguments() const { return arguments_.get(); } 61 Database2Values *arguments() const { return arguments_.get(); }
64 private: 62 private:
65 Database2Statement() {} 63 Database2Statement() {}
66 // if true, the statement has invalid arguments 64 // if true, the statement has invalid arguments
67 bool bogus_; 65 bool bogus_;
68 std::string16 sql_statement_; 66 std::string16 sql_statement_;
69 scoped_ptr<Database2Values> arguments_; 67 scoped_ptr<Database2Values> arguments_;
70 scoped_ptr<JsRootedCallback> callback_; 68 scoped_ptr<JsRootedCallback> callback_;
71 scoped_ptr<JsRootedCallback> error_callback_; 69 scoped_ptr<JsRootedCallback> error_callback_;
72 70
73 DISALLOW_EVIL_CONSTRUCTORS(Database2Statement); 71 DISALLOW_EVIL_CONSTRUCTORS(Database2Statement);
74 }; 72 };
75 73
76 // Used for marshaling statement arguments to the database thread and results to 74 // used for marshaling statement arguments to the database thread and
77 // the main thread. A union-based structure is used to store value-type pairs. 75 // results to the main thread
78 class Database2Values { 76 class Database2Values {
Aaron 2008/05/22 22:16:42 The shape of this internal class is really bugging
Dimitri 2008/05/24 02:13:40 On 2008/05/22 22:16:42, Aaron wrote: > The shape o
79 public: 77 public:
80 Database2Values() {} 78 ~Database2Values();
81 ~Database2Values() {
82 // remove all rows
83 for(unsigned int i = 0; i < rows_.size(); i++) {
84 delete[] rows_[i];
85 }
86 }
87 79
88 static bool CreateFromJsArray(const JsArray *js_array, 80 static bool CreateFromJsArray(const JsArray &sql_arguments,
89 Database2Values **instance); 81 Database2Values **instance);
90 82
91 // methods for reading values
92 int length() const { return length_; } 83 int length() const { return length_; }
93 void Reset() { current_row_ = 0; }
94 bool Next();
95 JsParamType GetType(int index) const; 84 JsParamType GetType(int index) const;
96 bool GetElementAsInt(int index, int *value); 85 int GetAsInt(int index) const;
97 bool GetElementAsDouble(int index, double *value); 86 double GetAsDouble(int index) const;
98 bool GetElementAsString(int index, std::string16 *value); 87 std::string16 &GetAsString(int index) const;
88 private:
89 Database2Values() {}
99 90
100 private: 91 static bool SetJsParamToSend(JsArray js_array,
101 void StartNewRow(); 92 int index,
93 JsParamToSend *param);
102 94
103 // used to store value-type pairs 95 scoped_array<JsParamToSend> arguments_;
104 struct Variant {
105 Variant() : type(JSPARAM_NULL) {}
106 ~Variant() {
107 if (type == JSPARAM_STRING16) { delete string_value; }
108 }
109
110 JsParamType type;
111 union {
112 int int_value;
113 double double_value;
114 std::string16 *string_value;
115 };
116 };
117
118 inline Variant &current(int index) const {
119 assert(index >= 0 && index < length_);
120 assert(current_row_ < length_);
121 return rows_[current_row_][index];
122 }
123
124 friend class vector;
125 std::vector<Variant*> rows_;
126 int length_; 96 int length_;
Aaron 2008/05/11 18:11:21 Should length be passed separately? The way it is
Dimitri 2008/05/13 22:52:03 On 2008/05/11 18:11:21, Aaron wrote: > Should leng
127 int current_row_;
128 97
129 DISALLOW_EVIL_CONSTRUCTORS(Database2Values); 98 DISALLOW_EVIL_CONSTRUCTORS(Database2Values);
130 }; 99 };
131 100
132 #endif // GEARS_DATABASE2_STATEMENT_H__ 101 #endif // GEARS_DATABASE2_STATEMENT_H__
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld r338