1/* sqlite.c
2 *
3 * ====================================================================
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.
20 * ====================================================================
21 */
22
23#include <apr_pools.h>
24
25#include "svn_types.h"
26#include "svn_error.h"
27#include "svn_pools.h"
28#include "svn_io.h"
29#include "svn_dirent_uri.h"
30#include "svn_checksum.h"
31
32#include "internal_statements.h"
33
34#include "private/svn_sqlite.h"
35#include "svn_private_config.h"
36#include "private/svn_dep_compat.h"
37#include "private/svn_atomic.h"
38#include "private/svn_skel.h"
39#include "private/svn_token.h"
40#ifdef WIN32
41#include "private/svn_io_private.h"
42#include "private/svn_utf_private.h"
43#endif
44
45#ifdef SVN_UNICODE_NORMALIZATION_FIXES
46#include "private/svn_utf_private.h"
47#include "private/svn_string_private.h"
48#endif /* SVN_UNICODE_NORMALIZATION_FIXES */
49
50#ifdef SQLITE3_DEBUG
51#include "private/svn_debug.h"
52#endif
53
54#ifdef SVN_SQLITE_INLINE
55/* Import the sqlite3 API vtable from sqlite3wrapper.c */
56#  define SQLITE_OMIT_DEPRECATED
57#  include <sqlite3ext.h>
58extern const sqlite3_api_routines *const svn_sqlite3__api_funcs;
59extern int (*const svn_sqlite3__api_initialize)(void);
60extern int (*const svn_sqlite3__api_config)(int, ...);
61#  define sqlite3_api svn_sqlite3__api_funcs
62#  define sqlite3_initialize svn_sqlite3__api_initialize
63#  define sqlite3_config svn_sqlite3__api_config
64#else
65#  include <sqlite3.h>
66#endif
67
68#if !SQLITE_VERSION_AT_LEAST(3,7,12)
69#error SQLite is too old -- version 3.7.12 is the minimum required version
70#endif
71
72#ifndef SQLITE_DETERMINISTIC
73#define SQLITE_DETERMINISTIC 0
74#endif
75
76#ifdef SVN_UNICODE_NORMALIZATION_FIXES
77/* Limit the length of a GLOB or LIKE pattern. */
78#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
79# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
80#endif
81#endif /* SVN_UNICODE_NORMALIZATION_FIXES */
82
83const char *
84svn_sqlite__compiled_version(void)
85{
86  static const char sqlite_version[] = SQLITE_VERSION;
87  return sqlite_version;
88}
89
90const char *
91svn_sqlite__runtime_version(void)
92{
93  return sqlite3_libversion();
94}
95
96
97INTERNAL_STATEMENTS_SQL_DECLARE_STATEMENTS(internal_statements);
98
99
100#ifdef SQLITE3_DEBUG
101/* An sqlite query execution callback. */
102static void
103sqlite_tracer(void *data, const char *sql)
104{
105  /*  sqlite3 *db3 = data; */
106  SVN_DBG(("sql=\"%s\"\n", sql));
107}
108#endif
109
110#ifdef SQLITE3_PROFILE
111/* An sqlite execution timing callback. */
112static void
113sqlite_profiler(void *data, const char *sql, sqlite3_uint64 duration)
114{
115  /*  sqlite3 *db3 = data; */
116  SVN_DBG(("[%.3f] sql=\"%s\"\n", 1e-9 * duration, sql));
117}
118#endif
119
120#if defined(SVN_DEBUG) && defined(SQLITE_CONFIG_LOG)
121static void
122sqlite_error_log(void* baton, int err, const char* msg)
123{
124  fprintf(SVN_DBG_OUTPUT, "DBG: sqlite[S%d]: %s\n", err, msg);
125}
126#endif
127
128void
129svn_sqlite__dbg_enable_errorlog(void)
130{
131#if defined(SVN_DEBUG) && defined(SQLITE_CONFIG_LOG)
132  sqlite3_config(SQLITE_CONFIG_LOG, sqlite_error_log, (void*)NULL /* baton */);
133#endif
134}
135
136
137struct svn_sqlite__db_t
138{
139  sqlite3 *db3;
140  const char * const *statement_strings;
141  int nbr_statements;
142  svn_sqlite__stmt_t **prepared_stmts;
143  apr_pool_t *state_pool;
144
145#ifdef SVN_UNICODE_NORMALIZATION_FIXES
146  /* Buffers for SQLite extensoins. */
147  svn_membuf_t sqlext_buf1;
148  svn_membuf_t sqlext_buf2;
149  svn_membuf_t sqlext_buf3;
150#endif /* SVN_UNICODE_NORMALIZATION_FIXES */
151};
152
153struct svn_sqlite__stmt_t
154{
155  sqlite3_stmt *s3stmt;
156  svn_sqlite__db_t *db;
157  svn_boolean_t needs_reset;
158};
159
160struct svn_sqlite__context_t
161{
162  sqlite3_context *context;
163};
164
165struct svn_sqlite__value_t
166{
167  sqlite3_value *value;
168};
169
170
171/* Convert SQLite error codes to SVN. Evaluates X multiple times */
172#define SQLITE_ERROR_CODE(x) ((x) == SQLITE_READONLY            \
173                              ? SVN_ERR_SQLITE_READONLY         \
174                              : ((x) == SQLITE_BUSY             \
175                                 ? SVN_ERR_SQLITE_BUSY          \
176                                 : ((x) == SQLITE_CONSTRAINT    \
177                                    ? SVN_ERR_SQLITE_CONSTRAINT \
178                                    : SVN_ERR_SQLITE_ERROR)))
179
180
181/* SQLITE->SVN quick error wrap, much like SVN_ERR. */
182#define SQLITE_ERR(x, db) do                                     \
183{                                                                \
184  int sqlite_err__temp = (x);                                    \
185  if (sqlite_err__temp != SQLITE_OK)                             \
186    return svn_error_createf(SQLITE_ERROR_CODE(sqlite_err__temp), \
187                             NULL, "sqlite[S%d]: %s",             \
188                             sqlite_err__temp,                    \
189                             sqlite3_errmsg((db)->db3));          \
190} while (0)
191
192#define SQLITE_ERR_CLOSE(x, db, pool) do                          \
193{                                                                 \
194  int sqlite_err__temp = (x);                                     \
195  if (sqlite_err__temp != SQLITE_OK)                              \
196    {                                                             \
197      const char *sqlite_err__msg                                 \
198        = apr_pstrdup(pool, sqlite3_errmsg((db)->db3));           \
199      return svn_error_compose_create(                            \
200           svn_error_createf(SQLITE_ERROR_CODE(sqlite_err__temp), \
201                             NULL, "sqlite[S%d]: %s",             \
202                             sqlite_err__temp, sqlite_err__msg),  \
203           svn_sqlite__close(db));                                \
204    }                                                             \
205} while (0)
206
207#define SQLITE_ERR_MSG(x, msg) do                                \
208{                                                                \
209  int sqlite_err__temp = (x);                                    \
210  if (sqlite_err__temp != SQLITE_OK)                             \
211    return svn_error_createf(SQLITE_ERROR_CODE(sqlite_err__temp), \
212                             NULL, "sqlite[S%d]: %s",            \
213                             sqlite_err__temp, msg);             \
214} while (0)
215
216#define SVN_ERR_CLOSE(x, db) do                                       \
217{                                                                     \
218  svn_error_t *svn__err = (x);                                        \
219  if (svn__err)                                                       \
220    return svn_error_compose_create(svn__err, svn_sqlite__close(db)); \
221} while (0)
222
223
224/* Time (in milliseconds) to wait for sqlite locks before giving up. */
225#define BUSY_TIMEOUT 10000
226
227
228/* Convenience wrapper around exec_sql2(). */
229#define exec_sql(db, sql) exec_sql2((db), (sql), SQLITE_OK)
230
231/* Run the statement SQL on DB, ignoring SQLITE_OK and IGNORED_ERR.
232   (Note: the IGNORED_ERR parameter itself is not ignored.) */
233static svn_error_t *
234exec_sql2(svn_sqlite__db_t *db, const char *sql, int ignored_err)
235{
236  char *err_msg;
237  int sqlite_err = sqlite3_exec(db->db3, sql, NULL, NULL, &err_msg);
238
239  if (sqlite_err != SQLITE_OK && sqlite_err != ignored_err)
240    {
241      svn_error_t *err = svn_error_createf(SQLITE_ERROR_CODE(sqlite_err), NULL,
242                                           _("sqlite[S%d]: %s,"
243                                             " executing statement '%s'"),
244                                           sqlite_err, err_msg, sql);
245      sqlite3_free(err_msg);
246      return err;
247    }
248
249  return SVN_NO_ERROR;
250}
251
252
253static svn_error_t *
254prepare_statement(svn_sqlite__stmt_t **stmt, svn_sqlite__db_t *db,
255                  const char *text, apr_pool_t *result_pool)
256{
257  *stmt = apr_palloc(result_pool, sizeof(**stmt));
258  (*stmt)->db = db;
259  (*stmt)->needs_reset = FALSE;
260
261  SQLITE_ERR(sqlite3_prepare_v2(db->db3, text, -1, &(*stmt)->s3stmt, NULL), db);
262
263  return SVN_NO_ERROR;
264}
265
266
267svn_error_t *
268svn_sqlite__exec_statements(svn_sqlite__db_t *db, int stmt_idx)
269{
270  SVN_ERR_ASSERT(stmt_idx < db->nbr_statements);
271
272  return svn_error_trace(exec_sql(db, db->statement_strings[stmt_idx]));
273}
274
275
276svn_error_t *
277svn_sqlite__get_statement(svn_sqlite__stmt_t **stmt, svn_sqlite__db_t *db,
278                          int stmt_idx)
279{
280  SVN_ERR_ASSERT(stmt_idx < db->nbr_statements);
281
282  if (db->prepared_stmts[stmt_idx] == NULL)
283    SVN_ERR(prepare_statement(&db->prepared_stmts[stmt_idx], db,
284                              db->statement_strings[stmt_idx],
285                              db->state_pool));
286
287  *stmt = db->prepared_stmts[stmt_idx];
288
289  if ((*stmt)->needs_reset)
290    return svn_error_trace(svn_sqlite__reset(*stmt));
291
292  return SVN_NO_ERROR;
293}
294
295/* Like svn_sqlite__get_statement but gets an internal statement.
296
297   All internal statements that use this api are executed with step_done(),
298   so we don't need the fallback reset handling here or in the pool cleanup */
299static svn_error_t *
300get_internal_statement(svn_sqlite__stmt_t **stmt, svn_sqlite__db_t *db,
301                       int stmt_idx)
302{
303  /* The internal statements are stored after the registered statements */
304  int prep_idx = db->nbr_statements + stmt_idx;
305  SVN_ERR_ASSERT(stmt_idx < STMT_INTERNAL_LAST);
306
307  if (db->prepared_stmts[prep_idx] == NULL)
308    SVN_ERR(prepare_statement(&db->prepared_stmts[prep_idx], db,
309                              internal_statements[stmt_idx],
310                              db->state_pool));
311
312  *stmt = db->prepared_stmts[prep_idx];
313
314  return SVN_NO_ERROR;
315}
316
317
318static svn_error_t *
319step_with_expectation(svn_sqlite__stmt_t* stmt,
320                      svn_boolean_t expecting_row)
321{
322  svn_boolean_t got_row;
323
324  SVN_ERR(svn_sqlite__step(&got_row, stmt));
325  if ((got_row && !expecting_row)
326      ||
327      (!got_row && expecting_row))
328    return svn_error_create(SVN_ERR_SQLITE_ERROR,
329                            svn_sqlite__reset(stmt),
330                            expecting_row
331                              ? _("sqlite: Expected database row missing")
332                              : _("sqlite: Extra database row found"));
333
334  return SVN_NO_ERROR;
335}
336
337svn_error_t *
338svn_sqlite__step_done(svn_sqlite__stmt_t *stmt)
339{
340  SVN_ERR(step_with_expectation(stmt, FALSE));
341  return svn_error_trace(svn_sqlite__reset(stmt));
342}
343
344svn_error_t *
345svn_sqlite__step_row(svn_sqlite__stmt_t *stmt)
346{
347  return svn_error_trace(step_with_expectation(stmt, TRUE));
348}
349
350
351svn_error_t *
352svn_sqlite__step(svn_boolean_t *got_row, svn_sqlite__stmt_t *stmt)
353{
354  int sqlite_result = sqlite3_step(stmt->s3stmt);
355
356  if (sqlite_result != SQLITE_DONE && sqlite_result != SQLITE_ROW)
357    {
358      svn_error_t *err1, *err2;
359
360      err1 = svn_error_createf(SQLITE_ERROR_CODE(sqlite_result), NULL,
361                               "sqlite[S%d]: %s",
362                               sqlite_result, sqlite3_errmsg(stmt->db->db3));
363      err2 = svn_sqlite__reset(stmt);
364      return svn_error_compose_create(err1, err2);
365    }
366
367  *got_row = (sqlite_result == SQLITE_ROW);
368  stmt->needs_reset = TRUE;
369
370  return SVN_NO_ERROR;
371}
372
373svn_error_t *
374svn_sqlite__insert(apr_int64_t *row_id, svn_sqlite__stmt_t *stmt)
375{
376  svn_boolean_t got_row;
377
378  SVN_ERR(svn_sqlite__step(&got_row, stmt));
379  if (row_id)
380    *row_id = sqlite3_last_insert_rowid(stmt->db->db3);
381
382  return svn_error_trace(svn_sqlite__reset(stmt));
383}
384
385svn_error_t *
386svn_sqlite__update(int *affected_rows, svn_sqlite__stmt_t *stmt)
387{
388  SVN_ERR(step_with_expectation(stmt, FALSE));
389
390  if (affected_rows)
391    *affected_rows = sqlite3_changes(stmt->db->db3);
392
393  return svn_error_trace(svn_sqlite__reset(stmt));
394}
395
396
397static svn_error_t *
398vbindf(svn_sqlite__stmt_t *stmt, const char *fmt, va_list ap)
399{
400  int count;
401
402  for (count = 1; *fmt; fmt++, count++)
403    {
404      const void *blob;
405      apr_size_t blob_size;
406      const svn_token_map_t *map;
407
408      switch (*fmt)
409        {
410          case 's':
411            SVN_ERR(svn_sqlite__bind_text(stmt, count,
412                                          va_arg(ap, const char *)));
413            break;
414
415          case 'd':
416            SVN_ERR(svn_sqlite__bind_int(stmt, count,
417                                         va_arg(ap, int)));
418            break;
419
420          case 'i':
421          case 'L':
422            SVN_ERR(svn_sqlite__bind_int64(stmt, count,
423                                           va_arg(ap, apr_int64_t)));
424            break;
425
426          case 'b':
427            blob = va_arg(ap, const void *);
428            blob_size = va_arg(ap, apr_size_t);
429            SVN_ERR(svn_sqlite__bind_blob(stmt, count, blob, blob_size));
430            break;
431
432          case 'r':
433            SVN_ERR(svn_sqlite__bind_revnum(stmt, count,
434                                            va_arg(ap, svn_revnum_t)));
435            break;
436
437          case 't':
438            map = va_arg(ap, const svn_token_map_t *);
439            SVN_ERR(svn_sqlite__bind_token(stmt, count, map, va_arg(ap, int)));
440            break;
441
442          case 'n':
443            /* Skip this column: no binding */
444            break;
445
446          default:
447            SVN_ERR_MALFUNCTION();
448        }
449    }
450
451  return SVN_NO_ERROR;
452}
453
454svn_error_t *
455svn_sqlite__bindf(svn_sqlite__stmt_t *stmt, const char *fmt, ...)
456{
457  svn_error_t *err;
458  va_list ap;
459
460  va_start(ap, fmt);
461  err = vbindf(stmt, fmt, ap);
462  va_end(ap);
463  return svn_error_trace(err);
464}
465
466svn_error_t *
467svn_sqlite__bind_int(svn_sqlite__stmt_t *stmt,
468                     int slot,
469                     int val)
470{
471  SQLITE_ERR(sqlite3_bind_int(stmt->s3stmt, slot, val), stmt->db);
472  return SVN_NO_ERROR;
473}
474
475svn_error_t *
476svn_sqlite__bind_int64(svn_sqlite__stmt_t *stmt,
477                       int slot,
478                       apr_int64_t val)
479{
480  SQLITE_ERR(sqlite3_bind_int64(stmt->s3stmt, slot, val), stmt->db);
481  return SVN_NO_ERROR;
482}
483
484svn_error_t *
485svn_sqlite__bind_text(svn_sqlite__stmt_t *stmt,
486                      int slot,
487                      const char *val)
488{
489  SQLITE_ERR(sqlite3_bind_text(stmt->s3stmt, slot, val, -1, SQLITE_TRANSIENT),
490             stmt->db);
491  return SVN_NO_ERROR;
492}
493
494svn_error_t *
495svn_sqlite__bind_blob(svn_sqlite__stmt_t *stmt,
496                      int slot,
497                      const void *val,
498                      apr_size_t len)
499{
500  SQLITE_ERR(sqlite3_bind_blob(stmt->s3stmt, slot, val, (int) len,
501                               SQLITE_TRANSIENT),
502             stmt->db);
503  return SVN_NO_ERROR;
504}
505
506svn_error_t *
507svn_sqlite__bind_token(svn_sqlite__stmt_t *stmt,
508                       int slot,
509                       const svn_token_map_t *map,
510                       int value)
511{
512  const char *word = svn_token__to_word(map, value);
513
514  SQLITE_ERR(sqlite3_bind_text(stmt->s3stmt, slot, word, -1, SQLITE_STATIC),
515             stmt->db);
516  return SVN_NO_ERROR;
517}
518
519svn_error_t *
520svn_sqlite__bind_revnum(svn_sqlite__stmt_t *stmt,
521                        int slot,
522                        svn_revnum_t value)
523{
524  if (SVN_IS_VALID_REVNUM(value))
525    SQLITE_ERR(sqlite3_bind_int64(stmt->s3stmt, slot,
526                                  (sqlite_int64)value), stmt->db);
527  else
528    SQLITE_ERR(sqlite3_bind_null(stmt->s3stmt, slot), stmt->db);
529
530  return SVN_NO_ERROR;
531}
532
533svn_error_t *
534svn_sqlite__bind_properties(svn_sqlite__stmt_t *stmt,
535                            int slot,
536                            const apr_hash_t *props,
537                            apr_pool_t *scratch_pool)
538{
539  svn_skel_t *skel;
540  svn_stringbuf_t *properties;
541
542  if (props == NULL)
543    return svn_error_trace(svn_sqlite__bind_blob(stmt, slot, NULL, 0));
544
545  SVN_ERR(svn_skel__unparse_proplist(&skel, props, scratch_pool));
546  properties = svn_skel__unparse(skel, scratch_pool);
547  return svn_error_trace(svn_sqlite__bind_blob(stmt,
548                                               slot,
549                                               properties->data,
550                                               properties->len));
551}
552
553svn_error_t *
554svn_sqlite__bind_iprops(svn_sqlite__stmt_t *stmt,
555                        int slot,
556                        const apr_array_header_t *inherited_props,
557                        apr_pool_t *scratch_pool)
558{
559  svn_skel_t *skel;
560  svn_stringbuf_t *properties;
561
562  if (inherited_props == NULL)
563    return svn_error_trace(svn_sqlite__bind_blob(stmt, slot, NULL, 0));
564
565  SVN_ERR(svn_skel__unparse_iproplist(&skel, inherited_props,
566                                      scratch_pool, scratch_pool));
567  properties = svn_skel__unparse(skel, scratch_pool);
568  return svn_error_trace(svn_sqlite__bind_blob(stmt,
569                                               slot,
570                                               properties->data,
571                                               properties->len));
572}
573
574svn_error_t *
575svn_sqlite__bind_checksum(svn_sqlite__stmt_t *stmt,
576                          int slot,
577                          const svn_checksum_t *checksum,
578                          apr_pool_t *scratch_pool)
579{
580  const char *csum_str;
581
582  if (checksum == NULL)
583    csum_str = NULL;
584  else
585    csum_str = svn_checksum_serialize(checksum, scratch_pool, scratch_pool);
586
587  return svn_error_trace(svn_sqlite__bind_text(stmt, slot, csum_str));
588}
589
590
591const void *
592svn_sqlite__column_blob(svn_sqlite__stmt_t *stmt, int column,
593                        apr_size_t *len, apr_pool_t *result_pool)
594{
595  const void *val = sqlite3_column_blob(stmt->s3stmt, column);
596  *len = sqlite3_column_bytes(stmt->s3stmt, column);
597
598  if (result_pool && val != NULL)
599    val = apr_pmemdup(result_pool, val, *len);
600
601  return val;
602}
603
604const char *
605svn_sqlite__column_text(svn_sqlite__stmt_t *stmt, int column,
606                        apr_pool_t *result_pool)
607{
608  /* cast from 'unsigned char' to regular 'char'  */
609  const char *result = (const char *)sqlite3_column_text(stmt->s3stmt, column);
610
611  if (result_pool && result != NULL)
612    result = apr_pstrdup(result_pool, result);
613
614  return result;
615}
616
617svn_revnum_t
618svn_sqlite__column_revnum(svn_sqlite__stmt_t *stmt, int column)
619{
620  if (svn_sqlite__column_is_null(stmt, column))
621    return SVN_INVALID_REVNUM;
622  return (svn_revnum_t) sqlite3_column_int64(stmt->s3stmt, column);
623}
624
625svn_boolean_t
626svn_sqlite__column_boolean(svn_sqlite__stmt_t *stmt, int column)
627{
628  return sqlite3_column_int64(stmt->s3stmt, column) != 0;
629}
630
631int
632svn_sqlite__column_int(svn_sqlite__stmt_t *stmt, int column)
633{
634  return sqlite3_column_int(stmt->s3stmt, column);
635}
636
637apr_int64_t
638svn_sqlite__column_int64(svn_sqlite__stmt_t *stmt, int column)
639{
640  return sqlite3_column_int64(stmt->s3stmt, column);
641}
642
643int
644svn_sqlite__column_token(svn_sqlite__stmt_t *stmt,
645                         int column,
646                         const svn_token_map_t *map)
647{
648  /* cast from 'unsigned char' to regular 'char'  */
649  const char *word = (const char *)sqlite3_column_text(stmt->s3stmt, column);
650
651  return svn_token__from_word_strict(map, word);
652}
653
654int
655svn_sqlite__column_token_null(svn_sqlite__stmt_t *stmt,
656                              int column,
657                              const svn_token_map_t *map,
658                              int null_val)
659{
660  /* cast from 'unsigned char' to regular 'char'  */
661  const char *word = (const char *)sqlite3_column_text(stmt->s3stmt, column);
662
663  if (!word)
664    return null_val;
665
666  return svn_token__from_word_strict(map, word);
667}
668
669svn_error_t *
670svn_sqlite__column_properties(apr_hash_t **props,
671                              svn_sqlite__stmt_t *stmt,
672                              int column,
673                              apr_pool_t *result_pool,
674                              apr_pool_t *scratch_pool)
675{
676  apr_size_t len;
677  const void *val;
678
679  /* svn_skel__parse_proplist copies everything needed to result_pool */
680  val = svn_sqlite__column_blob(stmt, column, &len, NULL);
681  if (val == NULL)
682    {
683      *props = NULL;
684      return SVN_NO_ERROR;
685    }
686
687  SVN_ERR(svn_skel__parse_proplist(props,
688                                   svn_skel__parse(val, len, scratch_pool),
689                                   result_pool));
690
691  return SVN_NO_ERROR;
692}
693
694svn_error_t *
695svn_sqlite__column_iprops(apr_array_header_t **iprops,
696                          svn_sqlite__stmt_t *stmt,
697                          int column,
698                          apr_pool_t *result_pool,
699                          apr_pool_t *scratch_pool)
700{
701  apr_size_t len;
702  const void *val;
703
704  /* svn_skel__parse_iprops copies everything needed to result_pool */
705  val = svn_sqlite__column_blob(stmt, column, &len, NULL);
706  if (val == NULL)
707    {
708      *iprops = NULL;
709      return SVN_NO_ERROR;
710    }
711
712  SVN_ERR(svn_skel__parse_iprops(iprops,
713                                 svn_skel__parse(val, len, scratch_pool),
714                                 result_pool));
715
716  return SVN_NO_ERROR;
717}
718
719svn_error_t *
720svn_sqlite__column_checksum(const svn_checksum_t **checksum,
721                            svn_sqlite__stmt_t *stmt, int column,
722                            apr_pool_t *result_pool)
723{
724  const char *digest = svn_sqlite__column_text(stmt, column, NULL);
725
726  if (digest == NULL)
727    *checksum = NULL;
728  else
729    SVN_ERR(svn_checksum_deserialize(checksum, digest,
730                                     result_pool, result_pool));
731
732  return SVN_NO_ERROR;
733}
734
735svn_boolean_t
736svn_sqlite__column_is_null(svn_sqlite__stmt_t *stmt, int column)
737{
738  return sqlite3_column_type(stmt->s3stmt, column) == SQLITE_NULL;
739}
740
741int
742svn_sqlite__column_bytes(svn_sqlite__stmt_t *stmt, int column)
743{
744  return sqlite3_column_bytes(stmt->s3stmt, column);
745}
746
747svn_error_t *
748svn_sqlite__finalize(svn_sqlite__stmt_t *stmt)
749{
750  SQLITE_ERR(sqlite3_finalize(stmt->s3stmt), stmt->db);
751  return SVN_NO_ERROR;
752}
753
754svn_error_t *
755svn_sqlite__reset(svn_sqlite__stmt_t *stmt)
756{
757  /* No need to reset again after a first attempt */
758  stmt->needs_reset = FALSE;
759
760  /* Clear bindings first, as there are no documented reasons
761     why this would ever fail, but keeping variable bindings
762     when reset is not what we expect. */
763  SQLITE_ERR(sqlite3_clear_bindings(stmt->s3stmt), stmt->db);
764
765  /* Reset last, as this *will* fail if the statement failed since
766     the last time it was reset, while reporting just the same failure.
767     (In this case the statement is also properly reset).
768
769     See the sqlite3_reset() documentation for more details. */
770  SQLITE_ERR(sqlite3_reset(stmt->s3stmt), stmt->db);
771  return SVN_NO_ERROR;
772}
773
774
775svn_error_t *
776svn_sqlite__read_schema_version(int *version,
777                                svn_sqlite__db_t *db,
778                                apr_pool_t *scratch_pool)
779{
780  svn_sqlite__stmt_t *stmt;
781
782  SVN_ERR(prepare_statement(&stmt, db, "PRAGMA user_version;", scratch_pool));
783  SVN_ERR(svn_sqlite__step_row(stmt));
784
785  *version = svn_sqlite__column_int(stmt, 0);
786
787  return svn_error_trace(svn_sqlite__finalize(stmt));
788}
789
790
791static volatile svn_atomic_t sqlite_init_state = 0;
792
793/* If possible, verify that SQLite was compiled in a thread-safe
794   manner. */
795/* Don't call this function directly!  Use svn_atomic__init_once(). */
796static svn_error_t *
797init_sqlite(void *baton, apr_pool_t *pool)
798{
799  if (sqlite3_libversion_number() < SVN_SQLITE_MIN_VERSION_NUMBER)
800    {
801      return svn_error_createf(
802                    SVN_ERR_SQLITE_ERROR, NULL,
803                    _("SQLite compiled for %s, but running with %s"),
804                    SVN_SQLITE_MIN_VERSION, sqlite3_libversion());
805    }
806
807#if APR_HAS_THREADS
808
809  /* SQLite 3.5 allows verification of its thread-safety at runtime.
810     Older versions are simply expected to have been configured with
811     --enable-threadsafe, which compiles with -DSQLITE_THREADSAFE=1
812     (or -DTHREADSAFE, for older versions). */
813  if (! sqlite3_threadsafe())
814    return svn_error_create(SVN_ERR_SQLITE_ERROR, NULL,
815                            _("SQLite is required to be compiled and run in "
816                              "thread-safe mode"));
817
818  /* If SQLite has been already initialized, sqlite3_config() returns
819     SQLITE_MISUSE. */
820  {
821    int err = sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
822    if (err != SQLITE_OK && err != SQLITE_MISUSE)
823      return svn_error_createf(SQLITE_ERROR_CODE(err), NULL,
824                               _("Could not configure SQLite [S%d]"), err);
825  }
826  SQLITE_ERR_MSG(sqlite3_initialize(), _("Could not initialize SQLite"));
827
828#endif /* APR_HAS_THRADS */
829
830  return SVN_NO_ERROR;
831}
832
833static svn_error_t *
834internal_open(svn_sqlite__db_t *db, const char *path, svn_sqlite__mode_t mode,
835              apr_int32_t timeout, apr_pool_t *scratch_pool)
836{
837  {
838    int flags;
839
840    if (mode == svn_sqlite__mode_readonly)
841      flags = SQLITE_OPEN_READONLY;
842    else if (mode == svn_sqlite__mode_readwrite)
843      flags = SQLITE_OPEN_READWRITE;
844    else if (mode == svn_sqlite__mode_rwcreate)
845      flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
846    else
847      SVN_ERR_MALFUNCTION();
848
849    /* Turn off SQLite's mutexes. All svn objects are single-threaded,
850       so we can already guarantee that our use of the SQLite handle
851       will be serialized properly.
852
853       Note: in 3.6.x, we've already config'd SQLite into MULTITHREAD mode,
854       so this is probably redundant, but if we are running in a process where
855       somebody initialized SQLite before us it is needed anyway.  */
856    flags |= SQLITE_OPEN_NOMUTEX;
857
858#if !defined(WIN32) && !defined(SVN_SQLITE_INLINE)
859    if (mode == svn_sqlite__mode_rwcreate)
860      {
861        svn_node_kind_t kind;
862
863        /* Create the file before SQLite to avoid any permissions
864           problems with an SQLite build that uses the default
865           SQLITE_DEFAULT_FILE_PERMISSIONS of 644 modified by umask.
866           We simply want umask permissions. */
867        SVN_ERR(svn_io_check_path(path, &kind, scratch_pool));
868        if (kind == svn_node_none)
869          {
870            /* Another thread may have created the file, that's OK. */
871            svn_error_t *err = svn_io_file_create_empty(path, scratch_pool);
872            if (err && !APR_STATUS_IS_EEXIST(err->apr_err))
873              return svn_error_trace(err);
874            svn_error_clear(err);
875          }
876      }
877#endif
878
879    /* Open the database. Note that a handle is returned, even when an error
880       occurs (except for out-of-memory); thus, we can safely use it to
881       extract an error message and construct an svn_error_t.  SQLite always
882       requires sqlite3_close() after sqlite3_open_v2() while Subversion
883       typically does not require close() after an open() that returns an
884       error.  So we must ensure we close the handle if this function, or
885       the caller svn_sqlite__open, returns an error to the application. */
886    {
887      const char *vFs = NULL;
888
889#if defined(WIN32) && SQLITE_VERSION_AT_LEAST(3, 8, 1)
890      if (strlen(path) > 248)
891        {
892          WCHAR *win_path;
893          vFs = "win32-longpath"; /* Enable long paths in sqlite */
894
895          /* Long paths must be absolute */
896          if (!svn_dirent_is_absolute(path))
897            SVN_ERR(svn_dirent_get_absolute(&path, path, scratch_pool));
898
899          /* Convert the path to a properly canonicalized \\?\C:\long\path */
900          SVN_ERR(svn_io__utf8_to_unicode_longpath(&win_path, path,
901                                                   scratch_pool));
902
903          /* And convert it back to UTF-8 because there is no
904              sqlite3_open16_v2() yet */
905          SVN_ERR(svn_utf__win32_utf16_to_utf8(&path, win_path, NULL,
906                                               scratch_pool));
907        }
908#endif
909
910      /* ### SQLITE_CANTOPEN */
911      SQLITE_ERR_CLOSE(sqlite3_open_v2(path, &db->db3, flags, vFs),
912                       db, scratch_pool);
913    }
914  }
915
916  if (timeout <= 0)
917    timeout = BUSY_TIMEOUT;
918
919  /* Retry until timeout when database is busy. */
920  SQLITE_ERR_CLOSE(sqlite3_busy_timeout(db->db3, timeout),
921                   db, scratch_pool);
922
923  return SVN_NO_ERROR;
924}
925
926
927/* APR cleanup function used to close the database when its pool is destroyed.
928   DATA should be the svn_sqlite__db_t handle for the database. */
929static apr_status_t
930close_apr(void *data)
931{
932  svn_sqlite__db_t *db = data;
933  svn_error_t *err = SVN_NO_ERROR;
934  apr_status_t result;
935  int i;
936
937  /* Check to see if we've already closed this database. */
938  if (db->db3 == NULL)
939    return APR_SUCCESS;
940
941  /* Finalize any prepared statements. */
942  if (db->prepared_stmts)
943    {
944      for (i = 0; i < db->nbr_statements + STMT_INTERNAL_LAST; i++)
945        {
946          if (db->prepared_stmts[i])
947            {
948              if (i < db->nbr_statements
949                  && db->prepared_stmts[i]->needs_reset)
950                {
951#ifdef SVN_DEBUG
952                  const char *stmt_text = db->statement_strings[i];
953                  SVN_UNUSED(stmt_text);
954
955                  SVN_ERR_MALFUNCTION_NO_RETURN();
956#else
957                  err = svn_error_compose_create(err,
958                            svn_sqlite__reset(db->prepared_stmts[i]));
959#endif
960                }
961              err = svn_error_compose_create(
962                        svn_sqlite__finalize(db->prepared_stmts[i]), err);
963            }
964        }
965    }
966
967  result = sqlite3_close(db->db3);
968
969  /* If there's a pre-existing error, return it. */
970  if (err)
971    {
972      result = err->apr_err;
973      svn_error_clear(err);
974      return result;
975    }
976
977  if (result != SQLITE_OK)
978    return SQLITE_ERROR_CODE(result); /* ### lossy */
979
980  db->db3 = NULL;
981
982  return APR_SUCCESS;
983}
984
985#ifdef SVN_UNICODE_NORMALIZATION_FIXES
986/* Unicode normalizing collation for WC paths */
987static int
988collate_ucs_nfd(void *baton,
989                int len1, const void *key1,
990                int len2, const void *key2)
991{
992  svn_sqlite__db_t *db = baton;
993  int result;
994
995  if (svn_utf__normcmp(key1, len1, key2, len2,
996                       &db->sqlext_buf1, &db->sqlext_buf2, &result))
997    {
998      /* There is really nothing we can do here if an error occurs
999         during Unicode normalizetion, and attempting to recover could
1000         result in the wc.db index being corrupted. Presumably this
1001         can only happen if the index already contains invalid UTF-8
1002         strings, which should never happen in any case ... */
1003      SVN_ERR_MALFUNCTION_NO_RETURN();
1004    }
1005
1006  return result;
1007}
1008
1009static void
1010glob_like_ucs_nfd_common(sqlite3_context *context,
1011                         int argc, sqlite3_value **argv,
1012                         svn_boolean_t sql_like)
1013{
1014  svn_sqlite__db_t *const db = sqlite3_user_data(context);
1015
1016  const char *const pattern = (void*)sqlite3_value_text(argv[0]);
1017  const apr_size_t pattern_len = sqlite3_value_bytes(argv[0]);
1018  const char *const string = (void*)sqlite3_value_text(argv[1]);
1019  const apr_size_t string_len = sqlite3_value_bytes(argv[1]);
1020
1021  const char *escape = NULL;
1022  apr_size_t escape_len = 0;
1023
1024  svn_boolean_t match;
1025  svn_error_t *err;
1026
1027  if (pattern_len > SQLITE_MAX_LIKE_PATTERN_LENGTH)
1028    {
1029      sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
1030      return;
1031    }
1032
1033  if (argc == 3 && sql_like)
1034    {
1035      escape = (void*)sqlite3_value_text(argv[2]);
1036      escape_len = sqlite3_value_bytes(argv[2]);
1037    }
1038
1039  if (pattern && string)
1040    {
1041      err = svn_utf__glob(pattern, pattern_len, string, string_len,
1042                          escape, escape_len, sql_like,
1043                          &db->sqlext_buf1, &db->sqlext_buf2, &db->sqlext_buf3,
1044                          &match);
1045
1046      if (err)
1047        {
1048          const char *errmsg;
1049          svn_membuf__ensure(&db->sqlext_buf1, 512);
1050          errmsg = svn_err_best_message(err,
1051                                        db->sqlext_buf1.data,
1052                                        db->sqlext_buf1.size - 1);
1053          svn_error_clear(err);
1054          sqlite3_result_error(context, errmsg, -1);
1055          return;
1056        }
1057
1058      sqlite3_result_int(context, match);
1059    }
1060}
1061
1062/* Unicode normalizing implementation of GLOB */
1063static void
1064glob_ucs_nfd(sqlite3_context *context,
1065             int argc, sqlite3_value **argv)
1066{
1067  glob_like_ucs_nfd_common(context, argc, argv, FALSE);
1068}
1069
1070/* Unicode normalizing implementation of LIKE */
1071static void
1072like_ucs_nfd(sqlite3_context *context,
1073             int argc, sqlite3_value **argv)
1074{
1075  glob_like_ucs_nfd_common(context, argc, argv, TRUE);
1076}
1077#endif /* SVN_UNICODE_NORMALIZATION_FIXES */
1078
1079svn_error_t *
1080svn_sqlite__open(svn_sqlite__db_t **db, const char *path,
1081                 svn_sqlite__mode_t mode, const char * const statements[],
1082                 int unused1, const char * const *unused2,
1083                 apr_int32_t timeout,
1084                 apr_pool_t *result_pool, apr_pool_t *scratch_pool)
1085{
1086  SVN_ERR(svn_atomic__init_once(&sqlite_init_state,
1087                                init_sqlite, NULL, scratch_pool));
1088
1089  *db = apr_pcalloc(result_pool, sizeof(**db));
1090
1091  SVN_ERR(internal_open(*db, path, mode, timeout, scratch_pool));
1092
1093#if SQLITE_VERSION_NUMBER >= 3008000 && SQLITE_VERSION_NUMBER < 3009000
1094  /* disable SQLITE_ENABLE_STAT3/4 from 3.8.1 - 3.8.3 (but not 3.8.3.1+)
1095   * to prevent using it when it's buggy.
1096   * See: https://www.sqlite.org/src/info/4c86b126f2 */
1097  if (sqlite3_libversion_number() > 3008000 &&
1098      sqlite3_libversion_number() < 3008004 &&
1099      strcmp(sqlite3_sourceid(),"2014-02-11")<0)
1100    {
1101      sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, (*db)->db3, 0x800);
1102    }
1103#endif
1104
1105#ifdef SVN_UNICODE_NORMALIZATION_FIXES
1106  /* Create extension buffers with space for 200 UCS-4 characters. */
1107  svn_membuf__create(&(*db)->sqlext_buf1, 800, result_pool);
1108  svn_membuf__create(&(*db)->sqlext_buf2, 800, result_pool);
1109  svn_membuf__create(&(*db)->sqlext_buf3, 800, result_pool);
1110
1111  /* Register collation and LIKE and GLOB operator replacements. */
1112  SQLITE_ERR_CLOSE(sqlite3_create_collation((*db)->db3,
1113                                            "svn-ucs-nfd", SQLITE_UTF8,
1114                                            *db, collate_ucs_nfd),
1115                   db, scratch_pool);
1116  /* ### Is it really necessary to override these functions?
1117         I would assume the default implementation to be collation agnostic?
1118         And otherwise our implementation should be...
1119
1120         The default implementation is in some cases index backed, while our
1121         implementation can't be. With an index based on the collation it could
1122         be. */
1123  SQLITE_ERR_CLOSE(sqlite3_create_function((*db)->db3, "glob", 2,
1124                                           SQLITE_UTF8 | SQLITE_DETERMINISTIC,
1125                                           *db, glob_ucs_nfd, NULL, NULL),
1126                   db, scratch_pool);
1127  SQLITE_ERR_CLOSE(sqlite3_create_function((*db)->db3, "like", 2,
1128                                           SQLITE_UTF8 | SQLITE_DETERMINISTIC,
1129                                           *db, like_ucs_nfd, NULL, NULL),
1130                   db, scratch_pool);
1131  SQLITE_ERR_CLOSE(sqlite3_create_function((*db)->db3, "like", 3,
1132                                           SQLITE_UTF8 | SQLITE_DETERMINISTIC,
1133                                           *db, like_ucs_nfd, NULL, NULL),
1134                   db, scratch_pool);
1135#endif /* SVN_UNICODE_NORMALIZATION_FIXES */
1136
1137#ifdef SQLITE3_DEBUG
1138  sqlite3_trace((*db)->db3, sqlite_tracer, (*db)->db3);
1139#endif
1140#ifdef SQLITE3_PROFILE
1141  sqlite3_profile((*db)->db3, sqlite_profiler, (*db)->db3);
1142#endif
1143
1144  SVN_ERR_CLOSE(exec_sql(*db,
1145              /* The default behavior of the LIKE operator is to ignore case
1146                 for ASCII characters. Hence, by default 'a' LIKE 'A' is true.
1147                 The case_sensitive_like pragma installs a new application-
1148                 defined LIKE function that is either case sensitive or
1149                 insensitive depending on the value of the case_sensitive_like
1150                 pragma. */
1151              "PRAGMA case_sensitive_like=1;"
1152              /* Disable synchronization to disable the explicit disk flushes
1153                 that make Sqlite up to 50 times slower; especially on small
1154                 transactions.
1155
1156                 This removes some stability guarantees on specific hardware
1157                 and power failures, but still guarantees atomic commits on
1158                 application crashes. With our dependency on external data
1159                 like pristine files (Wc) and revision files (repository),
1160                 we can't keep up these additional guarantees anyway.
1161
1162                 ### Maybe switch to NORMAL(1) when we use larger transaction
1163                     scopes */
1164              "PRAGMA synchronous=OFF;"
1165              /* Enable recursive triggers so that a user trigger will fire
1166                 in the deletion phase of an INSERT OR REPLACE statement.
1167                 Requires SQLite >= 3.6.18  */
1168              "PRAGMA recursive_triggers=ON;"
1169              /* Enforce current Sqlite default behavior. Some distributions
1170                 might change the Sqlite defaults without realizing how this
1171                 affects application(read: Subversion) performance/behavior. */
1172              "PRAGMA foreign_keys=OFF;"      /* SQLITE_DEFAULT_FOREIGN_KEYS*/
1173              "PRAGMA locking_mode = NORMAL;" /* SQLITE_DEFAULT_LOCKING_MODE */
1174              /* Testing shows TRUNCATE is faster than DELETE on Windows. */
1175              "PRAGMA journal_mode = TRUNCATE;"
1176              ),
1177                *db);
1178
1179#if defined(SVN_DEBUG)
1180  /* When running in debug mode, enable the checking of foreign key
1181     constraints.  This has possible performance implications, so we don't
1182     bother to do it for production...for now. */
1183  SVN_ERR_CLOSE(exec_sql(*db, "PRAGMA foreign_keys=ON;"),
1184                *db);
1185#endif
1186
1187#ifdef SVN_SQLITE_REVERSE_UNORDERED_SELECTS
1188  /* When enabled, this PRAGMA causes SELECT statements without an ORDER BY
1189     clause to emit their results in the reverse order of what they normally
1190     would.  This can help detecting invalid assumptions about the result
1191     order.*/
1192  SVN_ERR_CLOSE(exec_sql(*db, "PRAGMA reverse_unordered_selects=ON;"),
1193                *db);
1194#endif
1195
1196  /* Store temporary tables in RAM instead of in temporary files, but don't
1197     fail on this if this option is disabled in the sqlite compilation by
1198     setting SQLITE_TEMP_STORE to 0 (always to disk) */
1199  svn_error_clear(exec_sql(*db, "PRAGMA temp_store = MEMORY;"));
1200
1201  /* Store the provided statements. */
1202  if (statements)
1203    {
1204      (*db)->statement_strings = statements;
1205      (*db)->nbr_statements = 0;
1206      while (*statements != NULL)
1207        {
1208          statements++;
1209          (*db)->nbr_statements++;
1210        }
1211
1212      (*db)->prepared_stmts = apr_pcalloc(
1213                                  result_pool,
1214                                  ((*db)->nbr_statements + STMT_INTERNAL_LAST)
1215                                                * sizeof(svn_sqlite__stmt_t *));
1216    }
1217  else
1218    {
1219      (*db)->nbr_statements = 0;
1220      (*db)->prepared_stmts = apr_pcalloc(result_pool,
1221                                          (0 + STMT_INTERNAL_LAST)
1222                                                * sizeof(svn_sqlite__stmt_t *));
1223    }
1224
1225  (*db)->state_pool = result_pool;
1226  apr_pool_cleanup_register(result_pool, *db, close_apr, apr_pool_cleanup_null);
1227
1228  return SVN_NO_ERROR;
1229}
1230
1231svn_error_t *
1232svn_sqlite__close(svn_sqlite__db_t *db)
1233{
1234  apr_status_t result = apr_pool_cleanup_run(db->state_pool, db, close_apr);
1235
1236  if (result == APR_SUCCESS)
1237    return SVN_NO_ERROR;
1238
1239  return svn_error_wrap_apr(result, NULL);
1240}
1241
1242static svn_error_t *
1243reset_all_statements(svn_sqlite__db_t *db,
1244                     svn_error_t *error_to_wrap)
1245{
1246  int i;
1247  svn_error_t *err;
1248
1249  /* ### Should we reorder the errors in this specific case
1250     ### to avoid returning the normal error as top level error? */
1251
1252  err = svn_error_compose_create(error_to_wrap,
1253                   svn_error_create(SVN_ERR_SQLITE_RESETTING_FOR_ROLLBACK,
1254                                    NULL, NULL));
1255
1256  for (i = 0; i < db->nbr_statements; i++)
1257    if (db->prepared_stmts[i] && db->prepared_stmts[i]->needs_reset)
1258      err = svn_error_compose_create(err,
1259                                svn_sqlite__reset(db->prepared_stmts[i]));
1260
1261  return err;
1262}
1263
1264static svn_error_t *
1265rollback_transaction(svn_sqlite__db_t *db,
1266                     svn_error_t *error_to_wrap)
1267{
1268  svn_sqlite__stmt_t *stmt;
1269  svn_error_t *err;
1270
1271  err = get_internal_statement(&stmt, db, STMT_INTERNAL_ROLLBACK_TRANSACTION);
1272  if (!err)
1273    {
1274      err = svn_error_trace(svn_sqlite__step_done(stmt));
1275
1276      if (err && err->apr_err == SVN_ERR_SQLITE_BUSY)
1277        {
1278          /* ### Houston, we have a problem!
1279
1280             We are trying to rollback but we can't because some
1281             statements are still busy. This leaves the database
1282             unusable for future transactions as the current transaction
1283             is still open.
1284
1285             As we are returning the actual error as the most relevant
1286             error in the chain, our caller might assume that it can
1287             retry/compensate on this error (e.g. SVN_WC_LOCKED), while
1288             in fact the SQLite database is unusable until the statements
1289             started within this transaction are reset and the transaction
1290             aborted.
1291
1292             We try to compensate by resetting all prepared but unreset
1293             statements; but we leave the busy error in the chain anyway to
1294             help diagnosing the original error and help in finding where
1295             a reset statement is missing. */
1296          err = svn_error_trace(reset_all_statements(db, err));
1297          err = svn_error_compose_create(
1298                      svn_error_trace(svn_sqlite__step_done(stmt)),
1299                      err);
1300        }
1301    }
1302
1303  if (err)
1304    {
1305      /* Rollback failed, use a specific error code. */
1306      err = svn_error_create(SVN_SQLITE__ERR_ROLLBACK_FAILED, err,
1307                             _("SQLite transaction rollback failed"));
1308    }
1309
1310  return svn_error_compose_create(error_to_wrap, err);
1311}
1312
1313svn_error_t *
1314svn_sqlite__begin_transaction(svn_sqlite__db_t *db)
1315{
1316  svn_sqlite__stmt_t *stmt;
1317
1318  SVN_ERR(get_internal_statement(&stmt, db,
1319                                 STMT_INTERNAL_BEGIN_TRANSACTION));
1320  SVN_ERR(svn_sqlite__step_done(stmt));
1321  return SVN_NO_ERROR;
1322}
1323
1324svn_error_t *
1325svn_sqlite__begin_immediate_transaction(svn_sqlite__db_t *db)
1326{
1327  svn_sqlite__stmt_t *stmt;
1328
1329  SVN_ERR(get_internal_statement(&stmt, db,
1330                                 STMT_INTERNAL_BEGIN_IMMEDIATE_TRANSACTION));
1331  SVN_ERR(svn_sqlite__step_done(stmt));
1332  return SVN_NO_ERROR;
1333}
1334
1335svn_error_t *
1336svn_sqlite__begin_savepoint(svn_sqlite__db_t *db)
1337{
1338  svn_sqlite__stmt_t *stmt;
1339
1340  SVN_ERR(get_internal_statement(&stmt, db,
1341                                 STMT_INTERNAL_SAVEPOINT_SVN));
1342  SVN_ERR(svn_sqlite__step_done(stmt));
1343  return SVN_NO_ERROR;
1344}
1345
1346svn_error_t *
1347svn_sqlite__finish_transaction(svn_sqlite__db_t *db,
1348                               svn_error_t *err)
1349{
1350  svn_sqlite__stmt_t *stmt;
1351
1352  /* Commit or rollback the sqlite transaction. */
1353  if (err)
1354    {
1355      return svn_error_trace(rollback_transaction(db, err));
1356    }
1357  else
1358    {
1359      err = get_internal_statement(&stmt, db,
1360                                   STMT_INTERNAL_COMMIT_TRANSACTION);
1361      if (!err)
1362        err = svn_error_trace(svn_sqlite__step_done(stmt));
1363
1364      /* Need to rollback if the commit fails as well, because otherwise the
1365         db connection will be left in an unusable state.
1366
1367         One important case to keep in mind is trying to COMMIT with concurrent
1368         readers. In case the commit fails, because someone else is holding a
1369         shared lock, sqlite keeps the transaction, and *also* keeps the file
1370         locks on the database. While the first part only prevents from using
1371         this connection, the second part prevents everyone else from accessing
1372         the database while the connection is open.
1373
1374         See https://www.sqlite.org/lang_transaction.html
1375
1376         COMMIT might also result in an SQLITE_BUSY return code if an another
1377         thread or process has a shared lock on the database that prevented
1378         the database from being updated. When COMMIT fails in this way, the
1379         transaction remains active and the COMMIT can be retried later after
1380         the reader has had a chance to clear. */
1381      if (err)
1382        return svn_error_trace(rollback_transaction(db, err));
1383    }
1384
1385  return SVN_NO_ERROR;
1386}
1387
1388svn_error_t *
1389svn_sqlite__finish_savepoint(svn_sqlite__db_t *db,
1390                             svn_error_t *err)
1391{
1392  svn_sqlite__stmt_t *stmt;
1393
1394  if (err)
1395    {
1396      svn_error_t *err2;
1397
1398      err2 = get_internal_statement(&stmt, db,
1399                                    STMT_INTERNAL_ROLLBACK_TO_SAVEPOINT_SVN);
1400
1401      if (!err2)
1402        {
1403          err2 = svn_error_trace(svn_sqlite__step_done(stmt));
1404
1405          if (err2 && err2->apr_err == SVN_ERR_SQLITE_BUSY)
1406            {
1407              /* Ok, we have a major problem. Some statement is still open,
1408                 which makes it impossible to release this savepoint.
1409
1410                 ### See huge comment in svn_sqlite__finish_transaction for
1411                     further details */
1412
1413              err2 = svn_error_trace(reset_all_statements(db, err2));
1414              err2 = svn_error_compose_create(
1415                          svn_error_trace(svn_sqlite__step_done(stmt)),
1416                          err2);
1417            }
1418        }
1419
1420      err = svn_error_compose_create(err, err2);
1421      err2 = get_internal_statement(&stmt, db,
1422                                    STMT_INTERNAL_RELEASE_SAVEPOINT_SVN);
1423
1424      if (!err2)
1425        err2 = svn_error_trace(svn_sqlite__step_done(stmt));
1426
1427      return svn_error_compose_create(err, err2);
1428    }
1429
1430  SVN_ERR(get_internal_statement(&stmt, db,
1431                                 STMT_INTERNAL_RELEASE_SAVEPOINT_SVN));
1432
1433  return svn_error_trace(svn_sqlite__step_done(stmt));
1434}
1435
1436svn_error_t *
1437svn_sqlite__with_transaction(svn_sqlite__db_t *db,
1438                             svn_sqlite__transaction_callback_t cb_func,
1439                             void *cb_baton,
1440                             apr_pool_t *scratch_pool /* NULL allowed */)
1441{
1442  SVN_SQLITE__WITH_TXN(cb_func(cb_baton, db, scratch_pool), db);
1443  return SVN_NO_ERROR;
1444}
1445
1446svn_error_t *
1447svn_sqlite__with_immediate_transaction(
1448  svn_sqlite__db_t *db,
1449  svn_sqlite__transaction_callback_t cb_func,
1450  void *cb_baton,
1451  apr_pool_t *scratch_pool /* NULL allowed */)
1452{
1453  SVN_SQLITE__WITH_IMMEDIATE_TXN(cb_func(cb_baton, db, scratch_pool), db);
1454  return SVN_NO_ERROR;
1455}
1456
1457svn_error_t *
1458svn_sqlite__with_lock(svn_sqlite__db_t *db,
1459                      svn_sqlite__transaction_callback_t cb_func,
1460                      void *cb_baton,
1461                      apr_pool_t *scratch_pool /* NULL allowed */)
1462{
1463  SVN_SQLITE__WITH_LOCK(cb_func(cb_baton, db, scratch_pool), db);
1464  return SVN_NO_ERROR;
1465}
1466
1467svn_error_t *
1468svn_sqlite__hotcopy(const char *src_path,
1469                    const char *dst_path,
1470                    apr_pool_t *scratch_pool)
1471{
1472  svn_sqlite__db_t *src_db;
1473
1474  SVN_ERR(svn_sqlite__open(&src_db, src_path, svn_sqlite__mode_readonly,
1475                           NULL, 0, NULL, 0,
1476                           scratch_pool, scratch_pool));
1477
1478  {
1479    svn_sqlite__db_t *dst_db;
1480    sqlite3_backup *backup;
1481    int rc1, rc2;
1482
1483    SVN_ERR(svn_sqlite__open(&dst_db, dst_path, svn_sqlite__mode_rwcreate,
1484                             NULL, 0, NULL, 0, scratch_pool, scratch_pool));
1485    backup = sqlite3_backup_init(dst_db->db3, "main", src_db->db3, "main");
1486    if (!backup)
1487      return svn_error_createf(SVN_ERR_SQLITE_ERROR, NULL,
1488                               _("SQLite hotcopy failed for %s"), src_path);
1489    do
1490      {
1491        /* Pages are usually 1024 byte (SQLite docs). On my laptop
1492           copying gets faster as the number of pages is increased up
1493           to about 64, beyond that speed levels off.  Lets put the
1494           number of pages an order of magnitude higher, this is still
1495           likely to be a fraction of large databases. */
1496        rc1 = sqlite3_backup_step(backup, 1024);
1497
1498        /* Should we sleep on SQLITE_OK?  That would make copying a
1499           large database take much longer.  When we do sleep how,
1500           long should we sleep?  Should the sleep get longer if we
1501           keep getting BUSY/LOCKED?  I have no real reason for
1502           choosing 25. */
1503        if (rc1 == SQLITE_BUSY || rc1 == SQLITE_LOCKED)
1504          sqlite3_sleep(25);
1505      }
1506    while (rc1 == SQLITE_OK || rc1 == SQLITE_BUSY || rc1 == SQLITE_LOCKED);
1507    rc2 = sqlite3_backup_finish(backup);
1508    if (rc1 != SQLITE_DONE)
1509      SQLITE_ERR(rc1, dst_db);
1510    SQLITE_ERR(rc2, dst_db);
1511    SVN_ERR(svn_sqlite__close(dst_db));
1512  }
1513
1514  SVN_ERR(svn_sqlite__close(src_db));
1515
1516  SVN_ERR(svn_io_copy_perms(src_path, dst_path, scratch_pool));
1517
1518  return SVN_NO_ERROR;
1519}
1520
1521struct function_wrapper_baton_t
1522{
1523  svn_sqlite__func_t func;
1524  void *baton;
1525};
1526
1527static void
1528wrapped_func(sqlite3_context *context,
1529             int argc,
1530             sqlite3_value *values[])
1531{
1532  struct function_wrapper_baton_t *fwb = sqlite3_user_data(context);
1533  svn_sqlite__context_t sctx;
1534  svn_error_t *err;
1535  void *void_values = values;
1536
1537  sctx.context = context;
1538
1539  err = fwb->func(&sctx, argc, void_values, fwb->baton);
1540
1541  if (err)
1542    {
1543      char buf[256];
1544      sqlite3_result_error(context,
1545                           svn_err_best_message(err, buf, sizeof(buf)),
1546                           -1);
1547      svn_error_clear(err);
1548    }
1549}
1550
1551
1552svn_error_t *
1553svn_sqlite__create_scalar_function(svn_sqlite__db_t *db,
1554                                   const char *func_name,
1555                                   int argc,
1556                                   svn_boolean_t deterministic,
1557                                   svn_sqlite__func_t func,
1558                                   void *baton)
1559{
1560  int eTextRep;
1561  struct function_wrapper_baton_t *fwb = apr_pcalloc(db->state_pool,
1562                                                     sizeof(*fwb));
1563
1564  fwb->func = func;
1565  fwb->baton = baton;
1566
1567  eTextRep = SQLITE_ANY;
1568  if (deterministic)
1569    eTextRep |= SQLITE_DETERMINISTIC;
1570
1571  SQLITE_ERR(sqlite3_create_function(db->db3, func_name, argc, eTextRep,
1572                                     fwb, wrapped_func, NULL, NULL),
1573             db);
1574
1575  return SVN_NO_ERROR;
1576}
1577
1578int
1579svn_sqlite__value_type(svn_sqlite__value_t *val)
1580{
1581  void *v = val;
1582  return sqlite3_value_type(v);
1583}
1584
1585const char *
1586svn_sqlite__value_text(svn_sqlite__value_t *val)
1587{
1588  void *v = val;
1589  return (const char *) sqlite3_value_text(v);
1590}
1591
1592void
1593svn_sqlite__result_null(svn_sqlite__context_t *sctx)
1594{
1595  sqlite3_result_null(sctx->context);
1596}
1597
1598void
1599svn_sqlite__result_int64(svn_sqlite__context_t *sctx, apr_int64_t val)
1600{
1601  sqlite3_result_int64(sctx->context, val);
1602}
1603
1604void
1605svn_sqlite__result_error(svn_sqlite__context_t *sctx, const char *msg, int num)
1606{
1607  sqlite3_result_error(sctx->context, msg, num);
1608}
1609