~ubuntu-branches/ubuntu/trusty/subversion/trusty-proposed

« back to all changes in this revision

Viewing changes to subversion/libsvn_subr/sqlite.c

  • Committer: Package Import Robot
  • Author(s): Andy Whitcroft
  • Date: 2012-06-21 15:36:36 UTC
  • mfrom: (0.4.13 sid)
  • Revision ID: package-import@ubuntu.com-20120621153636-amqqmuidgwgxz1ly
Tags: 1.7.5-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - Create pot file on build.
  - Build a python-subversion-dbg package.
  - Build-depend on python-dbg.
  - Build-depend on default-jre-headless/-jdk.
  - Do not apply java-build patch.
  - debian/rules: Manually create the doxygen output directory, otherwise
    we get weird build failures when running parallel builds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* sqlite.c
2
2
 *
3
3
 * ====================================================================
4
 
 * Copyright (c) 2008-2009 CollabNet.  All rights reserved.
5
 
 *
6
 
 * This software is licensed as described in the file COPYING, which
7
 
 * you should have received as part of this distribution.  The terms
8
 
 * are also available at http://subversion.tigris.org/license-1.html.
9
 
 * If newer versions of this license are posted there, you may use a
10
 
 * newer version instead, at your option.
11
 
 *
12
 
 * This software consists of voluntary contributions made by many
13
 
 * individuals.  For exact contribution history, see the revision
14
 
 * history and logs, available at http://subversion.tigris.org/.
 
4
 *    Licensed to the Apache Software Foundation (ASF) under one
 
5
 *    or more contributor license agreements.  See the NOTICE file
 
6
 *    distributed with this work for additional information
 
7
 *    regarding copyright ownership.  The ASF licenses this file
 
8
 *    to you under the Apache License, Version 2.0 (the
 
9
 *    "License"); you may not use this file except in compliance
 
10
 *    with the License.  You may obtain a copy of the License at
 
11
 *
 
12
 *      http://www.apache.org/licenses/LICENSE-2.0
 
13
 *
 
14
 *    Unless required by applicable law or agreed to in writing,
 
15
 *    software distributed under the License is distributed on an
 
16
 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 
17
 *    KIND, either express or implied.  See the License for the
 
18
 *    specific language governing permissions and limitations
 
19
 *    under the License.
15
20
 * ====================================================================
16
21
 */
17
22
 
21
26
#include "svn_error.h"
22
27
#include "svn_pools.h"
23
28
#include "svn_io.h"
24
 
#include "svn_path.h"
 
29
#include "svn_dirent_uri.h"
 
30
#include "svn_checksum.h"
 
31
 
 
32
#include "internal_statements.h"
25
33
 
26
34
#include "private/svn_sqlite.h"
27
35
#include "svn_private_config.h"
28
36
#include "private/svn_dep_compat.h"
29
37
#include "private/svn_atomic.h"
 
38
#include "private/svn_skel.h"
 
39
#include "private/svn_token.h"
30
40
 
 
41
#ifdef SQLITE3_DEBUG
 
42
#include "private/svn_debug.h"
 
43
#endif
31
44
 
32
45
#ifdef SVN_SQLITE_INLINE
33
46
/* Include sqlite3 inline, making all symbols private. */
37
50
  #include <sqlite3.h>
38
51
#endif
39
52
 
 
53
#if !SQLITE_VERSION_AT_LEAST(3,6,18)
 
54
#error SQLite is too old -- version 3.6.18 is the minimum required version
 
55
#endif
 
56
 
 
57
INTERNAL_STATEMENTS_SQL_DECLARE_STATEMENTS(internal_statements);
 
58
 
 
59
 
40
60
#ifdef SQLITE3_DEBUG
41
61
/* An sqlite query execution callback. */
42
62
static void
43
63
sqlite_tracer(void *data, const char *sql)
44
64
{
45
65
  /*  sqlite3 *db3 = data; */
46
 
  fprintf(stderr, "SQLITE SQL is \"%s\"\n", sql);
 
66
  SVN_DBG(("sql=\"%s\"\n", sql));
47
67
}
48
68
#endif
49
69
 
 
70
#ifdef SQLITE3_PROFILE
 
71
/* An sqlite execution timing callback. */
 
72
static void
 
73
sqlite_profiler(void *data, const char *sql, sqlite3_uint64 duration)
 
74
{
 
75
  /*  sqlite3 *db3 = data; */
 
76
  SVN_DBG(("[%.3f] sql=\"%s\"\n", 1e-9 * duration, sql));
 
77
}
 
78
#endif
50
79
 
51
80
struct svn_sqlite__db_t
52
81
{
54
83
  const char * const *statement_strings;
55
84
  int nbr_statements;
56
85
  svn_sqlite__stmt_t **prepared_stmts;
57
 
  apr_pool_t *result_pool;
 
86
  apr_pool_t *state_pool;
 
87
  unsigned savepoint_nr;
58
88
};
59
89
 
60
90
struct svn_sqlite__stmt_t
61
91
{
62
92
  sqlite3_stmt *s3stmt;
63
93
  svn_sqlite__db_t *db;
64
 
};
65
 
 
66
 
 
67
 
/* Convert SQLite error codes to SVN */
68
 
#define SQLITE_ERROR_CODE(x) ((x) == SQLITE_READONLY    \
69
 
                              ? SVN_ERR_SQLITE_READONLY \
70
 
                              : SVN_ERR_SQLITE_ERROR )
 
94
  svn_boolean_t needs_reset;
 
95
};
 
96
 
 
97
struct svn_sqlite__context_t
 
98
{
 
99
  sqlite3_context *context;
 
100
};
 
101
 
 
102
struct svn_sqlite__value_t
 
103
{
 
104
  sqlite3_value *value;
 
105
};
 
106
 
 
107
 
 
108
/* Convert SQLite error codes to SVN. Evaluates X multiple times */
 
109
#define SQLITE_ERROR_CODE(x) ((x) == SQLITE_READONLY            \
 
110
                              ? SVN_ERR_SQLITE_READONLY         \
 
111
                              : ((x) == SQLITE_BUSY             \
 
112
                                 ? SVN_ERR_SQLITE_BUSY          \
 
113
                                 : ((x) == SQLITE_CONSTRAINT    \
 
114
                                    ? SVN_ERR_SQLITE_CONSTRAINT \
 
115
                                    : SVN_ERR_SQLITE_ERROR)))
71
116
 
72
117
 
73
118
/* SQLITE->SVN quick error wrap, much like SVN_ERR. */
75
120
{                                                                \
76
121
  int sqlite_err__temp = (x);                                    \
77
122
  if (sqlite_err__temp != SQLITE_OK)                             \
78
 
    return svn_error_create(SQLITE_ERROR_CODE(sqlite_err__temp), \
79
 
                            NULL, sqlite3_errmsg((db)->db3));    \
 
123
    return svn_error_createf(SQLITE_ERROR_CODE(sqlite_err__temp), \
 
124
                             NULL, "sqlite: %s",                 \
 
125
                             sqlite3_errmsg((db)->db3));         \
80
126
} while (0)
81
127
 
82
128
#define SQLITE_ERR_MSG(x, msg) do                                \
83
129
{                                                                \
84
130
  int sqlite_err__temp = (x);                                    \
85
131
  if (sqlite_err__temp != SQLITE_OK)                             \
86
 
    return svn_error_create(SQLITE_ERROR_CODE(sqlite_err__temp), \
87
 
                            NULL, msg);                          \
 
132
    return svn_error_createf(SQLITE_ERROR_CODE(sqlite_err__temp), \
 
133
                             NULL, "sqlite: %s", (msg));         \
88
134
} while (0)
89
135
 
