OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/sync/util/query_helpers.h" |
| 6 |
| 7 #if defined(OS_WINDOWS) |
| 8 #include <windows.h> |
| 9 #endif |
| 10 |
| 11 #include <limits> |
| 12 #include <string> |
| 13 #include <vector> |
| 14 |
| 15 #include "chrome/browser/sync/util/sync_types.h" |
| 16 |
| 17 using std::numeric_limits; |
| 18 using std::string; |
| 19 using std::vector; |
| 20 |
| 21 sqlite3_stmt* PrepareQuery(sqlite3* dbhandle, const char* query) { |
| 22 sqlite3_stmt* statement = NULL; |
| 23 const char* query_tail; |
| 24 if (SQLITE_OK != sqlite3_prepare(dbhandle, query, |
| 25 CountBytes(query), &statement, |
| 26 &query_tail)) { |
| 27 LOG(ERROR) << query << "\n" << sqlite3_errmsg(dbhandle); |
| 28 } |
| 29 return statement; |
| 30 } |
| 31 |
| 32 void ExecOrDie(sqlite3* dbhandle, const char* query) { |
| 33 return ExecOrDie(dbhandle, query, PrepareQuery(dbhandle, query)); |
| 34 } |
| 35 |
| 36 // Finalizes (deletes) the query before returning. |
| 37 void ExecOrDie(sqlite3* dbhandle, const char* query, sqlite3_stmt* statement) { |
| 38 int result = Exec(dbhandle, query, statement); |
| 39 if (SQLITE_DONE != result) { |
| 40 LOG(FATAL) << query << "\n" << sqlite3_errmsg(dbhandle); |
| 41 } |
| 42 } |
| 43 |
| 44 int Exec(sqlite3* dbhandle, const char* query) { |
| 45 return Exec(dbhandle, query, PrepareQuery(dbhandle, query)); |
| 46 } |
| 47 |
| 48 // Finalizes (deletes) the query before returning. |
| 49 int Exec(sqlite3* dbhandle, const char* query, sqlite3_stmt* statement) { |
| 50 int result; |
| 51 do { |
| 52 result = sqlite3_step(statement); |
| 53 } while (SQLITE_ROW == result); |
| 54 int finalize_result = sqlite3_finalize(statement); |
| 55 return SQLITE_OK == finalize_result ? result : finalize_result; |
| 56 } |
| 57 |
| 58 int SqliteOpen(PathString filename, sqlite3** db) { |
| 59 int result = |
| 60 #if PATHSTRING_IS_STD_STRING |
| 61 sqlite3_open |
| 62 #else |
| 63 sqlite3_open16 |
| 64 #endif |
| 65 (filename.c_str(), db); |
| 66 LOG_IF(ERROR, SQLITE_OK != result) << "Error opening " << filename << ": " |
| 67 << result; |
| 68 #ifdef OS_WINDOWS |
| 69 if (SQLITE_OK == result) { |
| 70 // Make sure we mark the db file as not indexed so since if any other app |
| 71 // opens it, it can break our db locking. |
| 72 DWORD attrs = GetFileAttributes(filename.c_str()); |
| 73 if (FILE_ATTRIBUTE_NORMAL == attrs) |
| 74 attrs = FILE_ATTRIBUTE_NOT_CONTENT_INDEXED; |
| 75 else |
| 76 attrs = attrs | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED; |
| 77 SetFileAttributes(filename.c_str(), attrs); |
| 78 } |
| 79 #endif |
| 80 // Be patient as we set pragmas. |
| 81 sqlite3_busy_timeout(*db, numeric_limits<int>::max()); |
| 82 #ifndef DISABLE_SQLITE_FULL_FSYNC |
| 83 ExecOrDie(*db, "PRAGMA fullfsync = 1"); |
| 84 #endif // DISABLE_SQLITE_FULL_FSYNC |
| 85 ExecOrDie(*db, "PRAGMA synchronous = 2"); |
| 86 sqlite3_busy_timeout(*db, 0); |
| 87 return SQLITE_OK; |
| 88 } |
| 89 |
| 90 #if !PATHSTRING_IS_STD_STRING |
| 91 sqlite3_stmt* BindArg(sqlite3_stmt* statement, const PathString& s, int index) { |
| 92 if (NULL == statement) |
| 93 return statement; |
| 94 CHECK(SQLITE_OK == sqlite3_bind_text16(statement, index, s.data(), |
| 95 CountBytes(s), SQLITE_TRANSIENT)); |
| 96 return statement; |
| 97 } |
| 98 |
| 99 sqlite3_stmt* BindArg(sqlite3_stmt* statement, const PathChar* s, int index) { |
| 100 if (NULL == statement) |
| 101 return statement; |
| 102 CHECK(SQLITE_OK == sqlite3_bind_text16(statement, |
| 103 index, |
| 104 s, |
| 105 -1, // -1 means s is zero-terminated |
| 106 SQLITE_TRANSIENT)); |
| 107 return statement; |
| 108 } |
| 109 #endif |
| 110 |
| 111 sqlite3_stmt* BindArg(sqlite3_stmt* statement, const string& s, int index) { |
| 112 if (NULL == statement) |
| 113 return statement; |
| 114 CHECK(SQLITE_OK == sqlite3_bind_text(statement, |
| 115 index, |
| 116 s.data(), |
| 117 CountBytes(s), |
| 118 SQLITE_TRANSIENT)); |
| 119 return statement; |
| 120 } |
| 121 |
| 122 sqlite3_stmt* BindArg(sqlite3_stmt* statement, const char* s, int index) { |
| 123 if (NULL == statement) |
| 124 return statement; |
| 125 CHECK(SQLITE_OK == sqlite3_bind_text(statement, |
| 126 index, |
| 127 s, |
| 128 -1, // -1 means s is zero-terminated |
| 129 SQLITE_TRANSIENT)); |
| 130 return statement; |
| 131 } |
| 132 |
| 133 sqlite3_stmt* BindArg(sqlite3_stmt* statement, int32 n, int index) { |
| 134 if (NULL == statement) |
| 135 return statement; |
| 136 CHECK(SQLITE_OK == sqlite3_bind_int(statement, index, n)); |
| 137 return statement; |
| 138 } |
| 139 |
| 140 sqlite3_stmt* BindArg(sqlite3_stmt* statement, int64 n, int index) { |
| 141 if (NULL == statement) |
| 142 return statement; |
| 143 CHECK(SQLITE_OK == sqlite3_bind_int64(statement, index, n)); |
| 144 return statement; |
| 145 } |
| 146 |
| 147 sqlite3_stmt* BindArg(sqlite3_stmt* statement, double n, int index) { |
| 148 if (NULL == statement) |
| 149 return statement; |
| 150 CHECK(SQLITE_OK == sqlite3_bind_double(statement, index, n)); |
| 151 return statement; |
| 152 } |
| 153 |
| 154 sqlite3_stmt* BindArg(sqlite3_stmt* statement, bool b, int index) { |
| 155 if (NULL == statement) |
| 156 return statement; |
| 157 int32 n = b ? 1 : 0; |
| 158 CHECK(SQLITE_OK == sqlite3_bind_int(statement, index, n)); |
| 159 return statement; |
| 160 } |
| 161 |
| 162 sqlite3_stmt* BindArg(sqlite3_stmt* statement, const vector<uint8>& v, |
| 163 int index) { |
| 164 if (NULL == statement) |
| 165 return statement; |
| 166 uint8* blob = v.empty() ? NULL : const_cast<uint8*>(&v[0]); |
| 167 CHECK(SQLITE_OK == sqlite3_bind_blob(statement, |
| 168 index, |
| 169 blob, |
| 170 v.size(), |
| 171 SQLITE_TRANSIENT)); |
| 172 return statement; |
| 173 } |
| 174 |
| 175 sqlite3_stmt* BindArg(sqlite3_stmt* statement, SqliteNullType, int index) { |
| 176 if (NULL == statement) |
| 177 return statement; |
| 178 CHECK(SQLITE_OK == sqlite3_bind_null(statement, index)); |
| 179 return statement; |
| 180 } |
| 181 |
| 182 |
| 183 #if !PATHSTRING_IS_STD_STRING |
| 184 void GetColumn(sqlite3_stmt* statement, int index, PathString* value) { |
| 185 if (sqlite3_column_type(statement, index) == SQLITE_NULL) { |
| 186 value->clear(); |
| 187 } else { |
| 188 value->assign( |
| 189 static_cast<const PathChar*>(sqlite3_column_text16(statement, index)), |
| 190 sqlite3_column_bytes16(statement, index) / sizeof(PathChar)); |
| 191 } |
| 192 } |
| 193 #endif |
| 194 |
| 195 void GetColumn(sqlite3_stmt* statement, int index, string* value) { |
| 196 if (sqlite3_column_type(statement, index) == SQLITE_NULL) { |
| 197 value->clear(); |
| 198 } else { |
| 199 value->assign( |
| 200 reinterpret_cast<const char*>(sqlite3_column_text(statement, index)), |
| 201 sqlite3_column_bytes(statement, index)); |
| 202 } |
| 203 } |
| 204 |
| 205 void GetColumn(sqlite3_stmt* statement, int index, int32* value) { |
| 206 *value = sqlite3_column_int(statement, index); |
| 207 } |
| 208 |
| 209 void GetColumn(sqlite3_stmt* statement, int index, int64* value) { |
| 210 *value = sqlite3_column_int64(statement, index); |
| 211 } |
| 212 |
| 213 void GetColumn(sqlite3_stmt* statement, int index, double* value) { |
| 214 *value = sqlite3_column_double(statement, index); |
| 215 } |
| 216 |
| 217 void GetColumn(sqlite3_stmt* statement, int index, bool* value) { |
| 218 *value = (0 != sqlite3_column_int(statement, index)); |
| 219 } |
| 220 |
| 221 void GetColumn(sqlite3_stmt* statement, int index, std::vector<uint8>* value) { |
| 222 if (sqlite3_column_type(statement, index) == SQLITE_NULL) { |
| 223 value->clear(); |
| 224 } else { |
| 225 const uint8* blob = |
| 226 reinterpret_cast<const uint8*>(sqlite3_column_blob(statement, index)); |
| 227 for (int i = 0; i < sqlite3_column_bytes(statement, index); i++) |
| 228 value->push_back(blob[i]); |
| 229 } |
| 230 } |
| 231 |
| 232 bool DoesTableExist(sqlite3 *dbhandle, const string &table_name) { |
| 233 ScopedStatement count_query |
| 234 (PrepareQuery(dbhandle, |
| 235 "SELECT count(*) from sqlite_master where name = ?", |
| 236 table_name)); |
| 237 |
| 238 int query_result = sqlite3_step(count_query.get()); |
| 239 CHECK(SQLITE_ROW == query_result); |
| 240 int count = sqlite3_column_int(count_query.get(), 0); |
| 241 |
| 242 return 1 == count; |
| 243 } |
| 244 |
| 245 void ScopedStatement::reset(sqlite3_stmt* statement) { |
| 246 if (NULL != statement_) |
| 247 sqlite3_finalize(statement_); |
| 248 statement_ = statement; |
| 249 } |
| 250 |
| 251 ScopedStatement::~ScopedStatement() { |
| 252 reset(NULL); |
| 253 } |
| 254 |
| 255 ScopedStatementResetter::~ScopedStatementResetter() { |
| 256 sqlite3_reset(statement_); |
| 257 } |
| 258 |
| 259 // Useful for encoding any sequence of bytes into a string that can be used in |
| 260 // a table name. Kind of like hex encoding, except that A is zero and P is 15. |
| 261 string APEncode(const string& in) { |
| 262 string result; |
| 263 result.reserve(in.size() * 2); |
| 264 for (string::const_iterator i = in.begin(); i != in.end(); ++i) { |
| 265 unsigned int c = static_cast<unsigned char>(*i); |
| 266 result.push_back((c & 0x0F) + 'A'); |
| 267 result.push_back(((c >> 4) & 0x0F) + 'A'); |
| 268 } |
| 269 return result; |
| 270 } |
| 271 |
| 272 string APDecode(const string& in) { |
| 273 string result; |
| 274 result.reserve(in.size() / 2); |
| 275 for (string::const_iterator i = in.begin(); i != in.end(); ++i) { |
| 276 unsigned int c = *i - 'A'; |
| 277 if (++i != in.end()) |
| 278 c = c | (static_cast<unsigned char>(*i - 'A') << 4); |
| 279 result.push_back(c); |
| 280 } |
| 281 return result; |
| 282 } |
OLD | NEW |