summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/util/query_helpers.h
blob: 54cda1ff58344c21b09da401f74f39e4941b10f7 (plain)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
// 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.
//
// Typesafe composition of SQL query strings.

#ifndef CHROME_BROWSER_SYNC_UTIL_QUERY_HELPERS_H_
#define CHROME_BROWSER_SYNC_UTIL_QUERY_HELPERS_H_

#include <limits>
#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/logging.h"
#include "build/build_config.h"
#include "chrome/browser/sync/util/sync_types.h"
#include "third_party/sqlite/preprocessed/sqlite3.h"

enum SqliteNullType {
  SQLITE_NULL_VALUE
};

int SqliteOpen(PathString filename, sqlite3** ppDb);

sqlite3_stmt* PrepareQuery(sqlite3* dbhandle, const char* query);
#if !PATHSTRING_IS_STD_STRING
sqlite3_stmt* BindArg(sqlite3_stmt*, const PathString&, int index);
sqlite3_stmt* BindArg(sqlite3_stmt*, const PathChar*, int index);
#endif
sqlite3_stmt* BindArg(sqlite3_stmt*, const std::string&, int index);
sqlite3_stmt* BindArg(sqlite3_stmt*, const char*, int index);
sqlite3_stmt* BindArg(sqlite3_stmt*, int32, int index);
sqlite3_stmt* BindArg(sqlite3_stmt*, int64, int index);
sqlite3_stmt* BindArg(sqlite3_stmt*, double, int index);
sqlite3_stmt* BindArg(sqlite3_stmt*, bool, int index);
sqlite3_stmt* BindArg(sqlite3_stmt*, const std::vector<uint8>&, int index);
sqlite3_stmt* BindArg(sqlite3_stmt*, SqliteNullType, int index);

#if !PATHSTRING_IS_STD_STRING
void GetColumn(sqlite3_stmt*, int index, PathString* value);
#endif
void GetColumn(sqlite3_stmt*, int index, std::string* value);
void GetColumn(sqlite3_stmt*, int index, int32* value);
void GetColumn(sqlite3_stmt*, int index, int64* value);
void GetColumn(sqlite3_stmt*, int index, double* value);
void GetColumn(sqlite3_stmt*, int index, bool* value);
void GetColumn(sqlite3_stmt*, int index, std::vector<uint8>* value);

bool DoesTableExist(sqlite3* dbhandle, const std::string& tablename);

// Prepares a query with a WHERE clause that filters the values by the items
// passed inside of the Vector.
// Example:
//
// vector<PathString> v;
// v.push_back("abc");
// v.push_back("123");
// PrepareQuery(dbhandle, "SELECT * FROM table", "column_name", v.begin(),
//              v.end(), "ORDER BY id");
//
// will produce the following query.
//
// SELECT * FROM table WHERE column_name = 'abc' OR column_name = '123' ORDER BY
// id.
//
template<typename ItemIterator>
sqlite3_stmt* PrepareQueryWhereColumnIn(sqlite3* dbhandle,
                                        const std::string& query_head,
                                        const std::string& filtername,
                                        ItemIterator begin, ItemIterator end,
                                        const std::string& query_options) {
  std::string query;
  query.reserve(512);
  query += query_head;
  const char* joiner = " WHERE ";
  for (ItemIterator it = begin; it != end; ++it) {
    query += joiner;
    query += filtername;
    query += " = ?";
    joiner = " OR ";
  }
  query += " ";
  query += query_options;
  sqlite3_stmt* statement = NULL;
  const char* query_tail;
  if (SQLITE_OK != sqlite3_prepare(dbhandle, query.data(),
                                   CountBytes(query), &statement,
                                   &query_tail)) {
    LOG(ERROR) << query << "\n" << sqlite3_errmsg(dbhandle);
  }
  int index = 1;
  for (ItemIterator it = begin; it != end; ++it) {
    BindArg(statement, *it, index);
    ++index;
  }
  return statement;
}

template <typename Type1>
inline sqlite3_stmt* PrepareQuery(sqlite3* dbhandle, const char* query,
                                  const Type1& arg1) {
  return BindArg(PrepareQuery(dbhandle, query), arg1, 1);
}

template <typename Type1, typename Type2>
inline sqlite3_stmt* PrepareQuery(sqlite3* dbhandle, const char* query,
                                  const Type1& arg1, const Type2& arg2) {
  return BindArg(PrepareQuery(dbhandle, query, arg1), arg2, 2);
}

