| 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. |
| 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_CONNECTION_H__ | 26 #ifndef GEARS_DATABASE2_CONNECTION_H__ |
| 27 #define GEARS_DATABASE2_CONNECTION_H__ | 27 #define GEARS_DATABASE2_CONNECTION_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/base/common/scoped_refptr.h" | 31 #include "gears/base/common/scoped_refptr.h" |
| 34 #include "gears/base/common/security_model.h" | 32 #include "gears/base/common/security_model.h" |
| 35 #include "gears/base/common/string16.h" | 33 #include "gears/base/common/string16.h" |
| 36 #include "gears/third_party/scoped_ptr/scoped_ptr.h" | |
| 37 #include "gears/third_party/sqlite_google/preprocessed/sqlite3.h" | 34 #include "gears/third_party/sqlite_google/preprocessed/sqlite3.h" |
| 38 | 35 |
| 39 // used to store value-type pairs | 36 class Database2Values; |
| 40 struct Database2Variant { | |
| 41 Database2Variant() : type(JSPARAM_NULL) {} | |
| 42 ~Database2Variant() { | |
| 43 if (type == JSPARAM_STRING16) { delete string_value; } | |
| 44 } | |
| 45 | 37 |
| 46 JsParamType type; | |
| 47 union { | |
| 48 int int_value; | |
| 49 double double_value; | |
| 50 std::string16 *string_value; | |
| 51 }; | |
| 52 }; | |
| 53 | |
| 54 // Represents a consumer of results, produced by Connection::Execute method. | |
| 55 // Conceptually, it is a forward-only cursor over a result set. The cursor | |
| 56 // allows creating new rows and assigning the values in one row at a time. | |
| 57 // The proper execution sequence is: | |
| 58 // 1) Init -- to initialize the result set. This method can be only called once | |
| 59 // for a given instance. Re-initialization is not supported. | |
| 60 // 2) HandleNewRow -- to create a new row (no rows exist in a newly initialized | |
| 61 // result set. | |
| 62 // 3) HandleColumn* -- to set a value in a row. The index value must be between
0 and | |
| 63 // column_count, specified in the Init | |
| 64 class Database2RowHandlerInterface { | 38 class Database2RowHandlerInterface { |
| 65 public: | 39 public: |
| 66 // initialize the handler with an array of column names | 40 Database2RowHandlerInterface() {}; |
| 67 // the method takes ownership of the array | 41 ~Database2RowHandlerInterface() {}; |
| 68 virtual void Init(int column_count, std::string16 *column_names) = 0; | 42 |
| 69 virtual void HandleNewRow() = 0; | 43 virtual void Begin() = 0; |
| 70 virtual bool HandleColumnInt(int index, int value) = 0; | 44 // TODO(dimitri.glazkov): Add row data parameter(s) |
| 71 virtual bool HandleColumnDouble(int index, double value) = 0; | 45 virtual void HandleRow() = 0; |
| 72 // The implementation must copy the string value argument. | 46 virtual void End() = 0; |
| 73 virtual bool HandleColumnString(int index, const std::string16 &value) = 0; | 47 |
| 74 virtual bool HandleColumnNull(int index) = 0; | 48 DISALLOW_EVIL_CONSTRUCTORS(Database2RowHandlerInterface); |
| 75 virtual void HandleStats(int64 last_insert_rowid, int rows_affected) = 0; | |
| 76 }; | 49 }; |
| 77 | 50 |
| 78 // Encapsulates database operations, opens and closes database connection | 51 // Encapsulates database operations, opens and closes database connection |
| 79 class Database2Connection : public RefCounted { | 52 class Database2Connection : public RefCounted { |
| 80 public: | 53 public: |
| 81 Database2Connection(const std::string16 &name, | 54 // lazily initialized |
| 82 const SecurityOrigin &origin) : | 55 Database2Connection(const std::string16 &name, |
| 83 name_(name), origin_(origin), handle_(NULL), bogus_version_(false) {} | 56 const SecurityOrigin &origin) : |
| 57 name_(name), origin_(origin) {} |
| 84 ~Database2Connection() { | 58 ~Database2Connection() { |
| 85 if (handle_) { | 59 // close connection |
| 86 sqlite3_close(handle_); | |
| 87 } | |
| 88 } | 60 } |
| 89 | 61 |
| 90 bool OpenAndVerifyVersion(const std::string16 &database_version); | 62 bool OpenAndVerifyVersion(const std::string16 &database_version); |
| 91 bool Execute(const std::string16 &statement, | 63 bool Execute(const std::string16 &statement, |
| 92 int num_arguments, | 64 Database2Values *arguments, |
| 93 Database2Variant *arguments, | |
| 94 Database2RowHandlerInterface *row_handler); | 65 Database2RowHandlerInterface *row_handler); |
| 95 bool Begin(); | 66 bool Begin(); |
| 96 void Rollback(); | 67 void Rollback(); |
| 97 bool Commit(); | 68 bool Commit(); |
| 98 | 69 |
| 99 int error_code() const { return error_code_; } | 70 int error_code() const { return error_code_; } |
| 100 const std::string16 &error_message() const { return error_message_; } | 71 std::string16 error_message() const { return error_message_; } |
| 101 | 72 |
| 102 private: | 73 private: |
| 103 // populates error code and error message fields using sqlite_wrapper helpers | |
| 104 // and if the sqlite3 returned SQLITE_CORRUPT code, poisons the database | |
| 105 void SetAndHandleError(int error_code, | |
| 106 const char16 *summary, | |
| 107 int sqlite_result_code); | |
| 108 bool bogus_version_; | 74 bool bogus_version_; |
| 109 int expected_version_; | 75 int expected_version_; |
| 110 | 76 |
| 111 std::string16 error_message_; | 77 std::string16 error_message_; |
| 112 int error_code_; | 78 int error_code_; |
| 113 | 79 |
| 114 sqlite3 *handle_; | 80 sqlite3 *handle_; |
| 115 std::string16 name_; | 81 std::string16 name_; |
| 116 SecurityOrigin origin_; | 82 SecurityOrigin origin_; |
| 117 | 83 |
| 118 DISALLOW_EVIL_CONSTRUCTORS(Database2Connection); | 84 DISALLOW_EVIL_CONSTRUCTORS(Database2Connection); |
| 119 }; | 85 }; |
| 120 | 86 |
| 121 // Used for marshaling of results from the database thread to the main thread. | |
| 122 // Implements a row handler that stores all of supplied data, and a CopyTo | |
| 123 // method to apply the data to another row handler. | |
| 124 class Database2BufferingRowHandler : public Database2RowHandlerInterface { | |
| 125 public: | |
| 126 Database2BufferingRowHandler() : column_count_(0) {} | |
| 127 ~Database2BufferingRowHandler() { | |
| 128 // remove all rows | |
| 129 for(unsigned int i = 0; i < rows_.size(); ++i) { | |
| 130 delete[] rows_[i]; | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 // Database2RowHandlerInterface | |
| 135 virtual void Init(int column_count, std::string16 *column_names); | |
| 136 virtual void HandleNewRow(); | |
| 137 virtual bool HandleColumnInt(int index, int value); | |
| 138 virtual bool HandleColumnDouble(int index, double value); | |
| 139 virtual bool HandleColumnString(int index, const std::string16 &value); | |
| 140 virtual bool HandleColumnNull(int index); | |
| 141 virtual void HandleStats(int64 last_insert_rowid, int rows_affected); | |
| 142 | |
| 143 bool CopyTo(Database2RowHandlerInterface *target); | |
| 144 private: | |
| 145 | |
| 146 std::vector<Database2Variant*> rows_; | |
| 147 scoped_array<std::string16> column_names_; | |
| 148 int rows_affected_; | |
| 149 int64 last_insert_rowid_; | |
| 150 int column_count_; | |
| 151 | |
| 152 DISALLOW_EVIL_CONSTRUCTORS(Database2BufferingRowHandler); | |
| 153 }; | |
| 154 | |
| 155 #endif // GEARS_DATABASE2_CONNECTION_H__ | 87 #endif // GEARS_DATABASE2_CONNECTION_H__ |
| LEFT | RIGHT |