Skip to content

Commit 2bedf8d

Browse files
authored
Update SQLite to version 3.52.0 (#90)
1 parent cd04ca2 commit 2bedf8d

7 files changed

Lines changed: 5312 additions & 3583 deletions

File tree

Sources/CSQLite/csqlite_shims.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ int csqlite_sqlite3_db_config_enable_comments(sqlite3 *db, int x, int *y)
269269
return sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_COMMENTS, x, y);
270270
}
271271

272+
int csqlite_sqlite3_db_config_fp_digits(sqlite3 *db, int x, int *y)
273+
{
274+
return sqlite3_db_config(db, SQLITE_DBCONFIG_FP_DIGITS, x, y);
275+
}
276+
272277
// MARK: - Virtual table configuration
273278

274279
int csqlite_sqlite3_vtab_config_constraint_support(sqlite3 *db, int x)

Sources/CSQLite/decimal.c

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,36 @@ static void decimal_result(sqlite3_context *pCtx, Decimal *p){
291291
sqlite3_result_text(pCtx, z, i, sqlite3_free);
292292
}
293293

294+
/*
295+
** Round a decimal value to N significant digits. N must be positive.
296+
*/
297+
static void decimal_round(Decimal *p, int N){
298+
int i;
299+
int nZero;
300+
if( N<1 ) return;
301+
for(nZero=0; nZero<p->nDigit && p->a[nZero]==0; nZero++){}
302+
N += nZero;
303+
if( p->nDigit<=N ) return;
304+
if( p->a[N]>4 ){
305+
p->a[N-1]++;
306+
for(i=N-1; i>0 && p->a[i]>9; i--){
307+
p->a[i] = 0;
308+
p->a[i-1]++;
309+
}
310+
if( p->a[0]>9 ){
311+
p->a[0] = 1;
312+
p->nFrac--;
313+
}
314+
}
315+
memset(&p->a[N], 0, p->nDigit - N);
316+
}
317+
294318
/*
295319
** Make the given Decimal the result in an format similar to '%+#e'.
296320
** In other words, show exponential notation with leading and trailing
297321
** zeros omitted.
298322
*/
299-
static void decimal_result_sci(sqlite3_context *pCtx, Decimal *p){
323+
static void decimal_result_sci(sqlite3_context *pCtx, Decimal *p, int N){
300324
char *z; /* The output buffer */
301325
int i; /* Loop counter */
302326
int nZero; /* Number of leading zeros */
@@ -314,7 +338,8 @@ static void decimal_result_sci(sqlite3_context *pCtx, Decimal *p){
314338
sqlite3_result_null(pCtx);
315339
return;
316340
}
317-
for(nDigit=p->nDigit; nDigit>0 && p->a[nDigit-1]==0; nDigit--){}
341+
if( N<1 ) N = 0;
342+
for(nDigit=p->nDigit; nDigit>N && p->a[nDigit-1]==0; nDigit--){}
318343
for(nZero=0; nZero<nDigit && p->a[nZero]==0; nZero++){}
319344
nFrac = p->nFrac + (nDigit - p->nDigit);
320345
nDigit -= nZero;
@@ -677,10 +702,16 @@ static void decimalFunc(
677702
sqlite3_value **argv
678703
){
679704
Decimal *p = decimal_new(context, argv[0], 0);
680-
UNUSED_PARAMETER(argc);
705+
int N;
706+
if( argc==2 ){
707+
N = sqlite3_value_int(argv[1]);
708+
if( N>0 ) decimal_round(p, N);
709+
}else{
710+
N = 0;
711+
}
681712
if( p ){
682713
if( sqlite3_user_data(context)!=0 ){
683-
decimal_result_sci(context, p);
714+
decimal_result_sci(context, p, N);
684715
}else{
685716
decimal_result(context, p);
686717
}
@@ -850,7 +881,7 @@ static void decimalPow2Func(
850881
UNUSED_PARAMETER(argc);
851882
if( sqlite3_value_type(argv[0])==SQLITE_INTEGER ){
852883
Decimal *pA = decimalPow2(sqlite3_value_int(argv[0]));
853-
decimal_result_sci(context, pA);
884+
decimal_result_sci(context, pA, 0);
854885
decimal_free(pA);
855886
}
856887
}
@@ -871,7 +902,9 @@ int sqlite3_decimal_init(
871902
void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
872903
} aFunc[] = {
873904
{ "decimal", 1, 0, decimalFunc },
905+
{ "decimal", 2, 0, decimalFunc },
874906
{ "decimal_exp", 1, 1, decimalFunc },
907+
{ "decimal_exp", 2, 1, decimalFunc },
875908
{ "decimal_cmp", 2, 0, decimalCmpFunc },
876909
{ "decimal_add", 2, 0, decimalAddFunc },
877910
{ "decimal_sub", 2, 0, decimalSubFunc },

Sources/CSQLite/ieee754.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ static void ieee754func(
179179
}
180180

181181
if( m<0 ){
182+
if( m<(-9223372036854775807LL) ) return;
182183
isNeg = 1;
183184
m = -m;
184-
if( m<0 ) return;
185185
}else if( m==0 && e>-1000 && e<1000 ){
186186
sqlite3_result_double(context, 0.0);
187187
return;
@@ -259,6 +259,38 @@ static void ieee754func_to_blob(
259259
}
260260
}
261261

262+
/*
263+
** Functions to convert between 64-bit integers and floats.
264+
**
265+
** The bit patterns are copied. The numeric values are different.
266+
*/
267+
static void ieee754func_from_int(
268+
sqlite3_context *context,
269+
int argc,
270+
sqlite3_value **argv
271+
){
272+
UNUSED_PARAMETER(argc);
273+
if( sqlite3_value_type(argv[0])==SQLITE_INTEGER ){
274+
double r;
275+
sqlite3_int64 v = sqlite3_value_int64(argv[0]);
276+
memcpy(&r, &v, sizeof(r));
277+
sqlite3_result_double(context, r);
278+
}
279+
}
280+
static void ieee754func_to_int(
281+
sqlite3_context *context,
282+
int argc,
283+
sqlite3_value **argv
284+
){
285+
UNUSED_PARAMETER(argc);
286+
if( sqlite3_value_type(argv[0])==SQLITE_FLOAT ){
287+
double r = sqlite3_value_double(argv[0]);
288+
sqlite3_uint64 v;
289+
memcpy(&v, &r, sizeof(v));
290+
sqlite3_result_int64(context, v);
291+
}
292+
}
293+
262294
/*
263295
** SQL Function: ieee754_inc(r,N)
264296
**
@@ -311,6 +343,8 @@ int sqlite3_ieee_init(
311343
{ "ieee754_exponent", 1, 2, ieee754func },
312344
{ "ieee754_to_blob", 1, 0, ieee754func_to_blob },
313345
{ "ieee754_from_blob", 1, 0, ieee754func_from_blob },
346+
{ "ieee754_to_int", 1, 0, ieee754func_to_int },
347+
{ "ieee754_from_int", 1, 0, ieee754func_from_int },
314348
{ "ieee754_inc", 2, 0, ieee754inc },
315349
};
316350
unsigned int i;

Sources/CSQLite/include/csqlite_shims.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ int csqlite_sqlite3_db_config_enable_attach_create(sqlite3 *db, int x, int *y);
127127
int csqlite_sqlite3_db_config_enable_attach_write(sqlite3 *db, int x, int *y);
128128
/// Equivalent to `sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_COMMENTS, x, y)`
129129
int csqlite_sqlite3_db_config_enable_comments(sqlite3 *db, int x, int *y);
130+
/// Equivalent to `sqlite3_db_config(db, SQLITE_DBCONFIG_FP_DIGITS, x, y)`
131+
int csqlite_sqlite3_db_config_fp_digits(sqlite3 *db, int x, int *y);
130132

131133
// MARK: - Virtual table configuration
132134
// See https://sqlite.org/c3ref/vtab_config.html

0 commit comments

Comments
 (0)