Hex Artifact Content
Not logged in

Artifact 3bee80077521f9904660ea0b411c111ee69e42ea:

File src/db.c part of check-in [7351b6346d] - Add the "/doc" method on the server. by drh on 2008-05-15 16:58:42.

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 7d 3b  _prepare() */.};
0660: 0a 23 65 6e 64 69 66 20 2f 2a 20 49 4e 54 45 52  .#endif /* INTER
0670: 46 41 43 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43  FACE */../*.** C
0680: 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  all this routine
0690: 20 77 68 65 6e 20 61 20 64 61 74 61 62 61 73 65   when a database
06a0: 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a   error occurs..*
06b0: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 62  /.static void db
06c0: 5f 65 72 72 28 63 6f 6e 73 74 20 63 68 61 72 20  _err(const char 
06d0: 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a  *zFormat, ...){.
06e0: 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20    va_list ap;.  
06f0: 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 73 74  char *z;.  va_st
0700: 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29  art(ap, zFormat)
0710: 3b 0a 20 20 7a 20 3d 20 76 6d 70 72 69 6e 74 66  ;.  z = vmprintf
0720: 28 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20  (zFormat, ap);. 
0730: 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 69   va_end(ap);.  i
0740: 66 28 20 67 2e 63 67 69 50 61 6e 69 63 20 29 7b  f( g.cgiPanic ){
0750: 0a 20 20 20 20 67 2e 63 67 69 50 61 6e 69 63 20  .    g.cgiPanic 
0760: 3d 20 30 3b 0a 20 20 20 20 63 67 69 5f 70 72 69  = 0;.    cgi_pri
0770: 6e 74 66 28 22 3c 70 3e 3c 66 6f 6e 74 20 63 6f  ntf("<p><font co
0780: 6c 6f 72 3d 5c 22 72 65 64 5c 22 3e 25 68 3c 2f  lor=\"red\">%h</
0790: 66 6f 6e 74 3e 3c 2f 70 3e 22 2c 20 7a 29 3b 0a  font></p>", z);.
07a0: 20 20 20 20 73 74 79 6c 65 5f 66 6f 6f 74 65 72      style_footer
07b0: 28 29 3b 0a 20 20 20 20 63 67 69 5f 72 65 70 6c  ();.    cgi_repl
07c0: 79 28 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  y();.  }else{.  
07d0: 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72    fprintf(stderr
07e0: 2c 20 22 25 73 3a 20 25 73 5c 6e 22 2c 20 67 2e  , "%s: %s\n", g.
07f0: 61 72 67 76 5b 30 5d 2c 20 7a 29 3b 0a 20 20 7d  argv[0], z);.  }
0800: 0a 20 20 64 62 5f 66 6f 72 63 65 5f 72 6f 6c 6c  .  db_force_roll
0810: 62 61 63 6b 28 29 3b 0a 20 20 65 78 69 74 28 31  back();.  exit(1
0820: 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74  );.}..static int
0830: 20 6e 42 65 67 69 6e 20 3d 20 30 3b 20 20 20 20   nBegin = 0;    
0840: 20 20 2f 2a 20 4e 65 73 74 69 6e 67 20 64 65 70    /* Nesting dep
0850: 74 68 20 6f 66 20 42 45 47 49 4e 20 2a 2f 0a 73  th of BEGIN */.s
0860: 74 61 74 69 63 20 69 6e 74 20 69 73 4e 65 77 52  tatic int isNewR
0870: 65 70 6f 20 3d 20 30 3b 20 20 20 2f 2a 20 54 72  epo = 0;   /* Tr
0880: 75 65 20 69 66 20 74 68 65 20 72 65 70 6f 73 69  ue if the reposi
0890: 74 6f 72 79 20 69 73 20 6e 65 77 6c 79 20 63 72  tory is newly cr
08a0: 65 61 74 65 64 20 2a 2f 0a 73 74 61 74 69 63 20  eated */.static 
08b0: 69 6e 74 20 64 6f 52 6f 6c 6c 62 61 63 6b 20 3d  int doRollback =
08c0: 20 30 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f 20   0;  /* True to 
08d0: 66 6f 72 63 65 20 61 20 72 6f 6c 6c 62 61 63 6b  force a rollback
08e0: 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e   */.static int n
08f0: 43 6f 6d 6d 69 74 48 6f 6f 6b 20 3d 20 30 3b 20  CommitHook = 0; 
0900: 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6d  /* Number of com
0910: 6d 69 74 20 68 6f 6f 6b 73 20 2a 2f 0a 73 74 61  mit hooks */.sta
0920: 74 69 63 20 73 74 72 75 63 74 20 73 43 6f 6d 6d  tic struct sComm
0930: 69 74 48 6f 6f 6b 20 7b 0a 20 20 69 6e 74 20 28  itHook {.  int (
0940: 2a 78 48 6f 6f 6b 29 28 76 6f 69 64 29 3b 20 20  *xHook)(void);  
0950: 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 74 6f 20  /* Functions to 
0960: 63 61 6c 6c 20 61 74 20 64 62 5f 65 6e 64 5f 74  call at db_end_t
0970: 72 61 6e 73 61 63 74 69 6f 6e 28 29 20 2a 2f 0a  ransaction() */.
0980: 20 20 69 6e 74 20 73 65 71 75 65 6e 63 65 3b 20    int sequence; 
0990: 20 20 20 20 20 20 20 2f 2a 20 43 61 6c 6c 20 66         /* Call f
09a0: 75 6e 63 74 69 6f 6e 73 20 69 6e 20 73 65 71 75  unctions in sequ
09b0: 65 6e 63 65 20 6f 72 64 65 72 20 2a 2f 0a 7d 20  ence order */.} 
09c0: 61 48 6f 6f 6b 5b 35 5d 3b 0a 0a 2f 2a 0a 2a 2a  aHook[5];../*.**
09d0: 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
09e0: 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 53   called by the S
09f0: 51 4c 69 74 65 20 63 6f 6d 6d 69 74 2d 68 6f 6f  QLite commit-hoo
0a00: 6b 20 6d 65 63 68 61 6e 69 73 6d 0a 2a 2a 20 6a  k mechanism.** j
0a10: 75 73 74 20 70 72 69 6f 72 20 74 6f 20 65 61 63  ust prior to eac
0a20: 68 20 6f 6d 69 74 2e 20 20 41 6c 6c 20 74 68 69  h omit.  All thi
0a30: 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 69  s routine does i
0a40: 73 20 76 65 72 69 66 79 0a 2a 2a 20 74 68 61 74  s verify.** that
0a50: 20 6e 42 65 67 69 6e 20 72 65 61 6c 6c 79 20 69   nBegin really i
0a60: 73 20 7a 65 72 6f 2e 20 20 54 68 61 74 20 69 6e  s zero.  That in
0a70: 73 75 72 65 73 20 74 68 61 74 20 74 72 61 6e 73  sures that trans
0a80: 61 63 74 69 6f 6e 73 0a 2a 2a 20 63 61 6e 6e 6f  actions.** canno
0a90: 74 20 63 6f 6d 6d 69 74 20 62 79 20 61 6e 79 20  t commit by any 
0aa0: 6d 65 61 6e 73 20 6f 74 68 65 72 20 74 68 61 6e  means other than
0ab0: 20 62 79 20 63 61 6c 6c 69 6e 67 20 64 62 5f 65   by calling db_e
0ac0: 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29  nd_transaction()
0ad0: 0a 2a 2a 20 62 65 6c 6f 77 2e 0a 2a 2a 0a 2a 2a  .** below..**.**
0ae0: 20 54 68 69 73 20 69 73 20 6a 75 73 74 20 61 20   This is just a 
0af0: 73 61 66 65 74 79 20 61 6e 64 20 73 61 6e 69 74  safety and sanit
0b00: 79 20 63 68 65 63 6b 2e 0a 2a 2f 0a 73 74 61 74  y check..*/.stat
0b10: 69 63 20 69 6e 74 20 64 62 5f 76 65 72 69 66 79  ic int db_verify
0b20: 5f 61 74 5f 63 6f 6d 6d 69 74 28 76 6f 69 64 20  _at_commit(void 
0b30: 2a 6e 6f 74 55 73 65 64 29 7b 0a 20 20 69 66 28  *notUsed){.  if(
0b40: 20 6e 42 65 67 69 6e 20 29 7b 0a 20 20 20 20 66   nBegin ){.    f
0b50: 6f 73 73 69 6c 5f 70 61 6e 69 63 28 22 69 6c 6c  ossil_panic("ill
0b60: 65 67 61 6c 20 63 6f 6d 6d 69 74 20 61 74 74 65  egal commit atte
0b70: 6d 70 74 22 29 3b 0a 20 20 20 20 72 65 74 75 72  mpt");.    retur
0b80: 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  n 1;.  }.  retur
0b90: 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65  n 0;.}../*.** Be
0ba0: 67 69 6e 20 61 6e 64 20 65 6e 64 20 61 20 6e 65  gin and end a ne
0bb0: 73 74 65 64 20 74 72 61 6e 73 61 63 74 69 6f 6e  sted transaction
0bc0: 0a 2a 2f 0a 76 6f 69 64 20 64 62 5f 62 65 67 69  .*/.void db_begi
0bd0: 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 76 6f  n_transaction(vo
0be0: 69 64 29 7b 0a 20 20 69 66 28 20 6e 42 65 67 69  id){.  if( nBegi
0bf0: 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62 5f 6d  n==0 ){.    db_m
0c00: 75 6c 74 69 5f 65 78 65 63 28 22 42 45 47 49 4e  ulti_exec("BEGIN
0c10: 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ");.    sqlite3_
0c20: 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 67 2e 64 62  commit_hook(g.db
0c30: 2c 20 64 62 5f 76 65 72 69 66 79 5f 61 74 5f 63  , db_verify_at_c
0c40: 6f 6d 6d 69 74 2c 20 30 29 3b 0a 20 20 7d 0a 20  ommit, 0);.  }. 
0c50: 20 6e 42 65 67 69 6e 2b 2b 3b 0a 7d 0a 76 6f 69   nBegin++;.}.voi
0c60: 64 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63  d db_end_transac
0c70: 74 69 6f 6e 28 69 6e 74 20 72 6f 6c 6c 62 61 63  tion(int rollbac
0c80: 6b 46 6c 61 67 29 7b 0a 20 20 69 66 28 20 6e 42  kFlag){.  if( nB
0c90: 65 67 69 6e 3c 3d 30 20 29 20 72 65 74 75 72 6e  egin<=0 ) return
0ca0: 3b 0a 20 20 69 66 28 20 72 6f 6c 6c 62 61 63 6b  ;.  if( rollback
0cb0: 46 6c 61 67 20 29 20 64 6f 52 6f 6c 6c 62 61 63  Flag ) doRollbac
0cc0: 6b 20 3d 20 31 3b 0a 20 20 6e 42 65 67 69 6e 2d  k = 1;.  nBegin-
0cd0: 2d 3b 0a 20 20 69 66 28 20 6e 42 65 67 69 6e 3d  -;.  if( nBegin=
0ce0: 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  =0 ){.    int i;
0cf0: 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 64 6f  .    for(i=0; do
0d00: 52 6f 6c 6c 62 61 63 6b 3d 3d 30 20 26 26 20 69  Rollback==0 && i
0d10: 3c 6e 43 6f 6d 6d 69 74 48 6f 6f 6b 3b 20 69 2b  <nCommitHook; i+
0d20: 2b 29 7b 0a 20 20 20 20 20 20 64 6f 52 6f 6c 6c  +){.      doRoll
0d30: 62 61 63 6b 20 7c 3d 20 61 48 6f 6f 6b 5b 69 5d  back |= aHook[i]
0d40: 2e 78 48 6f 6f 6b 28 29 3b 0a 20 20 20 20 7d 0a  .xHook();.    }.
0d50: 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65      db_multi_exe
0d60: 63 28 64 6f 52 6f 6c 6c 62 61 63 6b 20 3f 20 22  c(doRollback ? "
0d70: 52 4f 4c 4c 42 41 43 4b 22 20 3a 20 22 43 4f 4d  ROLLBACK" : "COM
0d80: 4d 49 54 22 29 3b 0a 20 20 20 20 64 6f 52 6f 6c  MIT");.    doRol
0d90: 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20 7d 0a 7d  lback = 0;.  }.}
0da0: 0a 76 6f 69 64 20 64 62 5f 66 6f 72 63 65 5f 72  .void db_force_r
0db0: 6f 6c 6c 62 61 63 6b 28 76 6f 69 64 29 7b 0a 20  ollback(void){. 
0dc0: 20 69 66 28 20 6e 42 65 67 69 6e 20 29 7b 0a 20   if( nBegin ){. 
0dd0: 20 20 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28     sqlite3_exec(
0de0: 67 2e 64 62 2c 20 22 52 4f 4c 4c 42 41 43 4b 22  g.db, "ROLLBACK"
0df0: 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  , 0, 0, 0);.    
0e00: 69 66 28 20 69 73 4e 65 77 52 65 70 6f 20 29 7b  if( isNewRepo ){
0e10: 0a 20 20 20 20 20 20 64 62 5f 63 6c 6f 73 65 28  .      db_close(
0e20: 29 3b 0a 20 20 20 20 20 20 75 6e 6c 69 6e 6b 28  );.      unlink(
0e30: 67 2e 7a 52 65 70 6f 73 69 74 6f 72 79 4e 61 6d  g.zRepositoryNam
0e40: 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e);.    }.  }.  
0e50: 6e 42 65 67 69 6e 20 3d 20 30 3b 0a 7d 0a 0a 2f  nBegin = 0;.}../
0e60: 2a 0a 2a 2a 20 49 6e 73 74 61 6c 6c 20 61 20 63  *.** Install a c
0e70: 6f 6d 6d 69 74 20 68 6f 6f 6b 2e 20 20 48 6f 6f  ommit hook.  Hoo
0e80: 6b 73 20 61 72 65 20 69 6e 73 74 61 6c 6c 65 64  ks are installed
0e90: 20 69 6e 20 73 65 71 75 65 6e 63 65 20 6f 72 64   in sequence ord
0ea0: 65 72 2e 0a 2a 2a 20 49 74 20 69 73 20 61 6e 20  er..** It is an 
0eb0: 65 72 72 6f 72 20 74 6f 20 69 6e 73 74 61 6c 6c  error to install
0ec0: 20 74 68 65 20 73 61 6d 65 20 63 6f 6d 6d 69 74   the same commit
0ed0: 20 68 6f 6f 6b 20 6d 6f 72 65 20 74 68 61 6e 20   hook more than 
0ee0: 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68  once..**.** Each
0ef0: 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 69 73 20   commit hook is 
0f00: 63 61 6c 6c 65 64 20 28 69 6e 20 6f 72 64 65 72  called (in order
0f10: 20 6f 66 20 61 63 63 65 6e 64 69 6e 67 20 73 65   of accending se
0f20: 71 75 65 6e 63 65 29 20 61 74 0a 2a 2a 20 65 61  quence) at.** ea
0f30: 63 68 20 63 6f 6d 6d 69 74 20 6f 70 65 72 61 74  ch commit operat
0f40: 69 6f 6e 2e 20 20 49 66 20 61 6e 79 20 63 6f 6d  ion.  If any com
0f50: 6d 69 74 20 68 6f 6f 6b 20 72 65 74 75 72 6e 73  mit hook returns
0f60: 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68   non-zero,.** th
0f70: 65 20 73 75 62 73 65 71 75 65 6e 63 65 20 63 6f  e subsequence co
0f80: 6d 6d 69 74 20 68 6f 6f 6b 73 20 61 72 65 20 6f  mmit hooks are o
0f90: 6d 69 74 74 65 64 20 61 6e 64 20 74 68 65 20 74  mitted and the t
0fa0: 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 72 6f  ransaction.** ro
0fb0: 6c 6c 73 20 62 61 63 6b 20 72 61 74 68 65 72 20  lls back rather 
0fc0: 74 68 61 6e 20 63 6f 6d 6d 69 74 2e 20 20 49 74  than commit.  It
0fd0: 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73 69   is the responsi
0fe0: 62 69 6c 69 74 79 20 6f 66 20 74 68 65 0a 2a 2a  bility of the.**
0ff0: 20 68 6f 6f 6b 73 20 74 68 65 6d 73 65 6c 76 65   hooks themselve
1000: 73 20 74 6f 20 69 73 73 75 65 20 61 6e 79 20 65  s to issue any e
1010: 72 72 6f 72 20 6d 65 73 73 61 67 65 73 2e 0a 2a  rror messages..*
1020: 2f 0a 76 6f 69 64 20 64 62 5f 63 6f 6d 6d 69 74  /.void db_commit
1030: 5f 68 6f 6f 6b 28 69 6e 74 20 28 2a 78 29 28 76  _hook(int (*x)(v
1040: 6f 69 64 29 2c 20 69 6e 74 20 73 65 71 75 65 6e  oid), int sequen
1050: 63 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  ce){.  int i;.  
1060: 61 73 73 65 72 74 28 20 6e 43 6f 6d 6d 69 74 48  assert( nCommitH
1070: 6f 6f 6b 20 3c 20 73 69 7a 65 6f 66 28 61 48 6f  ook < sizeof(aHo
1080: 6f 6b 29 2f 73 69 7a 65 6f 66 28 61 48 6f 6f 6b  ok)/sizeof(aHook
1090: 5b 31 5d 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d  [1]) );.  for(i=
10a0: 30 3b 20 69 3c 6e 43 6f 6d 6d 69 74 48 6f 6f 6b  0; i<nCommitHook
10b0: 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65  ; i++){.    asse
10c0: 72 74 28 20 78 21 3d 61 48 6f 6f 6b 5b 69 5d 2e  rt( x!=aHook[i].
10d0: 78 48 6f 6f 6b 20 29 3b 0a 20 20 20 20 69 66 28  xHook );.    if(
10e0: 20 61 48 6f 6f 6b 5b 69 5d 2e 73 65 71 75 65 6e   aHook[i].sequen
10f0: 63 65 3e 73 65 71 75 65 6e 63 65 20 29 7b 0a 20  ce>sequence ){. 
1100: 20 20 20 20 20 69 6e 74 20 73 20 3d 20 73 65 71       int s = seq
1110: 75 65 6e 63 65 3b 0a 20 20 20 20 20 20 69 6e 74  uence;.      int
1120: 20 28 2a 78 53 29 28 76 6f 69 64 29 20 3d 20 78   (*xS)(void) = x
1130: 3b 0a 20 20 20 20 20 20 73 65 71 75 65 6e 63 65  ;.      sequence
1140: 20 3d 20 61 48 6f 6f 6b 5b 69 5d 2e 73 65 71 75   = aHook[i].sequ
1150: 65 6e 63 65 3b 0a 20 20 20 20 20 20 78 20 3d 20  ence;.      x = 
1160: 61 48 6f 6f 6b 5b 69 5d 2e 78 48 6f 6f 6b 3b 0a  aHook[i].xHook;.
1170: 20 20 20 20 20 20 61 48 6f 6f 6b 5b 69 5d 2e 73        aHook[i].s
1180: 65 71 75 65 6e 63 65 20 3d 20 73 3b 0a 20 20 20  equence = s;.   
1190: 20 20 20 61 48 6f 6f 6b 5b 69 5d 2e 78 48 6f 6f     aHook[i].xHoo
11a0: 6b 20 3d 20 78 53 3b 0a 20 20 20 20 7d 0a 20 20  k = xS;.    }.  
11b0: 7d 0a 20 20 61 48 6f 6f 6b 5b 6e 43 6f 6d 6d 69  }.  aHook[nCommi
11c0: 74 48 6f 6f 6b 5d 2e 73 65 71 75 65 6e 63 65 20  tHook].sequence 
11d0: 3d 20 73 65 71 75 65 6e 63 65 3b 0a 20 20 61 48  = sequence;.  aH
11e0: 6f 6f 6b 5b 6e 43 6f 6d 6d 69 74 48 6f 6f 6b 5d  ook[nCommitHook]
11f0: 2e 78 48 6f 6f 6b 20 3d 20 78 3b 0a 20 20 6e 43  .xHook = x;.  nC
1200: 6f 6d 6d 69 74 48 6f 6f 6b 2b 2b 3b 0a 7d 0a 0a  ommitHook++;.}..
1210: 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 6f 72  /*.** Prepare or
1220: 20 72 65 70 72 65 70 61 72 65 20 74 68 65 20 73   reprepare the s
1230: 71 6c 69 74 65 33 20 73 74 61 74 65 6d 65 6e 74  qlite3 statement
1240: 20 66 72 6f 6d 20 74 68 65 20 72 61 77 20 53 51   from the raw SQ
1250: 4c 20 74 65 78 74 2e 0a 2a 2f 0a 73 74 61 74 69  L text..*/.stati
1260: 63 20 76 6f 69 64 20 72 65 70 72 65 70 61 72 65  c void reprepare
1270: 28 53 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20  (Stmt *pStmt){. 
1280: 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
1290: 4e 65 77 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  New;.  if( sqlit
12a0: 65 33 5f 70 72 65 70 61 72 65 28 67 2e 64 62 2c  e3_prepare(g.db,
12b0: 20 62 6c 6f 62 5f 62 75 66 66 65 72 28 26 70 53   blob_buffer(&pS
12c0: 74 6d 74 2d 3e 73 71 6c 29 2c 20 2d 31 2c 20 26  tmt->sql), -1, &
12d0: 70 4e 65 77 2c 20 30 29 21 3d 30 20 29 7b 0a 20  pNew, 0)!=0 ){. 
12e0: 20 20 20 64 62 5f 65 72 72 28 22 25 73 5c 6e 25     db_err("%s\n%
12f0: 73 22 2c 20 62 6c 6f 62 5f 73 74 72 28 26 70 53  s", blob_str(&pS
1300: 74 6d 74 2d 3e 73 71 6c 29 2c 20 73 71 6c 69 74  tmt->sql), sqlit
1310: 65 33 5f 65 72 72 6d 73 67 28 67 2e 64 62 29 29  e3_errmsg(g.db))
1320: 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 74 6d  ;.  }.  if( pStm
1330: 74 2d 3e 70 53 74 6d 74 20 29 7b 0a 20 20 20 20  t->pStmt ){.    
1340: 73 71 6c 69 74 65 33 5f 74 72 61 6e 73 66 65 72  sqlite3_transfer
1350: 5f 62 69 6e 64 69 6e 67 73 28 70 53 74 6d 74 2d  _bindings(pStmt-
1360: 3e 70 53 74 6d 74 2c 20 70 4e 65 77 29 3b 0a 20  >pStmt, pNew);. 
1370: 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c     sqlite3_final
1380: 69 7a 65 28 70 53 74 6d 74 2d 3e 70 53 74 6d 74  ize(pStmt->pStmt
1390: 29 3b 0a 20 20 7d 0a 20 20 70 53 74 6d 74 2d 3e  );.  }.  pStmt->
13a0: 70 53 74 6d 74 20 3d 20 70 4e 65 77 3b 0a 7d 0a  pStmt = pNew;.}.
13b0: 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 61  ./*.** Prepare a
13c0: 20 53 74 6d 74 2e 20 20 41 73 73 75 6d 65 20 74   Stmt.  Assume t
13d0: 68 61 74 20 74 68 65 20 53 74 6d 74 20 69 73 20  hat the Stmt is 
13e0: 70 72 65 76 69 6f 75 73 6c 79 20 75 6e 69 6e 69  previously unini
13f0: 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49 66 20  tialized..** If 
1400: 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67  the input string
1410: 20 63 6f 6e 74 61 69 6e 73 20 6d 75 6c 74 69 70   contains multip
1420: 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  le SQL statement
1430: 73 2c 20 6f 6e 6c 79 20 74 68 65 20 66 69 72 73  s, only the firs
1440: 74 0a 2a 2a 20 6f 6e 65 20 69 73 20 70 72 6f 63  t.** one is proc
1450: 65 73 73 65 64 2e 20 20 41 6c 6c 20 73 74 61 74  essed.  All stat
1460: 65 6d 65 6e 74 73 20 62 65 79 6f 6e 64 20 74 68  ements beyond th
1470: 65 20 66 69 72 73 74 20 61 72 65 20 73 69 6c 65  e first are sile
1480: 6e 74 6c 79 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f  ntly ignored..*/
1490: 0a 69 6e 74 20 64 62 5f 76 70 72 65 70 61 72 65  .int db_vprepare
14a0: 28 53 74 6d 74 20 2a 70 53 74 6d 74 2c 20 63 6f  (Stmt *pStmt, co
14b0: 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61  nst char *zForma
14c0: 74 2c 20 76 61 5f 6c 69 73 74 20 61 70 29 7b 0a  t, va_list ap){.
14d0: 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 70 53 74    blob_zero(&pSt
14e0: 6d 74 2d 3e 73 71 6c 29 3b 0a 20 20 62 6c 6f 62  mt->sql);.  blob
14f0: 5f 76 61 70 70 65 6e 64 66 28 26 70 53 74 6d 74  _vappendf(&pStmt
1500: 2d 3e 73 71 6c 2c 20 7a 46 6f 72 6d 61 74 2c 20  ->sql, zFormat, 
1510: 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70  ap);.  va_end(ap
1520: 29 3b 0a 20 20 70 53 74 6d 74 2d 3e 70 53 74 6d  );.  pStmt->pStm
1530: 74 20 3d 20 30 3b 0a 20 20 72 65 70 72 65 70 61  t = 0;.  reprepa
1540: 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74  re(pStmt);.  ret
1550: 75 72 6e 20 30 3b 0a 7d 0a 69 6e 74 20 64 62 5f  urn 0;.}.int db_
1560: 70 72 65 70 61 72 65 28 53 74 6d 74 20 2a 70 53  prepare(Stmt *pS
1570: 74 6d 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  tmt, const char 
1580: 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a  *zFormat, ...){.
1590: 20 20 69 6e 74 20 72 63 3b 0a 20 20 76 61 5f 6c    int rc;.  va_l
15a0: 69 73 74 20 61 70 3b 0a 20 20 76 61 5f 73 74 61  ist ap;.  va_sta
15b0: 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b  rt(ap, zFormat);
15c0: 0a 20 20 72 63 20 3d 20 64 62 5f 76 70 72 65 70  .  rc = db_vprep
15d0: 61 72 65 28 70 53 74 6d 74 2c 20 7a 46 6f 72 6d  are(pStmt, zForm
15e0: 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e  at, ap);.  va_en
15f0: 64 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  d(ap);.  return 
1600: 72 63 3b 0a 7d 0a 69 6e 74 20 64 62 5f 73 74 61  rc;.}.int db_sta
1610: 74 69 63 5f 70 72 65 70 61 72 65 28 53 74 6d 74  tic_prepare(Stmt
1620: 20 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20 63   *pStmt, const c
1630: 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e  har *zFormat, ..
1640: 2e 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
1650: 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
1660: 62 6c 6f 62 5f 73 69 7a 65 28 26 70 53 74 6d 74  blob_size(&pStmt
1670: 2d 3e 73 71 6c 29 3d 3d 30 20 29 7b 0a 20 20 20  ->sql)==0 ){.   
1680: 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 20   va_list ap;.   
1690: 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46   va_start(ap, zF
16a0: 6f 72 6d 61 74 29 3b 0a 20 20 20 20 72 63 20 3d  ormat);.    rc =
16b0: 20 64 62 5f 76 70 72 65 70 61 72 65 28 70 53 74   db_vprepare(pSt
16c0: 6d 74 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29  mt, zFormat, ap)
16d0: 3b 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29  ;.    va_end(ap)
16e0: 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
16f0: 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  c;.}../*.** Retu
1700: 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  rn the index of 
1710: 61 20 62 69 6e 64 20 70 61 72 61 6d 65 74 65 72  a bind parameter
1720: 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
1730: 61 72 61 6d 49 64 78 28 53 74 6d 74 20 2a 70 53  aramIdx(Stmt *pS
1740: 74 6d 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  tmt, const char 
1750: 2a 7a 50 61 72 61 6d 4e 61 6d 65 29 7b 0a 20 20  *zParamName){.  
1760: 69 6e 74 20 69 20 3d 20 73 71 6c 69 74 65 33 5f  int i = sqlite3_
1770: 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69  bind_parameter_i
1780: 6e 64 65 78 28 70 53 74 6d 74 2d 3e 70 53 74 6d  ndex(pStmt->pStm
1790: 74 2c 20 7a 50 61 72 61 6d 4e 61 6d 65 29 3b 0a  t, zParamName);.
17a0: 20 20 69 66 28 20 69 3d 3d 30 20 29 7b 0a 20 20    if( i==0 ){.  
17b0: 20 20 64 62 5f 65 72 72 28 22 6e 6f 20 73 75 63    db_err("no suc
17c0: 68 20 62 69 6e 64 20 70 61 72 61 6d 65 74 65 72  h bind parameter
17d0: 3a 20 25 73 5c 6e 25 62 22 2c 20 7a 50 61 72 61  : %s\n%b", zPara
17e0: 6d 4e 61 6d 65 2c 20 26 70 53 74 6d 74 2d 3e 73  mName, &pStmt->s
17f0: 71 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ql);.  }.  retur
1800: 6e 20 69 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 42 69 6e  n i;.}./*.** Bin
1810: 64 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 73 74  d an integer, st
1820: 72 69 6e 67 2c 20 6f 72 20 42 6c 6f 62 20 76 61  ring, or Blob va
1830: 6c 75 65 20 74 6f 20 61 20 6e 61 6d 65 64 20 70  lue to a named p
1840: 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 69 6e 74  arameter..*/.int
1850: 20 64 62 5f 62 69 6e 64 5f 69 6e 74 28 53 74 6d   db_bind_int(Stm
1860: 74 20 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20  t *pStmt, const 
1870: 63 68 61 72 20 2a 7a 50 61 72 61 6d 4e 61 6d 65  char *zParamName
1880: 2c 20 69 6e 74 20 69 56 61 6c 75 65 29 7b 0a 20  , int iValue){. 
1890: 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f   return sqlite3_
18a0: 62 69 6e 64 5f 69 6e 74 28 70 53 74 6d 74 2d 3e  bind_int(pStmt->
18b0: 70 53 74 6d 74 2c 20 70 61 72 61 6d 49 64 78 28  pStmt, paramIdx(
18c0: 70 53 74 6d 74 2c 20 7a 50 61 72 61 6d 4e 61 6d  pStmt, zParamNam
18d0: 65 29 2c 20 69 56 61 6c 75 65 29 3b 0a 7d 0a 69  e), iValue);.}.i
18e0: 6e 74 20 64 62 5f 62 69 6e 64 5f 69 6e 74 36 34  nt db_bind_int64
18f0: 28 53 74 6d 74 20 2a 70 53 74 6d 74 2c 20 63 6f  (Stmt *pStmt, co
1900: 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 72 61 6d  nst char *zParam
1910: 4e 61 6d 65 2c 20 69 36 34 20 69 56 61 6c 75 65  Name, i64 iValue
1920: 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
1930: 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
1940: 53 74 6d 74 2d 3e 70 53 74 6d 74 2c 20 70 61 72  Stmt->pStmt, par
1950: 61 6d 49 64 78 28 70 53 74 6d 74 2c 20 7a 50 61  amIdx(pStmt, zPa
1960: 72 61 6d 4e 61 6d 65 29 2c 20 69 56 61 6c 75 65  ramName), iValue
1970: 29 3b 0a 7d 0a 69 6e 74 20 64 62 5f 62 69 6e 64  );.}.int db_bind
1980: 5f 64 6f 75 62 6c 65 28 53 74 6d 74 20 2a 70 53  _double(Stmt *pS
1990: 74 6d 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  tmt, const char 
19a0: 2a 7a 50 61 72 61 6d 4e 61 6d 65 2c 20 64 6f 75  *zParamName, dou
19b0: 62 6c 65 20 72 56 61 6c 75 65 29 7b 0a 20 20 72  ble rValue){.  r
19c0: 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 62 69  eturn sqlite3_bi
19d0: 6e 64 5f 64 6f 75 62 6c 65 28 70 53 74 6d 74 2d  nd_double(pStmt-
19e0: 3e 70 53 74 6d 74 2c 20 70 61 72 61 6d 49 64 78  >pStmt, paramIdx
19f0: 28 70 53 74 6d 74 2c 20 7a 50 61 72 61 6d 4e 61  (pStmt, zParamNa
1a00: 6d 65 29 2c 20 72 56 61 6c 75 65 29 3b 0a 7d 0a  me), rValue);.}.
1a10: 69 6e 74 20 64 62 5f 62 69 6e 64 5f 74 65 78 74  int db_bind_text
1a20: 28 53 74 6d 74 20 2a 70 53 74 6d 74 2c 20 63 6f  (Stmt *pStmt, co
1a30: 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 72 61 6d  nst char *zParam
1a40: 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  Name, const char
1a50: 20 2a 7a 56 61 6c 75 65 29 7b 0a 20 20 72 65 74   *zValue){.  ret
1a60: 75 72 6e 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  urn sqlite3_bind
1a70: 5f 74 65 78 74 28 70 53 74 6d 74 2d 3e 70 53 74  _text(pStmt->pSt
1a80: 6d 74 2c 20 70 61 72 61 6d 49 64 78 28 70 53 74  mt, paramIdx(pSt
1a90: 6d 74 2c 20 7a 50 61 72 61 6d 4e 61 6d 65 29 2c  mt, zParamName),
1aa0: 20 7a 56 61 6c 75 65 2c 0a 20 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 2d 31 2c 20 53 51 4c 49 54 45 5f 53      -1, SQLITE_S
1ad0: 54 41 54 49 43 29 3b 0a 7d 0a 69 6e 74 20 64 62  TATIC);.}.int db
1ae0: 5f 62 69 6e 64 5f 6e 75 6c 6c 28 53 74 6d 74 20  _bind_null(Stmt 
1af0: 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20 63 68  *pStmt, const ch
1b00: 61 72 20 2a 7a 50 61 72 61 6d 4e 61 6d 65 29 7b  ar *zParamName){
1b10: 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
1b20: 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70 53 74 6d  3_bind_null(pStm
1b30: 74 2d 3e 70 53 74 6d 74 2c 20 70 61 72 61 6d 49  t->pStmt, paramI
1b40: 64 78 28 70 53 74 6d 74 2c 20 7a 50 61 72 61 6d  dx(pStmt, zParam
1b50: 4e 61 6d 65 29 29 3b 0a 7d 0a 69 6e 74 20 64 62  Name));.}.int db
1b60: 5f 62 69 6e 64 5f 62 6c 6f 62 28 53 74 6d 74 20  _bind_blob(Stmt 
1b70: 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20 63 68  *pStmt, const ch
1b80: 61 72 20 2a 7a 50 61 72 61 6d 4e 61 6d 65 2c 20  ar *zParamName, 
1b90: 42 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 20  Blob *pBlob){.  
1ba0: 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 62  return sqlite3_b
1bb0: 69 6e 64 5f 62 6c 6f 62 28 70 53 74 6d 74 2d 3e  ind_blob(pStmt->
1bc0: 70 53 74 6d 74 2c 20 70 61 72 61 6d 49 64 78 28  pStmt, paramIdx(
1bd0: 70 53 74 6d 74 2c 20 7a 50 61 72 61 6d 4e 61 6d  pStmt, zParamNam
1be0: 65 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  e),.            
1bf0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 6c                bl
1c00: 6f 62 5f 62 75 66 66 65 72 28 70 42 6c 6f 62 29  ob_buffer(pBlob)
1c10: 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 70 42 6c 6f  , blob_size(pBlo
1c20: 62 29 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  b), SQLITE_STATI
1c30: 43 29 3b 0a 7d 0a 0a 2f 2a 20 62 69 6e 64 5f 73  C);.}../* bind_s
1c40: 74 72 28 29 20 74 72 65 61 74 73 20 61 20 42 6c  tr() treats a Bl
1c50: 6f 62 20 6f 62 6a 65 63 74 20 6c 69 6b 65 20 61  ob object like a
1c60: 20 54 45 58 54 20 73 74 72 69 6e 67 20 61 6e 64   TEXT string and
1c70: 20 62 69 6e 64 73 20 69 74 0a 2a 2a 20 74 6f 20   binds it.** to 
1c80: 74 68 65 20 53 51 4c 20 76 61 72 69 61 62 6c 65  the SQL variable
1c90: 2e 20 20 43 6f 6e 73 74 72 61 73 74 20 74 68 69  .  Constrast thi
1ca0: 73 20 74 6f 20 62 69 6e 64 5f 62 6c 6f 62 28 29  s to bind_blob()
1cb0: 20 77 68 69 63 68 20 74 72 65 61 74 73 0a 2a 2a   which treats.**
1cc0: 20 74 68 65 20 42 6c 6f 62 20 6f 62 6a 65 63 74   the Blob object
1cd0: 20 6c 69 6b 65 20 61 6e 20 53 51 4c 20 42 4c 4f   like an SQL BLO
1ce0: 42 2e 0a 2a 2f 0a 69 6e 74 20 64 62 5f 62 69 6e  B..*/.int db_bin
1cf0: 64 5f 73 74 72 28 53 74 6d 74 20 2a 70 53 74 6d  d_str(Stmt *pStm
1d00: 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  t, const char *z
1d10: 50 61 72 61 6d 4e 61 6d 65 2c 20 42 6c 6f 62 20  ParamName, Blob 
1d20: 2a 70 42 6c 6f 62 29 7b 0a 20 20 72 65 74 75 72  *pBlob){.  retur
1d30: 6e 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74  n sqlite3_bind_t
1d40: 65 78 74 28 70 53 74 6d 74 2d 3e 70 53 74 6d 74  ext(pStmt->pStmt
1d50: 2c 20 70 61 72 61 6d 49 64 78 28 70 53 74 6d 74  , paramIdx(pStmt
1d60: 2c 20 7a 50 61 72 61 6d 4e 61 6d 65 29 2c 0a 20  , 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 62 6c 6f 62 5f 62 75           blob_bu
1d90: 66 66 65 72 28 70 42 6c 6f 62 29 2c 20 62 6c 6f  ffer(pBlob), blo
1da0: 62 5f 73 69 7a 65 28 70 42 6c 6f 62 29 2c 20 53  b_size(pBlob), S
1db0: 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d  QLITE_STATIC);.}
1dc0: 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 70 20 74 68 65  ../*.** Step the
1dd0: 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20   SQL statement. 
1de0: 20 52 65 74 75 72 6e 20 65 69 74 68 65 72 20 53   Return either S
1df0: 51 4c 49 54 45 5f 52 4f 57 20 6f 72 20 61 6e 20  QLITE_ROW or an 
1e00: 65 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 6f 72  error code.** or
1e10: 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 74 68   SQLITE_OK if th
1e20: 65 20 73 74 61 74 65 6d 65 6e 74 20 66 69 6e 69  e statement fini
1e30: 73 68 65 73 20 73 75 63 63 65 73 73 66 75 6c 6c  shes successfull
1e40: 79 2e 0a 2a 2f 0a 69 6e 74 20 64 62 5f 73 74 65  y..*/.int db_ste
1e50: 70 28 53 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a  p(Stmt *pStmt){.
1e60: 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1e70: 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6c 69 6d 69  E_OK;.  int limi
1e80: 74 20 3d 20 33 3b 0a 20 20 77 68 69 6c 65 28 20  t = 3;.  while( 
1e90: 6c 69 6d 69 74 2d 2d 20 29 7b 0a 20 20 20 20 72  limit-- ){.    r
1ea0: 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
1eb0: 28 70 53 74 6d 74 2d 3e 70 53 74 6d 74 29 3b 0a  (pStmt->pStmt);.
1ec0: 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1ed0: 54 45 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  TE_ERROR ){.    
1ee0: 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72    rc = sqlite3_r
1ef0: 65 73 65 74 28 70 53 74 6d 74 2d 3e 70 53 74 6d  eset(pStmt->pStm
1f00: 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  t);.    }.    if
1f10: 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48  ( rc==SQLITE_SCH
1f20: 45 4d 41 20 29 7b 0a 20 20 20 20 20 20 72 65 70  EMA ){.      rep
1f30: 72 65 70 61 72 65 28 70 53 74 6d 74 29 3b 0a 20  repare(pStmt);. 
1f40: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1f50: 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
1f60: 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1f70: 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 6f 72 20  ./*.** Reset or 
1f80: 66 69 6e 61 6c 69 7a 65 20 61 20 73 74 61 74 65  finalize a state
1f90: 6d 65 6e 74 2e 0a 2a 2f 0a 69 6e 74 20 64 62 5f  ment..*/.int db_
1fa0: 72 65 73 65 74 28 53 74 6d 74 20 2a 70 53 74 6d  reset(Stmt *pStm
1fb0: 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73  t){.  int rc = s
1fc0: 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 53 74  qlite3_reset(pSt
1fd0: 6d 74 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 64 62  mt->pStmt);.  db
1fe0: 5f 63 68 65 63 6b 5f 72 65 73 75 6c 74 28 72 63  _check_result(rc
1ff0: 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
2000: 7d 0a 69 6e 74 20 64 62 5f 66 69 6e 61 6c 69 7a  }.int db_finaliz
2010: 65 28 53 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a  e(Stmt *pStmt){.
2020: 20 20 69 6e 74 20 72 63 3b 0a 20 20 62 6c 6f 62    int rc;.  blob
2030: 5f 72 65 73 65 74 28 26 70 53 74 6d 74 2d 3e 73  _reset(&pStmt->s
2040: 71 6c 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  ql);.  rc = sqli
2050: 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74  te3_finalize(pSt
2060: 6d 74 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 64 62  mt->pStmt);.  db
2070: 5f 63 68 65 63 6b 5f 72 65 73 75 6c 74 28 72 63  _check_result(rc
2080: 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
2090: 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
20a0: 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65  the rowid of the
20b0: 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 69 6e 73   most recent ins
20c0: 65 72 74 0a 2a 2f 0a 69 36 34 20 64 62 5f 6c 61  ert.*/.i64 db_la
20d0: 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28  st_insert_rowid(
20e0: 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20  void){.  return 
20f0: 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73  sqlite3_last_ins
2100: 65 72 74 5f 72 6f 77 69 64 28 67 2e 64 62 29 3b  ert_rowid(g.db);
2110: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
2120: 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72   the number of r
2130: 6f 77 73 20 74 68 61 74 20 77 65 72 65 20 63 68  ows that were ch
2140: 61 6e 67 65 64 20 62 79 20 74 68 65 20 6d 6f 73  anged by the mos
2150: 74 20 72 65 63 65 6e 74 0a 2a 2a 20 49 4e 53 45  t recent.** INSE
2160: 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44  RT, UPDATE, or D
2170: 45 4c 45 54 45 2e 20 20 41 75 78 69 6c 69 61 72  ELETE.  Auxiliar
2180: 79 20 63 68 61 6e 67 65 73 20 63 61 75 73 65 64  y changes caused
2190: 20 62 79 20 74 72 69 67 67 65 72 73 0a 2a 2a 20   by triggers.** 
21a0: 6f 72 20 6f 74 68 65 72 20 73 69 64 65 20 65 66  or other side ef
21b0: 66 65 63 74 73 20 61 72 65 20 6e 6f 74 20 63 6f  fects are not co
21c0: 75 6e 74 65 64 2e 0a 2a 2f 0a 69 6e 74 20 64 62  unted..*/.int db
21d0: 5f 63 68 61 6e 67 65 73 28 76 6f 69 64 29 7b 0a  _changes(void){.
21e0: 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
21f0: 5f 63 68 61 6e 67 65 73 28 67 2e 64 62 29 3b 0a  _changes(g.db);.
2200: 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74  }../*.** Extract
2210: 20 74 65 78 74 2c 20 69 6e 74 65 67 65 72 2c 20   text, integer, 
2220: 6f 72 20 62 6c 6f 62 20 76 61 6c 75 65 73 20 66  or blob values f
2230: 72 6f 6d 20 74 68 65 20 4e 2d 74 68 20 63 6f 6c  rom the N-th col
2240: 75 6d 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 63 75  umn of the.** cu
2250: 72 72 65 6e 74 20 72 6f 77 2e 0a 2a 2f 0a 69 6e  rrent row..*/.in
2260: 74 20 64 62 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  t db_column_byte
2270: 73 28 53 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  s(Stmt *pStmt, i
2280: 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20  nt N){.  return 
2290: 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
22a0: 79 74 65 73 28 70 53 74 6d 74 2d 3e 70 53 74 6d  ytes(pStmt->pStm
22b0: 74 2c 20 4e 29 3b 0a 7d 0a 69 6e 74 20 64 62 5f  t, N);.}.int db_
22c0: 63 6f 6c 75 6d 6e 5f 69 6e 74 28 53 74 6d 74 20  column_int(Stmt 
22d0: 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a  *pStmt, int N){.
22e0: 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
22f0: 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 70 53 74 6d  _column_int(pStm
2300: 74 2d 3e 70 53 74 6d 74 2c 20 4e 29 3b 0a 7d 0a  t->pStmt, N);.}.
2310: 69 36 34 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e  i64 db_column_in
2320: 74 36 34 28 53 74 6d 74 20 2a 70 53 74 6d 74 2c  t64(Stmt *pStmt,
2330: 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72   int N){.  retur
2340: 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  n sqlite3_column
2350: 5f 69 6e 74 36 34 28 70 53 74 6d 74 2d 3e 70 53  _int64(pStmt->pS
2360: 74 6d 74 2c 20 4e 29 3b 0a 7d 0a 64 6f 75 62 6c  tmt, N);.}.doubl
2370: 65 20 64 62 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62  e db_column_doub
2380: 6c 65 28 53 74 6d 74 20 2a 70 53 74 6d 74 2c 20  le(Stmt *pStmt, 
2390: 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e  int N){.  return
23a0: 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
23b0: 64 6f 75 62 6c 65 28 70 53 74 6d 74 2d 3e 70 53  double(pStmt->pS
23c0: 74 6d 74 2c 20 4e 29 3b 0a 7d 0a 63 6f 6e 73 74  tmt, N);.}.const
23d0: 20 63 68 61 72 20 2a 64 62 5f 63 6f 6c 75 6d 6e   char *db_column
23e0: 5f 74 65 78 74 28 53 74 6d 74 20 2a 70 53 74 6d  _text(Stmt *pStm
23f0: 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74  t, int N){.  ret
2400: 75 72 6e 20 28 63 68 61 72 2a 29 73 71 6c 69 74  urn (char*)sqlit
2410: 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 70  e3_column_text(p
2420: 53 74 6d 74 2d 3e 70 53 74 6d 74 2c 20 4e 29 3b  Stmt->pStmt, N);
2430: 0a 7d 0a 63 6f 6e 73 74 20 63 68 61 72 20 2a 64  .}.const char *d
2440: 62 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 53 74  b_column_name(St
2450: 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e  mt *pStmt, int N
2460: 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 63 68 61  ){.  return (cha
2470: 72 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  r*)sqlite3_colum
2480: 6e 5f 6e 61 6d 65 28 70 53 74 6d 74 2d 3e 70 53  n_name(pStmt->pS
2490: 74 6d 74 2c 20 4e 29 3b 0a 7d 0a 69 6e 74 20 64  tmt, N);.}.int d
24a0: 62 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 53  b_column_count(S
24b0: 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 72  tmt *pStmt){.  r
24c0: 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 63 6f  eturn sqlite3_co
24d0: 6c 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74 6d 74  lumn_count(pStmt
24e0: 2d 3e 70 53 74 6d 74 29 3b 0a 7d 0a 63 68 61 72  ->pStmt);.}.char
24f0: 20 2a 64 62 5f 63 6f 6c 75 6d 6e 5f 6d 61 6c 6c   *db_column_mall
2500: 6f 63 28 53 74 6d 74 20 2a 70 53 74 6d 74 2c 20  oc(Stmt *pStmt, 
2510: 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e  int N){.  return
2520: 20 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 20 64   mprintf("%s", d
2530: 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 70 53  b_column_text(pS
2540: 74 6d 74 2c 20 4e 29 29 3b 0a 7d 0a 76 6f 69 64  tmt, N));.}.void
2550: 20 64 62 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28   db_column_blob(
2560: 53 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  Stmt *pStmt, int
2570: 20 4e 2c 20 42 6c 6f 62 20 2a 70 42 6c 6f 62 29   N, Blob *pBlob)
2580: 7b 0a 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 28  {.  blob_append(
2590: 70 42 6c 6f 62 2c 20 73 71 6c 69 74 65 33 5f 63  pBlob, sqlite3_c
25a0: 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 53 74 6d 74  olumn_blob(pStmt
25b0: 2d 3e 70 53 74 6d 74 2c 20 4e 29 2c 0a 20 20 20  ->pStmt, N),.   
25c0: 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
25d0: 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
25e0: 70 53 74 6d 74 2d 3e 70 53 74 6d 74 2c 20 4e 29  pStmt->pStmt, N)
25f0: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74  );.}../*.** Init
2600: 69 61 6c 69 7a 65 20 61 20 62 6c 6f 62 20 74 6f  ialize a blob to
2610: 20 61 6e 20 65 70 68 65 72 6d 65 72 61 6c 20 63   an ephermeral c
2620: 6f 70 79 20 6f 66 20 74 68 65 20 63 6f 6e 74 65  opy of the conte
2630: 6e 74 20 6f 66 20 61 0a 2a 2a 20 63 6f 6c 75 6d  nt of a.** colum
2640: 6e 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74  n in the current
2650: 20 72 6f 77 2e 20 20 54 68 65 20 64 61 74 61 20   row.  The data 
2660: 69 6e 20 74 68 65 20 62 6c 6f 62 20 77 69 6c 6c  in the blob will
2670: 20 62 65 63 6f 6d 65 0a 2a 2a 20 69 6e 76 61 6c   become.** inval
2680: 69 64 20 77 68 65 6e 20 74 68 65 20 73 74 61 74  id when the stat
2690: 65 6d 65 6e 74 20 69 73 20 73 74 65 70 70 65 64  ement is stepped
26a0: 20 6f 72 20 72 65 73 65 74 2e 0a 2a 2f 0a 76 6f   or reset..*/.vo
26b0: 69 64 20 64 62 5f 65 70 68 65 6d 65 72 61 6c 5f  id db_ephemeral_
26c0: 62 6c 6f 62 28 53 74 6d 74 20 2a 70 53 74 6d 74  blob(Stmt *pStmt
26d0: 2c 20 69 6e 74 20 4e 2c 20 42 6c 6f 62 20 2a 70  , int N, Blob *p
26e0: 42 6c 6f 62 29 7b 0a 20 20 62 6c 6f 62 5f 69 6e  Blob){.  blob_in
26f0: 69 74 28 70 42 6c 6f 62 2c 20 73 71 6c 69 74 65  it(pBlob, sqlite
2700: 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 53  3_column_blob(pS
2710: 74 6d 74 2d 3e 70 53 74 6d 74 2c 20 4e 29 2c 0a  tmt->pStmt, N),.
2720: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
2730: 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
2740: 65 73 28 70 53 74 6d 74 2d 3e 70 53 74 6d 74 2c  es(pStmt->pStmt,
2750: 20 4e 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43   N));.}../*.** C
2760: 68 65 63 6b 20 61 20 72 65 73 75 6c 74 20 63 6f  heck a result co
2770: 64 65 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f  de.  If it is no
2780: 74 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 70 72 69  t SQLITE_OK, pri
2790: 6e 74 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73  nt the.** corres
27a0: 70 6f 6e 64 69 6e 67 20 65 72 72 6f 72 20 6d 65  ponding error me
27b0: 73 73 61 67 65 20 61 6e 64 20 65 78 69 74 2e 0a  ssage and exit..
27c0: 2a 2f 0a 76 6f 69 64 20 64 62 5f 63 68 65 63 6b  */.void db_check
27d0: 5f 72 65 73 75 6c 74 28 69 6e 74 20 72 63 29 7b  _result(int rc){
27e0: 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
27f0: 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 64 62 5f 65  E_OK ){.    db_e
2800: 72 72 28 22 53 51 4c 20 65 72 72 6f 72 3a 20 25  rr("SQL error: %
2810: 73 22 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  s", sqlite3_errm
2820: 73 67 28 67 2e 64 62 29 29 3b 0a 20 20 7d 0a 7d  sg(g.db));.  }.}
2830: 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20  ../*.** Execute 
2840: 61 20 73 69 6e 67 6c 65 20 70 72 65 70 61 72 65  a single prepare
2850: 64 20 73 74 61 74 65 6d 65 6e 74 20 75 6e 74 69  d statement unti
2860: 6c 20 69 74 20 66 69 6e 69 73 68 65 73 2e 0a 2a  l it finishes..*
2870: 2f 0a 69 6e 74 20 64 62 5f 65 78 65 63 28 53 74  /.int db_exec(St
2880: 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e  mt *pStmt){.  in
2890: 74 20 72 63 3b 0a 20 20 77 68 69 6c 65 28 20 28  t rc;.  while( (
28a0: 72 63 20 3d 20 64 62 5f 73 74 65 70 28 70 53 74  rc = db_step(pSt
28b0: 6d 74 29 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  mt))==SQLITE_ROW
28c0: 20 29 7b 7d 0a 20 20 72 63 20 3d 20 64 62 5f 72   ){}.  rc = db_r
28d0: 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 20 20 64  eset(pStmt);.  d
28e0: 62 5f 63 68 65 63 6b 5f 72 65 73 75 6c 74 28 72  b_check_result(r
28f0: 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  c);.  return rc;
2900: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74  .}../*.** Execut
2910: 65 20 6d 75 6c 74 69 70 6c 65 20 53 51 4c 20 73  e multiple SQL s
2920: 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 69 6e  tatements..*/.in
2930: 74 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28  t db_multi_exec(
2940: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
2950: 2c 20 2e 2e 2e 29 7b 0a 20 20 42 6c 6f 62 20 73  , ...){.  Blob s
2960: 71 6c 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  ql;.  int rc;.  
2970: 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68  va_list ap;.  ch
2980: 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 20 20  ar *zErr = 0;.  
2990: 62 6c 6f 62 5f 69 6e 69 74 28 26 73 71 6c 2c 20  blob_init(&sql, 
29a0: 30 2c 20 30 29 3b 0a 20 20 76 61 5f 73 74 61 72  0, 0);.  va_star
29b0: 74 28 61 70 2c 20 7a 53 71 6c 29 3b 0a 20 20 62  t(ap, zSql);.  b
29c0: 6c 6f 62 5f 76 61 70 70 65 6e 64 66 28 26 73 71  lob_vappendf(&sq
29d0: 6c 2c 20 7a 53 71 6c 2c 20 61 70 29 3b 0a 20 20  l, zSql, ap);.  
29e0: 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 63  va_end(ap);.  rc
29f0: 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28   = sqlite3_exec(
2a00: 67 2e 64 62 2c 20 62 6c 6f 62 5f 62 75 66 66 65  g.db, blob_buffe
2a10: 72 28 26 73 71 6c 29 2c 20 30 2c 20 30 2c 20 26  r(&sql), 0, 0, &
2a20: 7a 45 72 72 29 3b 0a 20 20 69 66 28 20 72 63 21  zErr);.  if( rc!
2a30: 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
2a40: 20 20 64 62 5f 65 72 72 28 22 25 73 5c 6e 25 73    db_err("%s\n%s
2a50: 22 2c 20 7a 45 72 72 2c 20 62 6c 6f 62 5f 62 75  ", zErr, blob_bu
2a60: 66 66 65 72 28 26 73 71 6c 29 29 3b 0a 20 20 7d  ffer(&sql));.  }
2a70: 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73  .  blob_reset(&s
2a80: 71 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ql);.  return rc
2a90: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75  ;.}../*.** Execu
2aa0: 74 65 20 61 20 71 75 65 72 79 20 61 6e 64 20 72  te a query and r
2ab0: 65 74 75 72 6e 20 61 20 73 69 6e 67 6c 65 20 69  eturn a single i
2ac0: 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 0a 2a 2f  nteger value..*/
2ad0: 0a 69 36 34 20 64 62 5f 69 6e 74 36 34 28 69 36  .i64 db_int64(i6
2ae0: 34 20 69 44 66 6c 74 2c 20 63 6f 6e 73 74 20 63  4 iDflt, const c
2af0: 68 61 72 20 2a 7a 53 71 6c 2c 20 2e 2e 2e 29 7b  har *zSql, ...){
2b00: 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20  .  va_list ap;. 
2b10: 20 53 74 6d 74 20 73 3b 0a 20 20 69 36 34 20 72   Stmt s;.  i64 r
2b20: 63 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70  c;.  va_start(ap
2b30: 2c 20 7a 53 71 6c 29 3b 0a 20 20 64 62 5f 76 70  , zSql);.  db_vp
2b40: 72 65 70 61 72 65 28 26 73 2c 20 7a 53 71 6c 2c  repare(&s, zSql,
2b50: 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61   ap);.  va_end(a
2b60: 70 29 3b 0a 20 20 69 66 28 20 64 62 5f 73 74 65  p);.  if( db_ste
2b70: 70 28 26 73 29 21 3d 53 51 4c 49 54 45 5f 52 4f  p(&s)!=SQLITE_RO
2b80: 57 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 69 44  W ){.    rc = iD
2b90: 66 6c 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  flt;.  }else{.  
2ba0: 20 20 72 63 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e    rc = db_column
2bb0: 5f 69 6e 74 36 34 28 26 73 2c 20 30 29 3b 0a 20  _int64(&s, 0);. 
2bc0: 20 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65   }.  db_finalize
2bd0: 28 26 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  (&s);.  return r
2be0: 63 3b 0a 7d 0a 69 6e 74 20 64 62 5f 69 6e 74 28  c;.}.int db_int(
2bf0: 69 6e 74 20 69 44 66 6c 74 2c 20 63 6f 6e 73 74  int iDflt, const
2c00: 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 2e 2e 2e   char *zSql, ...
2c10: 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b  ){.  va_list ap;
2c20: 0a 20 20 53 74 6d 74 20 73 3b 0a 20 20 69 6e 74  .  Stmt s;.  int
2c30: 20 72 63 3b 0a 20 20 76 61 5f 73 74 61 72 74 28   rc;.  va_start(
2c40: 61 70 2c 20 7a 53 71 6c 29 3b 0a 20 20 64 62 5f  ap, zSql);.  db_
2c50: 76 70 72 65 70 61 72 65 28 26 73 2c 20 7a 53 71  vprepare(&s, zSq
2c60: 6c 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64  l, ap);.  va_end
2c70: 28 61 70 29 3b 0a 20 20 69 66 28 20 64 62 5f 73  (ap);.  if( db_s
2c80: 74 65 70 28 26 73 29 21 3d 53 51 4c 49 54 45 5f  tep(&s)!=SQLITE_
2c90: 52 4f 57 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ROW ){.    rc = 
2ca0: 69 44 66 6c 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a  iDflt;.  }else{.
2cb0: 20 20 20 20 72 63 20 3d 20 64 62 5f 63 6f 6c 75      rc = db_colu
2cc0: 6d 6e 5f 69 6e 74 28 26 73 2c 20 30 29 3b 0a 20  mn_int(&s, 0);. 
2cd0: 20 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65   }.  db_finalize
2ce0: 28 26 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  (&s);.  return r
2cf0: 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  c;.}../*.** Retu
2d00: 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 71  rn TRUE if the q
2d10: 75 65 72 79 20 77 6f 75 6c 64 20 72 65 74 75 72  uery would retur
2d20: 6e 20 31 20 6f 72 20 6d 6f 72 65 20 72 6f 77 73  n 1 or more rows
2d30: 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 46 41 4c  .  Return.** FAL
2d40: 53 45 20 69 66 20 74 68 65 20 71 75 65 72 79 20  SE if the query 
2d50: 72 65 73 75 6c 74 20 77 6f 75 6c 64 20 62 65 20  result would be 
2d60: 61 6e 20 65 6d 70 74 79 20 73 65 74 2e 0a 2a 2f  an empty set..*/
2d70: 0a 69 6e 74 20 64 62 5f 65 78 69 73 74 73 28 63  .int db_exists(c
2d80: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c  onst char *zSql,
2d90: 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74   ...){.  va_list
2da0: 20 61 70 3b 0a 20 20 53 74 6d 74 20 73 3b 0a 20   ap;.  Stmt s;. 
2db0: 20 69 6e 74 20 72 63 3b 0a 20 20 76 61 5f 73 74   int rc;.  va_st
2dc0: 61 72 74 28 61 70 2c 20 7a 53 71 6c 29 3b 0a 20  art(ap, zSql);. 
2dd0: 20 64 62 5f 76 70 72 65 70 61 72 65 28 26 73 2c   db_vprepare(&s,
2de0: 20 7a 53 71 6c 2c 20 61 70 29 3b 0a 20 20 76 61   zSql, ap);.  va
2df0: 5f 65 6e 64 28 61 70 29 3b 0a 20 20 69 66 28 20  _end(ap);.  if( 
2e00: 64 62 5f 73 74 65 70 28 26 73 29 21 3d 53 51 4c  db_step(&s)!=SQL
2e10: 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 72  ITE_ROW ){.    r
2e20: 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  c = 0;.  }else{.
2e30: 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 7d 0a      rc = 1;.  }.
2e40: 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73    db_finalize(&s
2e50: 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
2e60: 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74  }.../*.** Execut
2e70: 65 20 61 20 71 75 65 72 79 20 61 6e 64 20 72 65  e a query and re
2e80: 74 75 72 6e 20 61 20 66 6c 6f 61 74 69 6e 67 2d  turn a floating-
2e90: 70 6f 69 6e 74 20 76 61 6c 75 65 2e 0a 2a 2f 0a  point value..*/.
2ea0: 64 6f 75 62 6c 65 20 64 62 5f 64 6f 75 62 6c 65  double db_double
2eb0: 28 64 6f 75 62 6c 65 20 72 44 66 6c 74 2c 20 63  (double rDflt, c
2ec0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c  onst char *zSql,
2ed0: 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74   ...){.  va_list
2ee0: 20 61 70 3b 0a 20 20 53 74 6d 74 20 73 3b 0a 20   ap;.  Stmt s;. 
2ef0: 20 64 6f 75 62 6c 65 20 72 3b 0a 20 20 76 61 5f   double r;.  va_
2f00: 73 74 61 72 74 28 61 70 2c 20 7a 53 71 6c 29 3b  start(ap, zSql);
2f10: 0a 20 20 64 62 5f 76 70 72 65 70 61 72 65 28 26  .  db_vprepare(&
2f20: 73 2c 20 7a 53 71 6c 2c 20 61 70 29 3b 0a 20 20  s, zSql, ap);.  
2f30: 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 69 66  va_end(ap);.  if
2f40: 28 20 64 62 5f 73 74 65 70 28 26 73 29 21 3d 53  ( db_step(&s)!=S
2f50: 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
2f60: 20 72 20 3d 20 72 44 66 6c 74 3b 0a 20 20 7d 65   r = rDflt;.  }e
2f70: 6c 73 65 7b 0a 20 20 20 20 72 20 3d 20 64 62 5f  lse{.    r = db_
2f80: 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 26 73  column_double(&s
2f90: 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 66  , 0);.  }.  db_f
2fa0: 69 6e 61 6c 69 7a 65 28 26 73 29 3b 0a 20 20 72  inalize(&s);.  r
2fb0: 65 74 75 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn r;.}../*.*
2fc0: 2a 20 45 78 65 63 75 74 65 20 61 20 71 75 65 72  * Execute a quer
2fd0: 79 20 61 6e 64 20 61 70 70 65 6e 64 20 74 68 65  y and append the
2fe0: 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20 6f 66   first column of
2ff0: 20 74 68 65 20 66 69 72 73 74 20 72 6f 77 0a 2a   the first row.*
3000: 2a 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20  * of the result 
3010: 73 65 74 20 74 6f 20 62 6c 6f 62 20 67 69 76 65  set to blob give
3020: 6e 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61  n in the first a
3030: 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 76 6f 69 64  rgument..*/.void
3040: 20 64 62 5f 62 6c 6f 62 28 42 6c 6f 62 20 2a 70   db_blob(Blob *p
3050: 52 65 73 75 6c 74 2c 20 63 6f 6e 73 74 20 63 68  Result, const ch
3060: 61 72 20 2a 7a 53 71 6c 2c 20 2e 2e 2e 29 7b 0a  ar *zSql, ...){.
3070: 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20    va_list ap;.  
3080: 53 74 6d 74 20 73 3b 0a 20 20 76 61 5f 73 74 61  Stmt s;.  va_sta
3090: 72 74 28 61 70 2c 20 7a 53 71 6c 29 3b 0a 20 20  rt(ap, zSql);.  
30a0: 64 62 5f 76 70 72 65 70 61 72 65 28 26 73 2c 20  db_vprepare(&s, 
30b0: 7a 53 71 6c 2c 20 61 70 29 3b 0a 20 20 76 61 5f  zSql, ap);.  va_
30c0: 65 6e 64 28 61 70 29 3b 0a 20 20 69 66 28 20 64  end(ap);.  if( d
30d0: 62 5f 73 74 65 70 28 26 73 29 3d 3d 53 51 4c 49  b_step(&s)==SQLI
30e0: 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 62 6c  TE_ROW ){.    bl
30f0: 6f 62 5f 61 70 70 65 6e 64 28 70 52 65 73 75 6c  ob_append(pResul
3100: 74 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  t, sqlite3_colum
3110: 6e 5f 62 6c 6f 62 28 73 2e 70 53 74 6d 74 2c 20  n_blob(s.pStmt, 
3120: 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  0),.            
3130: 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
3140: 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
3150: 73 28 73 2e 70 53 74 6d 74 2c 20 30 29 29 3b 0a  s(s.pStmt, 0));.
3160: 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a    }.  db_finaliz
3170: 65 28 26 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  e(&s);.}../*.** 
3180: 45 78 65 63 75 74 65 20 61 20 71 75 65 72 79 2e  Execute a query.
3190: 20 20 52 65 74 75 72 6e 20 74 68 65 20 66 69 72    Return the fir
31a0: 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  st column of the
31b0: 20 66 69 72 73 74 20 72 6f 77 0a 2a 2a 20 6f 66   first row.** of
31c0: 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
31d0: 61 73 20 61 20 73 74 72 69 6e 67 2e 20 20 53 70  as a string.  Sp
31e0: 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ace to hold the 
31f0: 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 6f 62 74  string is.** obt
3200: 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f  ained from mallo
3210: 63 28 29 2e 20 20 49 66 20 74 68 65 20 72 65 73  c().  If the res
3220: 75 6c 74 20 73 65 74 20 69 73 20 65 6d 70 74 79  ult set is empty
3230: 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 7a 44 65 66  , return.** zDef
3240: 61 75 6c 74 20 69 6e 73 74 65 61 64 2e 0a 2a 2f  ault instead..*/
3250: 0a 63 68 61 72 20 2a 64 62 5f 74 65 78 74 28 63  .char *db_text(c
3260: 68 61 72 20 2a 7a 44 65 66 61 75 6c 74 2c 20 63  har *zDefault, c
3270: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c  onst char *zSql,
3280: 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74   ...){.  va_list
3290: 20 61 70 3b 0a 20 20 53 74 6d 74 20 73 3b 0a 20   ap;.  Stmt s;. 
32a0: 20 63 68 61 72 20 2a 7a 20 3d 20 7a 44 65 66 61   char *z = zDefa
32b0: 75 6c 74 3b 0a 20 20 76 61 5f 73 74 61 72 74 28  ult;.  va_start(
32c0: 61 70 2c 20 7a 53 71 6c 29 3b 0a 20 20 64 62 5f  ap, zSql);.  db_
32d0: 76 70 72 65 70 61 72 65 28 26 73 2c 20 7a 53 71  vprepare(&s, zSq
32e0: 6c 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64  l, ap);.  va_end
32f0: 28 61 70 29 3b 0a 20 20 69 66 28 20 64 62 5f 73  (ap);.  if( db_s
3300: 74 65 70 28 26 73 29 3d 3d 53 51 4c 49 54 45 5f  tep(&s)==SQLITE_
3310: 52 4f 57 20 29 7b 0a 20 20 20 20 7a 20 3d 20 6d  ROW ){.    z = m
3320: 70 72 69 6e 74 66 28 22 25 73 22 2c 20 73 71 6c  printf("%s", sql
3330: 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
3340: 28 73 2e 70 53 74 6d 74 2c 20 30 29 29 3b 0a 20  (s.pStmt, 0));. 
3350: 20 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65   }.  db_finalize
3360: 28 26 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a  (&s);.  return z
3370: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69  ;.}../*.** Initi
3380: 61 6c 69 7a 65 20 61 20 6e 65 77 20 64 61 74 61  alize a new data
3390: 62 61 73 65 20 66 69 6c 65 20 77 69 74 68 20 74  base file with t
33a0: 68 65 20 67 69 76 65 6e 20 73 63 68 65 6d 61 2e  he given schema.
33b0: 20 20 49 66 20 61 6e 79 74 68 69 6e 67 0a 2a 2a    If anything.**
33c0: 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 63 61 6c   goes wrong, cal
33d0: 6c 20 64 62 5f 65 72 72 28 29 20 74 6f 20 65 78  l db_err() to ex
33e0: 69 74 2e 0a 2a 2f 0a 76 6f 69 64 20 64 62 5f 69  it..*/.void db_i
33f0: 6e 69 74 5f 64 61 74 61 62 61 73 65 28 0a 20 20  nit_database(.  
3400: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
3410: 65 4e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65  eName,   /* Name
3420: 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 6c   of database fil
3430: 65 20 74 6f 20 63 72 65 61 74 65 20 2a 2f 0a 20  e to create */. 
3440: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 63   const char *zSc
3450: 68 65 6d 61 2c 20 20 20 20 20 2f 2a 20 46 69 72  hema,     /* Fir
3460: 73 74 20 70 61 72 74 20 6f 66 20 73 63 68 65 6d  st part of schem
3470: 61 20 2a 2f 0a 20 20 2e 2e 2e 20 20 20 20 20 20  a */.  ...      
3480: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3490: 2f 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 53 51  /* Additional SQ
34a0: 4c 20 74 6f 20 72 75 6e 2e 20 20 54 65 72 6d 69  L to run.  Termi
34b0: 6e 61 74 65 20 77 69 74 68 20 4e 55 4c 4c 2e 20  nate with NULL. 
34c0: 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  */.){.  sqlite3 
34d0: 2a 64 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  *db;.  int rc;. 
34e0: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71   const char *zSq
34f0: 6c 3b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b  l;.  va_list ap;
3500: 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
3510: 5f 6f 70 65 6e 28 7a 46 69 6c 65 4e 61 6d 65 2c  _open(zFileName,
3520: 20 26 64 62 29 3b 0a 20 20 69 66 28 20 72 63 21   &db);.  if( rc!
3530: 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
3540: 20 20 64 62 5f 65 72 72 28 73 71 6c 69 74 65 33    db_err(sqlite3
3550: 5f 65 72 72 6d 73 67 28 64 62 29 29 3b 0a 20 20  _errmsg(db));.  
3560: 7d 0a 20 20 73 71 6c 69 74 65 33 5f 65 78 65 63  }.  sqlite3_exec
3570: 28 64 62 2c 20 22 42 45 47 49 4e 20 45 58 43 4c  (db, "BEGIN EXCL
3580: 55 53 49 56 45 22 2c 20 30 2c 20 30 2c 20 30 29  USIVE", 0, 0, 0)
3590: 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
35a0: 5f 65 78 65 63 28 64 62 2c 20 7a 53 63 68 65 6d  _exec(db, zSchem
35b0: 61 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 69  a, 0, 0, 0);.  i
35c0: 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
35d0: 20 29 7b 0a 20 20 20 20 64 62 5f 65 72 72 28 73   ){.    db_err(s
35e0: 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64 62  qlite3_errmsg(db
35f0: 29 29 3b 0a 20 20 7d 0a 20 20 76 61 5f 73 74 61  ));.  }.  va_sta
3600: 72 74 28 61 70 2c 20 7a 53 63 68 65 6d 61 29 3b  rt(ap, zSchema);
3610: 0a 20 20 77 68 69 6c 65 28 20 28 7a 53 71 6c 20  .  while( (zSql 
3620: 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 63 6f 6e  = va_arg(ap, con
3630: 73 74 20 63 68 61 72 2a 29 29 21 3d 30 20 29 7b  st char*))!=0 ){
3640: 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
3650: 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 71 6c 2c  3_exec(db, zSql,
3660: 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69   0, 0, 0);.    i
3670: 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3680: 20 29 7b 0a 20 20 20 20 20 20 64 62 5f 65 72 72   ){.      db_err
3690: 28 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28  (sqlite3_errmsg(
36a0: 64 62 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  db));.    }.  }.
36b0: 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20    va_end(ap);.  
36c0: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c  sqlite3_exec(db,
36d0: 20 22 43 4f 4d 4d 49 54 22 2c 20 30 2c 20 30 2c   "COMMIT", 0, 0,
36e0: 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63   0);.  sqlite3_c
36f0: 6c 6f 73 65 28 64 62 29 3b 0a 7d 0a 0a 2f 2a 0a  lose(db);.}../*.
3700: 2a 2a 20 7a 44 62 4e 61 6d 65 20 69 73 20 74 68  ** zDbName is th
3710: 65 20 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 61  e name of a data
3720: 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 6e  base file.  If n
3730: 6f 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65  o other database
3740: 0a 2a 2a 20 66 69 6c 65 20 69 73 20 6f 70 65 6e  .** file is open
3750: 2c 20 74 68 65 6e 20 6f 70 65 6e 20 74 68 69 73  , then open this
3760: 20 6f 6e 65 2e 20 20 49 66 20 61 6e 6f 74 68 65   one.  If anothe
3770: 72 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  r database file 
3780: 69 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 6f 70  is.** already op
3790: 65 6e 2c 20 74 68 65 6e 20 61 74 74 61 63 68 20  en, then attach 
37a0: 7a 44 62 4e 61 6d 65 20 75 73 69 6e 67 20 74 68  zDbName using th
37b0: 65 20 6e 61 6d 65 20 7a 4c 61 62 65 6c 2e 0a 2a  e name zLabel..*
37c0: 2f 0a 76 6f 69 64 20 64 62 5f 6f 70 65 6e 5f 6f  /.void db_open_o
37d0: 72 5f 61 74 74 61 63 68 28 63 6f 6e 73 74 20 63  r_attach(const c
37e0: 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 63 6f  har *zDbName, co
37f0: 6e 73 74 20 63 68 61 72 20 2a 7a 4c 61 62 65 6c  nst char *zLabel
3800: 29 7b 0a 20 20 69 66 28 20 21 67 2e 64 62 20 29  ){.  if( !g.db )
3810: 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73  {.    int rc = s
3820: 71 6c 69 74 65 33 5f 6f 70 65 6e 28 7a 44 62 4e  qlite3_open(zDbN
3830: 61 6d 65 2c 20 26 67 2e 64 62 29 3b 0a 20 20 20  ame, &g.db);.   
3840: 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
3850: 4f 4b 20 29 7b 0a 20 20 20 20 20 20 64 62 5f 65  OK ){.      db_e
3860: 72 72 28 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  rr(sqlite3_errms
3870: 67 28 67 2e 64 62 29 29 3b 0a 20 20 20 20 7d 0a  g(g.db));.    }.
3880: 20 20 20 20 64 62 5f 63 6f 6e 6e 65 63 74 69 6f      db_connectio
3890: 6e 5f 69 6e 69 74 28 29 3b 0a 20 20 7d 65 6c 73  n_init();.  }els
38a0: 65 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f  e{.    db_multi_
38b0: 65 78 65 63 28 22 41 54 54 41 43 48 20 44 41 54  exec("ATTACH DAT
38c0: 41 42 41 53 45 20 25 51 20 41 53 20 25 73 22 2c  ABASE %Q AS %s",
38d0: 20 7a 44 62 4e 61 6d 65 2c 20 7a 4c 61 62 65 6c   zDbName, zLabel
38e0: 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
38f0: 4f 70 65 6e 20 74 68 65 20 75 73 65 72 20 64 61  Open the user da
3900: 74 61 62 61 73 65 20 69 6e 20 22 7e 2f 2e 66 6f  tabase in "~/.fo
3910: 73 73 69 6c 22 2e 20 20 43 72 65 61 74 65 20 74  ssil".  Create t
3920: 68 65 20 64 61 74 61 62 61 73 65 20 61 6e 65 77  he database anew
3930: 20 69 66 0a 2a 2a 20 69 74 20 64 6f 65 73 20 6e   if.** it does n
3940: 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  ot already exist
3950: 2e 0a 2a 2f 0a 76 6f 69 64 20 64 62 5f 6f 70 65  ..*/.void db_ope
3960: 6e 5f 63 6f 6e 66 69 67 28 76 6f 69 64 29 7b 0a  n_config(void){.
3970: 20 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 3b    char *zDbName;
3980: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
3990: 48 6f 6d 65 3b 0a 23 69 66 64 65 66 20 5f 5f 4d  Home;.#ifdef __M
39a0: 49 4e 47 57 33 32 5f 5f 0a 20 20 7a 48 6f 6d 65  INGW32__.  zHome
39b0: 20 3d 20 67 65 74 65 6e 76 28 22 4c 4f 43 41 4c   = getenv("LOCAL
39c0: 41 50 50 44 41 54 41 22 29 3b 0a 20 20 69 66 28  APPDATA");.  if(
39d0: 20 7a 48 6f 6d 65 3d 3d 30 20 29 7b 0a 20 20 20   zHome==0 ){.   
39e0: 20 7a 48 6f 6d 65 20 3d 20 67 65 74 65 6e 76 28   zHome = getenv(
39f0: 22 41 50 50 44 41 54 41 22 29 3b 0a 20 20 20 20  "APPDATA");.    
3a00: 69 66 28 20 7a 48 6f 6d 65 3d 3d 30 20 29 7b 0a  if( zHome==0 ){.
3a10: 20 20 20 20 20 20 7a 48 6f 6d 65 20 3d 20 67 65        zHome = ge
3a20: 74 65 6e 76 28 22 48 4f 4d 45 50 41 54 48 22 29  tenv("HOMEPATH")
3a30: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73  ;.    }.  }.#els
3a40: 65 0a 20 20 7a 48 6f 6d 65 20 3d 20 67 65 74 65  e.  zHome = gete
3a50: 6e 76 28 22 48 4f 4d 45 22 29 3b 0a 23 65 6e 64  nv("HOME");.#end
3a60: 69 66 0a 20 20 69 66 28 20 7a 48 6f 6d 65 3d 3d  if.  if( zHome==
3a70: 30 20 29 7b 0a 20 20 20 20 64 62 5f 65 72 72 28  0 ){.    db_err(
3a80: 22 63 61 6e 6e 6f 74 20 6c 6f 63 61 6c 20 68 6f  "cannot local ho
3a90: 6d 65 20 64 69 72 65 63 74 6f 72 79 22 29 3b 0a  me directory");.
3aa0: 20 20 7d 0a 23 69 66 64 65 66 20 5f 5f 4d 49 4e    }.#ifdef __MIN
3ab0: 47 57 33 32 5f 5f 0a 20 20 2f 2a 20 2e 20 66 69  GW32__.  /* . fi
3ac0: 6c 65 6e 61 6d 65 73 20 67 69 76 65 20 73 6f 6d  lenames give som
3ad0: 65 20 77 69 6e 64 6f 77 20 73 79 73 74 65 6d 73  e window systems
3ae0: 20 70 72 6f 62 6c 65 6d 73 20 61 6e 64 20 6d 61   problems and ma
3af0: 6e 79 20 61 70 70 73 20 70 72 6f 62 6c 65 6d 73  ny apps problems
3b00: 20 2a 2f 0a 20 20 7a 44 62 4e 61 6d 65 20 3d 20   */.  zDbName = 
3b10: 6d 70 72 69 6e 74 66 28 22 25 73 2f 5f 66 6f 73  mprintf("%s/_fos
3b20: 73 69 6c 22 2c 20 7a 48 6f 6d 65 29 3b 0a 23 65  sil", zHome);.#e
3b30: 6c 73 65 0a 20 20 7a 44 62 4e 61 6d 65 20 3d 20  lse.  zDbName = 
3b40: 6d 70 72 69 6e 74 66 28 22 25 73 2f 2e 66 6f 73  mprintf("%s/.fos
3b50: 73 69 6c 22 2c 20 7a 48 6f 6d 65 29 3b 0a 23 65  sil", zHome);.#e
3b60: 6e 64 69 66 0a 20 20 69 66 28 20 67 2e 63 6f 6e  ndif.  if( g.con
3b70: 66 69 67 4f 70 65 6e 20 29 20 72 65 74 75 72 6e  figOpen ) return
3b80: 3b 0a 20 20 69 66 28 20 66 69 6c 65 5f 73 69 7a  ;.  if( file_siz
3b90: 65 28 7a 44 62 4e 61 6d 65 29 3c 31 30 32 34 2a  e(zDbName)<1024*
3ba0: 33 20 29 7b 0a 20 20 20 20 64 62 5f 69 6e 69 74  3 ){.    db_init
3bb0: 5f 64 61 74 61 62 61 73 65 28 7a 44 62 4e 61 6d  _database(zDbNam
3bc0: 65 2c 20 7a 43 6f 6e 66 69 67 53 63 68 65 6d 61  e, zConfigSchema
3bd0: 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20 7d  , (char*)0);.  }
3be0: 0a 20 20 64 62 5f 6f 70 65 6e 5f 6f 72 5f 61 74  .  db_open_or_at
3bf0: 74 61 63 68 28 7a 44 62 4e 61 6d 65 2c 20 22 63  tach(zDbName, "c
3c00: 6f 6e 66 69 67 64 62 22 29 3b 0a 20 20 67 2e 63  onfigdb");.  g.c
3c10: 6f 6e 66 69 67 4f 70 65 6e 20 3d 20 31 3b 0a 7d  onfigOpen = 1;.}
3c20: 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 7a 44 62 4e 61  ../*.** If zDbNa
3c30: 6d 65 20 69 73 20 61 20 76 61 6c 69 64 20 6c 6f  me is a valid lo
3c40: 63 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c  cal database fil
3c50: 65 2c 20 6f 70 65 6e 20 69 74 20 61 6e 64 20 72  e, open it and r
3c60: 65 74 75 72 6e 0a 2a 2a 20 74 72 75 65 2e 20 20  eturn.** true.  
3c70: 49 66 20 69 74 20 69 73 20 6e 6f 74 20 61 20 76  If it is not a v
3c80: 61 6c 69 64 20 6c 6f 63 61 6c 20 64 61 74 61 62  alid local datab
3c90: 61 73 65 20 66 69 6c 65 2c 20 72 65 74 75 72 6e  ase file, return
3ca0: 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e   0..*/.static in
3cb0: 74 20 69 73 56 61 6c 69 64 4c 6f 63 61 6c 44 62  t isValidLocalDb
3cc0: 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62  (const char *zDb
3cd0: 4e 61 6d 65 29 7b 0a 20 20 69 36 34 20 6c 73 69  Name){.  i64 lsi
3ce0: 7a 65 3b 0a 20 20 69 66 28 20 61 63 63 65 73 73  ze;.  if( access
3cf0: 28 7a 44 62 4e 61 6d 65 2c 20 46 5f 4f 4b 29 20  (zDbName, F_OK) 
3d00: 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6c 73  ) return 0;.  ls
3d10: 69 7a 65 20 3d 20 66 69 6c 65 5f 73 69 7a 65 28  ize = file_size(
3d20: 7a 44 62 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20  zDbName);.  if( 
3d30: 6c 73 69 7a 65 25 31 30 32 34 21 3d 30 20 7c 7c  lsize%1024!=0 ||
3d40: 20 6c 73 69 7a 65 3c 34 30 39 36 20 29 20 72 65   lsize<4096 ) re
3d50: 74 75 72 6e 20 30 3b 0a 20 20 64 62 5f 6f 70 65  turn 0;.  db_ope
3d60: 6e 5f 6f 72 5f 61 74 74 61 63 68 28 7a 44 62 4e  n_or_attach(zDbN
3d70: 61 6d 65 2c 20 22 6c 6f 63 61 6c 64 62 22 29 3b  ame, "localdb");
3d80: 0a 20 20 67 2e 6c 6f 63 61 6c 4f 70 65 6e 20 3d  .  g.localOpen =
3d90: 20 31 3b 0a 20 20 64 62 5f 6f 70 65 6e 5f 63 6f   1;.  db_open_co
3da0: 6e 66 69 67 28 29 3b 0a 20 20 64 62 5f 6f 70 65  nfig();.  db_ope
3db0: 6e 5f 72 65 70 6f 73 69 74 6f 72 79 28 30 29 3b  n_repository(0);
3dc0: 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
3dd0: 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65  /*.** Locate the
3de0: 20 72 6f 6f 74 20 64 69 72 65 63 74 6f 72 79 20   root directory 
3df0: 6f 66 20 74 68 65 20 6c 6f 63 61 6c 20 72 65 70  of the local rep
3e00: 6f 73 69 74 6f 72 79 20 74 72 65 65 2e 20 20 54  ository tree.  T
3e10: 68 65 20 72 6f 6f 74 0a 2a 2a 20 64 69 72 65 63  he root.** direc
3e20: 74 6f 72 79 20 69 73 20 66 6f 75 6e 64 20 62 79  tory is found by
3e30: 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 61   searching for a
3e40: 20 66 69 6c 65 20 6e 61 6d 65 64 20 22 46 4f 53   file named "FOS
3e50: 53 49 4c 22 20 74 68 61 74 20 63 6f 6e 74 61 69  SIL" that contai
3e60: 6e 73 0a 2a 2a 20 61 20 76 61 6c 69 64 20 72 65  ns.** a valid re
3e70: 70 6f 73 69 74 6f 72 79 20 64 61 74 61 62 61 73  pository databas
3e80: 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 76  e..**.** If no v
3e90: 61 6c 69 64 20 46 4f 53 53 49 4c 20 66 69 6c 65  alid FOSSIL file
3ea0: 20 69 73 20 66 6f 75 6e 64 2c 20 77 65 20 6d 6f   is found, we mo
3eb0: 76 65 20 75 70 20 6f 6e 65 20 6c 65 76 65 6c 20  ve up one level 
3ec0: 61 6e 64 20 74 72 79 20 61 67 61 69 6e 2e 0a 2a  and try again..*
3ed0: 2a 20 4f 6e 63 65 20 74 68 65 20 66 69 6c 65 20  * Once the file 
3ee0: 69 73 20 66 6f 75 6e 64 2c 20 74 68 65 20 67 2e  is found, the g.
3ef0: 7a 4c 6f 63 61 6c 52 6f 6f 74 20 76 61 72 69 61  zLocalRoot varia
3f00: 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20 74 68  ble is set to th
3f10: 65 20 72 6f 6f 74 20 6f 66 0a 2a 2a 20 74 68 65  e root of.** the
3f20: 20 72 65 70 6f 73 69 74 6f 72 79 20 74 72 65 65   repository tree
3f30: 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e   and this routin
3f40: 65 20 72 65 74 75 72 6e 73 20 31 2e 20 20 49 66  e returns 1.  If
3f50: 20 6e 6f 20 64 61 74 61 62 61 73 65 20 69 73 0a   no database is.
3f60: 2a 2a 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20 74  ** found, then t
3f70: 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
3f80: 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  rn 0..**.** This
3f90: 20 72 6f 75 74 69 6e 65 20 61 6c 77 61 79 73 20   routine always 
3fa0: 6f 70 65 6e 73 20 74 68 65 20 75 73 65 72 20 64  opens the user d
3fb0: 61 74 61 62 61 73 65 20 72 65 67 61 72 64 6c 65  atabase regardle
3fc0: 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72  ss of whether or
3fd0: 0a 2a 2a 20 6e 6f 74 20 74 68 65 20 72 65 70 6f  .** not the repo
3fe0: 73 69 74 6f 72 79 20 64 61 74 61 62 61 73 65 20  sitory database 
3ff0: 69 73 20 66 6f 75 6e 64 2e 20 20 49 66 20 74 68  is found.  If th
4000: 65 20 46 4f 53 53 49 4c 20 66 69 6c 65 20 69 73  e FOSSIL file is
4010: 20 66 6f 75 6e 64 2c 0a 2a 2a 20 69 74 20 69 73   found,.** it is
4020: 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68 65   attached to the
4030: 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 63   open database c
4040: 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 6f 2e 0a 2a  onnection too..*
4050: 2f 0a 69 6e 74 20 64 62 5f 6f 70 65 6e 5f 6c 6f  /.int db_open_lo
4060: 63 61 6c 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74  cal(void){.  int
4070: 20 6e 3b 0a 20 20 63 68 61 72 20 7a 50 77 64 5b   n;.  char zPwd[
4080: 32 30 30 30 5d 3b 0a 20 20 63 68 61 72 20 2a 7a  2000];.  char *z
4090: 50 77 64 43 6f 6e 76 3b 0a 20 20 69 66 28 20 67  PwdConv;.  if( g
40a0: 2e 6c 6f 63 61 6c 4f 70 65 6e 29 20 72 65 74 75  .localOpen) retu
40b0: 72 6e 20 31 3b 0a 20 20 69 66 28 20 67 65 74 63  rn 1;.  if( getc
40c0: 77 64 28 7a 50 77 64 2c 20 73 69 7a 65 6f 66 28  wd(zPwd, sizeof(
40d0: 7a 50 77 64 29 2d 32 30 29 3d 3d 30 20 29 7b 0a  zPwd)-20)==0 ){.
40e0: 20 20 20 20 64 62 5f 65 72 72 28 22 70 77 64 20      db_err("pwd 
40f0: 74 6f 6f 20 62 69 67 3a 20 6d 61 78 20 25 64 22  too big: max %d"
4100: 2c 20 73 69 7a 65 6f 66 28 7a 50 77 64 29 2d 32  , sizeof(zPwd)-2
4110: 30 29 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20 73 74  0);.  }.  n = st
4120: 72 6c 65 6e 28 7a 50 77 64 29 3b 0a 20 20 7a 50  rlen(zPwd);.  zP
4130: 77 64 43 6f 6e 76 20 3d 20 6d 70 72 69 6e 74 66  wdConv = mprintf
4140: 28 22 25 2f 22 2c 20 7a 50 77 64 29 3b 0a 20 20  ("%/", zPwd);.  
4150: 73 74 72 6e 63 70 79 28 7a 50 77 64 2c 20 7a 50  strncpy(zPwd, zP
4160: 77 64 43 6f 6e 76 2c 20 32 30 30 30 2d 32 30 29  wdConv, 2000-20)
4170: 3b 0a 20 20 66 72 65 65 28 7a 50 77 64 43 6f 6e  ;.  free(zPwdCon
4180: 76 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e 3e 30  v);.  while( n>0
4190: 20 29 7b 0a 20 20 20 20 69 66 28 20 61 63 63 65   ){.    if( acce
41a0: 73 73 28 7a 50 77 64 2c 20 57 5f 4f 4b 29 20 29  ss(zPwd, W_OK) )
41b0: 20 62 72 65 61 6b 3b 0a 20 20 20 20 73 74 72 63   break;.    strc
41c0: 70 79 28 26 7a 50 77 64 5b 6e 5d 2c 20 22 2f 5f  py(&zPwd[n], "/_
41d0: 46 4f 53 53 49 4c 5f 22 29 3b 0a 20 20 20 20 69  FOSSIL_");.    i
41e0: 66 28 20 69 73 56 61 6c 69 64 4c 6f 63 61 6c 44  f( isValidLocalD
41f0: 62 28 7a 50 77 64 29 20 29 7b 0a 20 20 20 20 20  b(zPwd) ){.     
4200: 20 2f 2a 20 46 6f 75 6e 64 20 61 20 76 61 6c 69   /* Found a vali
4210: 64 20 5f 46 4f 53 53 49 4c 5f 20 66 69 6c 65 20  d _FOSSIL_ file 
4220: 2a 2f 0a 20 20 20 20 20 20 7a 50 77 64 5b 6e 5d  */.      zPwd[n]
4230: 20 3d 20 30 3b 0a 20 20 20 20 20 20 67 2e 7a 4c   = 0;.      g.zL
4240: 6f 63 61 6c 52 6f 6f 74 20 3d 20 6d 70 72 69 6e  ocalRoot = mprin
4250: 74 66 28 22 25 73 2f 22 2c 20 7a 50 77 64 29 3b  tf("%s/", zPwd);
4260: 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
4270: 0a 20 20 20 20 7d 0a 20 20 20 20 6e 2d 2d 3b 0a  .    }.    n--;.
4280: 20 20 20 20 77 68 69 6c 65 28 20 6e 3e 30 20 26      while( n>0 &
4290: 26 20 7a 50 77 64 5b 6e 5d 21 3d 27 2f 27 20 29  & zPwd[n]!='/' )
42a0: 7b 20 6e 2d 2d 3b 20 7d 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 2d 31 5d 3d 3d 27 2f 27 20 29 7b 20 6e 2d 2d  n-1]=='/' ){ n--
42d0: 3b 20 7d 0a 20 20 20 20 7a 50 77 64 5b 6e 5d 20  ; }.    zPwd[n] 
42e0: 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41  = 0;.  }..  /* A
42f0: 20 5f 46 4f 53 53 49 4c 5f 20 66 69 6c 65 20 63   _FOSSIL_ file c
4300: 6f 75 6c 64 20 6e 6f 74 20 62 65 20 66 6f 75 6e  ould not be foun
4310: 64 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b  d */.  return 0;
4320: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74  .}../*.** Open t
4330: 68 65 20 72 65 70 6f 73 69 74 6f 72 79 20 64 61  he repository da
4340: 74 61 62 61 73 65 20 67 69 76 65 6e 20 62 79 20  tabase given by 
4350: 7a 44 62 4e 61 6d 65 2e 20 20 49 66 20 7a 44 62  zDbName.  If zDb
4360: 4e 61 6d 65 3d 3d 4e 55 4c 4c 20 74 68 65 6e 0a  Name==NULL then.
4370: 2a 2a 20 67 65 74 20 74 68 65 20 6e 61 6d 65 20  ** get the name 
4380: 66 72 6f 6d 20 74 68 65 20 61 6c 72 65 61 64 79  from the already
4390: 20 6f 70 65 6e 20 6c 6f 63 61 6c 20 64 61 74 61   open local data
43a0: 62 61 73 65 2e 0a 2a 2f 0a 76 6f 69 64 20 64 62  base..*/.void db
43b0: 5f 6f 70 65 6e 5f 72 65 70 6f 73 69 74 6f 72 79  _open_repository
43c0: 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62  (const char *zDb
43d0: 4e 61 6d 65 29 7b 0a 20 20 69 66 28 20 67 2e 72  Name){.  if( g.r
43e0: 65 70 6f 73 69 74 6f 72 79 4f 70 65 6e 20 29 20  epositoryOpen ) 
43f0: 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 7a 44  return;.  if( zD
4400: 62 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20  bName==0 ){.    
4410: 69 66 28 20 67 2e 6c 6f 63 61 6c 4f 70 65 6e 20  if( g.localOpen 
4420: 29 7b 0a 20 20 20 20 20 20 7a 44 62 4e 61 6d 65  ){.      zDbName
4430: 20 3d 20 64 62 5f 6c 67 65 74 28 22 72 65 70 6f   = db_lget("repo
4440: 73 69 74 6f 72 79 22 2c 20 30 29 3b 0a 20 20 20  sitory", 0);.   
4450: 20 7d 0a 20 20 20 20 69 66 28 20 7a 44 62 4e 61   }.    if( zDbNa
4460: 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 64  me==0 ){.      d
4470: 62 5f 65 72 72 28 22 75 6e 61 62 6c 65 20 74 6f  b_err("unable to
4480: 20 66 69 6e 64 20 74 68 65 20 6e 61 6d 65 20 6f   find the name o
4490: 66 20 61 20 72 65 70 6f 73 69 74 6f 72 79 20 64  f a repository d
44a0: 61 74 61 62 61 73 65 22 29 3b 0a 20 20 20 20 7d  atabase");.    }
44b0: 0a 20 20 7d 0a 20 20 69 66 28 20 61 63 63 65 73  .  }.  if( acces
44c0: 73 28 7a 44 62 4e 61 6d 65 2c 20 52 5f 4f 4b 29  s(zDbName, R_OK)
44d0: 20 7c 7c 20 66 69 6c 65 5f 73 69 7a 65 28 7a 44   || file_size(zD
44e0: 62 4e 61 6d 65 29 3c 31 30 32 34 20 29 7b 0a 20  bName)<1024 ){. 
44f0: 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e 69 63 28     fossil_panic(
4500: 22 6e 6f 20 73 75 63 68 20 72 65 70 6f 73 69 74  "no such reposit
4510: 6f 72 79 3a 20 25 73 22 2c 20 7a 44 62 4e 61 6d  ory: %s", zDbNam
4520: 65 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 6f 70 65  e);.  }.  db_ope
4530: 6e 5f 6f 72 5f 61 74 74 61 63 68 28 7a 44 62 4e  n_or_attach(zDbN
4540: 61 6d 65 2c 20 22 72 65 70 6f 73 69 74 6f 72 79  ame, "repository
4550: 22 29 3b 0a 20 20 67 2e 72 65 70 6f 73 69 74 6f  ");.  g.reposito
4560: 72 79 4f 70 65 6e 20 3d 20 31 3b 0a 20 20 67 2e  ryOpen = 1;.  g.
4570: 7a 52 65 70 6f 73 69 74 6f 72 79 4e 61 6d 65 20  zRepositoryName 
4580: 3d 20 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 20  = mprintf("%s", 
4590: 7a 44 62 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a  zDbName);.}../*.
45a0: 2a 2a 20 54 72 79 20 74 6f 20 66 69 6e 64 20 74  ** Try to find t
45b0: 68 65 20 72 65 70 6f 73 69 74 6f 72 79 20 61 6e  he repository an
45c0: 64 20 6f 70 65 6e 20 69 74 2e 20 20 55 73 65 20  d open it.  Use 
45d0: 74 68 65 20 2d 52 20 6f 72 20 2d 2d 72 65 70 6f  the -R or --repo
45e0: 73 69 74 6f 72 79 0a 2a 2a 20 6f 70 74 69 6f 6e  sitory.** option
45f0: 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 72   to locate the r
4600: 65 70 6f 73 69 74 6f 72 79 2e 20 20 49 66 20 6e  epository.  If n
4610: 6f 20 73 75 63 68 20 6f 70 74 69 6f 6e 20 69 73  o such option is
4620: 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e   available, then
4630: 0a 2a 2a 20 75 73 65 20 74 68 65 20 72 65 70 6f  .** use the repo
4640: 73 69 74 6f 72 79 20 6f 66 20 74 68 65 20 6f 70  sitory of the op
4650: 65 6e 20 63 68 65 63 6b 6f 75 74 20 69 66 20 74  en checkout if t
4660: 68 65 72 65 20 69 73 20 6f 6e 65 2e 0a 2a 2a 0a  here is one..**.
4670: 2a 2a 20 45 72 72 6f 72 20 6f 75 74 20 69 66 20  ** Error out if 
4680: 74 68 65 20 72 65 70 6f 73 69 74 6f 72 79 20 63  the repository c
4690: 61 6e 6e 6f 74 20 62 65 20 6f 70 65 6e 65 64 2e  annot be opened.
46a0: 0a 2a 2f 0a 76 6f 69 64 20 64 62 5f 66 69 6e 64  .*/.void db_find
46b0: 5f 61 6e 64 5f 6f 70 65 6e 5f 72 65 70 6f 73 69  _and_open_reposi
46c0: 74 6f 72 79 28 69 6e 74 20 65 72 72 49 66 4e 6f  tory(int errIfNo
46d0: 74 46 6f 75 6e 64 29 7b 0a 20 20 63 6f 6e 73 74  tFound){.  const
46e0: 20 63 68 61 72 20 2a 7a 52 65 70 20 3d 20 66 69   char *zRep = fi
46f0: 6e 64 5f 6f 70 74 69 6f 6e 28 22 72 65 70 6f 73  nd_option("repos
4700: 69 74 6f 72 79 22 2c 20 22 52 22 2c 20 31 29 3b  itory", "R", 1);
4710: 0a 20 20 69 66 28 20 7a 52 65 70 3d 3d 30 20 29  .  if( zRep==0 )
4720: 7b 0a 20 20 20 20 69 66 28 20 64 62 5f 6f 70 65  {.    if( db_ope
4730: 6e 5f 6c 6f 63 61 6c 28 29 3d 3d 30 20 29 7b 0a  n_local()==0 ){.
4740: 20 20 20 20 20 20 67 6f 74 6f 20 72 65 70 5f 6e        goto rep_n
4750: 6f 74 5f 66 6f 75 6e 64 3b 0a 20 20 20 20 7d 0a  ot_found;.    }.
4760: 20 20 20 20 7a 52 65 70 20 3d 20 64 62 5f 6c 67      zRep = db_lg
4770: 65 74 28 22 72 65 70 6f 73 69 74 6f 72 79 22 2c  et("repository",
4780: 20 30 29 3b 0a 20 20 20 20 69 66 28 20 7a 52 65   0);.    if( zRe
4790: 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f  p==0 ){.      go
47a0: 74 6f 20 72 65 70 5f 6e 6f 74 5f 66 6f 75 6e 64  to rep_not_found
47b0: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64 62  ;.    }.  }.  db
47c0: 5f 6f 70 65 6e 5f 72 65 70 6f 73 69 74 6f 72 79  _open_repository
47d0: 28 7a 52 65 70 29 3b 0a 20 20 69 66 28 20 67 2e  (zRep);.  if( g.
47e0: 72 65 70 6f 73 69 74 6f 72 79 4f 70 65 6e 20 29  repositoryOpen )
47f0: 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
4800: 7d 0a 72 65 70 5f 6e 6f 74 5f 66 6f 75 6e 64 3a  }.rep_not_found:
4810: 0a 20 20 69 66 28 20 65 72 72 49 66 4e 6f 74 46  .  if( errIfNotF
4820: 6f 75 6e 64 20 29 7b 0a 20 20 20 20 66 6f 73 73  ound ){.    foss
4830: 69 6c 5f 66 61 74 61 6c 28 22 75 73 65 20 2d 2d  il_fatal("use --
4840: 72 65 70 6f 73 69 74 6f 72 79 20 6f 72 20 2d 52  repository or -R
4850: 20 74 6f 20 73 70 65 63 69 66 69 63 20 74 68 65   to specific the
4860: 20 72 65 70 6f 73 69 74 6f 72 79 20 64 61 74 61   repository data
4870: 62 61 73 65 22 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  base");.  }.}../
4880: 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 6c 6f  *.** Open the lo
4890: 63 61 6c 20 64 61 74 61 62 61 73 65 2e 20 20 49  cal database.  I
48a0: 66 20 75 6e 61 62 6c 65 2c 20 65 78 69 74 20 77  f unable, exit w
48b0: 69 74 68 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f  ith an error..*/
48c0: 0a 76 6f 69 64 20 64 62 5f 6d 75 73 74 5f 62 65  .void db_must_be
48d0: 5f 77 69 74 68 69 6e 5f 74 72 65 65 28 76 6f 69  _within_tree(voi
48e0: 64 29 7b 0a 20 20 69 66 28 20 64 62 5f 6f 70 65  d){.  if( db_ope
48f0: 6e 5f 6c 6f 63 61 6c 28 29 3d 3d 30 20 29 7b 0a  n_local()==0 ){.
4900: 20 20 20 20 66 6f 73 73 69 6c 5f 66 61 74 61 6c      fossil_fatal
4910: 28 22 6e 6f 74 20 77 69 74 68 69 6e 20 61 6e 20  ("not within an 
4920: 6f 70 65 6e 20 63 68 65 63 6b 6f 75 74 22 29 3b  open checkout");
4930: 0a 20 20 7d 0a 20 20 64 62 5f 6f 70 65 6e 5f 72  .  }.  db_open_r
4940: 65 70 6f 73 69 74 6f 72 79 28 30 29 3b 0a 7d 0a  epository(0);.}.
4950: 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65  ./*.** Close the
4960: 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
4970: 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 64 20 64 62  tion..*/.void db
4980: 5f 63 6c 6f 73 65 28 76 6f 69 64 29 7b 0a 20 20  _close(void){.  
4990: 69 66 28 20 67 2e 64 62 3d 3d 30 20 29 20 72 65  if( g.db==0 ) re
49a0: 74 75 72 6e 3b 0a 20 20 67 2e 72 65 70 6f 73 69  turn;.  g.reposi
49b0: 74 6f 72 79 4f 70 65 6e 20 3d 20 30 3b 0a 20 20  toryOpen = 0;.  
49c0: 67 2e 6c 6f 63 61 6c 4f 70 65 6e 20 3d 20 30 3b  g.localOpen = 0;
49d0: 0a 20 20 67 2e 63 6f 6e 66 69 67 4f 70 65 6e 20  .  g.configOpen 
49e0: 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63  = 0;.  sqlite3_c
49f0: 6c 6f 73 65 28 67 2e 64 62 29 3b 0a 20 20 67 2e  lose(g.db);.  g.
4a00: 64 62 20 3d 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  db = 0;.}.../*.*
4a10: 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 65  * Create a new e
4a20: 6d 70 74 79 20 72 65 70 6f 73 69 74 6f 72 79 20  mpty repository 
4a30: 64 61 74 61 62 61 73 65 20 77 69 74 68 20 74 68  database with th
4a40: 65 20 67 69 76 65 6e 20 6e 61 6d 65 2e 0a 2a 2a  e given name..**
4a50: 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 73 63 68  .** Only the sch
4a60: 65 6d 61 20 69 73 20 69 6e 69 74 69 61 6c 69 7a  ema is initializ
4a70: 65 64 2e 20 20 54 68 65 20 72 65 71 75 69 72 65  ed.  The require
4a80: 64 20 56 41 52 20 74 61 62 6c 65 73 20 65 6e 74  d VAR tables ent
4a90: 72 69 65 73 0a 2a 2a 20 61 72 65 20 6e 6f 74 20  ries.** are not 
4aa0: 73 65 74 20 62 79 20 74 68 69 73 20 72 6f 75 74  set by this rout
4ab0: 69 6e 65 20 61 6e 64 20 6d 75 73 74 20 62 65 20  ine and must be 
4ac0: 73 65 74 20 73 65 70 61 72 61 74 65 6c 79 20 69  set separately i
4ad0: 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 6d 61  n order.** to ma
4ae0: 6b 65 20 74 68 65 20 6e 65 77 20 66 69 6c 65 20  ke the new file 
4af0: 61 20 76 61 6c 69 64 20 64 61 74 61 62 61 73 65  a valid database
4b00: 2e 0a 2a 2f 0a 76 6f 69 64 20 64 62 5f 63 72 65  ..*/.void db_cre
4b10: 61 74 65 5f 72 65 70 6f 73 69 74 6f 72 79 28 63  ate_repository(c
4b20: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
4b30: 6e 61 6d 65 29 7b 0a 20 20 64 62 5f 69 6e 69 74  name){.  db_init
4b40: 5f 64 61 74 61 62 61 73 65 28 0a 20 20 20 20 20  _database(.     
4b50: 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 20  zFilename,.     
4b60: 7a 52 65 70 6f 73 69 74 6f 72 79 53 63 68 65 6d  zRepositorySchem
4b70: 61 31 2c 0a 20 20 20 20 20 7a 52 65 70 6f 73 69  a1,.     zReposi
4b80: 74 6f 72 79 53 63 68 65 6d 61 32 2c 0a 20 20 20  torySchema2,.   
4b90: 20 20 28 63 68 61 72 2a 29 30 0a 20 20 29 3b 0a    (char*)0.  );.
4ba0: 20 20 69 73 4e 65 77 52 65 70 6f 20 3d 20 31 3b    isNewRepo = 1;
4bb0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c 6c 20 61  .}../*.** Fill a
4bc0: 6e 20 65 6d 70 74 79 20 72 65 70 6f 73 69 74 6f  n empty reposito
4bd0: 72 79 20 64 61 74 61 62 61 73 65 20 77 69 74 68  ry database with
4be0: 20 74 68 65 20 62 61 73 69 63 20 69 6e 66 6f 72   the basic infor
4bf0: 6d 61 74 69 6f 6e 20 66 6f 72 20 61 0a 2a 2a 20  mation for a.** 
4c00: 72 65 70 6f 73 69 74 6f 72 79 2e 20 54 68 69 73  repository. This
4c10: 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 73 68 61   function is sha
4c20: 72 65 64 20 62 65 74 77 65 65 6e 20 27 63 72 65  red between 'cre
4c30: 61 74 65 5f 72 65 70 6f 73 69 74 6f 72 79 5f 63  ate_repository_c
4c40: 6d 64 27 0a 2a 2a 20 28 27 6e 65 77 27 29 20 61  md'.** ('new') a
4c50: 6e 64 20 27 72 65 63 6f 6e 73 74 72 75 63 74 5f  nd 'reconstruct_
4c60: 63 6d 64 27 20 28 27 72 65 63 6f 6e 73 74 72 75  cmd' ('reconstru
4c70: 63 74 27 29 2c 20 62 6f 74 68 20 6f 66 20 77 68  ct'), both of wh
4c80: 69 63 68 20 63 72 65 61 74 65 0a 2a 2a 20 6e 65  ich create.** ne
4c90: 77 20 72 65 70 6f 73 69 74 6f 72 69 65 73 2e 0a  w repositories..
4ca0: 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 6b 65 49 6e  **.** The makeIn
4cb0: 69 74 69 61 6c 56 65 72 73 69 6f 6e 20 66 6c 61  itialVersion fla
4cc0: 67 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65  g determines whe
4cd0: 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 6e 20 69  ther or not an i
4ce0: 6e 69 74 69 61 6c 0a 2a 2a 20 6d 61 6e 69 66 65  nitial.** manife
4cf0: 73 74 20 69 73 20 63 72 65 61 74 65 64 2e 20 20  st is created.  
4d00: 54 68 65 20 6d 61 6b 65 53 65 72 76 65 72 43 6f  The makeServerCo
4d10: 64 65 73 20 66 6c 61 67 20 64 65 74 65 72 6d 69  des flag determi
4d20: 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 0a 2a  nes whether or.*
4d30: 2a 20 6e 6f 74 20 73 65 72 76 65 72 20 61 6e 64  * not server and
4d40: 20 70 72 6f 6a 65 63 74 20 63 6f 64 65 73 20 61   project codes a
4d50: 72 65 20 69 6e 76 65 6e 74 65 64 20 66 6f 72 20  re invented for 
4d60: 74 68 69 73 20 72 65 70 6f 73 69 74 6f 72 79 2e  this repository.
4d70: 0a 2a 2f 0a 76 6f 69 64 20 64 62 5f 69 6e 69 74  .*/.void db_init
4d80: 69 61 6c 5f 73 65 74 75 70 20 28 69 6e 74 20 6d  ial_setup (int m
4d90: 61 6b 65 49 6e 69 74 69 61 6c 56 65 72 73 69 6f  akeInitialVersio
4da0: 6e 2c 20 69 6e 74 20 6d 61 6b 65 53 65 72 76 65  n, int makeServe
4db0: 72 43 6f 64 65 73 29 7b 0a 20 20 63 68 61 72 20  rCodes){.  char 
4dc0: 2a 7a 44 61 74 65 3b 0a 20 20 63 6f 6e 73 74 20  *zDate;.  const 
4dd0: 63 68 61 72 20 2a 7a 55 73 65 72 3b 0a 20 20 42  char *zUser;.  B
4de0: 6c 6f 62 20 68 61 73 68 3b 0a 20 20 42 6c 6f 62  lob hash;.  Blob
4df0: 20 6d 61 6e 69 66 65 73 74 3b 0a 0a 20 20 64 62   manifest;..  db
4e00: 5f 73 65 74 28 22 63 6f 6e 74 65 6e 74 2d 73 63  _set("content-sc
4e10: 68 65 6d 61 22 2c 20 43 4f 4e 54 45 4e 54 5f 53  hema", CONTENT_S
4e20: 43 48 45 4d 41 2c 20 30 29 3b 0a 20 20 64 62 5f  CHEMA, 0);.  db_
4e30: 73 65 74 28 22 61 75 78 2d 73 63 68 65 6d 61 22  set("aux-schema"
4e40: 2c 20 41 55 58 5f 53 43 48 45 4d 41 2c 20 30 29  , AUX_SCHEMA, 0)
4e50: 3b 0a 20 20 69 66 28 20 6d 61 6b 65 53 65 72 76  ;.  if( makeServ
4e60: 65 72 43 6f 64 65 73 20 29 7b 0a 20 20 20 20 64  erCodes ){.    d
4e70: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20  b_multi_exec(.  
4e80: 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f      "INSERT INTO
4e90: 20 63 6f 6e 66 69 67 28 6e 61 6d 65 2c 76 61 6c   config(name,val
4ea0: 75 65 29 22 0a 20 20 20 20 20 20 22 20 56 41 4c  ue)".      " VAL
4eb0: 55 45 53 28 27 73 65 72 76 65 72 2d 63 6f 64 65  UES('server-code
4ec0: 27 2c 20 6c 6f 77 65 72 28 68 65 78 28 72 61 6e  ', lower(hex(ran
4ed0: 64 6f 6d 62 6c 6f 62 28 32 30 29 29 29 29 3b 22  domblob(20))));"
4ee0: 0a 20 20 20 20 20 20 22 49 4e 53 45 52 54 20 49  .      "INSERT I
4ef0: 4e 54 4f 20 63 6f 6e 66 69 67 28 6e 61 6d 65 2c  NTO config(name,
4f00: 76 61 6c 75 65 29 22 0a 20 20 20 20 20 20 22 20  value)".      " 
4f10: 56 41 4c 55 45 53 28 27 70 72 6f 6a 65 63 74 2d  VALUES('project-
4f20: 63 6f 64 65 27 2c 20 6c 6f 77 65 72 28 68 65 78  code', lower(hex
4f30: 28 72 61 6e 64 6f 6d 62 6c 6f 62 28 32 30 29 29  (randomblob(20))
4f40: 29 29 3b 22 0a 20 20 20 20 29 3b 0a 20 20 7d 0a  ));".    );.  }.
4f50: 20 20 69 66 28 20 21 64 62 5f 69 73 5f 67 6c 6f    if( !db_is_glo
4f60: 62 61 6c 28 22 61 75 74 6f 73 79 6e 63 22 29 20  bal("autosync") 
4f70: 29 20 64 62 5f 73 65 74 5f 69 6e 74 28 22 61 75  ) db_set_int("au
4f80: 74 6f 73 79 6e 63 22 2c 20 31 2c 20 30 29 3b 0a  tosync", 1, 0);.
4f90: 20 20 69 66 28 20 21 64 62 5f 69 73 5f 67 6c 6f    if( !db_is_glo
4fa0: 62 61 6c 28 22 6c 6f 63 61 6c 61 75 74 68 22 29  bal("localauth")
4fb0: 20 29 20 64 62 5f 73 65 74 5f 69 6e 74 28 22 6c   ) db_set_int("l
4fc0: 6f 63 61 6c 61 75 74 68 22 2c 20 30 2c 20 30 29  ocalauth", 0, 0)
4fd0: 3b 0a 20 20 7a 55 73 65 72 20 3d 20 64 62 5f 67  ;.  zUser = db_g
4fe0: 65 74 28 22 64 65 66 61 75 6c 74 2d 75 73 65 72  et("default-user
4ff0: 22 2c 20 30 29 3b 0a 20 20 69 66 28 20 7a 55 73  ", 0);.  if( zUs
5000: 65 72 3d 3d 30 20 29 7b 0a 23 69 66 64 65 66 20  er==0 ){.#ifdef 
5010: 5f 5f 4d 49 4e 47 57 33 32 5f 5f 0a 20 20 20 20  __MINGW32__.    
5020: 7a 55 73 65 72 20 3d 20 67 65 74 65 6e 76 28 22  zUser = getenv("
5030: 55 53 45 52 4e 41 4d 45 22 29 3b 0a 23 65 6c 73  USERNAME");.#els
5040: 65 0a 20 20 20 20 7a 55 73 65 72 20 3d 20 67 65  e.    zUser = ge
5050: 74 65 6e 76 28 22 55 53 45 52 22 29 3b 0a 23 65  tenv("USER");.#e
5060: 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 7a  ndif.  }.  if( z
5070: 55 73 65 72 3d 3d 30 20 29 7b 0a 20 20 20 20 7a  User==0 ){.    z
5080: 55 73 65 72 20 3d 20 22 72 6f 6f 74 22 3b 0a 20  User = "root";. 
5090: 20 7d 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78   }.  db_multi_ex
50a0: 65 63 28 0a 20 20 20 20 20 22 49 4e 53 45 52 54  ec(.     "INSERT
50b0: 20 49 4e 54 4f 20 75 73 65 72 28 6c 6f 67 69 6e   INTO user(login
50c0: 2c 20 70 77 2c 20 63 61 70 2c 20 69 6e 66 6f 29  , pw, cap, info)
50d0: 22 0a 20 20 20 20 20 22 56 41 4c 55 45 53 28 25  ".     "VALUES(%
50e0: 51 2c 27 27 2c 27 73 27 2c 27 27 29 22 2c 20 7a  Q,'','s','')", z
50f0: 55 73 65 72 0a 20 20 29 3b 0a 20 20 64 62 5f 6d  User.  );.  db_m
5100: 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20 20  ulti_exec(.     
5110: 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 75 73 65  "INSERT INTO use
5120: 72 28 6c 6f 67 69 6e 2c 70 77 2c 63 61 70 2c 69  r(login,pw,cap,i
5130: 6e 66 6f 29 22 0a 20 20 20 20 20 22 20 20 20 56  nfo)".     "   V
5140: 41 4c 55 45 53 28 27 61 6e 6f 6e 79 6d 6f 75 73  ALUES('anonymous
5150: 27 2c 27 61 6e 6f 6e 79 6d 6f 75 73 27 2c 27 68  ','anonymous','h
5160: 6a 6b 6f 72 77 27 2c 27 41 6e 6f 6e 27 29 3b 22  jkorw','Anon');"
5170: 0a 20 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e  .     "INSERT IN
5180: 54 4f 20 75 73 65 72 28 6c 6f 67 69 6e 2c 70 77  TO user(login,pw
5190: 2c 63 61 70 2c 69 6e 66 6f 29 22 0a 20 20 20 20  ,cap,info)".    
51a0: 20 22 20 20 20 56 41 4c 55 45 53 28 27 6e 6f 62   "   VALUES('nob
51b0: 6f 64 79 27 2c 27 27 2c 27 6a 6f 72 27 2c 27 4e  ody','','jor','N
51c0: 6f 62 6f 64 79 27 29 3b 22 0a 20 20 29 3b 0a 20  obody');".  );. 
51d0: 20 75 73 65 72 5f 73 65 6c 65 63 74 28 29 3b 0a   user_select();.
51e0: 0a 20 20 69 66 20 28 6d 61 6b 65 49 6e 69 74 69  .  if (makeIniti
51f0: 61 6c 56 65 72 73 69 6f 6e 29 7b 0a 20 20 20 20  alVersion){.    
5200: 62 6c 6f 62 5f 7a 65 72 6f 28 26 6d 61 6e 69 66  blob_zero(&manif
5210: 65 73 74 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 61  est);.    blob_a
5220: 70 70 65 6e 64 66 28 26 6d 61 6e 69 66 65 73 74  ppendf(&manifest
5230: 2c 20 22 43 20 69 6e 69 74 69 61 6c 5c 5c 73 65  , "C initial\\se
5240: 6d 70 74 79 5c 5c 73 62 61 73 65 6c 69 6e 65 5c  mpty\\sbaseline\
5250: 6e 22 29 3b 0a 20 20 20 20 7a 44 61 74 65 20 3d  n");.    zDate =
5260: 20 64 62 5f 74 65 78 74 28 30 2c 20 22 53 45 4c   db_text(0, "SEL
5270: 45 43 54 20 64 61 74 65 74 69 6d 65 28 27 6e 6f  ECT datetime('no
5280: 77 27 29 22 29 3b 0a 20 20 20 20 7a 44 61 74 65  w')");.    zDate
5290: 5b 31 30 5d 3d 27 54 27 3b 0a 20 20 20 20 62 6c  [10]='T';.    bl
52a0: 6f 62 5f 61 70 70 65 6e 64 66 28 26 6d 61 6e 69  ob_appendf(&mani
52b0: 66 65 73 74 2c 20 22 44 20 25 73 5c 6e 22 2c 20  fest, "D %s\n", 
52c0: 7a 44 61 74 65 29 3b 0a 20 20 20 20 62 6c 6f 62  zDate);.    blob
52d0: 5f 61 70 70 65 6e 64 66 28 26 6d 61 6e 69 66 65  _appendf(&manife
52e0: 73 74 2c 20 22 50 5c 6e 22 29 3b 0a 20 20 20 20  st, "P\n");.    
52f0: 6d 64 35 73 75 6d 5f 69 6e 69 74 28 29 3b 0a 20  md5sum_init();. 
5300: 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28     blob_appendf(
5310: 26 6d 61 6e 69 66 65 73 74 2c 20 22 52 20 25 73  &manifest, "R %s
5320: 5c 6e 22 2c 20 6d 64 35 73 75 6d 5f 66 69 6e 69  \n", md5sum_fini
5330: 73 68 28 30 29 29 3b 0a 20 20 20 20 62 6c 6f 62  sh(0));.    blob
5340: 5f 61 70 70 65 6e 64 66 28 26 6d 61 6e 69 66 65  _appendf(&manife
5350: 73 74 2c 20 22 55 20 25 46 5c 6e 22 2c 20 67 2e  st, "U %F\n", g.
5360: 7a 4c 6f 67 69 6e 29 3b 0a 20 20 20 20 6d 64 35  zLogin);.    md5
5370: 73 75 6d 5f 62 6c 6f 62 28 26 6d 61 6e 69 66 65  sum_blob(&manife
5380: 73 74 2c 20 26 68 61 73 68 29 3b 0a 20 20 20 20  st, &hash);.    
5390: 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 6d 61  blob_appendf(&ma
53a0: 6e 69 66 65 73 74 2c 20 22 5a 20 25 62 5c 6e 22  nifest, "Z %b\n"
53b0: 2c 20 26 68 61 73 68 29 3b 0a 20 20 20 20 62 6c  , &hash);.    bl
53c0: 6f 62 5f 72 65 73 65 74 28 26 68 61 73 68 29 3b  ob_reset(&hash);
53d0: 0a 20 20 20 20 63 6f 6e 74 65 6e 74 5f 70 75 74  .    content_put
53e0: 28 26 6d 61 6e 69 66 65 73 74 2c 20 30 2c 20 30  (&manifest, 0, 0
53f0: 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
5400: 43 4f 4d 4d 41 4e 44 3a 20 6e 65 77 0a 2a 2a 0a  COMMAND: new.**.
5410: 2a 2a 20 55 73 61 67 65 3a 20 25 66 6f 73 73 69  ** Usage: %fossi
5420: 6c 20 6e 65 77 20 46 49 4c 45 4e 41 4d 45 0a 2a  l new FILENAME.*
5430: 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 72 65  *.** Create a re
5440: 70 6f 73 69 74 6f 72 79 20 66 6f 72 20 61 20 6e  pository for a n
5450: 65 77 20 70 72 6f 6a 65 63 74 20 69 6e 20 74 68  ew project in th
5460: 65 20 66 69 6c 65 20 6e 61 6d 65 64 20 46 49 4c  e file named FIL
5470: 45 4e 41 4d 45 2e 0a 2a 2a 20 54 68 69 73 20 63  ENAME..** This c
5480: 6f 6d 6d 61 6e 64 20 69 73 20 64 69 73 74 69 6e  ommand is distin
5490: 63 74 20 66 72 6f 6d 20 22 63 6c 6f 6e 65 22 2e  ct from "clone".
54a0: 20 20 54 68 65 20 22 63 6c 6f 6e 65 22 20 63 6f    The "clone" co
54b0: 6d 6d 61 6e 64 20 6d 61 6b 65 73 0a 2a 2a 20 61  mmand makes.** a
54c0: 20 63 6f 70 79 20 6f 66 20 61 6e 20 65 78 69 73   copy of an exis
54d0: 74 69 6e 67 20 70 72 6f 6a 65 63 74 2e 20 20 54  ting project.  T
54e0: 68 69 73 20 63 6f 6d 6d 61 6e 64 20 73 74 61 72  his command star
54f0: 74 73 20 61 20 6e 65 77 20 70 72 6f 6a 65 63 74  ts a new project
5500: 2e 0a 2a 2f 0a 76 6f 69 64 20 63 72 65 61 74 65  ..*/.void create
5510: 5f 72 65 70 6f 73 69 74 6f 72 79 5f 63 6d 64 28  _repository_cmd(
5520: 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 67 2e 61  void){.  if( g.a
5530: 72 67 63 21 3d 33 20 29 7b 0a 20 20 20 20 75 73  rgc!=3 ){.    us
5540: 61 67 65 28 22 52 45 50 4f 53 49 54 4f 52 59 2d  age("REPOSITORY-
5550: 4e 41 4d 45 22 29 3b 0a 20 20 7d 0a 20 20 64 62  NAME");.  }.  db
5560: 5f 63 72 65 61 74 65 5f 72 65 70 6f 73 69 74 6f  _create_reposito
5570: 72 79 28 67 2e 61 72 67 76 5b 32 5d 29 3b 0a 20  ry(g.argv[2]);. 
5580: 20 64 62 5f 6f 70 65 6e 5f 72 65 70 6f 73 69 74   db_open_reposit
5590: 6f 72 79 28 67 2e 61 72 67 76 5b 32 5d 29 3b 0a  ory(g.argv[2]);.
55a0: 20 20 64 62 5f 6f 70 65 6e 5f 63 6f 6e 66 69 67    db_open_config
55b0: 28 29 3b 0a 20 20 64 62 5f 62 65 67 69 6e 5f 74  ();.  db_begin_t
55c0: 72 61 6e 73 61 63 74 69 6f 6e 28 29 3b 0a 20 20  ransaction();.  
55d0: 64 62 5f 69 6e 69 74 69 61 6c 5f 73 65 74 75 70  db_initial_setup
55e0: 28 31 2c 20 31 29 3b 0a 20 20 64 62 5f 65 6e 64  (1, 1);.  db_end
55f0: 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b  _transaction(0);
5600: 0a 20 20 70 72 69 6e 74 66 28 22 70 72 6f 6a 65  .  printf("proje
5610: 63 74 2d 69 64 3a 20 25 73 5c 6e 22 2c 20 64 62  ct-id: %s\n", db
5620: 5f 67 65 74 28 22 70 72 6f 6a 65 63 74 2d 63 6f  _get("project-co
5630: 64 65 22 2c 20 30 29 29 3b 0a 20 20 70 72 69 6e  de", 0));.  prin
5640: 74 66 28 22 73 65 72 76 65 72 2d 69 64 3a 20 20  tf("server-id:  
5650: 25 73 5c 6e 22 2c 20 64 62 5f 67 65 74 28 22 73  %s\n", db_get("s
5660: 65 72 76 65 72 2d 63 6f 64 65 22 2c 20 30 29 29  erver-code", 0))
5670: 3b 0a 20 20 70 72 69 6e 74 66 28 22 61 64 6d 69  ;.  printf("admi
5680: 6e 2d 75 73 65 72 3a 20 25 73 20 28 6e 6f 20 70  n-user: %s (no p
5690: 61 73 73 77 6f 72 64 20 73 65 74 20 79 65 74 21  assword set yet!
56a0: 29 5c 6e 22 2c 20 67 2e 7a 4c 6f 67 69 6e 29 3b  )\n", g.zLogin);
56b0: 0a 20 20 70 72 69 6e 74 66 28 22 62 61 73 65 6c  .  printf("basel
56c0: 69 6e 65 3a 20 20 20 25 73 5c 6e 22 2c 20 64 62  ine:   %s\n", db
56d0: 5f 74 65 78 74 28 30 2c 20 22 53 45 4c 45 43 54  _text(0, "SELECT
56e0: 20 75 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62 22   uuid FROM blob"
56f0: 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c  ));.}../*.** SQL
5700: 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 64   functions for d
5710: 65 62 75 67 67 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  ebugging..**.** 
5720: 54 68 65 20 70 72 69 6e 74 28 29 20 66 75 6e 63  The print() func
5730: 74 69 6f 6e 20 77 72 69 74 65 73 20 69 74 73 20  tion writes its 
5740: 61 72 67 75 6d 65 6e 74 73 20 6f 6e 20 73 74 64  arguments on std
5750: 6f 75 74 2c 20 62 75 74 20 6f 6e 6c 79 0a 2a 2a  out, but only.**
5760: 20 69 66 20 74 68 65 20 2d 73 71 6c 70 72 69 6e   if the -sqlprin
5770: 74 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 6f  t command-line o
5780: 70 74 69 6f 6e 20 69 73 20 74 75 72 6e 65 64 20  ption is turned 
5790: 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  on..*/.static vo
57a0: 69 64 20 64 62 5f 73 71 6c 5f 70 72 69 6e 74 28  id db_sql_print(
57b0: 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
57c0: 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
57d0: 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
57e0: 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
57f0: 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66  ){.  int i;.  if
5800: 28 20 67 2e 66 53 71 6c 50 72 69 6e 74 20 29 7b  ( g.fSqlPrint ){
5810: 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
5820: 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  argc; i++){.    
5830: 20 20 63 68 61 72 20 63 20 3d 20 69 3d 3d 61 72    char c = i==ar
5840: 67 63 2d 31 20 3f 20 27 5c 6e 27 20 3a 20 27 20  gc-1 ? '\n' : ' 
5850: 27 3b 0a 20 20 20 20 20 20 70 72 69 6e 74 66 28  ';.      printf(
5860: 22 25 73 25 63 22 2c 20 73 71 6c 69 74 65 33 5f  "%s%c", sqlite3_
5870: 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
5880: 69 5d 29 2c 20 63 29 3b 0a 20 20 20 20 7d 0a 20  i]), c);.    }. 
5890: 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64   }.}.static void
58a0: 20 64 62 5f 73 71 6c 5f 74 72 61 63 65 28 76 6f   db_sql_trace(vo
58b0: 69 64 20 2a 6e 6f 74 55 73 65 64 2c 20 63 6f 6e  id *notUsed, con
58c0: 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 29 7b 0a  st char *zSql){.
58d0: 20 20 70 72 69 6e 74 66 28 22 25 73 5c 6e 22 2c    printf("%s\n",
58e0: 20 7a 53 71 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   zSql);.}../*.**
58f0: 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79   This is used by
5900: 20 74 68 65 20 5b 63 6f 6d 6d 69 74 5d 20 63 6f   the [commit] co
5910: 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  mmand..**.** Ret
5920: 75 72 6e 20 74 72 75 65 20 69 66 20 65 69 74 68  urn true if eith
5930: 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 29  er:.**.**     a)
5940: 20 47 6c 6f 62 61 6c 2e 61 43 6f 6d 6d 69 74 46   Global.aCommitF
5950: 69 6c 65 20 69 73 20 4e 55 4c 4c 2c 20 6f 72 0a  ile is NULL, or.
5960: 2a 2a 20 20 20 20 20 62 29 20 47 6c 6f 62 61 6c  **     b) Global
5970: 2e 61 43 6f 6d 6d 69 74 46 69 6c 65 20 63 6f 6e  .aCommitFile con
5980: 74 61 69 6e 73 20 74 68 65 20 69 6e 74 65 67 65  tains the intege
5990: 72 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 61  r passed as an a
59a0: 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f  rgument..**.** O
59b0: 74 68 65 72 77 69 73 65 20 72 65 74 75 72 6e 20  therwise return 
59c0: 66 61 6c 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  false..*/.static
59d0: 20 76 6f 69 64 20 66 69 6c 65 5f 69 73 5f 73 65   void file_is_se
59e0: 6c 65 63 74 65 64 28 0a 20 20 73 71 6c 69 74 65  lected(.  sqlite
59f0: 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
5a00: 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
5a10: 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
5a20: 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 61 73 73 65  **argv.){.  asse
5a30: 72 74 28 61 72 67 63 3d 3d 31 29 3b 0a 20 20 69  rt(argc==1);.  i
5a40: 66 28 20 67 2e 61 43 6f 6d 6d 69 74 46 69 6c 65  f( g.aCommitFile
5a50: 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 49 64 20   ){.    int iId 
5a60: 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
5a70: 69 6e 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  int(argv[0]);.  
5a80: 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 66 6f    int ii;.    fo
5a90: 72 28 69 69 3d 30 3b 20 67 2e 61 43 6f 6d 6d 69  r(ii=0; g.aCommi
5aa0: 74 46 69 6c 65 5b 69 69 5d 3b 20 69 69 2b 2b 29  tFile[ii]; ii++)
5ab0: 7b 0a 20 20 20 20 20 20 69 66 28 20 69 49 64 3d  {.      if( iId=
5ac0: 3d 67 2e 61 43 6f 6d 6d 69 74 46 69 6c 65 5b 69  =g.aCommitFile[i
5ad0: 69 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  i] ){.        sq
5ae0: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
5af0: 28 63 6f 6e 74 65 78 74 2c 20 31 29 3b 0a 20 20  (context, 1);.  
5b00: 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
5b10: 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
5b20: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
5b30: 6e 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a  nt(context, 0);.
5b40: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
5b50: 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28  ite3_result_int(
5b60: 63 6f 6e 74 65 78 74 2c 20 31 29 3b 0a 20 20 7d  context, 1);.  }
5b70: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
5b80: 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72  unction register
5b90: 73 20 61 75 78 69 6c 69 61 72 79 20 66 75 6e 63  s auxiliary func
5ba0: 74 69 6f 6e 73 20 77 68 65 6e 20 74 68 65 20 53  tions when the S
5bb0: 51 4c 69 74 65 0a 2a 2a 20 64 61 74 61 62 61 73  QLite.** databas
5bc0: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
5bd0: 66 69 72 73 74 20 65 73 74 61 62 6c 69 73 68 65  first establishe
5be0: 64 2e 0a 2a 2f 0a 4c 4f 43 41 4c 20 76 6f 69 64  d..*/.LOCAL void
5bf0: 20 64 62 5f 63 6f 6e 6e 65 63 74 69 6f 6e 5f 69   db_connection_i
5c00: 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61  nit(void){.  sta
5c10: 74 69 63 20 69 6e 74 20 6f 6e 63 65 20 3d 20 31  tic int once = 1
5c20: 3b 0a 20 20 69 66 28 20 6f 6e 63 65 20 29 7b 0a  ;.  if( once ){.
5c30: 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
5c40: 74 65 5f 66 75 6e 63 74 69 6f 6e 28 67 2e 64 62  te_function(g.db
5c50: 2c 20 22 70 72 69 6e 74 22 2c 20 2d 31 2c 20 53  , "print", -1, S
5c60: 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 64 62  QLITE_UTF8, 0,db
5c70: 5f 73 71 6c 5f 70 72 69 6e 74 2c 30 2c 30 29 3b  _sql_print,0,0);
5c80: 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
5c90: 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20  ate_function(.  
5ca0: 20 20 20 20 67 2e 64 62 2c 20 22 66 69 6c 65 5f      g.db, "file_
5cb0: 69 73 5f 73 65 6c 65 63 74 65 64 22 2c 20 31 2c  is_selected", 1,
5cc0: 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c   SQLITE_UTF8, 0,
5cd0: 20 66 69 6c 65 5f 69 73 5f 73 65 6c 65 63 74 65   file_is_selecte
5ce0: 64 2c 30 2c 30 0a 20 20 20 20 29 3b 0a 20 20 20  d,0,0.    );.   
5cf0: 20 69 66 28 20 67 2e 66 53 71 6c 54 72 61 63 65   if( g.fSqlTrace
5d00: 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
5d10: 33 5f 74 72 61 63 65 28 67 2e 64 62 2c 20 64 62  3_trace(g.db, db
5d20: 5f 73 71 6c 5f 74 72 61 63 65 2c 20 30 29 3b 0a  _sql_trace, 0);.
5d30: 20 20 20 20 7d 0a 20 20 20 20 6f 6e 63 65 20 3d      }.    once =
5d40: 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   0;.  }.}../*.**
5d50: 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
5d60: 74 68 65 20 73 74 72 69 6e 67 20 7a 56 61 6c 20  the string zVal 
5d70: 72 65 70 72 65 73 65 6e 74 73 20 22 74 72 75 65  represents "true
5d80: 22 20 28 6f 72 20 22 66 61 6c 73 65 22 29 2e 0a  " (or "false")..
5d90: 2a 2f 0a 69 6e 74 20 69 73 5f 74 72 75 74 68 28  */.int is_truth(
5da0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 61 6c  const char *zVal
5db0: 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ){.  static cons
5dc0: 74 20 63 68 61 72 20 2a 61 7a 4f 6e 5b 5d 20 3d  t char *azOn[] =
5dd0: 20 7b 20 22 6f 6e 22 2c 20 22 79 65 73 22 2c 20   { "on", "yes", 
5de0: 22 74 72 75 65 22 2c 20 22 31 22 20 7d 3b 0a 20  "true", "1" };. 
5df0: 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
5e00: 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 7a 4f 6e  0; i<sizeof(azOn
5e10: 29 2f 73 69 7a 65 6f 66 28 61 7a 4f 6e 5b 30 5d  )/sizeof(azOn[0]
5e20: 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  ); i++){.    if(
5e30: 20 73 74 72 63 6d 70 28 7a 56 61 6c 2c 61 7a 4f   strcmp(zVal,azO
5e40: 6e 5b 69 5d 29 3d 3d 30 20 29 20 72 65 74 75 72  n[i])==0 ) retur
5e50: 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  n 1;.  }.  retur
5e60: 6e 20 30 3b 0a 7d 0a 69 6e 74 20 69 73 5f 66 61  n 0;.}.int is_fa
5e70: 6c 73 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  lse(const char *
5e80: 7a 56 61 6c 29 7b 0a 20 20 73 74 61 74 69 63 20  zVal){.  static 
5e90: 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 7a 4f 66  const char *azOf
5ea0: 66 5b 5d 20 3d 20 7b 20 22 6f 66 66 22 2c 20 22  f[] = { "off", "
5eb0: 6e 6f 22 2c 20 22 66 61 6c 73 65 22 2c 20 22 30  no", "false", "0
5ec0: 22 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  " };.  int i;.  
5ed0: 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f  for(i=0; i<sizeo
5ee0: 66 28 61 7a 4f 66 66 29 2f 73 69 7a 65 6f 66 28  f(azOff)/sizeof(
5ef0: 61 7a 4f 66 66 5b 30 5d 29 3b 20 69 2b 2b 29 7b  azOff[0]); i++){
5f00: 0a 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28  .    if( strcmp(
5f10: 7a 56 61 6c 2c 61 7a 4f 66 66 5b 69 5d 29 3d 3d  zVal,azOff[i])==
5f20: 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  0 ) return 1;.  
5f30: 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
5f40: 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 6e 64 20 73  ./*.** Get and s
5f50: 65 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74  et values from t
5f60: 68 65 20 43 4f 4e 46 49 47 2c 20 47 4c 4f 42 41  he CONFIG, GLOBA
5f70: 4c 5f 43 4f 4e 46 49 47 20 61 6e 64 20 56 56 41  L_CONFIG and VVA
5f80: 52 20 74 61 62 6c 65 20 69 6e 20 74 68 65 0a 2a  R table in the.*
5f90: 2a 20 72 65 70 6f 73 69 74 6f 72 79 20 61 6e 64  * repository and
5fa0: 20 6c 6f 63 61 6c 20 64 61 74 61 62 61 73 65 73   local databases
5fb0: 2e 0a 2a 2f 0a 63 68 61 72 20 2a 64 62 5f 67 65  ..*/.char *db_ge
5fc0: 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  t(const char *zN
5fd0: 61 6d 65 2c 20 63 68 61 72 20 2a 7a 44 65 66 61  ame, char *zDefa
5fe0: 75 6c 74 29 7b 0a 20 20 63 68 61 72 20 2a 7a 20  ult){.  char *z 
5ff0: 3d 20 30 3b 0a 20 20 69 66 28 20 67 2e 72 65 70  = 0;.  if( g.rep
6000: 6f 73 69 74 6f 72 79 4f 70 65 6e 20 29 7b 0a 20  ositoryOpen ){. 
6010: 20 20 20 7a 20 3d 20 64 62 5f 74 65 78 74 28 30     z = db_text(0
6020: 2c 20 22 53 45 4c 45 43 54 20 76 61 6c 75 65 20  , "SELECT value 
6030: 46 52 4f 4d 20 63 6f 6e 66 69 67 20 57 48 45 52  FROM config WHER
6040: 45 20 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d  E name=%Q", zNam
6050: 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 3d  e);.  }.  if( z=
6060: 3d 30 20 26 26 20 67 2e 63 6f 6e 66 69 67 4f 70  =0 && g.configOp
6070: 65 6e 20 29 7b 0a 20 20 20 20 7a 20 3d 20 64 62  en ){.    z = db
6080: 5f 74 65 78 74 28 30 2c 20 22 53 45 4c 45 43 54  _text(0, "SELECT
6090: 20 76 61 6c 75 65 20 46 52 4f 4d 20 67 6c 6f 62   value FROM glob
60a0: 61 6c 5f 63 6f 6e 66 69 67 20 57 48 45 52 45 20  al_config WHERE 
60b0: 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d 65 29  name=%Q", zName)
60c0: 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 3d 3d 30  ;.  }.  if( z==0
60d0: 20 29 7b 0a 20 20 20 20 7a 20 3d 20 7a 44 65 66   ){.    z = zDef
60e0: 61 75 6c 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ault;.  }.  retu
60f0: 72 6e 20 7a 3b 0a 7d 0a 76 6f 69 64 20 64 62 5f  rn z;.}.void db_
6100: 73 65 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  set(const char *
6110: 7a 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61  zName, const cha
6120: 72 20 2a 7a 56 61 6c 75 65 2c 20 69 6e 74 20 67  r *zValue, int g
6130: 6c 6f 62 61 6c 46 6c 61 67 29 7b 0a 20 20 64 62  lobalFlag){.  db
6140: 5f 62 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69  _begin_transacti
6150: 6f 6e 28 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69  on();.  db_multi
6160: 5f 65 78 65 63 28 22 52 45 50 4c 41 43 45 20 49  _exec("REPLACE I
6170: 4e 54 4f 20 25 73 63 6f 6e 66 69 67 28 6e 61 6d  NTO %sconfig(nam
6180: 65 2c 76 61 6c 75 65 29 20 56 41 4c 55 45 53 28  e,value) VALUES(
6190: 25 51 2c 25 51 29 22 2c 0a 20 20 20 20 20 20 20  %Q,%Q)",.       
61a0: 20 20 20 20 20 20 20 20 20 20 67 6c 6f 62 61 6c            global
61b0: 46 6c 61 67 20 3f 20 22 67 6c 6f 62 61 6c 5f 22  Flag ? "global_"
61c0: 20 3a 20 22 22 2c 20 7a 4e 61 6d 65 2c 20 7a 56   : "", zName, zV
61d0: 61 6c 75 65 29 3b 0a 20 20 69 66 28 20 67 6c 6f  alue);.  if( glo
61e0: 62 61 6c 46 6c 61 67 20 26 26 20 67 2e 72 65 70  balFlag && g.rep
61f0: 6f 73 69 74 6f 72 79 4f 70 65 6e 20 29 7b 0a 20  ositoryOpen ){. 
6200: 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63     db_multi_exec
6210: 28 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 63 6f  ("DELETE FROM co
6220: 6e 66 69 67 20 57 48 45 52 45 20 6e 61 6d 65 3d  nfig WHERE name=
6230: 25 51 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 7d  %Q", zName);.  }
6240: 0a 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61  .  db_end_transa
6250: 63 74 69 6f 6e 28 30 29 3b 0a 7d 0a 76 6f 69 64  ction(0);.}.void
6260: 20 64 62 5f 75 6e 73 65 74 28 63 6f 6e 73 74 20   db_unset(const 
6270: 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74  char *zName, int
6280: 20 67 6c 6f 62 61 6c 46 6c 61 67 29 7b 0a 20 20   globalFlag){.  
6290: 64 62 5f 62 65 67 69 6e 5f 74 72 61 6e 73 61 63  db_begin_transac
62a0: 74 69 6f 6e 28 29 3b 0a 20 20 64 62 5f 6d 75 6c  tion();.  db_mul
62b0: 74 69 5f 65 78 65 63 28 22 44 45 4c 45 54 45 20  ti_exec("DELETE 
62c0: 46 52 4f 4d 20 25 73 63 6f 6e 66 69 67 20 57 48  FROM %sconfig WH
62d0: 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c 0a 20 20  ERE name=%Q",.  
62e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 67                 g
62f0: 6c 6f 62 61 6c 46 6c 61 67 20 3f 20 22 67 6c 6f  lobalFlag ? "glo
6300: 62 61 6c 5f 22 20 3a 20 22 22 2c 20 7a 4e 61 6d  bal_" : "", zNam
6310: 65 29 3b 0a 20 20 69 66 28 20 67 6c 6f 62 61 6c  e);.  if( global
6320: 46 6c 61 67 20 26 26 20 67 2e 72 65 70 6f 73 69  Flag && g.reposi
6330: 74 6f 72 79 4f 70 65 6e 20 29 7b 0a 20 20 20 20  toryOpen ){.    
6340: 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 44  db_multi_exec("D
6350: 45 4c 45 54 45 20 46 52 4f 4d 20 63 6f 6e 66 69  ELETE FROM confi
6360: 67 20 57 48 45 52 45 20 6e 61 6d 65 3d 25 51 22  g WHERE name=%Q"
6370: 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20  , zName);.  }.  
6380: 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69  db_end_transacti
6390: 6f 6e 28 30 29 3b 0a 7d 0a 69 6e 74 20 64 62 5f  on(0);.}.int db_
63a0: 69 73 5f 67 6c 6f 62 61 6c 28 63 6f 6e 73 74 20  is_global(const 
63b0: 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20  char *zName){.  
63c0: 69 66 28 20 67 2e 63 6f 6e 66 69 67 4f 70 65 6e  if( g.configOpen
63d0: 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 64   ){.    return d
63e0: 62 5f 65 78 69 73 74 73 28 22 53 45 4c 45 43 54  b_exists("SELECT
63f0: 20 31 20 46 52 4f 4d 20 67 6c 6f 62 61 6c 5f 63   1 FROM global_c
6400: 6f 6e 66 69 67 20 57 48 45 52 45 20 6e 61 6d 65  onfig WHERE name
6410: 3d 25 51 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  =%Q", zName);.  
6420: 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
6430: 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 69 6e 74 20 64  n 0;.  }.}.int d
6440: 62 5f 67 65 74 5f 69 6e 74 28 63 6f 6e 73 74 20  b_get_int(const 
6450: 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74  char *zName, int
6460: 20 64 66 6c 74 29 7b 0a 20 20 69 6e 74 20 76 20   dflt){.  int v 
6470: 3d 20 64 66 6c 74 3b 0a 20 20 69 6e 74 20 72 63  = dflt;.  int rc
6480: 3b 0a 20 20 69 66 28 20 67 2e 72 65 70 6f 73 69  ;.  if( g.reposi
6490: 74 6f 72 79 4f 70 65 6e 20 29 7b 0a 20 20 20 20  toryOpen ){.    
64a0: 53 74 6d 74 20 71 3b 0a 20 20 20 20 64 62 5f 70  Stmt q;.    db_p
64b0: 72 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45  repare(&q, "SELE
64c0: 43 54 20 76 61 6c 75 65 20 46 52 4f 4d 20 63 6f  CT value FROM co
64d0: 6e 66 69 67 20 57 48 45 52 45 20 6e 61 6d 65 3d  nfig WHERE name=
64e0: 25 51 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  %Q", zName);.   
64f0: 20 72 63 20 3d 20 64 62 5f 73 74 65 70 28 26 71   rc = db_step(&q
6500: 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
6510: 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
6520: 20 20 20 76 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e     v = db_column
6530: 5f 69 6e 74 28 26 71 2c 20 30 29 3b 0a 20 20 20  _int(&q, 0);.   
6540: 20 7d 0a 20 20 20 20 64 62 5f 66 69 6e 61 6c 69   }.    db_finali
6550: 7a 65 28 26 71 29 3b 0a 20 20 7d 65 6c 73 65 7b  ze(&q);.  }else{
6560: 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
6570: 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 66 28  _DONE;.  }.  if(
6580: 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc==SQLITE_DONE
6590: 20 26 26 20 67 2e 63 6f 6e 66 69 67 4f 70 65 6e   && g.configOpen
65a0: 20 29 7b 0a 20 20 20 20 76 20 3d 20 64 62 5f 69   ){.    v = db_i
65b0: 6e 74 28 64 66 6c 74 2c 20 22 53 45 4c 45 43 54  nt(dflt, "SELECT
65c0: 20 76 61 6c 75 65 20 46 52 4f 4d 20 67 6c 6f 62   value FROM glob
65d0: 61 6c 5f 63 6f 6e 66 69 67 20 57 48 45 52 45 20  al_config WHERE 
65e0: 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d 65 29  name=%Q", zName)
65f0: 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 76  ;.  }.  return v
6600: 3b 0a 7d 0a 76 6f 69 64 20 64 62 5f 73 65 74 5f  ;.}.void db_set_
6610: 69 6e 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  int(const char *
6620: 7a 4e 61 6d 65 2c 20 69 6e 74 20 76 61 6c 75 65  zName, int value
6630: 2c 20 69 6e 74 20 67 6c 6f 62 61 6c 46 6c 61 67  , int globalFlag
6640: 29 7b 0a 20 20 64 62 5f 62 65 67 69 6e 5f 74 72  ){.  db_begin_tr
6650: 61 6e 73 61 63 74 69 6f 6e 28 29 3b 0a 20 20 64  ansaction();.  d
6660: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 52 45  b_multi_exec("RE
6670: 50 4c 41 43 45 20 49 4e 54 4f 20 25 73 63 6f 6e  PLACE INTO %scon
6680: 66 69 67 28 6e 61 6d 65 2c 76 61 6c 75 65 29 20  fig(name,value) 
6690: 56 41 4c 55 45 53 28 25 51 2c 25 64 29 22 2c 0a  VALUES(%Q,%d)",.
66a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
66b0: 67 6c 6f 62 61 6c 46 6c 61 67 20 3f 20 22 67 6c  globalFlag ? "gl
66c0: 6f 62 61 6c 5f 22 20 3a 20 22 22 2c 20 7a 4e 61  obal_" : "", zNa
66d0: 6d 65 2c 20 76 61 6c 75 65 29 3b 0a 20 20 69 66  me, value);.  if
66e0: 28 20 67 6c 6f 62 61 6c 46 6c 61 67 20 26 26 20  ( globalFlag && 
66f0: 67 2e 72 65 70 6f 73 69 74 6f 72 79 4f 70 65 6e  g.repositoryOpen
6700: 20 29 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69   ){.    db_multi
6710: 5f 65 78 65 63 28 22 44 45 4c 45 54 45 20 46 52  _exec("DELETE FR
6720: 4f 4d 20 63 6f 6e 66 69 67 20 57 48 45 52 45 20  OM config WHERE 
6730: 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d 65 29  name=%Q", zName)
6740: 3b 0a 20 20 7d 0a 20 20 64 62 5f 65 6e 64 5f 74  ;.  }.  db_end_t
6750: 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b 0a 7d  ransaction(0);.}
6760: 0a 69 6e 74 20 64 62 5f 67 65 74 5f 62 6f 6f 6c  .int db_get_bool
6770: 65 61 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ean(const char *
6780: 7a 4e 61 6d 65 2c 20 69 6e 74 20 64 66 6c 74 29  zName, int dflt)
6790: 7b 0a 20 20 63 68 61 72 20 2a 7a 56 61 6c 20 3d  {.  char *zVal =
67a0: 20 64 62 5f 67 65 74 28 7a 4e 61 6d 65 2c 20 64   db_get(zName, d
67b0: 66 6c 74 20 3f 20 22 6f 6e 22 20 3a 20 22 6f 66  flt ? "on" : "of
67c0: 66 22 29 3b 0a 20 20 69 66 28 20 69 73 5f 74 72  f");.  if( is_tr
67d0: 75 74 68 28 7a 56 61 6c 29 20 29 20 72 65 74 75  uth(zVal) ) retu
67e0: 72 6e 20 31 3b 0a 20 20 69 66 28 20 69 73 5f 66  rn 1;.  if( is_f
67f0: 61 6c 73 65 28 7a 56 61 6c 29 20 29 20 72 65 74  alse(zVal) ) ret
6800: 75 72 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e 20  urn 0;.  return 
6810: 64 66 6c 74 3b 0a 7d 0a 63 68 61 72 20 2a 64 62  dflt;.}.char *db
6820: 5f 6c 67 65 74 28 63 6f 6e 73 74 20 63 68 61 72  _lget(const char
6830: 20 2a 7a 4e 61 6d 65 2c 20 63 68 61 72 20 2a 7a   *zName, char *z
6840: 44 65 66 61 75 6c 74 29 7b 0a 20 20 72 65 74 75  Default){.  retu
6850: 72 6e 20 64 62 5f 74 65 78 74 28 28 63 68 61 72  rn db_text((char
6860: 2a 29 7a 44 65 66 61 75 6c 74 2c 0a 20 20 20 20  *)zDefault,.    
6870: 20 20 20 20 20 20 20 20 20 20 20 20 20 22 53 45               "SE
6880: 4c 45 43 54 20 76 61 6c 75 65 20 46 52 4f 4d 20  LECT value FROM 
6890: 76 76 61 72 20 57 48 45 52 45 20 6e 61 6d 65 3d  vvar WHERE name=
68a0: 25 51 22 2c 20 7a 4e 61 6d 65 29 3b 0a 7d 0a 76  %Q", zName);.}.v
68b0: 6f 69 64 20 64 62 5f 6c 73 65 74 28 63 6f 6e 73  oid db_lset(cons
68c0: 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63  t char *zName, c
68d0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 61 6c 75  onst char *zValu
68e0: 65 29 7b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65  e){.  db_multi_e
68f0: 78 65 63 28 22 52 45 50 4c 41 43 45 20 49 4e 54  xec("REPLACE INT
6900: 4f 20 76 76 61 72 28 6e 61 6d 65 2c 76 61 6c 75  O vvar(name,valu
6910: 65 29 20 56 41 4c 55 45 53 28 25 51 2c 25 51 29  e) VALUES(%Q,%Q)
6920: 22 2c 20 7a 4e 61 6d 65 2c 20 7a 56 61 6c 75 65  ", zName, zValue
6930: 29 3b 0a 7d 0a 69 6e 74 20 64 62 5f 6c 67 65 74  );.}.int db_lget
6940: 5f 69 6e 74 28 63 6f 6e 73 74 20 63 68 61 72 20  _int(const char 
6950: 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 64 66 6c 74  *zName, int dflt
6960: 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 5f 69  ){.  return db_i
6970: 6e 74 28 64 66 6c 74 2c 20 22 53 45 4c 45 43 54  nt(dflt, "SELECT
6980: 20 76 61 6c 75 65 20 46 52 4f 4d 20 76 76 61 72   value FROM vvar
6990: 20 57 48 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c   WHERE name=%Q",
69a0: 20 7a 4e 61 6d 65 29 3b 0a 7d 0a 76 6f 69 64 20   zName);.}.void 
69b0: 64 62 5f 6c 73 65 74 5f 69 6e 74 28 63 6f 6e 73  db_lset_int(cons
69c0: 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69  t char *zName, i
69d0: 6e 74 20 76 61 6c 75 65 29 7b 0a 20 20 64 62 5f  nt value){.  db_
69e0: 6d 75 6c 74 69 5f 65 78 65 63 28 22 52 45 50 4c  multi_exec("REPL
69f0: 41 43 45 20 49 4e 54 4f 20 76 76 61 72 28 6e 61  ACE INTO vvar(na
6a00: 6d 65 2c 76 61 6c 75 65 29 20 56 41 4c 55 45 53  me,value) VALUES
6a10: 28 25 51 2c 25 64 29 22 2c 20 7a 4e 61 6d 65 2c  (%Q,%d)", zName,
6a20: 20 76 61 6c 75 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a   value);.}../*.*
6a30: 2a 20 43 4f 4d 4d 41 4e 44 3a 20 6f 70 65 6e 0a  * COMMAND: open.
6a40: 2a 2a 0a 2a 2a 20 55 73 61 67 65 3a 20 6f 70 65  **.** Usage: ope
6a50: 6e 20 46 49 4c 45 4e 41 4d 45 0a 2a 2a 0a 2a 2a  n FILENAME.**.**
6a60: 20 4f 70 65 6e 20 61 20 63 6f 6e 6e 65 63 74 69   Open a connecti
6a70: 6f 6e 20 74 6f 20 74 68 65 20 6c 6f 63 61 6c 20  on to the local 
6a80: 72 65 70 6f 73 69 74 6f 72 79 20 69 6e 20 46 49  repository in FI
6a90: 4c 45 4e 41 4d 45 2e 20 20 41 20 63 68 65 63 6b  LENAME.  A check
6aa0: 6f 75 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 72  out.** for the r
6ab0: 65 70 6f 73 69 74 6f 72 79 20 69 73 20 63 72 65  epository is cre
6ac0: 61 74 65 64 20 77 69 74 68 20 69 74 73 20 72 6f  ated with its ro
6ad0: 6f 74 20 61 74 20 74 68 65 20 77 6f 72 6b 69 6e  ot at the workin
6ae0: 67 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 20  g directory..** 
6af0: 53 65 65 20 61 6c 73 6f 20 74 68 65 20 22 63 6c  See also the "cl
6b00: 6f 73 65 22 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f  ose" command..*/
6b10: 0a 76 6f 69 64 20 63 6d 64 5f 6f 70 65 6e 28 76  .void cmd_open(v
6b20: 6f 69 64 29 7b 0a 20 20 42 6c 6f 62 20 70 61 74  oid){.  Blob pat
6b30: 68 3b 0a 20 20 69 6e 74 20 76 69 64 3b 0a 20 20  h;.  int vid;.  
6b40: 73 74 61 74 69 63 20 63 68 61 72 20 2a 61 7a 4e  static char *azN
6b50: 65 77 41 72 67 76 5b 5d 20 3d 20 7b 20 30 2c 20  ewArgv[] = { 0, 
6b60: 22 75 70 64 61 74 65 22 2c 20 22 2d 2d 6c 61 74  "update", "--lat
6b70: 65 73 74 22 2c 20 30 20 7d 3b 0a 20 20 75 72 6c  est", 0 };.  url
6b80: 5f 70 72 6f 78 79 5f 6f 70 74 69 6f 6e 73 28 29  _proxy_options()
6b90: 3b 0a 20 20 69 66 28 20 67 2e 61 72 67 63 21 3d  ;.  if( g.argc!=
6ba0: 33 20 29 7b 0a 20 20 20 20 75 73 61 67 65 28 22  3 ){.    usage("
6bb0: 52 45 50 4f 53 49 54 4f 52 59 2d 46 49 4c 45 4e  REPOSITORY-FILEN
6bc0: 41 4d 45 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28  AME");.  }.  if(
6bd0: 20 64 62 5f 6f 70 65 6e 5f 6c 6f 63 61 6c 28 29   db_open_local()
6be0: 20 29 7b 0a 20 20 20 20 66 6f 73 73 69 6c 5f 70   ){.    fossil_p
6bf0: 61 6e 69 63 28 22 61 6c 72 65 61 64 79 20 77 69  anic("already wi
6c00: 74 68 69 6e 20 61 6e 20 6f 70 65 6e 20 74 72 65  thin an open tre
6c10: 65 20 72 6f 6f 74 65 64 20 61 74 20 25 73 22 2c  e rooted at %s",
6c20: 20 67 2e 7a 4c 6f 63 61 6c 52 6f 6f 74 29 3b 0a   g.zLocalRoot);.
6c30: 20 20 7d 0a 20 20 66 69 6c 65 5f 63 61 6e 6f 6e    }.  file_canon
6c40: 69 63 61 6c 5f 6e 61 6d 65 28 67 2e 61 72 67 76  ical_name(g.argv
6c50: 5b 32 5d 2c 20 26 70 61 74 68 29 3b 0a 20 20 64  [2], &path);.  d
6c60: 62 5f 6f 70 65 6e 5f 72 65 70 6f 73 69 74 6f 72  b_open_repositor
6c70: 79 28 62 6c 6f 62 5f 73 74 72 28 26 70 61 74 68  y(blob_str(&path
6c80: 29 29 3b 0a 20 20 64 62 5f 69 6e 69 74 5f 64 61  ));.  db_init_da
6c90: 74 61 62 61 73 65 28 22 2e 2f 5f 46 4f 53 53 49  tabase("./_FOSSI
6ca0: 4c 5f 22 2c 20 7a 4c 6f 63 61 6c 53 63 68 65 6d  L_", zLocalSchem
6cb0: 61 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20  a, (char*)0);.  
6cc0: 64 62 5f 6f 70 65 6e 5f 6c 6f 63 61 6c 28 29 3b  db_open_local();
6cd0: 0a 20 20 64 62 5f 6c 73 65 74 28 22 72 65 70 6f  .  db_lset("repo
6ce0: 73 69 74 6f 72 79 22 2c 20 62 6c 6f 62 5f 73 74  sitory", blob_st
6cf0: 72 28 26 70 61 74 68 29 29 3b 0a 20 20 76 69 64  r(&path));.  vid
6d00: 20 3d 20 64 62 5f 69 6e 74 28 30 2c 20 22 53 45   = db_int(0, "SE
6d10: 4c 45 43 54 20 70 69 64 20 46 52 4f 4d 20 70 6c  LECT pid FROM pl
6d20: 69 6e 6b 20 79 22 0a 20 20 20 20 20 20 20 20 20  ink y".         
6d30: 20 20 20 20 20 20 20 20 20 22 20 57 48 45 52 45           " WHERE
6d40: 20 4e 4f 54 20 45 58 49 53 54 53 28 53 45 4c 45   NOT EXISTS(SELE
6d50: 43 54 20 31 20 46 52 4f 4d 20 70 6c 69 6e 6b 20  CT 1 FROM plink 
6d60: 78 20 57 48 45 52 45 20 78 2e 63 69 64 3d 79 2e  x WHERE x.cid=y.
6d70: 70 69 64 29 22 29 3b 0a 20 20 69 66 28 20 76 69  pid)");.  if( vi
6d80: 64 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62 5f 6c  d==0 ){.    db_l
6d90: 73 65 74 5f 69 6e 74 28 22 63 68 65 63 6b 6f 75  set_int("checkou
6da0: 74 22 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b  t", 1);.  }else{
6db0: 0a 20 20 20 20 64 62 5f 6c 73 65 74 5f 69 6e 74  .    db_lset_int
6dc0: 28 22 63 68 65 63 6b 6f 75 74 22 2c 20 76 69 64  ("checkout", vid
6dd0: 29 3b 0a 20 20 20 20 67 2e 61 72 67 76 20 3d 20  );.    g.argv = 
6de0: 61 7a 4e 65 77 41 72 67 76 3b 0a 20 20 20 20 67  azNewArgv;.    g
6df0: 2e 61 72 67 63 20 3d 20 33 3b 0a 20 20 20 20 75  .argc = 3;.    u
6e00: 70 64 61 74 65 5f 63 6d 64 28 29 3b 0a 20 20 7d  pdate_cmd();.  }
6e10: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20  .}../*.** Print 
6e20: 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 73  the value of a s
6e30: 65 74 74 69 6e 67 20 6e 61 6d 65 64 20 7a 4e 61  etting named zNa
6e40: 6d 65 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  me.*/.static voi
6e50: 64 20 70 72 69 6e 74 5f 73 65 74 74 69 6e 67 28  d print_setting(
6e60: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
6e70: 65 29 7b 0a 20 20 53 74 6d 74 20 71 3b 0a 20 20  e){.  Stmt q;.  
6e80: 69 66 28 20 67 2e 72 65 70 6f 73 69 74 6f 72 79  if( g.repository
6e90: 4f 70 65 6e 20 29 7b 0a 20 20 20 20 64 62 5f 70  Open ){.    db_p
6ea0: 72 65 70 61 72 65 28 26 71 2c 0a 20 20 20 20 20  repare(&q,.     
6eb0: 20 20 22 53 45 4c 45 43 54 20 27 28 6c 6f 63 61    "SELECT '(loca
6ec0: 6c 29 27 2c 20 76 61 6c 75 65 20 46 52 4f 4d 20  l)', value FROM 
6ed0: 63 6f 6e 66 69 67 20 57 48 45 52 45 20 6e 61 6d  config WHERE nam
6ee0: 65 3d 25 51 22 0a 20 20 20 20 20 20 20 22 20 55  e=%Q".       " U
6ef0: 4e 49 4f 4e 20 41 4c 4c 20 22 0a 20 20 20 20 20  NION ALL ".     
6f00: 20 20 22 53 45 4c 45 43 54 20 27 28 67 6c 6f 62    "SELECT '(glob
6f10: 61 6c 29 27 2c 20 76 61 6c 75 65 20 46 52 4f 4d  al)', value FROM
6f20: 20 67 6c 6f 62 61 6c 5f 63 6f 6e 66 69 67 20 57   global_config W
6f30: 48 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c 0a 20  HERE name=%Q",. 
6f40: 20 20 20 20 20 20 7a 4e 61 6d 65 2c 20 7a 4e 61        zName, zNa
6f50: 6d 65 0a 20 20 20 20 29 3b 0a 20 20 7d 65 6c 73  me.    );.  }els
6f60: 65 7b 0a 20 20 20 20 64 62 5f 70 72 65 70 61 72  e{.    db_prepar
6f70: 65 28 26 71 2c 0a 20 20 20 20 20 20 22 53 45 4c  e(&q,.      "SEL
6f80: 45 43 54 20 27 28 67 6c 6f 62 61 6c 29 27 2c 20  ECT '(global)', 
6f90: 76 61 6c 75 65 20 46 52 4f 4d 20 67 6c 6f 62 61  value FROM globa
6fa0: 6c 5f 63 6f 6e 66 69 67 20 57 48 45 52 45 20 6e  l_config WHERE n
6fb0: 61 6d 65 3d 25 51 22 2c 0a 20 20 20 20 20 20 7a  ame=%Q",.      z
6fc0: 4e 61 6d 65 0a 20 20 20 20 29 3b 0a 20 20 7d 0a  Name.    );.  }.
6fd0: 20 20 69 66 28 20 64 62 5f 73 74 65 70 28 26 71    if( db_step(&q
6fe0: 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  )==SQLITE_ROW ){
6ff0: 0a 20 20 20 20 70 72 69 6e 74 66 28 22 25 2d 32  .    printf("%-2
7000: 30 73 20 25 2d 38 73 20 25 73 5c 6e 22 2c 20 7a  0s %-8s %s\n", z
7010: 4e 61 6d 65 2c 20 64 62 5f 63 6f 6c 75 6d 6e 5f  Name, db_column_
7020: 74 65 78 74 28 26 71 2c 20 30 29 2c 0a 20 20 20  text(&q, 0),.   
7030: 20 20 20 20 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74       db_column_t
7040: 65 78 74 28 26 71 2c 20 31 29 29 3b 0a 20 20 7d  ext(&q, 1));.  }
7050: 65 6c 73 65 7b 0a 20 20 20 20 70 72 69 6e 74 66  else{.    printf
7060: 28 22 25 2d 32 30 73 5c 6e 22 2c 20 7a 4e 61 6d  ("%-20s\n", zNam
7070: 65 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e  e);.  }.  db_fin
7080: 61 6c 69 7a 65 28 26 71 29 3b 0a 7d 0a 0a 0a 2f  alize(&q);.}.../
7090: 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 73 65  *.** COMMAND: se
70a0: 74 74 69 6e 67 73 0a 2a 2a 20 43 4f 4d 4d 41 4e  ttings.** COMMAN
70b0: 44 3a 20 75 6e 73 65 74 0a 2a 2a 20 25 66 6f 73  D: unset.** %fos
70c0: 73 69 6c 20 73 65 74 74 69 6e 67 20 3f 50 52 4f  sil setting ?PRO
70d0: 50 45 52 54 59 3f 20 3f 56 41 4c 55 45 3f 20 3f  PERTY? ?VALUE? ?
70e0: 2d 67 6c 6f 62 61 6c 3f 0a 2a 2a 20 25 66 6f 73  -global?.** %fos
70f0: 73 69 6c 20 75 6e 73 65 74 20 50 52 4f 50 45 52  sil unset PROPER
7100: 54 59 20 3f 2d 67 6c 6f 62 61 6c 3f 0a 2a 2a 0a  TY ?-global?.**.
7110: 2a 2a 20 54 68 65 20 22 73 65 74 74 69 6e 67 22  ** The "setting"
7120: 20 63 6f 6d 6d 61 6e 64 20 77 69 74 68 20 6e 6f   command with no
7130: 20 61 72 67 75 6d 65 6e 74 73 20 6c 69 73 74 73   arguments lists
7140: 20 61 6c 6c 20 70 72 6f 70 65 72 74 69 65 73 20   all properties 
7150: 61 6e 64 20 74 68 65 69 72 0a 2a 2a 20 76 61 6c  and their.** val
7160: 75 65 73 2e 20 20 57 69 74 68 20 6a 75 73 74 20  ues.  With just 
7170: 61 20 70 72 6f 70 65 72 74 79 20 6e 61 6d 65 20  a property name 
7180: 69 74 20 73 68 6f 77 73 20 74 68 65 20 76 61 6c  it shows the val
7190: 75 65 20 6f 66 20 74 68 61 74 20 70 72 6f 70 65  ue of that prope
71a0: 72 74 79 2e 0a 2a 2a 20 57 69 74 68 20 61 20 76  rty..** With a v
71b0: 61 6c 75 65 20 61 72 67 75 6d 65 6e 74 20 69 74  alue argument it
71c0: 20 63 68 61 6e 67 65 73 20 74 68 65 20 70 72 6f   changes the pro
71d0: 70 65 72 74 79 20 66 6f 72 20 74 68 65 20 63 75  perty for the cu
71e0: 72 72 65 6e 74 20 72 65 70 6f 73 69 74 6f 72 79  rrent repository
71f0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 75 6e 73  ..**.** The "uns
7200: 65 74 22 20 63 6f 6d 6d 61 6e 64 20 63 6c 65 61  et" command clea
7210: 72 73 20 61 20 70 72 6f 70 65 72 74 79 20 73 65  rs a property se
7220: 74 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  tting..**.**    
7230: 61 75 74 6f 73 79 6e 63 20 20 20 20 20 20 20 20  autosync        
7240: 20 49 66 20 65 6e 61 62 6c 65 64 2c 20 61 75 74   If enabled, aut
7250: 6f 6d 61 74 69 63 61 6c 6c 79 20 70 75 6c 6c 20  omatically pull 
7260: 70 72 69 6f 72 20 74 6f 0a 2a 2a 20 20 20 20 20  prior to.**     
7270: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7280: 63 6f 6d 6d 69 74 20 6f 72 20 75 70 64 61 74 65  commit or update
7290: 20 61 6e 64 20 61 75 74 6f 6d 61 74 69 63 61 6c   and automatical
72a0: 6c 79 20 70 75 73 68 0a 2a 2a 20 20 20 20 20 20  ly push.**      
72b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
72c0: 66 74 65 72 20 63 6f 6d 6d 69 74 20 6f 72 20 74  fter commit or t
72d0: 61 67 20 6f 72 20 62 72 61 6e 63 68 20 63 72 65  ag or branch cre
72e0: 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  ation..**.**    
72f0: 70 67 70 2d 63 6f 6d 6d 61 6e 64 20 20 20 20 20  pgp-command     
7300: 20 43 6f 6d 6d 61 6e 64 20 75 73 65 64 20 74 6f   Command used to
7310: 20 63 6c 65 61 72 2d 73 69 67 6e 20 6d 61 6e 69   clear-sign mani
7320: 66 65 73 74 73 20 61 74 20 63 68 65 63 6b 2d 69  fests at check-i
7330: 6e 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  n..**           
7340: 20 20 20 20 20 20 20 20 20 20 54 68 65 20 64 65            The de
7350: 66 61 75 6c 74 20 69 73 20 22 67 70 67 20 2d 2d  fault is "gpg --
7360: 63 6c 65 61 72 73 69 67 6e 20 2d 6f 20 22 2e 0a  clearsign -o "..
7370: 2a 2a 0a 2a 2a 20 20 20 20 65 64 69 74 6f 72 20  **.**    editor 
7380: 20 20 20 20 20 20 20 20 20 20 54 65 78 74 20 65            Text e
7390: 64 69 74 6f 72 20 63 6f 6d 6d 61 6e 64 20 75 73  ditor command us
73a0: 65 64 20 66 6f 72 20 63 68 65 63 6b 2d 69 6e 20  ed for check-in 
73b0: 63 6f 6d 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20  comments..**.** 
73c0: 20 20 20 6c 6f 63 61 6c 61 75 74 68 20 20 20 20     localauth    
73d0: 20 20 20 20 49 66 20 65 6e 61 62 6c 65 64 2c 20      If enabled, 
73e0: 72 65 71 75 69 72 65 20 74 68 61 74 20 48 54 54  require that HTT
73f0: 50 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 72  P connections fr
7400: 6f 6d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  om.**           
7410: 20 20 20 20 20 20 20 20 20 20 31 32 37 2e 30 2e            127.0.
7420: 30 2e 31 20 62 65 20 61 75 74 68 65 6e 74 69 63  0.1 be authentic
7430: 61 74 65 64 20 62 79 20 70 61 73 73 77 6f 72 64  ated by password
7440: 2e 20 20 49 66 0a 2a 2a 20 20 20 20 20 20 20 20  .  If.**        
7450: 20 20 20 20 20 20 20 20 20 20 20 20 20 66 61 6c               fal
7460: 73 65 2c 20 61 6c 6c 20 48 54 54 50 20 72 65 71  se, all HTTP req
7470: 75 65 73 74 73 20 66 72 6f 6d 20 6c 6f 63 61 6c  uests from local
7480: 68 6f 73 74 20 68 61 76 65 0a 2a 2a 20 20 20 20  host have.**    
7490: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
74a0: 20 75 6e 72 65 73 74 72 69 63 74 65 64 20 61 63   unrestricted ac
74b0: 63 65 73 73 20 74 6f 20 74 68 65 20 72 65 70 6f  cess to the repo
74c0: 73 69 74 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 20 20  sitory..**.**   
74d0: 20 6f 6d 69 74 73 69 67 6e 20 20 20 20 20 20 20   omitsign       
74e0: 20 20 57 68 65 6e 20 65 6e 61 62 6c 65 64 2c 20    When enabled, 
74f0: 66 6f 73 73 69 6c 20 77 69 6c 6c 20 6e 6f 74 20  fossil will not 
7500: 61 74 74 65 6d 70 74 20 74 6f 20 73 69 67 6e 20  attempt to sign 
7510: 61 6e 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  any.**          
7520: 20 20 20 20 20 20 20 20 20 20 20 63 6f 6d 6d 69             commi
7530: 74 20 77 69 74 68 20 67 70 67 2e 20 41 6c 6c 20  t with gpg. All 
7540: 63 6f 6d 6d 69 74 73 20 77 69 6c 6c 20 62 65 20  commits will be 
7550: 75 6e 73 69 67 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  unsigned..**.** 
7560: 20 20 20 70 72 6f 78 79 20 20 20 20 20 20 20 20     proxy        
7570: 20 20 20 20 55 52 4c 20 6f 66 20 74 68 65 20 48      URL of the H
7580: 54 54 50 20 70 72 6f 78 79 2e 20 20 49 66 20 75  TTP proxy.  If u
7590: 6e 64 65 66 69 6e 65 64 20 6f 72 20 22 6f 66 66  ndefined or "off
75a0: 22 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20  " then.**       
75b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68                th
75c0: 65 20 22 68 74 74 70 5f 70 72 6f 78 79 22 20 65  e "http_proxy" e
75d0: 6e 76 69 72 6f 6e 6d 65 6e 74 20 76 61 72 69 61  nvironment varia
75e0: 62 6c 65 20 69 73 20 63 6f 6e 73 75 6c 74 65 64  ble is consulted
75f0: 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  ..**            
7600: 20 20 20 20 20 20 20 20 20 49 66 20 74 68 65 20           If the 
7610: 68 74 74 70 5f 70 72 6f 78 79 20 65 6e 76 69 72  http_proxy envir
7620: 6f 6e 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 20  onment variable 
7630: 69 73 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20  is undefined.** 
7640: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7650: 20 20 20 20 74 68 65 6e 20 61 20 64 69 72 65 63      then a direc
7660: 74 20 48 54 54 50 20 63 6f 6e 6e 65 63 74 69 6f  t HTTP connectio
7670: 6e 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a  n is used..**.**
7680: 20 20 20 20 64 69 66 66 2d 63 6f 6d 6d 61 6e 64      diff-command
7690: 20 20 20 20 20 45 78 74 65 72 6e 61 6c 20 63 6f       External co
76a0: 6d 6d 61 6e 64 20 74 6f 20 72 75 6e 20 77 68 65  mmand to run whe
76b0: 6e 20 70 65 72 66 6f 72 6d 69 6e 67 20 61 20 64  n performing a d
76c0: 69 66 66 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20  iff..**         
76d0: 20 20 20 20 20 20 20 20 20 20 20 20 49 66 20 75              If u
76e0: 6e 64 65 66 69 6e 65 64 2c 20 74 68 65 20 69 6e  ndefined, the in
76f0: 74 65 72 6e 61 6c 20 74 65 78 74 20 64 69 66 66  ternal text diff
7700: 20 77 69 6c 6c 20 62 65 20 75 73 65 64 2e 0a 2a   will be used..*
7710: 2a 0a 2a 2a 20 20 20 20 67 64 69 66 66 2d 63 6f  *.**    gdiff-co
7720: 6d 6d 61 6e 64 20 20 20 20 45 78 74 65 72 6e 61  mmand    Externa
7730: 6c 20 63 6f 6d 6d 61 6e 64 20 74 6f 20 72 75 6e  l command to run
7740: 20 77 68 65 6e 20 70 65 72 66 6f 72 6d 69 6e 67   when performing
7750: 20 61 20 67 72 61 70 68 69 63 61 6c 0a 2a 2a 20   a graphical.** 
7760: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7770: 20 20 20 20 64 69 66 66 2e 20 49 66 20 75 6e 64      diff. If und
7780: 65 66 69 6e 65 64 2c 20 74 65 78 74 20 64 69 66  efined, text dif
7790: 66 20 77 69 6c 6c 20 62 65 20 75 73 65 64 2e 0a  f will be used..
77a0: 2a 2f 0a 76 6f 69 64 20 73 65 74 74 69 6e 67 5f  */.void setting_
77b0: 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 73 74 61  cmd(void){.  sta
77c0: 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
77d0: 61 7a 4e 61 6d 65 5b 5d 20 3d 20 7b 0a 20 20 20  azName[] = {.   
77e0: 20 22 61 75 74 6f 73 79 6e 63 22 2c 0a 20 20 20   "autosync",.   
77f0: 20 22 64 69 66 66 2d 63 6f 6d 6d 61 6e 64 22 2c   "diff-command",
7800: 0a 20 20 20 20 22 65 64 69 74 6f 72 22 2c 0a 20  .    "editor",. 
7810: 20 20 20 22 67 64 69 66 66 2d 63 6f 6d 6d 61 6e     "gdiff-comman
7820: 64 22 2c 0a 20 20 20 20 22 6c 6f 63 61 6c 61 75  d",.    "localau
7830: 74 68 22 2c 0a 20 20 20 20 22 6f 6d 69 74 73 69  th",.    "omitsi
7840: 67 6e 22 2c 0a 20 20 20 20 22 70 67 70 2d 63 6f  gn",.    "pgp-co
7850: 6d 6d 61 6e 64 22 2c 0a 20 20 20 20 22 70 72 6f  mmand",.    "pro
7860: 78 79 22 2c 0a 20 20 7d 3b 0a 20 20 69 6e 74 20  xy",.  };.  int 
7870: 69 3b 0a 20 20 69 6e 74 20 67 6c 6f 62 61 6c 46  i;.  int globalF
7880: 6c 61 67 20 3d 20 66 69 6e 64 5f 6f 70 74 69 6f  lag = find_optio
7890: 6e 28 22 67 6c 6f 62 61 6c 22 2c 22 67 22 2c 30  n("global","g",0
78a0: 29 21 3d 30 3b 0a 20 20 69 6e 74 20 75 6e 73 65  )!=0;.  int unse
78b0: 74 46 6c 61 67 20 3d 20 67 2e 61 72 67 76 5b 31  tFlag = g.argv[1
78c0: 5d 5b 30 5d 3d 3d 27 75 27 3b 0a 20 20 64 62 5f  ][0]=='u';.  db_
78d0: 66 69 6e 64 5f 61 6e 64 5f 6f 70 65 6e 5f 72 65  find_and_open_re
78e0: 70 6f 73 69 74 6f 72 79 28 30 29 3b 0a 20 20 69  pository(0);.  i
78f0: 66 28 20 21 67 2e 72 65 70 6f 73 69 74 6f 72 79  f( !g.repository
7900: 4f 70 65 6e 20 29 7b 0a 20 20 20 20 64 62 5f 6f  Open ){.    db_o
7910: 70 65 6e 5f 63 6f 6e 66 69 67 28 29 3b 0a 20 20  pen_config();.  
7920: 20 20 67 6c 6f 62 61 6c 46 6c 61 67 20 3d 20 31    globalFlag = 1
7930: 3b 0a 20 20 7d 0a 20 20 69 66 28 20 75 6e 73 65  ;.  }.  if( unse
7940: 74 46 6c 61 67 20 26 26 20 67 2e 61 72 67 63 21  tFlag && g.argc!
7950: 3d 33 20 29 7b 0a 20 20 20 20 75 73 61 67 65 28  =3 ){.    usage(
7960: 22 50 52 4f 50 45 52 54 59 20 3f 2d 67 6c 6f 62  "PROPERTY ?-glob
7970: 61 6c 3f 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28  al?");.  }.  if(
7980: 20 67 2e 61 72 67 63 3d 3d 32 20 29 7b 0a 20 20   g.argc==2 ){.  
7990: 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a    for(i=0; i<siz
79a0: 65 6f 66 28 61 7a 4e 61 6d 65 29 2f 73 69 7a 65  eof(azName)/size
79b0: 6f 66 28 61 7a 4e 61 6d 65 5b 30 5d 29 3b 20 69  of(azName[0]); i
79c0: 2b 2b 29 7b 0a 20 20 20 20 20 20 70 72 69 6e 74  ++){.      print
79d0: 5f 73 65 74 74 69 6e 67 28 61 7a 4e 61 6d 65 5b  _setting(azName[
79e0: 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  i]);.    }.  }el
79f0: 73 65 20 69 66 28 20 67 2e 61 72 67 63 3d 3d 33  se if( g.argc==3
7a00: 20 7c 7c 20 67 2e 61 72 67 63 3d 3d 34 20 29 7b   || g.argc==4 ){
7a10: 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
7a20: 2a 7a 4e 61 6d 65 20 3d 20 67 2e 61 72 67 76 5b  *zName = g.argv[
7a30: 32 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20  2];.    int n = 
7a40: 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 3b 0a 20  strlen(zName);. 
7a50: 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69     for(i=0; i<si
7a60: 7a 65 6f 66 28 61 7a 4e 61 6d 65 29 2f 73 69 7a  zeof(azName)/siz
7a70: 65 6f 66 28 61 7a 4e 61 6d 65 5b 30 5d 29 3b 20  eof(azName[0]); 
7a80: 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
7a90: 73 74 72 6e 63 6d 70 28 61 7a 4e 61 6d 65 5b 69  strncmp(azName[i
7aa0: 5d 2c 20 7a 4e 61 6d 65 2c 20 6e 29 3d 3d 30 20  ], zName, n)==0 
7ab0: 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20  ) break;.    }. 
7ac0: 20 20 20 69 66 28 20 69 3e 3d 73 69 7a 65 6f 66     if( i>=sizeof
7ad0: 28 61 7a 4e 61 6d 65 29 2f 73 69 7a 65 6f 66 28  (azName)/sizeof(
7ae0: 61 7a 4e 61 6d 65 5b 30 5d 29 20 29 7b 0a 20 20  azName[0]) ){.  
7af0: 20 20 20 20 66 6f 73 73 69 6c 5f 66 61 74 61 6c      fossil_fatal
7b00: 28 22 6e 6f 20 73 75 63 68 20 73 65 74 74 69 6e  ("no such settin
7b10: 67 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a  g: %s", zName);.
7b20: 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75 6e      }.    if( un
7b30: 73 65 74 46 6c 61 67 20 29 7b 0a 20 20 20 20 20  setFlag ){.     
7b40: 20 64 62 5f 75 6e 73 65 74 28 61 7a 4e 61 6d 65   db_unset(azName
7b50: 5b 69 5d 2c 20 67 6c 6f 62 61 6c 46 6c 61 67 29  [i], globalFlag)
7b60: 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
7b70: 67 2e 61 72 67 63 3d 3d 34 20 29 7b 0a 20 20 20  g.argc==4 ){.   
7b80: 20 20 20 64 62 5f 73 65 74 28 61 7a 4e 61 6d 65     db_set(azName
7b90: 5b 69 5d 2c 20 67 2e 61 72 67 76 5b 33 5d 2c 20  [i], g.argv[3], 
7ba0: 67 6c 6f 62 61 6c 46 6c 61 67 29 3b 0a 20 20 20  globalFlag);.   
7bb0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 72   }else{.      pr
7bc0: 69 6e 74 5f 73 65 74 74 69 6e 67 28 61 7a 4e 61  int_setting(azNa
7bd0: 6d 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20  me[i]);.    }.  
7be0: 7d 65 6c 73 65 7b 0a 20 20 20 20 75 73 61 67 65  }else{.    usage
7bf0: 28 22 3f 50 52 4f 50 45 52 54 59 3f 20 3f 56 41  ("?PROPERTY? ?VA
7c00: 4c 55 45 3f 22 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  LUE?");.  }.}../
7c10: 2a 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  *.** SQL functio
7c20: 6e 20 74 6f 20 72 65 6e 64 65 72 20 61 20 55 55  n to render a UU
7c30: 49 44 20 61 73 20 61 20 68 79 70 65 72 6c 69 6e  ID as a hyperlin
7c40: 6b 20 74 6f 20 61 20 70 61 67 65 20 64 65 73 63  k to a page desc
7c50: 72 69 62 69 6e 67 0a 2a 2a 20 74 68 61 74 20 55  ribing.** that U
7c60: 55 49 44 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  UID..*/.static v
7c70: 6f 69 64 20 68 79 70 65 72 6c 69 6e 6b 55 75 69  oid hyperlinkUui
7c80: 64 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  dFunc(.  sqlite3
7c90: 5f 63 6f 6e 74 65 78 74 20 2a 70 43 78 74 2c 20  _context *pCxt, 
7ca0: 20 20 20 20 2f 2a 20 66 75 6e 63 74 69 6f 6e 20      /* function 
7cb0: 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74  context */.  int
7cc0: 20 61 72 67 63 2c 20 20 20 20 20 20 20 20 20 20   argc,          
7cd0: 20 20 20 20 20 20 20 20 2f 2a 20 6e 75 6d 62 65          /* numbe
7ce0: 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74  r of arguments t
7cf0: 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a  o the function *
7d00: 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  /.  sqlite3_valu
7d10: 65 20 2a 2a 61 72 67 76 20 20 20 20 20 20 20 2f  e **argv       /
7d20: 2a 20 76 61 6c 75 65 73 20 6f 66 20 61 6c 6c 20  * values of all 
7d30: 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e  function argumen
7d40: 74 73 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74  ts */.){.  const
7d50: 20 63 68 61 72 20 2a 7a 55 75 69 64 3b 20 20 20   char *zUuid;   
7d60: 20 20 20 20 20 20 2f 2a 20 54 68 65 20 55 55 49        /* The UUI
7d70: 44 20 74 6f 20 72 65 6e 64 65 72 20 2a 2f 0a 20  D to render */. 
7d80: 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20   char *z;       
7d90: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
7da0: 65 6e 64 65 72 65 64 20 48 54 4d 4c 20 74 65 78  endered HTML tex
7db0: 74 20 2a 2f 0a 0a 20 20 7a 55 75 69 64 20 3d 20  t */..  zUuid = 
7dc0: 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c  (const char*)sql
7dd0: 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
7de0: 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20  argv[0]);.  if( 
7df0: 67 2e 6f 6b 48 69 73 74 6f 72 79 20 26 26 20 7a  g.okHistory && z
7e00: 55 75 69 64 20 26 26 20 73 74 72 6c 65 6e 28 7a  Uuid && strlen(z
7e10: 55 75 69 64 29 3e 3d 31 30 20 29 7b 0a 20 20 20  Uuid)>=10 ){.   
7e20: 20 7a 20 3d 20 6d 70 72 69 6e 74 66 28 22 3c 74   z = mprintf("<t
7e30: 74 3e 3c 61 20 68 72 65 66 3d 27 25 73 2f 76 69  t><a href='%s/vi
7e40: 6e 66 6f 2f 25 74 27 3e 3c 73 70 61 6e 20 73 74  nfo/%t'><span st
7e50: 79 6c 65 3d 27 66 6f 6e 74 2d 73 69 7a 65 3a 31  yle='font-size:1
7e60: 2e 35 65 6d 27 3e 22 0a 20 20 20 20 20 20 20 20  .5em'>".        
7e70: 20 20 20 20 20 20 20 20 22 25 23 68 3c 2f 73 70          "%#h</sp
7e80: 61 6e 3e 25 68 3c 2f 61 3e 3c 2f 74 74 3e 22 2c  an>%h</a></tt>",
7e90: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
7ea0: 20 67 2e 7a 42 61 73 65 55 52 4c 2c 20 7a 55 75   g.zBaseURL, zUu
7eb0: 69 64 2c 20 31 30 2c 20 7a 55 75 69 64 2c 20 26  id, 10, zUuid, &
7ec0: 7a 55 75 69 64 5b 31 30 5d 29 3b 0a 20 20 20 20  zUuid[10]);.    
7ed0: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
7ee0: 65 78 74 28 70 43 78 74 2c 20 7a 2c 20 2d 31 2c  ext(pCxt, z, -1,
7ef0: 20 66 72 65 65 29 3b 0a 20 20 7d 65 6c 73 65 7b   free);.  }else{
7f00: 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
7f10: 75 6c 74 5f 74 65 78 74 28 70 43 78 74 2c 20 7a  ult_text(pCxt, z
7f20: 55 75 69 64 2c 20 2d 31 2c 20 53 51 4c 49 54 45  Uuid, -1, SQLITE
7f30: 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d  _TRANSIENT);.  }
7f40: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 20 66 75  .}../*.** SQL fu
7f50: 6e 63 74 69 6f 6e 20 74 6f 20 72 65 6e 64 65 72  nction to render
7f60: 20 61 20 54 41 47 49 44 20 61 73 20 61 20 68 79   a TAGID as a hy
7f70: 70 65 72 6c 69 6e 6b 20 74 6f 20 61 20 70 61 67  perlink to a pag
7f80: 65 20 64 65 73 63 72 69 62 69 6e 67 0a 2a 2a 20  e describing.** 
7f90: 74 68 61 74 20 74 61 67 2e 0a 2a 2f 0a 73 74 61  that tag..*/.sta
7fa0: 74 69 63 20 76 6f 69 64 20 68 79 70 65 72 6c 69  tic void hyperli
7fb0: 6e 6b 54 61 67 69 64 46 75 6e 63 28 0a 20 20 73  nkTagidFunc(.  s
7fc0: 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
7fd0: 70 43 78 74 2c 20 20 20 20 20 2f 2a 20 66 75 6e  pCxt,     /* fun
7fe0: 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f  ction context */
7ff0: 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 20 20 20  .  int argc,    
8000: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
8010: 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   number of argum
8020: 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75 6e 63  ents to the func
8030: 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  tion */.  sqlite
8040: 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 20 20  3_value **argv  
8050: 20 20 20 20 20 2f 2a 20 76 61 6c 75 65 73 20 6f       /* values o
8060: 66 20 61 6c 6c 20 66 75 6e 63 74 69 6f 6e 20 61  f all function a
8070: 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 29 7b 0a 20  rguments */.){. 
8080: 20 69 6e 74 20 74 61 67 69 64 3b 20 20 20 20 20   int tagid;     
8090: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
80a0: 68 65 20 74 61 67 69 64 20 74 6f 20 72 65 6e 64  he tagid to rend
80b0: 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b  er */.  char *z;
80c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
80d0: 20 20 20 2f 2a 20 72 65 6e 64 65 72 65 64 20 68     /* rendered h
80e0: 74 6d 6c 20 74 65 78 74 20 2a 2f 0a 0a 20 20 74  tml text */..  t
80f0: 61 67 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 76  agid = sqlite3_v
8100: 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 30 5d  alue_int(argv[0]
8110: 29 3b 0a 20 20 69 66 28 20 67 2e 6f 6b 48 69 73  );.  if( g.okHis
8120: 74 6f 72 79 20 29 7b 0a 20 20 20 20 7a 20 3d 20  tory ){.    z = 
8130: 6d 70 72 69 6e 74 66 28 22 3c 61 20 68 72 65 66  mprintf("<a href
8140: 3d 27 25 73 2f 74 61 67 76 69 65 77 3f 74 61 67  ='%s/tagview?tag
8150: 69 64 3d 25 64 27 3e 25 64 3c 2f 61 3e 22 2c 20  id=%d'>%d</a>", 
8160: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
8170: 20 20 20 67 2e 7a 42 61 73 65 55 52 4c 2c 20 74     g.zBaseURL, t
8180: 61 67 69 64 2c 20 74 61 67 69 64 29 3b 0a 20 20  agid, tagid);.  
8190: 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 6d  }else{.    z = m
81a0: 70 72 69 6e 74 66 28 22 25 64 22 2c 20 74 61 67  printf("%d", tag
81b0: 69 64 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  id);.  }.  sqlit
81c0: 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70  e3_result_text(p
81d0: 43 78 74 2c 20 7a 2c 20 2d 31 2c 20 66 72 65 65  Cxt, z, -1, free
81e0: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 20  );.}../*.** SQL 
81f0: 66 75 6e 63 74 69 6f 6e 20 74 6f 20 72 65 6e 64  function to rend
8200: 65 72 20 61 20 54 41 47 4e 41 4d 45 20 61 73 20  er a TAGNAME as 
8210: 61 20 68 79 70 65 72 6c 69 6e 6b 20 74 6f 20 61  a hyperlink to a
8220: 20 70 61 67 65 20 64 65 73 63 72 69 62 69 6e 67   page describing
8230: 0a 2a 2a 20 74 68 61 74 20 74 61 67 2e 0a 2a 2f  .** that tag..*/
8240: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 79 70  .static void hyp
8250: 65 72 6c 69 6e 6b 54 61 67 6e 61 6d 65 46 75 6e  erlinkTagnameFun
8260: 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
8270: 74 65 78 74 20 2a 70 43 78 74 2c 20 20 20 20 20  text *pCxt,     
8280: 2f 2a 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74  /* function cont
8290: 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 61 72 67  ext */.  int arg
82a0: 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c,              
82b0: 20 20 20 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66      /* number of
82c0: 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68   arguments to th
82d0: 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  e function */.  
82e0: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
82f0: 61 72 67 76 20 20 20 20 20 20 20 2f 2a 20 76 61  argv       /* va
8300: 6c 75 65 73 20 6f 66 20 61 6c 6c 20 66 75 6e 63  lues of all func
8310: 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 2a  tion arguments *
8320: 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61  /.){.  const cha
8330: 72 20 2a 7a 54 61 67 3b 20 20 20 20 20 20 20 20  r *zTag;        
8340: 20 20 2f 2a 20 54 68 65 20 74 61 67 20 74 6f 20    /* The tag to 
8350: 72 65 6e 64 65 72 20 2a 2f 0a 20 20 63 68 61 72  render */.  char
8360: 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20   *z;            
8370: 20 20 20 20 20 20 20 2f 2a 20 72 65 6e 64 65 72         /* render
8380: 65 64 20 68 74 6d 6c 20 74 65 78 74 20 2a 2f 0a  ed html text */.
8390: 0a 20 20 7a 54 61 67 20 3d 20 28 63 6f 6e 73 74  .  zTag = (const
83a0: 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76   char*)sqlite3_v
83b0: 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
83c0: 5d 29 3b 0a 20 20 69 66 28 20 67 2e 6f 6b 48 69  ]);.  if( g.okHi
83d0: 73 74 6f 72 79 20 29 7b 0a 20 20 20 20 7a 20 3d  story ){.    z =
83e0: 20 6d 70 72 69 6e 74 66 28 22 3c 61 20 68 72 65   mprintf("<a hre
83f0: 66 3d 27 25 73 2f 74 61 67 76 69 65 77 3f 6e 61  f='%s/tagview?na
8400: 6d 65 3d 25 54 27 3e 25 68 3c 2f 61 3e 22 2c 20  me=%T'>%h</a>", 
8410: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
8420: 20 20 20 67 2e 7a 42 61 73 65 55 52 4c 2c 20 7a     g.zBaseURL, z
8430: 54 61 67 2c 20 7a 54 61 67 29 3b 0a 20 20 7d 65  Tag, zTag);.  }e
8440: 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 6d 70 72  lse{.    z = mpr
8450: 69 6e 74 66 28 22 25 68 22 2c 20 7a 54 61 67 29  intf("%h", zTag)
8460: 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
8470: 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43 78 74  result_text(pCxt
8480: 2c 20 7a 2c 20 2d 31 2c 20 66 72 65 65 29 3b 0a  , z, -1, free);.
8490: 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 20 66 75 6e  }../*.** SQL fun
84a0: 63 74 69 6f 6e 20 74 6f 20 65 73 63 61 70 65 20  ction to escape 
84b0: 61 6c 6c 20 63 68 61 72 61 63 74 65 72 73 20 69  all characters i
84c0: 6e 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20  n a string that 
84d0: 68 61 76 65 20 73 70 65 63 69 61 6c 0a 2a 2a 20  have special.** 
84e0: 6d 65 61 6e 69 6e 67 20 74 6f 20 48 54 4d 4c 2e  meaning to HTML.
84f0: 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
8500: 68 74 6d 6c 69 7a 65 46 75 6e 63 28 0a 20 20 73  htmlizeFunc(.  s
8510: 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
8520: 70 43 78 74 2c 20 20 20 20 20 2f 2a 20 66 75 6e  pCxt,     /* fun
8530: 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f  ction context */
8540: 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 20 20 20  .  int argc,    
8550: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
8560: 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   number of argum
8570: 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75 6e 63  ents to the func
8580: 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  tion */.  sqlite
8590: 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 20 20  3_value **argv  
85a0: 20 20 20 20 20 2f 2a 20 76 61 6c 75 65 73 20 6f       /* values o
85b0: 66 20 61 6c 6c 20 66 75 6e 63 74 69 6f 6e 20 61  f all function a
85c0: 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 29 7b 0a 20  rguments */.){. 
85d0: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 65   const char *zTe
85e0: 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  xt;         /* T
85f0: 65 78 74 20 74 6f 20 62 65 20 68 74 6d 6c 69 7a  ext to be htmliz
8600: 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b  ed */.  char *z;
8610: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8620: 20 20 20 2f 2a 20 72 65 6e 64 65 72 65 64 20 68     /* rendered h
8630: 74 6d 6c 20 74 65 78 74 20 2a 2f 0a 0a 20 20 7a  tml text */..  z
8640: 54 65 78 74 20 3d 20 28 63 6f 6e 73 74 20 63 68  Text = (const ch
8650: 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar*)sqlite3_valu
8660: 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
8670: 0a 20 20 7a 20 3d 20 68 74 6d 6c 69 7a 65 28 7a  .  z = htmlize(z
8680: 54 65 78 74 2c 20 2d 31 29 3b 0a 20 20 73 71 6c  Text, -1);.  sql
8690: 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
86a0: 28 70 43 78 74 2c 20 7a 2c 20 2d 31 2c 20 66 72  (pCxt, z, -1, fr
86b0: 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  ee);.}../*.** Th
86c0: 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
86d0: 68 65 6c 70 65 72 20 74 6f 20 72 75 6e 20 61 6e  helper to run an
86e0: 20 53 51 4c 20 71 75 65 72 79 20 61 6e 64 20 74   SQL query and t
86f0: 61 62 6c 65 2d 69 7a 65 20 74 68 65 0a 2a 2a 20  able-ize the.** 
8700: 72 65 73 75 6c 74 73 2e 0a 2a 2a 0a 2a 2a 20 54  results..**.** T
8710: 68 65 20 7a 53 71 6c 20 70 61 72 61 6d 65 74 65  he zSql paramete
8720: 72 20 73 68 6f 75 6c 64 20 62 65 20 61 20 73 69  r should be a si
8730: 6e 67 6c 65 2c 20 63 6f 6d 70 6c 65 74 65 20 53  ngle, complete S
8740: 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  QL statement..**
8750: 20 54 61 62 6c 65 69 7a 65 64 20 6f 75 74 70 75   Tableized outpu
8760: 74 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61  t of the SQL sta
8770: 74 65 6d 65 6e 74 20 69 73 20 72 65 6e 64 65 72  tement is render
8780: 65 64 20 62 61 63 6b 20 74 6f 20 74 68 65 20 63  ed back to the c
8790: 6c 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  lient..**.** The
87a0: 20 69 73 53 61 66 65 20 66 6c 61 67 20 69 73 20   isSafe flag is 
87b0: 74 72 75 65 20 69 66 20 61 6c 6c 20 71 75 65 72  true if all quer
87c0: 79 20 72 65 73 75 6c 74 73 20 68 61 76 65 20 62  y results have b
87d0: 65 65 6e 20 70 72 6f 63 65 73 73 65 64 20 0a 2a  een processed .*
87e0: 2a 20 62 79 20 72 6f 75 74 69 6e 65 73 20 73 75  * by routines su
87f0: 63 68 20 61 73 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ch as.**.**     
8800: 20 20 20 6c 69 6e 6b 75 75 69 64 28 29 0a 2a 2a     linkuuid().**
8810: 20 20 20 20 20 20 20 20 6c 69 6e 6b 74 61 67 69          linktagi
8820: 64 28 29 0a 2a 2a 20 20 20 20 20 20 20 20 6c 69  d().**        li
8830: 6e 6b 74 61 67 6e 61 6d 65 28 29 0a 2a 2a 20 20  nktagname().**  
8840: 20 20 20 20 20 20 68 74 6d 6c 69 7a 65 28 29 0a        htmlize().
8850: 2a 2a 0a 2a 2a 20 61 6e 64 20 61 72 65 20 74 68  **.** and are th
8860: 65 72 65 66 6f 72 65 20 73 61 66 65 20 66 6f 72  erefore safe for
8870: 20 64 69 72 65 63 74 20 72 65 6e 64 65 72 69 6e   direct renderin
8880: 67 2e 20 20 49 66 20 69 73 53 61 66 65 20 69 73  g.  If isSafe is
8890: 20 66 61 6c 73 65 2c 0a 2a 2a 20 74 68 65 6e 20   false,.** then 
88a0: 61 6c 6c 20 63 68 61 72 61 63 74 65 72 73 20 69  all characters i
88b0: 6e 20 74 68 65 20 71 75 65 72 79 20 72 65 73 75  n the query resu
88c0: 6c 74 20 74 68 61 74 20 68 61 76 65 20 73 70 65  lt that have spe
88d0: 63 69 61 6c 20 6d 65 61 6e 69 6e 67 0a 2a 2a 20  cial meaning.** 
88e0: 74 6f 20 48 54 4d 4c 20 61 72 65 20 65 73 63 61  to HTML are esca
88f0: 70 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ped..**.** Retur
8900: 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20  ns SQLITE_OK on 
8910: 73 75 63 63 65 73 73 20 61 6e 64 20 61 6e 79 20  success and any 
8920: 6f 74 68 65 72 20 76 61 6c 75 65 20 6f 6e 20 65  other value on e
8930: 72 72 6f 72 2e 0a 2a 2f 0a 69 6e 74 20 64 62 5f  rror..*/.int db_
8940: 67 65 6e 65 72 69 63 5f 71 75 65 72 79 5f 76 69  generic_query_vi
8950: 65 77 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  ew(const char *z
8960: 53 71 6c 2c 20 69 6e 74 20 69 73 53 61 66 65 29  Sql, int isSafe)
8970: 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  {.  sqlite3_stmt
8980: 20 2a 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 72   *pStmt;.  int r
8990: 63 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 2c 20 69  c;.  int nCol, i
89a0: 3b 0a 20 20 69 6e 74 20 6e 52 6f 77 3b 0a 20 20  ;.  int nRow;.  
89b0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 6f 77  const char *zRow
89c0: 3b 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 6f  ;.  static int o
89d0: 6e 63 65 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 49  nce = 1;..  /* I
89e0: 6e 73 74 61 6c 6c 20 74 68 65 20 73 70 65 63 69  nstall the speci
89f0: 61 6c 20 66 75 6e 63 74 69 6f 6e 73 20 6f 6e 20  al functions on 
8a00: 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 74  the first call t
8a10: 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 2a  o this routine *
8a20: 2f 0a 20 20 69 66 28 20 6f 6e 63 65 20 29 7b 0a  /.  if( once ){.
8a30: 20 20 20 20 6f 6e 63 65 20 3d 20 30 3b 0a 20 20      once = 0;.  
8a40: 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8a50: 5f 66 75 6e 63 74 69 6f 6e 28 67 2e 64 62 2c 20  _function(g.db, 
8a60: 22 6c 69 6e 6b 75 75 69 64 22 2c 20 31 2c 20 53  "linkuuid", 1, S
8a70: 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 0a  QLITE_UTF8, 0, .
8a80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8a90: 20 20 20 20 20 20 20 20 20 20 20 20 68 79 70 65              hype
8aa0: 72 6c 69 6e 6b 55 75 69 64 46 75 6e 63 2c 20 30  rlinkUuidFunc, 0
8ab0: 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
8ac0: 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
8ad0: 6e 28 67 2e 64 62 2c 20 22 6c 69 6e 6b 74 61 67  n(g.db, "linktag
8ae0: 69 64 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 55  id", 1, SQLITE_U
8af0: 54 46 38 2c 20 30 2c 20 0a 20 20 20 20 20 20 20  TF8, 0, .       
8b00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8b10: 20 20 20 20 20 68 79 70 65 72 6c 69 6e 6b 54 61       hyperlinkTa
8b20: 67 69 64 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a  gidFunc, 0, 0);.
8b30: 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
8b40: 74 65 5f 66 75 6e 63 74 69 6f 6e 28 67 2e 64 62  te_function(g.db
8b50: 2c 20 22 6c 69 6e 6b 74 61 67 6e 61 6d 65 22 2c  , "linktagname",
8b60: 20 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   1, SQLITE_UTF8,
8b70: 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20   0, .           
8b80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8b90: 20 68 79 70 65 72 6c 69 6e 6b 54 61 67 6e 61 6d   hyperlinkTagnam
8ba0: 65 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 20 20  eFunc, 0, 0);.  
8bb0: 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8bc0: 5f 66 75 6e 63 74 69 6f 6e 28 67 2e 64 62 2c 20  _function(g.db, 
8bd0: 22 68 74 6d 6c 69 7a 65 22 2c 20 31 2c 20 53 51  "htmlize", 1, SQ
8be0: 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 0a 20  LITE_UTF8, 0, . 
8bf0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8c00: 20 20 20 20 20 20 20 20 20 20 20 68 74 6d 6c 69             htmli
8c10: 7a 65 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 20  zeFunc, 0, 0);. 
8c20: 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 55 73   }..  /*.  ** Us
8c30: 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 64  e sqlite3_stmt d
8c40: 69 72 65 63 74 6c 79 20 72 61 74 68 65 72 20 74  irectly rather t
8c50: 68 61 6e 20 67 6f 69 6e 67 20 74 68 72 6f 75 67  han going throug
8c60: 68 20 64 62 5f 70 72 65 70 61 72 65 28 29 2c 0a  h db_prepare(),.
8c70: 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 77 65 20    ** so that we 
8c80: 63 61 6e 20 74 72 65 61 74 20 65 72 72 6f 72 73  can treat errors
8c90: 20 61 20 6e 6f 6e 2d 66 61 74 61 6c 2e 0a 20 20   a non-fatal..  
8ca0: 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
8cb0: 33 5f 70 72 65 70 61 72 65 28 67 2e 64 62 2c 20  3_prepare(g.db, 
8cc0: 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74  zSql, -1, &pStmt
8cd0: 2c 20 30 29 3b 0a 20 20 69 66 28 20 53 51 4c 49  , 0);.  if( SQLI
8ce0: 54 45 5f 4f 4b 20 21 3d 20 72 63 20 29 7b 0a 20  TE_OK != rc ){. 
8cf0: 20 20 20 40 20 3c 73 70 61 6e 20 73 74 79 6c 65     @ <span style
8d00: 3d 27 63 6f 6c 6f 72 3a 72 65 64 27 3e 64 62 5f  ='color:red'>db_
8d10: 67 65 6e 65 72 69 63 5f 71 75 65 72 79 5f 76 69  generic_query_vi
8d20: 65 77 28 29 20 53 51 4c 20 65 72 72 6f 72 3a 0a  ew() SQL error:.
8d30: 20 20 20 20 40 20 25 68 28 73 71 6c 69 74 65 33      @ %h(sqlite3
8d40: 5f 65 72 72 6d 73 67 28 67 2e 64 62 29 29 3c 2f  _errmsg(g.db))</
8d50: 73 70 61 6e 3e 0a 20 20 20 20 72 65 74 75 72 6e  span>.    return
8d60: 20 72 63 3b 0a 20 20 7d 0a 20 20 6e 43 6f 6c 20   rc;.  }.  nCol 
8d70: 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
8d80: 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20  _count(pStmt);. 
8d90: 20 40 20 3c 74 61 62 6c 65 20 63 6c 61 73 73 3d   @ <table class=
8da0: 27 66 6f 73 73 69 6c 5f 64 62 5f 67 65 6e 65 72  'fossil_db_gener
8db0: 69 63 5f 71 75 65 72 79 5f 76 69 65 77 27 3e 3c  ic_query_view'><
8dc0: 74 62 6f 64 79 3e 0a 20 20 40 20 3c 74 72 20 63  tbody>.  @ <tr c
8dd0: 6c 61 73 73 3d 27 68 65 61 64 65 72 27 3e 0a 20  lass='header'>. 
8de0: 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c   for(i=0; i<nCol
8df0: 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 40 20 3c 74  ; ++i){.    @ <t
8e00: 64 3e 25 68 28 73 71 6c 69 74 65 33 5f 63 6f 6c  d>%h(sqlite3_col
8e10: 75 6d 6e 5f 6e 61 6d 65 28 70 53 74 6d 74 2c 69  umn_name(pStmt,i
8e20: 29 29 3c 2f 74 64 3e 0a 20 20 7d 0a 20 20 40 20  ))</td>.  }.  @ 
8e30: 3c 2f 74 72 3e 0a 0a 20 20 6e 52 6f 77 20 3d 20  </tr>..  nRow = 
8e40: 30 3b 0a 20 20 77 68 69 6c 65 28 20 53 51 4c 49  0;.  while( SQLI
8e50: 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f  TE_ROW==sqlite3_
8e60: 73 74 65 70 28 70 53 74 6d 74 29 20 29 7b 0a 20  step(pStmt) ){. 
8e70: 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61     const char *a
8e80: 7a 43 6c 61 73 73 5b 5d 20 3d 20 7b 20 22 65 76  zClass[] = { "ev
8e90: 65 6e 22 2c 20 22 6f 64 64 22 20 7d 3b 0a 20 20  en", "odd" };.  
8ea0: 20 20 40 20 3c 74 72 20 63 6c 61 73 73 3d 27 25    @ <tr class='%
8eb0: 73 28 61 7a 43 6c 61 73 73 5b 28 6e 52 6f 77 2b  s(azClass[(nRow+
8ec0: 2b 29 26 31 5d 29 27 3e 0a 20 20 20 20 20 20 66  +)&1])'>.      f
8ed0: 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20  or(i=0; i<nCol; 
8ee0: 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 7a 52  i++){.        zR
8ef0: 6f 77 20 3d 20 28 63 68 61 72 20 63 6f 6e 73 74  ow = (char const
8f00: 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  *)sqlite3_column
8f10: 5f 74 65 78 74 28 70 53 74 6d 74 2c 69 29 3b 0a  _text(pStmt,i);.
8f20: 20 20 20 20 20 20 20 20 69 66 28 20 69 73 53 61          if( isSa
8f30: 66 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  fe ){.          
8f40: 40 20 3c 74 64 3e 25 73 28 7a 52 6f 77 29 3c 2f  @ <td>%s(zRow)</
8f50: 74 64 3e 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  td>.        }els
8f60: 65 7b 0a 20 20 20 20 20 20 20 20 20 20 40 20 3c  e{.          @ <
8f70: 74 64 3e 25 68 28 7a 52 6f 77 29 3c 2f 74 64 3e  td>%h(zRow)</td>
8f80: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
8f90: 20 7d 0a 20 20 20 20 40 20 3c 2f 74 72 3e 0a 20   }.    @ </tr>. 
8fa0: 20 7d 0a 20 20 40 20 3c 2f 74 62 6f 64 79 3e 3c   }.  @ </tbody><
8fb0: 2f 74 61 62 6c 65 3e 0a 20 20 73 71 6c 69 74 65  /table>.  sqlite
8fc0: 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74  3_finalize(pStmt
8fd0: 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
8fe0: 54 45 5f 4f 4b 3b 0a 7d 0a                       TE_OK;.}.