145
189
sqlite3_errmsg((db)->db3)); \
192
#define SQLITE_ERR_CLOSE(x, db, pool) do \
194
int sqlite_err__temp = (x); \
195
if (sqlite_err__temp != SQLITE_OK) \
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)); \
148
207
#define SQLITE_ERR_MSG(x, msg) do \
150
209
int sqlite_err__temp = (x); \
689
755
svn_sqlite__reset(svn_sqlite__stmt_t *stmt)
757
/* No need to reset again after a first attempt */
758
stmt->needs_reset = FALSE;
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);
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).
769
See the sqlite3_reset() documentation for more details. */
691
770
SQLITE_ERR(sqlite3_reset(stmt->s3stmt), stmt->db);
692
SQLITE_ERR(sqlite3_clear_bindings(stmt->s3stmt), stmt->db);
693
stmt->needs_reset = FALSE;
694
771
return SVN_NO_ERROR;
789
866
We simply want umask permissions. */
790
867
SVN_ERR(svn_io_check_path(path, &kind, scratch_pool));
791
868
if (kind == svn_node_none)
792
SVN_ERR(svn_io_file_create(path, "", scratch_pool));
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);
796
879
/* Open the database. Note that a handle is returned, even when an error
797
880
occurs (except for out-of-memory); thus, we can safely use it to
798
extract an error message and construct an svn_error_t. */
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. */
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
887
const char *vFs = NULL;
889
#if defined(WIN32) && SQLITE_VERSION_AT_LEAST(3, 8, 1)
890
if (strlen(path) > 248)
893
vFs = "win32-longpath"; /* Enable long paths in sqlite */
895
/* Long paths must be absolute */
896
if (!svn_dirent_is_absolute(path))
897
SVN_ERR(svn_dirent_get_absolute(&path, path, scratch_pool));
899
/* Convert the path to a properly canonicalized \\?\C:\long\path */
900
SVN_ERR(svn_io__utf8_to_unicode_longpath(&win_path, path,
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,
803
910
/* ### 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);
911
SQLITE_ERR_CLOSE(sqlite3_open_v2(path, &db->db3, flags, vFs),
917
timeout = BUSY_TIMEOUT;
819
919
/* Retry until timeout when database is busy. */
820
SQLITE_ERR_MSG(sqlite3_busy_timeout(*db3, BUSY_TIMEOUT),
821
sqlite3_errmsg(*db3));
920
SQLITE_ERR_CLOSE(sqlite3_busy_timeout(db->db3, timeout),
823
923
return SVN_NO_ERROR;
838
938
if (db->db3 == NULL)
839
939
return APR_SUCCESS;
841
/* Finalize any existing prepared statements. */
842
for (i = 0; i < db->nbr_statements; i++)
941
/* Finalize any prepared statements. */
942
if (db->prepared_stmts)
844
if (db->prepared_stmts[i])
944
for (i = 0; i < db->nbr_statements + STMT_INTERNAL_LAST; i++)
846
if (db->prepared_stmts[i]->needs_reset)
946
if (db->prepared_stmts[i])
948
if (i < db->nbr_statements
949
&& db->prepared_stmts[i]->needs_reset)
849
const char *stmt_text = db->statement_strings[i];
850
stmt_text = stmt_text; /* Provide value for debugger */
952
const char *stmt_text = db->statement_strings[i];
953
SVN_UNUSED(stmt_text);
852
SVN_ERR_MALFUNCTION_NO_RETURN();
955
SVN_ERR_MALFUNCTION_NO_RETURN();
957
err = svn_error_compose_create(err,
958
svn_sqlite__reset(db->prepared_stmts[i]));
854
961
err = svn_error_compose_create(
856
svn_sqlite__reset(db->prepared_stmts[i]));
962
svn_sqlite__finalize(db->prepared_stmts[i]), err);
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);
888
982
return APR_SUCCESS;
985
#ifdef SVN_UNICODE_NORMALIZATION_FIXES
986
/* Unicode normalizing collation for WC paths */
988
collate_ucs_nfd(void *baton,
989
int len1, const void *key1,
990
int len2, const void *key2)
992
svn_sqlite__db_t *db = baton;
995
if (svn_utf__normcmp(key1, len1, key2, len2,
996
&db->sqlext_buf1, &db->sqlext_buf2, &result))
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();
1010
glob_like_ucs_nfd_common(sqlite3_context *context,
1011
int argc, sqlite3_value **argv,
1012
svn_boolean_t sql_like)
1014
svn_sqlite__db_t *const db = sqlite3_user_data(context);
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]);
1021
const char *escape = NULL;
1022
apr_size_t escape_len = 0;
1024
svn_boolean_t match;
1027
if (pattern_len > SQLITE_MAX_LIKE_PATTERN_LENGTH)
1029
sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
1033
if (argc == 3 && sql_like)
1035
escape = (void*)sqlite3_value_text(argv[2]);
1036
escape_len = sqlite3_value_bytes(argv[2]);
1039
if (pattern && string)
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,
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);
1058
sqlite3_result_int(context, match);
1062
/* Unicode normalizing implementation of GLOB */
1064
glob_ucs_nfd(sqlite3_context *context,
1065
int argc, sqlite3_value **argv)
1067
glob_like_ucs_nfd_common(context, argc, argv, FALSE);
1070
/* Unicode normalizing implementation of LIKE */
1072
like_ucs_nfd(sqlite3_context *context,
1073
int argc, sqlite3_value **argv)
1075
glob_like_ucs_nfd_common(context, argc, argv, TRUE);
1077
#endif /* SVN_UNICODE_NORMALIZATION_FIXES */
893
1080
svn_sqlite__open(svn_sqlite__db_t **db, const char *path,
894
1081
svn_sqlite__mode_t mode, const char * const statements[],
895
1082
int unused1, const char * const *unused2,
1083
apr_int32_t timeout,
896
1084
apr_pool_t *result_pool, apr_pool_t *scratch_pool)
898
1086
SVN_ERR(svn_atomic__init_once(&sqlite_init_state,
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);
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),
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...
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
1123
SQLITE_ERR_CLOSE(sqlite3_create_function((*db)->db3, "glob", 2,
1124
SQLITE_UTF8 | SQLITE_DETERMINISTIC,
1125
*db, glob_ucs_nfd, NULL, NULL),
1127
SQLITE_ERR_CLOSE(sqlite3_create_function((*db)->db3, "like", 2,
1128
SQLITE_UTF8 | SQLITE_DETERMINISTIC,
1129
*db, like_ucs_nfd, NULL, NULL),
1131
SQLITE_ERR_CLOSE(sqlite3_create_function((*db)->db3, "like", 3,
1132
SQLITE_UTF8 | SQLITE_DETERMINISTIC,
1133
*db, like_ucs_nfd, NULL, NULL),
1135
#endif /* SVN_UNICODE_NORMALIZATION_FIXES */
917
1137
#ifdef SQLITE3_DEBUG
918
1138
sqlite3_trace((*db)->db3, sqlite_tracer, (*db)->db3);
921
1141
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,
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
1151
"PRAGMA case_sensitive_like=1;"
932
1152
/* Disable synchronization to disable the explicit disk flushes
933
1153
that make Sqlite up to 50 times slower; especially on small
951
1171
affects application(read: Subversion) performance/behavior. */
952
1172
"PRAGMA foreign_keys=OFF;" /* SQLITE_DEFAULT_FOREIGN_KEYS*/
953
1173
"PRAGMA locking_mode = NORMAL;" /* SQLITE_DEFAULT_LOCKING_MODE */
1174
"PRAGMA journal_mode = TRUNCATE;"
956
1178
#if defined(SVN_DEBUG)
957
1179
/* When running in debug mode, enable the checking of foreign key
958
1180
constraints. This has possible performance implications, so we don't
959
1181
bother to do it for production...for now. */
960
SVN_ERR(exec_sql(*db, "PRAGMA foreign_keys=ON;"));
1182
SVN_ERR_CLOSE(exec_sql(*db, "PRAGMA foreign_keys=ON;"),
963
1186
#ifdef SVN_SQLITE_REVERSE_UNORDERED_SELECTS
1214
1438
SVN_ERR(svn_sqlite__open(&dst_db, dst_path, svn_sqlite__mode_rwcreate,
1215
NULL, 0, NULL, scratch_pool, scratch_pool));
1439
NULL, 0, NULL, 0, scratch_pool, scratch_pool));
1216
1440
backup = sqlite3_backup_init(dst_db->db3, "main", src_db->db3, "main");
1218
1442
return svn_error_createf(SVN_ERR_SQLITE_ERROR, NULL,
1263
1487
struct function_wrapper_baton_t *fwb = sqlite3_user_data(context);
1264
1488
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);
1268
1489
svn_error_t *err;
1490
void *void_values = values;
1271
1492
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);
1494
err = fwb->func(&sctx, argc, void_values, fwb->baton);
1293
1508
svn_sqlite__create_scalar_function(svn_sqlite__db_t *db,
1294
1509
const char *func_name,
1511
svn_boolean_t deterministic,
1296
1512
svn_sqlite__func_t func,
1299
1516
struct function_wrapper_baton_t *fwb = apr_pcalloc(db->state_pool,
1302
fwb->scratch_pool = svn_pool_create(db->state_pool);
1303
1519
fwb->func = func;
1304
1520
fwb->baton = baton;
1306
SQLITE_ERR(sqlite3_create_function(db->db3, func_name, argc, SQLITE_ANY,
1522
eTextRep = SQLITE_ANY;
1524
eTextRep |= SQLITE_DETERMINISTIC;
1526
SQLITE_ERR(sqlite3_create_function(db->db3, func_name, argc, eTextRep,
1307
1527
fwb, wrapped_func, NULL, NULL),