90
136
 
91
 
svn_error_t *
92
 
svn_sqlite__exec(svn_sqlite__db_t *db, const char *sql)
 
137
/* Convenience wrapper around exec_sql2(). */
 
138
#define exec_sql(db, sql) exec_sql2((db), (sql), SQLITE_OK)
 
139
 
 
140
/* Run the statement SQL on DB, ignoring SQLITE_OK and IGNORED_ERR.
 
141
   (Note: the IGNORED_ERR parameter itself is not ignored.) */
 
142
static svn_error_t *
 
143
exec_sql2(svn_sqlite__db_t *db, const char *sql, int ignored_err)
93
144
{
94
145
  char *err_msg;
95
146
  int sqlite_err = sqlite3_exec(db->db3, sql, NULL, NULL, &err_msg);
96
147
 
97
 
  if (sqlite_err != SQLITE_OK)
 
148
  if (sqlite_err != SQLITE_OK && sqlite_err != ignored_err)
98
149
    {
99
 
      svn_error_t *err = svn_error_create(SQLITE_ERROR_CODE(sqlite_err), NULL,
100
 
                                          err_msg);
 
150
      svn_error_t *err = svn_error_createf(SQLITE_ERROR_CODE(sqlite_err), NULL,
 
151
                                           _("%s, executing statement '%s'"),
 
152
                                           err_msg, sql);
101
153
      sqlite3_free(err_msg);
102
154
      return err;
103
155
    }
105
157
  return SVN_NO_ERROR;
106
158
}
107
159
 
108
 
svn_error_t *
109
 
svn_sqlite__transaction_begin(svn_sqlite__db_t *db)
110
 
{
111
 
  return svn_sqlite__exec(db, "BEGIN TRANSACTION;");
112
 
}
113
 
 
114
 
svn_error_t *
115
 
svn_sqlite__transaction_commit(svn_sqlite__db_t *db)
116
 
{
117
 
  return svn_sqlite__exec(db, "COMMIT TRANSACTION;");
118
 
}
119
 
 
120
 
svn_error_t *
121
 
svn_sqlite__transaction_rollback(svn_sqlite__db_t *db)
122
 
{
123
 
  return svn_sqlite__exec(db, "ROLLBACK TRANSACTION;");
124
 
}
 
160
 
 
161
static svn_error_t *
 
162
prepare_statement(svn_sqlite__stmt_t **stmt, svn_sqlite__db_t *db,
 
163
                  const char *text, apr_pool_t *result_pool)
 
164
{
 
165
  *stmt = apr_palloc(result_pool, sizeof(**stmt));
 
166
  (*stmt)->db = db;
 
167
  (*stmt)->needs_reset = FALSE;
 
168
 
 
169
  SQLITE_ERR(sqlite3_prepare_v2(db->db3, text, -1, &(*stmt)->s3stmt, NULL), db);
 
170
 
 
171
  return SVN_NO_ERROR;
 
172
}
 
173
 
 
174
 
 
175
svn_error_t *
 
176
svn_sqlite__exec_statements(svn_sqlite__db_t *db, int stmt_idx)
 
177
{
 
178
  SVN_ERR_ASSERT(stmt_idx < db->nbr_statements);
 
179
 
 
180
  return svn_error_trace(exec_sql(db, db->statement_strings[stmt_idx]));
 
181
}
 
182
 
125
183
 
126
184
svn_error_t *
127
185
svn_sqlite__get_statement(svn_sqlite__stmt_t **stmt, svn_sqlite__db_t *db,
130
188
  SVN_ERR_ASSERT(stmt_idx < db->nbr_statements);
131
189
 
132
190
  if (db->prepared_stmts[stmt_idx] == NULL)
133
 
    SVN_ERR(svn_sqlite__prepare(&db->prepared_stmts[stmt_idx], db,
134
 
                                db->statement_strings[stmt_idx],
135
 
                                db->result_pool));
 
191
    SVN_ERR(prepare_statement(&db->prepared_stmts[stmt_idx], db,
 
192
                              db->statement_strings[stmt_idx],
 
193
                              db->state_pool));
136
194
 
137
195
  *stmt = db->prepared_stmts[stmt_idx];
138
 
  return SVN_NO_ERROR;
139
 
}
140
 
 
141
 
svn_error_t *
142
 
svn_sqlite__prepare(svn_sqlite__stmt_t **stmt, svn_sqlite__db_t *db,
143
 
                    const char *text, apr_pool_t *result_pool)
144
 
{
145
 
  *stmt = apr_palloc(result_pool, sizeof(**stmt));
146
 
  (*stmt)->db = db;
147
 
 
148
 
  SQLITE_ERR(sqlite3_prepare_v2(db->db3, text, -1, &(*stmt)->s3stmt, NULL), db);
 
196
 
 
197
  if ((*stmt)->needs_reset)
 
198
    return svn_error_trace(svn_sqlite__reset(*stmt));
149
199
 
150
200
  return SVN_NO_ERROR;
151
201
}
160
210
  if ((got_row && !expecting_row)
161
211
      ||
162
212
      (!got_row && expecting_row))
163
 
    return svn_error_create(SVN_ERR_SQLITE_ERROR, NULL,
 
213
    return svn_error_create(SVN_ERR_SQLITE_ERROR,
 
214
                            svn_sqlite__reset(stmt),
164
215
                            expecting_row
165
 
                            ? _("Expected database row missing")
166
 
                            : _("Extra database row found"));
 
216
                              ? _("Expected database row missing")
 
217
                              : _("Extra database row found"));
167
218
 
168
219
  return SVN_NO_ERROR;
169
220
}
171
222
svn_error_t *
172
223
svn_sqlite__step_done(svn_sqlite__stmt_t *stmt)
173
224
{
174
 
  return step_with_expectation(stmt, FALSE);
 
225
  SVN_ERR(step_with_expectation(stmt, FALSE));
 
226
  return svn_error_trace(svn_sqlite__reset(stmt));
175
227
}
176
228
 
177
229
svn_error_t *
178
230
svn_sqlite__step_row(svn_sqlite__stmt_t *stmt)
179
231
{
180
 
  return step_with_expectation(stmt, TRUE);
 
232
  return svn_error_trace(step_with_expectation(stmt, TRUE));
181
233
}
182
234
 
 
235
 
183
236
svn_error_t *
184
237
svn_sqlite__step(svn_boolean_t *got_row, svn_sqlite__stmt_t *stmt)
185
238
{
189
242
    {
190
243
      svn_error_t *err1, *err2;
191
244
 
192
 
      err1 = svn_error_create(SQLITE_ERROR_CODE(sqlite_result), NULL,
193
 
                              sqlite3_errmsg(stmt->db->db3));
 
245
      err1 = svn_error_createf(SQLITE_ERROR_CODE(sqlite_result), NULL,
 
246
                               "sqlite: %s", sqlite3_errmsg(stmt->db->db3));
194
247
      err2 = svn_sqlite__reset(stmt);
195
248
      return svn_error_compose_create(err1, err2);
196
249
    }
197
250
 
198
251
  *got_row = (sqlite_result == SQLITE_ROW);
 
252
  stmt->needs_reset = TRUE;
199
253
 
200
254
  return SVN_NO_ERROR;
201
255
}
209
263
  if (row_id)
210
264
    *row_id = sqlite3_last_insert_rowid(stmt->db->db3);
211
265
 
212
 
  return svn_sqlite__reset(stmt);
213
 
}
 
266
  return svn_error_trace(svn_sqlite__reset(stmt));
 
267
}
 
268
 
 
269
svn_error_t *
 
