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