76
121
int sqlite_err__temp = (x); \
77
122
if (sqlite_err__temp != SQLITE_OK) \
78
return svn_error_create(SQLITE_ERROR_CODE(sqlite_err__temp), \
79
NULL, sqlite3_errmsg((db)->db3)); \
123
return svn_error_createf(SQLITE_ERROR_CODE(sqlite_err__temp), \
124
NULL, "sqlite: %s", \
125
sqlite3_errmsg((db)->db3)); \
82
128
#define SQLITE_ERR_MSG(x, msg) do \
84
130
int sqlite_err__temp = (x); \
85
131
if (sqlite_err__temp != SQLITE_OK) \
86
return svn_error_create(SQLITE_ERROR_CODE(sqlite_err__temp), \
132
return svn_error_createf(SQLITE_ERROR_CODE(sqlite_err__temp), \
133
NULL, "sqlite: %s", (msg)); \
92
svn_sqlite__exec(svn_sqlite__db_t *db, const char *sql)
137
/* Convenience wrapper around exec_sql2(). */
138
#define exec_sql(db, sql) exec_sql2((db), (sql), SQLITE_OK)
140
/* Run the statement SQL on DB, ignoring SQLITE_OK and IGNORED_ERR.
141
(Note: the IGNORED_ERR parameter itself is not ignored.) */
143
exec_sql2(svn_sqlite__db_t *db, const char *sql, int ignored_err)
95
146
int sqlite_err = sqlite3_exec(db->db3, sql, NULL, NULL, &err_msg);
97
if (sqlite_err != SQLITE_OK)
148
if (sqlite_err != SQLITE_OK && sqlite_err != ignored_err)
99
svn_error_t *err = svn_error_create(SQLITE_ERROR_CODE(sqlite_err), NULL,
150
svn_error_t *err = svn_error_createf(SQLITE_ERROR_CODE(sqlite_err), NULL,
151
_("%s, executing statement '%s'"),
101
153
sqlite3_free(err_msg);
297
SQLITE_ERR(sqlite3_bind_blob(stmt->s3stmt, slot, val, len, SQLITE_TRANSIENT),
378
SQLITE_ERR(sqlite3_bind_blob(stmt->s3stmt, slot, val, (int) len,
385
svn_sqlite__bind_token(svn_sqlite__stmt_t *stmt,
387
const svn_token_map_t *map,
390
const char *word = svn_token__to_word(map, value);
392
SQLITE_ERR(sqlite3_bind_text(stmt->s3stmt, slot, word, -1, SQLITE_STATIC),
398
svn_sqlite__bind_revnum(svn_sqlite__stmt_t *stmt,
402
if (SVN_IS_VALID_REVNUM(value))
403
SQLITE_ERR(sqlite3_bind_int64(stmt->s3stmt, slot,
404
(sqlite_int64)value), stmt->db);
406
SQLITE_ERR(sqlite3_bind_null(stmt->s3stmt, slot), stmt->db);
412
svn_sqlite__bind_properties(svn_sqlite__stmt_t *stmt,
414
const apr_hash_t *props,
415
apr_pool_t *scratch_pool)
418
svn_stringbuf_t *properties;
421
return svn_error_trace(svn_sqlite__bind_blob(stmt, slot, NULL, 0));
423
SVN_ERR(svn_skel__unparse_proplist(&skel, (apr_hash_t *)props,
425
properties = svn_skel__unparse(skel, scratch_pool);
426
return svn_error_trace(svn_sqlite__bind_blob(stmt,
433
svn_sqlite__bind_checksum(svn_sqlite__stmt_t *stmt,
435
const svn_checksum_t *checksum,
436
apr_pool_t *scratch_pool)
438
const char *csum_str;
440
if (checksum == NULL)
443
csum_str = svn_checksum_serialize(checksum, scratch_pool, scratch_pool);
445
return svn_error_trace(svn_sqlite__bind_text(stmt, slot, csum_str));
303
svn_sqlite__column_blob(svn_sqlite__stmt_t *stmt, int column, apr_size_t *len)
450
svn_sqlite__column_blob(svn_sqlite__stmt_t *stmt, int column,
451
apr_size_t *len, apr_pool_t *result_pool)
305
453
const void *val = sqlite3_column_blob(stmt->s3stmt, column);
306
454
*len = sqlite3_column_bytes(stmt->s3stmt, column);
456
if (result_pool && val != NULL)
457
val = apr_pmemdup(result_pool, val, *len);
343
498
return sqlite3_column_int64(stmt->s3stmt, column);
502
svn_sqlite__column_token(svn_sqlite__stmt_t *stmt,
504
const svn_token_map_t *map)
506
/* cast from 'unsigned char' to regular 'char' */
507
const char *word = (const char *)sqlite3_column_text(stmt->s3stmt, column);
509
return svn_token__from_word_strict(map, word);
513
svn_sqlite__column_properties(apr_hash_t **props,
514
svn_sqlite__stmt_t *stmt,
516
apr_pool_t *result_pool,
517
apr_pool_t *scratch_pool)
522
/* svn_skel__parse_proplist copies everything needed to result_pool */
523
val = svn_sqlite__column_blob(stmt, column, &len, NULL);
530
SVN_ERR(svn_skel__parse_proplist(props,
531
svn_skel__parse(val, len, scratch_pool),
538
svn_sqlite__column_checksum(const svn_checksum_t **checksum,
539
svn_sqlite__stmt_t *stmt, int column,
540
apr_pool_t *result_pool)
542
const char *digest = svn_sqlite__column_text(stmt, column, NULL);
547
SVN_ERR(svn_checksum_deserialize(checksum, digest,
548
result_pool, result_pool));
347
554
svn_sqlite__column_is_null(svn_sqlite__stmt_t *stmt, int column)
349
556
return sqlite3_column_type(stmt->s3stmt, column) == SQLITE_NULL;
560
svn_sqlite__column_bytes(svn_sqlite__stmt_t *stmt, int column)
562
return sqlite3_column_bytes(stmt->s3stmt, column);
353
566
svn_sqlite__finalize(svn_sqlite__stmt_t *stmt)
650
const char * const *upgrade_sql;
654
/* This implements svn_sqlite__transaction_callback_t */
418
655
static svn_error_t *
419
upgrade_format(svn_sqlite__db_t *db, int current_schema, int latest_schema,
420
const char * const *upgrade_sql, apr_pool_t *scratch_pool)
656
upgrade_format(void *baton,
657
svn_sqlite__db_t *db,
658
apr_pool_t *scratch_pool)
422
while (current_schema < latest_schema)
660
struct upgrade_baton *ub = baton;
661
int current_schema = ub->current_schema;
662
apr_pool_t *iterpool = svn_pool_create(scratch_pool);
664
while (current_schema < ub->latest_schema)
424
const char *pragma_cmd;
666
svn_pool_clear(iterpool);
426
668
/* Go to the next schema */
427
669
current_schema++;
429
671
/* Run the upgrade SQL */
430
SVN_ERR(svn_sqlite__exec(db, upgrade_sql[current_schema]));
672
if (ub->upgrade_sql[current_schema])
673
SVN_ERR(exec_sql(db, ub->upgrade_sql[current_schema]));
432
675
/* Update the user version pragma */
433
pragma_cmd = apr_psprintf(scratch_pool,
434
"PRAGMA user_version = %d;",
436
SVN_ERR(svn_sqlite__exec(db, pragma_cmd));
676
SVN_ERR(svn_sqlite__set_schema_version(db, current_schema, iterpool));
679
svn_pool_destroy(iterpool);
439
681
return SVN_NO_ERROR;
443
get_schema(int *version, svn_sqlite__db_t *db, apr_pool_t *scratch_pool)
685
svn_sqlite__read_schema_version(int *version,
686
svn_sqlite__db_t *db,
687
apr_pool_t *scratch_pool)
445
689
svn_sqlite__stmt_t *stmt;
447
SVN_ERR(svn_sqlite__prepare(&stmt, db, "PRAGMA user_version;", scratch_pool));
691
SVN_ERR(prepare_statement(&stmt, db, "PRAGMA user_version;", scratch_pool));
448
692
SVN_ERR(svn_sqlite__step_row(stmt));
450
694
*version = svn_sqlite__column_int(stmt, 0);
452
return svn_sqlite__finalize(stmt);
696
return svn_error_trace(svn_sqlite__finalize(stmt));
502
758
return svn_error_create(SVN_ERR_SQLITE_ERROR, NULL,
503
759
_("SQLite is required to be compiled and run in "
504
760
"thread-safe mode"));
506
#if SQLITE_VERSION_AT_LEAST(3,6,0)
507
762
/* If SQLite has been already initialized, sqlite3_config() returns
508
763
SQLITE_MISUSE. */
510
765
int err = sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
511
766
if (err != SQLITE_OK && err != SQLITE_MISUSE)
512
767
return svn_error_create(SQLITE_ERROR_CODE(err), NULL,
513
"Could not configure SQLite");
768
_("Could not configure SQLite"));
515
SQLITE_ERR_MSG(sqlite3_initialize(), "Could not initialize SQLite");
770
SQLITE_ERR_MSG(sqlite3_initialize(), _("Could not initialize SQLite"));
517
772
#endif /* APR_HAS_THRADS */
519
774
return SVN_NO_ERROR;
778
internal_open(sqlite3 **db3, const char *path, svn_sqlite__mode_t mode,
779
apr_pool_t *scratch_pool)
784
if (mode == svn_sqlite__mode_readonly)
785
flags = SQLITE_OPEN_READONLY;
786
else if (mode == svn_sqlite__mode_readwrite)
787
flags = SQLITE_OPEN_READWRITE;
788
else if (mode == svn_sqlite__mode_rwcreate)
789
flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
791
SVN_ERR_MALFUNCTION();
793
/* If this flag is defined (3.6.x), then let's turn off SQLite's mutexes.
794
All svn objects are single-threaded, so we can already guarantee that
795
our use of the SQLite handle will be serialized properly.
797
Note: in 3.6.x, we've already config'd SQLite into MULTITHREAD mode,
798
so this is probably redundant, but if we are running in a process where
799
somebody initialized SQLite before us it is needed anyway. */
800
#ifdef SQLITE_OPEN_NOMUTEX
801
flags |= SQLITE_OPEN_NOMUTEX;
804
/* Open the database. Note that a handle is returned, even when an error
805
occurs (except for out-of-memory); thus, we can safely use it to
806
extract an error message and construct an svn_error_t. */
808
/* We'd like to use SQLITE_ERR_MSG here, but we can't since it would
809
just return an error and leave the database open. So, we need to
811
/* ### SQLITE_CANTOPEN */
812
int err_code = sqlite3_open_v2(path, db3, flags, NULL);
813
if (err_code != SQLITE_OK)
815
/* Save the error message before closing the SQLite handle. */
816
char *msg = apr_pstrdup(scratch_pool, sqlite3_errmsg(*db3));
818
/* We don't catch the error here, since we care more about the open
819
error than the close error at this point. */
822
return svn_error_createf(SQLITE_ERROR_CODE(err_code), NULL,
823
"sqlite: %s: '%s'", msg, path);
828
/* Retry until timeout when database is busy. */
829
SQLITE_ERR_MSG(sqlite3_busy_timeout(*db3, BUSY_TIMEOUT),
830
sqlite3_errmsg(*db3));
523
836
svn_sqlite__get_schema_version(int *version,
524
837
const char *path,
578
899
SVN_ERR(svn_atomic__init_once(&sqlite_init_state,
579
900
init_sqlite, NULL, scratch_pool));
581
*db = apr_palloc(result_pool, sizeof(**db));
583
#if SQLITE_VERSION_AT_LEAST(3,5,0)
587
if (mode == svn_sqlite__mode_readonly)
588
flags = SQLITE_OPEN_READONLY;
589
else if (mode == svn_sqlite__mode_readwrite)
590
flags = SQLITE_OPEN_READWRITE;
591
else if (mode == svn_sqlite__mode_rwcreate)
592
flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
594
SVN_ERR_MALFUNCTION();
596
/* If this flag is defined (3.6.x), then let's turn off SQLite's mutexes.
597
All svn objects are single-threaded, so we can already guarantee that
598
our use of the SQLite handle will be serialized properly.
599
Note: in 3.6.x, we've already config'd SQLite into MULTITHREAD mode,
600
so this is probably redundant... */
601
/* ### yeah. remove when autoconf magic is done for init/config. */
602
#ifdef SQLITE_OPEN_NOMUTEX
603
flags |= SQLITE_OPEN_NOMUTEX;
606
/* Open the database. Note that a handle is returned, even when an error
607
occurs (except for out-of-memory); thus, we can safely use it to
608
extract an error message and construct an svn_error_t. */
609
SQLITE_ERR(sqlite3_open_v2(path, &(*db)->db3, flags, NULL), *db);
612
/* Older versions of SQLite (pre-3.5.x) will always create the database
613
if it doesn't exist. So, if we are asked to be read-only or read-write,
614
we ensure the database already exists - if it doesn't, then we will
615
explicitly error out before asking SQLite to do anything.
617
Pre-3.5.x SQLite versions also don't support read-only ops either.
902
*db = apr_pcalloc(result_pool, sizeof(**db));
904
SVN_ERR(internal_open(&(*db)->db3, path, mode, scratch_pool));
907
sqlite3_trace((*db)->db3, sqlite_tracer, (*db)->db3);
909
#ifdef SQLITE3_PROFILE
910
sqlite3_profile((*db)->db3, sqlite_profiler, (*db)->db3);
913
/* Work around a bug in SQLite 3.7.7. The bug was fixed in SQLite 3.7.7.1.
917
Date: Sun, 26 Jun 2011 18:52:14 -0400
918
From: Richard Hipp <drh@sqlite.org>
919
To: General Discussion of SQLite Database <sqlite-users@sqlite.org>
920
Cc: dev@subversion.apache.org
921
Subject: Re: [sqlite] PRAGMA bug in 3.7.7 (but fine in 3.7.6.3)
922
Message-ID: <BANLkTimDypWGY-8tHFgJsTxN6ty6OkdJ0Q@mail.gmail.com>
619
if (mode == svn_sqlite__mode_readonly || mode == svn_sqlite__mode_readwrite)
621
svn_node_kind_t kind;
622
SVN_ERR(svn_io_check_path(path, &kind, scratch_pool));
623
if (kind != svn_node_file) {
624
return svn_error_createf(SVN_ERR_WC_CORRUPT, NULL,
625
_("Expected SQLite database not found: %s"),
626
svn_path_local_style(path, scratch_pool));
629
else if (mode == svn_sqlite__mode_rwcreate)
631
/* do nothing - older SQLite's will create automatically. */
634
SVN_ERR_MALFUNCTION();
636
SQLITE_ERR(sqlite3_open(path, &(*db)->db3), *db);
639
/* Retry until timeout when database is busy. */
640
SQLITE_ERR(sqlite3_busy_timeout((*db)->db3, BUSY_TIMEOUT), *db);
642
sqlite3_trace((*db)->db3, sqlite_tracer, (*db)->db3);
645
SVN_ERR(svn_sqlite__exec(*db, "PRAGMA case_sensitive_like=on;"));
925
int ignored_err = SQLITE_OK;
926
#if !SQLITE_VERSION_AT_LEAST(3,7,8) && defined(SQLITE_SCHEMA)
927
if (!strcmp(sqlite3_libversion(), "3.7.7"))
928
ignored_err = SQLITE_SCHEMA;
931
SVN_ERR(exec_sql2(*db, "PRAGMA case_sensitive_like=1;", ignored_err));
934
SVN_ERR(exec_sql(*db,
935
/* Disable synchronization to disable the explicit disk flushes
936
that make Sqlite up to 50 times slower; especially on small
939
This removes some stability guarantees on specific hardware
940
and power failures, but still guarantees atomic commits on
941
application crashes. With our dependency on external data
942
like pristine files (Wc) and revision files (repository),
943
we can't keep up these additional guarantees anyway.
945
### Maybe switch to NORMAL(1) when we use larger transaction
947
"PRAGMA synchronous=OFF;"
948
/* Enable recursive triggers so that a user trigger will fire
949
in the deletion phase of an INSERT OR REPLACE statement.
950
Requires SQLite >= 3.6.18 */
951
"PRAGMA recursive_triggers=ON;"));
953
#if SQLITE_VERSION_AT_LEAST(3,6,19) && defined(SVN_DEBUG)
954
/* When running in debug mode, enable the checking of foreign key
955
constraints. This has possible performance implications, so we don't
956
bother to do it for production...for now. */
957
SVN_ERR(exec_sql(*db, "PRAGMA foreign_keys=ON;"));
960
/* Store temporary tables in RAM instead of in temporary files, but don't
961
fail on this if this option is disabled in the sqlite compilation by
962
setting SQLITE_TEMP_STORE to 0 (always to disk) */
963
svn_error_clear(exec_sql(*db, "PRAGMA temp_store = MEMORY;"));
647
965
/* Validate the schema, upgrading if necessary. */
648
SVN_ERR(check_format(*db, latest_schema, upgrade_sql, scratch_pool));
966
if (upgrade_sql != NULL)
967
SVN_ERR(check_format(*db, latest_schema, upgrade_sql, scratch_pool));
650
969
/* Store the provided statements. */
664
983
(*db)->nbr_statements = 0;
666
(*db)->result_pool = result_pool;
985
(*db)->state_pool = result_pool;
667
986
apr_pool_cleanup_register(result_pool, *db, close_apr, apr_pool_cleanup_null);
669
988
return SVN_NO_ERROR;
992
svn_sqlite__close(svn_sqlite__db_t *db)
994
apr_status_t result = apr_pool_cleanup_run(db->state_pool, db, close_apr);
996
if (result == APR_SUCCESS)
999
return svn_error_wrap_apr(result, NULL);
1002
static svn_error_t *
1003
reset_all_statements(svn_sqlite__db_t *db,
1004
svn_error_t *error_to_wrap)
1009
/* ### Should we reorder the errors in this specific case
1010
### to avoid returning the normal error as top level error? */
1012
err = svn_error_compose_create(error_to_wrap,
1013
svn_error_create(SVN_ERR_SQLITE_RESETTING_FOR_ROLLBACK,
1016
for (i = 0; i < db->nbr_statements; i++)
1017
if (db->prepared_stmts[i] && db->prepared_stmts[i]->needs_reset)
1018
err = svn_error_compose_create(err,
1019
svn_sqlite__reset(db->prepared_stmts[i]));
1024
/* The body of svn_sqlite__with_transaction() and
1025
svn_sqlite__with_immediate_transaction(), which see. */
1026
static svn_error_t *
1027
with_transaction(svn_sqlite__db_t *db,
1028
svn_sqlite__transaction_callback_t cb_func,
1030
apr_pool_t *scratch_pool /* NULL allowed */)
1034
err = cb_func(cb_baton, db, scratch_pool);
1036
/* Commit or rollback the sqlite transaction. */
1039
svn_error_t *err2 = exec_sql(db, "ROLLBACK TRANSACTION;");
1041
if (err2 && err2->apr_err == SVN_ERR_SQLITE_BUSY)
1043
/* ### Houston, we have a problem!
1045
We are trying to rollback but we can't because some
1046
statements are still busy. This leaves the database
1047
unusable for future transactions as the current transaction
1050
As we are returning the actual error as the most relevant
1051
error in the chain, our caller might assume that it can
1052
retry/compensate on this error (e.g. SVN_WC_LOCKED), while
1053
in fact the SQLite database is unusable until the statements
1054
started within this transaction are reset and the transaction
1057
We try to compensate by resetting all prepared but unreset
1058
statements; but we leave the busy error in the chain anyway to
1059
help diagnosing the original error and help in finding where
1060
a reset statement is missing. */
1062
err2 = reset_all_statements(db, err2);
1063
err2 = svn_error_compose_create(
1064
exec_sql(db, "ROLLBACK TRANSACTION;"),
1068
return svn_error_compose_create(err,
1072
return svn_error_trace(exec_sql(db, "COMMIT TRANSACTION;"));
673
1076
svn_sqlite__with_transaction(svn_sqlite__db_t *db,
674
1077
svn_sqlite__transaction_callback_t cb_func,
679
SVN_ERR(svn_sqlite__transaction_begin(db));
680
err = cb_func(cb_baton, db);
682
/* Commit or rollback the sqlite transaction. */
685
svn_error_clear(svn_sqlite__transaction_rollback(db));
689
return svn_sqlite__transaction_commit(db);
1079
apr_pool_t *scratch_pool /* NULL allowed */)
1081
SVN_ERR(exec_sql(db, "BEGIN TRANSACTION;"));
1082
return svn_error_trace(with_transaction(db, cb_func, cb_baton,
1087
svn_sqlite__with_immediate_transaction(
1088
svn_sqlite__db_t *db,
1089
svn_sqlite__transaction_callback_t cb_func,
1091
apr_pool_t *scratch_pool /* NULL allowed */)
1093
SVN_ERR(exec_sql(db, "BEGIN IMMEDIATE TRANSACTION;"));
1094
return svn_error_trace(with_transaction(db, cb_func, cb_baton,
1099
svn_sqlite__with_lock(svn_sqlite__db_t *db,
1100
svn_sqlite__transaction_callback_t cb_func,
1102
apr_pool_t *scratch_pool)
1105
int savepoint = db->savepoint_nr++;
1106
/* This buffer is plenty big to hold the SAVEPOINT and RELEASE commands. */
1109
snprintf(buf, sizeof(buf), "SAVEPOINT s%u", savepoint);
1110
SVN_ERR(exec_sql(db, buf));
1111
err = cb_func(cb_baton, db, scratch_pool);
1117
snprintf(buf, sizeof(buf), "ROLLBACK TO s%u", savepoint);
1118
err2 = exec_sql(db, buf);
1120
if (err2 && err2->apr_err == SVN_ERR_SQLITE_BUSY)
1122
/* Ok, we have a major problem. Some statement is still open, which
1123
makes it impossible to release this savepoint.
1125
### See huge comment in svn_sqlite__with_transaction for
1128
err2 = reset_all_statements(db, err2);
1129
err2 = svn_error_compose_create(exec_sql(db, buf), err2);
1132
snprintf(buf, sizeof(buf), "RELEASE s%u", savepoint);
1133
err2 = svn_error_compose_create(exec_sql(db, buf), err2);
1135
return svn_error_trace(svn_error_compose_create(err, err2));
1138
snprintf(buf, sizeof(buf), "RELEASE s%u", savepoint);
1139
return svn_error_trace(exec_sql(db, buf));
1143
svn_sqlite__hotcopy(const char *src_path,
1144
const char *dst_path,
1145
apr_pool_t *scratch_pool)
1147
svn_sqlite__db_t *src_db;
1149
SVN_ERR(svn_sqlite__open(&src_db, src_path, svn_sqlite__mode_readonly,
1150
internal_statements, 0, NULL,
1151
scratch_pool, scratch_pool));
1154
svn_sqlite__db_t *dst_db;
1155
sqlite3_backup *backup;
1158
SVN_ERR(svn_sqlite__open(&dst_db, dst_path, svn_sqlite__mode_rwcreate,
1159
NULL, 0, NULL, scratch_pool, scratch_pool));
1160
backup = sqlite3_backup_init(dst_db->db3, "main", src_db->db3, "main");
1162
return svn_error_createf(SVN_ERR_SQLITE_ERROR, NULL,
1163
_("SQLite hotcopy failed for %s"), src_path);
1166
/* Pages are usually 1024 byte (SQLite docs). On my laptop
1167
copying gets faster as the number of pages is increased up
1168
to about 64, beyond that speed levels off. Lets put the
1169
number of pages an order of magnitude higher, this is still
1170
likely to be a fraction of large databases. */
1171
rc1 = sqlite3_backup_step(backup, 1024);
1173
/* Should we sleep on SQLITE_OK? That would make copying a
1174
large database take much longer. When we do sleep how,
1175
long should we sleep? Should the sleep get longer if we
1176
keep getting BUSY/LOCKED? I have no real reason for
1178
if (rc1 == SQLITE_BUSY || rc1 == SQLITE_LOCKED)
1181
while (rc1 == SQLITE_OK || rc1 == SQLITE_BUSY || rc1 == SQLITE_LOCKED);
1182
rc2 = sqlite3_backup_finish(backup);
1183
if (rc1 != SQLITE_DONE)
1184
SQLITE_ERR(rc1, dst_db);
1185
SQLITE_ERR(rc2, dst_db);
1186
SVN_ERR(svn_sqlite__close(dst_db));
1189
SVN_ERR(svn_sqlite__close(src_db));
1191
return SVN_NO_ERROR;
1194
struct function_wrapper_baton_t
1196
svn_sqlite__func_t func;
1199
apr_pool_t *scratch_pool;
1203
wrapped_func(sqlite3_context *context,
1205
sqlite3_value *values[])
1207
struct function_wrapper_baton_t *fwb = sqlite3_user_data(context);
1208
svn_sqlite__context_t sctx = { context };
1209
svn_sqlite__value_t **local_vals =
1210
apr_palloc(fwb->scratch_pool,
1211
sizeof(svn_sqlite__value_t *) * argc);
1215
for (i = 0; i < argc; i++)
1217
local_vals[i] = apr_palloc(fwb->scratch_pool, sizeof(*local_vals[i]));
1218
local_vals[i]->value = values[i];
1221
err = fwb->func(&sctx, argc, local_vals, fwb->scratch_pool);
1222
svn_pool_clear(fwb->scratch_pool);
1227
sqlite3_result_error(context,
1228
svn_err_best_message(err, buf, sizeof(buf)),
1230
svn_error_clear(err);
1235
svn_sqlite__create_scalar_function(svn_sqlite__db_t *db,
1236
const char *func_name,
1238
svn_sqlite__func_t func,
1241
struct function_wrapper_baton_t *fwb = apr_pcalloc(db->state_pool,
1244
fwb->scratch_pool = svn_pool_create(db->state_pool);
1248
SQLITE_ERR(sqlite3_create_function(db->db3, func_name, argc, SQLITE_ANY,
1249
fwb, wrapped_func, NULL, NULL),
1252
return SVN_NO_ERROR;
1256
svn_sqlite__value_type(svn_sqlite__value_t *val)
1258
return sqlite3_value_type(val->value);
1262
svn_sqlite__value_text(svn_sqlite__value_t *val)
1264
return (const char *) sqlite3_value_text(val->value);
1268
svn_sqlite__result_null(svn_sqlite__context_t *sctx)
1270
sqlite3_result_null(sctx->context);
1274
svn_sqlite__result_int64(svn_sqlite__context_t *sctx, apr_int64_t val)
1276
sqlite3_result_int64(sctx->context, val);