270
svn_sqlite__update(int *affected_rows, svn_sqlite__stmt_t *stmt)
 
271
{
 
272
  SVN_ERR(step_with_expectation(stmt, FALSE));
 
273
 
 
274
  if (affected_rows)
 
275
    *affected_rows = sqlite3_changes(stmt->db->db3);
 
276
 
 
277
  return svn_error_trace(svn_sqlite__reset(stmt));
 
278
}
 
279
 
214
280
 
215
281
static svn_error_t *
216
282
vbindf(svn_sqlite__stmt_t *stmt, const char *fmt, va_list ap)
221
287
    {
222
288
      const void *blob;
223
289
      apr_size_t blob_size;
 
290
      const svn_token_map_t *map;
224
291
 
225
292
      switch (*fmt)
226
293
        {
240
307
            SVN_ERR(svn_sqlite__bind_blob(stmt, count, blob, blob_size));
241
308
            break;
242
309
 
 
310
          case 'r':
 
311
            SVN_ERR(svn_sqlite__bind_revnum(stmt, count,
 
312
                                            va_arg(ap, svn_revnum_t)));
 
313
            break;
 
314
 
 
315
          case 't':
 
316
            map = va_arg(ap, const svn_token_map_t *);
 
317
            SVN_ERR(svn_sqlite__bind_token(stmt, count, map, va_arg(ap, int)));
 
318
            break;
 
319
 
 
320
          case 'n':
 
321
            /* Skip this column: no binding */
 
322
            break;
 
323
 
243
324
          default:
244
325
            SVN_ERR_MALFUNCTION();
245
326
        }
257
338
  va_start(ap, fmt);
258
339
  err = vbindf(stmt, fmt, ap);
259
340
  va_end(ap);
260
 
  return err;
 
341
  return svn_error_trace(err);
261
342
}
262
343
 
263
344
svn_error_t *
294
375
                      const void *val,
295
376
                      apr_size_t len)
296
377
{
297
 
  SQLITE_ERR(sqlite3_bind_blob(stmt->s3stmt, slot, val, len, SQLITE_TRANSIENT),
298
 
             stmt->db);
299
 
  return SVN_NO_ERROR;
300
 
}
 
378
  SQLITE_ERR(sqlite3_bind_blob(stmt->s3stmt, slot, val, (int) len,
 
379
                               SQLITE_TRANSIENT),
 
380
             stmt->db);
 
381
  return SVN_NO_ERROR;
 
382
}
 
383
 
 
384
svn_error_t *
 
385
svn_sqlite__bind_token(svn_sqlite__stmt_t *stmt,
 
386
                       int slot,
 
387
                       const svn_token_map_t *map,
 
388
                       int value)
 
389
{
 
390
  const char *word = svn_token__to_word(map, value);
 
391
 
 
392
  SQLITE_ERR(sqlite3_bind_text(stmt->s3stmt, slot, word, -1, SQLITE_STATIC),
 
393
             stmt->db);
 
394
  return SVN_NO_ERROR;
 
395
}
 
396
 
 
397
svn_error_t *
 
398
svn_sqlite__bind_revnum(svn_sqlite__stmt_t *stmt,
 
399
                        int slot,
 
400
                        svn_revnum_t value)
 
401
{
 
402
  if (SVN_IS_VALID_REVNUM(value))
 
403
    SQLITE_ERR(sqlite3_bind_int64(stmt->s3stmt, slot,
 
404
                                  (sqlite_int64)value), stmt->db);
 
405
  else
 
406
    SQLITE_ERR(sqlite3_bind_null(stmt->s3stmt, slot), stmt->db);
 
407
 
 
408
  return SVN_NO_ERROR;
 
409
}
 
410
 
 
411
svn_error_t *
 
412
svn_sqlite__bind_properties(svn_sqlite__stmt_t *stmt,
 
413
                            int slot,
 
414
                            const apr_hash_t *props,
 
415
                            apr_pool_t *scratch_pool)
 
416
{
 
417
  svn_skel_t *skel;
 
418
  svn_stringbuf_t *properties;
 
419
 
 
420
  if (props == NULL)
 
421
    return svn_error_trace(svn_sqlite__bind_blob(stmt, slot, NULL, 0));
 
422
 
 
423
  SVN_ERR(svn_skel__unparse_proplist(&skel, (apr_hash_t *)props,
 
424
                                     scratch_pool));
 
425
  properties = svn_skel__unparse(skel, scratch_pool);
 
426
  return svn_error_trace(svn_sqlite__bind_blob(stmt,
 
427
                                               slot,
 
428
                                               properties->data,
 
429
                                               properties->len));
 
430
}
 
431
 
 
432
svn_error_t *
 
433
svn_sqlite__bind_checksum(svn_sqlite__stmt_t *stmt,
 
434
                          int slot,
 
435
                          const svn_checksum_t *checksum,
 
436
                          apr_pool_t *scratch_pool)
 
437
{
 
438
  const char *csum_str;
 
439
 
 
440
  if (checksum == NULL)
 
441
    csum_str = NULL;
 
442
  else
 
443
    csum_str = svn_checksum_serialize(checksum, scratch_pool, scratch_pool);
 
444
 
 
445
  return svn_error_trace(svn_sqlite__bind_text(stmt, slot, csum_str));
 
446
}
 
447
 
301
448
 
302
449
const void *
303
 
svn_sqlite__column_blob(svn_sqlite__stmt_t *stmt, int column, apr_size_t *len)
 
450
svn_sqlite__column_blob(svn_sqlite__stmt_t *stmt, int column,
 
451
                        apr_size_t *len, apr_pool_t *result_pool)
304
452
{
305
453
  const void *val = sqlite3_column_blob(stmt->s3stmt, column);
306
454
  *len = sqlite3_column_bytes(stmt->s3stmt, column);
 
455
 
 
456
  if (result_pool && val != NULL)
 
457
    val = apr_pmemdup(result_pool, val, *len);
 
458
 
307
459
  return val;
308
460
}
309
461
 
311
463
svn_sqlite__column_text(svn_sqlite__stmt_t *stmt, int column,
312
464
                        apr_pool_t *result_pool)
313
465
{
314
 
  const char *result = (const char *) sqlite3_column_text(stmt->s3stmt, column);
 
466
  /* cast from 'unsigned char' to regular 'char'  */
 
467
  const char *result = (const char *)sqlite3_column_text(stmt->s3stmt, column);
315
468
 
316
 
  if (result_pool)
 
469
  if (result_pool && result != NULL)
317
470
    result = apr_pstrdup(result_pool, result);
318
471
 
319
472
  return result;
322
475
svn_revnum_t
323
476
svn_sqlite__column_revnum(svn_sqlite__stmt_t *stmt, int column)
324
477
{
 
478
  if (svn_sqlite__column_is_null(stmt, column))
 
479
    return SVN_INVALID_REVNUM;
325
480
  return (svn_revnum_t) sqlite3_column_int64(stmt->s3stmt, column);
326
481
}
327
482
 
328
483
svn_boolean_t
329
484
svn_sqlite__column_boolean(svn_sqlite__stmt_t *stmt, int column)
330
485
{
331
 
  return (sqlite3_column_int64(stmt->s3stmt, column) != 0);
 
486
  return sqlite3_column_int64(stmt->s3stmt, column) != 0;
332
487
}
333
488
 
334
489
int
343
498
  return sqlite3_column_int64(stmt->s3stmt, column);
344
499
}
345
500
 
 
501
int
 
502
svn_sqlite__column_token(svn_sqlite__stmt_t *stmt,
 
503
                         int column,
 
504
                         const svn_token_map_t *map)
 
