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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
20 * ====================================================================
23 #include <apr_pools.h>
25 #include "svn_types.h"
26 #include "svn_error.h"
27 #include "svn_pools.h"
29 #include "svn_dirent_uri.h"
30 #include "svn_checksum.h"
32 #include "internal_statements.h"
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"
42 #include "private/svn_debug.h"
45 #ifdef SVN_SQLITE_INLINE
46 /* Import the sqlite3 API vtable from sqlite3wrapper.c */
47 # define SQLITE_OMIT_DEPRECATED
48 # include <sqlite3ext.h>
49 extern const sqlite3_api_routines *const svn_sqlite3__api_funcs;
50 extern int (*const svn_sqlite3__api_initialize)(void);
51 extern int (*const svn_sqlite3__api_config)(int, ...);
52 # define sqlite3_api svn_sqlite3__api_funcs
53 # define sqlite3_initialize svn_sqlite3__api_initialize
54 # define sqlite3_config svn_sqlite3__api_config
59 #if !SQLITE_VERSION_AT_LEAST(3,7,12)
60 #error SQLite is too old -- version 3.7.12 is the minimum required version
64 svn_sqlite__compiled_version(void)
66 static const char sqlite_version[] = SQLITE_VERSION;
67 return sqlite_version;
71 svn_sqlite__runtime_version(void)
73 return sqlite3_libversion();
77 INTERNAL_STATEMENTS_SQL_DECLARE_STATEMENTS(internal_statements);
81 /* An sqlite query execution callback. */
83 sqlite_tracer(void *data, const char *sql)
85 /* sqlite3 *db3 = data; */
86 SVN_DBG(("sql=\"%s\"\n", sql));
90 #ifdef SQLITE3_PROFILE
91 /* An sqlite execution timing callback. */
93 sqlite_profiler(void *data, const char *sql, sqlite3_uint64 duration)
95 /* sqlite3 *db3 = data; */
96 SVN_DBG(("[%.3f] sql=\"%s\"\n", 1e-9 * duration, sql));
100 struct svn_sqlite__db_t
103 const char * const *statement_strings;
105 svn_sqlite__stmt_t **prepared_stmts;
106 apr_pool_t *state_pool;
109 struct svn_sqlite__stmt_t
111 sqlite3_stmt *s3stmt;
112 svn_sqlite__db_t *db;
113 svn_boolean_t needs_reset;
116 struct svn_sqlite__context_t
118 sqlite3_context *context;
121 struct svn_sqlite__value_t
123 sqlite3_value *value;
127 /* Convert SQLite error codes to SVN. Evaluates X multiple times */
128 #define SQLITE_ERROR_CODE(x) ((x) == SQLITE_READONLY \
129 ? SVN_ERR_SQLITE_READONLY \
130 : ((x) == SQLITE_BUSY \
131 ? SVN_ERR_SQLITE_BUSY \
132 : ((x) == SQLITE_CONSTRAINT \
133 ? SVN_ERR_SQLITE_CONSTRAINT \
134 : SVN_ERR_SQLITE_ERROR)))
137 /* SQLITE->SVN quick error wrap, much like SVN_ERR. */
138 #define SQLITE_ERR(x, db) do \
140 int sqlite_err__temp = (x); \
141 if (sqlite_err__temp != SQLITE_OK) \
142 return svn_error_createf(SQLITE_ERROR_CODE(sqlite_err__temp), \
143 NULL, "sqlite[S%d]: %s", \
145 sqlite3_errmsg((db)->db3)); \
148 #define SQLITE_ERR_MSG(x, msg) do \
150 int sqlite_err__temp = (x); \
151 if (sqlite_err__temp != SQLITE_OK) \
152 return svn_error_createf(SQLITE_ERROR_CODE(sqlite_err__temp), \
153 NULL, "sqlite[S%d]: %s", \
154 sqlite_err__temp, msg); \
158 /* Time (in milliseconds) to wait for sqlite locks before giving up. */
159 #define BUSY_TIMEOUT 10000
162 /* Convenience wrapper around exec_sql2(). */
163 #define exec_sql(db, sql) exec_sql2((db), (sql), SQLITE_OK)
165 /* Run the statement SQL on DB, ignoring SQLITE_OK and IGNORED_ERR.
166 (Note: the IGNORED_ERR parameter itself is not ignored.) */
168 exec_sql2(svn_sqlite__db_t *db, const char *sql, int ignored_err)
171 int sqlite_err = sqlite3_exec(db->db3, sql, NULL, NULL, &err_msg);
173 if (sqlite_err != SQLITE_OK && sqlite_err != ignored_err)
175 svn_error_t *err = svn_error_createf(SQLITE_ERROR_CODE(sqlite_err), NULL,
177 " executing statement '%s'"),
178 sqlite_err, err_msg, sql);
179 sqlite3_free(err_msg);
188 prepare_statement(svn_sqlite__stmt_t **stmt, svn_sqlite__db_t *db,
189 const char *text, apr_pool_t *result_pool)
191 *stmt = apr_palloc(result_pool, sizeof(**stmt));
193 (*stmt)->needs_reset = FALSE;
195 SQLITE_ERR(sqlite3_prepare_v2(db->db3, text, -1, &(*stmt)->s3stmt, NULL), db);
202 svn_sqlite__exec_statements(svn_sqlite__db_t *db, int stmt_idx)
204 SVN_ERR_ASSERT(stmt_idx < db->nbr_statements);
206 return svn_error_trace(exec_sql(db, db->statement_strings[stmt_idx]));
211 svn_sqlite__get_statement(svn_sqlite__stmt_t **stmt, svn_sqlite__db_t *db,
214 SVN_ERR_ASSERT(stmt_idx < db->nbr_statements);
216 if (db->prepared_stmts[stmt_idx] == NULL)
217 SVN_ERR(prepare_statement(&db->prepared_stmts[stmt_idx], db,
218 db->statement_strings[stmt_idx],
221 *stmt = db->prepared_stmts[stmt_idx];
223 if ((*stmt)->needs_reset)
224 return svn_error_trace(svn_sqlite__reset(*stmt));
229 /* Like svn_sqlite__get_statement but gets an internal statement.
231 All internal statements that use this api are executed with step_done(),
232 so we don't need the fallback reset handling here or in the pool cleanup */
234 get_internal_statement(svn_sqlite__stmt_t **stmt, svn_sqlite__db_t *db,
237 /* The internal statements are stored after the registered statements */
238 int prep_idx = db->nbr_statements + stmt_idx;
239 SVN_ERR_ASSERT(stmt_idx < STMT_INTERNAL_LAST);
241 if (db->prepared_stmts[prep_idx] == NULL)
242 SVN_ERR(prepare_statement(&db->prepared_stmts[prep_idx], db,
243 internal_statements[stmt_idx],
246 *stmt = db->prepared_stmts[prep_idx];
253 step_with_expectation(svn_sqlite__stmt_t* stmt,
254 svn_boolean_t expecting_row)
256 svn_boolean_t got_row;
258 SVN_ERR(svn_sqlite__step(&got_row, stmt));
259 if ((got_row && !expecting_row)
261 (!got_row && expecting_row))
262 return svn_error_create(SVN_ERR_SQLITE_ERROR,
263 svn_sqlite__reset(stmt),
265 ? _("sqlite: Expected database row missing")
266 : _("sqlite: Extra database row found"));
272 svn_sqlite__step_done(svn_sqlite__stmt_t *stmt)
274 SVN_ERR(step_with_expectation(stmt, FALSE));
275 return svn_error_trace(svn_sqlite__reset(stmt));
279 svn_sqlite__step_row(svn_sqlite__stmt_t *stmt)
281 return svn_error_trace(step_with_expectation(stmt, TRUE));
286 svn_sqlite__step(svn_boolean_t *got_row, svn_sqlite__stmt_t *stmt)
288 int sqlite_result = sqlite3_step(stmt->s3stmt);
290 if (sqlite_result != SQLITE_DONE && sqlite_result != SQLITE_ROW)
292 svn_error_t *err1, *err2;
294 err1 = svn_error_createf(SQLITE_ERROR_CODE(sqlite_result), NULL,
296 sqlite_result, sqlite3_errmsg(stmt->db->db3));
297 err2 = svn_sqlite__reset(stmt);
298 return svn_error_compose_create(err1, err2);
301 *got_row = (sqlite_result == SQLITE_ROW);
302 stmt->needs_reset = TRUE;
308 svn_sqlite__insert(apr_int64_t *row_id, svn_sqlite__stmt_t *stmt)
310 svn_boolean_t got_row;
312 SVN_ERR(svn_sqlite__step(&got_row, stmt));
314 *row_id = sqlite3_last_insert_rowid(stmt->db->db3);
316 return svn_error_trace(svn_sqlite__reset(stmt));
320 svn_sqlite__update(int *affected_rows, svn_sqlite__stmt_t *stmt)
322 SVN_ERR(step_with_expectation(stmt, FALSE));
325 *affected_rows = sqlite3_changes(stmt->db->db3);
327 return svn_error_trace(svn_sqlite__reset(stmt));
332 vbindf(svn_sqlite__stmt_t *stmt, const char *fmt, va_list ap)
336 for (count = 1; *fmt; fmt++, count++)
339 apr_size_t blob_size;
340 const svn_token_map_t *map;
345 SVN_ERR(svn_sqlite__bind_text(stmt, count,
346 va_arg(ap, const char *)));
350 SVN_ERR(svn_sqlite__bind_int(stmt, count,
356 SVN_ERR(svn_sqlite__bind_int64(stmt, count,
357 va_arg(ap, apr_int64_t)));
361 blob = va_arg(ap, const void *);
362 blob_size = va_arg(ap, apr_size_t);
363 SVN_ERR(svn_sqlite__bind_blob(stmt, count, blob, blob_size));
367 SVN_ERR(svn_sqlite__bind_revnum(stmt, count,
368 va_arg(ap, svn_revnum_t)));
372 map = va_arg(ap, const svn_token_map_t *);
373 SVN_ERR(svn_sqlite__bind_token(stmt, count, map, va_arg(ap, int)));
377 /* Skip this column: no binding */
381 SVN_ERR_MALFUNCTION();
389 svn_sqlite__bindf(svn_sqlite__stmt_t *stmt, const char *fmt, ...)
395 err = vbindf(stmt, fmt, ap);
397 return svn_error_trace(err);
401 svn_sqlite__bind_int(svn_sqlite__stmt_t *stmt,
405 SQLITE_ERR(sqlite3_bind_int(stmt->s3stmt, slot, val), stmt->db);
410 svn_sqlite__bind_int64(svn_sqlite__stmt_t *stmt,
414 SQLITE_ERR(sqlite3_bind_int64(stmt->s3stmt, slot, val), stmt->db);
419 svn_sqlite__bind_text(svn_sqlite__stmt_t *stmt,
423 SQLITE_ERR(sqlite3_bind_text(stmt->s3stmt, slot, val, -1, SQLITE_TRANSIENT),
429 svn_sqlite__bind_blob(svn_sqlite__stmt_t *stmt,
434 SQLITE_ERR(sqlite3_bind_blob(stmt->s3stmt, slot, val, (int) len,
441 svn_sqlite__bind_token(svn_sqlite__stmt_t *stmt,
443 const svn_token_map_t *map,
446 const char *word = svn_token__to_word(map, value);
448 SQLITE_ERR(sqlite3_bind_text(stmt->s3stmt, slot, word, -1, SQLITE_STATIC),
454 svn_sqlite__bind_revnum(svn_sqlite__stmt_t *stmt,
458 if (SVN_IS_VALID_REVNUM(value))
459 SQLITE_ERR(sqlite3_bind_int64(stmt->s3stmt, slot,
460 (sqlite_int64)value), stmt->db);
462 SQLITE_ERR(sqlite3_bind_null(stmt->s3stmt, slot), stmt->db);
468 svn_sqlite__bind_properties(svn_sqlite__stmt_t *stmt,
470 const apr_hash_t *props,
471 apr_pool_t *scratch_pool)
474 svn_stringbuf_t *properties;
477 return svn_error_trace(svn_sqlite__bind_blob(stmt, slot, NULL, 0));
479 SVN_ERR(svn_skel__unparse_proplist(&skel, props, scratch_pool));
480 properties = svn_skel__unparse(skel, scratch_pool);
481 return svn_error_trace(svn_sqlite__bind_blob(stmt,
488 svn_sqlite__bind_iprops(svn_sqlite__stmt_t *stmt,
490 const apr_array_header_t *inherited_props,
491 apr_pool_t *scratch_pool)
494 svn_stringbuf_t *properties;
496 if (inherited_props == NULL)
497 return svn_error_trace(svn_sqlite__bind_blob(stmt, slot, NULL, 0));
499 SVN_ERR(svn_skel__unparse_iproplist(&skel, inherited_props,
500 scratch_pool, scratch_pool));
501 properties = svn_skel__unparse(skel, scratch_pool);
502 return svn_error_trace(svn_sqlite__bind_blob(stmt,
509 svn_sqlite__bind_checksum(svn_sqlite__stmt_t *stmt,
511 const svn_checksum_t *checksum,
512 apr_pool_t *scratch_pool)
514 const char *csum_str;
516 if (checksum == NULL)
519 csum_str = svn_checksum_serialize(checksum, scratch_pool, scratch_pool);
521 return svn_error_trace(svn_sqlite__bind_text(stmt, slot, csum_str));
526 svn_sqlite__column_blob(svn_sqlite__stmt_t *stmt, int column,
527 apr_size_t *len, apr_pool_t *result_pool)
529 const void *val = sqlite3_column_blob(stmt->s3stmt, column);
530 *len = sqlite3_column_bytes(stmt->s3stmt, column);
532 if (result_pool && val != NULL)
533 val = apr_pmemdup(result_pool, val, *len);
539 svn_sqlite__column_text(svn_sqlite__stmt_t *stmt, int column,
540 apr_pool_t *result_pool)
542 /* cast from 'unsigned char' to regular 'char' */
543 const char *result = (const char *)sqlite3_column_text(stmt->s3stmt, column);
545 if (result_pool && result != NULL)
546 result = apr_pstrdup(result_pool, result);
552 svn_sqlite__column_revnum(svn_sqlite__stmt_t *stmt, int column)
554 if (svn_sqlite__column_is_null(stmt, column))
555 return SVN_INVALID_REVNUM;
556 return (svn_revnum_t) sqlite3_column_int64(stmt->s3stmt, column);
560 svn_sqlite__column_boolean(svn_sqlite__stmt_t *stmt, int column)
562 return sqlite3_column_int64(stmt->s3stmt, column) != 0;
566 svn_sqlite__column_int(svn_sqlite__stmt_t *stmt, int column)
568 return sqlite3_column_int(stmt->s3stmt, column);
572 svn_sqlite__column_int64(svn_sqlite__stmt_t *stmt, int column)
574 return sqlite3_column_int64(stmt->s3stmt, column);
578 svn_sqlite__column_token(svn_sqlite__stmt_t *stmt,
580 const svn_token_map_t *map)
582 /* cast from 'unsigned char' to regular 'char' */
583 const char *word = (const char *)sqlite3_column_text(stmt->s3stmt, column);
585 return svn_token__from_word_strict(map, word);
589 svn_sqlite__column_token_null(svn_sqlite__stmt_t *stmt,
591 const svn_token_map_t *map,
594 /* cast from 'unsigned char' to regular 'char' */
595 const char *word = (const char *)sqlite3_column_text(stmt->s3stmt, column);
600 return svn_token__from_word_strict(map, word);
604 svn_sqlite__column_properties(apr_hash_t **props,
605 svn_sqlite__stmt_t *stmt,
607 apr_pool_t *result_pool,
608 apr_pool_t *scratch_pool)
613 /* svn_skel__parse_proplist copies everything needed to result_pool */
614 val = svn_sqlite__column_blob(stmt, column, &len, NULL);
621 SVN_ERR(svn_skel__parse_proplist(props,
622 svn_skel__parse(val, len, scratch_pool),
629 svn_sqlite__column_iprops(apr_array_header_t **iprops,
630 svn_sqlite__stmt_t *stmt,
632 apr_pool_t *result_pool,
633 apr_pool_t *scratch_pool)
638 /* svn_skel__parse_iprops copies everything needed to result_pool */
639 val = svn_sqlite__column_blob(stmt, column, &len, NULL);
646 SVN_ERR(svn_skel__parse_iprops(iprops,
647 svn_skel__parse(val, len, scratch_pool),
654 svn_sqlite__column_checksum(const svn_checksum_t **checksum,
655 svn_sqlite__stmt_t *stmt, int column,
656 apr_pool_t *result_pool)
658 const char *digest = svn_sqlite__column_text(stmt, column, NULL);
663 SVN_ERR(svn_checksum_deserialize(checksum, digest,
664 result_pool, result_pool));
670 svn_sqlite__column_is_null(svn_sqlite__stmt_t *stmt, int column)
672 return sqlite3_column_type(stmt->s3stmt, column) == SQLITE_NULL;
676 svn_sqlite__column_bytes(svn_sqlite__stmt_t *stmt, int column)
678 return sqlite3_column_bytes(stmt->s3stmt, column);
682 svn_sqlite__finalize(svn_sqlite__stmt_t *stmt)
684 SQLITE_ERR(sqlite3_finalize(stmt->s3stmt), stmt->db);
689 svn_sqlite__reset(svn_sqlite__stmt_t *stmt)
691 SQLITE_ERR(sqlite3_reset(stmt->s3stmt), stmt->db);
692 SQLITE_ERR(sqlite3_clear_bindings(stmt->s3stmt), stmt->db);
693 stmt->needs_reset = FALSE;
699 svn_sqlite__read_schema_version(int *version,
700 svn_sqlite__db_t *db,
701 apr_pool_t *scratch_pool)
703 svn_sqlite__stmt_t *stmt;
705 SVN_ERR(prepare_statement(&stmt, db, "PRAGMA user_version;", scratch_pool));
706 SVN_ERR(svn_sqlite__step_row(stmt));
708 *version = svn_sqlite__column_int(stmt, 0);
710 return svn_error_trace(svn_sqlite__finalize(stmt));
714 static volatile svn_atomic_t sqlite_init_state = 0;
716 /* If possible, verify that SQLite was compiled in a thread-safe
718 /* Don't call this function directly! Use svn_atomic__init_once(). */
720 init_sqlite(void *baton, apr_pool_t *pool)
722 if (sqlite3_libversion_number() < SVN_SQLITE_MIN_VERSION_NUMBER)
724 return svn_error_createf(
725 SVN_ERR_SQLITE_ERROR, NULL,
726 _("SQLite compiled for %s, but running with %s"),
727 SVN_SQLITE_MIN_VERSION, sqlite3_libversion());
732 /* SQLite 3.5 allows verification of its thread-safety at runtime.
733 Older versions are simply expected to have been configured with
734 --enable-threadsafe, which compiles with -DSQLITE_THREADSAFE=1
735 (or -DTHREADSAFE, for older versions). */
736 if (! sqlite3_threadsafe())
737 return svn_error_create(SVN_ERR_SQLITE_ERROR, NULL,
738 _("SQLite is required to be compiled and run in "
739 "thread-safe mode"));
741 /* If SQLite has been already initialized, sqlite3_config() returns
744 int err = sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
745 if (err != SQLITE_OK && err != SQLITE_MISUSE)
746 return svn_error_createf(SQLITE_ERROR_CODE(err), NULL,
747 _("Could not configure SQLite [S%d]"), err);
749 SQLITE_ERR_MSG(sqlite3_initialize(), _("Could not initialize SQLite"));
751 #endif /* APR_HAS_THRADS */
757 internal_open(sqlite3 **db3, const char *path, svn_sqlite__mode_t mode,
758 apr_pool_t *scratch_pool)
763 if (mode == svn_sqlite__mode_readonly)
764 flags = SQLITE_OPEN_READONLY;
765 else if (mode == svn_sqlite__mode_readwrite)
766 flags = SQLITE_OPEN_READWRITE;
767 else if (mode == svn_sqlite__mode_rwcreate)
768 flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
770 SVN_ERR_MALFUNCTION();
772 /* Turn off SQLite's mutexes. All svn objects are single-threaded,
773 so we can already guarantee that our use of the SQLite handle
774 will be serialized properly.
776 Note: in 3.6.x, we've already config'd SQLite into MULTITHREAD mode,
777 so this is probably redundant, but if we are running in a process where
778 somebody initialized SQLite before us it is needed anyway. */
779 flags |= SQLITE_OPEN_NOMUTEX;
781 #if !defined(WIN32) && !defined(SVN_SQLITE_INLINE)
782 if (mode == svn_sqlite__mode_rwcreate)
784 svn_node_kind_t kind;
786 /* Create the file before SQLite to avoid any permissions
787 problems with an SQLite build that uses the default
788 SQLITE_DEFAULT_FILE_PERMISSIONS of 644 modified by umask.
789 We simply want umask permissions. */
790 SVN_ERR(svn_io_check_path(path, &kind, scratch_pool));
791 if (kind == svn_node_none)
792 SVN_ERR(svn_io_file_create(path, "", scratch_pool));
796 /* Open the database. Note that a handle is returned, even when an error
797 occurs (except for out-of-memory); thus, we can safely use it to
798 extract an error message and construct an svn_error_t. */
800 /* We'd like to use SQLITE_ERR here, but we can't since it would
801 just return an error and leave the database open. So, we need to
803 /* ### SQLITE_CANTOPEN */
804 int err_code = sqlite3_open_v2(path, db3, flags, NULL);
805 if (err_code != SQLITE_OK)
807 /* Save the error message before closing the SQLite handle. */
808 char *msg = apr_pstrdup(scratch_pool, sqlite3_errmsg(*db3));
810 /* We don't catch the error here, since we care more about the open
811 error than the close error at this point. */
814 SQLITE_ERR_MSG(err_code, msg);
819 /* Retry until timeout when database is busy. */
820 SQLITE_ERR_MSG(sqlite3_busy_timeout(*db3, BUSY_TIMEOUT),
821 sqlite3_errmsg(*db3));
827 /* APR cleanup function used to close the database when its pool is destroyed.
828 DATA should be the svn_sqlite__db_t handle for the database. */
830 close_apr(void *data)
832 svn_sqlite__db_t *db = data;
833 svn_error_t *err = SVN_NO_ERROR;
837 /* Check to see if we've already closed this database. */
841 /* Finalize any existing prepared statements. */
842 for (i = 0; i < db->nbr_statements; i++)
844 if (db->prepared_stmts[i])
846 if (db->prepared_stmts[i]->needs_reset)
849 const char *stmt_text = db->statement_strings[i];
850 stmt_text = stmt_text; /* Provide value for debugger */
852 SVN_ERR_MALFUNCTION_NO_RETURN();
854 err = svn_error_compose_create(
856 svn_sqlite__reset(db->prepared_stmts[i]));
859 err = svn_error_compose_create(
860 svn_sqlite__finalize(db->prepared_stmts[i]), err);
863 /* And finalize any used internal statements */
864 for (; i < db->nbr_statements + STMT_INTERNAL_LAST; i++)
866 if (db->prepared_stmts[i])
868 err = svn_error_compose_create(
869 svn_sqlite__finalize(db->prepared_stmts[i]), err);
873 result = sqlite3_close(db->db3);
875 /* If there's a pre-existing error, return it. */
878 result = err->apr_err;
879 svn_error_clear(err);
883 if (result != SQLITE_OK)
884 return SQLITE_ERROR_CODE(result); /* ### lossy */
893 svn_sqlite__open(svn_sqlite__db_t **db, const char *path,
894 svn_sqlite__mode_t mode, const char * const statements[],
895 int unused1, const char * const *unused2,
896 apr_pool_t *result_pool, apr_pool_t *scratch_pool)
898 SVN_ERR(svn_atomic__init_once(&sqlite_init_state,
899 init_sqlite, NULL, scratch_pool));
901 *db = apr_pcalloc(result_pool, sizeof(**db));
903 SVN_ERR(internal_open(&(*db)->db3, path, mode, scratch_pool));
905 #if SQLITE_VERSION_NUMBER >= 3008000 && SQLITE_VERSION_NUMBER < 3009000
906 /* disable SQLITE_ENABLE_STAT3/4 from 3.8.1 - 3.8.3 (but not 3.8.3.1+)
907 * to prevent using it when it's buggy.
908 * See: https://www.sqlite.org/src/info/4c86b126f2 */
909 if (sqlite3_libversion_number() > 3008000 &&
910 sqlite3_libversion_number() < 3008004 &&
911 strcmp(sqlite3_sourceid(),"2014-02-11")<0)
913 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, (*db)->db3, 0x800);
918 sqlite3_trace((*db)->db3, sqlite_tracer, (*db)->db3);
920 #ifdef SQLITE3_PROFILE
921 sqlite3_profile((*db)->db3, sqlite_profiler, (*db)->db3);
924 /* ### simplify this. remnants of some old SQLite compat code. */
926 int ignored_err = SQLITE_OK;
928 SVN_ERR(exec_sql2(*db, "PRAGMA case_sensitive_like=1;", ignored_err));
931 SVN_ERR(exec_sql(*db,
932 /* Disable synchronization to disable the explicit disk flushes
933 that make Sqlite up to 50 times slower; especially on small
936 This removes some stability guarantees on specific hardware
937 and power failures, but still guarantees atomic commits on
938 application crashes. With our dependency on external data
939 like pristine files (Wc) and revision files (repository),
940 we can't keep up these additional guarantees anyway.
942 ### Maybe switch to NORMAL(1) when we use larger transaction
944 "PRAGMA synchronous=OFF;"
945 /* Enable recursive triggers so that a user trigger will fire
946 in the deletion phase of an INSERT OR REPLACE statement.
947 Requires SQLite >= 3.6.18 */
948 "PRAGMA recursive_triggers=ON;"
949 /* Enforce current Sqlite default behavior. Some distributions
950 might change the Sqlite defaults without realizing how this
951 affects application(read: Subversion) performance/behavior. */
952 "PRAGMA foreign_keys=OFF;" /* SQLITE_DEFAULT_FOREIGN_KEYS*/
953 "PRAGMA locking_mode = NORMAL;" /* SQLITE_DEFAULT_LOCKING_MODE */
956 #if defined(SVN_DEBUG)
957 /* When running in debug mode, enable the checking of foreign key
958 constraints. This has possible performance implications, so we don't
959 bother to do it for production...for now. */
960 SVN_ERR(exec_sql(*db, "PRAGMA foreign_keys=ON;"));
963 #ifdef SVN_SQLITE_REVERSE_UNORDERED_SELECTS
964 /* When enabled, this PRAGMA causes SELECT statements without an ORDER BY
965 clause to emit their results in the reverse order of what they normally
966 would. This can help detecting invalid assumptions about the result
968 SVN_ERR(exec_sql(*db, "PRAGMA reverse_unordered_selects=ON;"));
971 /* Store temporary tables in RAM instead of in temporary files, but don't
972 fail on this if this option is disabled in the sqlite compilation by
973 setting SQLITE_TEMP_STORE to 0 (always to disk) */
974 svn_error_clear(exec_sql(*db, "PRAGMA temp_store = MEMORY;"));
976 /* Store the provided statements. */
979 (*db)->statement_strings = statements;
980 (*db)->nbr_statements = 0;
981 while (*statements != NULL)
984 (*db)->nbr_statements++;
987 (*db)->prepared_stmts = apr_pcalloc(
989 ((*db)->nbr_statements + STMT_INTERNAL_LAST)
990 * sizeof(svn_sqlite__stmt_t *));
994 (*db)->nbr_statements = 0;
995 (*db)->prepared_stmts = apr_pcalloc(result_pool,
996 (0 + STMT_INTERNAL_LAST)
997 * sizeof(svn_sqlite__stmt_t *));
1000 (*db)->state_pool = result_pool;
1001 apr_pool_cleanup_register(result_pool, *db, close_apr, apr_pool_cleanup_null);
1003 return SVN_NO_ERROR;
1007 svn_sqlite__close(svn_sqlite__db_t *db)
1009 apr_status_t result = apr_pool_cleanup_run(db->state_pool, db, close_apr);
1011 if (result == APR_SUCCESS)
1012 return SVN_NO_ERROR;
1014 return svn_error_wrap_apr(result, NULL);
1017 static svn_error_t *
1018 reset_all_statements(svn_sqlite__db_t *db,
1019 svn_error_t *error_to_wrap)
1024 /* ### Should we reorder the errors in this specific case
1025 ### to avoid returning the normal error as top level error? */
1027 err = svn_error_compose_create(error_to_wrap,
1028 svn_error_create(SVN_ERR_SQLITE_RESETTING_FOR_ROLLBACK,
1031 for (i = 0; i < db->nbr_statements; i++)
1032 if (db->prepared_stmts[i] && db->prepared_stmts[i]->needs_reset)
1033 err = svn_error_compose_create(err,
1034 svn_sqlite__reset(db->prepared_stmts[i]));
1040 svn_sqlite__begin_transaction(svn_sqlite__db_t *db)
1042 svn_sqlite__stmt_t *stmt;
1044 SVN_ERR(get_internal_statement(&stmt, db,
1045 STMT_INTERNAL_BEGIN_TRANSACTION));
1046 SVN_ERR(svn_sqlite__step_done(stmt));
1047 return SVN_NO_ERROR;
1051 svn_sqlite__begin_immediate_transaction(svn_sqlite__db_t *db)
1053 svn_sqlite__stmt_t *stmt;
1055 SVN_ERR(get_internal_statement(&stmt, db,
1056 STMT_INTERNAL_BEGIN_IMMEDIATE_TRANSACTION));
1057 SVN_ERR(svn_sqlite__step_done(stmt));
1058 return SVN_NO_ERROR;
1062 svn_sqlite__begin_savepoint(svn_sqlite__db_t *db)
1064 svn_sqlite__stmt_t *stmt;
1066 SVN_ERR(get_internal_statement(&stmt, db,
1067 STMT_INTERNAL_SAVEPOINT_SVN));
1068 SVN_ERR(svn_sqlite__step_done(stmt));
1069 return SVN_NO_ERROR;
1073 svn_sqlite__finish_transaction(svn_sqlite__db_t *db,
1076 svn_sqlite__stmt_t *stmt;
1078 /* Commit or rollback the sqlite transaction. */
1083 err2 = get_internal_statement(&stmt, db,
1084 STMT_INTERNAL_ROLLBACK_TRANSACTION);
1086 err2 = svn_sqlite__step_done(stmt);
1088 if (err2 && err2->apr_err == SVN_ERR_SQLITE_BUSY)
1090 /* ### Houston, we have a problem!
1092 We are trying to rollback but we can't because some
1093 statements are still busy. This leaves the database
1094 unusable for future transactions as the current transaction
1097 As we are returning the actual error as the most relevant
1098 error in the chain, our caller might assume that it can
1099 retry/compensate on this error (e.g. SVN_WC_LOCKED), while
1100 in fact the SQLite database is unusable until the statements
1101 started within this transaction are reset and the transaction
1104 We try to compensate by resetting all prepared but unreset
1105 statements; but we leave the busy error in the chain anyway to
1106 help diagnosing the original error and help in finding where
1107 a reset statement is missing. */
1109 err2 = reset_all_statements(db, err2);
1110 err2 = svn_error_compose_create(
1111 svn_sqlite__step_done(stmt),
1115 return svn_error_compose_create(err,
1119 SVN_ERR(get_internal_statement(&stmt, db, STMT_INTERNAL_COMMIT_TRANSACTION));
1120 return svn_error_trace(svn_sqlite__step_done(stmt));
1124 svn_sqlite__finish_savepoint(svn_sqlite__db_t *db,
1127 svn_sqlite__stmt_t *stmt;
1133 err2 = get_internal_statement(&stmt, db,
1134 STMT_INTERNAL_ROLLBACK_TO_SAVEPOINT_SVN);
1137 err2 = svn_sqlite__step_done(stmt);
1139 if (err2 && err2->apr_err == SVN_ERR_SQLITE_BUSY)
1141 /* Ok, we have a major problem. Some statement is still open, which
1142 makes it impossible to release this savepoint.
1144 ### See huge comment in svn_sqlite__finish_transaction for
1147 err2 = reset_all_statements(db, err2);
1148 err2 = svn_error_compose_create(svn_sqlite__step_done(stmt), err2);
1151 err = svn_error_compose_create(err, err2);
1152 err2 = get_internal_statement(&stmt, db,
1153 STMT_INTERNAL_RELEASE_SAVEPOINT_SVN);
1156 err2 = svn_sqlite__step_done(stmt);
1158 return svn_error_trace(svn_error_compose_create(err, err2));
1161 SVN_ERR(get_internal_statement(&stmt, db,
1162 STMT_INTERNAL_RELEASE_SAVEPOINT_SVN));
1164 return svn_error_trace(svn_sqlite__step_done(stmt));
1168 svn_sqlite__with_transaction(svn_sqlite__db_t *db,
1169 svn_sqlite__transaction_callback_t cb_func,
1171 apr_pool_t *scratch_pool /* NULL allowed */)
1173 SVN_SQLITE__WITH_TXN(cb_func(cb_baton, db, scratch_pool), db);
1174 return SVN_NO_ERROR;
1178 svn_sqlite__with_immediate_transaction(
1179 svn_sqlite__db_t *db,
1180 svn_sqlite__transaction_callback_t cb_func,
1182 apr_pool_t *scratch_pool /* NULL allowed */)
1184 SVN_SQLITE__WITH_IMMEDIATE_TXN(cb_func(cb_baton, db, scratch_pool), db);
1185 return SVN_NO_ERROR;
1189 svn_sqlite__with_lock(svn_sqlite__db_t *db,
1190 svn_sqlite__transaction_callback_t cb_func,
1192 apr_pool_t *scratch_pool /* NULL allowed */)
1194 SVN_SQLITE__WITH_LOCK(cb_func(cb_baton, db, scratch_pool), db);
1195 return SVN_NO_ERROR;
1199 svn_sqlite__hotcopy(const char *src_path,
1200 const char *dst_path,
1201 apr_pool_t *scratch_pool)
1203 svn_sqlite__db_t *src_db;
1205 SVN_ERR(svn_sqlite__open(&src_db, src_path, svn_sqlite__mode_readonly,
1207 scratch_pool, scratch_pool));
1210 svn_sqlite__db_t *dst_db;
1211 sqlite3_backup *backup;
1214 SVN_ERR(svn_sqlite__open(&dst_db, dst_path, svn_sqlite__mode_rwcreate,
1215 NULL, 0, NULL, scratch_pool, scratch_pool));
1216 backup = sqlite3_backup_init(dst_db->db3, "main", src_db->db3, "main");
1218 return svn_error_createf(SVN_ERR_SQLITE_ERROR, NULL,
1219 _("SQLite hotcopy failed for %s"), src_path);
1222 /* Pages are usually 1024 byte (SQLite docs). On my laptop
1223 copying gets faster as the number of pages is increased up
1224 to about 64, beyond that speed levels off. Lets put the
1225 number of pages an order of magnitude higher, this is still
1226 likely to be a fraction of large databases. */
1227 rc1 = sqlite3_backup_step(backup, 1024);
1229 /* Should we sleep on SQLITE_OK? That would make copying a
1230 large database take much longer. When we do sleep how,
1231 long should we sleep? Should the sleep get longer if we
1232 keep getting BUSY/LOCKED? I have no real reason for
1234 if (rc1 == SQLITE_BUSY || rc1 == SQLITE_LOCKED)
1237 while (rc1 == SQLITE_OK || rc1 == SQLITE_BUSY || rc1 == SQLITE_LOCKED);
1238 rc2 = sqlite3_backup_finish(backup);
1239 if (rc1 != SQLITE_DONE)
1240 SQLITE_ERR(rc1, dst_db);
1241 SQLITE_ERR(rc2, dst_db);
1242 SVN_ERR(svn_sqlite__close(dst_db));
1245 SVN_ERR(svn_sqlite__close(src_db));
1247 return SVN_NO_ERROR;
1250 struct function_wrapper_baton_t
1252 svn_sqlite__func_t func;
1255 apr_pool_t *scratch_pool;
1259 wrapped_func(sqlite3_context *context,
1261 sqlite3_value *values[])
1263 struct function_wrapper_baton_t *fwb = sqlite3_user_data(context);
1264 svn_sqlite__context_t sctx;
1265 svn_sqlite__value_t **local_vals =
1266 apr_palloc(fwb->scratch_pool,
1267 sizeof(svn_sqlite__value_t *) * argc);
1271 sctx.context = context;
1273 for (i = 0; i < argc; i++)
1275 local_vals[i] = apr_palloc(fwb->scratch_pool, sizeof(*local_vals[i]));
1276 local_vals[i]->value = values[i];
1279 err = fwb->func(&sctx, argc, local_vals, fwb->scratch_pool);
1280 svn_pool_clear(fwb->scratch_pool);
1285 sqlite3_result_error(context,
1286 svn_err_best_message(err, buf, sizeof(buf)),
1288 svn_error_clear(err);
1293 svn_sqlite__create_scalar_function(svn_sqlite__db_t *db,
1294 const char *func_name,
1296 svn_sqlite__func_t func,
1299 struct function_wrapper_baton_t *fwb = apr_pcalloc(db->state_pool,
1302 fwb->scratch_pool = svn_pool_create(db->state_pool);
1306 SQLITE_ERR(sqlite3_create_function(db->db3, func_name, argc, SQLITE_ANY,
1307 fwb, wrapped_func, NULL, NULL),
1310 return SVN_NO_ERROR;
1314 svn_sqlite__value_type(svn_sqlite__value_t *val)
1316 return sqlite3_value_type(val->value);
1320 svn_sqlite__value_text(svn_sqlite__value_t *val)
1322 return (const char *) sqlite3_value_text(val->value);
1326 svn_sqlite__result_null(svn_sqlite__context_t *sctx)
1328 sqlite3_result_null(sctx->context);
1332 svn_sqlite__result_int64(svn_sqlite__context_t *sctx, apr_int64_t val)
1334 sqlite3_result_int64(sctx->context, val);