| 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. |
| (...skipping 21 matching lines...) Show 10 above Show 10 below | |
| 32 | 32 |
| 33 // forward declarations | 33 // forward declarations |
| 34 class Database2; | 34 class Database2; |
| 35 class Database2Transaction; | 35 class Database2Transaction; |
| 36 class Database2Values; | 36 class Database2Values; |
| 37 | 37 |
| 38 // represents statement, for both synchronous and asynchronous operations | 38 // represents statement, for both synchronous and asynchronous operations |
| 39 class Database2Statement { | 39 class Database2Statement { |
| 40 public: | 40 public: |
| 41 bool HasCallback() const { | 41 bool HasCallback() const { |
| 42 return callback_.get() != NULL; | 42 assert(callback_.get()); |
| 43 return !JsTokenIsNullOrUndefined(callback_->token()); | |
| 43 } | 44 } |
| 44 | 45 |
| 45 bool HasErrorCallback() const { | 46 bool HasErrorCallback() const { |
| 46 return error_callback_.get() != NULL; | 47 assert(error_callback_.get()); |
| 48 return !JsTokenIsNullOrUndefined(error_callback_->token()); | |
| 47 } | 49 } |
| 48 | 50 |
| 49 void InvokeCallback(Database2Transaction *tx); | 51 void InvokeCallback(Database2Transaction *tx); |
| 50 void InvokeErrorCallback(Database2Transaction *tx, JsObject *error); | 52 void InvokeErrorCallback(Database2Transaction *tx, JsObject *error); |
| 51 | 53 |
| 52 // create a statement instance | |
| 53 // must passs NULL for arguments or callbacks if they are not specified | |
| 54 static bool Create(const std::string16 &sql_statement, | 54 static bool Create(const std::string16 &sql_statement, |
| 55 JsArray *sql_arguments, | 55 const JsArray &sql_arguments, |
| 56 JsRootedCallback *callback, | 56 JsRootedCallback *callback, |
| 57 JsRootedCallback *error_callback, | 57 JsRootedCallback *error_callback, |
| 58 Database2Statement **instance); | 58 Database2Statement **instance); |
| 59 | 59 |
| 60 std::string16 sql() const { return sql_statement_; } | 60 std::string16 sql() const { return sql_statement_; } |
| 61 Database2Values *arguments() const { return arguments_.get(); } | 61 Database2Values *arguments() const { return arguments_.get(); } |
| 62 private: | 62 private: |
| 63 Database2Statement() {} | 63 Database2Statement() {} |
| 64 // if true, the statement has invalid arguments | 64 // if true, the statement has invalid arguments |
| 65 bool bogus_; | 65 bool bogus_; |
| 66 std::string16 sql_statement_; | 66 std::string16 sql_statement_; |
| 67 scoped_ptr<Database2Values> arguments_; | 67 scoped_ptr<Database2Values> 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 | 74 // used for marshaling statement arguments to the database thread and |
| 75 // results to the main thread. | 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 { | 76 class Database2Values { |
| 81 public: | 77 public: |
| 82 ~Database2Values(); | 78 ~Database2Values(); |
| 83 | 79 |
| 84 static bool CreateFromJsArray(const JsArray *sql_arguments, | 80 static bool CreateFromJsArray(const JsArray &sql_arguments, |
| 85 Database2Values **instance); | 81 Database2Values **instance); |
| 86 | 82 |
| 87 int length() const { return length_; } | 83 int length() const { return length_; } |
| 88 JsParamType GetType(int index) const; | 84 JsParamType GetType(int index) const; |
| 89 int GetAsInt(int index) const; | 85 int GetAsInt(int index) const; |
| 90 double GetAsDouble(int index) const; | 86 double GetAsDouble(int index) const; |
| 91 std::string16 &GetAsString(int index) const; | 87 std::string16 &GetAsString(int index) const; |
| 92 private: | 88 private: |
| 93 Database2Values() {} | 89 Database2Values() {} |
| 94 | 90 |
| 95 static bool SetJsParamToSend(const JsArray *js_array, | 91 static bool SetJsParamToSend(JsArray js_array, |
| 96 int index, | 92 int index, |
| 97 JsParamToSend *param); | 93 JsParamToSend *param); |
| 98 | 94 |
| 99 scoped_array<JsParamToSend> arguments_; | 95 scoped_array<JsParamToSend> arguments_; |
| 100 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
| |
| 101 | 97 |
| 102 DISALLOW_EVIL_CONSTRUCTORS(Database2Values); | 98 DISALLOW_EVIL_CONSTRUCTORS(Database2Values); |
| 103 }; | 99 }; |
| 104 | 100 |
| 105 #endif // GEARS_DATABASE2_STATEMENT_H__ | 101 #endif // GEARS_DATABASE2_STATEMENT_H__ |
| LEFT | RIGHT |