505
{
 
506
  /* cast from 'unsigned char' to regular 'char'  */
 
507
  const char *word = (const char *)sqlite3_column_text(stmt->s3stmt, column);
 
508
 
 
509
  return svn_token__from_word_strict(map, word);
 
510
}
 
511
 
 
512
svn_error_t *
 
513
svn_sqlite__column_properties(apr_hash_t **props,
 
514
                              svn_sqlite__stmt_t *stmt,
 
515
                              int column,
 
516
                              apr_pool_t *result_pool,
 
517
                              apr_pool_t *scratch_pool)
 
518
{
 
519
  apr_size_t len;
 
520
  const void *val;
 
521
 
 
522
  /* svn_skel__parse_proplist copies everything needed to result_pool */
 
523
  val = svn_sqlite__column_blob(stmt, column, &len, NULL);
 
524
  if (val == NULL)
 
525
    {
 
526
      *props = NULL;
 
527
      return SVN_NO_ERROR;
 
528
    }
 
529
 
 
530
  SVN_ERR(svn_skel__parse_proplist(props,
 
531
                                   svn_skel__parse(val, len, scratch_pool),
 
532
                                   result_pool));
 
533
 
 
534
  return SVN_NO_ERROR;
 
535
}
 
536
 
 
537
svn_error_t *
 
538
svn_sqlite__column_checksum(const svn_checksum_t **checksum,
 
539
                            svn_sqlite__stmt_t *stmt, int column,
 
540
                            apr_pool_t *result_pool)
 
541
{
 
542
  const char *digest = svn_sqlite__column_text(stmt, column, NULL);
 
543
 
 
544
  if (digest == NULL)
 
545
    *checksum = NULL;
 
546
  else
 
547
    SVN_ERR(svn_checksum_deserialize(checksum, digest,
 
548
                                     result_pool, result_pool));
 
549
 
 
550
  return SVN_NO_ERROR;
 
551
}
 
552
 
346
553
svn_boolean_t
347
554
svn_sqlite__column_is_null(svn_sqlite__stmt_t *stmt, int column)
348
555
{
349
556
  return sqlite3_column_type(stmt->s3stmt, column) == SQLITE_NULL;
350
557
}
351
558
 
 
559
int
 
560
svn_sqlite__column_bytes(svn_sqlite__stmt_t *stmt, int column)
 
561
{
 
562
  return sqlite3_column_bytes(stmt->s3stmt, column);
 
563
}
 
564
 
352
565
svn_error_t *
353
566
svn_sqlite__finalize(svn_sqlite__stmt_t *stmt)
354
567
{
360
573
svn_sqlite__reset(svn_sqlite__stmt_t *stmt)
361
574
{
362
575
  SQLITE_ERR(sqlite3_reset(stmt->s3stmt), stmt->db);
 
576
  SQLITE_ERR(sqlite3_clear_bindings(stmt->s3stmt), stmt->db);
 
577
  stmt->needs_reset = FALSE;
363
578
  return SVN_NO_ERROR;
364
579
}
365
580
 
 
581
 
 
582
svn_error_t *
 
583
svn_sqlite__set_schema_version(svn_sqlite__db_t *db,
 
584
                               int version,
 
585
                               apr_pool_t *scratch_pool)
 
586
{
 
587
  const char *pragma_cmd = apr_psprintf(scratch_pool,
 
588
                                        "PRAGMA user_version = %d;",
 
589
                                        version);
 
590
 
 
591
  return svn_error_trace(exec_sql(db, pragma_cmd));
 
592
}
 
593
 
 
594
 
366
595
/* Time (in milliseconds) to wait for sqlite locks before giving up. */
367
596
#define BUSY_TIMEOUT 10000
368
597
 
414
643
 
415
644
#endif
416
645
 
417
 
 
 
646
struct upgrade_baton
 
647
{
 
648
  int current_schema;
 
649
  int latest_schema;
 
650
  const char * const *upgrade_sql;
 
651
};
 
652
 
 
653
 
 
654
/* This implements svn_sqlite__transaction_callback_t */
418
655
static svn_error_t *
419
 
upgrade_format(svn_sqlite__db_t *db, int current_schema, int latest_schema,
420
 
               const char * const *upgrade_sql, apr_pool_t *scratch_pool)
 
656
upgrade_format(void *baton,
 
657
               svn_sqlite__db_t *db,
 
658
               apr_pool_t *scratch_pool)
421
659
{
422
 
  while (current_schema < latest_schema)
 
660
  struct upgrade_baton *ub = baton;
 
661
  int current_schema = ub->current_schema;
 
662
  apr_pool_t *iterpool = svn_pool_create(scratch_pool);
 
663
 
 
664
  while (current_schema < ub->latest_schema)
423
665
    {
424
 
      const char *pragma_cmd;
 
666
      svn_pool_clear(iterpool);
425
667
 
426
668
      /* Go to the next schema */
427
669
      current_schema++;
428
670
 
429
671
      /* Run the upgrade SQL */
430
 
      SVN_ERR(svn_sqlite__exec(db, upgrade_sql[current_schema]));
 
672
      if (ub->upgrade_sql[current_schema])
 
673
        SVN_ERR(exec_sql(db, ub->upgrade_sql[current_schema]));
431
674
 
432
675
      /* Update the user version pragma */
433
 
      pragma_cmd = apr_psprintf(scratch_pool,
434
 
                                "PRAGMA user_version = %d;",
435
 
                                current_schema);
436
 
      SVN_ERR(svn_sqlite__exec(db, pragma_cmd));
 
676
      SVN_ERR(svn_sqlite__set_schema_version(db, current_schema, iterpool));
437
677
    }
438
678
 
 
679
  svn_pool_destroy(iterpool);
 
680
 
439
681
  return SVN_NO_ERROR;
440
682
}
441
683
 
442
 
static svn_error_t *
443
 
get_schema(int *version, svn_sqlite__db_t *db, apr_pool_t *scratch_pool)
 
684
svn_error_t *
 
685
svn_sqlite__read_schema_version(int *version,
 
686
                                svn_sqlite__db_t *db,
 
687
                                apr_pool_t *scratch_pool)
444
688
{
445
689
  svn_sqlite__stmt_t *stmt;
446
690
 
447
 
  SVN_ERR(svn_sqlite__prepare(&stmt, db, "PRAGMA user_version;", scratch_pool));
 
691
  SVN_ERR(prepare_statement(&stmt, db, "PRAGMA user_version;", scratch_pool));
448
692
  SVN_ERR(svn_sqlite__step_row(stmt));
449
693
 
450
694
  *version = svn_sqlite__column_int(stmt, 0);
451
695
 
452
 
  return svn_sqlite__finalize(stmt);
 
696
  return svn_error_trace(svn_sqlite__finalize(stmt));
453
697
}
454
698
 
455
699
 
458
702
   or SVN_ERR_SQLITE_ERROR if an sqlite error occurs during validation.
459
703
   Return SVN_NO_ERROR if everything is okay. */
460
704
static svn_error_t *
461
 
check_format(svn_sqlite__db_t *db, int latest_schema,
462
 
             const char * const *upgrade_sql, apr_pool_t *scratch_pool)
 
705
check_format(svn_sqlite__db_t *db,
 
706
             int latest_schema,
 
707
             const char * const *upgrade_sql,
 
708
             apr_pool_t *scratch_pool)
463
709
{
464
710
  int current_schema;
465
711
 
466
712
  /* Validate that the schema exists as expected. */
467
 
  SVN_ERR(get_schema(&current_schema, db, scratch_pool));
 
713
  SVN_ERR(svn_sqlite__read_schema_version(&current_schema, db, scratch_pool));
468
714
 
469
715
  if (current_schema == latest_schema)
470
716
    return SVN_NO_ERROR;
471
717
 
472
718
  if (current_schema < latest_schema)
473
 
    return upgrade_format(db, current_schema, latest_schema, upgrade_sql,
474
 
                          scratch_pool);
 
719
    {
 
720
      struct upgrade_baton ub;
 
721
 
 
722
      ub.current_schema = current_schema;
 
723
      ub.latest_schema = latest_schema;
 
724
      ub.upgrade_sql = upgrade_sql;
 
725
 
 
726
      return svn_error_trace(svn_sqlite__with_transaction(
 
727
                               db, upgrade_format, &ub, scratch_pool));
 
728
    }
475
729
 
476
730
  return svn_error_createf(SVN_ERR_SQLITE_UNSUPPORTED_SCHEMA, NULL,
477
731
                           _("Schema format %d not recognized"),
478
732
                           current_schema);
479
733
}
480
734
 
481
 
static volatile svn_atomic_t sqlite_init_state;
 
735
static volatile svn_atomic_t sqlite_init_state = 0;
482
736
 
483
737
/* If possible, verify that SQLite was compiled in a thread-safe
484
738
   manner. */
486
740
static svn_error_t *
487
741
init_sqlite(void *baton, apr_pool_t *pool)
488
742
{
489
 
  if (sqlite3_libversion_number() < SQLITE_VERSION_NUMBER) {
490
 
    return svn_error_createf(SVN_ERR_SQLITE_ERROR, NULL,
491
 
                             _("SQLite compiled for %s, but running with %s"),
492
 
                             SQLITE_VERSION, sqlite3_libversion());
493
 
  }
 
743
  if (sqlite3_libversion_number() < SQLITE_VERSION_NUMBER)
 
744
    {
 
745
      return svn_error_createf(
 
746
                    SVN_ERR_SQLITE_ERROR, NULL,
 
747
                    _("SQLite compiled for %s, but running with %s"),
 
748
                    SQLITE_VERSION, sqlite3_libversion());
 
749
    }
494
750
 
495
751
#if APR_HAS_THREADS
496
 
#if SQLITE_VERSION_AT_LEAST(3,5,0)
 
752
 
497
753
  /* SQLite 3.5 allows verification of its thread-safety at runtime.
498
754
     Older versions are simply expected to have been configured with
499
755
     --enable-threadsafe, which compiles with -DSQLITE_THREADSAFE=1
502
758
    return svn_error_create(SVN_ERR_SQLITE_ERROR, NULL,
503
759
                            _("SQLite is required to be compiled and run in "
504
760
                              "thread-safe mode"));
505
 
#endif
506
 
#if SQLITE_VERSION_AT_LEAST(3,6,0)
 
761
 
507
762
  /* If SQLite has been already initialized, sqlite3_config() returns
508
763
     SQLITE_MISUSE. */
509
764
  {
510
765
    int err = sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
511
766
    if (err != SQLITE_OK && err != SQLITE_MISUSE)
512
767
      return svn_error_create(SQLITE_ERROR_CODE(err), NULL,
513
 
                              "Could not configure SQLite");
 
768
                              _("Could not configure SQLite"));
514
769
  }
515
 
  SQLITE_ERR_MSG(sqlite3_initialize(), "Could not initialize SQLite");
516
 
#endif
 
770
  SQLITE_ERR_MSG(sqlite3_initialize(), _("Could not initialize SQLite"));
 
771
 
517
772
#endif /* APR_HAS_THRADS */
518
773
 
519
774
  return SVN_NO_ERROR;
520
775
}
521
776
 
 
777
static svn_error_t *
 
