OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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/query_helpers.h" | 5 #include "chrome/browser/sync/util/query_helpers.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <windows.h> | 8 #include <windows.h> |
9 #endif | 9 #endif |
10 | 10 |
11 #include <limits> | 11 #include <limits> |
12 #include <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "chrome/browser/sync/util/sync_types.h" | 15 #include "chrome/browser/sync/util/sync_types.h" |
| 16 #include "chrome/common/sqlite_utils.h" |
16 | 17 |
17 using std::numeric_limits; | 18 using std::numeric_limits; |
18 using std::string; | 19 using std::string; |
19 using std::vector; | 20 using std::vector; |
20 | 21 |
21 sqlite3_stmt* PrepareQuery(sqlite3* dbhandle, const char* query) { | 22 sqlite3_stmt* PrepareQuery(sqlite3* dbhandle, const char* query) { |
22 sqlite3_stmt* statement = NULL; | 23 sqlite3_stmt* statement = NULL; |
23 const char* query_tail; | 24 const char* query_tail; |
24 if (SQLITE_OK != sqlite3_prepare(dbhandle, query, | 25 if (SQLITE_OK != sqlite3_prepare(dbhandle, query, |
25 CountBytes(query), &statement, | 26 CountBytes(query), &statement, |
(...skipping 22 matching lines...) Expand all Loading... |
48 // Finalizes (deletes) the query before returning. | 49 // Finalizes (deletes) the query before returning. |
49 int Exec(sqlite3* dbhandle, const char* query, sqlite3_stmt* statement) { | 50 int Exec(sqlite3* dbhandle, const char* query, sqlite3_stmt* statement) { |
50 int result; | 51 int result; |
51 do { | 52 do { |
52 result = sqlite3_step(statement); | 53 result = sqlite3_step(statement); |
53 } while (SQLITE_ROW == result); | 54 } while (SQLITE_ROW == result); |
54 int finalize_result = sqlite3_finalize(statement); | 55 int finalize_result = sqlite3_finalize(statement); |
55 return SQLITE_OK == finalize_result ? result : finalize_result; | 56 return SQLITE_OK == finalize_result ? result : finalize_result; |
56 } | 57 } |
57 | 58 |
58 int SqliteOpen(PathString filename, sqlite3** db) { | 59 int SqliteOpen(const FilePath& filename, sqlite3** db) { |
59 int result = | 60 int result = OpenSqliteDb(filename, db); |
60 #if PATHSTRING_IS_STD_STRING | 61 LOG_IF(ERROR, SQLITE_OK != result) << "Error opening " |
61 sqlite3_open | 62 << filename.value() << ": " |
62 #else | |
63 sqlite3_open16 | |
64 #endif | |
65 (filename.c_str(), db); | |
66 LOG_IF(ERROR, SQLITE_OK != result) << "Error opening " << filename << ": " | |
67 << result; | 63 << result; |
68 #if defined(OS_WIN) | 64 #if defined(OS_WIN) |
69 if (SQLITE_OK == result) { | 65 if (SQLITE_OK == result) { |
70 // Make sure we mark the db file as not indexed so since if any other app | 66 // 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. | 67 // opens it, it can break our db locking. |
72 DWORD attrs = GetFileAttributes(filename.c_str()); | 68 DWORD attrs = GetFileAttributesW(filename.value().c_str()); |
73 if (FILE_ATTRIBUTE_NORMAL == attrs) | 69 if (FILE_ATTRIBUTE_NORMAL == attrs) |
74 attrs = FILE_ATTRIBUTE_NOT_CONTENT_INDEXED; | 70 attrs = FILE_ATTRIBUTE_NOT_CONTENT_INDEXED; |
75 else | 71 else |
76 attrs = attrs | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED; | 72 attrs = attrs | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED; |
77 SetFileAttributes(filename.c_str(), attrs); | 73 SetFileAttributesW(filename.value().c_str(), attrs); |
78 } | 74 } |
79 #endif // defined(OS_WIN) | 75 #endif // defined(OS_WIN) |
80 // Be patient as we set pragmas. | 76 // Be patient as we set pragmas. |
81 sqlite3_busy_timeout(*db, numeric_limits<int>::max()); | 77 sqlite3_busy_timeout(*db, numeric_limits<int>::max()); |
82 #if !defined(DISABLE_SQLITE_FULL_FSYNC) | 78 #if !defined(DISABLE_SQLITE_FULL_FSYNC) |
83 ExecOrDie(*db, "PRAGMA fullfsync = 1"); | 79 ExecOrDie(*db, "PRAGMA fullfsync = 1"); |
84 #endif // !defined(DISABLE_SQLITE_FULL_FSYNC) | 80 #endif // !defined(DISABLE_SQLITE_FULL_FSYNC) |
85 ExecOrDie(*db, "PRAGMA synchronous = 2"); | 81 ExecOrDie(*db, "PRAGMA synchronous = 2"); |
86 sqlite3_busy_timeout(*db, 0); | 82 sqlite3_busy_timeout(*db, 0); |
87 return SQLITE_OK; | 83 return SQLITE_OK; |
88 } | 84 } |
89 | 85 |
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 // !PATHSTRING_IS_STD_STRING | |
110 | |
111 sqlite3_stmt* BindArg(sqlite3_stmt* statement, const string& s, int index) { | 86 sqlite3_stmt* BindArg(sqlite3_stmt* statement, const string& s, int index) { |
112 if (NULL == statement) | 87 if (NULL == statement) |
113 return statement; | 88 return statement; |
114 CHECK(SQLITE_OK == sqlite3_bind_text(statement, | 89 CHECK(SQLITE_OK == sqlite3_bind_text(statement, |
115 index, | 90 index, |
116 s.data(), | 91 s.data(), |
117 CountBytes(s), | 92 CountBytes(s), |
118 SQLITE_TRANSIENT)); | 93 SQLITE_TRANSIENT)); |
119 return statement; | 94 return statement; |
120 } | 95 } |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 return statement; | 147 return statement; |
173 } | 148 } |
174 | 149 |
175 sqlite3_stmt* BindArg(sqlite3_stmt* statement, SqliteNullType, int index) { | 150 sqlite3_stmt* BindArg(sqlite3_stmt* statement, SqliteNullType, int index) { |
176 if (NULL == statement) | 151 if (NULL == statement) |
177 return statement; | 152 return statement; |
178 CHECK(SQLITE_OK == sqlite3_bind_null(statement, index)); | 153 CHECK(SQLITE_OK == sqlite3_bind_null(statement, index)); |
179 return statement; | 154 return statement; |
180 } | 155 } |
181 | 156 |
182 #if !PATHSTRING_IS_STD_STRING | 157 void GetColumn(sqlite3_stmt* statement, int index, string16* value) { |
183 void GetColumn(sqlite3_stmt* statement, int index, PathString* value) { | |
184 if (sqlite3_column_type(statement, index) == SQLITE_NULL) { | 158 if (sqlite3_column_type(statement, index) == SQLITE_NULL) { |
185 value->clear(); | 159 value->clear(); |
186 } else { | 160 } else { |
187 value->assign( | 161 value->assign( |
188 static_cast<const PathChar*>(sqlite3_column_text16(statement, index)), | 162 static_cast<const char16*>(sqlite3_column_text16(statement, index)), |
189 sqlite3_column_bytes16(statement, index) / sizeof(PathChar)); | 163 sqlite3_column_bytes16(statement, index) / sizeof(char16)); |
190 } | 164 } |
191 } | 165 } |
192 #endif // !PATHSTRING_IS_STD_STRING | |
193 | 166 |
194 void GetColumn(sqlite3_stmt* statement, int index, string* value) { | 167 void GetColumn(sqlite3_stmt* statement, int index, string* value) { |
195 if (sqlite3_column_type(statement, index) == SQLITE_NULL) { | 168 if (sqlite3_column_type(statement, index) == SQLITE_NULL) { |
196 value->clear(); | 169 value->clear(); |
197 } else { | 170 } else { |
198 value->assign( | 171 value->assign( |
199 reinterpret_cast<const char*>(sqlite3_column_text(statement, index)), | 172 reinterpret_cast<const char*>(sqlite3_column_text(statement, index)), |
200 sqlite3_column_bytes(statement, index)); | 173 sqlite3_column_bytes(statement, index)); |
201 } | 174 } |
202 } | 175 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 string result; | 245 string result; |
273 result.reserve(in.size() / 2); | 246 result.reserve(in.size() / 2); |
274 for (string::const_iterator i = in.begin(); i != in.end(); ++i) { | 247 for (string::const_iterator i = in.begin(); i != in.end(); ++i) { |
275 unsigned int c = *i - 'A'; | 248 unsigned int c = *i - 'A'; |
276 if (++i != in.end()) | 249 if (++i != in.end()) |
277 c = c | (static_cast<unsigned char>(*i - 'A') << 4); | 250 c = c | (static_cast<unsigned char>(*i - 'A') << 4); |
278 result.push_back(c); | 251 result.push_back(c); |
279 } | 252 } |
280 return result; | 253 return result; |
281 } | 254 } |
OLD | NEW |