| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sync/util/sqlite_utils.h" | 5 #include "chrome/browser/sync/util/sqlite_utils.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 | 8 |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/string16.h" | 13 #include "base/string16.h" |
| 14 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 15 | 15 |
| 16 namespace sqlite_utils { |
| 17 |
| 16 // The vanilla error handler implements the common fucntionality for all the | 18 // The vanilla error handler implements the common fucntionality for all the |
| 17 // error handlers. Specialized error handlers are expected to only override | 19 // error handlers. Specialized error handlers are expected to only override |
| 18 // the Handler() function. | 20 // the Handler() function. |
| 19 class VanillaSQLErrorHandler : public SQLErrorHandler { | 21 class VanillaSQLErrorHandler : public SQLErrorHandler { |
| 20 public: | 22 public: |
| 21 VanillaSQLErrorHandler() : error_(SQLITE_OK) { | 23 VanillaSQLErrorHandler() : error_(SQLITE_OK) { |
| 22 } | 24 } |
| 23 virtual int GetLastError() const { | 25 virtual int GetLastError() const { |
| 24 return error_; | 26 return error_; |
| 25 } | 27 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 | 80 |
| 79 static base::LazyInstance<DefaultSQLErrorHandlerFactory> | 81 static base::LazyInstance<DefaultSQLErrorHandlerFactory> |
| 80 g_default_sql_error_handler_factory(base::LINKER_INITIALIZED); | 82 g_default_sql_error_handler_factory(base::LINKER_INITIALIZED); |
| 81 | 83 |
| 82 SQLErrorHandlerFactory* GetErrorHandlerFactory() { | 84 SQLErrorHandlerFactory* GetErrorHandlerFactory() { |
| 83 // TODO(cpu): Testing needs to override the error handler. | 85 // TODO(cpu): Testing needs to override the error handler. |
| 84 // Destruction of DefaultSQLErrorHandlerFactory handled by at_exit manager. | 86 // Destruction of DefaultSQLErrorHandlerFactory handled by at_exit manager. |
| 85 return g_default_sql_error_handler_factory.Pointer(); | 87 return g_default_sql_error_handler_factory.Pointer(); |
| 86 } | 88 } |
| 87 | 89 |
| 88 namespace sqlite_utils { | |
| 89 | |
| 90 int OpenSqliteDb(const FilePath& filepath, sqlite3** database) { | 90 int OpenSqliteDb(const FilePath& filepath, sqlite3** database) { |
| 91 #if defined(OS_WIN) | 91 #if defined(OS_WIN) |
| 92 // We want the default encoding to always be UTF-8, so we use the | 92 // We want the default encoding to always be UTF-8, so we use the |
| 93 // 8-bit version of open(). | 93 // 8-bit version of open(). |
| 94 return sqlite3_open(WideToUTF8(filepath.value()).c_str(), database); | 94 return sqlite3_open(WideToUTF8(filepath.value()).c_str(), database); |
| 95 #elif defined(OS_POSIX) | 95 #elif defined(OS_POSIX) |
| 96 return sqlite3_open(filepath.value().c_str(), database); | 96 return sqlite3_open(filepath.value().c_str(), database); |
| 97 #endif | 97 #endif |
| 98 } | 98 } |
| 99 | 99 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 std::string b; | 155 std::string b; |
| 156 b.append("SELECT * FROM "); | 156 b.append("SELECT * FROM "); |
| 157 b.append(table_name); | 157 b.append(table_name); |
| 158 | 158 |
| 159 if (s.prepare(db, b.c_str()) != SQLITE_OK) | 159 if (s.prepare(db, b.c_str()) != SQLITE_OK) |
| 160 return false; | 160 return false; |
| 161 | 161 |
| 162 return s.step() == SQLITE_ROW; | 162 return s.step() == SQLITE_ROW; |
| 163 } | 163 } |
| 164 | 164 |
| 165 } // namespace sqlite_utils | |
| 166 | |
| 167 SQLTransaction::SQLTransaction(sqlite3* db) : db_(db), began_(false) { | 165 SQLTransaction::SQLTransaction(sqlite3* db) : db_(db), began_(false) { |
| 168 } | 166 } |
| 169 | 167 |
| 170 SQLTransaction::~SQLTransaction() { | 168 SQLTransaction::~SQLTransaction() { |
| 171 if (began_) { | 169 if (began_) { |
| 172 Rollback(); | 170 Rollback(); |
| 173 } | 171 } |
| 174 } | 172 } |
| 175 | 173 |
| 176 int SQLTransaction::BeginCommand(const char* command) { | 174 int SQLTransaction::BeginCommand(const char* command) { |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 const char* s = column_text(index); | 499 const char* s = column_text(index); |
| 502 str->assign(s ? UTF8ToWide(s) : std::wstring()); | 500 str->assign(s ? UTF8ToWide(s) : std::wstring()); |
| 503 return (s != NULL); | 501 return (s != NULL); |
| 504 } | 502 } |
| 505 | 503 |
| 506 std::wstring SQLStatement::column_wstring(int index) { | 504 std::wstring SQLStatement::column_wstring(int index) { |
| 507 std::wstring wstr; | 505 std::wstring wstr; |
| 508 column_wstring(index, &wstr); | 506 column_wstring(index, &wstr); |
| 509 return wstr; | 507 return wstr; |
| 510 } | 508 } |
| 509 |
| 510 } // namespace sqlite_utils |
| OLD | NEW |