/ Check-in [7b2a65a6]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Better comments on the bindvtab.c implementation. All the two-argument version of the .set command. All bindings from .set and -D are still string.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | shell-bindings
Files: files | file ages | folders
SHA3-256:7b2a65a65475b80669af4ff5ac7adb6b310cd8275a36e672a3f5c88a259693c0
User & Date: drh 2018-04-27 20:49:13
Context
2018-04-27
20:49
Better comments on the bindvtab.c implementation. All the two-argument version of the .set command. All bindings from .set and -D are still string. Leaf check-in: 7b2a65a6 user: drh tags: shell-bindings
17:39
Add the ability to use bind parameters in the CLI. The new ".set KEY=VALUE" dot-command works to set bindings. Or use the "-Dkey=value" command-line option. Or use the built-in shell_bindings(k,v) virtual table to set, delete, or changing bindings. check-in: 1f2944d1 user: drh tags: shell-bindings
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to ext/misc/bindvtab.c.

10
11
12
13
14
15
16
































17
18
19
20
21
22
23
**
*************************************************************************
**
** This file implements a simple key/value store used to hold bind
** parameters for SQLite.  The key/value store is a singleton - there
** is exactly one per process.  The store can be accessed and controlled
** from SQLite using an eponymous virtual table.
































*/
#if !defined(SQLITEINT_H)
#include "sqlite3ext.h"
#endif
SQLITE_EXTENSION_INIT1
#include <string.h>
#include <assert.h>







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
**
*************************************************************************
**
** This file implements a simple key/value store used to hold bind
** parameters for SQLite.  The key/value store is a singleton - there
** is exactly one per process.  The store can be accessed and controlled
** from SQLite using an eponymous virtual table.
**
** This is used to do parameter binding in the command-line shell.
**
** The ".set key=value" command and the "-Dkey=value" command-line option
** invoke shell_bindings_new_text() on the argument ("key=value") in order
** to create entries in the store.  The CLI then invokes
** shell_bindings_apply() on each prepared statement just prior to
** running it.
**
** All bindings are accessible through an eponymous-only virtual table
** named shell_bindings.  Ex:
**
**    INSERT INTO shell_bindings(k,v) VALUES('p1',12345);
**    SELECT $p1, typeof($p1);
**
** The above results in an answer of 12345,'integer'.  Bindings generated
** using the virtual table can have any type other than NULL.  But bindings
** generated by the .set command and the -D command-line option are always
** text.
**
** The CLI is single-threaded, so there is no attempt to make this code
** threadsafe.
**
** The key/value store is kept in a global list, and uses malloc/free rather
** than sqlite3_malloc64/sqlite3_free so that it can be completely independent
** of SQLite, can exist both before sqlite3_initialize() and after
** sqlite3_shutdown(), and so that it will persist across multiple
** connections created using ".open".
**
** The number of parameters is expected to be small, so they are stored
** on a simple linked list.  If this proves to be too inefficient, some other
** algorithm can be substituted in the future without changing the interface.
*/
#if !defined(SQLITEINT_H)
#include "sqlite3ext.h"
#endif
SQLITE_EXTENSION_INIT1
#include <string.h>
#include <assert.h>

Changes to src/shell.c.in.

7150
7151
7152
7153
7154
7155
7156


7157




7158



7159
7160
7161
7162
7163
7164
7165

7166
7167
7168
7169
7170
7171
7172
      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
    }
  }else

  if( c=='s' && n==3 && strncmp(azArg[0],"set",3)==0 ){
    int x;


    if( nArg<2 ){




      raw_printf(stderr, "Usage: .set KEY=VALUE\n");



      rc = 1;
      goto meta_command_exit;
    }
    x = shell_bindings_new_text(azArg[1]);
    if( x ){
      utf8_printf(stderr, "Error: bad setting: %s\n", azArg[1]);
    }

  }else


  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
    int i;                   /* Loop counter */
    int bSchema = 0;         /* Also hash the schema */







>
>
|
>
>
>
>
|
>
>
>



|

|

>







7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
    }
  }else

  if( c=='s' && n==3 && strncmp(azArg[0],"set",3)==0 ){
    int x;
    char *zKey = 0;
    char *zToFree = 0;
    if( nArg==2 ){
      zKey = azArg[1];
    }else if( nArg==3 ){
      zKey = zToFree = sqlite3_mprintf("%s=%s",azArg[1],azArg[2]);
    }else{
      raw_printf(stderr,
         "Usage: .set KEY VALUE\n   Or: .set KEY=VALUE\n"
         "Use SQL on the \"shell_bindings\" table to query or delete keys.\n"
      );
      rc = 1;
      goto meta_command_exit;
    }
    x = shell_bindings_new_text(zKey);
    if( x ){
      utf8_printf(stderr, "Error: bad setting: %s\n", zKey);
    }
    sqlite3_free(zToFree);
  }else


  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
    int i;                   /* Loop counter */
    int bSchema = 0;         /* Also hash the schema */