template <typename Type1, typename Type2, typename Type3>
inline sqlite3_stmt* PrepareQuery(sqlite3* dbhandle, const char* query,
                                  const Type1& arg1, const Type2& arg2,
                                  const Type3& arg3) {
  return BindArg(PrepareQuery(dbhandle, query, arg1, arg2), arg3, 3);
}

template <typename Type1, typename Type2, typename Type3, typename Type4>
inline sqlite3_stmt* PrepareQuery(sqlite3* dbhandle, const char* query,
                                  const Type1& arg1, const Type2& arg2,
                                  const Type3& arg3, const Type4& arg4) {
  return BindArg(PrepareQuery(dbhandle, query, arg1, arg2, arg3), arg4, 4);
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5>
inline sqlite3_stmt* PrepareQuery(sqlite3* dbhandle, const char* query,
                                  const Type1& arg1, const Type2& arg2,
                                  const Type3& arg3, const Type4& arg4,
                                  const Type5& arg5) {
  return BindArg(PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4),
                 arg5, 5);
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6>
inline sqlite3_stmt* PrepareQuery(sqlite3* dbhandle, const char* query,
                                  const Type1& arg1, const Type2& arg2,
                                  const Type3& arg3, const Type4& arg4,
                                  const Type5& arg5, const Type6& arg6) {
  return BindArg(PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5),
                 arg6, 6);
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7>
inline sqlite3_stmt* PrepareQuery(sqlite3* dbhandle, const char* query,
                                  const Type1& arg1, const Type2& arg2,
                                  const Type3& arg3, const Type4& arg4,
                                  const Type5& arg5, const Type6& arg6,
                                  const Type7& arg7) {
  return BindArg(PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                              arg6),
                 arg7, 7);
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8>
inline sqlite3_stmt* PrepareQuery(sqlite3* dbhandle, const char* query,
                                  const Type1& arg1, const Type2& arg2,
                                  const Type3& arg3, const Type4& arg4,
                                  const Type5& arg5, const Type6& arg6,
                                  const Type7& arg7, const Type8& arg8) {
  return BindArg(PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                              arg6, arg7),
                 arg8, 8);
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8,
          typename Type9>
inline sqlite3_stmt* PrepareQuery(sqlite3* dbhandle, const char* query,
                                  const Type1& arg1, const Type2& arg2,
                                  const Type3& arg3, const Type4& arg4,
                                  const Type5& arg5, const Type6& arg6,
                                  const Type7& arg7, const Type8& arg8,
                                  const Type9& arg9) {
  return BindArg(PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                              arg6, arg7, arg8),
                 arg9, 9);
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8,
          typename Type9, typename Type10>
inline sqlite3_stmt* PrepareQuery(sqlite3* dbhandle, const char* query,
                                  const Type1& arg1, const Type2& arg2,
                                  const Type3& arg3, const Type4& arg4,
                                  const Type5& arg5, const Type6& arg6,
                                  const Type7& arg7, const Type8& arg8,
                                  const Type9& arg9, const Type10& arg10) {
  return BindArg(PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                              arg6, arg7, arg8, arg9),
                 arg10, 10);
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8,
          typename Type9, typename Type10, typename Type11>
inline sqlite3_stmt* PrepareQuery(sqlite3* dbhandle, const char* query,
                                  const Type1& arg1, const Type2& arg2,
                                  const Type3& arg3, const Type4& arg4,
                                  const Type5& arg5, const Type6& arg6,
                                  const Type7& arg7, const Type8& arg8,
                                  const Type9& arg9, const Type10& arg10,
                                  const Type11& arg11) {
  return BindArg(PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                              arg6, arg7, arg8, arg9, arg10),
                 arg11, 11);
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8,
          typename Type9, typename Type10, typename Type11, typename Type12>
inline sqlite3_stmt* PrepareQuery(sqlite3* dbhandle, const char* query,
                                  const Type1& arg1, const Type2& arg2,
                                  const Type3& arg3, const Type4& arg4,
                                  const Type5& arg5, const Type6& arg6,
                                  const Type7& arg7, const Type8& arg8,
                                  const Type9& arg9, const Type10& arg10,
                                  const Type11& arg11, const Type12& arg12) {
  return BindArg(PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                              arg6, arg7, arg8, arg9, arg10, arg11),
                 arg12, 12);
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8,
          typename Type9, typename Type10, typename Type11, typename Type12,
          typename Type13>
inline sqlite3_stmt* PrepareQuery(sqlite3* dbhandle, const char* query,
                                  const Type1& arg1, const Type2& arg2,
                                  const Type3& arg3, const Type4& arg4,
                                  const Type5& arg5, const Type6& arg6,
                                  const Type7& arg7, const Type8& arg8,
                                  const Type9& arg9, const Type10& arg10,
                                  const Type11& arg11, const Type12& arg12,
                                  const Type13& arg13) {
  return BindArg(PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                              arg6, arg7, arg8, arg9, arg10, arg11, arg12),
                 arg13, 13);
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8,
          typename Type9, typename Type10, typename Type11, typename Type12,
          typename Type13, typename Type14>
inline sqlite3_stmt* PrepareQuery(sqlite3* dbhandle, const char* query,
                                  const Type1& arg1, const Type2& arg2,
                                  const Type3& arg3, const Type4& arg4,
                                  const Type5& arg5, const Type6& arg6,
                                  const Type7& arg7, const Type8& arg8,
                                  const Type9& arg9, const Type10& arg10,
                                  const Type11& arg11, const Type12& arg12,
                                  const Type13& arg13, const Type14& arg14) {
  return BindArg(PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                              arg6, arg7, arg8, arg9, arg10, arg11, arg12,
                              arg13),
                 arg14, 14);
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8,
          typename Type9, typename Type10, typename Type11, typename Type12,
          typename Type13, typename Type14, typename Type15>
inline sqlite3_stmt* PrepareQuery(sqlite3* dbhandle, const char* query,
                                  const Type1& arg1, const Type2& arg2,
                                  const Type3& arg3, const Type4& arg4,
                                  const Type5& arg5, const Type6& arg6,
                                  const Type7& arg7, const Type8& arg8,
                                  const Type9& arg9, const Type10& arg10,
                                  const Type11& arg11, const Type12& arg12,
                                  const Type13& arg13, const Type14& arg14,
                                  const Type15& arg15) {
  return BindArg(PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                              arg6, arg7, arg8, arg9, arg10, arg11, arg12,
                              arg13, arg14),
                 arg15, 15);
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8,
          typename Type9, typename Type10, typename Type11, typename Type12,
          typename Type13, typename Type14, typename Type15, typename Type16>
inline sqlite3_stmt* PrepareQuery(sqlite3* dbhandle, const char* query,
                                  const Type1& arg1, const Type2& arg2,
                                  const Type3& arg3, const Type4& arg4,
                                  const Type5& arg5, const Type6& arg6,
                                  const Type7& arg7, const Type8& arg8,
                                  const Type9& arg9, const Type10& arg10,
                                  const Type11& arg11, const Type12& arg12,
                                  const Type13& arg13, const Type14& arg14,
                                  const Type15& arg15, const Type16& arg16) {
  return BindArg(PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                              arg6, arg7, arg8, arg9, arg10, arg11, arg12,
                              arg13, arg14, arg15),
                 arg16, 16);
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8,
          typename Type9, typename Type10, typename Type11, typename Type12,
          typename Type13, typename Type14, typename Type15, typename Type16,
          typename Type17>
inline sqlite3_stmt* PrepareQuery(sqlite3* dbhandle, const char* query,
                                  const Type1& arg1, const Type2& arg2,
                                  const Type3& arg3, const Type4& arg4,
                                  const Type5& arg5, const Type6& arg6,
                                  const Type7& arg7, const Type8& arg8,
                                  const Type9& arg9, const Type10& arg10,
                                  const Type11& arg11, const Type12& arg12,
                                  const Type13& arg13, const Type14& arg14,
                                  const Type15& arg15, const Type16& arg16,
                                  const Type17& arg17) {
  return BindArg(PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                              arg6, arg7, arg8, arg9, arg10, arg11, arg12,
                              arg13, arg14, arg15, arg16),
                 arg17, 17);
}