778
internal_open(sqlite3 **db3, const char *path, svn_sqlite__mode_t mode,
 
779
              apr_pool_t *scratch_pool)
 
780
{
 
781
  {
 
782
    int flags;
 
783
 
 
784
    if (mode == svn_sqlite__mode_readonly)
 
785
      flags = SQLITE_OPEN_READONLY;
 
786
    else if (mode == svn_sqlite__mode_readwrite)
 
787
      flags = SQLITE_OPEN_READWRITE;
 
788
    else if (mode == svn_sqlite__mode_rwcreate)
 
789
      flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
 
790
    else
 
791
      SVN_ERR_MALFUNCTION();
 
792
 
 
793
    /* If this flag is defined (3.6.x), then let's turn off SQLite's mutexes.
 
794
       All svn objects are single-threaded, so we can already guarantee that
 
795
       our use of the SQLite handle will be serialized properly.
 
796
 
 
797
       Note: in 3.6.x, we've already config'd SQLite into MULTITHREAD mode,
 
798
       so this is probably redundant, but if we are running in a process where
 
799
       somebody initialized SQLite before us it is needed anyway.  */
 
800
#ifdef SQLITE_OPEN_NOMUTEX
 
801
    flags |= SQLITE_OPEN_NOMUTEX;
 
802
#endif
 
803
 
 
804
    /* Open the database. Note that a handle is returned, even when an error
 
805
       occurs (except for out-of-memory); thus, we can safely use it to
 
806
       extract an error message and construct an svn_error_t. */
 
807
    {
 
808
      /* We'd like to use SQLITE_ERR_MSG here, but we can't since it would
 
809
         just return an error and leave the database open.  So, we need to
 
810
         do this manually. */
 
811
      /* ### SQLITE_CANTOPEN */
 
812
      int err_code = sqlite3_open_v2(path, db3, flags, NULL);
 
813
      if (err_code != SQLITE_OK)
 
814
        {
 
815
          /* Save the error message before closing the SQLite handle. */
 
816
          char *msg = apr_pstrdup(scratch_pool, sqlite3_errmsg(*db3));
 
817
 
 
818
          /* We don't catch the error here, since we care more about the open
 
819
             error than the close error at this point. */
 
820
          sqlite3_close(*db3);
 
821
 
 
822
          return svn_error_createf(SQLITE_ERROR_CODE(err_code), NULL,
 
823
                                   "sqlite: %s: '%s'", msg, path);
 
824
        }
 
825
    }
 
826
  }
 
827
 
 
828
  /* Retry until timeout when database is busy. */
 
829
  SQLITE_ERR_MSG(sqlite3_busy_timeout(*db3, BUSY_TIMEOUT),
 
830
                 sqlite3_errmsg(*db3));
 
831
 
 
832
  return SVN_NO_ERROR;
 
833
}
 
834
 
522
835
svn_error_t *
523
836
svn_sqlite__get_schema_version(int *version,
524
837
                               const char *path,
528
841
 
529
842
  SVN_ERR(svn_atomic__init_once(&sqlite_init_state,
530
843
                                init_sqlite, NULL, scratch_pool));
531
 
  SQLITE_ERR(sqlite3_open(path, &db.db3), &db);
532
 
  SVN_ERR(get_schema(version, &db, scratch_pool));
 
844
  SVN_ERR(internal_open(&db.db3, path, svn_sqlite__mode_readonly,
 
845
                        scratch_pool));
 
846
  SVN_ERR(svn_sqlite__read_schema_version(version, &db, scratch_pool));
533
847
  SQLITE_ERR(sqlite3_close(db.db3), &db);
534
848
 
535
849
  return SVN_NO_ERROR;
536
850
}
537
851
 
538
 
/* APR cleanup function used to close the database when its pool is destoryed.
 
852
/* APR cleanup function used to close the database when its pool is destroyed.
539
853
   DATA should be the svn_sqlite__db_t handle for the database. */
