diff options
author | drh <> | 2023-06-29 12:14:10 +0000 |
---|---|---|
committer | drh <> | 2023-06-29 12:14:10 +0000 |
commit | 7f5fe1faf3d8e1c339cf66f4e2835f42d96d9606 (patch) | |
tree | 3733841a379e84bd608952fa7180cef9bbe4f632 /src | |
parent | 8fbb335d9f73dfa61b6da3f99e3408435297b850 (diff) | |
download | sqlite-7f5fe1faf3d8e1c339cf66f4e2835f42d96d9606.tar.gz sqlite-7f5fe1faf3d8e1c339cf66f4e2835f42d96d9606.zip |
CLI enhancements to facilitate SQLite core testing:
(1) Add built-in functions strtod() and dtostr() that convert text to
floating point and back using C-library routines.
(2) Do not disable all of ".testctrl" without --unsafe-testing, but only
those subcommands of .testctrl that are actually dangerous.
FossilOrigin-Name: 669996a8ddcbf35f3de66cf466508fc1e6dd09ab269aba395ac86a11b2ec238c
Diffstat (limited to 'src')
-rw-r--r-- | src/shell.c.in | 55 |
1 files changed, 43 insertions, 12 deletions
diff --git a/src/shell.c.in b/src/shell.c.in index dfc09a28a..9bad1232e 100644 --- a/src/shell.c.in +++ b/src/shell.c.in @@ -1208,6 +1208,42 @@ static char *shellFakeSchema( } /* +** SQL function: strtod(X) +** +** Use the C-library strtod() function to convert string X into a double. +** Used for comparing the accuracy of SQLite's internal text-to-float conversion +** routines against the C-library. +*/ +static void shellStrtod( + sqlite3_context *pCtx, + int nVal, + sqlite3_value **apVal +){ + char *z = (char*)sqlite3_value_text(apVal[0]); + if( z==0 ) return; + sqlite3_result_double(pCtx, strtod(z,0)); +} + +/* +** SQL function: dtostr(X) +** +** Use the C-library printf() function to convert real value X into a string. +** Used for comparing the accuracy of SQLite's internal float-to-text conversion +** routines against the C-library. +*/ +static void shellDtostr( + sqlite3_context *pCtx, + int nVal, + sqlite3_value **apVal +){ + double r = sqlite3_value_double(apVal[0]); + char z[200]; + sprintf(z, "%#+.70e", r); + sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); +} + + +/* ** SQL function: shell_module_schema(X) ** ** Return a fake schema for the table-valued function or eponymous virtual @@ -5463,6 +5499,10 @@ static void open_db(ShellState *p, int openFlags){ } #endif + sqlite3_create_function(p->db, "strtod", 1, SQLITE_UTF8, 0, + shellStrtod, 0, 0); + sqlite3_create_function(p->db, "dtostr", 1, SQLITE_UTF8, 0, + shellDtostr, 0, 0); sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0, shellAddSchemaName, 0, 0); sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0, @@ -10805,7 +10845,7 @@ static int do_meta_command(char *zLine, ShellState *p){ static const struct { const char *zCtrlName; /* Name of a test-control option */ int ctrlCode; /* Integer code for that option */ - int unSafe; /* Not valid for --safe mode */ + int unSafe; /* Not valid unless --unsafe-testing */ const char *zUsage; /* Usage notes */ } aCtrl[] = { {"always", SQLITE_TESTCTRL_ALWAYS, 1, "BOOLEAN" }, @@ -10838,12 +10878,6 @@ static int do_meta_command(char *zLine, ShellState *p){ int i, n2; const char *zCmd = 0; - if( !ShellHasFlag(p,SHFLG_TestingMode) ){ - utf8_printf(stderr, ".%s unavailable without --unsafe-testing\n", - "testctrl"); - rc = 1; - goto meta_command_exit; - } open_db(p, 0); zCmd = nArg>=2 ? azArg[1] : "help"; @@ -10857,6 +10891,7 @@ static int do_meta_command(char *zLine, ShellState *p){ if( cli_strcmp(zCmd,"help")==0 ){ utf8_printf(p->out, "Available test-controls:\n"); for(i=0; i<ArraySize(aCtrl); i++){ + if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue; utf8_printf(p->out, " .testctrl %s %s\n", aCtrl[i].zCtrlName, aCtrl[i].zUsage); } @@ -10868,6 +10903,7 @@ static int do_meta_command(char *zLine, ShellState *p){ ** of the option name, or a numerical value. */ n2 = strlen30(zCmd); for(i=0; i<ArraySize(aCtrl); i++){ + if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue; if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){ if( testctrl<0 ){ testctrl = aCtrl[i].ctrlCode; @@ -10883,11 +10919,6 @@ static int do_meta_command(char *zLine, ShellState *p){ if( testctrl<0 ){ utf8_printf(stderr,"Error: unknown test-control: %s\n" "Use \".testctrl --help\" for help\n", zCmd); - }else if( aCtrl[iCtrl].unSafe && p->bSafeMode ){ - utf8_printf(stderr, - "line %d: \".testctrl %s\" may not be used in safe mode\n", - p->lineno, aCtrl[iCtrl].zCtrlName); - exit(1); }else{ switch(testctrl){ |