void ExecOrDie(sqlite3* dbhandle, const char* query);

// Finalizes (deletes) the query before returning.
void ExecOrDie(sqlite3* dbhandle, const char* query, sqlite3_stmt* statement);

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8,
          typename Type9, typename Type10>
inline void ExecOrDie(sqlite3* dbhandle, const char* query,
                      const Type1& arg1, const Type2& arg2,
                      const Type3& arg3, const Type4& arg4,
                      const Type5& arg5, const Type6& arg6,
                      const Type7& arg7, const Type8& arg8,
                      const Type9& arg9, const Type10& arg10) {
  return ExecOrDie(dbhandle, query,
                   PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                                arg6, arg7, arg8, arg9, arg10));
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8,
          typename Type9>
inline void ExecOrDie(sqlite3* dbhandle, const char* query,
                      const Type1& arg1, const Type2& arg2,
                      const Type3& arg3, const Type4& arg4,
                      const Type5& arg5, const Type6& arg6,
                      const Type7& arg7, const Type8& arg8,
                      const Type9& arg9) {
  return ExecOrDie(dbhandle, query,
                   PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                                arg6, arg7, arg8, arg9));
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8>
inline void ExecOrDie(sqlite3* dbhandle, const char* query,
                      const Type1& arg1, const Type2& arg2,
                      const Type3& arg3, const Type4& arg4,
                      const Type5& arg5, const Type6& arg6,
                      const Type7& arg7, const Type8& arg8) {
  return ExecOrDie(dbhandle, query,
                   PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                                arg6, arg7, arg8));
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7>
inline void ExecOrDie(sqlite3* dbhandle, const char* query,
                      const Type1& arg1, const Type2& arg2,
                      const Type3& arg3, const Type4& arg4,
                      const Type5& arg5, const Type6& arg6,
                      const Type7& arg7) {
  return ExecOrDie(dbhandle, query,
                   PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                                arg6, arg7));
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6>
inline void ExecOrDie(sqlite3* dbhandle, const char* query,
                      const Type1& arg1, const Type2& arg2,
                      const Type3& arg3, const Type4& arg4,
                      const Type5& arg5, const Type6& arg6) {
  return ExecOrDie(dbhandle, query, PrepareQuery(dbhandle, query, arg1, arg2,
                                                 arg3, arg4, arg5, arg6));
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5>
inline void ExecOrDie(sqlite3* dbhandle, const char* query,
                      const Type1& arg1, const Type2& arg2,
                      const Type3& arg3, const Type4& arg4,
                      const Type5& arg5) {
  return ExecOrDie(dbhandle, query, PrepareQuery(dbhandle, query, arg1, arg2,
                                                 arg3, arg4, arg5));
}

template <typename Type1, typename Type2, typename Type3, typename Type4>
inline void ExecOrDie(sqlite3* dbhandle, const char* query,
                      const Type1& arg1, const Type2& arg2,
                      const Type3& arg3, const Type4& arg4) {
  return ExecOrDie(dbhandle, query, PrepareQuery(dbhandle, query, arg1, arg2,
                                                 arg3, arg4));
}

template <typename Type1, typename Type2, typename Type3>
inline void ExecOrDie(sqlite3* dbhandle, const char* query,
                      const Type1& arg1, const Type2& arg2,
                      const Type3& arg3) {
  return ExecOrDie(dbhandle, query, PrepareQuery(dbhandle, query, arg1, arg2,
                                                 arg3));
}

template <typename Type1, typename Type2>
inline void ExecOrDie(sqlite3* dbhandle, const char* query,
                      const Type1& arg1, const Type2& arg2) {
  return ExecOrDie(dbhandle, query, PrepareQuery(dbhandle, query, arg1, arg2));
}

template <typename Type1>
inline void ExecOrDie(sqlite3* dbhandle, const char* query,
                      const Type1& arg1) {
  return ExecOrDie(dbhandle, query, PrepareQuery(dbhandle, query, arg1));
}


int Exec(sqlite3* dbhandle, const char* query);
// Finalizes (deletes) the query before returning.
int Exec(sqlite3* dbhandle, const char* query, sqlite3_stmt* statement);

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8,
          typename Type9, typename Type10, typename Type11, typename Type12,
          typename Type13, typename Type14, typename Type15, typename Type16,
          typename Type17>
inline int Exec(sqlite3* dbhandle, const char* query,
                const Type1& arg1, const Type2& arg2,
                const Type3& arg3, const Type4& arg4,
                const Type5& arg5, const Type6& arg6,
                const Type7& arg7, const Type8& arg8,
                const Type9& arg9, const Type10& arg10,
                const Type11& arg11, const Type12& arg12,
                const Type13& arg13, const Type14& arg14,
                const Type15& arg15, const Type16& arg16,
                const Type17& arg17) {
  return Exec(dbhandle, query,
              PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                           arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13,
                           arg14, arg15, arg16, arg17));
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8,
          typename Type9, typename Type10, typename Type11, typename Type12,
          typename Type13, typename Type14, typename Type15, typename Type16>
inline int Exec(sqlite3* dbhandle, const char* query,
                const Type1& arg1, const Type2& arg2,
                const Type3& arg3, const Type4& arg4,
                const Type5& arg5, const Type6& arg6,
                const Type7& arg7, const Type8& arg8,
                const Type9& arg9, const Type10& arg10,
                const Type11& arg11, const Type12& arg12,
                const Type13& arg13, const Type14& arg14,
                const Type15& arg15, const Type16& arg16) {
  return Exec(dbhandle, query,
              PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                           arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13,
                           arg14, arg15, arg16));
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8,
          typename Type9, typename Type10, typename Type11, typename Type12,
          typename Type13, typename Type14, typename Type15>
inline int Exec(sqlite3* dbhandle, const char* query,
                const Type1& arg1, const Type2& arg2,
                const Type3& arg3, const Type4& arg4,
                const Type5& arg5, const Type6& arg6,
                const Type7& arg7, const Type8& arg8,
                const Type9& arg9, const Type10& arg10,
                const Type11& arg11, const Type12& arg12,
                const Type13& arg13, const Type14& arg14,
                const Type15& arg15) {
  return Exec(dbhandle, query,
              PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                           arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13,
                           arg14, arg15));
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8,
          typename Type9, typename Type10, typename Type11, typename Type12,
          typename Type13, typename Type14>
inline int Exec(sqlite3* dbhandle, const char* query,
                const Type1& arg1, const Type2& arg2,
                const Type3& arg3, const Type4& arg4,
                const Type5& arg5, const Type6& arg6,
                const Type7& arg7, const Type8& arg8,
                const Type9& arg9, const Type10& arg10,
                const Type11& arg11, const Type12& arg12,
                const Type13& arg13, const Type14& arg14) {
  return Exec(dbhandle, query,
              PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                           arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13,
                           arg14));
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8,
          typename Type9, typename Type10, typename Type11, typename Type12,
          typename Type13>
inline int Exec(sqlite3* dbhandle, const char* query,
                const Type1& arg1, const Type2& arg2,
                const Type3& arg3, const Type4& arg4,
                const Type5& arg5, const Type6& arg6,
                const Type7& arg7, const Type8& arg8,
                const Type9& arg9, const Type10& arg10,
                const Type11& arg11, const Type12& arg12,
                const Type13& arg13) {
  return Exec(dbhandle, query,
              PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                           arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13));
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8,
          typename Type9, typename Type10, typename Type11, typename Type12>
inline int Exec(sqlite3* dbhandle, const char* query,
                const Type1& arg1, const Type2& arg2,
                const Type3& arg3, const Type4& arg4,
                const Type5& arg5, const Type6& arg6,
                const Type7& arg7, const Type8& arg8,
                const Type9& arg9, const Type10& arg10,
                const Type11& arg11, const Type12& arg12) {
  return Exec(dbhandle, query,
              PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                           arg6, arg7, arg8, arg9, arg10, arg11, arg12));
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8,
          typename Type9, typename Type10, typename Type11>
inline int Exec(sqlite3* dbhandle, const char* query,
                const Type1& arg1, const Type2& arg2,
                const Type3& arg3, const Type4& arg4,
                const Type5& arg5, const Type6& arg6,
                const Type7& arg7, const Type8& arg8,
                const Type9& arg9, const Type10& arg10,
                const Type11& arg11) {
  return Exec(dbhandle, query,
              PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                           arg6, arg7, arg8, arg9, arg10, arg11));
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8,
          typename Type9, typename Type10>
inline int Exec(sqlite3* dbhandle, const char* query,
                const Type1& arg1, const Type2& arg2,
                const Type3& arg3, const Type4& arg4,
                const Type5& arg5, const Type6& arg6,
                const Type7& arg7, const Type8& arg8,
                const Type9& arg9, const Type10& arg10) {
  return Exec(dbhandle, query,
              PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                           arg6, arg7, arg8, arg9, arg10));
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8,
          typename Type9>
inline int Exec(sqlite3* dbhandle, const char* query,
                const Type1& arg1, const Type2& arg2,
                const Type3& arg3, const Type4& arg4,
                const Type5& arg5, const Type6& arg6,
                const Type7& arg7, const Type8& arg8,
                const Type9& arg9) {
  return Exec(dbhandle, query,
              PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                           arg6, arg7, arg8, arg9));
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7, typename Type8>
inline int Exec(sqlite3* dbhandle, const char* query,
                const Type1& arg1, const Type2& arg2,
                const Type3& arg3, const Type4& arg4,
                const Type5& arg5, const Type6& arg6,
                const Type7& arg7, const Type8& arg8) {
  return Exec(dbhandle, query,
              PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                           arg6, arg7, arg8));
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6, typename Type7>
inline int Exec(sqlite3* dbhandle, const char* query,
                const Type1& arg1, const Type2& arg2,
                const Type3& arg3, const Type4& arg4,
                const Type5& arg5, const Type6& arg6,
                const Type7& arg7) {
  return Exec(dbhandle, query,
              PrepareQuery(dbhandle, query, arg1, arg2, arg3, arg4, arg5,
                           arg6, arg7));
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5, typename Type6>
inline int Exec(sqlite3* dbhandle, const char* query,
                const Type1& arg1, const Type2& arg2,
                const Type3& arg3, const Type4& arg4,
                const Type5& arg5, const Type6& arg6) {
  return Exec(dbhandle, query, PrepareQuery(dbhandle, query, arg1, arg2,
                                            arg3, arg4, arg5, arg6));
}

template <typename Type1, typename Type2, typename Type3, typename Type4,
          typename Type5>
inline int Exec(sqlite3* dbhandle, const char* query,
                const Type1& arg1, const Type2& arg2,
                const Type3& arg3, const Type4& arg4,
                const Type5& arg5) {
  return Exec(dbhandle, query, PrepareQuery(dbhandle, query, arg1, arg2,
                                            arg3, arg4, arg5));
}

template <typename Type1, typename Type2, typename Type3, typename Type4>
inline int Exec(sqlite3* dbhandle, const char* query,
                const Type1& arg1, const Type2& arg2,
                const Type3& arg3, const Type4& arg4) {
  return Exec(dbhandle, query, PrepareQuery(dbhandle, query, arg1, arg2,
                                            arg3, arg4));
}

template <typename Type1, typename Type2, typename Type3>
inline int Exec(sqlite3* dbhandle, const char* query,
                const Type1& arg1, const Type2& arg2,
                const Type3& arg3) {
  return Exec(dbhandle, query, PrepareQuery(dbhandle, query, arg1, arg2,
                                            arg3));
}

template <typename Type1, typename Type2>
inline int Exec(sqlite3* dbhandle, const char* query,
                const Type1& arg1, const Type2& arg2) {
  return Exec(dbhandle, query, PrepareQuery(dbhandle, query, arg1, arg2));
}

template <typename Type1>
inline int Exec(sqlite3* dbhandle, const char* query,
                const Type1& arg1) {
  return Exec(dbhandle, query, PrepareQuery(dbhandle, query, arg1));
}


// Holds an sqlite3_stmt* and automatically finalizes when passes out of scope.
class ScopedStatement {
 public:
  explicit ScopedStatement(sqlite3_stmt* statement = 0)
      : statement_(statement) { }
  ~ScopedStatement();

  sqlite3_stmt* get() const { return statement_; }

  // Finalizes currently held statement and sets to new one.
  void reset(sqlite3_stmt* statement);
 protected:
  sqlite3_stmt* statement_;

  DISALLOW_COPY_AND_ASSIGN(ScopedStatement);
};


// Holds an sqlite3_stmt* and automatically resets when passes out of scope.
class ScopedStatementResetter {
 public:
  explicit ScopedStatementResetter(sqlite3_stmt* statement)
      : statement_(statement) { }
  ~ScopedStatementResetter();

 protected:
  sqlite3_stmt* const statement_;

  DISALLOW_COPY_AND_ASSIGN(ScopedStatementResetter);
};

// 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.
std::string APEncode(const std::string& in);
std::string APDecode(const std::string& in);

#endif  // CHROME_BROWSER_SYNC_UTIL_QUERY_HELPERS_H_