540
854
static apr_status_t
541
855
close_apr(void *data)
542
856
{
543
857
  svn_sqlite__db_t *db = data;
544
858
  svn_error_t *err = SVN_NO_ERROR;
545
 
  int result;
 
859
  apr_status_t result;
546
860
  int i;
547
861
 
 
862
  /* Check to see if we've already closed this database. */
 
863
  if (db->db3 == NULL)
 
864
    return APR_SUCCESS;
 
865
 
548
866
  /* Finalize any existing prepared statements. */
549
867
  for (i = 0; i < db->nbr_statements; i++)
550
868
    {
566
884
  if (result != SQLITE_OK)
567
885
    return SQLITE_ERROR_CODE(result);
568
886
 
 
887
  db->db3 = NULL;
 
888
 
569
889
  return APR_SUCCESS;
570
890
}
571
891
 
 
892
 
572
893
svn_error_t *
573
894
svn_sqlite__open(svn_sqlite__db_t **db, const char *path,
574
895
                 svn_sqlite__mode_t mode, const char * const statements[],
578
899
  SVN_ERR(svn_atomic__init_once(&sqlite_init_state,
579
900
                                init_sqlite, NULL, scratch_pool));
580
901
 
581
 
  *db = apr_palloc(result_pool, sizeof(**db));
582
 
 
583
 
#if SQLITE_VERSION_AT_LEAST(3,5,0)
584
 
  {
585
 
    int flags;
586
 
 
587
 
    if (mode == svn_sqlite__mode_readonly)
588
 
      flags = SQLITE_OPEN_READONLY;
589
 
    else if (mode == svn_sqlite__mode_readwrite)
590
 
      flags = SQLITE_OPEN_READWRITE;
591
 
    else if (mode == svn_sqlite__mode_rwcreate)
592
 
      flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
593
 
    else
594
 
      SVN_ERR_MALFUNCTION();
595
 
 
596
 
    /* If this flag is defined (3.6.x), then let's turn off SQLite's mutexes.
597
 
       All svn objects are single-threaded, so we can already guarantee that
598
 
       our use of the SQLite handle will be serialized properly.
599
 
       Note: in 3.6.x, we've already config'd SQLite into MULTITHREAD mode,
600
 
       so this is probably redundant... */
601
 
    /* ### yeah. remove when autoconf magic is done for init/config. */
602
 
#ifdef SQLITE_OPEN_NOMUTEX
603
 
    flags |= SQLITE_OPEN_NOMUTEX;
604
 
#endif
605
 
 
606
 
    /* Open the database. Note that a handle is returned, even when an error
607
 
       occurs (except for out-of-memory); thus, we can safely use it to
608
 
       extract an error message and construct an svn_error_t. */
609
 
    SQLITE_ERR(sqlite3_open_v2(path, &(*db)->db3, flags, NULL), *db);
610
 
  }
611
 
#else
612
 
  /* Older versions of SQLite (pre-3.5.x) will always create the database
613
 
     if it doesn't exist.  So, if we are asked to be read-only or read-write,
614
 
     we ensure the database already exists - if it doesn't, then we will
615
 
     explicitly error out before asking SQLite to do anything.
616
 
 
617
 
     Pre-3.5.x SQLite versions also don't support read-only ops either.
 
902
  *db = apr_pcalloc(result_pool, sizeof(**db));
 
903
 
 
904
  SVN_ERR(internal_open(&(*db)->db3, path, mode, scratch_pool));
 
905
 
 
906
#ifdef SQLITE3_DEBUG
 
907
  sqlite3_trace((*db)->db3, sqlite_tracer, (*db)->db3);
 
908
#endif
 
909
#ifdef SQLITE3_PROFILE
 
910
  sqlite3_profile((*db)->db3, sqlite_profiler, (*db)->db3);
 
911
#endif
 
912
 
 
913
  /* Work around a bug in SQLite 3.7.7.  The bug was fixed in SQLite 3.7.7.1.
 
914
 
 
915
     See:
 
916
 
 
917
       Date: Sun, 26 Jun 2011 18:52:14 -0400
 
918
       From: Richard Hipp <drh@sqlite.org>
 
919
       To: General Discussion of SQLite Database <sqlite-users@sqlite.org>
 
920
       Cc: dev@subversion.apache.org
 
921
       Subject: Re: [sqlite] PRAGMA bug in 3.7.7 (but fine in 3.7.6.3)
 
922
       Message-ID: <BANLkTimDypWGY-8tHFgJsTxN6ty6OkdJ0Q@mail.gmail.com>
618
923
   */
619
 
  if (mode == svn_sqlite__mode_readonly || mode == svn_sqlite__mode_readwrite)
620
 
    {
621
 
      svn_node_kind_t kind;
622
 
      SVN_ERR(svn_io_check_path(path, &kind, scratch_pool));
623
 
      if (kind != svn_node_file) {
624
 
          return svn_error_createf(SVN_ERR_WC_CORRUPT, NULL,
625
 
                                   _("Expected SQLite database not found: %s"),
626
 
                                   svn_path_local_style(path, scratch_pool));
627
 
      }
628
 
    }
629
 
  else if (mode == svn_sqlite__mode_rwcreate)
630
 
    {
631
 
      /* do nothing - older SQLite's will create automatically. */
632
 
    }
633
 
  else
634
 
    SVN_ERR_MALFUNCTION();
635
 
 
636
 
  SQLITE_ERR(sqlite3_open(path, &(*db)->db3), *db);
637
 
#endif
638
 
 
639
 
  /* Retry until timeout when database is busy. */
640
 
  SQLITE_ERR(sqlite3_busy_timeout((*db)->db3, BUSY_TIMEOUT), *db);
641
 
#ifdef SQLITE3_DEBUG
642
 
  sqlite3_trace((*db)->db3, sqlite_tracer, (*db)->db3);
643
 
#endif
644
 
 
645
 
  SVN_ERR(svn_sqlite__exec(*db, "PRAGMA case_sensitive_like=on;"));
 
924
  {
 
925
    int ignored_err = SQLITE_OK;
 
926
#if !SQLITE_VERSION_AT_LEAST(3,7,8) && defined(SQLITE_SCHEMA)
 
927
    if (!strcmp(sqlite3_libversion(), "3.7.7"))
 
928
      ignored_err = SQLITE_SCHEMA;
 
929
#endif
 
930
 
 
931
    SVN_ERR(exec_sql2(*db, "PRAGMA case_sensitive_like=1;", ignored_err));
 
932
  }
 
933
 
 
934
  SVN_ERR(exec_sql(*db,
 
935
              /* Disable synchronization to disable the explicit disk flushes
 
936
                 that make Sqlite up to 50 times slower; especially on small
 
937
                 transactions.
 
938
 
 
939
                 This removes some stability guarantees on specific hardware
 
940
                 and power failures, but still guarantees atomic commits on
 
941
                 application crashes. With our dependency on external data
 
942
                 like pristine files (Wc) and revision files (repository),
 
943
                 we can't keep up these additional guarantees anyway.
 
944
 
 
945
                 ### Maybe switch to NORMAL(1) when we use larger transaction
 
946
                     scopes */
 
947
              "PRAGMA synchronous=OFF;"
 
948
              /* Enable recursive triggers so that a user trigger will fire
 
949
                 in the deletion phase of an INSERT OR REPLACE statement.
 
950
                 Requires SQLite >= 3.6.18  */
 
951
              "PRAGMA recursive_triggers=ON;"));
 
952
 
 
953
#if SQLITE_VERSION_AT_LEAST(3,6,19) && defined(SVN_DEBUG)
 
954
  /* When running in debug mode, enable the checking of foreign key
 
955
     constraints.  This has possible performance implications, so we don't
 
956
     bother to do it for production...for now. */
 
957
  SVN_ERR(exec_sql(*db, "PRAGMA foreign_keys=ON;"));
 
958
#endif
 
959
 
 
960
  /* Store temporary tables in RAM instead of in temporary files, but don't
 
961
     fail on this if this option is disabled in the sqlite compilation by
 
962
     setting SQLITE_TEMP_STORE to 0 (always to disk) */
 
963
  svn_error_clear(exec_sql(*db, "PRAGMA temp_store = MEMORY;"));
646
964
 
647
965
  /* Validate the schema, upgrading if necessary. */
648
 
  SVN_ERR(check_format(*db, latest_schema, upgrade_sql, scratch_pool));
 
966
  if (upgrade_sql != NULL)
 
967
    SVN_ERR(check_format(*db, latest_schema, upgrade_sql, scratch_pool));
649
968
 
650
969
  /* Store the provided statements. */
651
970
  if (statements)
663
982
  else
664
983
    (*db)->nbr_statements = 0;
665
984
 
666
 
  (*db)->result_pool = result_pool;
 
985
  (*db)->state_pool = result_pool;
667
986
  apr_pool_cleanup_register(result_pool, *db, close_apr, apr_pool_cleanup_null);
668
987
 
669
988
  return SVN_NO_ERROR;
670
989
}
671
990
 
672
991
svn_error_t *
 
992
svn_sqlite__close(svn_sqlite__db_t *db)
 
993
{
 
994
  apr_status_t result = apr_pool_cleanup_run(db->state_pool, db, close_apr);
 
995
 
 
996
  if (result == APR_SUCCESS)
 
997
    return SVN_NO_ERROR;
 
998
 
 
999
  return svn_error_wrap_apr(result, NULL);
 
1000
}
 
1001
 
 
1002
static svn_error_t *
 
1003
reset_all_statements(svn_sqlite__db_t *db,
 
1004
                     svn_error_t *error_to_wrap)
 
1005
{
 
1006
  int i;
 
1007
  svn_error_t *err;
 
1008
 
 
1009
  /* ### Should we reorder the errors in this specific case
 
1010
     ### to avoid returning the normal error as top level error? */
 
1011
 
 
1012
  err = svn_error_compose_create(error_to_wrap,
 
1013
                   svn_error_create(SVN_ERR_SQLITE_RESETTING_FOR_ROLLBACK,
 
1014
                                    NULL, NULL));
 
1015
 
 
1016
  for (i = 0; i < db->nbr_statements; i++)
 
1017
    if (db->prepared_stmts[i] && db->prepared_stmts[i]->needs_reset)
 
1018
      err = svn_error_compose_create(err,
 
1019
                                svn_sqlite__reset(db->prepared_stmts[i]));
 
1020
 
 
1021
  return err;
 
1022
}
 
1023
 
 
1024
/* The body of svn_sqlite__with_transaction() and
 
1025
   svn_sqlite__with_immediate_transaction(), which see. */
 
1026
static svn_error_t *
 
1027
with_transaction(svn_sqlite__db_t *db,
 
1028
                 svn_sqlite__transaction_callback_t cb_func,
 
1029
                 void *cb_baton,
 
1030
                 apr_pool_t *scratch_pool /* NULL allowed */)
 
1031
{
 
1032
  svn_error_t *err;
 
1033
 
 
1034
  err = cb_func(cb_baton, db, scratch_pool);
 
1035
 
 
1036
  /* Commit or rollback the sqlite transaction. */
 
1037
  if (err)
 
1038
    {
 
1039
      svn_error_t *err2 = exec_sql(db, "ROLLBACK TRANSACTION;");
 
1040
 
 
1041
      if (err2 && err2->apr_err == SVN_ERR_SQLITE_BUSY)
 
1042
        {
 
1043
          /* ### Houston, we have a problem!
 
1044
 
 
1045
             We are trying to rollback but we can't because some
 
1046
             statements are still busy. This leaves the database
 
1047
             unusable for future transactions as the current transaction
 
1048
             is still open.
 
1049
 
 
1050
             As we are returning the actual error as the most relevant
 
1051
             error in the chain, our caller might assume that it can
 
1052
             retry/compensate on this error (e.g. SVN_WC_LOCKED), while
 
1053
             in fact the SQLite database is unusable until the statements
 
1054
             started within this transaction are reset and the transaction
 
1055
             aborted.
 
1056
 
 
1057
             We try to compensate by resetting all prepared but unreset
 
1058
             statements; but we leave the busy error in the chain anyway to
 
1059
             help diagnosing the original error and help in finding where
 
1060
             a reset statement is missing. */
 
1061
 
 
1062
          err2 = reset_all_statements(db, err2);
 
1063
          err2 = svn_error_compose_create(
 
1064
                      exec_sql(db, "ROLLBACK TRANSACTION;"),
 
1065
                      err2);
 
1066
        }
 
1067
 
 
1068
      return svn_error_compose_create(err,
 
1069
                                      err2);
 
1070
    }
 
1071
 
 
1072
  return svn_error_trace(exec_sql(db, "COMMIT TRANSACTION;"));
 
1073
}
 
1074
 
 
1075
svn_error_t *
673
1076
svn_sqlite__with_transaction(svn_sqlite__db_t *db,
674
1077
                             svn_sqlite__transaction_callback_t cb_func,
675
 
                             void *cb_baton)
676
 
{
677
 
  svn_error_t *err;
678
 
 
679
 
  SVN_ERR(svn_sqlite__transaction_begin(db));
680
 
  err = cb_func(cb_baton, db);
681
 
 
682
 
  /* Commit or rollback the sqlite transaction. */
683
 
  if (err)
684
 
    {
685
 
      svn_error_clear(svn_sqlite__transaction_rollback(db));
686
 
      return err;
687
 
    }
688
 
  else
689
 
    return svn_sqlite__transaction_commit(db);
 
1078
                             void *cb_baton,
 
1079
                             apr_pool_t *scratch_pool /* NULL allowed */)
 
1080
{
 
1081
  SVN_ERR(exec_sql(db, "BEGIN TRANSACTION;"));
 
1082
  return svn_error_trace(with_transaction(db, cb_func, cb_baton,
 
1083
                                          scratch_pool));
 
1084
}
 
1085
 
 
1086
svn_error_t *
 
1087
svn_sqlite__with_immediate_transaction(
 
1088
  svn_sqlite__db_t *db,
 
1089
  svn_sqlite__transaction_callback_t cb_func,
 
1090
  void *cb_baton,
 
1091
  apr_pool_t *scratch_pool /* NULL allowed */)
 
1092
{
 
1093
  SVN_ERR(exec_sql(db, "BEGIN IMMEDIATE TRANSACTION;"));
 
1094
  return svn_error_trace(with_transaction(db, cb_func, cb_baton,
 
1095
                                          scratch_pool));
 
1096
}
 
1097
 
 
1098
svn_error_t *
 
1099
svn_sqlite__with_lock(svn_sqlite__db_t *db,
 
1100
                      svn_sqlite__transaction_callback_t cb_func,
 
1101
                      void *cb_baton,
 
1102
                      apr_pool_t *scratch_pool)
 
1103
{
 
1104
  svn_error_t *err;
 
1105
  int savepoint = db->savepoint_nr++;
 
1106
  /* This buffer is plenty big to hold the SAVEPOINT and RELEASE commands. */
 
1107
  char buf[32];
 
1108
 
 
1109
  snprintf(buf, sizeof(buf), "SAVEPOINT s%u", savepoint);
 
1110
  SVN_ERR(exec_sql(db, buf));
 
1111
  err = cb_func(cb_baton, db, scratch_pool);
 
1112
 
 
1113
  if (err)
 
1114
    {
 
1115
      svn_error_t *err2;
 
1116
 
 
1117
      snprintf(buf, sizeof(buf), "ROLLBACK TO s%u", savepoint);
 
1118
      err2 = exec_sql(db, buf);
 
1119
 
 
1120
      if (err2 && err2->apr_err == SVN_ERR_SQLITE_BUSY)
 
1121
        {
 
1122
          /* Ok, we have a major problem. Some statement is still open, which
 
1123
             makes it impossible to release this savepoint.
 
1124
 
 
1125
             ### See huge comment in svn_sqlite__with_transaction for
 
1126
                 further details */
 
1127
 
 
1128
          err2 = reset_all_statements(db, err2);
 
1129
          err2 = svn_error_compose_create(exec_sql(db, buf), err2);
 
1130
        }
 
1131
 
 
1132
      snprintf(buf, sizeof(buf), "RELEASE   s%u", savepoint);
 
1133
      err2 = svn_error_compose_create(exec_sql(db, buf), err2);
 
1134
 
 
1135
      return svn_error_trace(svn_error_compose_create(err, err2));
 
1136
    }
 
1137
 
 
1138
  snprintf(buf, sizeof(buf), "RELEASE   s%u", savepoint);
 
1139
  return svn_error_trace(exec_sql(db, buf));
 
1140
}
 
1141
 
 
1142
svn_error_t *
 
1143
svn_sqlite__hotcopy(const char *src_path,
 
1144
                    const char *dst_path,
 
1145
                    apr_pool_t *scratch_pool)
 
1146
{
 
1147
  svn_sqlite__db_t *src_db;
 
1148
 
 
1149
  SVN_ERR(svn_sqlite__open(&src_db, src_path, svn_sqlite__mode_readonly,
 
1150
                           internal_statements, 0, NULL,
 
1151
                           scratch_pool, scratch_pool));
 
1152
 
 
1153
  {
 
1154
    svn_sqlite__db_t *dst_db;
 
1155
    sqlite3_backup *backup;
 
1156
    int rc1, rc2;
 
1157
 
 
1158
    SVN_ERR(svn_sqlite__open(&dst_db, dst_path, svn_sqlite__mode_rwcreate,
 
1159
                             NULL, 0, NULL, scratch_pool, scratch_pool));
 
1160
    backup = sqlite3_backup_init(dst_db->db3, "main", src_db->db3, "main");
 
1161
    if (!backup)
 
1162
      return svn_error_createf(SVN_ERR_SQLITE_ERROR, NULL,
 
1163
                               _("SQLite hotcopy failed for %s"), src_path);
 
1164
    do
 
1165
      {
 
1166
        /* Pages are usually 1024 byte (SQLite docs). On my laptop
 
1167
           copying gets faster as the number of pages is increased up
 
1168
           to about 64, beyond that speed levels off.  Lets put the
 
1169
           number of pages an order of magnitude higher, this is still
 
1170
           likely to be a fraction of large databases. */
 
1171
        rc1 = sqlite3_backup_step(backup, 1024);
 
1172
 
 
1173
        /* Should we sleep on SQLITE_OK?  That would make copying a
 
1174
           large database take much longer.  When we do sleep how,
 
1175
           long should we sleep?  Should the sleep get longer if we
 
1176
           keep getting BUSY/LOCKED?  I have no real reason for
 
1177
           choosing 25. */
 
1178
        if (rc1 == SQLITE_BUSY || rc1 == SQLITE_LOCKED)
 
1179
          sqlite3_sleep(25);
 
1180
      }
 
1181
    while (rc1 == SQLITE_OK || rc1 == SQLITE_BUSY || rc1 == SQLITE_LOCKED);
 
1182
    rc2 = sqlite3_backup_finish(backup);
 
1183
    if (rc1 != SQLITE_DONE)
 
1184
      SQLITE_ERR(rc1, dst_db);
 
1185
    SQLITE_ERR(rc2, dst_db);
 
1186
    SVN_ERR(svn_sqlite__close(dst_db));
 
1187
  }
 
1188
 
 
1189
  SVN_ERR(svn_sqlite__close(src_db));
 
1190
 
 
1191
  return SVN_NO_ERROR;
 
1192
}
 
1193
 
 
1194
struct function_wrapper_baton_t
 
1195
{
 
1196
  svn_sqlite__func_t func;
 
1197
  void *baton;
 
1198
 
 
1199
  apr_pool_t *scratch_pool;
 
1200
};
 
1201
 
 
1202
static void
 
1203
wrapped_func(sqlite3_context *context,
 
1204
             int argc,
 
1205
             sqlite3_value *values[])
 
1206
{
 
1207
  struct function_wrapper_baton_t *fwb = sqlite3_user_data(context);
 
1208
  svn_sqlite__context_t sctx = { context };
 
1209
  svn_sqlite__value_t **local_vals =
 
1210
                            apr_palloc(fwb->scratch_pool,
 
1211
                                       sizeof(svn_sqlite__value_t *) * argc);
 
1212
  svn_error_t *err;
 
1213
  int i;
 
1214
 
 
1215
  for (i = 0; i < argc; i++)
 
1216
    {
 
1217
      local_vals[i] = apr_palloc(fwb->scratch_pool, sizeof(*local_vals[i]));
 
1218
      local_vals[i]->value = values[i];
 
1219
    }
 
1220
 
 
1221
  err = fwb->func(&sctx, argc, local_vals, fwb->scratch_pool);
 
1222
  svn_pool_clear(fwb->scratch_pool);
 
1223
 
 
1224
  if (err)
 
1225
    {
 
1226
      char buf[256];
 
1227
      sqlite3_result_error(context,
 
1228
                           svn_err_best_message(err, buf, sizeof(buf)),
 
1229
                           -1);
 
1230
      svn_error_clear(err);
 
1231
    }
 
1232
}
 
1233
 
 
1234
svn_error_t *
 
1235
svn_sqlite__create_scalar_function(svn_sqlite__db_t *db,
 
1236
                                   const char *func_name,
 
1237
                                   int argc,
 
1238
                                   svn_sqlite__func_t func,
 
1239
                                   void *baton)
 
1240
{
 
1241
  struct function_wrapper_baton_t *fwb = apr_pcalloc(db->state_pool,
 
1242
                                                     sizeof(*fwb));
 
1243
 
 
1244
  fwb->scratch_pool = svn_pool_create(db->state_pool);
 
1245
  fwb->func = func;
 
1246
  fwb->baton = baton;
 
1247
 
 
1248
  SQLITE_ERR(sqlite3_create_function(db->db3, func_name, argc, SQLITE_ANY,
 
1249
                                     fwb, wrapped_func, NULL, NULL),
 
1250
             db);
 
1251
 
 
1252
  return SVN_NO_ERROR;
 
1253
}
 
1254
 
 
1255
int
 
1256
svn_sqlite__value_type(svn_sqlite__value_t *val)
 
1257
{
 
1258
  return sqlite3_value_type(val->value);
 
1259
}
 
1260
 
 
1261
const char *
 
1262
svn_sqlite__value_text(svn_sqlite__value_t *val)
 
1263
{
 
1264
  return (const char *) sqlite3_value_text(val->value);
 
1265
}
 
1266
 
 
1267
void
 
1268
svn_sqlite__result_null(svn_sqlite__context_t *sctx)
 
1269
{
 
1270
  sqlite3_result_null(sctx->context);
 
1271
}
 
1272
 
 
1273
void
 
1274
svn_sqlite__result_int64(svn_sqlite__context_t *sctx, apr_int64_t val)
 
1275
{
 
1276
  sqlite3_result_int64(sctx->context, val);
690
1277
}