diff options
author | maruel@google.com <maruel@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-12 15:22:13 +0000 |
---|---|---|
committer | maruel@google.com <maruel@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-12 15:22:13 +0000 |
commit | e1981f43505c69351e5786eac1bff1913f4c96db (patch) | |
tree | 9be2552eea732795716489cba689918dc5dd74a3 /chrome/common | |
parent | 169da8f5bba74250fe73f708a1a3b666b4fb2431 (diff) | |
download | chromium_src-e1981f43505c69351e5786eac1bff1913f4c96db.zip chromium_src-e1981f43505c69351e5786eac1bff1913f4c96db.tar.gz chromium_src-e1981f43505c69351e5786eac1bff1913f4c96db.tar.bz2 |
Cleanup a few files, reduce the number of includes.
Applied glint.
No code change, just moving around.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@713 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common')
-rw-r--r-- | chrome/common/chrome_counters.cc | 2 | ||||
-rw-r--r-- | chrome/common/chrome_counters.h | 11 | ||||
-rw-r--r-- | chrome/common/gfx/chrome_canvas.cc | 19 | ||||
-rw-r--r-- | chrome/common/gfx/chrome_canvas.h | 18 | ||||
-rw-r--r-- | chrome/common/ipc_message_utils.cc | 95 | ||||
-rw-r--r-- | chrome/common/ipc_message_utils.h | 100 | ||||
-rw-r--r-- | chrome/common/sqlite_compiled_statement.cc | 22 | ||||
-rw-r--r-- | chrome/common/sqlite_compiled_statement.h | 29 | ||||
-rw-r--r-- | chrome/common/sqlite_utils.cc | 338 | ||||
-rw-r--r-- | chrome/common/sqlite_utils.h | 416 |
10 files changed, 576 insertions, 474 deletions
diff --git a/chrome/common/chrome_counters.cc b/chrome/common/chrome_counters.cc index 1474027..de40052 100644 --- a/chrome/common/chrome_counters.cc +++ b/chrome/common/chrome_counters.cc @@ -29,6 +29,8 @@ #include "chrome/common/chrome_counters.h" +#include "base/stats_counters.h" + namespace chrome { // Note: We use the construct-on-first-use pattern here, because we don't diff --git a/chrome/common/chrome_counters.h b/chrome/common/chrome_counters.h index 85f9c5d..41ee96b 100644 --- a/chrome/common/chrome_counters.h +++ b/chrome/common/chrome_counters.h @@ -29,10 +29,12 @@ // Counters used within the browser. -#ifndef CHROME_COMMON_CHROME_COUNTERS_H__ -#define CHROME_COMMON_CHROME_COUNTERS_H__ +#ifndef CHROME_COMMON_CHROME_COUNTERS_H_ +#define CHROME_COMMON_CHROME_COUNTERS_H_ -#include "base/stats_counters.h" +class StatsCounter; +class StatsCounterTimer; +class StatsRate; namespace chrome { @@ -58,9 +60,8 @@ class Counters { // Time/Count of plugin network interception. static StatsRate& plugin_intercept(); - }; } // namespace chrome -#endif // CHROME_COMMON_CHROME_COUNTERS_H__ +#endif // CHROME_COMMON_CHROME_COUNTERS_H_ diff --git a/chrome/common/gfx/chrome_canvas.cc b/chrome/common/gfx/chrome_canvas.cc index 631d2f3..b73a407 100644 --- a/chrome/common/gfx/chrome_canvas.cc +++ b/chrome/common/gfx/chrome_canvas.cc @@ -27,14 +27,15 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include <limits> - #include "chrome/common/gfx/chrome_canvas.h" -#include "base/gfx/platform_canvas.h" +#include <limits> + #include "base/gfx/rect.h" #include "base/logging.h" #include "skia/include/SkShader.h" +#include "chrome/common/gfx/chrome_font.h" +#include "chrome/common/l10n_util.h" ChromeCanvas::ChromeCanvas(int width, int height, bool is_opaque) : gfx::PlatformCanvas(width, height, is_opaque) { @@ -226,7 +227,8 @@ void ChromeCanvas::DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, shader_scale.setScale( SkFloatToScalar(static_cast<float>(dest_w) / src_w), SkFloatToScalar(static_cast<float>(dest_h) / src_h)); - shader_scale.postTranslate(SkIntToScalar(dest_x - src_x), SkIntToScalar(dest_y - src_y)); + shader_scale.postTranslate(SkIntToScalar(dest_x - src_x), + SkIntToScalar(dest_y - src_y)); shader->setLocalMatrix(shader_scale); // Set up our paint to use the shader & release our reference (now just owned @@ -338,6 +340,15 @@ void ChromeCanvas::DrawStringInt(const std::wstring& text, HFONT font, getTopPlatformDevice().postProcessGDI(x, y, w, h); } +void ChromeCanvas::DrawStringInt(const std::wstring& text, + const ChromeFont& font, + const SkColor& color, + int x, int y, + int w, int h) { + DrawStringInt(text, font, color, x, y, w, h, + l10n_util::DefaultCanvasTextAlignment()); +} + // We make sure that LTR text we draw in an RTL context is modified // appropriately to make sure it maintains it LTR orientation. void ChromeCanvas::DoDrawText(HDC hdc, const std::wstring& text, diff --git a/chrome/common/gfx/chrome_canvas.h b/chrome/common/gfx/chrome_canvas.h index a291880..4c6d63d 100644 --- a/chrome/common/gfx/chrome_canvas.h +++ b/chrome/common/gfx/chrome_canvas.h @@ -27,16 +27,15 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#ifndef CHROME_COMMON_GFX_CHROME_CANVAS_H__ -#define CHROME_COMMON_GFX_CHROME_CANVAS_H__ +#ifndef CHROME_COMMON_GFX_CHROME_CANVAS_H_ +#define CHROME_COMMON_GFX_CHROME_CANVAS_H_ #include <windows.h> #include <string> #include "base/basictypes.h" #include "base/gfx/platform_canvas.h" -#include "chrome/common/gfx/chrome_font.h" -#include "chrome/common/l10n_util.h" +class ChromeFont; namespace gfx { class Rect; } @@ -155,10 +154,7 @@ class ChromeCanvas : public gfx::PlatformCanvas { // aligned to the left, vertically centered, clipped to the region. If the // text is too big, it is truncated and '...' is added to the end. void DrawStringInt(const std::wstring& text, const ChromeFont& font, - const SkColor& color, int x, int y, int w, int h) { - DrawStringInt(text, font, color, x, y, w, h, - l10n_util::DefaultCanvasTextAlignment()); - } + const SkColor& color, int x, int y, int w, int h); // Draws text with the specified color, font and location. The last argument // specifies flags for how the text should be rendered. It can be one of @@ -190,8 +186,8 @@ class ChromeCanvas : public gfx::PlatformCanvas { private: // Draws text with the specified color, font and location. The text is - // aligned to the left, vertically centered, clipped to the region. If the - // text is too big, it is truncated and '...' is added to the end. + // aligned to the left, vertically centered, clipped to the region. If the + // text is too big, it is truncated and '...' is added to the end. void DrawStringInt(const std::wstring& text, HFONT font, const SkColor& color, int x, int y, int w, int h, int flags); @@ -210,4 +206,4 @@ class ChromeCanvas : public gfx::PlatformCanvas { typedef gfx::CanvasPaintT<ChromeCanvas> ChromeCanvasPaint; -#endif // CHROME_COMMON_GFX_CHROME_CANVAS_H__ +#endif // CHROME_COMMON_GFX_CHROME_CANVAS_H_ diff --git a/chrome/common/ipc_message_utils.cc b/chrome/common/ipc_message_utils.cc index ad5050c..ee6a554 100644 --- a/chrome/common/ipc_message_utils.cc +++ b/chrome/common/ipc_message_utils.cc @@ -31,11 +31,95 @@ #include "base/gfx/rect.h" #include "googleurl/src/gurl.h" +#include "SkBitmap.h" #include "webkit/glue/dom_operations.h" #include "webkit/glue/webcursor.h" namespace IPC { +namespace { + +struct SkBitmap_Data { + // The configuration for the bitmap (bits per pixel, etc). + SkBitmap::Config fConfig; + + // The width of the bitmap in pixels. + uint32 fWidth; + + // The height of the bitmap in pixels. + uint32 fHeight; + + // The number of bytes between subsequent rows of the bitmap. + uint32 fRowBytes; + + void InitSkBitmapDataForTransfer(const SkBitmap& bitmap) { + fConfig = bitmap.config(); + fWidth = bitmap.width(); + fHeight = bitmap.height(); + fRowBytes = bitmap.rowBytes(); + } + + void InitSkBitmapFromData(SkBitmap* bitmap, const char* pixels, + size_t total_pixels) const { + if (total_pixels) { + bitmap->setConfig(fConfig, fWidth, fHeight, fRowBytes); + bitmap->allocPixels(); + memcpy(bitmap->getPixels(), pixels, total_pixels); + } + } +}; + +struct WebCursor_Data { + WebCursor::Type cursor_type; + int hotspot_x; + int hotspot_y; + SkBitmap_Data bitmap_info; +}; + +} // namespace + + +void ParamTraits<SkBitmap>::Write(Message* m, const SkBitmap& p) { + size_t fixed_size = sizeof(SkBitmap_Data); + SkBitmap_Data bmp_data; + bmp_data.InitSkBitmapDataForTransfer(p); + m->WriteData(reinterpret_cast<const char*>(&bmp_data), + static_cast<int>(fixed_size)); + size_t pixel_size = p.getSize(); + SkAutoLockPixels p_lock(p); + m->WriteData(reinterpret_cast<const char*>(p.getPixels()), + static_cast<int>(pixel_size)); +} + +bool ParamTraits<SkBitmap>::Read(const Message* m, void** iter, SkBitmap* r) { + const char* fixed_data; + int fixed_data_size = 0; + if (!m->ReadData(iter, &fixed_data, &fixed_data_size) || + (fixed_data_size <= 0)) { + NOTREACHED(); + return false; + } + if (fixed_data_size != sizeof(SkBitmap_Data)) + return false; // Message is malformed. + + const char* variable_data; + int variable_data_size = 0; + if (!m->ReadData(iter, &variable_data, &variable_data_size) || + (variable_data_size < 0)) { + NOTREACHED(); + return false; + } + const SkBitmap_Data* bmp_data = + reinterpret_cast<const SkBitmap_Data*>(fixed_data); + bmp_data->InitSkBitmapFromData(r, variable_data, variable_data_size); + return true; +} + +void ParamTraits<SkBitmap>::Log(const SkBitmap& p, std::wstring* l) { + l->append(StringPrintf(L"<SkBitmap>")); +} + + void ParamTraits<GURL>::Write(Message* m, const GURL& p) { m->WriteString(p.possibly_invalid_spec()); // TODO(brettw) bug 684583: Add encoding for query params. @@ -77,7 +161,6 @@ void ParamTraits<gfx::Point>::Log(const gfx::Point& p, std::wstring* l) { } - void ParamTraits<gfx::Rect>::Write(Message* m, const gfx::Rect& p) { m->WriteInt(p.x()); m->WriteInt(p.y()); @@ -100,7 +183,8 @@ bool ParamTraits<gfx::Rect>::Read(const Message* m, void** iter, gfx::Rect* r) { } void ParamTraits<gfx::Rect>::Log(const gfx::Rect& p, std::wstring* l) { - l->append(StringPrintf(L"(%d, %d, %d, %d)", p.x(), p.y(), p.width(), p.height())); + l->append(StringPrintf(L"(%d, %d, %d, %d)", p.x(), p.y(), + p.width(), p.height())); } @@ -124,13 +208,6 @@ void ParamTraits<gfx::Size>::Log(const gfx::Size& p, std::wstring* l) { } -struct WebCursor_Data { - WebCursor::Type cursor_type; - int hotspot_x; - int hotspot_y; - SkBitmap_Data bitmap_info; -}; - void ParamTraits<WebCursor>::Write(Message* m, const WebCursor& p) { const SkBitmap& src_bitmap = p.bitmap(); WebCursor_Data web_cursor_info; diff --git a/chrome/common/ipc_message_utils.h b/chrome/common/ipc_message_utils.h index 263a877..3361467 100644 --- a/chrome/common/ipc_message_utils.h +++ b/chrome/common/ipc_message_utils.h @@ -27,8 +27,8 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#ifndef CHROME_COMMON_IPC_MESSAGE_UTILS_H__ -#define CHROME_COMMON_IPC_MESSAGE_UTILS_H__ +#ifndef CHROME_COMMON_IPC_MESSAGE_UTILS_H_ +#define CHROME_COMMON_IPC_MESSAGE_UTILS_H_ #include <string> #include <vector> @@ -38,13 +38,13 @@ #include "base/tuple.h" #include "chrome/common/ipc_sync_message.h" #include "chrome/common/thumbnail_score.h" -#include "skia/include/SkBitmap.h" #include "webkit/glue/cache_manager.h" #include "webkit/glue/console_message_level.h" #include "webkit/glue/window_open_disposition.h" // Forward declarations. class GURL; +class SkBitmap; class WebCursor; namespace gfx { @@ -73,7 +73,7 @@ void RegisterMessageLogger(int msg_start, LogFunction* func); class MessageIterator { public: - MessageIterator(const Message& m) : msg_(m), iter_(NULL) { + explicit MessageIterator(const Message& m) : msg_(m), iter_(NULL) { } int NextInt() const { int val; @@ -203,14 +203,14 @@ template <> struct ParamTraits<double> { typedef double param_type; static void Write(Message* m, const param_type& p) { - m->WriteData(reinterpret_cast<const char*>(&p), sizeof(double)); + m->WriteData(reinterpret_cast<const char*>(&p), sizeof(param_type)); } static bool Read(const Message* m, void** iter, param_type* r) { const char *data; int data_size = 0; bool result = m->ReadData(iter, &data, &data_size); - if (result && data_size == sizeof(double)) { - memcpy(r, data, sizeof(double)); + if (result && data_size == sizeof(param_type)) { + memcpy(r, data, sizeof(param_type)); } else { result = false; NOTREACHED(); @@ -227,14 +227,14 @@ template <> struct ParamTraits<wchar_t> { typedef wchar_t param_type; static void Write(Message* m, const param_type& p) { - m->WriteData(reinterpret_cast<const char*>(&p), sizeof(wchar_t)); + m->WriteData(reinterpret_cast<const char*>(&p), sizeof(param_type)); } static bool Read(const Message* m, void** iter, param_type* r) { const char *data; int data_size = 0; bool result = m->ReadData(iter, &data, &data_size); - if (result && data_size == sizeof(wchar_t)) { - memcpy(r, data, sizeof(wchar_t)); + if (result && data_size == sizeof(param_type)) { + memcpy(r, data, sizeof(param_type)); } else { result = false; NOTREACHED(); @@ -310,78 +310,16 @@ struct ParamTraits<MSG> { } }; -struct SkBitmap_Data { - // The configuration for the bitmap (bits per pixel, etc). - SkBitmap::Config fConfig; - - // The width of the bitmap in pixels. - uint32 fWidth; - - // The height of the bitmap in pixels. - uint32 fHeight; - - // The number of bytes between subsequent rows of the bitmap. - uint32 fRowBytes; - - void InitSkBitmapDataForTransfer(const SkBitmap& bitmap) { - fConfig = bitmap.config(); - fWidth = bitmap.width(); - fHeight = bitmap.height(); - fRowBytes = bitmap.rowBytes(); - } - - void InitSkBitmapFromData(SkBitmap* bitmap, const char* pixels, - size_t total_pixels) const { - if (total_pixels) { - bitmap->setConfig(fConfig, fWidth, fHeight, fRowBytes); - bitmap->allocPixels(); - memcpy(bitmap->getPixels(), pixels, total_pixels); - } - } -}; - template <> struct ParamTraits<SkBitmap> { typedef SkBitmap param_type; - static void Write(Message* m, const param_type& p) { - size_t fixed_size = sizeof(SkBitmap_Data); - SkBitmap_Data bmp_data; - bmp_data.InitSkBitmapDataForTransfer(p); - m->WriteData(reinterpret_cast<const char*>(&bmp_data), - static_cast<int>(fixed_size)); - size_t pixel_size = p.getSize(); - SkAutoLockPixels p_lock(p); - m->WriteData(reinterpret_cast<const char*>(p.getPixels()), - static_cast<int>(pixel_size)); - } + static void Write(Message* m, const param_type& p); + // Note: This function expects parameter |r| to be of type &SkBitmap since // r->SetConfig() and r->SetPixels() are called. - static bool Read(const Message* m, void** iter, param_type* r) { - const char* fixed_data; - int fixed_data_size = 0; - if (!m->ReadData(iter, &fixed_data, &fixed_data_size) || - (fixed_data_size <= 0)) { - NOTREACHED(); - return false; - } - if (fixed_data_size != sizeof(SkBitmap_Data)) - return false; // Message is malformed. + static bool Read(const Message* m, void** iter, param_type* r); - const char* variable_data; - int variable_data_size = 0; - if (!m->ReadData(iter, &variable_data, &variable_data_size) || - (variable_data_size < 0)) { - NOTREACHED(); - return false; - } - const SkBitmap_Data* bmp_data = - reinterpret_cast<const SkBitmap_Data*>(fixed_data); - bmp_data->InitSkBitmapFromData(r, variable_data, variable_data_size); - return true; - } - static void Log(const param_type& p, std::wstring* l) { - l->append(StringPrintf(L"<SkBitmap>")); - } + static void Log(const param_type& p, std::wstring* l); }; template <> @@ -856,8 +794,10 @@ struct LogData { uint16 type; std::wstring flags; int64 sent; // Time that the message was sent (i.e. at Send()). - int64 receive; // Time before it was dispatched (i.e. before calling OnMessageReceived). - int64 dispatch; // Time after it was dispatched (i.e. after calling OnMessageReceived). + int64 receive; // Time before it was dispatched (i.e. before calling + // OnMessageReceived). + int64 dispatch; // Time after it was dispatched (i.e. after calling + // OnMessageReceived). std::wstring params; }; @@ -1123,7 +1063,7 @@ class MessageWithTuple : public Message { template <class RefTuple> class ParamDeserializer : public MessageReplyDeserializer { public: - ParamDeserializer(const RefTuple& out) : out_(out) { } + explicit ParamDeserializer(const RefTuple& out) : out_(out) { } bool SerializeOutputParameters(const IPC::Message& msg, void* iter) { return ReadParam(&msg, &iter, &out_); @@ -1263,4 +1203,4 @@ class MessageWithReply : public SyncMessage { } // namespace IPC -#endif // CHROME_COMMON_IPC_MESSAGE_UTILS_H__ +#endif // CHROME_COMMON_IPC_MESSAGE_UTILS_H_ diff --git a/chrome/common/sqlite_compiled_statement.cc b/chrome/common/sqlite_compiled_statement.cc index 3916d14..a580a29 100644 --- a/chrome/common/sqlite_compiled_statement.cc +++ b/chrome/common/sqlite_compiled_statement.cc @@ -27,9 +27,11 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include "chrome/common/stl_util-inl.h" #include "chrome/common/sqlite_compiled_statement.h" +#include "base/logging.h" +#include "chrome/common/stl_util-inl.h" + // SqliteStatementCache ------------------------------------------------------- SqliteStatementCache::~SqliteStatementCache() { @@ -38,6 +40,11 @@ SqliteStatementCache::~SqliteStatementCache() { db_ = NULL; } +void SqliteStatementCache::set_db(sqlite3* db) { + DCHECK(!db_) << "Setting the database twice"; + db_ = db; +} + SQLStatement* SqliteStatementCache::InternalGetStatement(const char* func_name, int func_number, const char* sql) { @@ -86,3 +93,16 @@ SqliteCompiledStatement::~SqliteCompiledStatement() { if (statement_) statement_->reset(); } + +SQLStatement& SqliteCompiledStatement::operator*() { + DCHECK(statement_) << "Should check is_valid() before using the statement."; + return *statement_; +} +SQLStatement* SqliteCompiledStatement::operator->() { + DCHECK(statement_) << "Should check is_valid() before using the statement."; + return statement_; +} +SQLStatement* SqliteCompiledStatement::statement() { + DCHECK(statement_) << "Should check is_valid() before using the statement."; + return statement_; +} diff --git a/chrome/common/sqlite_compiled_statement.h b/chrome/common/sqlite_compiled_statement.h index ad12b45..376e991 100644 --- a/chrome/common/sqlite_compiled_statement.h +++ b/chrome/common/sqlite_compiled_statement.h @@ -27,13 +27,12 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#ifndef CHROME_COMMON_SQLITE_COMPILED_STATEMENT__ -#define CHROME_COMMON_SQLITE_COMPILED_STATEMENT__ +#ifndef CHROME_COMMON_SQLITE_COMPILED_STATEMENT_ +#define CHROME_COMMON_SQLITE_COMPILED_STATEMENT_ #include <map> #include <string> -#include "base/logging.h" #include "chrome/common/sqlite_utils.h" #include "chrome/third_party/sqlite/sqlite3.h" @@ -47,7 +46,7 @@ class SqliteStatementCache { SqliteStatementCache() : db_(NULL) { } - SqliteStatementCache(sqlite3* db) : db_(db) { + explicit SqliteStatementCache(sqlite3* db) : db_(db) { } // This object must be deleted before the sqlite connection it is associated @@ -55,10 +54,7 @@ class SqliteStatementCache { // statements. ~SqliteStatementCache(); - void set_db(sqlite3* db) { - DCHECK(!db_) << "Setting the database twice"; - db_ = db; - } + void set_db(sqlite3* db); // Creates or retrieves a cached SQL statement identified by the given // (name, number) pair. @@ -135,18 +131,9 @@ class SqliteCompiledStatement { // Allow accessing this object to be like accessing a statement for // convenience. The caller must ensure the statement is_valid() before using // these two functions. - SQLStatement& operator*() { - DCHECK(statement_) << "Should check is_valid() before using the statement."; - return *statement_; - } - SQLStatement* operator->() { - DCHECK(statement_) << "Should check is_valid() before using the statement."; - return statement_; - } - SQLStatement* statement() { - DCHECK(statement_) << "Should check is_valid() before using the statement."; - return statement_; - } + SQLStatement& operator*(); + SQLStatement* operator->(); + SQLStatement* statement(); private: // The sql statement if valid, NULL if not valid. This pointer is NOT owned @@ -168,4 +155,4 @@ class SqliteCompiledStatement { #define SQLITE_UNIQUE_STATEMENT(var_name, cache, sql) \ SqliteCompiledStatement var_name(__FILE__, __LINE__, cache, sql) -#endif // CHROME_COMMON_SQLITE_COMPILED_STATEMENT__ +#endif // CHROME_COMMON_SQLITE_COMPILED_STATEMENT_ diff --git a/chrome/common/sqlite_utils.cc b/chrome/common/sqlite_utils.cc index cadb609..26292c1 100644 --- a/chrome/common/sqlite_utils.cc +++ b/chrome/common/sqlite_utils.cc @@ -29,6 +29,8 @@ #include "chrome/common/sqlite_utils.h" +#include "base/logging.h" + bool DoesSqliteTableExist(sqlite3* db, const char* db_name, const char* table_name) { @@ -93,3 +95,339 @@ bool DoesSqliteTableHaveRow(sqlite3* db, const char* table_name) { return s.step() == SQLITE_ROW; } + + +SQLTransaction::SQLTransaction(sqlite3* db) : db_(db), began_(false) { +} + +SQLTransaction::~SQLTransaction() { + if (began_) { + Rollback(); + } +} + +int SQLTransaction::BeginCommand(const char* command) { + int rv = SQLITE_ERROR; + if (!began_ && db_) { + rv = sqlite3_exec(db_, command, NULL, NULL, NULL); + began_ = (rv == SQLITE_OK); + } + return rv; +} + +int SQLTransaction::EndCommand(const char* command) { + int rv = SQLITE_ERROR; + if (began_ && db_) { + rv = sqlite3_exec(db_, command, NULL, NULL, NULL); + began_ = (rv != SQLITE_OK); + } + return rv; +} + +SQLNestedTransactionSite::~SQLNestedTransactionSite() { + DCHECK(!top_transaction_); +} + +void SQLNestedTransactionSite::SetTopTransaction(SQLNestedTransaction* top) { + DCHECK(!top || !top_transaction_); + top_transaction_ = top; +} + +SQLNestedTransaction::SQLNestedTransaction(SQLNestedTransactionSite* site) + : SQLTransaction(site->GetSqlite3DB()), + needs_rollback_(false), + site_(site) { + DCHECK(site); + if (site->GetTopTransaction() == NULL) { + site->SetTopTransaction(this); + } +} + +SQLNestedTransaction::~SQLNestedTransaction() { + if (began_) { + Rollback(); + } + if (site_->GetTopTransaction() == this) { + site_->SetTopTransaction(NULL); + } +} + +int SQLNestedTransaction::BeginCommand(const char* command) { + DCHECK(db_); + DCHECK(site_ && site_->GetTopTransaction()); + if (!db_ || began_) { + return SQLITE_ERROR; + } + if (site_->GetTopTransaction() == this) { + int rv = sqlite3_exec(db_, command, NULL, NULL, NULL); + began_ = (rv == SQLITE_OK); + if (began_) { + site_->OnBegin(); + } + return rv; + } else { + if (site_->GetTopTransaction()->needs_rollback_) { + return SQLITE_ERROR; + } + began_ = true; + return SQLITE_OK; + } +} + +int SQLNestedTransaction::EndCommand(const char* command) { + DCHECK(db_); + DCHECK(site_ && site_->GetTopTransaction()); + if (!db_ || !began_) { + return SQLITE_ERROR; + } + if (site_->GetTopTransaction() == this) { + if (needs_rollback_) { + sqlite3_exec(db_, "ROLLBACK", NULL, NULL, NULL); + began_ = false; // reset so we don't try to rollback or call + // OnRollback() again + site_->OnRollback(); + return SQLITE_ERROR; + } else { + int rv = sqlite3_exec(db_, command, NULL, NULL, NULL); + began_ = (rv != SQLITE_OK); + if (strcmp(command, "ROLLBACK") == 0) { + began_ = false; // reset so we don't try to rollbck or call + // OnRollback() again + site_->OnRollback(); + } else { + DCHECK(strcmp(command, "COMMIT") == 0); + if (rv == SQLITE_OK) { + site_->OnCommit(); + } + } + return rv; + } + } else { + if (strcmp(command, "ROLLBACK") == 0) { + site_->GetTopTransaction()->needs_rollback_ = true; + } + began_ = false; + return SQLITE_OK; + } +} + +int SQLStatement::prepare(sqlite3* db, const char* sql, int sql_len) { + DCHECK(!stmt_); + int rv = sqlite3_prepare_v2(db, sql, sql_len, &stmt_, NULL); + if (rv != SQLITE_OK) { + DLOG(ERROR) << "SQLStatement.prepare_v2 failed: " << sqlite3_errmsg(db); + } + return rv; +} + +int SQLStatement::prepare16(sqlite3* db, const wchar_t* sql, int sql_len) { + DCHECK(!stmt_); + sql_len *= sizeof(wchar_t); + int rv = sqlite3_prepare16_v2(db, sql, sql_len, &stmt_, NULL); + if (rv != SQLITE_OK) { + DLOG(ERROR) << "SQLStatement.prepare16_v2 failed: " << sqlite3_errmsg(db); + } + return rv; +} + +int SQLStatement::step() { + DCHECK(stmt_); + return sqlite3_step(stmt_); +} + +int SQLStatement::reset() { + DCHECK(stmt_); + return sqlite3_reset(stmt_); +} + +sqlite_int64 SQLStatement::last_insert_rowid() { + DCHECK(stmt_); + return sqlite3_last_insert_rowid(db_handle()); +} + +sqlite3* SQLStatement::db_handle() { + DCHECK(stmt_); + return sqlite3_db_handle(stmt_); +} + +int SQLStatement::bind_parameter_count() { + DCHECK(stmt_); + return sqlite3_bind_parameter_count(stmt_); +} + +int SQLStatement::bind_blob(int index, std::vector<unsigned char>* blob) { + if (blob) { + const void* value = &(*blob)[0]; + int len = static_cast<int>(blob->size()); + return bind_blob(index, value, len); + } else { + return bind_null(index); + } +} + +int SQLStatement::bind_blob(int index, const void* value, int value_len) { + return bind_blob(index, value, value_len, SQLITE_TRANSIENT); +} + +int SQLStatement::bind_blob(int index, const void* value, int value_len, + Function dtor) { + DCHECK(stmt_); + return sqlite3_bind_blob(stmt_, index + 1, value, value_len, dtor); +} + +int SQLStatement::bind_double(int index, double value) { + DCHECK(stmt_); + return sqlite3_bind_double(stmt_, index + 1, value); +} + +int SQLStatement::bind_bool(int index, bool value) { + DCHECK(stmt_); + return sqlite3_bind_int(stmt_, index + 1, value); +} + +int SQLStatement::bind_int(int index, int value) { + DCHECK(stmt_); + return sqlite3_bind_int(stmt_, index + 1, value); +} + +int SQLStatement::bind_int64(int index, sqlite_int64 value) { + DCHECK(stmt_); + return sqlite3_bind_int64(stmt_, index + 1, value); +} + +int SQLStatement::bind_null(int index) { + DCHECK(stmt_); + return sqlite3_bind_null(stmt_, index + 1); +} + +int SQLStatement::bind_text(int index, const char* value, int value_len, + Function dtor) { + DCHECK(stmt_); + return sqlite3_bind_text(stmt_, index + 1, value, value_len, dtor); +} + +int SQLStatement::bind_text16(int index, const wchar_t* value, int value_len, + Function dtor) { + DCHECK(stmt_); + value_len *= sizeof(wchar_t); + return sqlite3_bind_text16(stmt_, index + 1, value, value_len, dtor); +} + +int SQLStatement::bind_value(int index, const sqlite3_value* value) { + DCHECK(stmt_); + return sqlite3_bind_value(stmt_, index + 1, value); +} + +int SQLStatement::column_count() { + DCHECK(stmt_); + return sqlite3_column_count(stmt_); +} + +int SQLStatement::column_type(int index) { + DCHECK(stmt_); + return sqlite3_column_type(stmt_, index); +} + +const wchar_t* SQLStatement::column_name16(int index) { + DCHECK(stmt_); + return static_cast<const wchar_t*>( sqlite3_column_name16(stmt_, index) ); +} + +const void* SQLStatement::column_blob(int index) { + DCHECK(stmt_); + return sqlite3_column_blob(stmt_, index); +} + +bool SQLStatement::column_blob_as_vector(int index, + std::vector<unsigned char>* blob) { + DCHECK(stmt_); + const void* p = column_blob(index); + size_t len = column_bytes(index); + blob->resize(len); + if (blob->size() != len) { + return false; + } + if (len > 0) + memcpy(&(blob->front()), p, len); + return true; +} + +bool SQLStatement::column_blob_as_string(int index, std::string* blob) { + DCHECK(stmt_); + const void* p = column_blob(index); + size_t len = column_bytes(index); + blob->resize(len); + if (blob->size() != len) { + return false; + } + blob->assign(reinterpret_cast<const char*>(p), len); + return true; +} + +int SQLStatement::column_bytes(int index) { + DCHECK(stmt_); + return sqlite3_column_bytes(stmt_, index); +} + +int SQLStatement::column_bytes16(int index) { + DCHECK(stmt_); + return sqlite3_column_bytes16(stmt_, index); +} + +double SQLStatement::column_double(int index) { + DCHECK(stmt_); + return sqlite3_column_double(stmt_, index); +} + +bool SQLStatement::column_bool(int index) { + DCHECK(stmt_); + return sqlite3_column_int(stmt_, index) ? true : false; +} + +int SQLStatement::column_int(int index) { + DCHECK(stmt_); + return sqlite3_column_int(stmt_, index); +} + +sqlite_int64 SQLStatement::column_int64(int index) { + DCHECK(stmt_); + return sqlite3_column_int64(stmt_, index); +} + +const char* SQLStatement::column_text(int index) { + DCHECK(stmt_); + return reinterpret_cast<const char*>(sqlite3_column_text(stmt_, index)); +} + +bool SQLStatement::column_string(int index, std::string* str) { + DCHECK(stmt_); + DCHECK(str); + const char* s = column_text(index); +str->assign(s ? s : std::string("")); + return s != NULL; +} + +std::string SQLStatement::column_string(int index) { + std::string str; + column_string(index, &str); + return str; +} + +const wchar_t* SQLStatement::column_text16(int index) { + DCHECK(stmt_); + return static_cast<const wchar_t*>( sqlite3_column_text16(stmt_, index) ); +} + +bool SQLStatement::column_string16(int index, std::wstring* str) { + DCHECK(stmt_); + DCHECK(str); + const wchar_t* s = column_text16(index); + str->assign(s ? s : std::wstring(L"")); + return (s != NULL); +} + +std::wstring SQLStatement::column_string16(int index) { + std::wstring wstr; + column_string16(index, &wstr); + return wstr; +} diff --git a/chrome/common/sqlite_utils.h b/chrome/common/sqlite_utils.h index 8b0d529..d5c87fd 100644 --- a/chrome/common/sqlite_utils.h +++ b/chrome/common/sqlite_utils.h @@ -27,12 +27,12 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#ifndef CHROME_COMMON_SQLITEUTILS_H__ -#define CHROME_COMMON_SQLITEUTILS_H__ +#ifndef CHROME_COMMON_SQLITEUTILS_H_ +#define CHROME_COMMON_SQLITEUTILS_H_ #include <vector> -#include "base/logging.h" +#include "base/basictypes.h" #include "chrome/third_party/sqlite/sqlite3.h" // forward declarations of classes defined here @@ -49,16 +49,8 @@ class SQLStatement; //------------------------------------------------------------------------------ class SQLTransaction { public: - SQLTransaction(sqlite3 *db) { - db_ = db; - began_ = false; - } - - virtual ~SQLTransaction() { - if (began_) { - Rollback(); - } - } + explicit SQLTransaction(sqlite3* db); + virtual ~SQLTransaction(); int Begin() { // By default, we BEGIN IMMEDIATE to establish file locks at the @@ -93,27 +85,12 @@ class SQLTransaction { } protected: - virtual int BeginCommand(const char *command) { - int rv = SQLITE_ERROR; - if (!began_ && db_) { - rv = sqlite3_exec(db_, command, NULL, NULL, NULL); - began_ = (rv == SQLITE_OK); - } - return rv; - } - - virtual int EndCommand(const char *command) { - int rv = SQLITE_ERROR; - if (began_ && db_) { - rv = sqlite3_exec(db_, command, NULL, NULL, NULL); - began_ = (rv != SQLITE_OK); - } - return rv; - } + virtual int BeginCommand(const char* command); + virtual int EndCommand(const char* command); bool began_; - sqlite3 *db_; - DISALLOW_EVIL_CONSTRUCTORS(SQLTransaction); + sqlite3* db_; + DISALLOW_COPY_AND_ASSIGN(SQLTransaction); }; @@ -123,7 +100,7 @@ class SQLTransaction { class SQLNestedTransactionSite { protected: SQLNestedTransactionSite() : db_(NULL), top_transaction_(NULL) {} - virtual ~SQLNestedTransactionSite() { DCHECK(!top_transaction_); } + virtual ~SQLNestedTransactionSite(); // The following virtual methods provide notification of true transaction // boundaries as they are crossed by a top nested transaction. @@ -141,19 +118,16 @@ class SQLNestedTransactionSite { // Returns the current top nested transaction associated with this site // Used by SQLNestedTransaction - SQLNestedTransaction *GetTopTransaction() { + SQLNestedTransaction* GetTopTransaction() { return top_transaction_; } // Sets or clears the top nested transaction associated with this site // Used by SQLNestedTransaction - void SetTopTransaction(SQLNestedTransaction *top) { - DCHECK(!top || !top_transaction_); - top_transaction_ = top; - } + void SetTopTransaction(SQLNestedTransaction* top); sqlite3* db_; - SQLNestedTransaction *top_transaction_; + SQLNestedTransaction* top_transaction_; friend class SQLNestedTransaction; }; @@ -179,89 +153,17 @@ class SQLNestedTransactionSite { //------------------------------------------------------------------------------ class SQLNestedTransaction : public SQLTransaction { public: - SQLNestedTransaction(SQLNestedTransactionSite *site) - : SQLTransaction(site->GetSqlite3DB()), - needs_rollback_(false), - site_(site) { - DCHECK(site); - if (site->GetTopTransaction() == NULL) { - site->SetTopTransaction(this); - } - } - - virtual ~SQLNestedTransaction() { - if (began_) { - Rollback(); - } - if (site_->GetTopTransaction() == this) { - site_->SetTopTransaction(NULL); - } - } + explicit SQLNestedTransaction(SQLNestedTransactionSite* site); + virtual ~SQLNestedTransaction(); protected: - virtual int BeginCommand(const char *command) { - DCHECK(db_); - DCHECK(site_ && site_->GetTopTransaction()); - if (!db_ || began_) { - return SQLITE_ERROR; - } - if (site_->GetTopTransaction() == this) { - int rv = sqlite3_exec(db_, command, NULL, NULL, NULL); - began_ = (rv == SQLITE_OK); - if (began_) { - site_->OnBegin(); - } - return rv; - } else { - if (site_->GetTopTransaction()->needs_rollback_) { - return SQLITE_ERROR; - } - began_ = true; - return SQLITE_OK; - } - } - - virtual int EndCommand(const char *command) { - DCHECK(db_); - DCHECK(site_ && site_->GetTopTransaction()); - if (!db_ || !began_) { - return SQLITE_ERROR; - } - if (site_->GetTopTransaction() == this) { - if (needs_rollback_) { - sqlite3_exec(db_, "ROLLBACK", NULL, NULL, NULL); - began_ = false; // reset so we don't try to rollback or call - // OnRollback() again - site_->OnRollback(); - return SQLITE_ERROR; - } else { - int rv = sqlite3_exec(db_, command, NULL, NULL, NULL); - began_ = (rv != SQLITE_OK); - if (strcmp(command, "ROLLBACK") == 0) { - began_ = false; // reset so we don't try to rollbck or call - // OnRollback() again - site_->OnRollback(); - } else { - DCHECK(strcmp(command, "COMMIT") == 0); - if (rv == SQLITE_OK) { - site_->OnCommit(); - } - } - return rv; - } - } else { - if (strcmp(command, "ROLLBACK") == 0) { - site_->GetTopTransaction()->needs_rollback_ = true; - } - began_ = false; - return SQLITE_OK; - } - } + virtual int BeginCommand(const char* command); + virtual int EndCommand(const char* command); private: bool needs_rollback_; - SQLNestedTransactionSite *site_; - DISALLOW_EVIL_CONSTRUCTORS(SQLNestedTransaction); + SQLNestedTransactionSite* site_; + DISALLOW_COPY_AND_ASSIGN(SQLNestedTransaction); }; //------------------------------------------------------------------------------ @@ -276,21 +178,21 @@ class scoped_sqlite3_stmt_ptr { scoped_sqlite3_stmt_ptr() : stmt_(NULL) { } - explicit scoped_sqlite3_stmt_ptr(sqlite3_stmt *stmt) + explicit scoped_sqlite3_stmt_ptr(sqlite3_stmt* stmt) : stmt_(stmt) { } - sqlite3_stmt *get() const { + sqlite3_stmt* get() const { return stmt_; } - void set(sqlite3_stmt *stmt) { + void set(sqlite3_stmt* stmt) { finalize(); stmt_ = stmt; } - sqlite3_stmt *release() { - sqlite3_stmt *tmp = stmt_; + sqlite3_stmt* release() { + sqlite3_stmt* tmp = stmt_; stmt_ = NULL; return tmp; } @@ -309,10 +211,10 @@ class scoped_sqlite3_stmt_ptr { } protected: - sqlite3_stmt *stmt_; + sqlite3_stmt* stmt_; private: - DISALLOW_EVIL_CONSTRUCTORS(scoped_sqlite3_stmt_ptr); + DISALLOW_COPY_AND_ASSIGN(scoped_sqlite3_stmt_ptr); }; @@ -320,113 +222,44 @@ class scoped_sqlite3_stmt_ptr { // A scoped sqlite statement with convenient C++ wrappers for sqlite3 APIs. //------------------------------------------------------------------------------ class SQLStatement : public scoped_sqlite3_stmt_ptr { -public: + public: SQLStatement() { } - int prepare(sqlite3 *db, const char *sql) { + int prepare(sqlite3* db, const char* sql) { return prepare(db, sql, -1); } - int prepare(sqlite3 *db, const char *sql, int sql_len) { - DCHECK(!stmt_); - int rv = sqlite3_prepare_v2(db, sql, sql_len, &stmt_, NULL); - if (rv != SQLITE_OK) { - DLOG(ERROR) << "SQLStatement.prepare_v2 failed: " << sqlite3_errmsg(db); - } - return rv; - } + int prepare(sqlite3* db, const char* sql, int sql_len); - int prepare16(sqlite3 *db, const wchar_t *sql) { + int prepare16(sqlite3* db, const wchar_t* sql) { return prepare16(db, sql, -1); } // sql_len is number of characters or may be negative // a for null-terminated sql string - int prepare16(sqlite3 *db, const wchar_t *sql, int sql_len) { - DCHECK(!stmt_); - sql_len *= sizeof(wchar_t); - int rv = sqlite3_prepare16_v2(db, sql, sql_len, &stmt_, NULL); - if (rv != SQLITE_OK) { - DLOG(ERROR) << "SQLStatement.prepare16_v2 failed: " << sqlite3_errmsg(db); - } - return rv; - } - - int step() { - DCHECK(stmt_); - return sqlite3_step(stmt_); - } - - int reset() { - DCHECK(stmt_); - return sqlite3_reset(stmt_); - } - - sqlite_int64 last_insert_rowid() { - DCHECK(stmt_); - return sqlite3_last_insert_rowid(db_handle()); - } - - sqlite3 *db_handle() { - DCHECK(stmt_); - return sqlite3_db_handle(stmt_); - } + int prepare16(sqlite3* db, const wchar_t* sql, int sql_len); + int step(); + int reset(); + sqlite_int64 last_insert_rowid(); + sqlite3* db_handle(); // // Parameter binding helpers (NOTE: index is 0-based) // - int bind_parameter_count() { - DCHECK(stmt_); - return sqlite3_bind_parameter_count(stmt_); - } + int bind_parameter_count(); typedef void (*Function)(void*); - int bind_blob(int index, std::vector<unsigned char> *blob) { - if (blob) { - const void *value = &(*blob)[0]; - int len = static_cast<int>(blob->size()); - return bind_blob(index, value, len); - } else { - return bind_null(index); - } - } - - int bind_blob(int index, const void *value, int value_len) { - return bind_blob(index, value, value_len, SQLITE_TRANSIENT); - } - - int bind_blob(int index, const void *value, int value_len, Function dtor) { - DCHECK(stmt_); - return sqlite3_bind_blob(stmt_, index + 1, value, value_len, dtor); - } - - int bind_double(int index, double value) { - DCHECK(stmt_); - return sqlite3_bind_double(stmt_, index + 1, value); - } - - int bind_bool(int index, bool value) { - DCHECK(stmt_); - return sqlite3_bind_int(stmt_, index + 1, value); - } - - int bind_int(int index, int value) { - DCHECK(stmt_); - return sqlite3_bind_int(stmt_, index + 1, value); - } - - int bind_int64(int index, sqlite_int64 value) { - DCHECK(stmt_); - return sqlite3_bind_int64(stmt_, index + 1, value); - } - - int bind_null(int index) { - DCHECK(stmt_); - return sqlite3_bind_null(stmt_, index + 1); - } + int bind_blob(int index, std::vector<unsigned char>* blob); + int bind_blob(int index, const void* value, int value_len); + int bind_blob(int index, const void* value, int value_len, Function dtor); + int bind_double(int index, double value); + int bind_bool(int index, bool value); + int bind_int(int index, int value); + int bind_int64(int index, sqlite_int64 value); + int bind_null(int index); int bind_string(int index, const std::string& value) { // don't use c_str so it doesn't have to fix up the null terminator @@ -442,166 +275,63 @@ public: static_cast<int>(value.length()), SQLITE_TRANSIENT); } - int bind_text(int index, const char *value) { + int bind_text(int index, const char* value) { return bind_text(index, value, -1, SQLITE_TRANSIENT); } // value_len is number of characters or may be negative // a for null-terminated value string - int bind_text(int index, const char *value, int value_len) { + int bind_text(int index, const char* value, int value_len) { return bind_text(index, value, value_len, SQLITE_TRANSIENT); } // value_len is number of characters or may be negative // a for null-terminated value string - int bind_text(int index, const char *value, int value_len, - Function dtor) { - DCHECK(stmt_); - return sqlite3_bind_text(stmt_, index + 1, value, value_len, dtor); - } + int bind_text(int index, const char* value, int value_len, + Function dtor); - int bind_text16(int index, const wchar_t *value) { + int bind_text16(int index, const wchar_t* value) { return bind_text16(index, value, -1, SQLITE_TRANSIENT); } // value_len is number of characters or may be negative // a for null-terminated value string - int bind_text16(int index, const wchar_t *value, int value_len) { + int bind_text16(int index, const wchar_t* value, int value_len) { return bind_text16(index, value, value_len, SQLITE_TRANSIENT); } // value_len is number of characters or may be negative // a for null-terminated value string - int bind_text16(int index, const wchar_t *value, int value_len, - Function dtor) { - DCHECK(stmt_); - value_len *= sizeof(wchar_t); - return sqlite3_bind_text16(stmt_, index + 1, value, value_len, dtor); - } + int bind_text16(int index, const wchar_t* value, int value_len, + Function dtor); - int bind_value(int index, const sqlite3_value *value) { - DCHECK(stmt_); - return sqlite3_bind_value(stmt_, index + 1, value); - } + int bind_value(int index, const sqlite3_value* value); // // Column helpers (NOTE: index is 0-based) // - int column_count() { - DCHECK(stmt_); - return sqlite3_column_count(stmt_); - } - - int column_type(int index) { - DCHECK(stmt_); - return sqlite3_column_type(stmt_, index); - } - - const wchar_t *column_name16(int index) { - DCHECK(stmt_); - return static_cast<const wchar_t*>( sqlite3_column_name16(stmt_, index) ); - } - - const void *column_blob(int index) { - DCHECK(stmt_); - return sqlite3_column_blob(stmt_, index); - } - - bool column_blob_as_vector(int index, std::vector<unsigned char> *blob) { - DCHECK(stmt_); - const void *p = column_blob(index); - size_t len = column_bytes(index); - blob->resize(len); - if (blob->size() != len) { - return false; - } - if (len > 0) - memcpy(&(blob->front()), p, len); - return true; - } - - bool column_blob_as_string(int index, std::string* blob) { - DCHECK(stmt_); - const void *p = column_blob(index); - size_t len = column_bytes(index); - blob->resize(len); - if (blob->size() != len) { - return false; - } - blob->assign(reinterpret_cast<const char*>(p), len); - return true; - } - - int column_bytes(int index) { - DCHECK(stmt_); - return sqlite3_column_bytes(stmt_, index); - } - - int column_bytes16(int index) { - DCHECK(stmt_); - return sqlite3_column_bytes16(stmt_, index); - } + int column_count(); + int column_type(int index); + const wchar_t* column_name16(int index); + const void* column_blob(int index); + bool column_blob_as_vector(int index, std::vector<unsigned char>* blob); + bool column_blob_as_string(int index, std::string* blob); + int column_bytes(int index); + int column_bytes16(int index); + double column_double(int index); + bool column_bool(int index); + int column_int(int index); + sqlite_int64 column_int64(int index); + const char* column_text(int index); + bool column_string(int index, std::string* str); + std::string column_string(int index); + const wchar_t* column_text16(int index); + bool column_string16(int index, std::wstring* str); + std::wstring column_string16(int index); - double column_double(int index) { - DCHECK(stmt_); - return sqlite3_column_double(stmt_, index); - } - - bool column_bool(int index) { - DCHECK(stmt_); - return sqlite3_column_int(stmt_, index) ? true : false; - } - - int column_int(int index) { - DCHECK(stmt_); - return sqlite3_column_int(stmt_, index); - } - - sqlite_int64 column_int64(int index) { - DCHECK(stmt_); - return sqlite3_column_int64(stmt_, index); - } - - const char* column_text(int index) { - DCHECK(stmt_); - return reinterpret_cast<const char*>(sqlite3_column_text(stmt_, index)); - } - - bool column_string(int index, std::string *str) { - DCHECK(stmt_); - DCHECK(str); - const char* s = column_text(index); - str->assign(s ? s : std::string("")); - return s != NULL; - } - - std::string column_string(int index) { - std::string str; - column_string(index, &str); - return str; - } - - const wchar_t *column_text16(int index) { - DCHECK(stmt_); - return static_cast<const wchar_t*>( sqlite3_column_text16(stmt_, index) ); - } - - bool column_string16(int index, std::wstring *str) { - DCHECK(stmt_); - DCHECK(str); - const wchar_t *s = column_text16(index); - str->assign(s ? s : std::wstring(L"")); - return (s != NULL); - } - - std::wstring column_string16(int index) { - std::wstring wstr; - column_string16(index, &wstr); - return wstr; - } private: - DISALLOW_EVIL_CONSTRUCTORS(SQLStatement); + DISALLOW_COPY_AND_ASSIGN(SQLStatement); }; // Returns true if there is a table with the given name in the database. @@ -638,4 +368,4 @@ inline bool DoesSqliteColumnExist(sqlite3* db, // has one or more rows and false if the table is empty or doesn't exist. bool DoesSqliteTableHaveRow(sqlite3* db, const char* table_name); -#endif // CHROME_COMMON_SQLITEUTILS_H__ +#endif // CHROME_COMMON_SQLITEUTILS_H_ |