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

Side by Side Diff: gears/database2/statement.h

Issue 717: Database2Values, argument conversion implemented (Closed) SVN Base: http://google-gears.googlecode.com/svn/contrib/dimitri.glazkov/database2/
Patch Set: ready for another look. Created 3 months, 3 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:
View unified diff | Download patch
OLDNEW
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 "gears/base/common/common.h" 29 #include "gears/base/common/common.h"
30 #include "gears/base/common/js_types.h" 30 #include "gears/base/common/js_types.h"
31 #include "gears/third_party/scoped_ptr/scoped_ptr.h" 31 #include "gears/third_party/scoped_ptr/scoped_ptr.h"
32 32
33 // forward declarations 33 // forward declarations
34 class Database2; 34 class Database2;
35 class Database2Transaction; 35 class Database2Transaction;
36 class Database2Values;
36 37
37 // represents Database2Statement 38 // represents statement, for both synchronous and asynchronous operations
38 class Database2Statement { 39 class Database2Statement {
39 public: 40 public:
40 Database2Statement() {}
41
42 bool HasCallback() const { 41 bool HasCallback() const {
43 assert(callback_.get()); 42 return callback_.get() != NULL;
44 return !JsTokenIsNullOrUndefined(callback_->token());
45 } 43 }
46 44
47 bool HasErrorCallback() const { 45 bool HasErrorCallback() const {
48 assert(error_callback_.get()); 46 return error_callback_.get() != NULL;
49 return !JsTokenIsNullOrUndefined(error_callback_->token());
50 } 47 }
51 48
52 void InvokeCallback(Database2Transaction *tx); 49 void InvokeCallback(Database2Transaction *tx);
53 void InvokeErrorCallback(Database2Transaction *tx, JsObject *error); 50 void InvokeErrorCallback(Database2Transaction *tx, JsObject *error);
54 51
52 // create a statement instance
53 // must passs NULL for arguments or callbacks if they are not specified
55 static bool Create(const std::string16 &sql_statement, 54 static bool Create(const std::string16 &sql_statement,
56 const JsArray &sql_arguments, 55 JsArray *sql_arguments,
57 JsRootedCallback *callback, 56 JsRootedCallback *callback,
58 JsRootedCallback *error_callback, 57 JsRootedCallback *error_callback,
59 Database2Statement **instance); 58 Database2Statement **instance);
60 59
61 std::string16 sql() const { return sql_statement_; } 60 std::string16 sql() const { return sql_statement_; }
62 JsParamToSend *arguments() const { return sql_arguments_; } 61 Database2Values *arguments() const { return arguments_.get(); }
63 int num_arguments() const { return num_arguments_; }
64 private: 62 private:
63 Database2Statement() {}
64 // if true, the statement has invalid arguments
65 bool bogus_;
65 std::string16 sql_statement_; 66 std::string16 sql_statement_;
66 int num_arguments_; 67 scoped_ptr<Database2Values> arguments_;
67 JsParamToSend *sql_arguments_;
68 scoped_ptr<JsRootedCallback> callback_; 68 scoped_ptr<JsRootedCallback> callback_;
69 scoped_ptr<JsRootedCallback> error_callback_; 69 scoped_ptr<JsRootedCallback> error_callback_;
70 70
71 DISALLOW_EVIL_CONSTRUCTORS(Database2Statement); 71 DISALLOW_EVIL_CONSTRUCTORS(Database2Statement);
72 }; 72 };
73 73
74 // Used for marshaling statement arguments to the database thread and
75 // results to the main thread.
76 // JsParamToSend is used to store the value-type pairs, and as a result the
77 // values are heap-allocated, which is unfortunate in the case of int/double.
78 // TODO(dimitri.glazkov): Reimplement using unions, possibly reusing or
79 // integrating with MarshaledJsToken
80 class Database2Values {
81 public:
82 ~Database2Values();
83
84 static bool CreateFromJsArray(const JsArray *sql_arguments,
85 Database2Values **instance);
86
87 int length() const { return length_; }
88 JsParamType GetType(int index) const;
89 int GetAsInt(int index) const;
90 double GetAsDouble(int index) const;
91 std::string16 &GetAsString(int index) const;
92 private:
93 Database2Values() {}
94
95 static bool SetJsParamToSend(const JsArray *js_array,
96 int index,
97 JsParamToSend *param);
98
99 scoped_array<JsParamToSend> arguments_;
100 int length_;
101
102 DISALLOW_EVIL_CONSTRUCTORS(Database2Values);
103 };
104
74 #endif // GEARS_DATABASE2_STATEMENT_H__ 105 #endif // GEARS_DATABASE2_STATEMENT_H__
OLDNEW

Powered by Google App Engine
This is Rietveld r305