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