Hex Artifact Content
Not logged in

Artifact 984f70bf7212af97ee827afe36726c76bf64b79e:

File src/db.c part of check-in [f46fe42d6d] - Store private ticket fields (ex: the originators email address) as their SHA1 hash so that malefactors cannot read them. Add the new "concealed" table to the repository database and store mappings from SHA1 hashes back to email addresses in that table. Ticket a24ec6005f. Note: run "rebuild" on repositories after updating to this version of fossil in order to create the "concealed" table. Need to add the ability to manage the concealed table from the web interface and the ability to sync concealed content between trusted repositories. by drh on 2008-07-24 02:04:36.

0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20  /*.** Copyright 
0010: 28 63 29 20 32 30 30 36 20 44 2e 20 52 69 63 68  (c) 2006 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 0a 2a 2a 20 54  ard Hipp.**.** T
0030: 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 20 66  his program is f
0040: 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 79 6f  ree software; yo
0050: 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 75  u can redistribu
0060: 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a 2a 2a 20  te it and/or.** 
0070: 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 65 72 20  modify it under 
0080: 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65  the terms of the
0090: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62   GNU General Pub
00a0: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 76  lic.** License v
00b0: 65 72 73 69 6f 6e 20 32 20 61 73 20 70 75 62 6c  ersion 2 as publ
00c0: 69 73 68 65 64 20 62 79 20 74 68 65 20 46 72 65  ished by the Fre
00d0: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
00e0: 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ation..**.** Thi
00f0: 73 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73  s program is dis
0100: 74 72 69 62 75 74 65 64 20 69 6e 20 74 68 65 20  tributed in the 
0110: 68 6f 70 65 20 74 68 61 74 20 69 74 20 77 69 6c  hope that it wil
0120: 6c 20 62 65 20 75 73 65 66 75 6c 2c 0a 2a 2a 20  l be useful,.** 
0130: 62 75 74 20 57 49 54 48 4f 55 54 20 41 4e 59 20  but WITHOUT ANY 
0140: 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75  WARRANTY; withou
0150: 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69  t even the impli
0160: 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 0a 2a  ed warranty of.*
0170: 2a 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54  * MERCHANTABILIT
0180: 59 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52  Y or FITNESS FOR
0190: 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55   A PARTICULAR PU
01a0: 52 50 4f 53 45 2e 20 20 53 65 65 20 74 68 65 20  RPOSE.  See the 
01b0: 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 61 6c 20 50  GNU.** General P
01c0: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f  ublic License fo
01d0: 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a  r more details..
01e0: 2a 2a 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c 64  **.** You should
01f0: 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20 61   have received a
0200: 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e 55   copy of the GNU
0210: 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63 0a   General Public.
0220: 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e 67  ** License along
0230: 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72 61   with this libra
0240: 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69 74  ry; if not, writ
0250: 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65 65  e to the.** Free
0260: 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 61   Software Founda
0270: 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20 54  tion, Inc., 59 T
0280: 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53 75  emple Place - Su
0290: 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73 74  ite 330,.** Bost
02a0: 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31 33  on, MA  02111-13
02b0: 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20 41  07, USA..**.** A
02c0: 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69 6e  uthor contact in
02d0: 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20 20  formation:.**   
02e0: 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a 2a  drh@hwaci.com.**
02f0: 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68 77     http://www.hw
0300: 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a 0a  aci.com/drh/.**.
0310: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
0360: 2a 2a 0a 2a 2a 20 43 6f 64 65 20 66 6f 72 20 69  **.** Code for i
0370: 6e 74 65 72 66 61 63 69 6e 67 20 74 6f 20 74 68  nterfacing to th
0380: 65 20 76 61 72 69 6f 75 73 20 64 61 74 61 62 61  e various databa
0390: 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  ses..**.** There
03a0: 20 61 72 65 20 74 68 72 65 65 20 73 65 70 61 72   are three separ
03b0: 61 74 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  ate database fil
03c0: 65 73 20 74 68 61 74 20 66 6f 73 73 69 6c 20 69  es that fossil i
03d0: 6e 74 65 72 61 63 74 73 0a 2a 2a 20 77 69 74 68  nteracts.** with
03e0: 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20 20  :.**.**    (1)  
03f0: 54 68 65 20 22 75 73 65 72 22 20 64 61 74 61 62  The "user" datab
0400: 61 73 65 20 69 6e 20 7e 2f 2e 66 6f 73 73 69 6c  ase in ~/.fossil
0410: 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 20 54  .**.**    (2)  T
0420: 68 65 20 22 72 65 70 6f 73 69 74 6f 72 79 22 20  he "repository" 
0430: 64 61 74 61 62 61 73 65 0a 2a 2a 0a 2a 2a 20 20  database.**.**  
0440: 20 20 28 33 29 20 20 41 20 6c 6f 63 61 6c 20 63    (3)  A local c
0450: 68 65 63 6b 6f 75 74 20 64 61 74 61 62 61 73 65  heckout database
0460: 20 6e 61 6d 65 64 20 22 46 4f 53 53 49 4c 22 20   named "FOSSIL" 
0470: 61 6e 64 20 6c 6f 63 61 74 65 64 20 61 74 20 74  and located at t
0480: 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 72 6f  he.**         ro
0490: 6f 74 20 6f 66 20 74 68 65 20 6c 6f 63 61 6c 20  ot of the local 
04a0: 63 6f 70 79 20 6f 66 20 74 68 65 20 73 6f 75 72  copy of the sour
04b0: 63 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2f 0a 23  ce tree..**.*/.#
04c0: 69 6e 63 6c 75 64 65 20 22 63 6f 6e 66 69 67 2e  include "config.
04d0: 68 22 0a 23 69 66 6e 64 65 66 20 5f 5f 4d 49 4e  h".#ifndef __MIN
04e0: 47 57 33 32 5f 5f 0a 23 20 20 69 6e 63 6c 75 64  GW32__.#  includ
04f0: 65 20 3c 70 77 64 2e 68 3e 0a 23 65 6e 64 69 66  e <pwd.h>.#endif
0500: 0a 23 69 6e 63 6c 75 64 65 20 3c 73 71 6c 69 74  .#include <sqlit
0510: 65 33 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c  e3.h>.#include <
0520: 73 79 73 2f 74 79 70 65 73 2e 68 3e 0a 23 69 6e  sys/types.h>.#in
0530: 63 6c 75 64 65 20 3c 73 79 73 2f 73 74 61 74 2e  clude <sys/stat.
0540: 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69  h>.#include <uni
0550: 73 74 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20  std.h>.#include 
0560: 22 64 62 2e 68 22 0a 0a 23 69 66 20 49 4e 54 45  "db.h"..#if INTE
0570: 52 46 41 43 45 0a 2f 2a 0a 2a 2a 20 41 6e 20 73  RFACE./*.** An s
0580: 69 6e 67 6c 65 20 53 51 4c 20 73 74 61 74 65 6d  ingle SQL statem
0590: 65 6e 74 20 69 73 20 72 65 70 72 65 73 65 6e 74  ent is represent
05a0: 65 64 20 61 73 20 61 6e 20 69 6e 73 74 61 6e 63  ed as an instanc
05b0: 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
05c0: 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e  ng.** structure.
05d0: 0a 2a 2f 0a 73 74 72 75 63 74 20 53 74 6d 74 20  .*/.struct Stmt 
05e0: 7b 0a 20 20 42 6c 6f 62 20 73 71 6c 3b 20 20 20  {.  Blob sql;   
05f0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
0600: 68 65 20 53 51 4c 20 66 6f 72 20 74 68 69 73 20  he SQL for this 
0610: 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 73  statement */.  s
0620: 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
0630: 6d 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 72 65  mt;    /* The re
0640: 73 75 6c 74 73 20 6f 66 20 73 71 6c 69 74 65 33  sults of sqlite3
0650: 5f 70 72 65 70 61 72 65 28 29 20 2a 2f 0a 20 20  _prepare() */.  
0660: 53 74 6d 74 20 2a 70 4e 65 78 74 2c 20 2a 70 50  Stmt *pNext, *pP
0670: 72 65 76 3b 20 20 20 20 2f 2a 20 4c 69 73 74 20  rev;    /* List 
0680: 6f 66 20 61 6c 6c 20 75 6e 66 69 6e 61 6c 69 7a  of all unfinaliz
0690: 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f  ed statements */
06a0: 0a 7d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 49 4e  .};.#endif /* IN
06b0: 54 45 52 46 41 43 45 20 2a 2f 0a 0a 2f 2a 0a 2a  TERFACE */../*.*
06c0: 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74  * Call this rout
06d0: 69 6e 65 20 77 68 65 6e 20 61 20 64 61 74 61 62  ine when a datab
06e0: 61 73 65 20 65 72 72 6f 72 20 6f 63 63 75 72 73  ase error occurs
06f0: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
0700: 20 64 62 5f 65 72 72 28 63 6f 6e 73 74 20 63 68   db_err(const ch
0710: 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e  ar *zFormat, ...
0720: 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b  ){.  va_list ap;
0730: 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61  .  char *z;.  va
0740: 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d  _start(ap, zForm
0750: 61 74 29 3b 0a 20 20 7a 20 3d 20 76 6d 70 72 69  at);.  z = vmpri
0760: 6e 74 66 28 7a 46 6f 72 6d 61 74 2c 20 61 70 29  ntf(zFormat, ap)
0770: 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a  ;.  va_end(ap);.
0780: 20 20 69 66 28 20 67 2e 63 67 69 50 61 6e 69 63    if( g.cgiPanic
0790: 20 29 7b 0a 20 20 20 20 67 2e 63 67 69 50 61 6e   ){.    g.cgiPan
07a0: 69 63 20 3d 20 30 3b 0a 20 20 20 20 63 67 69 5f  ic = 0;.    cgi_
07b0: 70 72 69 6e 74 66 28 22 3c 68 31 3e 44 61 74 61  printf("<h1>Data
07c0: 62 61 73 65 20 45 72 72 6f 72 3c 2f 68 31 3e 5c  base Error</h1>\
07d0: 6e 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n".             
07e0: 20 20 22 3c 70 72 65 3e 25 68 3c 2f 70 72 65 3e    "<pre>%h</pre>
07f0: 22 2c 20 7a 29 3b 0a 20 20 20 20 63 67 69 5f 72  ", z);.    cgi_r
0800: 65 70 6c 79 28 29 3b 0a 20 20 7d 65 6c 73 65 7b  eply();.  }else{
0810: 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64  .    fprintf(std
0820: 65 72 72 2c 20 22 25 73 3a 20 25 73 5c 6e 22 2c  err, "%s: %s\n",
0830: 20 67 2e 61 72 67 76 5b 30 5d 2c 20 7a 29 3b 0a   g.argv[0], z);.
0840: 20 20 7d 0a 20 20 64 62 5f 66 6f 72 63 65 5f 72    }.  db_force_r
0850: 6f 6c 6c 62 61 63 6b 28 29 3b 0a 20 20 65 78 69  ollback();.  exi
0860: 74 28 31 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  t(1);.}..static 
0870: 69 6e 74 20 6e 42 65 67 69 6e 20 3d 20 30 3b 20  int nBegin = 0; 
0880: 20 20 20 20 20 2f 2a 20 4e 65 73 74 69 6e 67 20       /* Nesting 
0890: 64 65 70 74 68 20 6f 66 20 42 45 47 49 4e 20 2a  depth of BEGIN *
08a0: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 4e  /.static int isN
08b0: 65 77 52 65 70 6f 20 3d 20 30 3b 20 20 20 2f 2a  ewRepo = 0;   /*
08c0: 20 54 72 75 65 20 69 66 20 74 68 65 20 72 65 70   True if the rep
08d0: 6f 73 69 74 6f 72 79 20 69 73 20 6e 65 77 6c 79  ository is newly
08e0: 20 63 72 65 61 74 65 64 20 2a 2f 0a 73 74 61 74   created */.stat
08f0: 69 63 20 69 6e 74 20 64 6f 52 6f 6c 6c 62 61 63  ic int doRollbac
0900: 6b 20 3d 20 30 3b 20 20 2f 2a 20 54 72 75 65 20  k = 0;  /* True 
0910: 74 6f 20 66 6f 72 63 65 20 61 20 72 6f 6c 6c 62  to force a rollb
0920: 61 63 6b 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ack */.static in
0930: 74 20 6e 43 6f 6d 6d 69 74 48 6f 6f 6b 20 3d 20  t nCommitHook = 
0940: 30 3b 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20  0; /* Number of 
0950: 63 6f 6d 6d 69 74 20 68 6f 6f 6b 73 20 2a 2f 0a  commit hooks */.
0960: 73 74 61 74 69 63 20 73 74 72 75 63 74 20 73 43  static struct sC
0970: 6f 6d 6d 69 74 48 6f 6f 6b 20 7b 0a 20 20 69 6e  ommitHook {.  in
0980: 74 20 28 2a 78 48 6f 6f 6b 29 28 76 6f 69 64 29  t (*xHook)(void)
0990: 3b 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20  ;  /* Functions 
09a0: 74 6f 20 63 61 6c 6c 20 61 74 20 64 62 5f 65 6e  to call at db_en
09b0: 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29 20  d_transaction() 
09c0: 2a 2f 0a 20 20 69 6e 74 20 73 65 71 75 65 6e 63  */.  int sequenc
09d0: 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6c  e;        /* Cal
09e0: 6c 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 73  l functions in s
09f0: 65 71 75 65 6e 63 65 20 6f 72 64 65 72 20 2a 2f  equence order */
0a00: 0a 7d 20 61 48 6f 6f 6b 5b 35 5d 3b 0a 73 74 61  .} aHook[5];.sta
0a10: 74 69 63 20 53 74 6d 74 20 2a 70 41 6c 6c 53 74  tic Stmt *pAllSt
0a20: 6d 74 20 3d 20 30 3b 20 20 2f 2a 20 4c 69 73 74  mt = 0;  /* List
0a30: 20 6f 66 20 61 6c 6c 20 75 6e 66 69 6e 61 6c 69   of all unfinali
0a40: 7a 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 2a  zed statements *
0a50: 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  /../*.** This ro
0a60: 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
0a70: 62 79 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f  by the SQLite co
0a80: 6d 6d 69 74 2d 68 6f 6f 6b 20 6d 65 63 68 61 6e  mmit-hook mechan
0a90: 69 73 6d 0a 2a 2a 20 6a 75 73 74 20 70 72 69 6f  ism.** just prio
0aa0: 72 20 74 6f 20 65 61 63 68 20 6f 6d 69 74 2e 20  r to each omit. 
0ab0: 20 41 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e   All this routin
0ac0: 65 20 64 6f 65 73 20 69 73 20 76 65 72 69 66 79  e does is verify
0ad0: 0a 2a 2a 20 74 68 61 74 20 6e 42 65 67 69 6e 20  .** that nBegin 
0ae0: 72 65 61 6c 6c 79 20 69 73 20 7a 65 72 6f 2e 20  really is zero. 
0af0: 20 54 68 61 74 20 69 6e 73 75 72 65 73 20 74 68   That insures th
0b00: 61 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 0a  at transactions.
0b10: 2a 2a 20 63 61 6e 6e 6f 74 20 63 6f 6d 6d 69 74  ** cannot commit
0b20: 20 62 79 20 61 6e 79 20 6d 65 61 6e 73 20 6f 74   by any means ot
0b30: 68 65 72 20 74 68 61 6e 20 62 79 20 63 61 6c 6c  her than by call
0b40: 69 6e 67 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73  ing db_end_trans
0b50: 61 63 74 69 6f 6e 28 29 0a 2a 2a 20 62 65 6c 6f  action().** belo
0b60: 77 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73  w..**.** This is
0b70: 20 6a 75 73 74 20 61 20 73 61 66 65 74 79 20 61   just a safety a
0b80: 6e 64 20 73 61 6e 69 74 79 20 63 68 65 63 6b 2e  nd sanity check.
0b90: 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .*/.static int d
0ba0: 62 5f 76 65 72 69 66 79 5f 61 74 5f 63 6f 6d 6d  b_verify_at_comm
0bb0: 69 74 28 76 6f 69 64 20 2a 6e 6f 74 55 73 65 64  it(void *notUsed
0bc0: 29 7b 0a 20 20 69 66 28 20 6e 42 65 67 69 6e 20  ){.  if( nBegin 
0bd0: 29 7b 0a 20 20 20 20 66 6f 73 73 69 6c 5f 70 61  ){.    fossil_pa
0be0: 6e 69 63 28 22 69 6c 6c 65 67 61 6c 20 63 6f 6d  nic("illegal com
0bf0: 6d 69 74 20 61 74 74 65 6d 70 74 22 29 3b 0a 20  mit attempt");. 
0c00: 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
0c10: 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
0c20: 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 6e 64 20  /*.** Begin and 
0c30: 65 6e 64 20 61 20 6e 65 73 74 65 64 20 74 72 61  end a nested tra
0c40: 6e 73 61 63 74 69 6f 6e 0a 2a 2f 0a 76 6f 69 64  nsaction.*/.void
0c50: 20 64 62 5f 62 65 67 69 6e 5f 74 72 61 6e 73 61   db_begin_transa
0c60: 63 74 69 6f 6e 28 76 6f 69 64 29 7b 0a 20 20 69  ction(void){.  i
0c70: 66 28 20 6e 42 65 67 69 6e 3d 3d 30 20 29 7b 0a  f( nBegin==0 ){.
0c80: 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65      db_multi_exe
0c90: 63 28 22 42 45 47 49 4e 22 29 3b 0a 20 20 20 20  c("BEGIN");.    
0ca0: 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68  sqlite3_commit_h
0cb0: 6f 6f 6b 28 67 2e 64 62 2c 20 64 62 5f 76 65 72  ook(g.db, db_ver
0cc0: 69 66 79 5f 61 74 5f 63 6f 6d 6d 69 74 2c 20 30  ify_at_commit, 0
0cd0: 29 3b 0a 20 20 7d 0a 20 20 6e 42 65 67 69 6e 2b  );.  }.  nBegin+
0ce0: 2b 3b 0a 7d 0a 76 6f 69 64 20 64 62 5f 65 6e 64  +;.}.void db_end
0cf0: 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 69 6e 74  _transaction(int
0d00: 20 72 6f 6c 6c 62 61 63 6b 46 6c 61 67 29 7b 0a   rollbackFlag){.
0d10: 20 20 69 66 28 20 6e 42 65 67 69 6e 3c 3d 30 20    if( nBegin<=0 
0d20: 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
0d30: 72 6f 6c 6c 62 61 63 6b 46 6c 61 67 20 29 20 64  rollbackFlag ) d
0d40: 6f 52 6f 6c 6c 62 61 63 6b 20 3d 20 31 3b 0a 20  oRollback = 1;. 
0d50: 20 6e 42 65 67 69 6e 2d 2d 3b 0a 20 20 69 66 28   nBegin--;.  if(
0d60: 20 6e 42 65 67 69 6e 3d 3d 30 20 29 7b 0a 20 20   nBegin==0 ){.  
0d70: 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72    int i;.    for
0d80: 28 69 3d 30 3b 20 64 6f 52 6f 6c 6c 62 61 63 6b  (i=0; doRollback
0d90: 3d 3d 30 20 26 26 20 69 3c 6e 43 6f 6d 6d 69 74  ==0 && i<nCommit
0da0: 48 6f 6f 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Hook; i++){.    
0db0: 20 20 64 6f 52 6f 6c 6c 62 61 63 6b 20 7c 3d 20    doRollback |= 
0dc0: 61 48 6f 6f 6b 5b 69 5d 2e 78 48 6f 6f 6b 28 29  aHook[i].xHook()
0dd0: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 5f 6d  ;.    }.    db_m
0de0: 75 6c 74 69 5f 65 78 65 63 28 64 6f 52 6f 6c 6c  ulti_exec(doRoll
0df0: 62 61 63 6b 20 3f 20 22 52 4f 4c 4c 42 41 43 4b  back ? "ROLLBACK
0e00: 22 20 3a 20 22 43 4f 4d 4d 49 54 22 29 3b 0a 20  " : "COMMIT");. 
0e10: 20 20 20 64 6f 52 6f 6c 6c 62 61 63 6b 20 3d 20     doRollback = 
0e20: 30 3b 0a 20 20 7d 0a 7d 0a 76 6f 69 64 20 64 62  0;.  }.}.void db
0e30: 5f 66 6f 72 63 65 5f 72 6f 6c 6c 62 61 63 6b 28  _force_rollback(
0e40: 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 6e 42 65  void){.  if( nBe
0e50: 67 69 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  gin ){.    sqlit
0e60: 65 33 5f 65 78 65 63 28 67 2e 64 62 2c 20 22 52  e3_exec(g.db, "R
0e70: 4f 4c 4c 42 41 43 4b 22 2c 20 30 2c 20 30 2c 20  OLLBACK", 0, 0, 
0e80: 30 29 3b 0a 20 20 20 20 69 66 28 20 69 73 4e 65  0);.    if( isNe
0e90: 77 52 65 70 6f 20 29 7b 0a 20 20 20 20 20 20 64  wRepo ){.      d
0ea0: 62 5f 63 6c 6f 73 65 28 29 3b 0a 20 20 20 20 20  b_close();.     
0eb0: 20 75 6e 6c 69 6e 6b 28 67 2e 7a 52 65 70 6f 73   unlink(g.zRepos
0ec0: 69 74 6f 72 79 4e 61 6d 65 29 3b 0a 20 20 20 20  itoryName);.    
0ed0: 7d 0a 20 20 7d 0a 20 20 6e 42 65 67 69 6e 20 3d  }.  }.  nBegin =
0ee0: 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73   0;.}../*.** Ins
0ef0: 74 61 6c 6c 20 61 20 63 6f 6d 6d 69 74 20 68 6f  tall a commit ho
0f00: 6f 6b 2e 20 20 48 6f 6f 6b 73 20 61 72 65 20 69  ok.  Hooks are i
0f10: 6e 73 74 61 6c 6c 65 64 20 69 6e 20 73 65 71 75  nstalled in sequ
0f20: 65 6e 63 65 20 6f 72 64 65 72 2e 0a 2a 2a 20 49  ence order..** I
0f30: 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 74 6f  t is an error to
0f40: 20 69 6e 73 74 61 6c 6c 20 74 68 65 20 73 61 6d   install the sam
0f50: 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 6d 6f  e commit hook mo
0f60: 72 65 20 74 68 61 6e 20 6f 6e 63 65 2e 0a 2a 2a  re than once..**
0f70: 0a 2a 2a 20 45 61 63 68 20 63 6f 6d 6d 69 74 20  .** Each commit 
0f80: 68 6f 6f 6b 20 69 73 20 63 61 6c 6c 65 64 20 28  hook is called (
0f90: 69 6e 20 6f 72 64 65 72 20 6f 66 20 61 63 63 65  in order of acce
0fa0: 6e 64 69 6e 67 20 73 65 71 75 65 6e 63 65 29 20  nding sequence) 
0fb0: 61 74 0a 2a 2a 20 65 61 63 68 20 63 6f 6d 6d 69  at.** each commi
0fc0: 74 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 49 66  t operation.  If
0fd0: 20 61 6e 79 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b   any commit hook
0fe0: 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72   returns non-zer
0ff0: 6f 2c 0a 2a 2a 20 74 68 65 20 73 75 62 73 65 71  o,.** the subseq
1000: 75 65 6e 63 65 20 63 6f 6d 6d 69 74 20 68 6f 6f  uence commit hoo
1010: 6b 73 20 61 72 65 20 6f 6d 69 74 74 65 64 20 61  ks are omitted a
1020: 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  nd the transacti
1030: 6f 6e 0a 2a 2a 20 72 6f 6c 6c 73 20 62 61 63 6b  on.** rolls back
1040: 20 72 61 74 68 65 72 20 74 68 61 6e 20 63 6f 6d   rather than com
1050: 6d 69 74 2e 20 20 49 74 20 69 73 20 74 68 65 20  mit.  It is the 
1060: 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f  responsibility o
1070: 66 20 74 68 65 0a 2a 2a 20 68 6f 6f 6b 73 20 74  f the.** hooks t
1080: 68 65 6d 73 65 6c 76 65 73 20 74 6f 20 69 73 73  hemselves to iss
1090: 75 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73  ue any error mes
10a0: 73 61 67 65 73 2e 0a 2a 2f 0a 76 6f 69 64 20 64  sages..*/.void d
10b0: 62 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 69 6e  b_commit_hook(in
10c0: 74 20 28 2a 78 29 28 76 6f 69 64 29 2c 20 69 6e  t (*x)(void), in
10d0: 74 20 73 65 71 75 65 6e 63 65 29 7b 0a 20 20 69  t sequence){.  i
10e0: 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20  nt i;.  assert( 
10f0: 6e 43 6f 6d 6d 69 74 48 6f 6f 6b 20 3c 20 73 69  nCommitHook < si
1100: 7a 65 6f 66 28 61 48 6f 6f 6b 29 2f 73 69 7a 65  zeof(aHook)/size
1110: 6f 66 28 61 48 6f 6f 6b 5b 31 5d 29 20 29 3b 0a  of(aHook[1]) );.
1120: 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f    for(i=0; i<nCo
1130: 6d 6d 69 74 48 6f 6f 6b 3b 20 69 2b 2b 29 7b 0a  mmitHook; i++){.
1140: 20 20 20 20 61 73 73 65 72 74 28 20 78 21 3d 61      assert( x!=a
1150: 48 6f 6f 6b 5b 69 5d 2e 78 48 6f 6f 6b 20 29 3b  Hook[i].xHook );
1160: 0a 20 20 20 20 69 66 28 20 61 48 6f 6f 6b 5b 69  .    if( aHook[i
1170: 5d 2e 73 65 71 75 65 6e 63 65 3e 73 65 71 75 65  ].sequence>seque
1180: 6e 63 65 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  nce ){.      int
1190: 20 73 20 3d 20 73 65 71 75 65 6e 63 65 3b 0a 20   s = sequence;. 
11a0: 20 20 20 20 20 69 6e 74 20 28 2a 78 53 29 28 76       int (*xS)(v
11b0: 6f 69 64 29 20 3d 20 78 3b 0a 20 20 20 20 20 20  oid) = x;.      
11c0: 73 65 71 75 65 6e 63 65 20 3d 20 61 48 6f 6f 6b  sequence = aHook
11d0: 5b 69 5d 2e 73 65 71 75 65 6e 63 65 3b 0a 20 20  [i].sequence;.  
11e0: 20 20 20 20 78 20 3d 20 61 48 6f 6f 6b 5b 69 5d      x = aHook[i]
11f0: 2e 78 48 6f 6f 6b 3b 0a 20 20 20 20 20 20 61 48  .xHook;.      aH
1200: 6f 6f 6b 5b 69 5d 2e 73 65 71 75 65 6e 63 65 20  ook[i].sequence 
1210: 3d 20 73 3b 0a 20 20 20 20 20 20 61 48 6f 6f 6b  = s;.      aHook
1220: 5b 69 5d 2e 78 48 6f 6f 6b 20 3d 20 78 53 3b 0a  [i].xHook = xS;.
1230: 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 48 6f 6f      }.  }.  aHoo
1240: 6b 5b 6e 43 6f 6d 6d 69 74 48 6f 6f 6b 5d 2e 73  k[nCommitHook].s
1250: 65 71 75 65 6e 63 65 20 3d 20 73 65 71 75 65 6e  equence = sequen
1260: 63 65 3b 0a 20 20 61 48 6f 6f 6b 5b 6e 43 6f 6d  ce;.  aHook[nCom
1270: 6d 69 74 48 6f 6f 6b 5d 2e 78 48 6f 6f 6b 20 3d  mitHook].xHook =
1280: 20 78 3b 0a 20 20 6e 43 6f 6d 6d 69 74 48 6f 6f   x;.  nCommitHoo
1290: 6b 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72  k++;.}../*.** Pr
12a0: 65 70 61 72 65 20 61 20 53 74 6d 74 2e 20 20 41  epare a Stmt.  A
12b0: 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 20 53  ssume that the S
12c0: 74 6d 74 20 69 73 20 70 72 65 76 69 6f 75 73 6c  tmt is previousl
12d0: 79 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e  y uninitialized.
12e0: 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74  .** If the input
12f0: 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 73   string contains
1300: 20 6d 75 6c 74 69 70 6c 65 20 53 51 4c 20 73 74   multiple SQL st
1310: 61 74 65 6d 65 6e 74 73 2c 20 6f 6e 6c 79 20 74  atements, only t
1320: 68 65 20 66 69 72 73 74 0a 2a 2a 20 6f 6e 65 20  he first.** one 
1330: 69 73 20 70 72 6f 63 65 73 73 65 64 2e 20 20 41  is processed.  A
1340: 6c 6c 20 73 74 61 74 65 6d 65 6e 74 73 20 62 65  ll statements be
1350: 79 6f 6e 64 20 74 68 65 20 66 69 72 73 74 20 61  yond the first a
1360: 72 65 20 73 69 6c 65 6e 74 6c 79 20 69 67 6e 6f  re silently igno
1370: 72 65 64 2e 0a 2a 2f 0a 69 6e 74 20 64 62 5f 76  red..*/.int db_v
1380: 70 72 65 70 61 72 65 28 53 74 6d 74 20 2a 70 53  prepare(Stmt *pS
1390: 74 6d 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  tmt, const char 
13a0: 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73  *zFormat, va_lis
13b0: 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a  t ap){.  char *z
13c0: 53 71 6c 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f  Sql;.  blob_zero
13d0: 28 26 70 53 74 6d 74 2d 3e 73 71 6c 29 3b 0a 20  (&pStmt->sql);. 
13e0: 20 62 6c 6f 62 5f 76 61 70 70 65 6e 64 66 28 26   blob_vappendf(&
13f0: 70 53 74 6d 74 2d 3e 73 71 6c 2c 20 7a 46 6f 72  pStmt->sql, zFor
1400: 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65  mat, ap);.  va_e
1410: 6e 64 28 61 70 29 3b 0a 20 20 7a 53 71 6c 20 3d  nd(ap);.  zSql =
1420: 20 62 6c 6f 62 5f 73 74 72 28 26 70 53 74 6d 74   blob_str(&pStmt
1430: 2d 3e 73 71 6c 29 3b 0a 20 20 69 66 28 20 73 71  ->sql);.  if( sq
1440: 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
1450: 28 67 2e 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c  (g.db, zSql, -1,
1460: 20 26 70 53 74 6d 74 2d 3e 70 53 74 6d 74 2c 20   &pStmt->pStmt, 
1470: 30 29 21 3d 30 20 29 7b 0a 20 20 20 20 64 62 5f  0)!=0 ){.    db_
1480: 65 72 72 28 22 25 73 5c 6e 25 73 22 2c 20 73 71  err("%s\n%s", sq
1490: 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 67 2e 64  lite3_errmsg(g.d
14a0: 62 29 2c 20 7a 53 71 6c 29 3b 0a 20 20 7d 0a 20  b), zSql);.  }. 
14b0: 20 70 53 74 6d 74 2d 3e 70 4e 65 78 74 20 3d 20   pStmt->pNext = 
14c0: 70 53 74 6d 74 2d 3e 70 50 72 65 76 20 3d 20 30  pStmt->pPrev = 0
14d0: 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  ;.  return 0;.}.
14e0: 69 6e 74 20 64 62 5f 70 72 65 70 61 72 65 28 53  int db_prepare(S
14f0: 74 6d 74 20 2a 70 53 74 6d 74 2c 20 63 6f 6e 73  tmt *pStmt, cons
1500: 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c  t char *zFormat,
1510: 20 2e 2e 2e 29 7b 0a 20 20 69 6e 74 20 72 63 3b   ...){.  int rc;
1520: 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20  .  va_list ap;. 
1530: 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46   va_start(ap, zF
1540: 6f 72 6d 61 74 29 3b 0a 20 20 72 63 20 3d 20 64  ormat);.  rc = d
1550: 62 5f 76 70 72 65 70 61 72 65 28 70 53 74 6d 74  b_vprepare(pStmt
1560: 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a  , zFormat, ap);.
1570: 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20    va_end(ap);.  
1580: 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 69 6e 74  return rc;.}.int
1590: 20 64 62 5f 73 74 61 74 69 63 5f 70 72 65 70 61   db_static_prepa
15a0: 72 65 28 53 74 6d 74 20 2a 70 53 74 6d 74 2c 20  re(Stmt *pStmt, 
15b0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
15c0: 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 69 6e 74  mat, ...){.  int
15d0: 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
15e0: 0a 20 20 69 66 28 20 62 6c 6f 62 5f 73 69 7a 65  .  if( blob_size
15f0: 28 26 70 53 74 6d 74 2d 3e 73 71 6c 29 3d 3d 30  (&pStmt->sql)==0
1600: 20 29 7b 0a 20 20 20 20 76 61 5f 6c 69 73 74 20   ){.    va_list 
1610: 61 70 3b 0a 20 20 20 20 76 61 5f 73 74 61 72 74  ap;.    va_start
1620: 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20  (ap, zFormat);. 
1630: 20 20 20 72 63 20 3d 20 64 62 5f 76 70 72 65 70     rc = db_vprep
1640: 61 72 65 28 70 53 74 6d 74 2c 20 7a 46 6f 72 6d  are(pStmt, zForm
1650: 61 74 2c 20 61 70 29 3b 0a 20 20 20 20 70 53 74  at, ap);.    pSt
1660: 6d 74 2d 3e 70 4e 65 78 74 20 3d 20 70 41 6c 6c  mt->pNext = pAll
1670: 53 74 6d 74 3b 0a 20 20 20 20 70 53 74 6d 74 2d  Stmt;.    pStmt-
1680: 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20  >pPrev = 0;.    
1690: 69 66 28 20 70 41 6c 6c 53 74 6d 74 20 29 20 70  if( pAllStmt ) p
16a0: 41 6c 6c 53 74 6d 74 2d 3e 70 50 72 65 76 20 3d  AllStmt->pPrev =
16b0: 20 70 53 74 6d 74 3b 0a 20 20 20 20 70 41 6c 6c   pStmt;.    pAll
16c0: 53 74 6d 74 20 3d 20 70 53 74 6d 74 3b 0a 20 20  Stmt = pStmt;.  
16d0: 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20    va_end(ap);.  
16e0: 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
16f0: 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
1700: 68 65 20 69 6e 64 65 78 20 6f 66 20 61 20 62 69  he index of a bi
1710: 6e 64 20 70 61 72 61 6d 65 74 65 72 0a 2a 2f 0a  nd parameter.*/.
1720: 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 61 6d  static int param
1730: 49 64 78 28 53 74 6d 74 20 2a 70 53 74 6d 74 2c  Idx(Stmt *pStmt,
1740: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61   const char *zPa
1750: 72 61 6d 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 20  ramName){.  int 
1760: 69 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  i = sqlite3_bind
1770: 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78  _parameter_index
1780: 28 70 53 74 6d 74 2d 3e 70 53 74 6d 74 2c 20 7a  (pStmt->pStmt, z
1790: 50 61 72 61 6d 4e 61 6d 65 29 3b 0a 20 20 69 66  ParamName);.  if
17a0: 28 20 69 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62  ( i==0 ){.    db
17b0: 5f 65 72 72 28 22 6e 6f 20 73 75 63 68 20 62 69  _err("no such bi
17c0: 6e 64 20 70 61 72 61 6d 65 74 65 72 3a 20 25 73  nd parameter: %s
17d0: 5c 6e 53 51 4c 3a 20 25 62 22 2c 20 7a 50 61 72  \nSQL: %b", zPar
17e0: 61 6d 4e 61 6d 65 2c 20 26 70 53 74 6d 74 2d 3e  amName, &pStmt->
17f0: 73 71 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  sql);.  }.  retu
1800: 72 6e 20 69 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 42 69  rn i;.}./*.** Bi
1810: 6e 64 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 73  nd an integer, s
1820: 74 72 69 6e 67 2c 20 6f 72 20 42 6c 6f 62 20 76  tring, or Blob v
1830: 61 6c 75 65 20 74 6f 20 61 20 6e 61 6d 65 64 20  alue to a named 
1840: 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 69 6e  parameter..*/.in
1850: 74 20 64 62 5f 62 69 6e 64 5f 69 6e 74 28 53 74  t db_bind_int(St
1860: 6d 74 20 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74  mt *pStmt, const
1870: 20 63 68 61 72 20 2a 7a 50 61 72 61 6d 4e 61 6d   char *zParamNam
1880: 65 2c 20 69 6e 74 20 69 56 61 6c 75 65 29 7b 0a  e, int iValue){.
1890: 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
18a0: 5f 62 69 6e 64 5f 69 6e 74 28 70 53 74 6d 74 2d  _bind_int(pStmt-
18b0: 3e 70 53 74 6d 74 2c 20 70 61 72 61 6d 49 64 78  >pStmt, paramIdx
18c0: 28 70 53 74 6d 74 2c 20 7a 50 61 72 61 6d 4e 61  (pStmt, zParamNa
18d0: 6d 65 29 2c 20 69 56 61 6c 75 65 29 3b 0a 7d 0a  me), iValue);.}.
18e0: 69 6e 74 20 64 62 5f 62 69 6e 64 5f 69 6e 74 36  int db_bind_int6
18f0: 34 28 53 74 6d 74 20 2a 70 53 74 6d 74 2c 20 63  4(Stmt *pStmt, c
1900: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 72 61  onst char *zPara
1910: 6d 4e 61 6d 65 2c 20 69 36 34 20 69 56 61 6c 75  mName, i64 iValu
1920: 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  e){.  return sql
1930: 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
1940: 70 53 74 6d 74 2d 3e 70 53 74 6d 74 2c 20 70 61  pStmt->pStmt, pa
1950: 72 61 6d 49 64 78 28 70 53 74 6d 74 2c 20 7a 50  ramIdx(pStmt, zP
1960: 61 72 61 6d 4e 61 6d 65 29 2c 20 69 56 61 6c 75  aramName), iValu
1970: 65 29 3b 0a 7d 0a 69 6e 74 20 64 62 5f 62 69 6e  e);.}.int db_bin
1980: 64 5f 64 6f 75 62 6c 65 28 53 74 6d 74 20 2a 70  d_double(Stmt *p
1990: 53 74 6d 74 2c 20 63 6f 6e 73 74 20 63 68 61 72  Stmt, const char
19a0: 20 2a 7a 50 61 72 61 6d 4e 61 6d 65 2c 20 64 6f   *zParamName, do
19b0: 75 62 6c 65 20 72 56 61 6c 75 65 29 7b 0a 20 20  uble rValue){.  
19c0: 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 62  return sqlite3_b
19d0: 69 6e 64 5f 64 6f 75 62 6c 65 28 70 53 74 6d 74  ind_double(pStmt
19e0: 2d 3e 70 53 74 6d 74 2c 20 70 61 72 61 6d 49 64  ->pStmt, paramId
19f0: 78 28 70 53 74 6d 74 2c 20 7a 50 61 72 61 6d 4e  x(pStmt, zParamN
1a00: 61 6d 65 29 2c 20 72 56 61 6c 75 65 29 3b 0a 7d  ame), rValue);.}
1a10: 0a 69 6e 74 20 64 62 5f 62 69 6e 64 5f 74 65 78  .int db_bind_tex
1a20: 74 28 53 74 6d 74 20 2a 70 53 74 6d 74 2c 20 63  t(Stmt *pStmt, c
1a30: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 72 61  onst char *zPara
1a40: 6d 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61  mName, const cha
1a50: 72 20 2a 7a 56 61 6c 75 65 29 7b 0a 20 20 72 65  r *zValue){.  re
1a60: 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 62 69 6e  turn sqlite3_bin
1a70: 64 5f 74 65 78 74 28 70 53 74 6d 74 2d 3e 70 53  d_text(pStmt->pS
1a80: 74 6d 74 2c 20 70 61 72 61 6d 49 64 78 28 70 53  tmt, paramIdx(pS
1a90: 74 6d 74 2c 20 7a 50 61 72 61 6d 4e 61 6d 65 29  tmt, zParamName)
1aa0: 2c 20 7a 56 61 6c 75 65 2c 0a 20 20 20 20 20 20  , zValue,.      
1ab0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ac0: 20 20 20 20 20 2d 31 2c 20 53 51 4c 49 54 45 5f       -1, SQLITE_
1ad0: 53 54 41 54 49 43 29 3b 0a 7d 0a 69 6e 74 20 64  STATIC);.}.int d
1ae0: 62 5f 62 69 6e 64 5f 6e 75 6c 6c 28 53 74 6d 74  b_bind_null(Stmt
1af0: 20 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20 63   *pStmt, const c
1b00: 68 61 72 20 2a 7a 50 61 72 61 6d 4e 61 6d 65 29  har *zParamName)
1b10: 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
1b20: 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70 53 74  e3_bind_null(pSt
1b30: 6d 74 2d 3e 70 53 74 6d 74 2c 20 70 61 72 61 6d  mt->pStmt, param
1b40: 49 64 78 28 70 53 74 6d 74 2c 20 7a 50 61 72 61  Idx(pStmt, zPara
1b50: 6d 4e 61 6d 65 29 29 3b 0a 7d 0a 69 6e 74 20 64  mName));.}.int d
1b60: 62 5f 62 69 6e 64 5f 62 6c 6f 62 28 53 74 6d 74  b_bind_blob(Stmt
1b70: 20 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20 63   *pStmt, const c
1b80: 68 61 72 20 2a 7a 50 61 72 61 6d 4e 61 6d 65 2c  har *zParamName,
1b90: 20 42 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20   Blob *pBlob){. 
1ba0: 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f   return sqlite3_
1bb0: 62 69 6e 64 5f 62 6c 6f 62 28 70 53 74 6d 74 2d  bind_blob(pStmt-
1bc0: 3e 70 53 74 6d 74 2c 20 70 61 72 61 6d 49 64 78  >pStmt, paramIdx
1bd0: 28 70 53 74 6d 74 2c 20 7a 50 61 72 61 6d 4e 61  (pStmt, zParamNa
1be0: 6d 65 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  me),.           
1bf0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
1c00: 6c 6f 62 5f 62 75 66 66 65 72 28 70 42 6c 6f 62  lob_buffer(pBlob
1c10: 29 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 70 42 6c  ), blob_size(pBl
1c20: 6f 62 29 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  ob), SQLITE_STAT
1c30: 49 43 29 3b 0a 7d 0a 0a 2f 2a 20 62 69 6e 64 5f  IC);.}../* bind_
1c40: 73 74 72 28 29 20 74 72 65 61 74 73 20 61 20 42  str() treats a B
1c50: 6c 6f 62 20 6f 62 6a 65 63 74 20 6c 69 6b 65 20  lob object like 
1c60: 61 20 54 45 58 54 20 73 74 72 69 6e 67 20 61 6e  a TEXT string an
1c70: 64 20 62 69 6e 64 73 20 69 74 0a 2a 2a 20 74 6f  d binds it.** to
1c80: 20 74 68 65 20 53 51 4c 20 76 61 72 69 61 62 6c   the SQL variabl
1c90: 65 2e 20 20 43 6f 6e 73 74 72 61 73 74 20 74 68  e.  Constrast th
1ca0: 69 73 20 74 6f 20 62 69 6e 64 5f 62 6c 6f 62 28  is to bind_blob(
1cb0: 29 20 77 68 69 63 68 20 74 72 65 61 74 73 0a 2a  ) which treats.*
1cc0: 2a 20 74 68 65 20 42 6c 6f 62 20 6f 62 6a 65 63  * the Blob objec
1cd0: 74 20 6c 69 6b 65 20 61 6e 20 53 51 4c 20 42 4c  t like an SQL BL
1ce0: 4f 42 2e 0a 2a 2f 0a 69 6e 74 20 64 62 5f 62 69  OB..*/.int db_bi
1cf0: 6e 64 5f 73 74 72 28 53 74 6d 74 20 2a 70 53 74  nd_str(Stmt *pSt
1d00: 6d 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  mt, const char *
1d10: 7a 50 61 72 61 6d 4e 61 6d 65 2c 20 42 6c 6f 62  zParamName, Blob
1d20: 20 2a 70 42 6c 6f 62 29 7b 0a 20 20 72 65 74 75   *pBlob){.  retu
1d30: 72 6e 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  rn sqlite3_bind_
1d40: 74 65 78 74 28 70 53 74 6d 74 2d 3e 70 53 74 6d  text(pStmt->pStm
1d50: 74 2c 20 70 61 72 61 6d 49 64 78 28 70 53 74 6d  t, paramIdx(pStm
1d60: 74 2c 20 7a 50 61 72 61 6d 4e 61 6d 65 29 2c 0a  t, zParamName),.
1d70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d80: 20 20 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 62            blob_b
1d90: 75 66 66 65 72 28 70 42 6c 6f 62 29 2c 20 62 6c  uffer(pBlob), bl
1da0: 6f 62 5f 73 69 7a 65 28 70 42 6c 6f 62 29 2c 20  ob_size(pBlob), 
1db0: 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
1dc0: 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 70 20 74 68  }../*.** Step th
1dd0: 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  e SQL statement.
1de0: 20 20 52 65 74 75 72 6e 20 65 69 74 68 65 72 20    Return either 
1df0: 53 51 4c 49 54 45 5f 52 4f 57 20 6f 72 20 61 6e  SQLITE_ROW or an
1e00: 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 6f   error code.** o
1e10: 72 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 74  r SQLITE_OK if t
1e20: 68 65 20 73 74 61 74 65 6d 65 6e 74 20 66 69 6e  he statement fin
1e30: 69 73 68 65 73 20 73 75 63 63 65 73 73 66 75 6c  ishes successful
1e40: 6c 79 2e 0a 2a 2f 0a 69 6e 74 20 64 62 5f 73 74  ly..*/.int db_st
1e50: 65 70 28 53 74 6d 74 20 2a 70 53 74 6d 74 29 7b  ep(Stmt *pStmt){
1e60: 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 72 63 20  .  int rc;.  rc 
1e70: 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70  = sqlite3_step(p
1e80: 53 74 6d 74 2d 3e 70 53 74 6d 74 29 3b 0a 20 20  Stmt->pStmt);.  
1e90: 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1ea0: 0a 2a 2a 20 52 65 73 65 74 20 6f 72 20 66 69 6e  .** Reset or fin
1eb0: 61 6c 69 7a 65 20 61 20 73 74 61 74 65 6d 65 6e  alize a statemen
1ec0: 74 2e 0a 2a 2f 0a 69 6e 74 20 64 62 5f 72 65 73  t..*/.int db_res
1ed0: 65 74 28 53 74 6d 74 20 2a 70 53 74 6d 74 29 7b  et(Stmt *pStmt){
1ee0: 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69  .  int rc = sqli
1ef0: 74 65 33 5f 72 65 73 65 74 28 70 53 74 6d 74 2d  te3_reset(pStmt-
1f00: 3e 70 53 74 6d 74 29 3b 0a 20 20 64 62 5f 63 68  >pStmt);.  db_ch
1f10: 65 63 6b 5f 72 65 73 75 6c 74 28 72 63 29 3b 0a  eck_result(rc);.
1f20: 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 69    return rc;.}.i
1f30: 6e 74 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 53  nt db_finalize(S
1f40: 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69  tmt *pStmt){.  i
1f50: 6e 74 20 72 63 3b 0a 20 20 62 6c 6f 62 5f 72 65  nt rc;.  blob_re
1f60: 73 65 74 28 26 70 53 74 6d 74 2d 3e 73 71 6c 29  set(&pStmt->sql)
1f70: 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1f80: 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 2d  _finalize(pStmt-
1f90: 3e 70 53 74 6d 74 29 3b 0a 20 20 64 62 5f 63 68  >pStmt);.  db_ch
1fa0: 65 63 6b 5f 72 65 73 75 6c 74 28 72 63 29 3b 0a  eck_result(rc);.
1fb0: 20 20 70 53 74 6d 74 2d 3e 70 53 74 6d 74 20 3d    pStmt->pStmt =
1fc0: 20 30 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 2d   0;.  if( pStmt-
1fd0: 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 70 53  >pNext ){.    pS
1fe0: 74 6d 74 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65  tmt->pNext->pPre
1ff0: 76 20 3d 20 70 53 74 6d 74 2d 3e 70 50 72 65 76  v = pStmt->pPrev
2000: 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 74 6d  ;.  }.  if( pStm
2010: 74 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20  t->pPrev ){.    
2020: 70 53 74 6d 74 2d 3e 70 50 72 65 76 2d 3e 70 4e  pStmt->pPrev->pN
2030: 65 78 74 20 3d 20 70 53 74 6d 74 2d 3e 70 4e 65  ext = pStmt->pNe
2040: 78 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  xt;.  }else if( 
2050: 70 41 6c 6c 53 74 6d 74 3d 3d 70 53 74 6d 74 20  pAllStmt==pStmt 
2060: 29 7b 0a 20 20 20 20 70 41 6c 6c 53 74 6d 74 20  ){.    pAllStmt 
2070: 3d 20 70 53 74 6d 74 2d 3e 70 4e 65 78 74 3b 0a  = pStmt->pNext;.
2080: 20 20 7d 0a 20 20 70 53 74 6d 74 2d 3e 70 4e 65    }.  pStmt->pNe
2090: 78 74 20 3d 20 30 3b 0a 20 20 70 53 74 6d 74 2d  xt = 0;.  pStmt-
20a0: 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 72 65  >pPrev = 0;.  re
20b0: 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
20c0: 2a 20 52 65 74 75 72 6e 20 74 68 65 20 72 6f 77  * Return the row
20d0: 69 64 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72  id of the most r
20e0: 65 63 65 6e 74 20 69 6e 73 65 72 74 0a 2a 2f 0a  ecent insert.*/.
20f0: 69 36 34 20 64 62 5f 6c 61 73 74 5f 69 6e 73 65  i64 db_last_inse
2100: 72 74 5f 72 6f 77 69 64 28 76 6f 69 64 29 7b 0a  rt_rowid(void){.
2110: 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
2120: 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77  _last_insert_row
2130: 69 64 28 67 2e 64 62 29 3b 0a 7d 0a 0a 2f 2a 0a  id(g.db);.}../*.
2140: 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
2150: 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61  mber of rows tha
2160: 74 20 77 65 72 65 20 63 68 61 6e 67 65 64 20 62  t were changed b
2170: 79 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  y the most recen
2180: 74 0a 2a 2a 20 49 4e 53 45 52 54 2c 20 55 50 44  t.** INSERT, UPD
2190: 41 54 45 2c 20 6f 72 20 44 45 4c 45 54 45 2e 20  ATE, or DELETE. 
21a0: 20 41 75 78 69 6c 69 61 72 79 20 63 68 61 6e 67   Auxiliary chang
21b0: 65 73 20 63 61 75 73 65 64 20 62 79 20 74 72 69  es caused by tri
21c0: 67 67 65 72 73 0a 2a 2a 20 6f 72 20 6f 74 68 65  ggers.** or othe
21d0: 72 20 73 69 64 65 20 65 66 66 65 63 74 73 20 61  r side effects a
21e0: 72 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64 2e 0a  re not counted..
21f0: 2a 2f 0a 69 6e 74 20 64 62 5f 63 68 61 6e 67 65  */.int db_change
2200: 73 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72  s(void){.  retur
2210: 6e 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65  n sqlite3_change
2220: 73 28 67 2e 64 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  s(g.db);.}../*.*
2230: 2a 20 45 78 74 72 61 63 74 20 74 65 78 74 2c 20  * Extract text, 
2240: 69 6e 74 65 67 65 72 2c 20 6f 72 20 62 6c 6f 62  integer, or blob
2250: 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65   values from the
2260: 20 4e 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20   N-th column of 
2270: 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 72  the.** current r
2280: 6f 77 2e 0a 2a 2f 0a 69 6e 74 20 64 62 5f 63 6f  ow..*/.int db_co
2290: 6c 75 6d 6e 5f 62 79 74 65 73 28 53 74 6d 74 20  lumn_bytes(Stmt 
22a0: 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a  *pStmt, int N){.
22b0: 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
22c0: 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 53  _column_bytes(pS
22d0: 74 6d 74 2d 3e 70 53 74 6d 74 2c 20 4e 29 3b 0a  tmt->pStmt, N);.
22e0: 7d 0a 69 6e 74 20 64 62 5f 63 6f 6c 75 6d 6e 5f  }.int db_column_
22f0: 69 6e 74 28 53 74 6d 74 20 2a 70 53 74 6d 74 2c  int(Stmt *pStmt,
2300: 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72   int N){.  retur
2310: 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  n sqlite3_column
2320: 5f 69 6e 74 28 70 53 74 6d 74 2d 3e 70 53 74 6d  _int(pStmt->pStm
2330: 74 2c 20 4e 29 3b 0a 7d 0a 69 36 34 20 64 62 5f  t, N);.}.i64 db_
2340: 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 53 74 6d  column_int64(Stm
2350: 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29  t *pStmt, int N)
2360: 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
2370: 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28  e3_column_int64(
2380: 70 53 74 6d 74 2d 3e 70 53 74 6d 74 2c 20 4e 29  pStmt->pStmt, N)
2390: 3b 0a 7d 0a 64 6f 75 62 6c 65 20 64 62 5f 63 6f  ;.}.double db_co
23a0: 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 53 74 6d 74  lumn_double(Stmt
23b0: 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b   *pStmt, int N){
23c0: 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
23d0: 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28  3_column_double(
23e0: 70 53 74 6d 74 2d 3e 70 53 74 6d 74 2c 20 4e 29  pStmt->pStmt, N)
23f0: 3b 0a 7d 0a 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.}.const char *
2400: 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 53  db_column_text(S
2410: 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
2420: 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 63 68  N){.  return (ch
2430: 61 72 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ar*)sqlite3_colu
2440: 6d 6e 5f 74 65 78 74 28 70 53 74 6d 74 2d 3e 70  mn_text(pStmt->p
2450: 53 74 6d 74 2c 20 4e 29 3b 0a 7d 0a 63 6f 6e 73  Stmt, N);.}.cons
2460: 74 20 63 68 61 72 20 2a 64 62 5f 63 6f 6c 75 6d  t char *db_colum
2470: 6e 5f 6e 61 6d 65 28 53 74 6d 74 20 2a 70 53 74  n_name(Stmt *pSt
2480: 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65  mt, int N){.  re
2490: 74 75 72 6e 20 28 63 68 61 72 2a 29 73 71 6c 69  turn (char*)sqli
24a0: 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28  te3_column_name(
24b0: 70 53 74 6d 74 2d 3e 70 53 74 6d 74 2c 20 4e 29  pStmt->pStmt, N)
24c0: 3b 0a 7d 0a 69 6e 74 20 64 62 5f 63 6f 6c 75 6d  ;.}.int db_colum
24d0: 6e 5f 63 6f 75 6e 74 28 53 74 6d 74 20 2a 70 53  n_count(Stmt *pS
24e0: 74 6d 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  tmt){.  return s
24f0: 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f  qlite3_column_co
2500: 75 6e 74 28 70 53 74 6d 74 2d 3e 70 53 74 6d 74  unt(pStmt->pStmt
2510: 29 3b 0a 7d 0a 63 68 61 72 20 2a 64 62 5f 63 6f  );.}.char *db_co
2520: 6c 75 6d 6e 5f 6d 61 6c 6c 6f 63 28 53 74 6d 74  lumn_malloc(Stmt
2530: 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b   *pStmt, int N){
2540: 0a 20 20 72 65 74 75 72 6e 20 6d 70 72 69 6e 74  .  return mprint
2550: 66 28 22 25 73 22 2c 20 64 62 5f 63 6f 6c 75 6d  f("%s", db_colum
2560: 6e 5f 74 65 78 74 28 70 53 74 6d 74 2c 20 4e 29  n_text(pStmt, N)
2570: 29 3b 0a 7d 0a 76 6f 69 64 20 64 62 5f 63 6f 6c  );.}.void db_col
2580: 75 6d 6e 5f 62 6c 6f 62 28 53 74 6d 74 20 2a 70  umn_blob(Stmt *p
2590: 53 74 6d 74 2c 20 69 6e 74 20 4e 2c 20 42 6c 6f  Stmt, int N, Blo
25a0: 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 20 62 6c 6f  b *pBlob){.  blo
25b0: 62 5f 61 70 70 65 6e 64 28 70 42 6c 6f 62 2c 20  b_append(pBlob, 
25c0: 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
25d0: 6c 6f 62 28 70 53 74 6d 74 2d 3e 70 53 74 6d 74  lob(pStmt->pStmt
25e0: 2c 20 4e 29 2c 0a 20 20 20 20 20 20 20 20 20 20  , N),.          
25f0: 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75      sqlite3_colu
2600: 6d 6e 5f 62 79 74 65 73 28 70 53 74 6d 74 2d 3e  mn_bytes(pStmt->
2610: 70 53 74 6d 74 2c 20 4e 29 29 3b 0a 7d 0a 0a 2f  pStmt, N));.}../
2620: 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
2630: 61 20 62 6c 6f 62 20 74 6f 20 61 6e 20 65 70 68  a blob to an eph
2640: 65 72 6d 65 72 61 6c 20 63 6f 70 79 20 6f 66 20  ermeral copy of 
2650: 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61  the content of a
2660: 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68  .** column in th
2670: 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2e 20 20  e current row.  
2680: 54 68 65 20 64 61 74 61 20 69 6e 20 74 68 65 20  The data in the 
2690: 62 6c 6f 62 20 77 69 6c 6c 20 62 65 63 6f 6d 65  blob will become
26a0: 0a 2a 2a 20 69 6e 76 61 6c 69 64 20 77 68 65 6e  .** invalid when
26b0: 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69   the statement i
26c0: 73 20 73 74 65 70 70 65 64 20 6f 72 20 72 65 73  s stepped or res
26d0: 65 74 2e 0a 2a 2f 0a 76 6f 69 64 20 64 62 5f 65  et..*/.void db_e
26e0: 70 68 65 6d 65 72 61 6c 5f 62 6c 6f 62 28 53 74  phemeral_blob(St
26f0: 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e  mt *pStmt, int N
2700: 2c 20 42 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a  , Blob *pBlob){.
2710: 20 20 62 6c 6f 62 5f 69 6e 69 74 28 70 42 6c 6f    blob_init(pBlo
2720: 62 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  b, sqlite3_colum
2730: 6e 5f 62 6c 6f 62 28 70 53 74 6d 74 2d 3e 70 53  n_blob(pStmt->pS
2740: 74 6d 74 2c 20 4e 29 2c 0a 20 20 20 20 20 20 20  tmt, N),.       
2750: 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63         sqlite3_c
2760: 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 53 74 6d  olumn_bytes(pStm
2770: 74 2d 3e 70 53 74 6d 74 2c 20 4e 29 29 3b 0a 7d  t->pStmt, N));.}
2780: 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 61 20  ../*.** Check a 
2790: 72 65 73 75 6c 74 20 63 6f 64 65 2e 20 20 49 66  result code.  If
27a0: 20 69 74 20 69 73 20 6e 6f 74 20 53 51 4c 49 54   it is not SQLIT
27b0: 45 5f 4f 4b 2c 20 70 72 69 6e 74 20 74 68 65 0a  E_OK, print the.
27c0: 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ** corresponding
27d0: 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 61   error message a
27e0: 6e 64 20 65 78 69 74 2e 0a 2a 2f 0a 76 6f 69 64  nd exit..*/.void
27f0: 20 64 62 5f 63 68 65 63 6b 5f 72 65 73 75 6c 74   db_check_result
2800: 28 69 6e 74 20 72 63 29 7b 0a 20 20 69 66 28 20  (int rc){.  if( 
2810: 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
2820: 0a 20 20 20 20 64 62 5f 65 72 72 28 22 53 51 4c  .    db_err("SQL
2830: 20 65 72 72 6f 72 3a 20 25 73 22 2c 20 73 71 6c   error: %s", sql
2840: 69 74 65 33 5f 65 72 72 6d 73 67 28 67 2e 64 62  ite3_errmsg(g.db
2850: 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ));.  }.}../*.**
2860: 20 45 78 65 63 75 74 65 20 61 20 73 69 6e 67 6c   Execute a singl
2870: 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  e prepared state
2880: 6d 65 6e 74 20 75 6e 74 69 6c 20 69 74 20 66 69  ment until it fi
2890: 6e 69 73 68 65 73 2e 0a 2a 2f 0a 69 6e 74 20 64  nishes..*/.int d
28a0: 62 5f 65 78 65 63 28 53 74 6d 74 20 2a 70 53 74  b_exec(Stmt *pSt
28b0: 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  mt){.  int rc;. 
28c0: 20 77 68 69 6c 65 28 20 28 72 63 20 3d 20 64 62   while( (rc = db
28d0: 5f 73 74 65 70 28 70 53 74 6d 74 29 29 3d 3d 53  _step(pStmt))==S
28e0: 51 4c 49 54 45 5f 52 4f 57 20 29 7b 7d 0a 20 20  QLITE_ROW ){}.  
28f0: 72 63 20 3d 20 64 62 5f 72 65 73 65 74 28 70 53  rc = db_reset(pS
2900: 74 6d 74 29 3b 0a 20 20 64 62 5f 63 68 65 63 6b  tmt);.  db_check
2910: 5f 72 65 73 75 6c 74 28 72 63 29 3b 0a 20 20 72  _result(rc);.  r
2920: 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
2930: 2a 2a 20 45 78 65 63 75 74 65 20 6d 75 6c 74 69  ** Execute multi
2940: 70 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ple SQL statemen
2950: 74 73 2e 0a 2a 2f 0a 69 6e 74 20 64 62 5f 6d 75  ts..*/.int db_mu
2960: 6c 74 69 5f 65 78 65 63 28 63 6f 6e 73 74 20 63  lti_exec(const c
2970: 68 61 72 20 2a 7a 53 71 6c 2c 20 2e 2e 2e 29 7b  har *zSql, ...){
2980: 0a 20 20 42 6c 6f 62 20 73 71 6c 3b 0a 20 20 69  .  Blob sql;.  i
2990: 6e 74 20 72 63 3b 0a 20 20 76 61 5f 6c 69 73 74  nt rc;.  va_list
29a0: 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72   ap;.  char *zEr
29b0: 72 20 3d 20 30 3b 0a 20 20 62 6c 6f 62 5f 69 6e  r = 0;.  blob_in
29c0: 69 74 28 26 73 71 6c 2c 20 30 2c 20 30 29 3b 0a  it(&sql, 0, 0);.
29d0: 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a    va_start(ap, z
29e0: 53 71 6c 29 3b 0a 20 20 62 6c 6f 62 5f 76 61 70  Sql);.  blob_vap
29f0: 70 65 6e 64 66 28 26 73 71 6c 2c 20 7a 53 71 6c  pendf(&sql, zSql
2a00: 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28  , ap);.  va_end(
2a10: 61 70 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  ap);.  rc = sqli
2a20: 74 65 33 5f 65 78 65 63 28 67 2e 64 62 2c 20 62  te3_exec(g.db, b
2a30: 6c 6f 62 5f 62 75 66 66 65 72 28 26 73 71 6c 29  lob_buffer(&sql)
2a40: 2c 20 30 2c 20 30 2c 20 26 7a 45 72 72 29 3b 0a  , 0, 0, &zErr);.
2a50: 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2a60: 5f 4f 4b 20 29 7b 0a 20 20 20 20 64 62 5f 65 72  _OK ){.    db_er
2a70: 72 28 22 25 73 5c 6e 25 73 22 2c 20 7a 45 72 72  r("%s\n%s", zErr
2a80: 2c 20 62 6c 6f 62 5f 62 75 66 66 65 72 28 26 73  , blob_buffer(&s
2a90: 71 6c 29 29 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62  ql));.  }.  blob
2aa0: 5f 72 65 73 65 74 28 26 73 71 6c 29 3b 0a 20 20  _reset(&sql);.  
2ab0: 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
2ac0: 0a 2a 2a 20 45 78 65 63 75 74 65 20 61 20 71 75  .** Execute a qu
2ad0: 65 72 79 20 61 6e 64 20 72 65 74 75 72 6e 20 61  ery and return a
2ae0: 20 73 69 6e 67 6c 65 20 69 6e 74 65 67 65 72 20   single integer 
2af0: 76 61 6c 75 65 2e 0a 2a 2f 0a 69 36 34 20 64 62  value..*/.i64 db
2b00: 5f 69 6e 74 36 34 28 69 36 34 20 69 44 66 6c 74  _int64(i64 iDflt
2b10: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53  , const char *zS
2b20: 71 6c 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c  ql, ...){.  va_l
2b30: 69 73 74 20 61 70 3b 0a 20 20 53 74 6d 74 20 73  ist ap;.  Stmt s
2b40: 3b 0a 20 20 69 36 34 20 72 63 3b 0a 20 20 76 61  ;.  i64 rc;.  va
2b50: 5f 73 74 61 72 74 28 61 70 2c 20 7a 53 71 6c 29  _start(ap, zSql)
2b60: 3b 0a 20 20 64 62 5f 76 70 72 65 70 61 72 65 28  ;.  db_vprepare(
2b70: 26 73 2c 20 7a 53 71 6c 2c 20 61 70 29 3b 0a 20  &s, zSql, ap);. 
2b80: 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 69   va_end(ap);.  i
2b90: 66 28 20 64 62 5f 73 74 65 70 28 26 73 29 21 3d  f( db_step(&s)!=
2ba0: 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
2bb0: 20 20 72 63 20 3d 20 69 44 66 6c 74 3b 0a 20 20    rc = iDflt;.  
2bc0: 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
2bd0: 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28  db_column_int64(
2be0: 26 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 64 62  &s, 0);.  }.  db
2bf0: 5f 66 69 6e 61 6c 69 7a 65 28 26 73 29 3b 0a 20  _finalize(&s);. 
2c00: 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 69 6e   return rc;.}.in
2c10: 74 20 64 62 5f 69 6e 74 28 69 6e 74 20 69 44 66  t db_int(int iDf
2c20: 6c 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  lt, const char *
2c30: 7a 53 71 6c 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  zSql, ...){.  va
2c40: 5f 6c 69 73 74 20 61 70 3b 0a 20 20 53 74 6d 74  _list ap;.  Stmt
2c50: 20 73 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20   s;.  int rc;.  
2c60: 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 53 71  va_start(ap, zSq
2c70: 6c 29 3b 0a 20 20 64 62 5f 76 70 72 65 70 61 72  l);.  db_vprepar
2c80: 65 28 26 73 2c 20 7a 53 71 6c 2c 20 61 70 29 3b  e(&s, zSql, ap);
2c90: 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20  .  va_end(ap);. 
2ca0: 20 69 66 28 20 64 62 5f 73 74 65 70 28 26 73 29   if( db_step(&s)
2cb0: 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  !=SQLITE_ROW ){.
2cc0: 20 20 20 20 72 63 20 3d 20 69 44 66 6c 74 3b 0a      rc = iDflt;.
2cd0: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
2ce0: 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28  = db_column_int(
2cf0: 26 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 64 62  &s, 0);.  }.  db
2d00: 5f 66 69 6e 61 6c 69 7a 65 28 26 73 29 3b 0a 20  _finalize(&s);. 
2d10: 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
2d20: 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45  *.** Return TRUE
2d30: 20 69 66 20 74 68 65 20 71 75 65 72 79 20 77 6f   if the query wo
2d40: 75 6c 64 20 72 65 74 75 72 6e 20 31 20 6f 72 20  uld return 1 or 
2d50: 6d 6f 72 65 20 72 6f 77 73 2e 20 20 52 65 74 75  more rows.  Retu
2d60: 72 6e 0a 2a 2a 20 46 41 4c 53 45 20 69 66 20 74  rn.** FALSE if t
2d70: 68 65 20 71 75 65 72 79 20 72 65 73 75 6c 74 20  he query result 
2d80: 77 6f 75 6c 64 20 62 65 20 61 6e 20 65 6d 70 74  would be an empt
2d90: 79 20 73 65 74 2e 0a 2a 2f 0a 69 6e 74 20 64 62  y set..*/.int db
2da0: 5f 65 78 69 73 74 73 28 63 6f 6e 73 74 20 63 68  _exists(const ch
2db0: 61 72 20 2a 7a 53 71 6c 2c 20 2e 2e 2e 29 7b 0a  ar *zSql, ...){.
2dc0: 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20    va_list ap;.  
2dd0: 53 74 6d 74 20 73 3b 0a 20 20 69 6e 74 20 72 63  Stmt s;.  int rc
2de0: 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c  ;.  va_start(ap,
2df0: 20 7a 53 71 6c 29 3b 0a 20 20 64 62 5f 76 70 72   zSql);.  db_vpr
2e00: 65 70 61 72 65 28 26 73 2c 20 7a 53 71 6c 2c 20  epare(&s, zSql, 
2e10: 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70  ap);.  va_end(ap
2e20: 29 3b 0a 20 20 69 66 28 20 64 62 5f 73 74 65 70  );.  if( db_step
2e30: 28 26 73 29 21 3d 53 51 4c 49 54 45 5f 52 4f 57  (&s)!=SQLITE_ROW
2e40: 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 30 3b 0a   ){.    rc = 0;.
2e50: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
2e60: 3d 20 31 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69  = 1;.  }.  db_fi
2e70: 6e 61 6c 69 7a 65 28 26 73 29 3b 0a 20 20 72 65  nalize(&s);.  re
2e80: 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
2e90: 2a 2a 20 45 78 65 63 75 74 65 20 61 20 71 75 65  ** Execute a que
2ea0: 72 79 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20  ry and return a 
2eb0: 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 76  floating-point v
2ec0: 61 6c 75 65 2e 0a 2a 2f 0a 64 6f 75 62 6c 65 20  alue..*/.double 
2ed0: 64 62 5f 64 6f 75 62 6c 65 28 64 6f 75 62 6c 65  db_double(double
2ee0: 20 72 44 66 6c 74 2c 20 63 6f 6e 73 74 20 63 68   rDflt, const ch
2ef0: 61 72 20 2a 7a 53 71 6c 2c 20 2e 2e 2e 29 7b 0a  ar *zSql, ...){.
2f00: 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20    va_list ap;.  
2f10: 53 74 6d 74 20 73 3b 0a 20 20 64 6f 75 62 6c 65  Stmt s;.  double
2f20: 20 72 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61   r;.  va_start(a
2f30: 70 2c 20 7a 53 71 6c 29 3b 0a 20 20 64 62 5f 76  p, zSql);.  db_v
2f40: 70 72 65 70 61 72 65 28 26 73 2c 20 7a 53 71 6c  prepare(&s, zSql
2f50: 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28  , ap);.  va_end(
2f60: 61 70 29 3b 0a 20 20 69 66 28 20 64 62 5f 73 74  ap);.  if( db_st
2f70: 65 70 28 26 73 29 21 3d 53 51 4c 49 54 45 5f 52  ep(&s)!=SQLITE_R
2f80: 4f 57 20 29 7b 0a 20 20 20 20 72 20 3d 20 72 44  OW ){.    r = rD
2f90: 66 6c 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  flt;.  }else{.  
2fa0: 20 20 72 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f    r = db_column_
2fb0: 64 6f 75 62 6c 65 28 26 73 2c 20 30 29 3b 0a 20  double(&s, 0);. 
2fc0: 20 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65   }.  db_finalize
2fd0: 28 26 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  (&s);.  return r
2fe0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75  ;.}../*.** Execu
2ff0: 74 65 20 61 20 71 75 65 72 79 20 61 6e 64 20 61  te a query and a
3000: 70 70 65 6e 64 20 74 68 65 20 66 69 72 73 74 20  ppend the first 
3010: 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 66 69  column of the fi
3020: 72 73 74 20 72 6f 77 0a 2a 2a 20 6f 66 20 74 68  rst row.** of th
3030: 65 20 72 65 73 75 6c 74 20 73 65 74 20 74 6f 20  e result set to 
3040: 62 6c 6f 62 20 67 69 76 65 6e 20 69 6e 20 74 68  blob given in th
3050: 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
3060: 2e 0a 2a 2f 0a 76 6f 69 64 20 64 62 5f 62 6c 6f  ..*/.void db_blo
3070: 62 28 42 6c 6f 62 20 2a 70 52 65 73 75 6c 74 2c  b(Blob *pResult,
3080: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71   const char *zSq
3090: 6c 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69  l, ...){.  va_li
30a0: 73 74 20 61 70 3b 0a 20 20 53 74 6d 74 20 73 3b  st ap;.  Stmt s;
30b0: 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20  .  va_start(ap, 
30c0: 7a 53 71 6c 29 3b 0a 20 20 64 62 5f 76 70 72 65  zSql);.  db_vpre
30d0: 70 61 72 65 28 26 73 2c 20 7a 53 71 6c 2c 20 61  pare(&s, zSql, a
30e0: 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29  p);.  va_end(ap)
30f0: 3b 0a 20 20 69 66 28 20 64 62 5f 73 74 65 70 28  ;.  if( db_step(
3100: 26 73 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20  &s)==SQLITE_ROW 
3110: 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65  ){.    blob_appe
3120: 6e 64 28 70 52 65 73 75 6c 74 2c 20 73 71 6c 69  nd(pResult, sqli
3130: 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
3140: 73 2e 70 53 74 6d 74 2c 20 30 29 2c 0a 20 20 20  s.pStmt, 0),.   
3150: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3160: 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f        sqlite3_co
3170: 6c 75 6d 6e 5f 62 79 74 65 73 28 73 2e 70 53 74  lumn_bytes(s.pSt
3180: 6d 74 2c 20 30 29 29 3b 0a 20 20 7d 0a 20 20 64  mt, 0));.  }.  d
3190: 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73 29 3b 0a  b_finalize(&s);.
31a0: 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65  }../*.** Execute
31b0: 20 61 20 71 75 65 72 79 2e 20 20 52 65 74 75 72   a query.  Retur
31c0: 6e 20 74 68 65 20 66 69 72 73 74 20 63 6f 6c 75  n the first colu
31d0: 6d 6e 20 6f 66 20 74 68 65 20 66 69 72 73 74 20  mn of the first 
31e0: 72 6f 77 0a 2a 2a 20 6f 66 20 74 68 65 20 72 65  row.** of the re
31f0: 73 75 6c 74 20 73 65 74 20 61 73 20 61 20 73 74  sult set as a st
3200: 72 69 6e 67 2e 20 20 53 70 61 63 65 20 74 6f 20  ring.  Space to 
3210: 68 6f 6c 64 20 74 68 65 20 73 74 72 69 6e 67 20  hold the string 
3220: 69 73 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66  is.** obtained f
3230: 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 20 20 49  rom malloc().  I
3240: 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  f the result set
3250: 20 69 73 20 65 6d 70 74 79 2c 20 72 65 74 75 72   is empty, retur
3260: 6e 0a 2a 2a 20 7a 44 65 66 61 75 6c 74 20 69 6e  n.** zDefault in
3270: 73 74 65 61 64 2e 0a 2a 2f 0a 63 68 61 72 20 2a  stead..*/.char *
3280: 64 62 5f 74 65 78 74 28 63 68 61 72 20 2a 7a 44  db_text(char *zD
3290: 65 66 61 75 6c 74 2c 20 63 6f 6e 73 74 20 63 68  efault, const ch
32a0: 61 72 20 2a 7a 53 71 6c 2c 20 2e 2e 2e 29 7b 0a  ar *zSql, ...){.
32b0: 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20    va_list ap;.  
32c0: 53 74 6d 74 20 73 3b 0a 20 20 63 68 61 72 20 2a  Stmt s;.  char *
32d0: 7a 20 3d 20 7a 44 65 66 61 75 6c 74 3b 0a 20 20  z = zDefault;.  
32e0: 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 53 71  va_start(ap, zSq
32f0: 6c 29 3b 0a 20 20 64 62 5f 76 70 72 65 70 61 72  l);.  db_vprepar
3300: 65 28 26 73 2c 20 7a 53 71 6c 2c 20 61 70 29 3b  e(&s, zSql, ap);
3310: 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20  .  va_end(ap);. 
3320: 20 69 66 28 20 64 62 5f 73 74 65 70 28 26 73 29   if( db_step(&s)
3330: 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  ==SQLITE_ROW ){.
3340: 20 20 20 20 7a 20 3d 20 6d 70 72 69 6e 74 66 28      z = mprintf(
3350: 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 63 6f  "%s", sqlite3_co
3360: 6c 75 6d 6e 5f 74 65 78 74 28 73 2e 70 53 74 6d  lumn_text(s.pStm
3370: 74 2c 20 30 29 29 3b 0a 20 20 7d 0a 20 20 64 62  t, 0));.  }.  db
3380: 5f 66 69 6e 61 6c 69 7a 65 28 26 73 29 3b 0a 20  _finalize(&s);. 
3390: 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a   return z;.}../*
33a0: 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61  .** Initialize a
33b0: 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 66 69   new database fi
33c0: 6c 65 20 77 69 74 68 20 74 68 65 20 67 69 76 65  le with the give
33d0: 6e 20 73 63 68 65 6d 61 2e 20 20 49 66 20 61 6e  n schema.  If an
33e0: 79 74 68 69 6e 67 0a 2a 2a 20 67 6f 65 73 20 77  ything.** goes w
33f0: 72 6f 6e 67 2c 20 63 61 6c 6c 20 64 62 5f 65 72  rong, call db_er
3400: 72 28 29 20 74 6f 20 65 78 69 74 2e 0a 2a 2f 0a  r() to exit..*/.
3410: 76 6f 69 64 20 64 62 5f 69 6e 69 74 5f 64 61 74  void db_init_dat
3420: 61 62 61 73 65 28 0a 20 20 63 6f 6e 73 74 20 63  abase(.  const c
3430: 68 61 72 20 2a 7a 46 69 6c 65 4e 61 6d 65 2c 20  har *zFileName, 
3440: 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74    /* Name of dat
3450: 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 63 72  abase file to cr
3460: 65 61 74 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  eate */.  const 
3470: 63 68 61 72 20 2a 7a 53 63 68 65 6d 61 2c 20 20  char *zSchema,  
3480: 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 72 74     /* First part
3490: 20 6f 66 20 73 63 68 65 6d 61 20 2a 2f 0a 20 20   of schema */.  
34a0: 2e 2e 2e 20 20 20 20 20 20 20 20 20 20 20 20 20  ...             
34b0: 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 69           /* Addi
34c0: 74 69 6f 6e 61 6c 20 53 51 4c 20 74 6f 20 72 75  tional SQL to ru
34d0: 6e 2e 20 20 54 65 72 6d 69 6e 61 74 65 20 77 69  n.  Terminate wi
34e0: 74 68 20 4e 55 4c 4c 2e 20 2a 2f 0a 29 7b 0a 20  th NULL. */.){. 
34f0: 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20   sqlite3 *db;.  
3500: 69 6e 74 20 72 63 3b 0a 20 20 63 6f 6e 73 74 20  int rc;.  const 
3510: 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 76 61  char *zSql;.  va
3520: 5f 6c 69 73 74 20 61 70 3b 0a 0a 20 20 72 63 20  _list ap;..  rc 
3530: 3d 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 7a  = sqlite3_open(z
3540: 46 69 6c 65 4e 61 6d 65 2c 20 26 64 62 29 3b 0a  FileName, &db);.
3550: 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
3560: 5f 4f 4b 20 29 7b 0a 20 20 20 20 64 62 5f 65 72  _OK ){.    db_er
3570: 72 28 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  r(sqlite3_errmsg
3580: 28 64 62 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  (db));.  }.  sql
3590: 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 22 42  ite3_exec(db, "B
35a0: 45 47 49 4e 20 45 58 43 4c 55 53 49 56 45 22 2c  EGIN EXCLUSIVE",
35b0: 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 72 63 20   0, 0, 0);.  rc 
35c0: 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64  = sqlite3_exec(d
35d0: 62 2c 20 7a 53 63 68 65 6d 61 2c 20 30 2c 20 30  b, zSchema, 0, 0
35e0: 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  , 0);.  if( rc!=
35f0: 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
3600: 20 64 62 5f 65 72 72 28 73 71 6c 69 74 65 33 5f   db_err(sqlite3_
3610: 65 72 72 6d 73 67 28 64 62 29 29 3b 0a 20 20 7d  errmsg(db));.  }
3620: 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20  .  va_start(ap, 
3630: 7a 53 63 68 65 6d 61 29 3b 0a 20 20 77 68 69 6c  zSchema);.  whil
3640: 65 28 20 28 7a 53 71 6c 20 3d 20 76 61 5f 61 72  e( (zSql = va_ar
3650: 67 28 61 70 2c 20 63 6f 6e 73 74 20 63 68 61 72  g(ap, const char
3660: 2a 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 72 63  *))!=0 ){.    rc
3670: 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28   = sqlite3_exec(
3680: 64 62 2c 20 7a 53 71 6c 2c 20 30 2c 20 30 2c 20  db, zSql, 0, 0, 
3690: 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  0);.    if( rc!=
36a0: 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
36b0: 20 20 20 64 62 5f 65 72 72 28 73 71 6c 69 74 65     db_err(sqlite
36c0: 33 5f 65 72 72 6d 73 67 28 64 62 29 29 3b 0a 20  3_errmsg(db));. 
36d0: 20 20 20 7d 0a 20 20 7d 0a 20 20 76 61 5f 65 6e     }.  }.  va_en
36e0: 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  d(ap);.  sqlite3
36f0: 5f 65 78 65 63 28 64 62 2c 20 22 43 4f 4d 4d 49  _exec(db, "COMMI
3700: 54 22 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20  T", 0, 0, 0);.  
3710: 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 64 62  sqlite3_close(db
3720: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 7a 44 62 4e  );.}../*.** zDbN
3730: 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20  ame is the name 
3740: 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 66 69  of a database fi
3750: 6c 65 2e 20 20 49 66 20 6e 6f 20 6f 74 68 65 72  le.  If no other
3760: 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c   database.** fil
3770: 65 20 69 73 20 6f 70 65 6e 2c 20 74 68 65 6e 20  e is open, then 
3780: 6f 70 65 6e 20 74 68 69 73 20 6f 6e 65 2e 20 20  open this one.  
3790: 49 66 20 61 6e 6f 74 68 65 72 20 64 61 74 61 62  If another datab
37a0: 61 73 65 20 66 69 6c 65 20 69 73 0a 2a 2a 20 61  ase file is.** a
37b0: 6c 72 65 61 64 79 20 6f 70 65 6e 2c 20 74 68 65  lready open, the
37c0: 6e 20 61 74 74 61 63 68 20 7a 44 62 4e 61 6d 65  n attach zDbName
37d0: 20 75 73 69 6e 67 20 74 68 65 20 6e 61 6d 65 20   using the name 
37e0: 7a 4c 61 62 65 6c 2e 0a 2a 2f 0a 76 6f 69 64 20  zLabel..*/.void 
37f0: 64 62 5f 6f 70 65 6e 5f 6f 72 5f 61 74 74 61 63  db_open_or_attac
3800: 68 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44  h(const char *zD
3810: 62 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61  bName, const cha
3820: 72 20 2a 7a 4c 61 62 65 6c 29 7b 0a 20 20 69 66  r *zLabel){.  if
3830: 28 20 21 67 2e 64 62 20 29 7b 0a 20 20 20 20 69  ( !g.db ){.    i
3840: 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  nt rc = sqlite3_
3850: 6f 70 65 6e 28 7a 44 62 4e 61 6d 65 2c 20 26 67  open(zDbName, &g
3860: 2e 64 62 29 3b 0a 20 20 20 20 69 66 28 20 72 63  .db);.    if( rc
3870: 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
3880: 20 20 20 20 20 64 62 5f 65 72 72 28 73 71 6c 69       db_err(sqli
3890: 74 65 33 5f 65 72 72 6d 73 67 28 67 2e 64 62 29  te3_errmsg(g.db)
38a0: 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 5f  );.    }.    db_
38b0: 63 6f 6e 6e 65 63 74 69 6f 6e 5f 69 6e 69 74 28  connection_init(
38c0: 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
38d0: 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 41  db_multi_exec("A
38e0: 54 54 41 43 48 20 44 41 54 41 42 41 53 45 20 25  TTACH DATABASE %
38f0: 51 20 41 53 20 25 73 22 2c 20 7a 44 62 4e 61 6d  Q AS %s", zDbNam
3900: 65 2c 20 7a 4c 61 62 65 6c 29 3b 0a 20 20 7d 0a  e, zLabel);.  }.
3910: 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68  }../*.** Open th
3920: 65 20 75 73 65 72 20 64 61 74 61 62 61 73 65 20  e user database 
3930: 69 6e 20 22 7e 2f 2e 66 6f 73 73 69 6c 22 2e 20  in "~/.fossil". 
3940: 20 43 72 65 61 74 65 20 74 68 65 20 64 61 74 61   Create the data
3950: 62 61 73 65 20 61 6e 65 77 20 69 66 0a 2a 2a 20  base anew if.** 
3960: 69 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65  it does not alre
3970: 61 64 79 20 65 78 69 73 74 2e 0a 2a 2f 0a 76 6f  ady exist..*/.vo
3980: 69 64 20 64 62 5f 6f 70 65 6e 5f 63 6f 6e 66 69  id db_open_confi
3990: 67 28 76 6f 69 64 29 7b 0a 20 20 63 68 61 72 20  g(void){.  char 
39a0: 2a 7a 44 62 4e 61 6d 65 3b 0a 20 20 63 6f 6e 73  *zDbName;.  cons
39b0: 74 20 63 68 61 72 20 2a 7a 48 6f 6d 65 3b 0a 23  t char *zHome;.#
39c0: 69 66 64 65 66 20 5f 5f 4d 49 4e 47 57 33 32 5f  ifdef __MINGW32_
39d0: 5f 0a 20 20 7a 48 6f 6d 65 20 3d 20 67 65 74 65  _.  zHome = gete
39e0: 6e 76 28 22 4c 4f 43 41 4c 41 50 50 44 41 54 41  nv("LOCALAPPDATA
39f0: 22 29 3b 0a 20 20 69 66 28 20 7a 48 6f 6d 65 3d  ");.  if( zHome=
3a00: 3d 30 20 29 7b 0a 20 20 20 20 7a 48 6f 6d 65 20  =0 ){.    zHome 
3a10: 3d 20 67 65 74 65 6e 76 28 22 41 50 50 44 41 54  = getenv("APPDAT
3a20: 41 22 29 3b 0a 20 20 20 20 69 66 28 20 7a 48 6f  A");.    if( zHo
3a30: 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 7a  me==0 ){.      z
3a40: 48 6f 6d 65 20 3d 20 67 65 74 65 6e 76 28 22 48  Home = getenv("H
3a50: 4f 4d 45 50 41 54 48 22 29 3b 0a 20 20 20 20 7d  OMEPATH");.    }
3a60: 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 7a 48 6f  .  }.#else.  zHo
3a70: 6d 65 20 3d 20 67 65 74 65 6e 76 28 22 48 4f 4d  me = getenv("HOM
3a80: 45 22 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66  E");.#endif.  if
3a90: 28 20 7a 48 6f 6d 65 3d 3d 30 20 29 7b 0a 20 20  ( zHome==0 ){.  
3aa0: 20 20 64 62 5f 65 72 72 28 22 63 61 6e 6e 6f 74    db_err("cannot
3ab0: 20 6c 6f 63 61 6c 20 68 6f 6d 65 20 64 69 72 65   local home dire
3ac0: 63 74 6f 72 79 22 29 3b 0a 20 20 7d 0a 23 69 66  ctory");.  }.#if
3ad0: 64 65 66 20 5f 5f 4d 49 4e 47 57 33 32 5f 5f 0a  def __MINGW32__.
3ae0: 20 20 2f 2a 20 2e 20 66 69 6c 65 6e 61 6d 65 73    /* . filenames
3af0: 20 67 69 76 65 20 73 6f 6d 65 20 77 69 6e 64 6f   give some windo
3b00: 77 20 73 79 73 74 65 6d 73 20 70 72 6f 62 6c 65  w systems proble
3b10: 6d 73 20 61 6e 64 20 6d 61 6e 79 20 61 70 70 73  ms and many apps
3b20: 20 70 72 6f 62 6c 65 6d 73 20 2a 2f 0a 20 20 7a   problems */.  z
3b30: 44 62 4e 61 6d 65 20 3d 20 6d 70 72 69 6e 74 66  DbName = mprintf
3b40: 28 22 25 2f 2f 5f 66 6f 73 73 69 6c 22 2c 20 7a  ("%//_fossil", z
3b50: 48 6f 6d 65 29 3b 0a 23 65 6c 73 65 0a 20 20 7a  Home);.#else.  z
3b60: 44 62 4e 61 6d 65 20 3d 20 6d 70 72 69 6e 74 66  DbName = mprintf
3b70: 28 22 25 73 2f 2e 66 6f 73 73 69 6c 22 2c 20 7a  ("%s/.fossil", z
3b80: 48 6f 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20  Home);.#endif.  
3b90: 69 66 28 20 67 2e 63 6f 6e 66 69 67 4f 70 65 6e  if( g.configOpen
3ba0: 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
3bb0: 20 66 69 6c 65 5f 73 69 7a 65 28 7a 44 62 4e 61   file_size(zDbNa
3bc0: 6d 65 29 3c 31 30 32 34 2a 33 20 29 7b 0a 20 20  me)<1024*3 ){.  
3bd0: 20 20 64 62 5f 69 6e 69 74 5f 64 61 74 61 62 61    db_init_databa
3be0: 73 65 28 7a 44 62 4e 61 6d 65 2c 20 7a 43 6f 6e  se(zDbName, zCon
3bf0: 66 69 67 53 63 68 65 6d 61 2c 20 28 63 68 61 72  figSchema, (char
3c00: 2a 29 30 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 6f  *)0);.  }.  db_o
3c10: 70 65 6e 5f 6f 72 5f 61 74 74 61 63 68 28 7a 44  pen_or_attach(zD
3c20: 62 4e 61 6d 65 2c 20 22 63 6f 6e 66 69 67 64 62  bName, "configdb
3c30: 22 29 3b 0a 20 20 67 2e 63 6f 6e 66 69 67 4f 70  ");.  g.configOp
3c40: 65 6e 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  en = 1;.}../*.**
3c50: 20 49 66 20 7a 44 62 4e 61 6d 65 20 69 73 20 61   If zDbName is a
3c60: 20 76 61 6c 69 64 20 6c 6f 63 61 6c 20 64 61 74   valid local dat
3c70: 61 62 61 73 65 20 66 69 6c 65 2c 20 6f 70 65 6e  abase file, open
3c80: 20 69 74 20 61 6e 64 20 72 65 74 75 72 6e 0a 2a   it and return.*
3c90: 2a 20 74 72 75 65 2e 20 20 49 66 20 69 74 20 69  * true.  If it i
3ca0: 73 20 6e 6f 74 20 61 20 76 61 6c 69 64 20 6c 6f  s not a valid lo
3cb0: 63 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c  cal database fil
3cc0: 65 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a  e, return 0..*/.
3cd0: 73 74 61 74 69 63 20 69 6e 74 20 69 73 56 61 6c  static int isVal
3ce0: 69 64 4c 6f 63 61 6c 44 62 28 63 6f 6e 73 74 20  idLocalDb(const 
3cf0: 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 29 7b 0a  char *zDbName){.
3d00: 20 20 69 36 34 20 6c 73 69 7a 65 3b 0a 20 20 69    i64 lsize;.  i
3d10: 66 28 20 61 63 63 65 73 73 28 7a 44 62 4e 61 6d  f( access(zDbNam
3d20: 65 2c 20 46 5f 4f 4b 29 20 29 20 72 65 74 75 72  e, F_OK) ) retur
3d30: 6e 20 30 3b 0a 20 20 6c 73 69 7a 65 20 3d 20 66  n 0;.  lsize = f
3d40: 69 6c 65 5f 73 69 7a 65 28 7a 44 62 4e 61 6d 65  ile_size(zDbName
3d50: 29 3b 0a 20 20 69 66 28 20 6c 73 69 7a 65 25 31  );.  if( lsize%1
3d60: 30 32 34 21 3d 30 20 7c 7c 20 6c 73 69 7a 65 3c  024!=0 || lsize<
3d70: 34 30 39 36 20 29 20 72 65 74 75 72 6e 20 30 3b  4096 ) return 0;
3d80: 0a 20 20 64 62 5f 6f 70 65 6e 5f 6f 72 5f 61 74  .  db_open_or_at
3d90: 74 61 63 68 28 7a 44 62 4e 61 6d 65 2c 20 22 6c  tach(zDbName, "l
3da0: 6f 63 61 6c 64 62 22 29 3b 0a 20 20 67 2e 6c 6f  ocaldb");.  g.lo
3db0: 63 61 6c 4f 70 65 6e 20 3d 20 31 3b 0a 20 20 64  calOpen = 1;.  d
3dc0: 62 5f 6f 70 65 6e 5f 63 6f 6e 66 69 67 28 29 3b  b_open_config();
3dd0: 0a 20 20 64 62 5f 6f 70 65 6e 5f 72 65 70 6f 73  .  db_open_repos
3de0: 69 74 6f 72 79 28 30 29 3b 0a 20 20 72 65 74 75  itory(0);.  retu
3df0: 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  rn 1;.}../*.** L
3e00: 6f 63 61 74 65 20 74 68 65 20 72 6f 6f 74 20 64  ocate the root d
3e10: 69 72 65 63 74 6f 72 79 20 6f 66 20 74 68 65 20  irectory of the 
3e20: 6c 6f 63 61 6c 20 72 65 70 6f 73 69 74 6f 72 79  local repository
3e30: 20 74 72 65 65 2e 20 20 54 68 65 20 72 6f 6f 74   tree.  The root
3e40: 0a 2a 2a 20 64 69 72 65 63 74 6f 72 79 20 69 73  .** directory is
3e50: 20 66 6f 75 6e 64 20 62 79 20 73 65 61 72 63 68   found by search
3e60: 69 6e 67 20 66 6f 72 20 61 20 66 69 6c 65 20 6e  ing for a file n
3e70: 61 6d 65 64 20 22 46 4f 53 53 49 4c 22 20 74 68  amed "FOSSIL" th
3e80: 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61  at contains.** a
3e90: 20 76 61 6c 69 64 20 72 65 70 6f 73 69 74 6f 72   valid repositor
3ea0: 79 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a  y database..**.*
3eb0: 2a 20 49 66 20 6e 6f 20 76 61 6c 69 64 20 46 4f  * If no valid FO
3ec0: 53 53 49 4c 20 66 69 6c 65 20 69 73 20 66 6f 75  SSIL file is fou
3ed0: 6e 64 2c 20 77 65 20 6d 6f 76 65 20 75 70 20 6f  nd, we move up o
3ee0: 6e 65 20 6c 65 76 65 6c 20 61 6e 64 20 74 72 79  ne level and try
3ef0: 20 61 67 61 69 6e 2e 0a 2a 2a 20 4f 6e 63 65 20   again..** Once 
3f00: 74 68 65 20 66 69 6c 65 20 69 73 20 66 6f 75 6e  the file is foun
3f10: 64 2c 20 74 68 65 20 67 2e 7a 4c 6f 63 61 6c 52  d, the g.zLocalR
3f20: 6f 6f 74 20 76 61 72 69 61 62 6c 65 20 69 73 20  oot variable is 
3f30: 73 65 74 20 74 6f 20 74 68 65 20 72 6f 6f 74 20  set to the root 
3f40: 6f 66 0a 2a 2a 20 74 68 65 20 72 65 70 6f 73 69  of.** the reposi
3f50: 74 6f 72 79 20 74 72 65 65 20 61 6e 64 20 74 68  tory tree and th
3f60: 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
3f70: 6e 73 20 31 2e 20 20 49 66 20 6e 6f 20 64 61 74  ns 1.  If no dat
3f80: 61 62 61 73 65 20 69 73 0a 2a 2a 20 66 6f 75 6e  abase is.** foun
3f90: 64 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  d, then this rou
3fa0: 74 69 6e 65 20 72 65 74 75 72 6e 20 30 2e 0a 2a  tine return 0..*
3fb0: 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
3fc0: 65 20 61 6c 77 61 79 73 20 6f 70 65 6e 73 20 74  e always opens t
3fd0: 68 65 20 75 73 65 72 20 64 61 74 61 62 61 73 65  he user database
3fe0: 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77   regardless of w
3ff0: 68 65 74 68 65 72 20 6f 72 0a 2a 2a 20 6e 6f 74  hether or.** not
4000: 20 74 68 65 20 72 65 70 6f 73 69 74 6f 72 79 20   the repository 
4010: 64 61 74 61 62 61 73 65 20 69 73 20 66 6f 75 6e  database is foun
4020: 64 2e 20 20 49 66 20 74 68 65 20 46 4f 53 53 49  d.  If the FOSSI
4030: 4c 20 66 69 6c 65 20 69 73 20 66 6f 75 6e 64 2c  L file is found,
4040: 0a 2a 2a 20 69 74 20 69 73 20 61 74 74 61 63 68  .** it is attach
4050: 65 64 20 74 6f 20 74 68 65 20 6f 70 65 6e 20 64  ed to the open d
4060: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
4070: 6f 6e 20 74 6f 6f 2e 0a 2a 2f 0a 69 6e 74 20 64  on too..*/.int d
4080: 62 5f 6f 70 65 6e 5f 6c 6f 63 61 6c 28 76 6f 69  b_open_local(voi
4090: 64 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 63  d){.  int n;.  c
40a0: 68 61 72 20 7a 50 77 64 5b 32 30 30 30 5d 3b 0a  har zPwd[2000];.
40b0: 20 20 63 68 61 72 20 2a 7a 50 77 64 43 6f 6e 76    char *zPwdConv
40c0: 3b 0a 20 20 69 66 28 20 67 2e 6c 6f 63 61 6c 4f  ;.  if( g.localO
40d0: 70 65 6e 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  pen) return 1;. 
40e0: 20 69 66 28 20 67 65 74 63 77 64 28 7a 50 77 64   if( getcwd(zPwd
40f0: 2c 20 73 69 7a 65 6f 66 28 7a 50 77 64 29 2d 32  , sizeof(zPwd)-2
4100: 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62 5f  0)==0 ){.    db_
4110: 65 72 72 28 22 70 77 64 20 74 6f 6f 20 62 69 67  err("pwd too big
4120: 3a 20 6d 61 78 20 25 64 22 2c 20 73 69 7a 65 6f  : max %d", sizeo
4130: 66 28 7a 50 77 64 29 2d 32 30 29 3b 0a 20 20 7d  f(zPwd)-20);.  }
4140: 0a 20 20 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 50  .  n = strlen(zP
4150: 77 64 29 3b 0a 20 20 7a 50 77 64 43 6f 6e 76 20  wd);.  zPwdConv 
4160: 3d 20 6d 70 72 69 6e 74 66 28 22 25 2f 22 2c 20  = mprintf("%/", 
4170: 7a 50 77 64 29 3b 0a 20 20 73 74 72 6e 63 70 79  zPwd);.  strncpy
4180: 28 7a 50 77 64 2c 20 7a 50 77 64 43 6f 6e 76 2c  (zPwd, zPwdConv,
4190: 20 32 30 30 30 2d 32 30 29 3b 0a 20 20 66 72 65   2000-20);.  fre
41a0: 65 28 7a 50 77 64 43 6f 6e 76 29 3b 0a 20 20 77  e(zPwdConv);.  w
41b0: 68 69 6c 65 28 20 6e 3e 30 20 29 7b 0a 20 20 20  hile( n>0 ){.   
41c0: 20 69 66 28 20 61 63 63 65 73 73 28 7a 50 77 64   if( access(zPwd
41d0: 2c 20 57 5f 4f 4b 29 20 29 20 62 72 65 61 6b 3b  , W_OK) ) break;
41e0: 0a 20 20 20 20 73 74 72 63 70 79 28 26 7a 50 77  .    strcpy(&zPw
41f0: 64 5b 6e 5d 2c 20 22 2f 5f 46 4f 53 53 49 4c 5f  d[n], "/_FOSSIL_
4200: 22 29 3b 0a 20 20 20 20 69 66 28 20 69 73 56 61  ");.    if( isVa
4210: 6c 69 64 4c 6f 63 61 6c 44 62 28 7a 50 77 64 29  lidLocalDb(zPwd)
4220: 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 46 6f 75   ){.      /* Fou
4230: 6e 64 20 61 20 76 61 6c 69 64 20 5f 46 4f 53 53  nd a valid _FOSS
4240: 49 4c 5f 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20  IL_ file */.    
4250: 20 20 7a 50 77 64 5b 6e 5d 20 3d 20 30 3b 0a 20    zPwd[n] = 0;. 
4260: 20 20 20 20 20 67 2e 7a 4c 6f 63 61 6c 52 6f 6f       g.zLocalRoo
4270: 74 20 3d 20 6d 70 72 69 6e 74 66 28 22 25 73 2f  t = mprintf("%s/
4280: 22 2c 20 7a 50 77 64 29 3b 0a 20 20 20 20 20 20  ", zPwd);.      
4290: 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
42a0: 20 20 20 20 6e 2d 2d 3b 0a 20 20 20 20 77 68 69      n--;.    whi
42b0: 6c 65 28 20 6e 3e 30 20 26 26 20 7a 50 77 64 5b  le( n>0 && zPwd[
42c0: 6e 5d 21 3d 27 2f 27 20 29 7b 20 6e 2d 2d 3b 20  n]!='/' ){ n--; 
42d0: 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 3e 30  }.    while( n>0
42e0: 20 26 26 20 7a 50 77 64 5b 6e 2d 31 5d 3d 3d 27   && zPwd[n-1]=='
42f0: 2f 27 20 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 20  /' ){ n--; }.   
4300: 20 7a 50 77 64 5b 6e 5d 20 3d 20 30 3b 0a 20 20   zPwd[n] = 0;.  
4310: 7d 0a 0a 20 20 2f 2a 20 41 20 5f 46 4f 53 53 49  }..  /* A _FOSSI
4320: 4c 5f 20 66 69 6c 65 20 63 6f 75 6c 64 20 6e 6f  L_ file could no
4330: 74 20 62 65 20 66 6f 75 6e 64 20 2a 2f 0a 20 20  t be found */.  
4340: 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
4350: 2a 2a 20 4f 70 65 6e 20 74 68 65 20 72 65 70 6f  ** Open the repo
4360: 73 69 74 6f 72 79 20 64 61 74 61 62 61 73 65 20  sitory database 
4370: 67 69 76 65 6e 20 62 79 20 7a 44 62 4e 61 6d 65  given by zDbName
4380: 2e 20 20 49 66 20 7a 44 62 4e 61 6d 65 3d 3d 4e  .  If zDbName==N
4390: 55 4c 4c 20 74 68 65 6e 0a 2a 2a 20 67 65 74 20  ULL then.** get 
43a0: 74 68 65 20 6e 61 6d 65 20 66 72 6f 6d 20 74 68  the name from th
43b0: 65 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 6c  e already open l
43c0: 6f 63 61 6c 20 64 61 74 61 62 61 73 65 2e 0a 2a  ocal database..*
43d0: 2f 0a 76 6f 69 64 20 64 62 5f 6f 70 65 6e 5f 72  /.void db_open_r
43e0: 65 70 6f 73 69 74 6f 72 79 28 63 6f 6e 73 74 20  epository(const 
43f0: 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 29 7b 0a  char *zDbName){.
4400: 20 20 69 66 28 20 67 2e 72 65 70 6f 73 69 74 6f    if( g.reposito
4410: 72 79 4f 70 65 6e 20 29 20 72 65 74 75 72 6e 3b  ryOpen ) return;
4420: 0a 20 20 69 66 28 20 7a 44 62 4e 61 6d 65 3d 3d  .  if( zDbName==
4430: 30 20 29 7b 0a 20 20 20 20 69 66 28 20 67 2e 6c  0 ){.    if( g.l
4440: 6f 63 61 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20  ocalOpen ){.    
4450: 20 20 7a 44 62 4e 61 6d 65 20 3d 20 64 62 5f 6c    zDbName = db_l
4460: 67 65 74 28 22 72 65 70 6f 73 69 74 6f 72 79 22  get("repository"
4470: 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , 0);.    }.    
4480: 69 66 28 20 7a 44 62 4e 61 6d 65 3d 3d 30 20 29  if( zDbName==0 )
4490: 7b 0a 20 20 20 20 20 20 64 62 5f 65 72 72 28 22  {.      db_err("
44a0: 75 6e 61 62 6c 65 20 74 6f 20 66 69 6e 64 20 74  unable to find t
44b0: 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 72 65 70  he name of a rep
44c0: 6f 73 69 74 6f 72 79 20 64 61 74 61 62 61 73 65  ository database
44d0: 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ");.    }.  }.  
44e0: 69 66 28 20 61 63 63 65 73 73 28 7a 44 62 4e 61  if( access(zDbNa
44f0: 6d 65 2c 20 52 5f 4f 4b 29 20 7c 7c 20 66 69 6c  me, R_OK) || fil
4500: 65 5f 73 69 7a 65 28 7a 44 62 4e 61 6d 65 29 3c  e_size(zDbName)<
4510: 31 30 32 34 20 29 7b 0a 20 20 20 20 69 66 28 20  1024 ){.    if( 
4520: 61 63 63 65 73 73 28 7a 44 62 4e 61 6d 65 2c 20  access(zDbName, 
4530: 30 29 20 29 7b 0a 20 20 20 20 20 20 66 6f 73 73  0) ){.      foss
4540: 69 6c 5f 70 61 6e 69 63 28 22 72 65 70 6f 73 69  il_panic("reposi
4550: 74 6f 72 79 20 64 6f 65 73 20 6e 6f 74 20 65 78  tory does not ex
4560: 69 73 74 73 20 6f 72 22 0a 20 20 20 20 20 20 20  ists or".       
4570: 20 20 20 20 20 20 20 20 20 20 20 20 22 20 69 73              " is
4580: 20 69 6e 20 61 6e 20 75 6e 72 65 61 64 61 62 6c   in an unreadabl
4590: 65 20 64 69 72 65 63 74 6f 72 79 3a 20 25 73 22  e directory: %s"
45a0: 2c 20 7a 44 62 4e 61 6d 65 29 3b 0a 20 20 20 20  , zDbName);.    
45b0: 7d 65 6c 73 65 20 69 66 28 20 61 63 63 65 73 73  }else if( access
45c0: 28 7a 44 62 4e 61 6d 65 2c 20 52 5f 4f 4b 29 20  (zDbName, R_OK) 
45d0: 29 7b 0a 20 20 20 20 20 20 66 6f 73 73 69 6c 5f  ){.      fossil_
45e0: 70 61 6e 69 63 28 22 72 65 61 64 20 70 65 72 6d  panic("read perm
45f0: 69 73 73 69 6f 6e 20 64 65 6e 69 65 64 20 66 6f  ission denied fo
4600: 72 20 72 65 70 6f 73 69 74 6f 72 79 20 25 73 22  r repository %s"
4610: 2c 20 7a 44 62 4e 61 6d 65 29 3b 0a 20 20 20 20  , zDbName);.    
4620: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 6f 73  }else{.      fos
4630: 73 69 6c 5f 70 61 6e 69 63 28 22 6e 6f 74 20 61  sil_panic("not a
4640: 20 76 61 6c 69 64 20 72 65 70 6f 73 69 74 6f 72   valid repositor
4650: 79 3a 20 25 73 22 2c 20 7a 44 62 4e 61 6d 65 29  y: %s", zDbName)
4660: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64 62  ;.    }.  }.  db
4670: 5f 6f 70 65 6e 5f 6f 72 5f 61 74 74 61 63 68 28  _open_or_attach(
4680: 7a 44 62 4e 61 6d 65 2c 20 22 72 65 70 6f 73 69  zDbName, "reposi
4690: 74 6f 72 79 22 29 3b 0a 20 20 67 2e 72 65 70 6f  tory");.  g.repo
46a0: 73 69 74 6f 72 79 4f 70 65 6e 20 3d 20 31 3b 0a  sitoryOpen = 1;.
46b0: 20 20 67 2e 7a 52 65 70 6f 73 69 74 6f 72 79 4e    g.zRepositoryN
46c0: 61 6d 65 20 3d 20 6d 70 72 69 6e 74 66 28 22 25  ame = mprintf("%
46d0: 73 22 2c 20 7a 44 62 4e 61 6d 65 29 3b 0a 7d 0a  s", zDbName);.}.
46e0: 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 66 69  ./*.** Try to fi
46f0: 6e 64 20 74 68 65 20 72 65 70 6f 73 69 74 6f 72  nd the repositor
4700: 79 20 61 6e 64 20 6f 70 65 6e 20 69 74 2e 20 20  y and open it.  
4710: 55 73 65 20 74 68 65 20 2d 52 20 6f 72 20 2d 2d  Use the -R or --
4720: 72 65 70 6f 73 69 74 6f 72 79 0a 2a 2a 20 6f 70  repository.** op
4730: 74 69 6f 6e 20 74 6f 20 6c 6f 63 61 74 65 20 74  tion to locate t
4740: 68 65 20 72 65 70 6f 73 69 74 6f 72 79 2e 20 20  he repository.  
4750: 49 66 20 6e 6f 20 73 75 63 68 20 6f 70 74 69 6f  If no such optio
4760: 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20  n is available, 
4770: 74 68 65 6e 0a 2a 2a 20 75 73 65 20 74 68 65 20  then.** use the 
4780: 72 65 70 6f 73 69 74 6f 72 79 20 6f 66 20 74 68  repository of th
4790: 65 20 6f 70 65 6e 20 63 68 65 63 6b 6f 75 74 20  e open checkout 
47a0: 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 2e  if there is one.
47b0: 0a 2a 2a 0a 2a 2a 20 45 72 72 6f 72 20 6f 75 74  .**.** Error out
47c0: 20 69 66 20 74 68 65 20 72 65 70 6f 73 69 74 6f   if the reposito
47d0: 72 79 20 63 61 6e 6e 6f 74 20 62 65 20 6f 70 65  ry cannot be ope
47e0: 6e 65 64 2e 0a 2a 2f 0a 76 6f 69 64 20 64 62 5f  ned..*/.void db_
47f0: 66 69 6e 64 5f 61 6e 64 5f 6f 70 65 6e 5f 72 65  find_and_open_re
4800: 70 6f 73 69 74 6f 72 79 28 69 6e 74 20 65 72 72  pository(int err
4810: 49 66 4e 6f 74 46 6f 75 6e 64 29 7b 0a 20 20 63  IfNotFound){.  c
4820: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 70 20  onst char *zRep 
4830: 3d 20 66 69 6e 64 5f 6f 70 74 69 6f 6e 28 22 72  = find_option("r
4840: 65 70 6f 73 69 74 6f 72 79 22 2c 20 22 52 22 2c  epository", "R",
4850: 20 31 29 3b 0a 20 20 69 66 28 20 7a 52 65 70 3d   1);.  if( zRep=
4860: 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62  =0 ){.    if( db
4870: 5f 6f 70 65 6e 5f 6c 6f 63 61 6c 28 29 3d 3d 30  _open_local()==0
4880: 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 72   ){.      goto r
4890: 65 70 5f 6e 6f 74 5f 66 6f 75 6e 64 3b 0a 20 20  ep_not_found;.  
48a0: 20 20 7d 0a 20 20 20 20 7a 52 65 70 20 3d 20 64    }.    zRep = d
48b0: 62 5f 6c 67 65 74 28 22 72 65 70 6f 73 69 74 6f  b_lget("reposito
48c0: 72 79 22 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  ry", 0);.    if(
48d0: 20 7a 52 65 70 3d 3d 30 20 29 7b 0a 20 20 20 20   zRep==0 ){.    
48e0: 20 20 67 6f 74 6f 20 72 65 70 5f 6e 6f 74 5f 66    goto rep_not_f
48f0: 6f 75 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ound;.    }.  }.
4900: 20 20 64 62 5f 6f 70 65 6e 5f 72 65 70 6f 73 69    db_open_reposi
4910: 74 6f 72 79 28 7a 52 65 70 29 3b 0a 20 20 69 66  tory(zRep);.  if
4920: 28 20 67 2e 72 65 70 6f 73 69 74 6f 72 79 4f 70  ( g.repositoryOp
4930: 65 6e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  en ){.    return
4940: 3b 0a 20 20 7d 0a 72 65 70 5f 6e 6f 74 5f 66 6f  ;.  }.rep_not_fo
4950: 75 6e 64 3a 0a 20 20 69 66 28 20 65 72 72 49 66  und:.  if( errIf
4960: 4e 6f 74 46 6f 75 6e 64 20 29 7b 0a 20 20 20 20  NotFound ){.    
4970: 66 6f 73 73 69 6c 5f 66 61 74 61 6c 28 22 75 73  fossil_fatal("us
4980: 65 20 2d 2d 72 65 70 6f 73 69 74 6f 72 79 20 6f  e --repository o
4990: 72 20 2d 52 20 74 6f 20 73 70 65 63 69 66 69 63  r -R to specific
49a0: 20 74 68 65 20 72 65 70 6f 73 69 74 6f 72 79 20   the repository 
49b0: 64 61 74 61 62 61 73 65 22 29 3b 0a 20 20 7d 0a  database");.  }.
49c0: 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68  }../*.** Open th
49d0: 65 20 6c 6f 63 61 6c 20 64 61 74 61 62 61 73 65  e local database
49e0: 2e 20 20 49 66 20 75 6e 61 62 6c 65 2c 20 65 78  .  If unable, ex
49f0: 69 74 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72  it with an error
4a00: 2e 0a 2a 2f 0a 76 6f 69 64 20 64 62 5f 6d 75 73  ..*/.void db_mus
4a10: 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72 65 65  t_be_within_tree
4a20: 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 64 62  (void){.  if( db
4a30: 5f 6f 70 65 6e 5f 6c 6f 63 61 6c 28 29 3d 3d 30  _open_local()==0
4a40: 20 29 7b 0a 20 20 20 20 66 6f 73 73 69 6c 5f 66   ){.    fossil_f
4a50: 61 74 61 6c 28 22 6e 6f 74 20 77 69 74 68 69 6e  atal("not within
4a60: 20 61 6e 20 6f 70 65 6e 20 63 68 65 63 6b 6f 75   an open checkou
4a70: 74 22 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 6f 70  t");.  }.  db_op
4a80: 65 6e 5f 72 65 70 6f 73 69 74 6f 72 79 28 30 29  en_repository(0)
4a90: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  ;.}../*.** Close
4aa0: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
4ab0: 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69  nnection..*/.voi
4ac0: 64 20 64 62 5f 63 6c 6f 73 65 28 76 6f 69 64 29  d db_close(void)
4ad0: 7b 0a 20 20 69 66 28 20 67 2e 64 62 3d 3d 30 20  {.  if( g.db==0 
4ae0: 29 20 72 65 74 75 72 6e 3b 0a 20 20 77 68 69 6c  ) return;.  whil
4af0: 65 28 20 70 41 6c 6c 53 74 6d 74 20 29 7b 0a 20  e( pAllStmt ){. 
4b00: 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 70     db_finalize(p
4b10: 41 6c 6c 53 74 6d 74 29 3b 0a 20 20 7d 0a 20 20  AllStmt);.  }.  
4b20: 67 2e 72 65 70 6f 73 69 74 6f 72 79 4f 70 65 6e  g.repositoryOpen
4b30: 20 3d 20 30 3b 0a 20 20 67 2e 6c 6f 63 61 6c 4f   = 0;.  g.localO
4b40: 70 65 6e 20 3d 20 30 3b 0a 20 20 67 2e 63 6f 6e  pen = 0;.  g.con
4b50: 66 69 67 4f 70 65 6e 20 3d 20 30 3b 0a 20 20 73  figOpen = 0;.  s
4b60: 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 67 2e 64  qlite3_close(g.d
4b70: 62 29 3b 0a 20 20 67 2e 64 62 20 3d 20 30 3b 0a  b);.  g.db = 0;.
4b80: 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  }.../*.** Create
4b90: 20 61 20 6e 65 77 20 65 6d 70 74 79 20 72 65 70   a new empty rep
4ba0: 6f 73 69 74 6f 72 79 20 64 61 74 61 62 61 73 65  ository database
4bb0: 20 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20   with the given 
4bc0: 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79  name..**.** Only
4bd0: 20 74 68 65 20 73 63 68 65 6d 61 20 69 73 20 69   the schema is i
4be0: 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 54 68 65  nitialized.  The
4bf0: 20 72 65 71 75 69 72 65 64 20 56 41 52 20 74 61   required VAR ta
4c00: 62 6c 65 73 20 65 6e 74 72 69 65 73 0a 2a 2a 20  bles entries.** 
4c10: 61 72 65 20 6e 6f 74 20 73 65 74 20 62 79 20 74  are not set by t
4c20: 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6e 64 20  his routine and 
4c30: 6d 75 73 74 20 62 65 20 73 65 74 20 73 65 70 61  must be set sepa
4c40: 72 61 74 65 6c 79 20 69 6e 20 6f 72 64 65 72 0a  rately in order.
4c50: 2a 2a 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 6e  ** to make the n
4c60: 65 77 20 66 69 6c 65 20 61 20 76 61 6c 69 64 20  ew file a valid 
4c70: 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 76 6f 69  database..*/.voi
4c80: 64 20 64 62 5f 63 72 65 61 74 65 5f 72 65 70 6f  d db_create_repo
4c90: 73 69 74 6f 72 79 28 63 6f 6e 73 74 20 63 68 61  sitory(const cha
4ca0: 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20  r *zFilename){. 
4cb0: 20 64 62 5f 69 6e 69 74 5f 64 61 74 61 62 61 73   db_init_databas
4cc0: 65 28 0a 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d  e(.     zFilenam
4cd0: 65 2c 0a 20 20 20 20 20 7a 52 65 70 6f 73 69 74  e,.     zReposit
4ce0: 6f 72 79 53 63 68 65 6d 61 31 2c 0a 20 20 20 20  orySchema1,.    
4cf0: 20 7a 52 65 70 6f 73 69 74 6f 72 79 53 63 68 65   zRepositorySche
4d00: 6d 61 32 2c 0a 20 20 20 20 20 28 63 68 61 72 2a  ma2,.     (char*
4d10: 29 30 0a 20 20 29 3b 0a 20 20 69 73 4e 65 77 52  )0.  );.  isNewR
4d20: 65 70 6f 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  epo = 1;.}../*.*
4d30: 2a 20 46 69 6c 6c 20 61 6e 20 65 6d 70 74 79 20  * Fill an empty 
4d40: 72 65 70 6f 73 69 74 6f 72 79 20 64 61 74 61 62  repository datab
4d50: 61 73 65 20 77 69 74 68 20 74 68 65 20 62 61 73  ase with the bas
4d60: 69 63 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  ic information f
4d70: 6f 72 20 61 0a 2a 2a 20 72 65 70 6f 73 69 74 6f  or a.** reposito
4d80: 72 79 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ry. This functio
4d90: 6e 20 69 73 20 73 68 61 72 65 64 20 62 65 74 77  n is shared betw
4da0: 65 65 6e 20 27 63 72 65 61 74 65 5f 72 65 70 6f  een 'create_repo
4db0: 73 69 74 6f 72 79 5f 63 6d 64 27 0a 2a 2a 20 28  sitory_cmd'.** (
4dc0: 27 6e 65 77 27 29 20 61 6e 64 20 27 72 65 63 6f  'new') and 'reco
4dd0: 6e 73 74 72 75 63 74 5f 63 6d 64 27 20 28 27 72  nstruct_cmd' ('r
4de0: 65 63 6f 6e 73 74 72 75 63 74 27 29 2c 20 62 6f  econstruct'), bo
4df0: 74 68 20 6f 66 20 77 68 69 63 68 20 63 72 65 61  th of which crea
4e00: 74 65 0a 2a 2a 20 6e 65 77 20 72 65 70 6f 73 69  te.** new reposi
4e10: 74 6f 72 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  tories..**.** Th
4e20: 65 20 6d 61 6b 65 49 6e 69 74 69 61 6c 56 65 72  e makeInitialVer
4e30: 73 69 6f 6e 20 66 6c 61 67 20 64 65 74 65 72 6d  sion flag determ
4e40: 69 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 20  ines whether or 
4e50: 6e 6f 74 20 61 6e 20 69 6e 69 74 69 61 6c 0a 2a  not an initial.*
4e60: 2a 20 6d 61 6e 69 66 65 73 74 20 69 73 20 63 72  * manifest is cr
4e70: 65 61 74 65 64 2e 20 20 54 68 65 20 6d 61 6b 65  eated.  The make
4e80: 53 65 72 76 65 72 43 6f 64 65 73 20 66 6c 61 67  ServerCodes flag
4e90: 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74   determines whet
4ea0: 68 65 72 20 6f 72 0a 2a 2a 20 6e 6f 74 20 73 65  her or.** not se
4eb0: 72 76 65 72 20 61 6e 64 20 70 72 6f 6a 65 63 74  rver and project
4ec0: 20 63 6f 64 65 73 20 61 72 65 20 69 6e 76 65 6e   codes are inven
4ed0: 74 65 64 20 66 6f 72 20 74 68 69 73 20 72 65 70  ted for this rep
4ee0: 6f 73 69 74 6f 72 79 2e 0a 2a 2f 0a 76 6f 69 64  ository..*/.void
4ef0: 20 64 62 5f 69 6e 69 74 69 61 6c 5f 73 65 74 75   db_initial_setu
4f00: 70 20 28 69 6e 74 20 6d 61 6b 65 49 6e 69 74 69  p (int makeIniti
4f10: 61 6c 56 65 72 73 69 6f 6e 2c 20 69 6e 74 20 6d  alVersion, int m
4f20: 61 6b 65 53 65 72 76 65 72 43 6f 64 65 73 29 7b  akeServerCodes){
4f30: 0a 20 20 63 68 61 72 20 2a 7a 44 61 74 65 3b 0a  .  char *zDate;.
4f40: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 55    const char *zU
4f50: 73 65 72 3b 0a 20 20 42 6c 6f 62 20 68 61 73 68  ser;.  Blob hash
4f60: 3b 0a 20 20 42 6c 6f 62 20 6d 61 6e 69 66 65 73  ;.  Blob manifes
4f70: 74 3b 0a 0a 20 20 64 62 5f 73 65 74 28 22 63 6f  t;..  db_set("co
4f80: 6e 74 65 6e 74 2d 73 63 68 65 6d 61 22 2c 20 43  ntent-schema", C
4f90: 4f 4e 54 45 4e 54 5f 53 43 48 45 4d 41 2c 20 30  ONTENT_SCHEMA, 0
4fa0: 29 3b 0a 20 20 64 62 5f 73 65 74 28 22 61 75 78  );.  db_set("aux
4fb0: 2d 73 63 68 65 6d 61 22 2c 20 41 55 58 5f 53 43  -schema", AUX_SC
4fc0: 48 45 4d 41 2c 20 30 29 3b 0a 20 20 69 66 28 20  HEMA, 0);.  if( 
4fd0: 6d 61 6b 65 53 65 72 76 65 72 43 6f 64 65 73 20  makeServerCodes 
4fe0: 29 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f  ){.    db_multi_
4ff0: 65 78 65 63 28 0a 20 20 20 20 20 20 22 49 4e 53  exec(.      "INS
5000: 45 52 54 20 49 4e 54 4f 20 63 6f 6e 66 69 67 28  ERT INTO config(
5010: 6e 61 6d 65 2c 76 61 6c 75 65 29 22 0a 20 20 20  name,value)".   
5020: 20 20 20 22 20 56 41 4c 55 45 53 28 27 73 65 72     " VALUES('ser
5030: 76 65 72 2d 63 6f 64 65 27 2c 20 6c 6f 77 65 72  ver-code', lower
5040: 28 68 65 78 28 72 61 6e 64 6f 6d 62 6c 6f 62 28  (hex(randomblob(
5050: 32 30 29 29 29 29 3b 22 0a 20 20 20 20 20 20 22  20))));".      "
5060: 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 6f 6e 66  INSERT INTO conf
5070: 69 67 28 6e 61 6d 65 2c 76 61 6c 75 65 29 22 0a  ig(name,value)".
5080: 20 20 20 20 20 20 22 20 56 41 4c 55 45 53 28 27        " VALUES('
5090: 70 72 6f 6a 65 63 74 2d 63 6f 64 65 27 2c 20 6c  project-code', l
50a0: 6f 77 65 72 28 68 65 78 28 72 61 6e 64 6f 6d 62  ower(hex(randomb
50b0: 6c 6f 62 28 32 30 29 29 29 29 3b 22 0a 20 20 20  lob(20))));".   
50c0: 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 64   );.  }.  if( !d
50d0: 62 5f 69 73 5f 67 6c 6f 62 61 6c 28 22 61 75 74  b_is_global("aut
50e0: 6f 73 79 6e 63 22 29 20 29 20 64 62 5f 73 65 74  osync") ) db_set
50f0: 5f 69 6e 74 28 22 61 75 74 6f 73 79 6e 63 22 2c  _int("autosync",
5100: 20 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 21 64   1, 0);.  if( !d
5110: 62 5f 69 73 5f 67 6c 6f 62 61 6c 28 22 6c 6f 63  b_is_global("loc
5120: 61 6c 61 75 74 68 22 29 20 29 20 64 62 5f 73 65  alauth") ) db_se
5130: 74 5f 69 6e 74 28 22 6c 6f 63 61 6c 61 75 74 68  t_int("localauth
5140: 22 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 55 73 65  ", 0, 0);.  zUse
5150: 72 20 3d 20 64 62 5f 67 65 74 28 22 64 65 66 61  r = db_get("defa
5160: 75 6c 74 2d 75 73 65 72 22 2c 20 30 29 3b 0a 20  ult-user", 0);. 
5170: 20 69 66 28 20 7a 55 73 65 72 3d 3d 30 20 29 7b   if( zUser==0 ){
5180: 0a 23 69 66 64 65 66 20 5f 5f 4d 49 4e 47 57 33  .#ifdef __MINGW3
5190: 32 5f 5f 0a 20 20 20 20 7a 55 73 65 72 20 3d 20  2__.    zUser = 
51a0: 67 65 74 65 6e 76 28 22 55 53 45 52 4e 41 4d 45  getenv("USERNAME
51b0: 22 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 7a 55  ");.#else.    zU
51c0: 73 65 72 20 3d 20 67 65 74 65 6e 76 28 22 55 53  ser = getenv("US
51d0: 45 52 22 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  ER");.#endif.  }
51e0: 0a 20 20 69 66 28 20 7a 55 73 65 72 3d 3d 30 20  .  if( zUser==0 
51f0: 29 7b 0a 20 20 20 20 7a 55 73 65 72 20 3d 20 22  ){.    zUser = "
5200: 72 6f 6f 74 22 3b 0a 20 20 7d 0a 20 20 64 62 5f  root";.  }.  db_
5210: 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20  multi_exec(.    
5220: 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 75 73   "INSERT INTO us
5230: 65 72 28 6c 6f 67 69 6e 2c 20 70 77 2c 20 63 61  er(login, pw, ca
5240: 70 2c 20 69 6e 66 6f 29 22 0a 20 20 20 20 20 22  p, info)".     "
5250: 56 41 4c 55 45 53 28 25 51 2c 27 27 2c 27 73 27  VALUES(%Q,'','s'
5260: 2c 27 27 29 22 2c 20 7a 55 73 65 72 0a 20 20 29  ,'')", zUser.  )
5270: 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65  ;.  db_multi_exe
5280: 63 28 0a 20 20 20 20 20 22 49 4e 53 45 52 54 20  c(.     "INSERT 
5290: 49 4e 54 4f 20 75 73 65 72 28 6c 6f 67 69 6e 2c  INTO user(login,
52a0: 70 77 2c 63 61 70 2c 69 6e 66 6f 29 22 0a 20 20  pw,cap,info)".  
52b0: 20 20 20 22 20 20 20 56 41 4c 55 45 53 28 27 61     "   VALUES('a
52c0: 6e 6f 6e 79 6d 6f 75 73 27 2c 27 61 6e 6f 6e 79  nonymous','anony
52d0: 6d 6f 75 73 27 2c 27 68 6a 6b 6f 72 77 27 2c 27  mous','hjkorw','
52e0: 41 6e 6f 6e 27 29 3b 22 0a 20 20 20 20 20 22 49  Anon');".     "I
52f0: 4e 53 45 52 54 20 49 4e 54 4f 20 75 73 65 72 28  NSERT INTO user(
5300: 6c 6f 67 69 6e 2c 70 77 2c 63 61 70 2c 69 6e 66  login,pw,cap,inf
5310: 6f 29 22 0a 20 20 20 20 20 22 20 20 20 56 41 4c  o)".     "   VAL
5320: 55 45 53 28 27 6e 6f 62 6f 64 79 27 2c 27 27 2c  UES('nobody','',
5330: 27 6a 6f 72 27 2c 27 4e 6f 62 6f 64 79 27 29 3b  'jor','Nobody');
5340: 22 0a 20 20 29 3b 0a 20 20 75 73 65 72 5f 73 65  ".  );.  user_se
5350: 6c 65 63 74 28 29 3b 0a 0a 20 20 69 66 20 28 6d  lect();..  if (m
5360: 61 6b 65 49 6e 69 74 69 61 6c 56 65 72 73 69 6f  akeInitialVersio
5370: 6e 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 7a 65 72  n){.    blob_zer
5380: 6f 28 26 6d 61 6e 69 66 65 73 74 29 3b 0a 20 20  o(&manifest);.  
5390: 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26    blob_appendf(&
53a0: 6d 61 6e 69 66 65 73 74 2c 20 22 43 20 69 6e 69  manifest, "C ini
53b0: 74 69 61 6c 5c 5c 73 65 6d 70 74 79 5c 5c 73 62  tial\\sempty\\sb
53c0: 61 73 65 6c 69 6e 65 5c 6e 22 29 3b 0a 20 20 20  aseline\n");.   
53d0: 20 7a 44 61 74 65 20 3d 20 64 62 5f 74 65 78 74   zDate = db_text
53e0: 28 30 2c 20 22 53 45 4c 45 43 54 20 64 61 74 65  (0, "SELECT date
53f0: 74 69 6d 65 28 27 6e 6f 77 27 29 22 29 3b 0a 20  time('now')");. 
5400: 20 20 20 7a 44 61 74 65 5b 31 30 5d 3d 27 54 27     zDate[10]='T'
5410: 3b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e  ;.    blob_appen
5420: 64 66 28 26 6d 61 6e 69 66 65 73 74 2c 20 22 44  df(&manifest, "D
5430: 20 25 73 5c 6e 22 2c 20 7a 44 61 74 65 29 3b 0a   %s\n", zDate);.
5440: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66      blob_appendf
5450: 28 26 6d 61 6e 69 66 65 73 74 2c 20 22 50 5c 6e  (&manifest, "P\n
5460: 22 29 3b 0a 20 20 20 20 6d 64 35 73 75 6d 5f 69  ");.    md5sum_i
5470: 6e 69 74 28 29 3b 0a 20 20 20 20 62 6c 6f 62 5f  nit();.    blob_
5480: 61 70 70 65 6e 64 66 28 26 6d 61 6e 69 66 65 73  appendf(&manifes
5490: 74 2c 20 22 52 20 25 73 5c 6e 22 2c 20 6d 64 35  t, "R %s\n", md5
54a0: 73 75 6d 5f 66 69 6e 69 73 68 28 30 29 29 3b 0a  sum_finish(0));.
54b0: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66      blob_appendf
54c0: 28 26 6d 61 6e 69 66 65 73 74 2c 20 22 55 20 25  (&manifest, "U %
54d0: 46 5c 6e 22 2c 20 67 2e 7a 4c 6f 67 69 6e 29 3b  F\n", g.zLogin);
54e0: 0a 20 20 20 20 6d 64 35 73 75 6d 5f 62 6c 6f 62  .    md5sum_blob
54f0: 28 26 6d 61 6e 69 66 65 73 74 2c 20 26 68 61 73  (&manifest, &has
5500: 68 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70  h);.    blob_app
5510: 65 6e 64 66 28 26 6d 61 6e 69 66 65 73 74 2c 20  endf(&manifest, 
5520: 22 5a 20 25 62 5c 6e 22 2c 20 26 68 61 73 68 29  "Z %b\n", &hash)
5530: 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74  ;.    blob_reset
5540: 28 26 68 61 73 68 29 3b 0a 20 20 20 20 63 6f 6e  (&hash);.    con
5550: 74 65 6e 74 5f 70 75 74 28 26 6d 61 6e 69 66 65  tent_put(&manife
5560: 73 74 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 7d  st, 0, 0);.  }.}
5570: 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a  ../*.** COMMAND:
5580: 20 6e 65 77 0a 2a 2a 0a 2a 2a 20 55 73 61 67 65   new.**.** Usage
5590: 3a 20 25 66 6f 73 73 69 6c 20 6e 65 77 20 46 49  : %fossil new FI
55a0: 4c 45 4e 41 4d 45 0a 2a 2a 0a 2a 2a 20 43 72 65  LENAME.**.** Cre
55b0: 61 74 65 20 61 20 72 65 70 6f 73 69 74 6f 72 79  ate a repository
55c0: 20 66 6f 72 20 61 20 6e 65 77 20 70 72 6f 6a 65   for a new proje
55d0: 63 74 20 69 6e 20 74 68 65 20 66 69 6c 65 20 6e  ct in the file n
55e0: 61 6d 65 64 20 46 49 4c 45 4e 41 4d 45 2e 0a 2a  amed FILENAME..*
55f0: 2a 20 54 68 69 73 20 63 6f 6d 6d 61 6e 64 20 69  * This command i
5600: 73 20 64 69 73 74 69 6e 63 74 20 66 72 6f 6d 20  s distinct from 
5610: 22 63 6c 6f 6e 65 22 2e 20 20 54 68 65 20 22 63  "clone".  The "c
5620: 6c 6f 6e 65 22 20 63 6f 6d 6d 61 6e 64 20 6d 61  lone" command ma
5630: 6b 65 73 0a 2a 2a 20 61 20 63 6f 70 79 20 6f 66  kes.** a copy of
5640: 20 61 6e 20 65 78 69 73 74 69 6e 67 20 70 72 6f   an existing pro
5650: 6a 65 63 74 2e 20 20 54 68 69 73 20 63 6f 6d 6d  ject.  This comm
5660: 61 6e 64 20 73 74 61 72 74 73 20 61 20 6e 65 77  and starts a new
5670: 20 70 72 6f 6a 65 63 74 2e 0a 2a 2f 0a 76 6f 69   project..*/.voi
5680: 64 20 63 72 65 61 74 65 5f 72 65 70 6f 73 69 74  d create_reposit
5690: 6f 72 79 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20  ory_cmd(void){. 
56a0: 20 69 66 28 20 67 2e 61 72 67 63 21 3d 33 20 29   if( g.argc!=3 )
56b0: 7b 0a 20 20 20 20 75 73 61 67 65 28 22 52 45 50  {.    usage("REP
56c0: 4f 53 49 54 4f 52 59 2d 4e 41 4d 45 22 29 3b 0a  OSITORY-NAME");.
56d0: 20 20 7d 0a 20 20 64 62 5f 63 72 65 61 74 65 5f    }.  db_create_
56e0: 72 65 70 6f 73 69 74 6f 72 79 28 67 2e 61 72 67  repository(g.arg
56f0: 76 5b 32 5d 29 3b 0a 20 20 64 62 5f 6f 70 65 6e  v[2]);.  db_open
5700: 5f 72 65 70 6f 73 69 74 6f 72 79 28 67 2e 61 72  _repository(g.ar
5710: 67 76 5b 32 5d 29 3b 0a 20 20 64 62 5f 6f 70 65  gv[2]);.  db_ope
5720: 6e 5f 63 6f 6e 66 69 67 28 29 3b 0a 20 20 64 62  n_config();.  db
5730: 5f 62 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69  _begin_transacti
5740: 6f 6e 28 29 3b 0a 20 20 64 62 5f 69 6e 69 74 69  on();.  db_initi
5750: 61 6c 5f 73 65 74 75 70 28 31 2c 20 31 29 3b 0a  al_setup(1, 1);.
5760: 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63    db_end_transac
5770: 74 69 6f 6e 28 30 29 3b 0a 20 20 70 72 69 6e 74  tion(0);.  print
5780: 66 28 22 70 72 6f 6a 65 63 74 2d 69 64 3a 20 25  f("project-id: %
5790: 73 5c 6e 22 2c 20 64 62 5f 67 65 74 28 22 70 72  s\n", db_get("pr
57a0: 6f 6a 65 63 74 2d 63 6f 64 65 22 2c 20 30 29 29  oject-code", 0))
57b0: 3b 0a 20 20 70 72 69 6e 74 66 28 22 73 65 72 76  ;.  printf("serv
57c0: 65 72 2d 69 64 3a 20 20 25 73 5c 6e 22 2c 20 64  er-id:  %s\n", d
57d0: 62 5f 67 65 74 28 22 73 65 72 76 65 72 2d 63 6f  b_get("server-co
57e0: 64 65 22 2c 20 30 29 29 3b 0a 20 20 70 72 69 6e  de", 0));.  prin
57f0: 74 66 28 22 61 64 6d 69 6e 2d 75 73 65 72 3a 20  tf("admin-user: 
5800: 25 73 20 28 6e 6f 20 70 61 73 73 77 6f 72 64 20  %s (no password 
5810: 73 65 74 20 79 65 74 21 29 5c 6e 22 2c 20 67 2e  set yet!)\n", g.
5820: 7a 4c 6f 67 69 6e 29 3b 0a 20 20 70 72 69 6e 74  zLogin);.  print
5830: 66 28 22 62 61 73 65 6c 69 6e 65 3a 20 20 20 25  f("baseline:   %
5840: 73 5c 6e 22 2c 20 64 62 5f 74 65 78 74 28 30 2c  s\n", db_text(0,
5850: 20 22 53 45 4c 45 43 54 20 75 75 69 64 20 46 52   "SELECT uuid FR
5860: 4f 4d 20 62 6c 6f 62 22 29 29 3b 0a 7d 0a 0a 2f  OM blob"));.}../
5870: 2a 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  *.** SQL functio
5880: 6e 73 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67  ns for debugging
5890: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 69 6e  ..**.** The prin
58a0: 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 72 69  t() function wri
58b0: 74 65 73 20 69 74 73 20 61 72 67 75 6d 65 6e 74  tes its argument
58c0: 73 20 6f 6e 20 73 74 64 6f 75 74 2c 20 62 75 74  s on stdout, but
58d0: 20 6f 6e 6c 79 0a 2a 2a 20 69 66 20 74 68 65 20   only.** if the 
58e0: 2d 73 71 6c 70 72 69 6e 74 20 63 6f 6d 6d 61 6e  -sqlprint comman
58f0: 64 2d 6c 69 6e 65 20 6f 70 74 69 6f 6e 20 69 73  d-line option is
5900: 20 74 75 72 6e 65 64 20 6f 6e 2e 0a 2a 2f 0a 73   turned on..*/.s
5910: 74 61 74 69 63 20 76 6f 69 64 20 64 62 5f 73 71  tatic void db_sq
5920: 6c 5f 70 72 69 6e 74 28 0a 20 20 73 71 6c 69 74  l_print(.  sqlit
5930: 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
5940: 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c  ext,.  int argc,
5950: 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
5960: 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74   **argv.){.  int
5970: 20 69 3b 0a 20 20 69 66 28 20 67 2e 66 53 71 6c   i;.  if( g.fSql
5980: 50 72 69 6e 74 20 29 7b 0a 20 20 20 20 66 6f 72  Print ){.    for
5990: 28 69 3d 30 3b 20 69 3c 61 72 67 63 3b 20 69 2b  (i=0; i<argc; i+
59a0: 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 63  +){.      char c
59b0: 20 3d 20 69 3d 3d 61 72 67 63 2d 31 20 3f 20 27   = i==argc-1 ? '
59c0: 5c 6e 27 20 3a 20 27 20 27 3b 0a 20 20 20 20 20  \n' : ' ';.     
59d0: 20 70 72 69 6e 74 66 28 22 25 73 25 63 22 2c 20   printf("%s%c", 
59e0: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
59f0: 78 74 28 61 72 67 76 5b 69 5d 29 2c 20 63 29 3b  xt(argv[i]), c);
5a00: 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61  .    }.  }.}.sta
5a10: 74 69 63 20 76 6f 69 64 20 64 62 5f 73 71 6c 5f  tic void db_sql_
5a20: 74 72 61 63 65 28 76 6f 69 64 20 2a 6e 6f 74 55  trace(void *notU
5a30: 73 65 64 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  sed, const char 
5a40: 2a 7a 53 71 6c 29 7b 0a 20 20 70 72 69 6e 74 66  *zSql){.  printf
5a50: 28 22 25 73 5c 6e 22 2c 20 7a 53 71 6c 29 3b 0a  ("%s\n", zSql);.
5a60: 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73  }../*.** This is
5a70: 20 75 73 65 64 20 62 79 20 74 68 65 20 5b 63 6f   used by the [co
5a80: 6d 6d 69 74 5d 20 63 6f 6d 6d 61 6e 64 2e 0a 2a  mmit] command..*
5a90: 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
5aa0: 20 69 66 20 65 69 74 68 65 72 3a 0a 2a 2a 0a 2a   if either:.**.*
5ab0: 2a 20 20 20 20 20 61 29 20 47 6c 6f 62 61 6c 2e  *     a) Global.
5ac0: 61 43 6f 6d 6d 69 74 46 69 6c 65 20 69 73 20 4e  aCommitFile is N
5ad0: 55 4c 4c 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 62  ULL, or.**     b
5ae0: 29 20 47 6c 6f 62 61 6c 2e 61 43 6f 6d 6d 69 74  ) Global.aCommit
5af0: 46 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  File contains th
5b00: 65 20 69 6e 74 65 67 65 72 20 70 61 73 73 65 64  e integer passed
5b10: 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e   as an argument.
5b20: 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  .**.** Otherwise
5b30: 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a   return false..*
5b40: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69  /.static void fi
5b50: 6c 65 5f 69 73 5f 73 65 6c 65 63 74 65 64 28 0a  le_is_selected(.
5b60: 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
5b70: 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
5b80: 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
5b90: 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
5ba0: 7b 0a 20 20 61 73 73 65 72 74 28 61 72 67 63 3d  {.  assert(argc=
5bb0: 3d 31 29 3b 0a 20 20 69 66 28 20 67 2e 61 43 6f  =1);.  if( g.aCo
5bc0: 6d 6d 69 74 46 69 6c 65 20 29 7b 0a 20 20 20 20  mmitFile ){.    
5bd0: 69 6e 74 20 69 49 64 20 3d 20 73 71 6c 69 74 65  int iId = sqlite
5be0: 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76  3_value_int(argv
5bf0: 5b 30 5d 29 3b 0a 20 20 20 20 69 6e 74 20 69 69  [0]);.    int ii
5c00: 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20  ;.    for(ii=0; 
5c10: 67 2e 61 43 6f 6d 6d 69 74 46 69 6c 65 5b 69 69  g.aCommitFile[ii
5c20: 5d 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ]; ii++){.      
5c30: 69 66 28 20 69 49 64 3d 3d 67 2e 61 43 6f 6d 6d  if( iId==g.aComm
5c40: 69 74 46 69 6c 65 5b 69 69 5d 20 29 7b 0a 20 20  itFile[ii] ){.  
5c50: 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
5c60: 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74  sult_int(context
5c70: 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 72 65  , 1);.        re
5c80: 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
5c90: 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
5ca0: 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65  result_int(conte
5cb0: 78 74 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 7b  xt, 0);.  }else{
5cc0: 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
5cd0: 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c  ult_int(context,
5ce0: 20 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a   1);.  }.}../*.*
5cf0: 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 69 6e  * Convert the in
5d00: 70 75 74 20 73 74 72 69 6e 67 20 69 6e 74 6f 20  put string into 
5d10: 61 6e 20 53 48 41 31 2e 20 20 4d 61 6b 65 20 61  an SHA1.  Make a
5d20: 20 6e 6f 74 61 74 69 6f 6e 20 69 6e 20 74 68 65   notation in the
5d30: 0a 2a 2a 20 43 4f 4e 43 45 41 4c 45 44 20 74 61  .** CONCEALED ta
5d40: 62 6c 65 20 73 6f 20 74 68 61 74 20 74 68 65 20  ble so that the 
5d50: 68 61 73 68 20 63 61 6e 20 62 65 20 75 6e 64 6f  hash can be undo
5d60: 20 75 73 69 6e 67 20 74 68 65 20 64 62 5f 72 65   using the db_re
5d70: 76 65 61 6c 28 29 0a 2a 2a 20 66 75 6e 63 74 69  veal().** functi
5d80: 6f 6e 20 61 74 20 73 6f 6d 65 20 6c 61 74 65 72  on at some later
5d90: 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   time..**.** The
5da0: 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
5db0: 69 73 20 73 74 6f 72 65 64 20 69 6e 20 73 74 61  is stored in sta
5dc0: 74 69 63 20 73 70 61 63 65 20 61 6e 64 20 77 69  tic space and wi
5dd0: 6c 6c 20 62 65 20 6f 76 65 72 77 72 69 74 74 65  ll be overwritte
5de0: 6e 0a 2a 2a 20 6f 6e 20 73 75 62 73 65 71 75 65  n.** on subseque
5df0: 6e 74 20 63 61 6c 6c 73 2e 0a 2a 2f 0a 63 68 61  nt calls..*/.cha
5e00: 72 20 2a 64 62 5f 63 6f 6e 63 65 61 6c 28 63 6f  r *db_conceal(co
5e10: 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65  nst char *zConte
5e20: 6e 74 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 73 74  nt, int n){.  st
5e30: 61 74 69 63 20 63 68 61 72 20 7a 48 61 73 68 5b  atic char zHash[
5e40: 34 32 5d 3b 0a 20 20 42 6c 6f 62 20 6f 75 74 3b  42];.  Blob out;
5e50: 0a 20 20 73 68 61 31 73 75 6d 5f 73 74 65 70 5f  .  sha1sum_step_
5e60: 74 65 78 74 28 7a 43 6f 6e 74 65 6e 74 2c 20 6e  text(zContent, n
5e70: 29 3b 0a 20 20 73 68 61 31 73 75 6d 5f 66 69 6e  );.  sha1sum_fin
5e80: 69 73 68 28 26 6f 75 74 29 3b 0a 20 20 73 74 72  ish(&out);.  str
5e90: 63 70 79 28 7a 48 61 73 68 2c 20 62 6c 6f 62 5f  cpy(zHash, blob_
5ea0: 73 74 72 28 26 6f 75 74 29 29 3b 0a 20 20 62 6c  str(&out));.  bl
5eb0: 6f 62 5f 72 65 73 65 74 28 26 6f 75 74 29 3b 0a  ob_reset(&out);.
5ec0: 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28    db_multi_exec(
5ed0: 0a 20 20 20 20 20 22 49 4e 53 45 52 54 20 4f 52  .     "INSERT OR
5ee0: 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 63 6f 6e   IGNORE INTO con
5ef0: 63 65 61 6c 65 64 20 56 41 4c 55 45 53 28 25 51  cealed VALUES(%Q
5f00: 2c 25 51 29 22 2c 0a 20 20 20 20 20 7a 48 61 73  ,%Q)",.     zHas
5f10: 68 2c 20 7a 43 6f 6e 74 65 6e 74 0a 20 20 29 3b  h, zContent.  );
5f20: 0a 20 20 72 65 74 75 72 6e 20 7a 48 61 73 68 3b  .  return zHash;
5f30: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70  .}../*.** Attemp
5f40: 74 20 74 6f 20 6c 6f 6f 6b 20 75 70 20 74 68 65  t to look up the
5f50: 20 69 6e 70 75 74 20 69 6e 20 74 68 65 20 43 4f   input in the CO
5f60: 4e 43 45 41 4c 45 44 20 74 61 62 6c 65 2e 20 20  NCEALED table.  
5f70: 49 66 20 66 6f 75 6e 64 2c 0a 2a 2a 20 61 6e 64  If found,.** and
5f80: 20 69 66 20 74 68 65 20 6f 6b 52 64 41 64 64 72   if the okRdAddr
5f90: 20 70 65 72 6d 69 73 73 69 6f 6e 20 69 73 20 65   permission is e
5fa0: 6e 61 62 6c 65 64 20 74 68 65 6e 20 72 65 74 75  nabled then retu
5fb0: 72 6e 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e  rn the.** origin
5fc0: 61 6c 20 76 61 6c 75 65 20 66 6f 72 20 77 68 69  al value for whi
5fd0: 63 68 20 74 68 65 20 69 6e 70 75 74 20 69 73 20  ch the input is 
5fe0: 61 20 68 61 73 68 2e 20 20 49 66 20 6f 6b 52 64  a hash.  If okRd
5ff0: 41 64 64 72 20 69 73 0a 2a 2a 20 66 61 6c 73 65  Addr is.** false
6000: 20 6f 72 20 69 66 20 74 68 65 20 6c 6f 6f 6b 75   or if the looku
6010: 70 20 66 61 69 6c 73 2c 20 72 65 74 75 72 6e 20  p fails, return 
6020: 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 69 6e 70  the original inp
6030: 75 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 65 69 74  ut..**.** In eit
6040: 68 65 72 20 63 61 73 65 2c 20 74 68 65 20 73 74  her case, the st
6050: 72 69 6e 67 20 72 65 74 75 72 6e 65 64 20 69 73  ring returned is
6060: 20 73 74 6f 72 65 64 20 69 6e 20 73 70 61 63 65   stored in space
6070: 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f   obtained.** fro
6080: 6d 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 73 68 6f  m malloc and sho
6090: 75 6c 64 20 62 65 20 66 72 65 65 64 20 62 79 20  uld be freed by 
60a0: 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63  the calling func
60b0: 74 69 6f 6e 2e 0a 2a 2f 0a 63 68 61 72 20 2a 64  tion..*/.char *d
60c0: 62 5f 72 65 76 65 61 6c 28 63 6f 6e 73 74 20 63  b_reveal(const c
60d0: 68 61 72 20 2a 7a 4b 65 79 29 7b 0a 20 20 63 68  har *zKey){.  ch
60e0: 61 72 20 2a 7a 4f 75 74 3b 0a 20 20 69 66 28 20  ar *zOut;.  if( 
60f0: 67 2e 6f 6b 52 64 41 64 64 72 20 29 7b 0a 20 20  g.okRdAddr ){.  
6100: 20 20 7a 4f 75 74 20 3d 20 64 62 5f 74 65 78 74    zOut = db_text
6110: 28 30 2c 20 22 53 45 4c 45 43 54 20 63 6f 6e 74  (0, "SELECT cont
6120: 65 6e 74 20 46 52 4f 4d 20 63 6f 6e 63 65 61 6c  ent FROM conceal
6130: 65 64 20 57 48 45 52 45 20 68 61 73 68 3d 25 51  ed WHERE hash=%Q
6140: 22 2c 20 7a 4b 65 79 29 3b 0a 20 20 7d 65 6c 73  ", zKey);.  }els
6150: 65 7b 0a 20 20 20 20 7a 4f 75 74 20 3d 20 30 3b  e{.    zOut = 0;
6160: 0a 20 20 7d 0a 20 20 69 66 28 20 7a 4f 75 74 3d  .  }.  if( zOut=
6170: 3d 30 20 29 7b 0a 20 20 20 20 7a 4f 75 74 20 3d  =0 ){.    zOut =
6180: 20 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 20 7a   mprintf("%s", z
6190: 4b 65 79 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Key);.  }.  retu
61a0: 72 6e 20 7a 4f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn zOut;.}../*.*
61b0: 2a 20 54 68 65 20 63 6f 6e 63 65 61 6c 28 29 20  * The conceal() 
61c0: 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 43  SQL function.  C
61d0: 6f 6d 70 75 74 65 20 61 6e 20 53 48 41 31 20 68  ompute an SHA1 h
61e0: 61 73 68 20 6f 66 20 74 68 65 20 61 72 67 75 6d  ash of the argum
61f0: 65 6e 74 0a 2a 2a 20 61 6e 64 20 72 65 74 75 72  ent.** and retur
6200: 6e 20 74 68 61 74 20 68 61 73 68 20 61 73 20 61  n that hash as a
6210: 20 34 30 2d 63 68 61 72 61 63 74 65 72 20 6c 6f   40-character lo
6220: 77 65 72 2d 63 61 73 65 20 68 65 78 20 6e 75 6d  wer-case hex num
6230: 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ber..*/.static v
6240: 6f 69 64 20 73 68 61 31 5f 66 75 6e 63 74 69 6f  oid sha1_functio
6250: 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  n(.  sqlite3_con
6260: 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
6270: 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
6280: 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
6290: 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61  v.){.  const cha
62a0: 72 20 2a 7a 49 6e 3b 0a 20 20 69 6e 74 20 6e 49  r *zIn;.  int nI
62b0: 6e 3b 0a 20 20 63 68 61 72 20 2a 7a 4f 75 74 3b  n;.  char *zOut;
62c0: 0a 20 20 61 73 73 65 72 74 28 61 72 67 63 3d 3d  .  assert(argc==
62d0: 31 29 3b 0a 20 20 0a 20 20 7a 49 6e 20 3d 20 28  1);.  .  zIn = (
62e0: 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69  const char*)sqli
62f0: 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
6300: 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 7a  rgv[0]);.  if( z
6310: 49 6e 20 29 7b 0a 20 20 20 20 6e 49 6e 20 3d 20  In ){.    nIn = 
6320: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
6330: 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  tes(argv[0]);.  
6340: 20 20 7a 4f 75 74 20 3d 20 64 62 5f 63 6f 6e 63    zOut = db_conc
6350: 65 61 6c 28 7a 49 6e 2c 20 6e 49 6e 29 3b 0a 20  eal(zIn, nIn);. 
6360: 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
6370: 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
6380: 7a 4f 75 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45  zOut, -1, SQLITE
6390: 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d  _TRANSIENT);.  }
63a0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65  .}../*.** The re
63b0: 76 65 61 6c 28 29 20 53 51 4c 20 66 75 6e 63 74  veal() SQL funct
63c0: 69 6f 6e 20 69 6e 76 6f 6b 65 73 20 74 68 65 20  ion invokes the 
63d0: 64 62 5f 72 65 76 65 61 6c 28 29 20 66 75 6e 63  db_reveal() func
63e0: 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
63f0: 76 6f 69 64 20 72 65 76 65 61 6c 5f 66 75 6e 63  void reveal_func
6400: 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f  tion(.  sqlite3_
6410: 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
6420: 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
6430: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
6440: 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  argv.){.  const 
6450: 63 68 61 72 20 2a 7a 49 6e 3b 0a 20 20 63 68 61  char *zIn;.  cha
6460: 72 20 2a 7a 4f 75 74 3b 0a 20 20 61 73 73 65 72  r *zOut;.  asser
6470: 74 28 61 72 67 63 3d 3d 31 29 3b 0a 20 20 0a 20  t(argc==1);.  . 
6480: 20 7a 49 6e 20 3d 20 28 63 6f 6e 73 74 20 63 68   zIn = (const ch
6490: 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar*)sqlite3_valu
64a0: 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
64b0: 0a 20 20 69 66 28 20 7a 49 6e 20 29 7b 0a 20 20  .  if( zIn ){.  
64c0: 20 20 7a 4f 75 74 20 3d 20 64 62 5f 72 65 76 65    zOut = db_reve
64d0: 61 6c 28 7a 49 6e 29 3b 0a 20 20 20 20 73 71 6c  al(zIn);.    sql
64e0: 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
64f0: 28 63 6f 6e 74 65 78 74 2c 20 7a 4f 75 74 2c 20  (context, zOut, 
6500: 2d 31 2c 20 66 72 65 65 29 3b 0a 20 20 7d 0a 7d  -1, free);.  }.}
6510: 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
6520: 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 73 20  ction registers 
6530: 61 75 78 69 6c 69 61 72 79 20 66 75 6e 63 74 69  auxiliary functi
6540: 6f 6e 73 20 77 68 65 6e 20 74 68 65 20 53 51 4c  ons when the SQL
6550: 69 74 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  ite.** database 
6560: 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 66 69  connection is fi
6570: 72 73 74 20 65 73 74 61 62 6c 69 73 68 65 64 2e  rst established.
6580: 0a 2a 2f 0a 4c 4f 43 41 4c 20 76 6f 69 64 20 64  .*/.LOCAL void d
6590: 62 5f 63 6f 6e 6e 65 63 74 69 6f 6e 5f 69 6e 69  b_connection_ini
65a0: 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69  t(void){.  stati
65b0: 63 20 69 6e 74 20 6f 6e 63 65 20 3d 20 31 3b 0a  c int once = 1;.
65c0: 20 20 69 66 28 20 6f 6e 63 65 20 29 7b 0a 20 20    if( once ){.  
65d0: 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
65e0: 5f 66 75 6e 63 74 69 6f 6e 28 67 2e 64 62 2c 20  _function(g.db, 
65f0: 22 70 72 69 6e 74 22 2c 20 2d 31 2c 20 53 51 4c  "print", -1, SQL
6600: 49 54 45 5f 55 54 46 38 2c 20 30 2c 64 62 5f 73  ITE_UTF8, 0,db_s
6610: 71 6c 5f 70 72 69 6e 74 2c 30 2c 30 29 3b 0a 20  ql_print,0,0);. 
6620: 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
6630: 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 20 20  e_function(.    
6640: 20 20 67 2e 64 62 2c 20 22 66 69 6c 65 5f 69 73    g.db, "file_is
6650: 5f 73 65 6c 65 63 74 65 64 22 2c 20 31 2c 20 53  _selected", 1, S
6660: 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 66  QLITE_UTF8, 0, f
6670: 69 6c 65 5f 69 73 5f 73 65 6c 65 63 74 65 64 2c  ile_is_selected,
6680: 30 2c 30 0a 20 20 20 20 29 3b 0a 20 20 20 20 73  0,0.    );.    s
6690: 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
66a0: 6e 63 74 69 6f 6e 28 0a 20 20 20 20 20 20 67 2e  nction(.      g.
66b0: 64 62 2c 20 22 63 6f 6e 63 65 61 6c 22 2c 20 31  db, "conceal", 1
66c0: 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30  , SQLITE_UTF8, 0
66d0: 2c 20 73 68 61 31 5f 66 75 6e 63 74 69 6f 6e 2c  , sha1_function,
66e0: 30 2c 30 0a 20 20 20 20 29 3b 0a 20 20 20 20 73  0,0.    );.    s
66f0: 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
6700: 6e 63 74 69 6f 6e 28 0a 20 20 20 20 20 20 67 2e  nction(.      g.
6710: 64 62 2c 20 22 72 65 76 65 61 6c 22 2c 20 31 2c  db, "reveal", 1,
6720: 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c   SQLITE_UTF8, 0,
6730: 20 72 65 76 65 61 6c 5f 66 75 6e 63 74 69 6f 6e   reveal_function
6740: 2c 30 2c 30 0a 20 20 20 20 29 3b 0a 20 20 20 20  ,0,0.    );.    
6750: 69 66 28 20 67 2e 66 53 71 6c 54 72 61 63 65 20  if( g.fSqlTrace 
6760: 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
6770: 5f 74 72 61 63 65 28 67 2e 64 62 2c 20 64 62 5f  _trace(g.db, db_
6780: 73 71 6c 5f 74 72 61 63 65 2c 20 30 29 3b 0a 20  sql_trace, 0);. 
6790: 20 20 20 7d 0a 20 20 20 20 6f 6e 63 65 20 3d 20     }.    once = 
67a0: 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  0;.  }.}../*.** 
67b0: 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74  Return true if t
67c0: 68 65 20 73 74 72 69 6e 67 20 7a 56 61 6c 20 72  he string zVal r
67d0: 65 70 72 65 73 65 6e 74 73 20 22 74 72 75 65 22  epresents "true"
67e0: 20 28 6f 72 20 22 66 61 6c 73 65 22 29 2e 0a 2a   (or "false")..*
67f0: 2f 0a 69 6e 74 20 69 73 5f 74 72 75 74 68 28 63  /.int is_truth(c
6800: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 61 6c 29  onst char *zVal)
6810: 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  {.  static const
6820: 20 63 68 61 72 20 2a 61 7a 4f 6e 5b 5d 20 3d 20   char *azOn[] = 
6830: 7b 20 22 6f 6e 22 2c 20 22 79 65 73 22 2c 20 22  { "on", "yes", "
6840: 74 72 75 65 22 2c 20 22 31 22 20 7d 3b 0a 20 20  true", "1" };.  
6850: 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30  int i;.  for(i=0
6860: 3b 20 69 3c 73 69 7a 65 6f 66 28 61 7a 4f 6e 29  ; i<sizeof(azOn)
6870: 2f 73 69 7a 65 6f 66 28 61 7a 4f 6e 5b 30 5d 29  /sizeof(azOn[0])
6880: 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
6890: 73 74 72 63 6d 70 28 7a 56 61 6c 2c 61 7a 4f 6e  strcmp(zVal,azOn
68a0: 5b 69 5d 29 3d 3d 30 20 29 20 72 65 74 75 72 6e  [i])==0 ) return
68b0: 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   1;.  }.  return
68c0: 20 30 3b 0a 7d 0a 69 6e 74 20 69 73 5f 66 61 6c   0;.}.int is_fal
68d0: 73 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  se(const char *z
68e0: 56 61 6c 29 7b 0a 20 20 73 74 61 74 69 63 20 63  Val){.  static c
68f0: 6f 6e 73 74 20 63 68 61 72 20 2a 61 7a 4f 66 66  onst char *azOff
6900: 5b 5d 20 3d 20 7b 20 22 6f 66 66 22 2c 20 22 6e  [] = { "off", "n
6910: 6f 22 2c 20 22 66 61 6c 73 65 22 2c 20 22 30 22  o", "false", "0"
6920: 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66   };.  int i;.  f
6930: 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66  or(i=0; i<sizeof
6940: 28 61 7a 4f 66 66 29 2f 73 69 7a 65 6f 66 28 61  (azOff)/sizeof(a
6950: 7a 4f 66 66 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a  zOff[0]); i++){.
6960: 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a      if( strcmp(z
6970: 56 61 6c 2c 61 7a 4f 66 66 5b 69 5d 29 3d 3d 30  Val,azOff[i])==0
6980: 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d   ) return 1;.  }
6990: 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
69a0: 2f 2a 0a 2a 2a 20 47 65 74 20 61 6e 64 20 73 65  /*.** Get and se
69b0: 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68  t values from th
69c0: 65 20 43 4f 4e 46 49 47 2c 20 47 4c 4f 42 41 4c  e CONFIG, GLOBAL
69d0: 5f 43 4f 4e 46 49 47 20 61 6e 64 20 56 56 41 52  _CONFIG and VVAR
69e0: 20 74 61 62 6c 65 20 69 6e 20 74 68 65 0a 2a 2a   table in the.**
69f0: 20 72 65 70 6f 73 69 74 6f 72 79 20 61 6e 64 20   repository and 
6a00: 6c 6f 63 61 6c 20 64 61 74 61 62 61 73 65 73 2e  local databases.
6a10: 0a 2a 2f 0a 63 68 61 72 20 2a 64 62 5f 67 65 74  .*/.char *db_get
6a20: 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61  (const char *zNa
6a30: 6d 65 2c 20 63 68 61 72 20 2a 7a 44 65 66 61 75  me, char *zDefau
6a40: 6c 74 29 7b 0a 20 20 63 68 61 72 20 2a 7a 20 3d  lt){.  char *z =
6a50: 20 30 3b 0a 20 20 69 66 28 20 67 2e 72 65 70 6f   0;.  if( g.repo
6a60: 73 69 74 6f 72 79 4f 70 65 6e 20 29 7b 0a 20 20  sitoryOpen ){.  
6a70: 20 20 7a 20 3d 20 64 62 5f 74 65 78 74 28 30 2c    z = db_text(0,
6a80: 20 22 53 45 4c 45 43 54 20 76 61 6c 75 65 20 46   "SELECT value F
6a90: 52 4f 4d 20 63 6f 6e 66 69 67 20 57 48 45 52 45  ROM config WHERE
6aa0: 20 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d 65   name=%Q", zName
6ab0: 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 3d 3d  );.  }.  if( z==
6ac0: 30 20 26 26 20 67 2e 63 6f 6e 66 69 67 4f 70 65  0 && g.configOpe
6ad0: 6e 20 29 7b 0a 20 20 20 20 7a 20 3d 20 64 62 5f  n ){.    z = db_
6ae0: 74 65 78 74 28 30 2c 20 22 53 45 4c 45 43 54 20  text(0, "SELECT 
6af0: 76 61 6c 75 65 20 46 52 4f 4d 20 67 6c 6f 62 61  value FROM globa
6b00: 6c 5f 63 6f 6e 66 69 67 20 57 48 45 52 45 20 6e  l_config WHERE n
6b10: 61 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d 65 29 3b  ame=%Q", zName);
6b20: 0a 20 20 7d 0a 20 20 69 66 28 20 7a 3d 3d 30 20  .  }.  if( z==0 
6b30: 29 7b 0a 20 20 20 20 7a 20 3d 20 7a 44 65 66 61  ){.    z = zDefa
6b40: 75 6c 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ult;.  }.  retur
6b50: 6e 20 7a 3b 0a 7d 0a 76 6f 69 64 20 64 62 5f 73  n z;.}.void db_s
6b60: 65 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  et(const char *z
6b70: 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  Name, const char
6b80: 20 2a 7a 56 61 6c 75 65 2c 20 69 6e 74 20 67 6c   *zValue, int gl
6b90: 6f 62 61 6c 46 6c 61 67 29 7b 0a 20 20 64 62 5f  obalFlag){.  db_
6ba0: 62 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f  begin_transactio
6bb0: 6e 28 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f  n();.  db_multi_
6bc0: 65 78 65 63 28 22 52 45 50 4c 41 43 45 20 49 4e  exec("REPLACE IN
6bd0: 54 4f 20 25 73 63 6f 6e 66 69 67 28 6e 61 6d 65  TO %sconfig(name
6be0: 2c 76 61 6c 75 65 29 20 56 41 4c 55 45 53 28 25  ,value) VALUES(%
6bf0: 51 2c 25 51 29 22 2c 0a 20 20 20 20 20 20 20 20  Q,%Q)",.        
6c00: 20 20 20 20 20 20 20 20 20 67 6c 6f 62 61 6c 46           globalF
6c10: 6c 61 67 20 3f 20 22 67 6c 6f 62 61 6c 5f 22 20  lag ? "global_" 
6c20: 3a 20 22 22 2c 20 7a 4e 61 6d 65 2c 20 7a 56 61  : "", zName, zVa
6c30: 6c 75 65 29 3b 0a 20 20 69 66 28 20 67 6c 6f 62  lue);.  if( glob
6c40: 61 6c 46 6c 61 67 20 26 26 20 67 2e 72 65 70 6f  alFlag && g.repo
6c50: 73 69 74 6f 72 79 4f 70 65 6e 20 29 7b 0a 20 20  sitoryOpen ){.  
6c60: 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28    db_multi_exec(
6c70: 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 63 6f 6e  "DELETE FROM con
6c80: 66 69 67 20 57 48 45 52 45 20 6e 61 6d 65 3d 25  fig WHERE name=%
6c90: 51 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a  Q", zName);.  }.
6ca0: 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63    db_end_transac
6cb0: 74 69 6f 6e 28 30 29 3b 0a 7d 0a 76 6f 69 64 20  tion(0);.}.void 
6cc0: 64 62 5f 75 6e 73 65 74 28 63 6f 6e 73 74 20 63  db_unset(const c
6cd0: 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20  har *zName, int 
6ce0: 67 6c 6f 62 61 6c 46 6c 61 67 29 7b 0a 20 20 64  globalFlag){.  d
6cf0: 62 5f 62 65 67 69 6e 5f 74 72 61 6e 73 61 63 74  b_begin_transact
6d00: 69 6f 6e 28 29 3b 0a 20 20 64 62 5f 6d 75 6c 74  ion();.  db_mult
6d10: 69 5f 65 78 65 63 28 22 44 45 4c 45 54 45 20 46  i_exec("DELETE F
6d20: 52 4f 4d 20 25 73 63 6f 6e 66 69 67 20 57 48 45  ROM %sconfig WHE
6d30: 52 45 20 6e 61 6d 65 3d 25 51 22 2c 0a 20 20 20  RE name=%Q",.   
6d40: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 67 6c                gl
6d50: 6f 62 61 6c 46 6c 61 67 20 3f 20 22 67 6c 6f 62  obalFlag ? "glob
6d60: 61 6c 5f 22 20 3a 20 22 22 2c 20 7a 4e 61 6d 65  al_" : "", zName
6d70: 29 3b 0a 20 20 69 66 28 20 67 6c 6f 62 61 6c 46  );.  if( globalF
6d80: 6c 61 67 20 26 26 20 67 2e 72 65 70 6f 73 69 74  lag && g.reposit
6d90: 6f 72 79 4f 70 65 6e 20 29 7b 0a 20 20 20 20 64  oryOpen ){.    d
6da0: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 44 45  b_multi_exec("DE
6db0: 4c 45 54 45 20 46 52 4f 4d 20 63 6f 6e 66 69 67  LETE FROM config
6dc0: 20 57 48 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c   WHERE name=%Q",
6dd0: 20 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 64   zName);.  }.  d
6de0: 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  b_end_transactio
6df0: 6e 28 30 29 3b 0a 7d 0a 69 6e 74 20 64 62 5f 69  n(0);.}.int db_i
6e00: 73 5f 67 6c 6f 62 61 6c 28 63 6f 6e 73 74 20 63  s_global(const c
6e10: 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69  har *zName){.  i
6e20: 66 28 20 67 2e 63 6f 6e 66 69 67 4f 70 65 6e 20  f( g.configOpen 
6e30: 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 64 62  ){.    return db
6e40: 5f 65 78 69 73 74 73 28 22 53 45 4c 45 43 54 20  _exists("SELECT 
6e50: 31 20 46 52 4f 4d 20 67 6c 6f 62 61 6c 5f 63 6f  1 FROM global_co
6e60: 6e 66 69 67 20 57 48 45 52 45 20 6e 61 6d 65 3d  nfig WHERE name=
6e70: 25 51 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 7d  %Q", zName);.  }
6e80: 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
6e90: 20 30 3b 0a 20 20 7d 0a 7d 0a 69 6e 74 20 64 62   0;.  }.}.int db
6ea0: 5f 67 65 74 5f 69 6e 74 28 63 6f 6e 73 74 20 63  _get_int(const c
6eb0: 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20  har *zName, int 
6ec0: 64 66 6c 74 29 7b 0a 20 20 69 6e 74 20 76 20 3d  dflt){.  int v =
6ed0: 20 64 66 6c 74 3b 0a 20 20 69 6e 74 20 72 63 3b   dflt;.  int rc;
6ee0: 0a 20 20 69 66 28 20 67 2e 72 65 70 6f 73 69 74  .  if( g.reposit
6ef0: 6f 72 79 4f 70 65 6e 20 29 7b 0a 20 20 20 20 53  oryOpen ){.    S
6f00: 74 6d 74 20 71 3b 0a 20 20 20 20 64 62 5f 70 72  tmt q;.    db_pr
6f10: 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45 43  epare(&q, "SELEC
6f20: 54 20 76 61 6c 75 65 20 46 52 4f 4d 20 63 6f 6e  T value FROM con
6f30: 66 69 67 20 57 48 45 52 45 20 6e 61 6d 65 3d 25  fig WHERE name=%
6f40: 51 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  Q", zName);.    
6f50: 72 63 20 3d 20 64 62 5f 73 74 65 70 28 26 71 29  rc = db_step(&q)
6f60: 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
6f70: 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
6f80: 20 20 76 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f    v = db_column_
6f90: 69 6e 74 28 26 71 2c 20 30 29 3b 0a 20 20 20 20  int(&q, 0);.    
6fa0: 7d 0a 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a  }.    db_finaliz
6fb0: 65 28 26 71 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  e(&q);.  }else{.
6fc0: 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
6fd0: 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20  DONE;.  }.  if( 
6fe0: 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
6ff0: 26 26 20 67 2e 63 6f 6e 66 69 67 4f 70 65 6e 20  && g.configOpen 
7000: 29 7b 0a 20 20 20 20 76 20 3d 20 64 62 5f 69 6e  ){.    v = db_in
7010: 74 28 64 66 6c 74 2c 20 22 53 45 4c 45 43 54 20  t(dflt, "SELECT 
7020: 76 61 6c 75 65 20 46 52 4f 4d 20 67 6c 6f 62 61  value FROM globa
7030: 6c 5f 63 6f 6e 66 69 67 20 57 48 45 52 45 20 6e  l_config WHERE n
7040: 61 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d 65 29 3b  ame=%Q", zName);
7050: 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 76 3b  .  }.  return v;
7060: 0a 7d 0a 76 6f 69 64 20 64 62 5f 73 65 74 5f 69  .}.void db_set_i
7070: 6e 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  nt(const char *z
7080: 4e 61 6d 65 2c 20 69 6e 74 20 76 61 6c 75 65 2c  Name, int value,
7090: 20 69 6e 74 20 67 6c 6f 62 61 6c 46 6c 61 67 29   int globalFlag)
70a0: 7b 0a 20 20 64 62 5f 62 65 67 69 6e 5f 74 72 61  {.  db_begin_tra
70b0: 6e 73 61 63 74 69 6f 6e 28 29 3b 0a 20 20 64 62  nsaction();.  db
70c0: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 52 45 50  _multi_exec("REP
70d0: 4c 41 43 45 20 49 4e 54 4f 20 25 73 63 6f 6e 66  LACE INTO %sconf
70e0: 69 67 28 6e 61 6d 65 2c 76 61 6c 75 65 29 20 56  ig(name,value) V
70f0: 41 4c 55 45 53 28 25 51 2c 25 64 29 22 2c 0a 20  ALUES(%Q,%d)",. 
7100: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 67                 g
7110: 6c 6f 62 61 6c 46 6c 61 67 20 3f 20 22 67 6c 6f  lobalFlag ? "glo
7120: 62 61 6c 5f 22 20 3a 20 22 22 2c 20 7a 4e 61 6d  bal_" : "", zNam
7130: 65 2c 20 76 61 6c 75 65 29 3b 0a 20 20 69 66 28  e, value);.  if(
7140: 20 67 6c 6f 62 61 6c 46 6c 61 67 20 26 26 20 67   globalFlag && g
7150: 2e 72 65 70 6f 73 69 74 6f 72 79 4f 70 65 6e 20  .repositoryOpen 
7160: 29 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f  ){.    db_multi_
7170: 65 78 65 63 28 22 44 45 4c 45 54 45 20 46 52 4f  exec("DELETE FRO
7180: 4d 20 63 6f 6e 66 69 67 20 57 48 45 52 45 20 6e  M config WHERE n
7190: 61 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d 65 29 3b  ame=%Q", zName);
71a0: 0a 20 20 7d 0a 20 20 64 62 5f 65 6e 64 5f 74 72  .  }.  db_end_tr
71b0: 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b 0a 7d 0a  ansaction(0);.}.
71c0: 69 6e 74 20 64 62 5f 67 65 74 5f 62 6f 6f 6c 65  int db_get_boole
71d0: 61 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  an(const char *z
71e0: 4e 61 6d 65 2c 20 69 6e 74 20 64 66 6c 74 29 7b  Name, int dflt){
71f0: 0a 20 20 63 68 61 72 20 2a 7a 56 61 6c 20 3d 20  .  char *zVal = 
7200: 64 62 5f 67 65 74 28 7a 4e 61 6d 65 2c 20 64 66  db_get(zName, df
7210: 6c 74 20 3f 20 22 6f 6e 22 20 3a 20 22 6f 66 66  lt ? "on" : "off
7220: 22 29 3b 0a 20 20 69 66 28 20 69 73 5f 74 72 75  ");.  if( is_tru
7230: 74 68 28 7a 56 61 6c 29 20 29 20 72 65 74 75 72  th(zVal) ) retur
7240: 6e 20 31 3b 0a 20 20 69 66 28 20 69 73 5f 66 61  n 1;.  if( is_fa
7250: 6c 73 65 28 7a 56 61 6c 29 20 29 20 72 65 74 75  lse(zVal) ) retu
7260: 72 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 64  rn 0;.  return d
7270: 66 6c 74 3b 0a 7d 0a 63 68 61 72 20 2a 64 62 5f  flt;.}.char *db_
7280: 6c 67 65 74 28 63 6f 6e 73 74 20 63 68 61 72 20  lget(const char 
7290: 2a 7a 4e 61 6d 65 2c 20 63 68 61 72 20 2a 7a 44  *zName, char *zD
72a0: 65 66 61 75 6c 74 29 7b 0a 20 20 72 65 74 75 72  efault){.  retur
72b0: 6e 20 64 62 5f 74 65 78 74 28 28 63 68 61 72 2a  n db_text((char*
72c0: 29 7a 44 65 66 61 75 6c 74 2c 0a 20 20 20 20 20  )zDefault,.     
72d0: 20 20 20 20 20 20 20 20 20 20 20 20 22 53 45 4c              "SEL
72e0: 45 43 54 20 76 61 6c 75 65 20 46 52 4f 4d 20 76  ECT value FROM v
72f0: 76 61 72 20 57 48 45 52 45 20 6e 61 6d 65 3d 25  var WHERE name=%
7300: 51 22 2c 20 7a 4e 61 6d 65 29 3b 0a 7d 0a 76 6f  Q", zName);.}.vo
7310: 69 64 20 64 62 5f 6c 73 65 74 28 63 6f 6e 73 74  id db_lset(const
7320: 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f   char *zName, co
7330: 6e 73 74 20 63 68 61 72 20 2a 7a 56 61 6c 75 65  nst char *zValue
7340: 29 7b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78  ){.  db_multi_ex
7350: 65 63 28 22 52 45 50 4c 41 43 45 20 49 4e 54 4f  ec("REPLACE INTO
7360: 20 76 76 61 72 28 6e 61 6d 65 2c 76 61 6c 75 65   vvar(name,value
7370: 29 20 56 41 4c 55 45 53 28 25 51 2c 25 51 29 22  ) VALUES(%Q,%Q)"
7380: 2c 20 7a 4e 61 6d 65 2c 20 7a 56 61 6c 75 65 29  , zName, zValue)
7390: 3b 0a 7d 0a 69 6e 74 20 64 62 5f 6c 67 65 74 5f  ;.}.int db_lget_
73a0: 69 6e 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  int(const char *
73b0: 7a 4e 61 6d 65 2c 20 69 6e 74 20 64 66 6c 74 29  zName, int dflt)
73c0: 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 5f 69 6e  {.  return db_in
73d0: 74 28 64 66 6c 74 2c 20 22 53 45 4c 45 43 54 20  t(dflt, "SELECT 
73e0: 76 61 6c 75 65 20 46 52 4f 4d 20 76 76 61 72 20  value FROM vvar 
73f0: 57 48 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c 20  WHERE name=%Q", 
7400: 7a 4e 61 6d 65 29 3b 0a 7d 0a 76 6f 69 64 20 64  zName);.}.void d
7410: 62 5f 6c 73 65 74 5f 69 6e 74 28 63 6f 6e 73 74  b_lset_int(const
7420: 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e   char *zName, in
7430: 74 20 76 61 6c 75 65 29 7b 0a 20 20 64 62 5f 6d  t value){.  db_m
7440: 75 6c 74 69 5f 65 78 65 63 28 22 52 45 50 4c 41  ulti_exec("REPLA
7450: 43 45 20 49 4e 54 4f 20 76 76 61 72 28 6e 61 6d  CE INTO vvar(nam
7460: 65 2c 76 61 6c 75 65 29 20 56 41 4c 55 45 53 28  e,value) VALUES(
7470: 25 51 2c 25 64 29 22 2c 20 7a 4e 61 6d 65 2c 20  %Q,%d)", zName, 
7480: 76 61 6c 75 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  value);.}../*.**
7490: 20 43 4f 4d 4d 41 4e 44 3a 20 6f 70 65 6e 0a 2a   COMMAND: open.*
74a0: 2a 0a 2a 2a 20 55 73 61 67 65 3a 20 6f 70 65 6e  *.** Usage: open
74b0: 20 46 49 4c 45 4e 41 4d 45 0a 2a 2a 0a 2a 2a 20   FILENAME.**.** 
74c0: 4f 70 65 6e 20 61 20 63 6f 6e 6e 65 63 74 69 6f  Open a connectio
74d0: 6e 20 74 6f 20 74 68 65 20 6c 6f 63 61 6c 20 72  n to the local r
74e0: 65 70 6f 73 69 74 6f 72 79 20 69 6e 20 46 49 4c  epository in FIL
74f0: 45 4e 41 4d 45 2e 20 20 41 20 63 68 65 63 6b 6f  ENAME.  A checko
7500: 75 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 72 65  ut.** for the re
7510: 70 6f 73 69 74 6f 72 79 20 69 73 20 63 72 65 61  pository is crea
7520: 74 65 64 20 77 69 74 68 20 69 74 73 20 72 6f 6f  ted with its roo
7530: 74 20 61 74 20 74 68 65 20 77 6f 72 6b 69 6e 67  t at the working
7540: 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 20 53   directory..** S
7550: 65 65 20 61 6c 73 6f 20 74 68 65 20 22 63 6c 6f  ee also the "clo
7560: 73 65 22 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a  se" command..*/.
7570: 76 6f 69 64 20 63 6d 64 5f 6f 70 65 6e 28 76 6f  void cmd_open(vo
7580: 69 64 29 7b 0a 20 20 42 6c 6f 62 20 70 61 74 68  id){.  Blob path
7590: 3b 0a 20 20 69 6e 74 20 76 69 64 3b 0a 20 20 73  ;.  int vid;.  s
75a0: 74 61 74 69 63 20 63 68 61 72 20 2a 61 7a 4e 65  tatic char *azNe
75b0: 77 41 72 67 76 5b 5d 20 3d 20 7b 20 30 2c 20 22  wArgv[] = { 0, "
75c0: 75 70 64 61 74 65 22 2c 20 22 2d 2d 6c 61 74 65  update", "--late
75d0: 73 74 22 2c 20 30 20 7d 3b 0a 20 20 75 72 6c 5f  st", 0 };.  url_
75e0: 70 72 6f 78 79 5f 6f 70 74 69 6f 6e 73 28 29 3b  proxy_options();
75f0: 0a 20 20 69 66 28 20 67 2e 61 72 67 63 21 3d 33  .  if( g.argc!=3
7600: 20 29 7b 0a 20 20 20 20 75 73 61 67 65 28 22 52   ){.    usage("R
7610: 45 50 4f 53 49 54 4f 52 59 2d 46 49 4c 45 4e 41  EPOSITORY-FILENA
7620: 4d 45 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ME");.  }.  if( 
7630: 64 62 5f 6f 70 65 6e 5f 6c 6f 63 61 6c 28 29 20  db_open_local() 
7640: 29 7b 0a 20 20 20 20 66 6f 73 73 69 6c 5f 70 61  ){.    fossil_pa
7650: 6e 69 63 28 22 61 6c 72 65 61 64 79 20 77 69 74  nic("already wit
7660: 68 69 6e 20 61 6e 20 6f 70 65 6e 20 74 72 65 65  hin an open tree
7670: 20 72 6f 6f 74 65 64 20 61 74 20 25 73 22 2c 20   rooted at %s", 
7680: 67 2e 7a 4c 6f 63 61 6c 52 6f 6f 74 29 3b 0a 20  g.zLocalRoot);. 
7690: 20 7d 0a 20 20 66 69 6c 65 5f 63 61 6e 6f 6e 69   }.  file_canoni
76a0: 63 61 6c 5f 6e 61 6d 65 28 67 2e 61 72 67 76 5b  cal_name(g.argv[
76b0: 32 5d 2c 20 26 70 61 74 68 29 3b 0a 20 20 64 62  2], &path);.  db
76c0: 5f 6f 70 65 6e 5f 72 65 70 6f 73 69 74 6f 72 79  _open_repository
76d0: 28 62 6c 6f 62 5f 73 74 72 28 26 70 61 74 68 29  (blob_str(&path)
76e0: 29 3b 0a 20 20 64 62 5f 69 6e 69 74 5f 64 61 74  );.  db_init_dat
76f0: 61 62 61 73 65 28 22 2e 2f 5f 46 4f 53 53 49 4c  abase("./_FOSSIL
7700: 5f 22 2c 20 7a 4c 6f 63 61 6c 53 63 68 65 6d 61  _", zLocalSchema
7710: 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20 64  , (char*)0);.  d
7720: 62 5f 6f 70 65 6e 5f 6c 6f 63 61 6c 28 29 3b 0a  b_open_local();.
7730: 20 20 64 62 5f 6c 73 65 74 28 22 72 65 70 6f 73    db_lset("repos
7740: 69 74 6f 72 79 22 2c 20 62 6c 6f 62 5f 73 74 72  itory", blob_str
7750: 28 26 70 61 74 68 29 29 3b 0a 20 20 76 69 64 20  (&path));.  vid 
7760: 3d 20 64 62 5f 69 6e 74 28 30 2c 20 22 53 45 4c  = db_int(0, "SEL
7770: 45 43 54 20 70 69 64 20 46 52 4f 4d 20 70 6c 69  ECT pid FROM pli
7780: 6e 6b 20 79 22 0a 20 20 20 20 20 20 20 20 20 20  nk y".          
7790: 20 20 20 20 20 20 20 20 22 20 57 48 45 52 45 20          " WHERE 
77a0: 4e 4f 54 20 45 58 49 53 54 53 28 53 45 4c 45 43  NOT EXISTS(SELEC
77b0: 54 20 31 20 46 52 4f 4d 20 70 6c 69 6e 6b 20 78  T 1 FROM plink x
77c0: 20 57 48 45 52 45 20 78 2e 63 69 64 3d 79 2e 70   WHERE x.cid=y.p
77d0: 69 64 29 22 29 3b 0a 20 20 69 66 28 20 76 69 64  id)");.  if( vid
77e0: 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62 5f 6c 73  ==0 ){.    db_ls
77f0: 65 74 5f 69 6e 74 28 22 63 68 65 63 6b 6f 75 74  et_int("checkout
7800: 22 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ", 1);.  }else{.
7810: 20 20 20 20 64 62 5f 6c 73 65 74 5f 69 6e 74 28      db_lset_int(
7820: 22 63 68 65 63 6b 6f 75 74 22 2c 20 76 69 64 29  "checkout", vid)
7830: 3b 0a 20 20 20 20 67 2e 61 72 67 76 20 3d 20 61  ;.    g.argv = a
7840: 7a 4e 65 77 41 72 67 76 3b 0a 20 20 20 20 67 2e  zNewArgv;.    g.
7850: 61 72 67 63 20 3d 20 33 3b 0a 20 20 20 20 75 70  argc = 3;.    up
7860: 64 61 74 65 5f 63 6d 64 28 29 3b 0a 20 20 7d 0a  date_cmd();.  }.
7870: 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74  }../*.** Print t
7880: 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 73 65  he value of a se
7890: 74 74 69 6e 67 20 6e 61 6d 65 64 20 7a 4e 61 6d  tting named zNam
78a0: 65 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  e.*/.static void
78b0: 20 70 72 69 6e 74 5f 73 65 74 74 69 6e 67 28 63   print_setting(c
78c0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
78d0: 29 7b 0a 20 20 53 74 6d 74 20 71 3b 0a 20 20 69  ){.  Stmt q;.  i
78e0: 66 28 20 67 2e 72 65 70 6f 73 69 74 6f 72 79 4f  f( g.repositoryO
78f0: 70 65 6e 20 29 7b 0a 20 20 20 20 64 62 5f 70 72  pen ){.    db_pr
7900: 65 70 61 72 65 28 26 71 2c 0a 20 20 20 20 20 20  epare(&q,.      
7910: 20 22 53 45 4c 45 43 54 20 27 28 6c 6f 63 61 6c   "SELECT '(local
7920: 29 27 2c 20 76 61 6c 75 65 20 46 52 4f 4d 20 63  )', value FROM c
7930: 6f 6e 66 69 67 20 57 48 45 52 45 20 6e 61 6d 65  onfig WHERE name
7940: 3d 25 51 22 0a 20 20 20 20 20 20 20 22 20 55 4e  =%Q".       " UN
7950: 49 4f 4e 20 41 4c 4c 20 22 0a 20 20 20 20 20 20  ION ALL ".      
7960: 20 22 53 45 4c 45 43 54 20 27 28 67 6c 6f 62 61   "SELECT '(globa
7970: 6c 29 27 2c 20 76 61 6c 75 65 20 46 52 4f 4d 20  l)', value FROM 
7980: 67 6c 6f 62 61 6c 5f 63 6f 6e 66 69 67 20 57 48  global_config WH
7990: 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c 0a 20 20  ERE name=%Q",.  
79a0: 20 20 20 20 20 7a 4e 61 6d 65 2c 20 7a 4e 61 6d       zName, zNam
79b0: 65 0a 20 20 20 20 29 3b 0a 20 20 7d 65 6c 73 65  e.    );.  }else
79c0: 7b 0a 20 20 20 20 64 62 5f 70 72 65 70 61 72 65  {.    db_prepare
79d0: 28 26 71 2c 0a 20 20 20 20 20 20 22 53 45 4c 45  (&q,.      "SELE
79e0: 43 54 20 27 28 67 6c 6f 62 61 6c 29 27 2c 20 76  CT '(global)', v
79f0: 61 6c 75 65 20 46 52 4f 4d 20 67 6c 6f 62 61 6c  alue FROM global
7a00: 5f 63 6f 6e 66 69 67 20 57 48 45 52 45 20 6e 61  _config WHERE na
7a10: 6d 65 3d 25 51 22 2c 0a 20 20 20 20 20 20 7a 4e  me=%Q",.      zN
7a20: 61 6d 65 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 20  ame.    );.  }. 
7a30: 20 69 66 28 20 64 62 5f 73 74 65 70 28 26 71 29   if( db_step(&q)
7a40: 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  ==SQLITE_ROW ){.
7a50: 20 20 20 20 70 72 69 6e 74 66 28 22 25 2d 32 30      printf("%-20
7a60: 73 20 25 2d 38 73 20 25 73 5c 6e 22 2c 20 7a 4e  s %-8s %s\n", zN
7a70: 61 6d 65 2c 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74  ame, db_column_t
7a80: 65 78 74 28 26 71 2c 20 30 29 2c 0a 20 20 20 20  ext(&q, 0),.    
7a90: 20 20 20 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65      db_column_te
7aa0: 78 74 28 26 71 2c 20 31 29 29 3b 0a 20 20 7d 65  xt(&q, 1));.  }e
7ab0: 6c 73 65 7b 0a 20 20 20 20 70 72 69 6e 74 66 28  lse{.    printf(
7ac0: 22 25 2d 32 30 73 5c 6e 22 2c 20 7a 4e 61 6d 65  "%-20s\n", zName
7ad0: 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61  );.  }.  db_fina
7ae0: 6c 69 7a 65 28 26 71 29 3b 0a 7d 0a 0a 0a 2f 2a  lize(&q);.}.../*
7af0: 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 73 65 74  .** COMMAND: set
7b00: 74 69 6e 67 73 0a 2a 2a 20 43 4f 4d 4d 41 4e 44  tings.** COMMAND
7b10: 3a 20 75 6e 73 65 74 0a 2a 2a 20 25 66 6f 73 73  : unset.** %foss
7b20: 69 6c 20 73 65 74 74 69 6e 67 20 3f 50 52 4f 50  il setting ?PROP
7b30: 45 52 54 59 3f 20 3f 56 41 4c 55 45 3f 20 3f 2d  ERTY? ?VALUE? ?-
7b40: 67 6c 6f 62 61 6c 3f 0a 2a 2a 20 25 66 6f 73 73  global?.** %foss
7b50: 69 6c 20 75 6e 73 65 74 20 50 52 4f 50 45 52 54  il unset PROPERT
7b60: 59 20 3f 2d 67 6c 6f 62 61 6c 3f 0a 2a 2a 0a 2a  Y ?-global?.**.*
7b70: 2a 20 54 68 65 20 22 73 65 74 74 69 6e 67 22 20  * The "setting" 
7b80: 63 6f 6d 6d 61 6e 64 20 77 69 74 68 20 6e 6f 20  command with no 
7b90: 61 72 67 75 6d 65 6e 74 73 20 6c 69 73 74 73 20  arguments lists 
7ba0: 61 6c 6c 20 70 72 6f 70 65 72 74 69 65 73 20 61  all properties a
7bb0: 6e 64 20 74 68 65 69 72 0a 2a 2a 20 76 61 6c 75  nd their.** valu
7bc0: 65 73 2e 20 20 57 69 74 68 20 6a 75 73 74 20 61  es.  With just a
7bd0: 20 70 72 6f 70 65 72 74 79 20 6e 61 6d 65 20 69   property name i
7be0: 74 20 73 68 6f 77 73 20 74 68 65 20 76 61 6c 75  t shows the valu
7bf0: 65 20 6f 66 20 74 68 61 74 20 70 72 6f 70 65 72  e of that proper
7c00: 74 79 2e 0a 2a 2a 20 57 69 74 68 20 61 20 76 61  ty..** With a va
7c10: 6c 75 65 20 61 72 67 75 6d 65 6e 74 20 69 74 20  lue argument it 
7c20: 63 68 61 6e 67 65 73 20 74 68 65 20 70 72 6f 70  changes the prop
7c30: 65 72 74 79 20 66 6f 72 20 74 68 65 20 63 75 72  erty for the cur
7c40: 72 65 6e 74 20 72 65 70 6f 73 69 74 6f 72 79 2e  rent repository.
7c50: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 75 6e 73 65  .**.** The "unse
7c60: 74 22 20 63 6f 6d 6d 61 6e 64 20 63 6c 65 61 72  t" command clear
7c70: 73 20 61 20 70 72 6f 70 65 72 74 79 20 73 65 74  s a property set
7c80: 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 61  ting..**.**    a
7c90: 75 74 6f 73 79 6e 63 20 20 20 20 20 20 20 20 20  utosync         
7ca0: 49 66 20 65 6e 61 62 6c 65 64 2c 20 61 75 74 6f  If enabled, auto
7cb0: 6d 61 74 69 63 61 6c 6c 79 20 70 75 6c 6c 20 70  matically pull p
7cc0: 72 69 6f 72 20 74 6f 0a 2a 2a 20 20 20 20 20 20  rior to.**      
7cd0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
7ce0: 6f 6d 6d 69 74 20 6f 72 20 75 70 64 61 74 65 20  ommit or update 
7cf0: 61 6e 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  and automaticall
7d00: 79 20 70 75 73 68 0a 2a 2a 20 20 20 20 20 20 20  y push.**       
7d10: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 66                af
7d20: 74 65 72 20 63 6f 6d 6d 69 74 20 6f 72 20 74 61  ter commit or ta
7d30: 67 20 6f 72 20 62 72 61 6e 63 68 20 63 72 65 61  g or branch crea
7d40: 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 64  tion..**.**    d
7d50: 69 66 66 2d 63 6f 6d 6d 61 6e 64 20 20 20 20 20  iff-command     
7d60: 45 78 74 65 72 6e 61 6c 20 63 6f 6d 6d 61 6e 64  External command
7d70: 20 74 6f 20 72 75 6e 20 77 68 65 6e 20 70 65 72   to run when per
7d80: 66 6f 72 6d 69 6e 67 20 61 20 64 69 66 66 2e 0a  forming a diff..
7d90: 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
7da0: 20 20 20 20 20 20 20 49 66 20 75 6e 64 65 66 69         If undefi
7db0: 6e 65 64 2c 20 74 68 65 20 69 6e 74 65 72 6e 61  ned, the interna
7dc0: 6c 20 74 65 78 74 20 64 69 66 66 20 77 69 6c 6c  l text diff will
7dd0: 20 62 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20   be used..**.** 
7de0: 20 20 20 65 64 69 74 6f 72 20 20 20 20 20 20 20     editor       
7df0: 20 20 20 20 54 65 78 74 20 65 64 69 74 6f 72 20      Text editor 
7e00: 63 6f 6d 6d 61 6e 64 20 75 73 65 64 20 66 6f 72  command used for
7e10: 20 63 68 65 63 6b 2d 69 6e 20 63 6f 6d 6d 65 6e   check-in commen
7e20: 74 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 67 64 69  ts..**.**    gdi
7e30: 66 66 2d 63 6f 6d 6d 61 6e 64 20 20 20 20 45 78  ff-command    Ex
7e40: 74 65 72 6e 61 6c 20 63 6f 6d 6d 61 6e 64 20 74  ternal command t
7e50: 6f 20 72 75 6e 20 77 68 65 6e 20 70 65 72 66 6f  o run when perfo
7e60: 72 6d 69 6e 67 20 61 20 67 72 61 70 68 69 63 61  rming a graphica
7e70: 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  l.**            
7e80: 20 20 20 20 20 20 20 20 20 64 69 66 66 2e 20 49           diff. I
7e90: 66 20 75 6e 64 65 66 69 6e 65 64 2c 20 74 65 78  f undefined, tex
7ea0: 74 20 64 69 66 66 20 77 69 6c 6c 20 62 65 20 75  t diff will be u
7eb0: 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 6c 6f  sed..**.**    lo
7ec0: 63 61 6c 61 75 74 68 20 20 20 20 20 20 20 20 49  calauth        I
7ed0: 66 20 65 6e 61 62 6c 65 64 2c 20 72 65 71 75 69  f enabled, requi
7ee0: 72 65 20 74 68 61 74 20 48 54 54 50 20 63 6f 6e  re that HTTP con
7ef0: 6e 65 63 74 69 6f 6e 73 20 66 72 6f 6d 0a 2a 2a  nections from.**
7f00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7f10: 20 20 20 20 20 31 32 37 2e 30 2e 30 2e 31 20 62       127.0.0.1 b
7f20: 65 20 61 75 74 68 65 6e 74 69 63 61 74 65 64 20  e authenticated 
7f30: 62 79 20 70 61 73 73 77 6f 72 64 2e 20 20 49 66  by password.  If
7f40: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
7f50: 20 20 20 20 20 20 20 20 66 61 6c 73 65 2c 20 61          false, a
7f60: 6c 6c 20 48 54 54 50 20 72 65 71 75 65 73 74 73  ll HTTP requests
7f70: 20 66 72 6f 6d 20 6c 6f 63 61 6c 68 6f 73 74 20   from localhost 
7f80: 68 61 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  have.**         
7f90: 20 20 20 20 20 20 20 20 20 20 20 20 75 6e 72 65              unre
7fa0: 73 74 72 69 63 74 65 64 20 61 63 63 65 73 73 20  stricted access 
7fb0: 74 6f 20 74 68 65 20 72 65 70 6f 73 69 74 6f 72  to the repositor
7fc0: 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 63 6c 65 61  y..**.**    clea
7fd0: 72 73 69 67 6e 20 20 20 20 20 20 20 20 57 68 65  rsign        Whe
7fe0: 6e 20 65 6e 61 62 6c 65 64 20 28 74 68 65 20 64  n enabled (the d
7ff0: 65 66 61 75 6c 74 29 2c 20 66 6f 73 73 69 6c 20  efault), fossil 
8000: 77 69 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f 0a  will attempt to.
8010: 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
8020: 20 20 20 20 20 20 20 73 69 67 6e 20 61 6c 6c 20         sign all 
8030: 63 6f 6d 6d 69 74 73 20 77 69 74 68 20 67 70 67  commits with gpg
8040: 2e 20 20 57 68 65 6e 20 64 69 73 61 62 6c 65 64  .  When disabled
8050: 2c 20 63 6f 6d 6d 69 74 73 20 77 69 6c 6c 0a 2a  , commits will.*
8060: 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
8070: 20 20 20 20 20 20 62 65 20 75 6e 73 69 67 6e 65        be unsigne
8080: 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 70 67 70 2d  d..**.**    pgp-
8090: 63 6f 6d 6d 61 6e 64 20 20 20 20 20 20 43 6f 6d  command      Com
80a0: 6d 61 6e 64 20 75 73 65 64 20 74 6f 20 63 6c 65  mand used to cle
80b0: 61 72 2d 73 69 67 6e 20 6d 61 6e 69 66 65 73 74  ar-sign manifest
80c0: 73 20 61 74 20 63 68 65 63 6b 2d 69 6e 2e 0a 2a  s at check-in..*
80d0: 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
80e0: 20 20 20 20 20 20 54 68 65 20 64 65 66 61 75 6c        The defaul
80f0: 74 20 69 73 20 22 67 70 67 20 2d 2d 63 6c 65 61  t is "gpg --clea
8100: 72 73 69 67 6e 20 2d 6f 20 22 2e 0a 2a 2a 0a 2a  rsign -o "..**.*
8110: 2a 20 20 20 20 70 72 6f 78 79 20 20 20 20 20 20  *    proxy      
8120: 20 20 20 20 20 20 55 52 4c 20 6f 66 20 74 68 65        URL of the
8130: 20 48 54 54 50 20 70 72 6f 78 79 2e 20 20 49 66   HTTP proxy.  If
8140: 20 75 6e 64 65 66 69 6e 65 64 20 6f 72 20 22 6f   undefined or "o
8150: 66 66 22 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20  ff" then.**     
8160: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8170: 74 68 65 20 22 68 74 74 70 5f 70 72 6f 78 79 22  the "http_proxy"
8180: 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 76 61 72   environment var
8190: 69 61 62 6c 65 20 69 73 20 63 6f 6e 73 75 6c 74  iable is consult
81a0: 65 64 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ed..**          
81b0: 20 20 20 20 20 20 20 20 20 20 20 49 66 20 74 68             If th
81c0: 65 20 68 74 74 70 5f 70 72 6f 78 79 20 65 6e 76  e http_proxy env
81d0: 69 72 6f 6e 6d 65 6e 74 20 76 61 72 69 61 62 6c  ironment variabl
81e0: 65 20 69 73 20 75 6e 64 65 66 69 6e 65 64 0a 2a  e is undefined.*
81f0: 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
8200: 20 20 20 20 20 20 74 68 65 6e 20 61 20 64 69 72        then a dir
8210: 65 63 74 20 48 54 54 50 20 63 6f 6e 6e 65 63 74  ect HTTP connect
8220: 69 6f 6e 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a  ion is used..**.
8230: 2a 2a 20 20 20 20 77 65 62 2d 62 72 6f 77 73 65  **    web-browse
8240: 72 20 20 20 20 20 20 41 20 73 68 65 6c 6c 20 63  r      A shell c
8250: 6f 6d 6d 61 6e 64 20 75 73 65 64 20 74 6f 20 6c  ommand used to l
8260: 61 75 6e 63 68 20 79 6f 75 72 20 70 72 65 66 65  aunch your prefe
8270: 72 72 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20  rred.**         
8280: 20 20 20 20 20 20 20 20 20 20 20 20 77 65 62 20              web 
8290: 62 72 6f 77 73 65 72 20 77 68 65 6e 20 67 69 76  browser when giv
82a0: 65 6e 20 61 20 55 52 4c 20 61 73 20 61 6e 20 61  en a URL as an a
82b0: 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 20 20 20 20  rgument..**     
82c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82d0: 44 65 66 61 75 6c 74 73 20 74 6f 20 22 73 74 61  Defaults to "sta
82e0: 72 74 22 20 6f 6e 20 77 69 6e 64 6f 77 73 2c 20  rt" on windows, 
82f0: 22 6f 70 65 6e 22 20 6f 6e 20 4d 61 63 2c 0a 2a  "open" on Mac,.*
8300: 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
8310: 20 20 20 20 20 20 61 6e 64 20 22 66 69 72 65 66        and "firef
8320: 6f 78 22 20 6f 6e 20 55 6e 69 78 2e 0a 2a 2f 0a  ox" on Unix..*/.
8330: 76 6f 69 64 20 73 65 74 74 69 6e 67 5f 63 6d 64  void setting_cmd
8340: 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63  (void){.  static
8350: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 7a 4e   const char *azN
8360: 61 6d 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 22 61  ame[] = {.    "a
8370: 75 74 6f 73 79 6e 63 22 2c 0a 20 20 20 20 22 64  utosync",.    "d
8380: 69 66 66 2d 63 6f 6d 6d 61 6e 64 22 2c 0a 20 20  iff-command",.  
8390: 20 20 22 65 64 69 74 6f 72 22 2c 0a 20 20 20 20    "editor",.    
83a0: 22 67 64 69 66 66 2d 63 6f 6d 6d 61 6e 64 22 2c  "gdiff-command",
83b0: 0a 20 20 20 20 22 6c 6f 63 61 6c 61 75 74 68 22  .    "localauth"
83c0: 2c 0a 20 20 20 20 22 63 6c 65 61 72 73 69 67 6e  ,.    "clearsign
83d0: 22 2c 0a 20 20 20 20 22 70 67 70 2d 63 6f 6d 6d  ",.    "pgp-comm
83e0: 61 6e 64 22 2c 0a 20 20 20 20 22 70 72 6f 78 79  and",.    "proxy
83f0: 22 2c 0a 20 20 20 20 22 77 65 62 2d 62 72 6f 77  ",.    "web-brow
8400: 73 65 72 22 2c 0a 20 20 7d 3b 0a 20 20 69 6e 74  ser",.  };.  int
8410: 20 69 3b 0a 20 20 69 6e 74 20 67 6c 6f 62 61 6c   i;.  int global
8420: 46 6c 61 67 20 3d 20 66 69 6e 64 5f 6f 70 74 69  Flag = find_opti
8430: 6f 6e 28 22 67 6c 6f 62 61 6c 22 2c 22 67 22 2c  on("global","g",
8440: 30 29 21 3d 30 3b 0a 20 20 69 6e 74 20 75 6e 73  0)!=0;.  int uns
8450: 65 74 46 6c 61 67 20 3d 20 67 2e 61 72 67 76 5b  etFlag = g.argv[
8460: 31 5d 5b 30 5d 3d 3d 27 75 27 3b 0a 20 20 64 62  1][0]=='u';.  db
8470: 5f 66 69 6e 64 5f 61 6e 64 5f 6f 70 65 6e 5f 72  _find_and_open_r
8480: 65 70 6f 73 69 74 6f 72 79 28 30 29 3b 0a 20 20  epository(0);.  
8490: 69 66 28 20 21 67 2e 72 65 70 6f 73 69 74 6f 72  if( !g.repositor
84a0: 79 4f 70 65 6e 20 29 7b 0a 20 20 20 20 64 62 5f  yOpen ){.    db_
84b0: 6f 70 65 6e 5f 63 6f 6e 66 69 67 28 29 3b 0a 20  open_config();. 
84c0: 20 20 20 67 6c 6f 62 61 6c 46 6c 61 67 20 3d 20     globalFlag = 
84d0: 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 75 6e 73  1;.  }.  if( uns
84e0: 65 74 46 6c 61 67 20 26 26 20 67 2e 61 72 67 63  etFlag && g.argc
84f0: 21 3d 33 20 29 7b 0a 20 20 20 20 75 73 61 67 65  !=3 ){.    usage
8500: 28 22 50 52 4f 50 45 52 54 59 20 3f 2d 67 6c 6f  ("PROPERTY ?-glo
8510: 62 61 6c 3f 22 29 3b 0a 20 20 7d 0a 20 20 69 66  bal?");.  }.  if
8520: 28 20 67 2e 61 72 67 63 3d 3d 32 20 29 7b 0a 20  ( g.argc==2 ){. 
8530: 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69     for(i=0; i<si
8540: 7a 65 6f 66 28 61 7a 4e 61 6d 65 29 2f 73 69 7a  zeof(azName)/siz
8550: 65 6f 66 28 61 7a 4e 61 6d 65 5b 30 5d 29 3b 20  eof(azName[0]); 
8560: 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 72 69 6e  i++){.      prin
8570: 74 5f 73 65 74 74 69 6e 67 28 61 7a 4e 61 6d 65  t_setting(azName
8580: 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  [i]);.    }.  }e
8590: 6c 73 65 20 69 66 28 20 67 2e 61 72 67 63 3d 3d  lse if( g.argc==
85a0: 33 20 7c 7c 20 67 2e 61 72 67 63 3d 3d 34 20 29  3 || g.argc==4 )
85b0: 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
85c0: 20 2a 7a 4e 61 6d 65 20 3d 20 67 2e 61 72 67 76   *zName = g.argv
85d0: 5b 32 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d  [2];.    int n =
85e0: 20 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 3b 0a   strlen(zName);.
85f0: 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73      for(i=0; i<s
8600: 69 7a 65 6f 66 28 61 7a 4e 61 6d 65 29 2f 73 69  izeof(azName)/si
8610: 7a 65 6f 66 28 61 7a 4e 61 6d 65 5b 30 5d 29 3b  zeof(azName[0]);
8620: 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
8630: 20 73 74 72 6e 63 6d 70 28 61 7a 4e 61 6d 65 5b   strncmp(azName[
8640: 69 5d 2c 20 7a 4e 61 6d 65 2c 20 6e 29 3d 3d 30  i], zName, n)==0
8650: 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a   ) break;.    }.
8660: 20 20 20 20 69 66 28 20 69 3e 3d 73 69 7a 65 6f      if( i>=sizeo
8670: 66 28 61 7a 4e 61 6d 65 29 2f 73 69 7a 65 6f 66  f(azName)/sizeof
8680: 28 61 7a 4e 61 6d 65 5b 30 5d 29 20 29 7b 0a 20  (azName[0]) ){. 
8690: 20 20 20 20 20 66 6f 73 73 69 6c 5f 66 61 74 61       fossil_fata
86a0: 6c 28 22 6e 6f 20 73 75 63 68 20 73 65 74 74 69  l("no such setti
86b0: 6e 67 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b  ng: %s", zName);
86c0: 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75  .    }.    if( u
86d0: 6e 73 65 74 46 6c 61 67 20 29 7b 0a 20 20 20 20  nsetFlag ){.    
86e0: 20 20 64 62 5f 75 6e 73 65 74 28 61 7a 4e 61 6d    db_unset(azNam
86f0: 65 5b 69 5d 2c 20 67 6c 6f 62 61 6c 46 6c 61 67  e[i], globalFlag
8700: 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  );.    }else if(
8710: 20 67 2e 61 72 67 63 3d 3d 34 20 29 7b 0a 20 20   g.argc==4 ){.  
8720: 20 20 20 20 64 62 5f 73 65 74 28 61 7a 4e 61 6d      db_set(azNam
8730: 65 5b 69 5d 2c 20 67 2e 61 72 67 76 5b 33 5d 2c  e[i], g.argv[3],
8740: 20 67 6c 6f 62 61 6c 46 6c 61 67 29 3b 0a 20 20   globalFlag);.  
8750: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
8760: 72 69 6e 74 5f 73 65 74 74 69 6e 67 28 61 7a 4e  rint_setting(azN
8770: 61 6d 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20  ame[i]);.    }. 
8780: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 73 61 67   }else{.    usag
8790: 65 28 22 3f 50 52 4f 50 45 52 54 59 3f 20 3f 56  e("?PROPERTY? ?V
87a0: 41 4c 55 45 3f 22 29 3b 0a 20 20 7d 0a 7d 0a 0a  ALUE?");.  }.}..
87b0: 2f 2a 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69  /*.** SQL functi
87c0: 6f 6e 20 74 6f 20 72 65 6e 64 65 72 20 61 20 55  on to render a U
87d0: 55 49 44 20 61 73 20 61 20 68 79 70 65 72 6c 69  UID as a hyperli
87e0: 6e 6b 20 74 6f 20 61 20 70 61 67 65 20 64 65 73  nk to a page des
87f0: 63 72 69 62 69 6e 67 0a 2a 2a 20 74 68 61 74 20  cribing.** that 
8800: 55 55 49 44 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  UUID..*/.static 
8810: 76 6f 69 64 20 68 79 70 65 72 6c 69 6e 6b 55 75  void hyperlinkUu
8820: 69 64 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  idFunc(.  sqlite
8830: 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 78 74 2c  3_context *pCxt,
8840: 20 20 20 20 20 2f 2a 20 66 75 6e 63 74 69 6f 6e       /* function
8850: 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e   context */.  in
8860: 74 20 61 72 67 63 2c 20 20 20 20 20 20 20 20 20  t argc,         
8870: 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 75 6d 62           /* numb
8880: 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20  er of arguments 
8890: 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  to the function 
88a0: 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  */.  sqlite3_val
88b0: 75 65 20 2a 2a 61 72 67 76 20 20 20 20 20 20 20  ue **argv       
88c0: 2f 2a 20 76 61 6c 75 65 73 20 6f 66 20 61 6c 6c  /* values of all
88d0: 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65   function argume
88e0: 6e 74 73 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73  nts */.){.  cons
88f0: 74 20 63 68 61 72 20 2a 7a 55 75 69 64 3b 20 20  t char *zUuid;  
8900: 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 55 55         /* The UU
8910: 49 44 20 74 6f 20 72 65 6e 64 65 72 20 2a 2f 0a  ID to render */.
8920: 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20    char *z;      
8930: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
8940: 52 65 6e 64 65 72 65 64 20 48 54 4d 4c 20 74 65  Rendered HTML te
8950: 78 74 20 2a 2f 0a 0a 20 20 7a 55 75 69 64 20 3d  xt */..  zUuid =
8960: 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71   (const char*)sq
8970: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
8980: 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28  (argv[0]);.  if(
8990: 20 67 2e 6f 6b 48 69 73 74 6f 72 79 20 26 26 20   g.okHistory && 
89a0: 7a 55 75 69 64 20 26 26 20 73 74 72 6c 65 6e 28  zUuid && strlen(
89b0: 7a 55 75 69 64 29 3e 3d 31 30 20 29 7b 0a 20 20  zUuid)>=10 ){.  
89c0: 20 20 7a 20 3d 20 6d 70 72 69 6e 74 66 28 22 3c    z = mprintf("<
89d0: 74 74 3e 3c 61 20 68 72 65 66 3d 27 25 73 2f 76  tt><a href='%s/v
89e0: 69 6e 66 6f 2f 25 74 27 3e 3c 73 70 61 6e 20 73  info/%t'><span s
89f0: 74 79 6c 65 3d 27 66 6f 6e 74 2d 73 69 7a 65 3a  tyle='font-size:
8a00: 31 2e 35 65 6d 27 3e 22 0a 20 20 20 20 20 20 20  1.5em'>".       
8a10: 20 20 20 20 20 20 20 20 20 22 25 23 68 3c 2f 73           "%#h</s
8a20: 70 61 6e 3e 25 68 3c 2f 61 3e 3c 2f 74 74 3e 22  pan>%h</a></tt>"
8a30: 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
8a40: 20 20 67 2e 7a 42 61 73 65 55 52 4c 2c 20 7a 55    g.zBaseURL, zU
8a50: 75 69 64 2c 20 31 30 2c 20 7a 55 75 69 64 2c 20  uid, 10, zUuid, 
8a60: 26 7a 55 75 69 64 5b 31 30 5d 29 3b 0a 20 20 20  &zUuid[10]);.   
8a70: 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
8a80: 74 65 78 74 28 70 43 78 74 2c 20 7a 2c 20 2d 31  text(pCxt, z, -1
8a90: 2c 20 66 72 65 65 29 3b 0a 20 20 7d 65 6c 73 65  , free);.  }else
8aa0: 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
8ab0: 73 75 6c 74 5f 74 65 78 74 28 70 43 78 74 2c 20  sult_text(pCxt, 
8ac0: 7a 55 75 69 64 2c 20 2d 31 2c 20 53 51 4c 49 54  zUuid, -1, SQLIT
8ad0: 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  E_TRANSIENT);.  
8ae0: 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 20 66  }.}../*.** SQL f
8af0: 75 6e 63 74 69 6f 6e 20 74 6f 20 72 65 6e 64 65  unction to rende
8b00: 72 20 61 20 54 41 47 49 44 20 61 73 20 61 20 68  r a TAGID as a h
8b10: 79 70 65 72 6c 69 6e 6b 20 74 6f 20 61 20 70 61  yperlink to a pa
8b20: 67 65 20 64 65 73 63 72 69 62 69 6e 67 0a 2a 2a  ge describing.**
8b30: 20 74 68 61 74 20 74 61 67 2e 0a 2a 2f 0a 73 74   that tag..*/.st
8b40: 61 74 69 63 20 76 6f 69 64 20 68 79 70 65 72 6c  atic void hyperl
8b50: 69 6e 6b 54 61 67 69 64 46 75 6e 63 28 0a 20 20  inkTagidFunc(.  
8b60: 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
8b70: 2a 70 43 78 74 2c 20 20 20 20 20 2f 2a 20 66 75  *pCxt,     /* fu
8b80: 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a  nction context *
8b90: 2f 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 20 20  /.  int argc,   
8ba0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
8bb0: 2a 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  * number of argu
8bc0: 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75 6e  ments to the fun
8bd0: 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74  ction */.  sqlit
8be0: 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 20  e3_value **argv 
8bf0: 20 20 20 20 20 20 2f 2a 20 76 61 6c 75 65 73 20        /* values 
8c00: 6f 66 20 61 6c 6c 20 66 75 6e 63 74 69 6f 6e 20  of all function 
8c10: 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 29 7b 0a  arguments */.){.
8c20: 20 20 69 6e 74 20 74 61 67 69 64 3b 20 20 20 20    int tagid;    
8c30: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
8c40: 54 68 65 20 74 61 67 69 64 20 74 6f 20 72 65 6e  The tagid to ren
8c50: 64 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  der */.  char *z
8c60: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
8c70: 20 20 20 20 2f 2a 20 72 65 6e 64 65 72 65 64 20      /* rendered 
8c80: 68 74 6d 6c 20 74 65 78 74 20 2a 2f 0a 0a 20 20  html text */..  
8c90: 74 61 67 69 64 20 3d 20 73 71 6c 69 74 65 33 5f  tagid = sqlite3_
8ca0: 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 30  value_int(argv[0
8cb0: 5d 29 3b 0a 20 20 69 66 28 20 67 2e 6f 6b 48 69  ]);.  if( g.okHi
8cc0: 73 74 6f 72 79 20 29 7b 0a 20 20 20 20 7a 20 3d  story ){.    z =
8cd0: 20 6d 70 72 69 6e 74 66 28 22 3c 61 20 68 72 65   mprintf("<a hre
8ce0: 66 3d 27 25 73 2f 74 61 67 76 69 65 77 3f 74 61  f='%s/tagview?ta
8cf0: 67 69 64 3d 25 64 27 3e 25 64 3c 2f 61 3e 22 2c  gid=%d'>%d</a>",
8d00: 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
8d10: 20 20 20 20 67 2e 7a 42 61 73 65 55 52 4c 2c 20      g.zBaseURL, 
8d20: 74 61 67 69 64 2c 20 74 61 67 69 64 29 3b 0a 20  tagid, tagid);. 
8d30: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20   }else{.    z = 
8d40: 6d 70 72 69 6e 74 66 28 22 25 64 22 2c 20 74 61  mprintf("%d", ta
8d50: 67 69 64 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  gid);.  }.  sqli
8d60: 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
8d70: 70 43 78 74 2c 20 7a 2c 20 2d 31 2c 20 66 72 65  pCxt, z, -1, fre
8d80: 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c  e);.}../*.** SQL
8d90: 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 72 65 6e   function to ren
8da0: 64 65 72 20 61 20 54 41 47 4e 41 4d 45 20 61 73  der a TAGNAME as
8db0: 20 61 20 68 79 70 65 72 6c 69 6e 6b 20 74 6f 20   a hyperlink to 
8dc0: 61 20 70 61 67 65 20 64 65 73 63 72 69 62 69 6e  a page describin
8dd0: 67 0a 2a 2a 20 74 68 61 74 20 74 61 67 2e 0a 2a  g.** that tag..*
8de0: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 79  /.static void hy
8df0: 70 65 72 6c 69 6e 6b 54 61 67 6e 61 6d 65 46 75  perlinkTagnameFu
8e00: 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
8e10: 6e 74 65 78 74 20 2a 70 43 78 74 2c 20 20 20 20  ntext *pCxt,    
8e20: 20 2f 2a 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e   /* function con
8e30: 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 61 72  text */.  int ar
8e40: 67 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  gc,             
8e50: 20 20 20 20 20 2f 2a 20 6e 75 6d 62 65 72 20 6f       /* number o
8e60: 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74  f arguments to t
8e70: 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  he function */. 
8e80: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
8e90: 2a 61 72 67 76 20 20 20 20 20 20 20 2f 2a 20 76  *argv       /* v
8ea0: 61 6c 75 65 73 20 6f 66 20 61 6c 6c 20 66 75 6e  alues of all fun
8eb0: 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20  ction arguments 
8ec0: 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68  */.){.  const ch
8ed0: 61 72 20 2a 7a 54 61 67 3b 20 20 20 20 20 20 20  ar *zTag;       
8ee0: 20 20 20 2f 2a 20 54 68 65 20 74 61 67 20 74 6f     /* The tag to
8ef0: 20 72 65 6e 64 65 72 20 2a 2f 0a 20 20 63 68 61   render */.  cha
8f00: 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20  r *z;           
8f10: 20 20 20 20 20 20 20 20 2f 2a 20 72 65 6e 64 65          /* rende
8f20: 72 65 64 20 68 74 6d 6c 20 74 65 78 74 20 2a 2f  red html text */
8f30: 0a 0a 20 20 7a 54 61 67 20 3d 20 28 63 6f 6e 73  ..  zTag = (cons
8f40: 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f  t char*)sqlite3_
8f50: 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
8f60: 30 5d 29 3b 0a 20 20 69 66 28 20 67 2e 6f 6b 48  0]);.  if( g.okH
8f70: 69 73 74 6f 72 79 20 29 7b 0a 20 20 20 20 7a 20  istory ){.    z 
8f80: 3d 20 6d 70 72 69 6e 74 66 28 22 3c 61 20 68 72  = mprintf("<a hr
8f90: 65 66 3d 27 25 73 2f 74 61 67 76 69 65 77 3f 6e  ef='%s/tagview?n
8fa0: 61 6d 65 3d 25 54 27 3e 25 68 3c 2f 61 3e 22 2c  ame=%T'>%h</a>",
8fb0: 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
8fc0: 20 20 20 20 67 2e 7a 42 61 73 65 55 52 4c 2c 20      g.zBaseURL, 
8fd0: 7a 54 61 67 2c 20 7a 54 61 67 29 3b 0a 20 20 7d  zTag, zTag);.  }
8fe0: 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 6d 70  else{.    z = mp
8ff0: 72 69 6e 74 66 28 22 25 68 22 2c 20 7a 54 61 67  rintf("%h", zTag
9000: 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
9010: 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43 78  _result_text(pCx
9020: 74 2c 20 7a 2c 20 2d 31 2c 20 66 72 65 65 29 3b  t, z, -1, free);
9030: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 20 66 75  .}../*.** SQL fu
9040: 6e 63 74 69 6f 6e 20 74 6f 20 65 73 63 61 70 65  nction to escape
9050: 20 61 6c 6c 20 63 68 61 72 61 63 74 65 72 73 20   all characters 
9060: 69 6e 20 61 20 73 74 72 69 6e 67 20 74 68 61 74  in a string that
9070: 20 68 61 76 65 20 73 70 65 63 69 61 6c 0a 2a 2a   have special.**
9080: 20 6d 65 61 6e 69 6e 67 20 74 6f 20 48 54 4d 4c   meaning to HTML
9090: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
90a0: 20 68 74 6d 6c 69 7a 65 46 75 6e 63 28 0a 20 20   htmlizeFunc(.  
90b0: 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
90c0: 2a 70 43 78 74 2c 20 20 20 20 20 2f 2a 20 66 75  *pCxt,     /* fu
90d0: 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a  nction context *
90e0: 2f 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 20 20  /.  int argc,   
90f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
9100: 2a 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  * number of argu
9110: 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75 6e  ments to the fun
9120: 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74  ction */.  sqlit
9130: 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 20  e3_value **argv 
9140: 20 20 20 20 20 20 2f 2a 20 76 61 6c 75 65 73 20        /* values 
9150: 6f 66 20 61 6c 6c 20 66 75 6e 63 74 69 6f 6e 20  of all function 
9160: 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 29 7b 0a  arguments */.){.
9170: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
9180: 65 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ext;         /* 
9190: 54 65 78 74 20 74 6f 20 62 65 20 68 74 6d 6c 69  Text to be htmli
91a0: 7a 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  zed */.  char *z
91b0: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
91c0: 20 20 20 20 2f 2a 20 72 65 6e 64 65 72 65 64 20      /* rendered 
91d0: 68 74 6d 6c 20 74 65 78 74 20 2a 2f 0a 0a 20 20  html text */..  
91e0: 7a 54 65 78 74 20 3d 20 28 63 6f 6e 73 74 20 63  zText = (const c
91f0: 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  har*)sqlite3_val
9200: 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
9210: 3b 0a 20 20 7a 20 3d 20 68 74 6d 6c 69 7a 65 28  ;.  z = htmlize(
9220: 7a 54 65 78 74 2c 20 2d 31 29 3b 0a 20 20 73 71  zText, -1);.  sq
9230: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
9240: 74 28 70 43 78 74 2c 20 7a 2c 20 2d 31 2c 20 66  t(pCxt, z, -1, f
9250: 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ree);.}../*.** T
9260: 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
9270: 20 68 65 6c 70 65 72 20 74 6f 20 72 75 6e 20 61   helper to run a
9280: 6e 20 53 51 4c 20 71 75 65 72 79 20 61 6e 64 20  n SQL query and 
9290: 74 61 62 6c 65 2d 69 7a 65 20 74 68 65 0a 2a 2a  table-ize the.**
92a0: 20 72 65 73 75 6c 74 73 2e 0a 2a 2a 0a 2a 2a 20   results..**.** 
92b0: 54 68 65 20 7a 53 71 6c 20 70 61 72 61 6d 65 74  The zSql paramet
92c0: 65 72 20 73 68 6f 75 6c 64 20 62 65 20 61 20 73  er should be a s
92d0: 69 6e 67 6c 65 2c 20 63 6f 6d 70 6c 65 74 65 20  ingle, complete 
92e0: 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  SQL statement..*
92f0: 2a 20 54 61 62 6c 65 69 7a 65 64 20 6f 75 74 70  * Tableized outp
9300: 75 74 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74  ut of the SQL st
9310: 61 74 65 6d 65 6e 74 20 69 73 20 72 65 6e 64 65  atement is rende
9320: 72 65 64 20 62 61 63 6b 20 74 6f 20 74 68 65 20  red back to the 
9330: 63 6c 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  client..**.** Th
9340: 65 20 69 73 53 61 66 65 20 66 6c 61 67 20 69 73  e isSafe flag is
9350: 20 74 72 75 65 20 69 66 20 61 6c 6c 20 71 75 65   true if all que
9360: 72 79 20 72 65 73 75 6c 74 73 20 68 61 76 65 20  ry results have 
9370: 62 65 65 6e 20 70 72 6f 63 65 73 73 65 64 20 0a  been processed .
9380: 2a 2a 20 62 79 20 72 6f 75 74 69 6e 65 73 20 73  ** by routines s
9390: 75 63 68 20 61 73 0a 2a 2a 0a 2a 2a 20 20 20 20  uch as.**.**    
93a0: 20 20 20 20 6c 69 6e 6b 75 75 69 64 28 29 0a 2a      linkuuid().*
93b0: 2a 20 20 20 20 20 20 20 20 6c 69 6e 6b 74 61 67  *        linktag
93c0: 69 64 28 29 0a 2a 2a 20 20 20 20 20 20 20 20 6c  id().**        l
93d0: 69 6e 6b 74 61 67 6e 61 6d 65 28 29 0a 2a 2a 20  inktagname().** 
93e0: 20 20 20 20 20 20 20 68 74 6d 6c 69 7a 65 28 29         htmlize()
93f0: 0a 2a 2a 0a 2a 2a 20 61 6e 64 20 61 72 65 20 74  .**.** and are t
9400: 68 65 72 65 66 6f 72 65 20 73 61 66 65 20 66 6f  herefore safe fo
9410: 72 20 64 69 72 65 63 74 20 72 65 6e 64 65 72 69  r direct renderi
9420: 6e 67 2e 20 20 49 66 20 69 73 53 61 66 65 20 69  ng.  If isSafe i
9430: 73 20 66 61 6c 73 65 2c 0a 2a 2a 20 74 68 65 6e  s false,.** then
9440: 20 61 6c 6c 20 63 68 61 72 61 63 74 65 72 73 20   all characters 
9450: 69 6e 20 74 68 65 20 71 75 65 72 79 20 72 65 73  in the query res
9460: 75 6c 74 20 74 68 61 74 20 68 61 76 65 20 73 70  ult that have sp
9470: 65 63 69 61 6c 20 6d 65 61 6e 69 6e 67 0a 2a 2a  ecial meaning.**
9480: 20 74 6f 20 48 54 4d 4c 20 61 72 65 20 65 73 63   to HTML are esc
9490: 61 70 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  aped..**.** Retu
94a0: 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e  rns SQLITE_OK on
94b0: 20 73 75 63 63 65 73 73 20 61 6e 64 20 61 6e 79   success and any
94c0: 20 6f 74 68 65 72 20 76 61 6c 75 65 20 6f 6e 20   other value on 
94d0: 65 72 72 6f 72 2e 0a 2a 2f 0a 69 6e 74 20 64 62  error..*/.int db
94e0: 5f 67 65 6e 65 72 69 63 5f 71 75 65 72 79 5f 76  _generic_query_v
94f0: 69 65 77 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  iew(const char *
9500: 7a 53 71 6c 2c 20 69 6e 74 20 69 73 53 61 66 65  zSql, int isSafe
9510: 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  ){.  sqlite3_stm
9520: 74 20 2a 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20  t *pStmt;.  int 
9530: 72 63 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 2c 20  rc;.  int nCol, 
9540: 69 3b 0a 20 20 69 6e 74 20 6e 52 6f 77 3b 0a 20  i;.  int nRow;. 
9550: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 6f   const char *zRo
9560: 77 3b 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20  w;.  static int 
9570: 6f 6e 63 65 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20  once = 1;..  /* 
9580: 49 6e 73 74 61 6c 6c 20 74 68 65 20 73 70 65 63  Install the spec
9590: 69 61 6c 20 66 75 6e 63 74 69 6f 6e 73 20 6f 6e  ial functions on
95a0: 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20   the first call 
95b0: 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  to this routine 
95c0: 2a 2f 0a 20 20 69 66 28 20 6f 6e 63 65 20 29 7b  */.  if( once ){
95d0: 0a 20 20 20 20 6f 6e 63 65 20 3d 20 30 3b 0a 20  .    once = 0;. 
95e0: 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
95f0: 65 5f 66 75 6e 63 74 69 6f 6e 28 67 2e 64 62 2c  e_function(g.db,
9600: 20 22 6c 69 6e 6b 75 75 69 64 22 2c 20 31 2c 20   "linkuuid", 1, 
9610: 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 20  SQLITE_UTF8, 0, 
9620: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
9630: 20 20 20 20 20 20 20 20 20 20 20 20 20 68 79 70               hyp
9640: 65 72 6c 69 6e 6b 55 75 69 64 46 75 6e 63 2c 20  erlinkUuidFunc, 
9650: 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
9660: 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
9670: 6f 6e 28 67 2e 64 62 2c 20 22 6c 69 6e 6b 74 61  on(g.db, "linkta
9680: 67 69 64 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f  gid", 1, SQLITE_
9690: 55 54 46 38 2c 20 30 2c 20 0a 20 20 20 20 20 20  UTF8, 0, .      
96a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
96b0: 20 20 20 20 20 20 68 79 70 65 72 6c 69 6e 6b 54        hyperlinkT
96c0: 61 67 69 64 46 75 6e 63 2c 20 30 2c 20 30 29 3b  agidFunc, 0, 0);
96d0: 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
96e0: 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 67 2e 64  ate_function(g.d
96f0: 62 2c 20 22 6c 69 6e 6b 74 61 67 6e 61 6d 65 22  b, "linktagname"
9700: 2c 20 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  , 1, SQLITE_UTF8
9710: 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20  , 0, .          
9720: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9730: 20 20 68 79 70 65 72 6c 69 6e 6b 54 61 67 6e 61    hyperlinkTagna
9740: 6d 65 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 20  meFunc, 0, 0);. 
9750: 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
9760: 65 5f 66 75 6e 63 74 69 6f 6e 28 67 2e 64 62 2c  e_function(g.db,
9770: 20 22 68 74 6d 6c 69 7a 65 22 2c 20 31 2c 20 53   "htmlize", 1, S
9780: 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 0a  QLITE_UTF8, 0, .
9790: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
97a0: 20 20 20 20 20 20 20 20 20 20 20 20 68 74 6d 6c              html
97b0: 69 7a 65 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a  izeFunc, 0, 0);.
97c0: 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 55    }..  /*.  ** U
97d0: 73 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  se sqlite3_stmt 
97e0: 64 69 72 65 63 74 6c 79 20 72 61 74 68 65 72 20  directly rather 
97f0: 74 68 61 6e 20 67 6f 69 6e 67 20 74 68 72 6f 75  than going throu
9800: 67 68 20 64 62 5f 70 72 65 70 61 72 65 28 29 2c  gh db_prepare(),
9810: 0a 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 77 65  .  ** so that we
9820: 20 63 61 6e 20 74 72 65 61 74 20 65 72 72 6f 72   can treat error
9830: 73 20 61 20 6e 6f 6e 2d 66 61 74 61 6c 2e 0a 20  s a non-fatal.. 
9840: 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   */.  rc = sqlit
9850: 65 33 5f 70 72 65 70 61 72 65 28 67 2e 64 62 2c  e3_prepare(g.db,
9860: 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d   zSql, -1, &pStm
9870: 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 53 51 4c  t, 0);.  if( SQL
9880: 49 54 45 5f 4f 4b 20 21 3d 20 72 63 20 29 7b 0a  ITE_OK != rc ){.
9890: 20 20 20 20 40 20 3c 73 70 61 6e 20 73 74 79 6c      @ <span styl
98a0: 65 3d 27 63 6f 6c 6f 72 3a 72 65 64 27 3e 64 62  e='color:red'>db
98b0: 5f 67 65 6e 65 72 69 63 5f 71 75 65 72 79 5f 76  _generic_query_v
98c0: 69 65 77 28 29 20 53 51 4c 20 65 72 72 6f 72 3a  iew() SQL error:
98d0: 0a 20 20 20 20 40 20 25 68 28 73 71 6c 69 74 65  .    @ %h(sqlite
98e0: 33 5f 65 72 72 6d 73 67 28 67 2e 64 62 29 29 3c  3_errmsg(g.db))<
98f0: 2f 73 70 61 6e 3e 0a 20 20 20 20 72 65 74 75 72  /span>.    retur
9900: 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 6e 43 6f 6c  n rc;.  }.  nCol
9910: 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
9920: 6e 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a  n_count(pStmt);.
9930: 20 20 40 20 3c 74 61 62 6c 65 20 63 6c 61 73 73    @ <table class
9940: 3d 27 66 6f 73 73 69 6c 5f 64 62 5f 67 65 6e 65  ='fossil_db_gene
9950: 72 69 63 5f 71 75 65 72 79 5f 76 69 65 77 27 3e  ric_query_view'>
9960: 3c 74 62 6f 64 79 3e 0a 20 20 40 20 3c 74 72 20  <tbody>.  @ <tr 
9970: 63 6c 61 73 73 3d 27 68 65 61 64 65 72 27 3e 0a  class='header'>.
9980: 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f    for(i=0; i<nCo
9990: 6c 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 40 20 3c  l; ++i){.    @ <
99a0: 74 64 3e 25 68 28 73 71 6c 69 74 65 33 5f 63 6f  td>%h(sqlite3_co
99b0: 6c 75 6d 6e 5f 6e 61 6d 65 28 70 53 74 6d 74 2c  lumn_name(pStmt,
99c0: 69 29 29 3c 2f 74 64 3e 0a 20 20 7d 0a 20 20 40  i))</td>.  }.  @
99d0: 20 3c 2f 74 72 3e 0a 0a 20 20 6e 52 6f 77 20 3d   </tr>..  nRow =
99e0: 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 53 51 4c   0;.  while( SQL
99f0: 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33  ITE_ROW==sqlite3
9a00: 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29 7b 0a  _step(pStmt) ){.
9a10: 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
9a20: 61 7a 43 6c 61 73 73 5b 5d 20 3d 20 7b 20 22 65  azClass[] = { "e
9a30: 76 65 6e 22 2c 20 22 6f 64 64 22 20 7d 3b 0a 20  ven", "odd" };. 
9a40: 20 20 20 40 20 3c 74 72 20 63 6c 61 73 73 3d 27     @ <tr class='
9a50: 25 73 28 61 7a 43 6c 61 73 73 5b 28 6e 52 6f 77  %s(azClass[(nRow
9a60: 2b 2b 29 26 31 5d 29 27 3e 0a 20 20 20 20 20 20  ++)&1])'>.      
9a70: 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b  for(i=0; i<nCol;
9a80: 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 7a   i++){.        z
9a90: 52 6f 77 20 3d 20 28 63 68 61 72 20 63 6f 6e 73  Row = (char cons
9aa0: 74 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  t*)sqlite3_colum
9ab0: 6e 5f 74 65 78 74 28 70 53 74 6d 74 2c 69 29 3b  n_text(pStmt,i);
9ac0: 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 73 53  .        if( isS
9ad0: 61 66 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  afe ){.         
9ae0: 20 40 20 3c 74 64 3e 25 73 28 7a 52 6f 77 29 3c   @ <td>%s(zRow)<
9af0: 2f 74 64 3e 0a 20 20 20 20 20 20 20 20 7d 65 6c  /td>.        }el
9b00: 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 40 20  se{.          @ 
9b10: 3c 74 64 3e 25 68 28 7a 52 6f 77 29 3c 2f 74 64  <td>%h(zRow)</td
9b20: 3e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  >.        }.    
9b30: 20 20 7d 0a 20 20 20 20 40 20 3c 2f 74 72 3e 0a    }.    @ </tr>.
9b40: 20 20 7d 0a 20 20 40 20 3c 2f 74 62 6f 64 79 3e    }.  @ </tbody>
9b50: 3c 2f 74 61 62 6c 65 3e 0a 20 20 73 71 6c 69 74  </table>.  sqlit
9b60: 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d  e3_finalize(pStm
9b70: 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  t);.  return SQL
9b80: 49 54 45 5f 4f 4b 3b 0a 7d 0a                    ITE_OK;.}.