Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Unified Diff: sql/connection.cc

Issue 10736042: [sql] Refactor to remove use of const_cast<>. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Elaborate on GetUntrackedStatement() usage. Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sql/connection.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sql/connection.cc
diff --git a/sql/connection.cc b/sql/connection.cc
index a9c4974a498d83f3d85ebe1b534556b6a8ad08e5..2d7baf25ad70279ffef454f35d29bbb850db663e 100644
--- a/sql/connection.cc
+++ b/sql/connection.cc
@@ -61,6 +61,11 @@ Connection::StatementRef::StatementRef()
stmt_(NULL) {
}
+Connection::StatementRef::StatementRef(sqlite3_stmt* stmt)
+ : connection_(NULL),
+ stmt_(stmt) {
+}
+
Connection::StatementRef::StatementRef(Connection* connection,
sqlite3_stmt* stmt)
: connection_(connection),
@@ -343,17 +348,32 @@ scoped_refptr<Connection::StatementRef> Connection::GetCachedStatement(
scoped_refptr<Connection::StatementRef> Connection::GetUniqueStatement(
const char* sql) {
if (!db_)
- return new StatementRef(this, NULL); // Return inactive statement.
+ return new StatementRef(); // Return inactive statement.
sqlite3_stmt* stmt = NULL;
if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK) {
// This is evidence of a syntax error in the incoming SQL.
DLOG(FATAL) << "SQL compile error " << GetErrorMessage();
- return new StatementRef(this, NULL);
+ return new StatementRef();
}
return new StatementRef(this, stmt);
}
+scoped_refptr<Connection::StatementRef> Connection::GetUntrackedStatement(
+ const char* sql) const {
+ if (!db_)
+ return new StatementRef(); // Return inactive statement.
+
+ sqlite3_stmt* stmt = NULL;
+ int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL);
+ if (rc != SQLITE_OK) {
+ // This is evidence of a syntax error in the incoming SQL.
+ DLOG(FATAL) << "SQL compile error " << GetErrorMessage();
+ return new StatementRef();
+ }
+ return new StatementRef(stmt);
+}
+
bool Connection::IsSQLValid(const char* sql) {
sqlite3_stmt* stmt = NULL;
if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK)
@@ -373,11 +393,8 @@ bool Connection::DoesIndexExist(const char* index_name) const {
bool Connection::DoesTableOrIndexExist(
const char* name, const char* type) const {
- // GetUniqueStatement can't be const since statements may modify the
- // database, but we know ours doesn't modify it, so the cast is safe.
- Statement statement(const_cast<Connection*>(this)->GetUniqueStatement(
- "SELECT name FROM sqlite_master "
- "WHERE type=? AND name=?"));
+ const char* kSql = "SELECT name FROM sqlite_master WHERE type=? AND name=?";
+ Statement statement(GetUntrackedStatement(kSql));
statement.BindString(0, type);
statement.BindString(1, name);
@@ -390,10 +407,7 @@ bool Connection::DoesColumnExist(const char* table_name,
sql.append(table_name);
sql.append(")");
- // Our SQL is non-mutating, so this cast is OK.
- Statement statement(const_cast<Connection*>(this)->GetUniqueStatement(
- sql.c_str()));
-
+ Statement statement(GetUntrackedStatement(sql.c_str()));
while (statement.Step()) {
if (!statement.ColumnString(1).compare(column_name))
return true;
« no previous file with comments | « sql/connection.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698