Hex Artifact Content
Not logged in

Artifact e7592f21b53b337b4422a17992c750050cf0bc5d:

File src/db.c part of check-in [a25092b14c] - Be sure to call mbcsToUtf8() when opening the global configuration database. Ticket c7c36ef52edab by drh on 2009-08-15 21:38:24.

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 5f 46 4f 53 53 49 4c 5f   named "_FOSSIL_
0470: 22 20 6f 72 20 22 2e 66 6f 73 22 0a 2a 2a 20 20  " or ".fos".**  
0480: 20 20 20 20 20 20 20 61 6e 64 20 6c 6f 63 61 74         and locat
0490: 65 64 20 61 74 20 74 68 65 20 72 6f 6f 74 20 6f  ed at the root o
04a0: 66 20 74 68 65 20 6c 6f 63 61 6c 20 63 6f 70 79  f the local copy
04b0: 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65 20 74   of the source t
04c0: 72 65 65 2e 0a 2a 2a 0a 2a 2f 0a 23 69 6e 63 6c  ree..**.*/.#incl
04d0: 75 64 65 20 22 63 6f 6e 66 69 67 2e 68 22 0a 23  ude "config.h".#
04e0: 69 66 6e 64 65 66 20 5f 5f 4d 49 4e 47 57 33 32  ifndef __MINGW32
04f0: 5f 5f 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 70  __.#  include <p
0500: 77 64 2e 68 3e 0a 23 65 6e 64 69 66 0a 23 69 66  wd.h>.#endif.#if
0510: 64 65 66 20 5f 5f 4d 49 4e 47 57 33 32 5f 5f 0a  def __MINGW32__.
0520: 23 20 20 69 6e 63 6c 75 64 65 20 3c 77 69 6e 64  #  include <wind
0530: 6f 77 73 2e 68 3e 0a 23 65 6e 64 69 66 0a 23 69  ows.h>.#endif.#i
0540: 6e 63 6c 75 64 65 20 3c 73 71 6c 69 74 65 33 2e  nclude <sqlite3.
0550: 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73  h>.#include <sys
0560: 2f 74 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c 75  /types.h>.#inclu
0570: 64 65 20 3c 73 79 73 2f 73 74 61 74 2e 68 3e 0a  de <sys/stat.h>.
0580: 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 73 74 64  #include <unistd
0590: 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 22 64 62  .h>.#include "db
05a0: 2e 68 22 0a 0a 23 69 66 20 49 4e 54 45 52 46 41  .h"..#if INTERFA
05b0: 43 45 0a 2f 2a 0a 2a 2a 20 41 6e 20 73 69 6e 67  CE./*.** An sing
05c0: 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  le SQL statement
05d0: 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 20   is represented 
05e0: 61 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  as an instance o
05f0: 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  f the following.
0600: 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  ** structure..*/
0610: 0a 73 74 72 75 63 74 20 53 74 6d 74 20 7b 0a 20  .struct Stmt {. 
0620: 20 42 6c 6f 62 20 73 71 6c 3b 20 20 20 20 20 20   Blob sql;      
0630: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
0640: 53 51 4c 20 66 6f 72 20 74 68 69 73 20 73 74 61  SQL for this sta
0650: 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 73 71 6c 69  tement */.  sqli
0660: 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b  te3_stmt *pStmt;
0670: 20 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75 6c      /* The resul
0680: 74 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 70 72  ts of sqlite3_pr
0690: 65 70 61 72 65 28 29 20 2a 2f 0a 20 20 53 74 6d  epare() */.  Stm
06a0: 74 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65 76  t *pNext, *pPrev
06b0: 3b 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20  ;    /* List of 
06c0: 61 6c 6c 20 75 6e 66 69 6e 61 6c 69 7a 65 64 20  all unfinalized 
06d0: 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 7d 3b  statements */.};
06e0: 0a 23 65 6e 64 69 66 20 2f 2a 20 49 4e 54 45 52  .#endif /* INTER
06f0: 46 41 43 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43  FACE */../*.** C
0700: 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  all this routine
0710: 20 77 68 65 6e 20 61 20 64 61 74 61 62 61 73 65   when a database
0720: 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a   error occurs..*
0730: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 62  /.static void db
0740: 5f 65 72 72 28 63 6f 6e 73 74 20 63 68 61 72 20  _err(const char 
0750: 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a  *zFormat, ...){.
0760: 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20    va_list ap;.  
0770: 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 73 74  char *z;.  va_st
0780: 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29  art(ap, zFormat)
0790: 3b 0a 20 20 7a 20 3d 20 76 6d 70 72 69 6e 74 66  ;.  z = vmprintf
07a0: 28 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20  (zFormat, ap);. 
07b0: 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 69   va_end(ap);.  i
07c0: 66 28 20 67 2e 78 66 65 72 50 61 6e 69 63 20 29  f( g.xferPanic )
07d0: 7b 0a 20 20 20 20 63 67 69 5f 72 65 73 65 74 5f  {.    cgi_reset_
07e0: 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 40  content();.    @
07f0: 20 65 72 72 6f 72 20 44 61 74 61 62 61 73 65 5c   error Database\
0800: 73 65 72 72 6f 72 3a 5c 73 25 46 28 7a 29 0a 20  serror:\s%F(z). 
0810: 20 20 20 63 67 69 5f 72 65 70 6c 79 28 29 3b 0a     cgi_reply();.
0820: 20 20 7d 0a 20 20 69 66 28 20 67 2e 63 67 69 50    }.  if( g.cgiP
0830: 61 6e 69 63 20 29 7b 0a 20 20 20 20 67 2e 63 67  anic ){.    g.cg
0840: 69 50 61 6e 69 63 20 3d 20 30 3b 0a 20 20 20 20  iPanic = 0;.    
0850: 63 67 69 5f 70 72 69 6e 74 66 28 22 3c 68 31 3e  cgi_printf("<h1>
0860: 44 61 74 61 62 61 73 65 20 45 72 72 6f 72 3c 2f  Database Error</
0870: 68 31 3e 5c 6e 22 0a 20 20 20 20 20 20 20 20 20  h1>\n".         
0880: 20 20 20 20 20 20 22 3c 70 72 65 3e 25 68 3c 2f        "<pre>%h</
0890: 70 72 65 3e 22 2c 20 7a 29 3b 0a 20 20 20 20 63  pre>", z);.    c
08a0: 67 69 5f 72 65 70 6c 79 28 29 3b 0a 20 20 7d 65  gi_reply();.  }e
08b0: 6c 73 65 7b 0a 20 20 20 20 66 70 72 69 6e 74 66  lse{.    fprintf
08c0: 28 73 74 64 65 72 72 2c 20 22 25 73 3a 20 25 73  (stderr, "%s: %s
08d0: 5c 6e 22 2c 20 67 2e 61 72 67 76 5b 30 5d 2c 20  \n", g.argv[0], 
08e0: 7a 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 6f 72  z);.  }.  db_for
08f0: 63 65 5f 72 6f 6c 6c 62 61 63 6b 28 29 3b 0a 20  ce_rollback();. 
0900: 20 65 78 69 74 28 31 29 3b 0a 7d 0a 0a 73 74 61   exit(1);.}..sta
0910: 74 69 63 20 69 6e 74 20 6e 42 65 67 69 6e 20 3d  tic int nBegin =
0920: 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 65 73 74   0;      /* Nest
0930: 69 6e 67 20 64 65 70 74 68 20 6f 66 20 42 45 47  ing depth of BEG
0940: 49 4e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  IN */.static int
0950: 20 69 73 4e 65 77 52 65 70 6f 20 3d 20 30 3b 20   isNewRepo = 0; 
0960: 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
0970: 20 72 65 70 6f 73 69 74 6f 72 79 20 69 73 20 6e   repository is n
0980: 65 77 6c 79 20 63 72 65 61 74 65 64 20 2a 2f 0a  ewly created */.
0990: 73 74 61 74 69 63 20 69 6e 74 20 64 6f 52 6f 6c  static int doRol
09a0: 6c 62 61 63 6b 20 3d 20 30 3b 20 20 2f 2a 20 54  lback = 0;  /* T
09b0: 72 75 65 20 74 6f 20 66 6f 72 63 65 20 61 20 72  rue to force a r
09c0: 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 73 74 61 74 69  ollback */.stati
09d0: 63 20 69 6e 74 20 6e 43 6f 6d 6d 69 74 48 6f 6f  c int nCommitHoo
09e0: 6b 20 3d 20 30 3b 20 2f 2a 20 4e 75 6d 62 65 72  k = 0; /* Number
09f0: 20 6f 66 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 73   of commit hooks
0a00: 20 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63   */.static struc
0a10: 74 20 73 43 6f 6d 6d 69 74 48 6f 6f 6b 20 7b 0a  t sCommitHook {.
0a20: 20 20 69 6e 74 20 28 2a 78 48 6f 6f 6b 29 28 76    int (*xHook)(v
0a30: 6f 69 64 29 3b 20 20 2f 2a 20 46 75 6e 63 74 69  oid);  /* Functi
0a40: 6f 6e 73 20 74 6f 20 63 61 6c 6c 20 61 74 20 64  ons to call at d
0a50: 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  b_end_transactio
0a60: 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 73 65 71  n() */.  int seq
0a70: 75 65 6e 63 65 3b 20 20 20 20 20 20 20 20 2f 2a  uence;        /*
0a80: 20 43 61 6c 6c 20 66 75 6e 63 74 69 6f 6e 73 20   Call functions 
0a90: 69 6e 20 73 65 71 75 65 6e 63 65 20 6f 72 64 65  in sequence orde
0aa0: 72 20 2a 2f 0a 7d 20 61 48 6f 6f 6b 5b 35 5d 3b  r */.} aHook[5];
0ab0: 0a 73 74 61 74 69 63 20 53 74 6d 74 20 2a 70 41  .static Stmt *pA
0ac0: 6c 6c 53 74 6d 74 20 3d 20 30 3b 20 20 2f 2a 20  llStmt = 0;  /* 
0ad0: 4c 69 73 74 20 6f 66 20 61 6c 6c 20 75 6e 66 69  List of all unfi
0ae0: 6e 61 6c 69 7a 65 64 20 73 74 61 74 65 6d 65 6e  nalized statemen
0af0: 74 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  ts */../*.** Thi
0b00: 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
0b10: 6c 65 64 20 62 79 20 74 68 65 20 53 51 4c 69 74  led by the SQLit
0b20: 65 20 63 6f 6d 6d 69 74 2d 68 6f 6f 6b 20 6d 65  e commit-hook me
0b30: 63 68 61 6e 69 73 6d 0a 2a 2a 20 6a 75 73 74 20  chanism.** just 
0b40: 70 72 69 6f 72 20 74 6f 20 65 61 63 68 20 6f 6d  prior to each om
0b50: 69 74 2e 20 20 41 6c 6c 20 74 68 69 73 20 72 6f  it.  All this ro
0b60: 75 74 69 6e 65 20 64 6f 65 73 20 69 73 20 76 65  utine does is ve
0b70: 72 69 66 79 0a 2a 2a 20 74 68 61 74 20 6e 42 65  rify.** that nBe
0b80: 67 69 6e 20 72 65 61 6c 6c 79 20 69 73 20 7a 65  gin really is ze
0b90: 72 6f 2e 20 20 54 68 61 74 20 69 6e 73 75 72 65  ro.  That insure
0ba0: 73 20 74 68 61 74 20 74 72 61 6e 73 61 63 74 69  s that transacti
0bb0: 6f 6e 73 0a 2a 2a 20 63 61 6e 6e 6f 74 20 63 6f  ons.** cannot co
0bc0: 6d 6d 69 74 20 62 79 20 61 6e 79 20 6d 65 61 6e  mmit by any mean
0bd0: 73 20 6f 74 68 65 72 20 74 68 61 6e 20 62 79 20  s other than by 
0be0: 63 61 6c 6c 69 6e 67 20 64 62 5f 65 6e 64 5f 74  calling db_end_t
0bf0: 72 61 6e 73 61 63 74 69 6f 6e 28 29 0a 2a 2a 20  ransaction().** 
0c00: 62 65 6c 6f 77 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  below..**.** Thi
0c10: 73 20 69 73 20 6a 75 73 74 20 61 20 73 61 66 65  s is just a safe
0c20: 74 79 20 61 6e 64 20 73 61 6e 69 74 79 20 63 68  ty and sanity ch
0c30: 65 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  eck..*/.static i
0c40: 6e 74 20 64 62 5f 76 65 72 69 66 79 5f 61 74 5f  nt db_verify_at_
0c50: 63 6f 6d 6d 69 74 28 76 6f 69 64 20 2a 6e 6f 74  commit(void *not
0c60: 55 73 65 64 29 7b 0a 20 20 69 66 28 20 6e 42 65  Used){.  if( nBe
0c70: 67 69 6e 20 29 7b 0a 20 20 20 20 66 6f 73 73 69  gin ){.    fossi
0c80: 6c 5f 70 61 6e 69 63 28 22 69 6c 6c 65 67 61 6c  l_panic("illegal
0c90: 20 63 6f 6d 6d 69 74 20 61 74 74 65 6d 70 74 22   commit attempt"
0ca0: 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  );.    return 1;
0cb0: 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
0cc0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20  .}../*.** Begin 
0cd0: 61 6e 64 20 65 6e 64 20 61 20 6e 65 73 74 65 64  and end a nested
0ce0: 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2f 0a   transaction.*/.
0cf0: 76 6f 69 64 20 64 62 5f 62 65 67 69 6e 5f 74 72  void db_begin_tr
0d00: 61 6e 73 61 63 74 69 6f 6e 28 76 6f 69 64 29 7b  ansaction(void){
0d10: 0a 20 20 69 66 28 20 6e 42 65 67 69 6e 3d 3d 30  .  if( nBegin==0
0d20: 20 29 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69   ){.    db_multi
0d30: 5f 65 78 65 63 28 22 42 45 47 49 4e 22 29 3b 0a  _exec("BEGIN");.
0d40: 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d      sqlite3_comm
0d50: 69 74 5f 68 6f 6f 6b 28 67 2e 64 62 2c 20 64 62  it_hook(g.db, db
0d60: 5f 76 65 72 69 66 79 5f 61 74 5f 63 6f 6d 6d 69  _verify_at_commi
0d70: 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 6e 42 65  t, 0);.  }.  nBe
0d80: 67 69 6e 2b 2b 3b 0a 7d 0a 76 6f 69 64 20 64 62  gin++;.}.void db
0d90: 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e  _end_transaction
0da0: 28 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 46 6c 61  (int rollbackFla
0db0: 67 29 7b 0a 20 20 69 66 28 20 6e 42 65 67 69 6e  g){.  if( nBegin
0dc0: 3c 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  <=0 ) return;.  
0dd0: 69 66 28 20 72 6f 6c 6c 62 61 63 6b 46 6c 61 67  if( rollbackFlag
0de0: 20 29 20 64 6f 52 6f 6c 6c 62 61 63 6b 20 3d 20   ) doRollback = 
0df0: 31 3b 0a 20 20 6e 42 65 67 69 6e 2d 2d 3b 0a 20  1;.  nBegin--;. 
0e00: 20 69 66 28 20 6e 42 65 67 69 6e 3d 3d 30 20 29   if( nBegin==0 )
0e10: 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  {.    int i;.   
0e20: 20 66 6f 72 28 69 3d 30 3b 20 64 6f 52 6f 6c 6c   for(i=0; doRoll
0e30: 62 61 63 6b 3d 3d 30 20 26 26 20 69 3c 6e 43 6f  back==0 && i<nCo
0e40: 6d 6d 69 74 48 6f 6f 6b 3b 20 69 2b 2b 29 7b 0a  mmitHook; i++){.
0e50: 20 20 20 20 20 20 64 6f 52 6f 6c 6c 62 61 63 6b        doRollback
0e60: 20 7c 3d 20 61 48 6f 6f 6b 5b 69 5d 2e 78 48 6f   |= aHook[i].xHo
0e70: 6f 6b 28 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ok();.    }.    
0e80: 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 64 6f  db_multi_exec(do
0e90: 52 6f 6c 6c 62 61 63 6b 20 3f 20 22 52 4f 4c 4c  Rollback ? "ROLL
0ea0: 42 41 43 4b 22 20 3a 20 22 43 4f 4d 4d 49 54 22  BACK" : "COMMIT"
0eb0: 29 3b 0a 20 20 20 20 64 6f 52 6f 6c 6c 62 61 63  );.    doRollbac
0ec0: 6b 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 76 6f 69  k = 0;.  }.}.voi
0ed0: 64 20 64 62 5f 66 6f 72 63 65 5f 72 6f 6c 6c 62  d db_force_rollb
0ee0: 61 63 6b 28 76 6f 69 64 29 7b 0a 20 20 69 66 28  ack(void){.  if(
0ef0: 20 6e 42 65 67 69 6e 20 29 7b 0a 20 20 20 20 73   nBegin ){.    s
0f00: 71 6c 69 74 65 33 5f 65 78 65 63 28 67 2e 64 62  qlite3_exec(g.db
0f10: 2c 20 22 52 4f 4c 4c 42 41 43 4b 22 2c 20 30 2c  , "ROLLBACK", 0,
0f20: 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20   0, 0);.    if( 
0f30: 69 73 4e 65 77 52 65 70 6f 20 29 7b 0a 20 20 20  isNewRepo ){.   
0f40: 20 20 20 64 62 5f 63 6c 6f 73 65 28 29 3b 0a 20     db_close();. 
0f50: 20 20 20 20 20 75 6e 6c 69 6e 6b 28 67 2e 7a 52       unlink(g.zR
0f60: 65 70 6f 73 69 74 6f 72 79 4e 61 6d 65 29 3b 0a  epositoryName);.
0f70: 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6e 42 65 67      }.  }.  nBeg
0f80: 69 6e 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  in = 0;.}../*.**
0f90: 20 49 6e 73 74 61 6c 6c 20 61 20 63 6f 6d 6d 69   Install a commi
0fa0: 74 20 68 6f 6f 6b 2e 20 20 48 6f 6f 6b 73 20 61  t hook.  Hooks a
0fb0: 72 65 20 69 6e 73 74 61 6c 6c 65 64 20 69 6e 20  re installed in 
0fc0: 73 65 71 75 65 6e 63 65 20 6f 72 64 65 72 2e 0a  sequence order..
0fd0: 2a 2a 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f  ** It is an erro
0fe0: 72 20 74 6f 20 69 6e 73 74 61 6c 6c 20 74 68 65  r to install the
0ff0: 20 73 61 6d 65 20 63 6f 6d 6d 69 74 20 68 6f 6f   same commit hoo
1000: 6b 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65  k more than once
1010: 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 63 6f 6d  ..**.** Each com
1020: 6d 69 74 20 68 6f 6f 6b 20 69 73 20 63 61 6c 6c  mit hook is call
1030: 65 64 20 28 69 6e 20 6f 72 64 65 72 20 6f 66 20  ed (in order of 
1040: 61 63 63 65 6e 64 69 6e 67 20 73 65 71 75 65 6e  accending sequen
1050: 63 65 29 20 61 74 0a 2a 2a 20 65 61 63 68 20 63  ce) at.** each c
1060: 6f 6d 6d 69 74 20 6f 70 65 72 61 74 69 6f 6e 2e  ommit operation.
1070: 20 20 49 66 20 61 6e 79 20 63 6f 6d 6d 69 74 20    If any commit 
1080: 68 6f 6f 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e  hook returns non
1090: 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 20 73 75  -zero,.** the su
10a0: 62 73 65 71 75 65 6e 63 65 20 63 6f 6d 6d 69 74  bsequence commit
10b0: 20 68 6f 6f 6b 73 20 61 72 65 20 6f 6d 69 74 74   hooks are omitt
10c0: 65 64 20 61 6e 64 20 74 68 65 20 74 72 61 6e 73  ed and the trans
10d0: 61 63 74 69 6f 6e 0a 2a 2a 20 72 6f 6c 6c 73 20  action.** rolls 
10e0: 62 61 63 6b 20 72 61 74 68 65 72 20 74 68 61 6e  back rather than
10f0: 20 63 6f 6d 6d 69 74 2e 20 20 49 74 20 69 73 20   commit.  It is 
1100: 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69  the responsibili
1110: 74 79 20 6f 66 20 74 68 65 0a 2a 2a 20 68 6f 6f  ty of the.** hoo
1120: 6b 73 20 74 68 65 6d 73 65 6c 76 65 73 20 74 6f  ks themselves to
1130: 20 69 73 73 75 65 20 61 6e 79 20 65 72 72 6f 72   issue any error
1140: 20 6d 65 73 73 61 67 65 73 2e 0a 2a 2f 0a 76 6f   messages..*/.vo
1150: 69 64 20 64 62 5f 63 6f 6d 6d 69 74 5f 68 6f 6f  id db_commit_hoo
1160: 6b 28 69 6e 74 20 28 2a 78 29 28 76 6f 69 64 29  k(int (*x)(void)
1170: 2c 20 69 6e 74 20 73 65 71 75 65 6e 63 65 29 7b  , int sequence){
1180: 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65  .  int i;.  asse
1190: 72 74 28 20 6e 43 6f 6d 6d 69 74 48 6f 6f 6b 20  rt( nCommitHook 
11a0: 3c 20 73 69 7a 65 6f 66 28 61 48 6f 6f 6b 29 2f  < sizeof(aHook)/
11b0: 73 69 7a 65 6f 66 28 61 48 6f 6f 6b 5b 31 5d 29  sizeof(aHook[1])
11c0: 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   );.  for(i=0; i
11d0: 3c 6e 43 6f 6d 6d 69 74 48 6f 6f 6b 3b 20 69 2b  <nCommitHook; i+
11e0: 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  +){.    assert( 
11f0: 78 21 3d 61 48 6f 6f 6b 5b 69 5d 2e 78 48 6f 6f  x!=aHook[i].xHoo
1200: 6b 20 29 3b 0a 20 20 20 20 69 66 28 20 61 48 6f  k );.    if( aHo
1210: 6f 6b 5b 69 5d 2e 73 65 71 75 65 6e 63 65 3e 73  ok[i].sequence>s
1220: 65 71 75 65 6e 63 65 20 29 7b 0a 20 20 20 20 20  equence ){.     
1230: 20 69 6e 74 20 73 20 3d 20 73 65 71 75 65 6e 63   int s = sequenc
1240: 65 3b 0a 20 20 20 20 20 20 69 6e 74 20 28 2a 78  e;.      int (*x
1250: 53 29 28 76 6f 69 64 29 20 3d 20 78 3b 0a 20 20  S)(void) = x;.  
1260: 20 20 20 20 73 65 71 75 65 6e 63 65 20 3d 20 61      sequence = a
1270: 48 6f 6f 6b 5b 69 5d 2e 73 65 71 75 65 6e 63 65  Hook[i].sequence
1280: 3b 0a 20 20 20 20 20 20 78 20 3d 20 61 48 6f 6f  ;.      x = aHoo
1290: 6b 5b 69 5d 2e 78 48 6f 6f 6b 3b 0a 20 20 20 20  k[i].xHook;.    
12a0: 20 20 61 48 6f 6f 6b 5b 69 5d 2e 73 65 71 75 65    aHook[i].seque
12b0: 6e 63 65 20 3d 20 73 3b 0a 20 20 20 20 20 20 61  nce = s;.      a
12c0: 48 6f 6f 6b 5b 69 5d 2e 78 48 6f 6f 6b 20 3d 20  Hook[i].xHook = 
12d0: 78 53 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  xS;.    }.  }.  
12e0: 61 48 6f 6f 6b 5b 6e 43 6f 6d 6d 69 74 48 6f 6f  aHook[nCommitHoo
12f0: 6b 5d 2e 73 65 71 75 65 6e 63 65 20 3d 20 73 65  k].sequence = se
1300: 71 75 65 6e 63 65 3b 0a 20 20 61 48 6f 6f 6b 5b  quence;.  aHook[
1310: 6e 43 6f 6d 6d 69 74 48 6f 6f 6b 5d 2e 78 48 6f  nCommitHook].xHo
1320: 6f 6b 20 3d 20 78 3b 0a 20 20 6e 43 6f 6d 6d 69  ok = x;.  nCommi
1330: 74 48 6f 6f 6b 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a  tHook++;.}../*.*
1340: 2a 20 50 72 65 70 61 72 65 20 61 20 53 74 6d 74  * Prepare a Stmt
1350: 2e 20 20 41 73 73 75 6d 65 20 74 68 61 74 20 74  .  Assume that t
1360: 68 65 20 53 74 6d 74 20 69 73 20 70 72 65 76 69  he Stmt is previ
1370: 6f 75 73 6c 79 20 75 6e 69 6e 69 74 69 61 6c 69  ously uninitiali
1380: 7a 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69  zed..** If the i
1390: 6e 70 75 74 20 73 74 72 69 6e 67 20 63 6f 6e 74  nput string cont
13a0: 61 69 6e 73 20 6d 75 6c 74 69 70 6c 65 20 53 51  ains multiple SQ
13b0: 4c 20 73 74 61 74 65 6d 65 6e 74 73 2c 20 6f 6e  L statements, on
13c0: 6c 79 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20  ly the first.** 
13d0: 6f 6e 65 20 69 73 20 70 72 6f 63 65 73 73 65 64  one is processed
13e0: 2e 20 20 41 6c 6c 20 73 74 61 74 65 6d 65 6e 74  .  All statement
13f0: 73 20 62 65 79 6f 6e 64 20 74 68 65 20 66 69 72  s beyond the fir
1400: 73 74 20 61 72 65 20 73 69 6c 65 6e 74 6c 79 20  st are silently 
1410: 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 69 6e 74 20  ignored..*/.int 
1420: 64 62 5f 76 70 72 65 70 61 72 65 28 53 74 6d 74  db_vprepare(Stmt
1430: 20 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20 63   *pStmt, const c
1440: 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61  har *zFormat, va
1450: 5f 6c 69 73 74 20 61 70 29 7b 0a 20 20 63 68 61  _list ap){.  cha
1460: 72 20 2a 7a 53 71 6c 3b 0a 20 20 62 6c 6f 62 5f  r *zSql;.  blob_
1470: 7a 65 72 6f 28 26 70 53 74 6d 74 2d 3e 73 71 6c  zero(&pStmt->sql
1480: 29 3b 0a 20 20 62 6c 6f 62 5f 76 61 70 70 65 6e  );.  blob_vappen
1490: 64 66 28 26 70 53 74 6d 74 2d 3e 73 71 6c 2c 20  df(&pStmt->sql, 
14a0: 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20  zFormat, ap);.  
14b0: 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 7a 53  va_end(ap);.  zS
14c0: 71 6c 20 3d 20 62 6c 6f 62 5f 73 74 72 28 26 70  ql = blob_str(&p
14d0: 53 74 6d 74 2d 3e 73 71 6c 29 3b 0a 20 20 69 66  Stmt->sql);.  if
14e0: 28 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  ( sqlite3_prepar
14f0: 65 5f 76 32 28 67 2e 64 62 2c 20 7a 53 71 6c 2c  e_v2(g.db, zSql,
1500: 20 2d 31 2c 20 26 70 53 74 6d 74 2d 3e 70 53 74   -1, &pStmt->pSt
1510: 6d 74 2c 20 30 29 21 3d 30 20 29 7b 0a 20 20 20  mt, 0)!=0 ){.   
1520: 20 64 62 5f 65 72 72 28 22 25 73 5c 6e 25 73 22   db_err("%s\n%s"
1530: 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  , sqlite3_errmsg
1540: 28 67 2e 64 62 29 2c 20 7a 53 71 6c 29 3b 0a 20  (g.db), zSql);. 
1550: 20 7d 0a 20 20 70 53 74 6d 74 2d 3e 70 4e 65 78   }.  pStmt->pNex
1560: 74 20 3d 20 70 53 74 6d 74 2d 3e 70 50 72 65 76  t = pStmt->pPrev
1570: 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 30   = 0;.  return 0
1580: 3b 0a 7d 0a 69 6e 74 20 64 62 5f 70 72 65 70 61  ;.}.int db_prepa
1590: 72 65 28 53 74 6d 74 20 2a 70 53 74 6d 74 2c 20  re(Stmt *pStmt, 
15a0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
15b0: 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 69 6e 74  mat, ...){.  int
15c0: 20 72 63 3b 0a 20 20 76 61 5f 6c 69 73 74 20 61   rc;.  va_list a
15d0: 70 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70  p;.  va_start(ap
15e0: 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 72 63  , zFormat);.  rc
15f0: 20 3d 20 64 62 5f 76 70 72 65 70 61 72 65 28 70   = db_vprepare(p
1600: 53 74 6d 74 2c 20 7a 46 6f 72 6d 61 74 2c 20 61  Stmt, zFormat, a
1610: 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29  p);.  va_end(ap)
1620: 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
1630: 0a 69 6e 74 20 64 62 5f 73 74 61 74 69 63 5f 70  .int db_static_p
1640: 72 65 70 61 72 65 28 53 74 6d 74 20 2a 70 53 74  repare(Stmt *pSt
1650: 6d 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  mt, const char *
1660: 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20  zFormat, ...){. 
1670: 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1680: 5f 4f 4b 3b 0a 20 20 69 66 28 20 62 6c 6f 62 5f  _OK;.  if( blob_
1690: 73 69 7a 65 28 26 70 53 74 6d 74 2d 3e 73 71 6c  size(&pStmt->sql
16a0: 29 3d 3d 30 20 29 7b 0a 20 20 20 20 76 61 5f 6c  )==0 ){.    va_l
16b0: 69 73 74 20 61 70 3b 0a 20 20 20 20 76 61 5f 73  ist ap;.    va_s
16c0: 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74  tart(ap, zFormat
16d0: 29 3b 0a 20 20 20 20 72 63 20 3d 20 64 62 5f 76  );.    rc = db_v
16e0: 70 72 65 70 61 72 65 28 70 53 74 6d 74 2c 20 7a  prepare(pStmt, z
16f0: 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 20  Format, ap);.   
1700: 20 70 53 74 6d 74 2d 3e 70 4e 65 78 74 20 3d 20   pStmt->pNext = 
1710: 70 41 6c 6c 53 74 6d 74 3b 0a 20 20 20 20 70 53  pAllStmt;.    pS
1720: 74 6d 74 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a  tmt->pPrev = 0;.
1730: 20 20 20 20 69 66 28 20 70 41 6c 6c 53 74 6d 74      if( pAllStmt
1740: 20 29 20 70 41 6c 6c 53 74 6d 74 2d 3e 70 50 72   ) pAllStmt->pPr
1750: 65 76 20 3d 20 70 53 74 6d 74 3b 0a 20 20 20 20  ev = pStmt;.    
1760: 70 41 6c 6c 53 74 6d 74 20 3d 20 70 53 74 6d 74  pAllStmt = pStmt
1770: 3b 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29  ;.    va_end(ap)
1780: 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1790: 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  c;.}../*.** Retu
17a0: 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  rn the index of 
17b0: 61 20 62 69 6e 64 20 70 61 72 61 6d 65 74 65 72  a bind parameter
17c0: 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
17d0: 61 72 61 6d 49 64 78 28 53 74 6d 74 20 2a 70 53  aramIdx(Stmt *pS
17e0: 74 6d 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  tmt, const char 
17f0: 2a 7a 50 61 72 61 6d 4e 61 6d 65 29 7b 0a 20 20  *zParamName){.  
1800: 69 6e 74 20 69 20 3d 20 73 71 6c 69 74 65 33 5f  int i = sqlite3_
1810: 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69  bind_parameter_i
1820: 6e 64 65 78 28 70 53 74 6d 74 2d 3e 70 53 74 6d  ndex(pStmt->pStm
1830: 74 2c 20 7a 50 61 72 61 6d 4e 61 6d 65 29 3b 0a  t, zParamName);.
1840: 20 20 69 66 28 20 69 3d 3d 30 20 29 7b 0a 20 20    if( i==0 ){.  
1850: 20 20 64 62 5f 65 72 72 28 22 6e 6f 20 73 75 63    db_err("no suc
1860: 68 20 62 69 6e 64 20 70 61 72 61 6d 65 74 65 72  h bind parameter
1870: 3a 20 25 73 5c 6e 53 51 4c 3a 20 25 62 22 2c 20  : %s\nSQL: %b", 
1880: 7a 50 61 72 61 6d 4e 61 6d 65 2c 20 26 70 53 74  zParamName, &pSt
1890: 6d 74 2d 3e 73 71 6c 29 3b 0a 20 20 7d 0a 20 20  mt->sql);.  }.  
18a0: 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 2f 2a 0a 2a  return i;.}./*.*
18b0: 2a 20 42 69 6e 64 20 61 6e 20 69 6e 74 65 67 65  * Bind an intege
18c0: 72 2c 20 73 74 72 69 6e 67 2c 20 6f 72 20 42 6c  r, string, or Bl
18d0: 6f 62 20 76 61 6c 75 65 20 74 6f 20 61 20 6e 61  ob value to a na
18e0: 6d 65 64 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a  med parameter..*
18f0: 2f 0a 69 6e 74 20 64 62 5f 62 69 6e 64 5f 69 6e  /.int db_bind_in
1900: 74 28 53 74 6d 74 20 2a 70 53 74 6d 74 2c 20 63  t(Stmt *pStmt, c
1910: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 72 61  onst char *zPara
1920: 6d 4e 61 6d 65 2c 20 69 6e 74 20 69 56 61 6c 75  mName, int iValu
1930: 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  e){.  return sql
1940: 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 70 53  ite3_bind_int(pS
1950: 74 6d 74 2d 3e 70 53 74 6d 74 2c 20 70 61 72 61  tmt->pStmt, para
1960: 6d 49 64 78 28 70 53 74 6d 74 2c 20 7a 50 61 72  mIdx(pStmt, zPar
1970: 61 6d 4e 61 6d 65 29 2c 20 69 56 61 6c 75 65 29  amName), iValue)
1980: 3b 0a 7d 0a 69 6e 74 20 64 62 5f 62 69 6e 64 5f  ;.}.int db_bind_
1990: 69 6e 74 36 34 28 53 74 6d 74 20 2a 70 53 74 6d  int64(Stmt *pStm
19a0: 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  t, const char *z
19b0: 50 61 72 61 6d 4e 61 6d 65 2c 20 69 36 34 20 69  ParamName, i64 i
19c0: 56 61 6c 75 65 29 7b 0a 20 20 72 65 74 75 72 6e  Value){.  return
19d0: 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
19e0: 74 36 34 28 70 53 74 6d 74 2d 3e 70 53 74 6d 74  t64(pStmt->pStmt
19f0: 2c 20 70 61 72 61 6d 49 64 78 28 70 53 74 6d 74  , paramIdx(pStmt
1a00: 2c 20 7a 50 61 72 61 6d 4e 61 6d 65 29 2c 20 69  , zParamName), i
1a10: 56 61 6c 75 65 29 3b 0a 7d 0a 69 6e 74 20 64 62  Value);.}.int db
1a20: 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 28 53 74 6d  _bind_double(Stm
1a30: 74 20 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20  t *pStmt, const 
1a40: 63 68 61 72 20 2a 7a 50 61 72 61 6d 4e 61 6d 65  char *zParamName
1a50: 2c 20 64 6f 75 62 6c 65 20 72 56 61 6c 75 65 29  , double rValue)
1a60: 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
1a70: 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 28 70  e3_bind_double(p
1a80: 53 74 6d 74 2d 3e 70 53 74 6d 74 2c 20 70 61 72  Stmt->pStmt, par
1a90: 61 6d 49 64 78 28 70 53 74 6d 74 2c 20 7a 50 61  amIdx(pStmt, zPa
1aa0: 72 61 6d 4e 61 6d 65 29 2c 20 72 56 61 6c 75 65  ramName), rValue
1ab0: 29 3b 0a 7d 0a 69 6e 74 20 64 62 5f 62 69 6e 64  );.}.int db_bind
1ac0: 5f 74 65 78 74 28 53 74 6d 74 20 2a 70 53 74 6d  _text(Stmt *pStm
1ad0: 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  t, const char *z
1ae0: 50 61 72 61 6d 4e 61 6d 65 2c 20 63 6f 6e 73 74  ParamName, const
1af0: 20 63 68 61 72 20 2a 7a 56 61 6c 75 65 29 7b 0a   char *zValue){.
1b00: 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
1b10: 5f 62 69 6e 64 5f 74 65 78 74 28 70 53 74 6d 74  _bind_text(pStmt
1b20: 2d 3e 70 53 74 6d 74 2c 20 70 61 72 61 6d 49 64  ->pStmt, paramId
1b30: 78 28 70 53 74 6d 74 2c 20 7a 50 61 72 61 6d 4e  x(pStmt, zParamN
1b40: 61 6d 65 29 2c 20 7a 56 61 6c 75 65 2c 0a 20 20  ame), zValue,.  
1b50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b60: 20 20 20 20 20 20 20 20 20 2d 31 2c 20 53 51 4c           -1, SQL
1b70: 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 69  ITE_STATIC);.}.i
1b80: 6e 74 20 64 62 5f 62 69 6e 64 5f 6e 75 6c 6c 28  nt db_bind_null(
1b90: 53 74 6d 74 20 2a 70 53 74 6d 74 2c 20 63 6f 6e  Stmt *pStmt, con
1ba0: 73 74 20 63 68 61 72 20 2a 7a 50 61 72 61 6d 4e  st char *zParamN
1bb0: 61 6d 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  ame){.  return s
1bc0: 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c  qlite3_bind_null
1bd0: 28 70 53 74 6d 74 2d 3e 70 53 74 6d 74 2c 20 70  (pStmt->pStmt, p
1be0: 61 72 61 6d 49 64 78 28 70 53 74 6d 74 2c 20 7a  aramIdx(pStmt, z
1bf0: 50 61 72 61 6d 4e 61 6d 65 29 29 3b 0a 7d 0a 69  ParamName));.}.i
1c00: 6e 74 20 64 62 5f 62 69 6e 64 5f 62 6c 6f 62 28  nt db_bind_blob(
1c10: 53 74 6d 74 20 2a 70 53 74 6d 74 2c 20 63 6f 6e  Stmt *pStmt, con
1c20: 73 74 20 63 68 61 72 20 2a 7a 50 61 72 61 6d 4e  st char *zParamN
1c30: 61 6d 65 2c 20 42 6c 6f 62 20 2a 70 42 6c 6f 62  ame, Blob *pBlob
1c40: 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
1c50: 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 70 53  te3_bind_blob(pS
1c60: 74 6d 74 2d 3e 70 53 74 6d 74 2c 20 70 61 72 61  tmt->pStmt, para
1c70: 6d 49 64 78 28 70 53 74 6d 74 2c 20 7a 50 61 72  mIdx(pStmt, zPar
1c80: 61 6d 4e 61 6d 65 29 2c 0a 20 20 20 20 20 20 20  amName),.       
1c90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ca0: 20 20 20 62 6c 6f 62 5f 62 75 66 66 65 72 28 70     blob_buffer(p
1cb0: 42 6c 6f 62 29 2c 20 62 6c 6f 62 5f 73 69 7a 65  Blob), blob_size
1cc0: 28 70 42 6c 6f 62 29 2c 20 53 51 4c 49 54 45 5f  (pBlob), SQLITE_
1cd0: 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 2f 2a 20 62  STATIC);.}../* b
1ce0: 69 6e 64 5f 73 74 72 28 29 20 74 72 65 61 74 73  ind_str() treats
1cf0: 20 61 20 42 6c 6f 62 20 6f 62 6a 65 63 74 20 6c   a Blob object l
1d00: 69 6b 65 20 61 20 54 45 58 54 20 73 74 72 69 6e  ike a TEXT strin
1d10: 67 20 61 6e 64 20 62 69 6e 64 73 20 69 74 0a 2a  g and binds it.*
1d20: 2a 20 74 6f 20 74 68 65 20 53 51 4c 20 76 61 72  * to the SQL var
1d30: 69 61 62 6c 65 2e 20 20 43 6f 6e 73 74 72 61 73  iable.  Constras
1d40: 74 20 74 68 69 73 20 74 6f 20 62 69 6e 64 5f 62  t this to bind_b
1d50: 6c 6f 62 28 29 20 77 68 69 63 68 20 74 72 65 61  lob() which trea
1d60: 74 73 0a 2a 2a 20 74 68 65 20 42 6c 6f 62 20 6f  ts.** the Blob o
1d70: 62 6a 65 63 74 20 6c 69 6b 65 20 61 6e 20 53 51  bject like an SQ
1d80: 4c 20 42 4c 4f 42 2e 0a 2a 2f 0a 69 6e 74 20 64  L BLOB..*/.int d
1d90: 62 5f 62 69 6e 64 5f 73 74 72 28 53 74 6d 74 20  b_bind_str(Stmt 
1da0: 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20 63 68  *pStmt, const ch
1db0: 61 72 20 2a 7a 50 61 72 61 6d 4e 61 6d 65 2c 20  ar *zParamName, 
1dc0: 42 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 20  Blob *pBlob){.  
1dd0: 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 62  return sqlite3_b
1de0: 69 6e 64 5f 74 65 78 74 28 70 53 74 6d 74 2d 3e  ind_text(pStmt->
1df0: 70 53 74 6d 74 2c 20 70 61 72 61 6d 49 64 78 28  pStmt, paramIdx(
1e00: 70 53 74 6d 74 2c 20 7a 50 61 72 61 6d 4e 61 6d  pStmt, zParamNam
1e10: 65 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  e),.            
1e20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 6c                bl
1e30: 6f 62 5f 62 75 66 66 65 72 28 70 42 6c 6f 62 29  ob_buffer(pBlob)
1e40: 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 70 42 6c 6f  , blob_size(pBlo
1e50: 62 29 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  b), SQLITE_STATI
1e60: 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 65  C);.}../*.** Ste
1e70: 70 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  p the SQL statem
1e80: 65 6e 74 2e 20 20 52 65 74 75 72 6e 20 65 69 74  ent.  Return eit
1e90: 68 65 72 20 53 51 4c 49 54 45 5f 52 4f 57 20 6f  her SQLITE_ROW o
1ea0: 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 0a  r an error code.
1eb0: 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4f 4b 20  ** or SQLITE_OK 
1ec0: 69 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  if the statement
1ed0: 20 66 69 6e 69 73 68 65 73 20 73 75 63 63 65 73   finishes succes
1ee0: 73 66 75 6c 6c 79 2e 0a 2a 2f 0a 69 6e 74 20 64  sfully..*/.int d
1ef0: 62 5f 73 74 65 70 28 53 74 6d 74 20 2a 70 53 74  b_step(Stmt *pSt
1f00: 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  mt){.  int rc;. 
1f10: 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74   rc = sqlite3_st
1f20: 65 70 28 70 53 74 6d 74 2d 3e 70 53 74 6d 74 29  ep(pStmt->pStmt)
1f30: 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
1f40: 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 6f 72  ../*.** Reset or
1f50: 20 66 69 6e 61 6c 69 7a 65 20 61 20 73 74 61 74   finalize a stat
1f60: 65 6d 65 6e 74 2e 0a 2a 2f 0a 69 6e 74 20 64 62  ement..*/.int db
1f70: 5f 72 65 73 65 74 28 53 74 6d 74 20 2a 70 53 74  _reset(Stmt *pSt
1f80: 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  mt){.  int rc = 
1f90: 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 53  sqlite3_reset(pS
1fa0: 74 6d 74 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 64  tmt->pStmt);.  d
1fb0: 62 5f 63 68 65 63 6b 5f 72 65 73 75 6c 74 28 72  b_check_result(r
1fc0: 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  c);.  return rc;
1fd0: 0a 7d 0a 69 6e 74 20 64 62 5f 66 69 6e 61 6c 69  .}.int db_finali
1fe0: 7a 65 28 53 74 6d 74 20 2a 70 53 74 6d 74 29 7b  ze(Stmt *pStmt){
1ff0: 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 62 6c 6f  .  int rc;.  blo
2000: 62 5f 72 65 73 65 74 28 26 70 53 74 6d 74 2d 3e  b_reset(&pStmt->
2010: 73 71 6c 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  sql);.  rc = sql
2020: 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53  ite3_finalize(pS
2030: 74 6d 74 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 64  tmt->pStmt);.  d
2040: 62 5f 63 68 65 63 6b 5f 72 65 73 75 6c 74 28 72  b_check_result(r
2050: 63 29 3b 0a 20 20 70 53 74 6d 74 2d 3e 70 53 74  c);.  pStmt->pSt
2060: 6d 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 53  mt = 0;.  if( pS
2070: 74 6d 74 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20  tmt->pNext ){.  
2080: 20 20 70 53 74 6d 74 2d 3e 70 4e 65 78 74 2d 3e    pStmt->pNext->
2090: 70 50 72 65 76 20 3d 20 70 53 74 6d 74 2d 3e 70  pPrev = pStmt->p
20a0: 50 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Prev;.  }.  if( 
20b0: 70 53 74 6d 74 2d 3e 70 50 72 65 76 20 29 7b 0a  pStmt->pPrev ){.
20c0: 20 20 20 20 70 53 74 6d 74 2d 3e 70 50 72 65 76      pStmt->pPrev
20d0: 2d 3e 70 4e 65 78 74 20 3d 20 70 53 74 6d 74 2d  ->pNext = pStmt-
20e0: 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 20  >pNext;.  }else 
20f0: 69 66 28 20 70 41 6c 6c 53 74 6d 74 3d 3d 70 53  if( pAllStmt==pS
2100: 74 6d 74 20 29 7b 0a 20 20 20 20 70 41 6c 6c 53  tmt ){.    pAllS
2110: 74 6d 74 20 3d 20 70 53 74 6d 74 2d 3e 70 4e 65  tmt = pStmt->pNe
2120: 78 74 3b 0a 20 20 7d 0a 20 20 70 53 74 6d 74 2d  xt;.  }.  pStmt-
2130: 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 70 53  >pNext = 0;.  pS
2140: 74 6d 74 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a  tmt->pPrev = 0;.
2150: 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
2160: 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
2170: 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 6d 6f   rowid of the mo
2180: 73 74 20 72 65 63 65 6e 74 20 69 6e 73 65 72 74  st recent insert
2190: 0a 2a 2f 0a 69 36 34 20 64 62 5f 6c 61 73 74 5f  .*/.i64 db_last_
21a0: 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 76 6f 69  insert_rowid(voi
21b0: 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  d){.  return sql
21c0: 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74  ite3_last_insert
21d0: 5f 72 6f 77 69 64 28 67 2e 64 62 29 3b 0a 7d 0a  _rowid(g.db);.}.
21e0: 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
21f0: 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
2200: 20 74 68 61 74 20 77 65 72 65 20 63 68 61 6e 67   that were chang
2210: 65 64 20 62 79 20 74 68 65 20 6d 6f 73 74 20 72  ed by the most r
2220: 65 63 65 6e 74 0a 2a 2a 20 49 4e 53 45 52 54 2c  ecent.** INSERT,
2230: 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45   UPDATE, or DELE
2240: 54 45 2e 20 20 41 75 78 69 6c 69 61 72 79 20 63  TE.  Auxiliary c
2250: 68 61 6e 67 65 73 20 63 61 75 73 65 64 20 62 79  hanges caused by
2260: 20 74 72 69 67 67 65 72 73 0a 2a 2a 20 6f 72 20   triggers.** or 
2270: 6f 74 68 65 72 20 73 69 64 65 20 65 66 66 65 63  other side effec
2280: 74 73 20 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74  ts are not count
2290: 65 64 2e 0a 2a 2f 0a 69 6e 74 20 64 62 5f 63 68  ed..*/.int db_ch
22a0: 61 6e 67 65 73 28 76 6f 69 64 29 7b 0a 20 20 72  anges(void){.  r
22b0: 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 63 68  eturn sqlite3_ch
22c0: 61 6e 67 65 73 28 67 2e 64 62 29 3b 0a 7d 0a 0a  anges(g.db);.}..
22d0: 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 65  /*.** Extract te
22e0: 78 74 2c 20 69 6e 74 65 67 65 72 2c 20 6f 72 20  xt, integer, or 
22f0: 62 6c 6f 62 20 76 61 6c 75 65 73 20 66 72 6f 6d  blob values from
2300: 20 74 68 65 20 4e 2d 74 68 20 63 6f 6c 75 6d 6e   the N-th column
2310: 20 6f 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65   of the.** curre
2320: 6e 74 20 72 6f 77 2e 0a 2a 2f 0a 69 6e 74 20 64  nt row..*/.int d
2330: 62 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 53  b_column_bytes(S
2340: 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
2350: 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  N){.  return sql
2360: 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
2370: 73 28 70 53 74 6d 74 2d 3e 70 53 74 6d 74 2c 20  s(pStmt->pStmt, 
2380: 4e 29 3b 0a 7d 0a 69 6e 74 20 64 62 5f 63 6f 6c  N);.}.int db_col
2390: 75 6d 6e 5f 69 6e 74 28 53 74 6d 74 20 2a 70 53  umn_int(Stmt *pS
23a0: 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72  tmt, int N){.  r
23b0: 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 63 6f  eturn sqlite3_co
23c0: 6c 75 6d 6e 5f 69 6e 74 28 70 53 74 6d 74 2d 3e  lumn_int(pStmt->
23d0: 70 53 74 6d 74 2c 20 4e 29 3b 0a 7d 0a 69 36 34  pStmt, N);.}.i64
23e0: 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34   db_column_int64
23f0: 28 53 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  (Stmt *pStmt, in
2400: 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  t N){.  return s
2410: 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
2420: 74 36 34 28 70 53 74 6d 74 2d 3e 70 53 74 6d 74  t64(pStmt->pStmt
2430: 2c 20 4e 29 3b 0a 7d 0a 64 6f 75 62 6c 65 20 64  , N);.}.double d
2440: 62 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28  b_column_double(
2450: 53 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  Stmt *pStmt, int
2460: 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71   N){.  return sq
2470: 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75  lite3_column_dou
2480: 62 6c 65 28 70 53 74 6d 74 2d 3e 70 53 74 6d 74  ble(pStmt->pStmt
2490: 2c 20 4e 29 3b 0a 7d 0a 63 6f 6e 73 74 20 63 68  , N);.}.const ch
24a0: 61 72 20 2a 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65  ar *db_column_te
24b0: 78 74 28 53 74 6d 74 20 2a 70 53 74 6d 74 2c 20  xt(Stmt *pStmt, 
24c0: 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e  int N){.  return
24d0: 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f   (char*)sqlite3_
24e0: 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 70 53 74 6d  column_text(pStm
24f0: 74 2d 3e 70 53 74 6d 74 2c 20 4e 29 3b 0a 7d 0a  t->pStmt, N);.}.
2500: 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 62 5f 63  const char *db_c
2510: 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 53 74 6d 74 20  olumn_name(Stmt 
2520: 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a  *pStmt, int N){.
2530: 20 20 72 65 74 75 72 6e 20 28 63 68 61 72 2a 29    return (char*)
2540: 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
2550: 61 6d 65 28 70 53 74 6d 74 2d 3e 70 53 74 6d 74  ame(pStmt->pStmt
2560: 2c 20 4e 29 3b 0a 7d 0a 69 6e 74 20 64 62 5f 63  , N);.}.int db_c
2570: 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 53 74 6d 74  olumn_count(Stmt
2580: 20 2a 70 53 74 6d 74 29 7b 0a 20 20 72 65 74 75   *pStmt){.  retu
2590: 72 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  rn sqlite3_colum
25a0: 6e 5f 63 6f 75 6e 74 28 70 53 74 6d 74 2d 3e 70  n_count(pStmt->p
25b0: 53 74 6d 74 29 3b 0a 7d 0a 63 68 61 72 20 2a 64  Stmt);.}.char *d
25c0: 62 5f 63 6f 6c 75 6d 6e 5f 6d 61 6c 6c 6f 63 28  b_column_malloc(
25d0: 53 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  Stmt *pStmt, int
25e0: 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 6d 70   N){.  return mp
25f0: 72 69 6e 74 66 28 22 25 73 22 2c 20 64 62 5f 63  rintf("%s", db_c
2600: 6f 6c 75 6d 6e 5f 74 65 78 74 28 70 53 74 6d 74  olumn_text(pStmt
2610: 2c 20 4e 29 29 3b 0a 7d 0a 76 6f 69 64 20 64 62  , N));.}.void db
2620: 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 53 74 6d  _column_blob(Stm
2630: 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 2c  t *pStmt, int N,
2640: 20 42 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20   Blob *pBlob){. 
2650: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 28 70 42 6c   blob_append(pBl
2660: 6f 62 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ob, sqlite3_colu
2670: 6d 6e 5f 62 6c 6f 62 28 70 53 74 6d 74 2d 3e 70  mn_blob(pStmt->p
2680: 53 74 6d 74 2c 20 4e 29 2c 0a 20 20 20 20 20 20  Stmt, N),.      
2690: 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
26a0: 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 53 74  column_bytes(pSt
26b0: 6d 74 2d 3e 70 53 74 6d 74 2c 20 4e 29 29 3b 0a  mt->pStmt, N));.
26c0: 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c  }../*.** Initial
26d0: 69 7a 65 20 61 20 62 6c 6f 62 20 74 6f 20 61 6e  ize a blob to an
26e0: 20 65 70 68 65 72 6d 65 72 61 6c 20 63 6f 70 79   ephermeral copy
26f0: 20 6f 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 20   of the content 
2700: 6f 66 20 61 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69  of a.** column i
2710: 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f  n the current ro
2720: 77 2e 20 20 54 68 65 20 64 61 74 61 20 69 6e 20  w.  The data in 
2730: 74 68 65 20 62 6c 6f 62 20 77 69 6c 6c 20 62 65  the blob will be
2740: 63 6f 6d 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 20  come.** invalid 
2750: 77 68 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65  when the stateme
2760: 6e 74 20 69 73 20 73 74 65 70 70 65 64 20 6f 72  nt is stepped or
2770: 20 72 65 73 65 74 2e 0a 2a 2f 0a 76 6f 69 64 20   reset..*/.void 
2780: 64 62 5f 65 70 68 65 6d 65 72 61 6c 5f 62 6c 6f  db_ephemeral_blo
2790: 62 28 53 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  b(Stmt *pStmt, i
27a0: 6e 74 20 4e 2c 20 42 6c 6f 62 20 2a 70 42 6c 6f  nt N, Blob *pBlo
27b0: 62 29 7b 0a 20 20 62 6c 6f 62 5f 69 6e 69 74 28  b){.  blob_init(
27c0: 70 42 6c 6f 62 2c 20 73 71 6c 69 74 65 33 5f 63  pBlob, sqlite3_c
27d0: 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 53 74 6d 74  olumn_blob(pStmt
27e0: 2d 3e 70 53 74 6d 74 2c 20 4e 29 2c 0a 20 20 20  ->pStmt, N),.   
27f0: 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2800: 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
2810: 70 53 74 6d 74 2d 3e 70 53 74 6d 74 2c 20 4e 29  pStmt->pStmt, N)
2820: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  );.}../*.** Chec
2830: 6b 20 61 20 72 65 73 75 6c 74 20 63 6f 64 65 2e  k a result code.
2840: 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 53    If it is not S
2850: 51 4c 49 54 45 5f 4f 4b 2c 20 70 72 69 6e 74 20  QLITE_OK, print 
2860: 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e  the.** correspon
2870: 64 69 6e 67 20 65 72 72 6f 72 20 6d 65 73 73 61  ding error messa
2880: 67 65 20 61 6e 64 20 65 78 69 74 2e 0a 2a 2f 0a  ge and exit..*/.
2890: 76 6f 69 64 20 64 62 5f 63 68 65 63 6b 5f 72 65  void db_check_re
28a0: 73 75 6c 74 28 69 6e 74 20 72 63 29 7b 0a 20 20  sult(int rc){.  
28b0: 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
28c0: 4b 20 29 7b 0a 20 20 20 20 64 62 5f 65 72 72 28  K ){.    db_err(
28d0: 22 53 51 4c 20 65 72 72 6f 72 3a 20 25 73 22 2c  "SQL error: %s",
28e0: 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28   sqlite3_errmsg(
28f0: 67 2e 64 62 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  g.db));.  }.}../
2900: 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 61 20 73  *.** Execute a s
2910: 69 6e 67 6c 65 20 70 72 65 70 61 72 65 64 20 73  ingle prepared s
2920: 74 61 74 65 6d 65 6e 74 20 75 6e 74 69 6c 20 69  tatement until i
2930: 74 20 66 69 6e 69 73 68 65 73 2e 0a 2a 2f 0a 69  t finishes..*/.i
2940: 6e 74 20 64 62 5f 65 78 65 63 28 53 74 6d 74 20  nt db_exec(Stmt 
2950: 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72  *pStmt){.  int r
2960: 63 3b 0a 20 20 77 68 69 6c 65 28 20 28 72 63 20  c;.  while( (rc 
2970: 3d 20 64 62 5f 73 74 65 70 28 70 53 74 6d 74 29  = db_step(pStmt)
2980: 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  )==SQLITE_ROW ){
2990: 7d 0a 20 20 72 63 20 3d 20 64 62 5f 72 65 73 65  }.  rc = db_rese
29a0: 74 28 70 53 74 6d 74 29 3b 0a 20 20 64 62 5f 63  t(pStmt);.  db_c
29b0: 68 65 63 6b 5f 72 65 73 75 6c 74 28 72 63 29 3b  heck_result(rc);
29c0: 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
29d0: 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 6d  ./*.** Execute m
29e0: 75 6c 74 69 70 6c 65 20 53 51 4c 20 73 74 61 74  ultiple SQL stat
29f0: 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 69 6e 74 20 64  ements..*/.int d
2a00: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 63 6f 6e  b_multi_exec(con
2a10: 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 2e  st char *zSql, .
2a20: 2e 2e 29 7b 0a 20 20 42 6c 6f 62 20 73 71 6c 3b  ..){.  Blob sql;
2a30: 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 76 61 5f  .  int rc;.  va_
2a40: 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20  list ap;.  char 
2a50: 2a 7a 45 72 72 20 3d 20 30 3b 0a 20 20 62 6c 6f  *zErr = 0;.  blo
2a60: 62 5f 69 6e 69 74 28 26 73 71 6c 2c 20 30 2c 20  b_init(&sql, 0, 
2a70: 30 29 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61  0);.  va_start(a
2a80: 70 2c 20 7a 53 71 6c 29 3b 0a 20 20 62 6c 6f 62  p, zSql);.  blob
2a90: 5f 76 61 70 70 65 6e 64 66 28 26 73 71 6c 2c 20  _vappendf(&sql, 
2aa0: 7a 53 71 6c 2c 20 61 70 29 3b 0a 20 20 76 61 5f  zSql, ap);.  va_
2ab0: 65 6e 64 28 61 70 29 3b 0a 20 20 72 63 20 3d 20  end(ap);.  rc = 
2ac0: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 67 2e 64  sqlite3_exec(g.d
2ad0: 62 2c 20 62 6c 6f 62 5f 62 75 66 66 65 72 28 26  b, blob_buffer(&
2ae0: 73 71 6c 29 2c 20 30 2c 20 30 2c 20 26 7a 45 72  sql), 0, 0, &zEr
2af0: 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  r);.  if( rc!=SQ
2b00: 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 64  LITE_OK ){.    d
2b10: 62 5f 65 72 72 28 22 25 73 5c 6e 25 73 22 2c 20  b_err("%s\n%s", 
2b20: 7a 45 72 72 2c 20 62 6c 6f 62 5f 62 75 66 66 65  zErr, blob_buffe
2b30: 72 28 26 73 71 6c 29 29 3b 0a 20 20 7d 0a 20 20  r(&sql));.  }.  
2b40: 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 71 6c 29  blob_reset(&sql)
2b50: 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
2b60: 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20  ../*.** Execute 
2b70: 61 20 71 75 65 72 79 20 61 6e 64 20 72 65 74 75  a query and retu
2b80: 72 6e 20 61 20 73 69 6e 67 6c 65 20 69 6e 74 65  rn a single inte
2b90: 67 65 72 20 76 61 6c 75 65 2e 0a 2a 2f 0a 69 36  ger value..*/.i6
2ba0: 34 20 64 62 5f 69 6e 74 36 34 28 69 36 34 20 69  4 db_int64(i64 i
2bb0: 44 66 6c 74 2c 20 63 6f 6e 73 74 20 63 68 61 72  Dflt, const char
2bc0: 20 2a 7a 53 71 6c 2c 20 2e 2e 2e 29 7b 0a 20 20   *zSql, ...){.  
2bd0: 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 53 74  va_list ap;.  St
2be0: 6d 74 20 73 3b 0a 20 20 69 36 34 20 72 63 3b 0a  mt s;.  i64 rc;.
2bf0: 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a    va_start(ap, z
2c00: 53 71 6c 29 3b 0a 20 20 64 62 5f 76 70 72 65 70  Sql);.  db_vprep
2c10: 61 72 65 28 26 73 2c 20 7a 53 71 6c 2c 20 61 70  are(&s, zSql, ap
2c20: 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b  );.  va_end(ap);
2c30: 0a 20 20 69 66 28 20 64 62 5f 73 74 65 70 28 26  .  if( db_step(&
2c40: 73 29 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  s)!=SQLITE_ROW )
2c50: 7b 0a 20 20 20 20 72 63 20 3d 20 69 44 66 6c 74  {.    rc = iDflt
2c60: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
2c70: 63 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e  c = db_column_in
2c80: 74 36 34 28 26 73 2c 20 30 29 3b 0a 20 20 7d 0a  t64(&s, 0);.  }.
2c90: 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73    db_finalize(&s
2ca0: 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
2cb0: 7d 0a 69 6e 74 20 64 62 5f 69 6e 74 28 69 6e 74  }.int db_int(int
2cc0: 20 69 44 66 6c 74 2c 20 63 6f 6e 73 74 20 63 68   iDflt, const ch
2cd0: 61 72 20 2a 7a 53 71 6c 2c 20 2e 2e 2e 29 7b 0a  ar *zSql, ...){.
2ce0: 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20    va_list ap;.  
2cf0: 53 74 6d 74 20 73 3b 0a 20 20 69 6e 74 20 72 63  Stmt s;.  int rc
2d00: 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c  ;.  va_start(ap,
2d10: 20 7a 53 71 6c 29 3b 0a 20 20 64 62 5f 76 70 72   zSql);.  db_vpr
2d20: 65 70 61 72 65 28 26 73 2c 20 7a 53 71 6c 2c 20  epare(&s, zSql, 
2d30: 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70  ap);.  va_end(ap
2d40: 29 3b 0a 20 20 69 66 28 20 64 62 5f 73 74 65 70  );.  if( db_step
2d50: 28 26 73 29 21 3d 53 51 4c 49 54 45 5f 52 4f 57  (&s)!=SQLITE_ROW
2d60: 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 69 44 66   ){.    rc = iDf
2d70: 6c 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  lt;.  }else{.   
2d80: 20 72 63 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f   rc = db_column_
2d90: 69 6e 74 28 26 73 2c 20 30 29 3b 0a 20 20 7d 0a  int(&s, 0);.  }.
2da0: 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73    db_finalize(&s
2db0: 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
2dc0: 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
2dd0: 54 52 55 45 20 69 66 20 74 68 65 20 71 75 65 72  TRUE if the quer
2de0: 79 20 77 6f 75 6c 64 20 72 65 74 75 72 6e 20 31  y would return 1
2df0: 20 6f 72 20 6d 6f 72 65 20 72 6f 77 73 2e 20 20   or more rows.  
2e00: 52 65 74 75 72 6e 0a 2a 2a 20 46 41 4c 53 45 20  Return.** FALSE 
2e10: 69 66 20 74 68 65 20 71 75 65 72 79 20 72 65 73  if the query res
2e20: 75 6c 74 20 77 6f 75 6c 64 20 62 65 20 61 6e 20  ult would be an 
2e30: 65 6d 70 74 79 20 73 65 74 2e 0a 2a 2f 0a 69 6e  empty set..*/.in
2e40: 74 20 64 62 5f 65 78 69 73 74 73 28 63 6f 6e 73  t db_exists(cons
2e50: 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 2e 2e  t char *zSql, ..
2e60: 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70  .){.  va_list ap
2e70: 3b 0a 20 20 53 74 6d 74 20 73 3b 0a 20 20 69 6e  ;.  Stmt s;.  in
2e80: 74 20 72 63 3b 0a 20 20 76 61 5f 73 74 61 72 74  t rc;.  va_start
2e90: 28 61 70 2c 20 7a 53 71 6c 29 3b 0a 20 20 64 62  (ap, zSql);.  db
2ea0: 5f 76 70 72 65 70 61 72 65 28 26 73 2c 20 7a 53  _vprepare(&s, zS
2eb0: 71 6c 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e  ql, ap);.  va_en
2ec0: 64 28 61 70 29 3b 0a 20 20 69 66 28 20 64 62 5f  d(ap);.  if( db_
2ed0: 73 74 65 70 28 26 73 29 21 3d 53 51 4c 49 54 45  step(&s)!=SQLITE
2ee0: 5f 52 4f 57 20 29 7b 0a 20 20 20 20 72 63 20 3d  _ROW ){.    rc =
2ef0: 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
2f00: 20 72 63 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 64   rc = 1;.  }.  d
2f10: 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73 29 3b 0a  b_finalize(&s);.
2f20: 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
2f30: 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 61  ./*.** Execute a
2f40: 20 71 75 65 72 79 20 61 6e 64 20 72 65 74 75 72   query and retur
2f50: 6e 20 61 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69  n a floating-poi
2f60: 6e 74 20 76 61 6c 75 65 2e 0a 2a 2f 0a 64 6f 75  nt value..*/.dou
2f70: 62 6c 65 20 64 62 5f 64 6f 75 62 6c 65 28 64 6f  ble db_double(do
2f80: 75 62 6c 65 20 72 44 66 6c 74 2c 20 63 6f 6e 73  uble rDflt, cons
2f90: 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 2e 2e  t char *zSql, ..
2fa0: 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70  .){.  va_list ap
2fb0: 3b 0a 20 20 53 74 6d 74 20 73 3b 0a 20 20 64 6f  ;.  Stmt s;.  do
2fc0: 75 62 6c 65 20 72 3b 0a 20 20 76 61 5f 73 74 61  uble r;.  va_sta
2fd0: 72 74 28 61 70 2c 20 7a 53 71 6c 29 3b 0a 20 20  rt(ap, zSql);.  
2fe0: 64 62 5f 76 70 72 65 70 61 72 65 28 26 73 2c 20  db_vprepare(&s, 
2ff0: 7a 53 71 6c 2c 20 61 70 29 3b 0a 20 20 76 61 5f  zSql, ap);.  va_
3000: 65 6e 64 28 61 70 29 3b 0a 20 20 69 66 28 20 64  end(ap);.  if( d
3010: 62 5f 73 74 65 70 28 26 73 29 21 3d 53 51 4c 49  b_step(&s)!=SQLI
3020: 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 72 20  TE_ROW ){.    r 
3030: 3d 20 72 44 66 6c 74 3b 0a 20 20 7d 65 6c 73 65  = rDflt;.  }else
3040: 7b 0a 20 20 20 20 72 20 3d 20 64 62 5f 63 6f 6c  {.    r = db_col
3050: 75 6d 6e 5f 64 6f 75 62 6c 65 28 26 73 2c 20 30  umn_double(&s, 0
3060: 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61  );.  }.  db_fina
3070: 6c 69 7a 65 28 26 73 29 3b 0a 20 20 72 65 74 75  lize(&s);.  retu
3080: 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  rn r;.}../*.** E
3090: 78 65 63 75 74 65 20 61 20 71 75 65 72 79 20 61  xecute a query a
30a0: 6e 64 20 61 70 70 65 6e 64 20 74 68 65 20 66 69  nd append the fi
30b0: 72 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  rst column of th
30c0: 65 20 66 69 72 73 74 20 72 6f 77 0a 2a 2a 20 6f  e first row.** o
30d0: 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  f the result set
30e0: 20 74 6f 20 62 6c 6f 62 20 67 69 76 65 6e 20 69   to blob given i
30f0: 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  n the first argu
3100: 6d 65 6e 74 2e 0a 2a 2f 0a 76 6f 69 64 20 64 62  ment..*/.void db
3110: 5f 62 6c 6f 62 28 42 6c 6f 62 20 2a 70 52 65 73  _blob(Blob *pRes
3120: 75 6c 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  ult, const char 
3130: 2a 7a 53 71 6c 2c 20 2e 2e 2e 29 7b 0a 20 20 76  *zSql, ...){.  v
3140: 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 53 74 6d  a_list ap;.  Stm
3150: 74 20 73 3b 0a 20 20 76 61 5f 73 74 61 72 74 28  t s;.  va_start(
3160: 61 70 2c 20 7a 53 71 6c 29 3b 0a 20 20 64 62 5f  ap, zSql);.  db_
3170: 76 70 72 65 70 61 72 65 28 26 73 2c 20 7a 53 71  vprepare(&s, zSq
3180: 6c 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64  l, ap);.  va_end
3190: 28 61 70 29 3b 0a 20 20 69 66 28 20 64 62 5f 73  (ap);.  if( db_s
31a0: 74 65 70 28 26 73 29 3d 3d 53 51 4c 49 54 45 5f  tep(&s)==SQLITE_
31b0: 52 4f 57 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f  ROW ){.    blob_
31c0: 61 70 70 65 6e 64 28 70 52 65 73 75 6c 74 2c 20  append(pResult, 
31d0: 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
31e0: 6c 6f 62 28 73 2e 70 53 74 6d 74 2c 20 30 29 2c  lob(s.pStmt, 0),
31f0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3200: 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
3210: 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73  3_column_bytes(s
3220: 2e 70 53 74 6d 74 2c 20 30 29 29 3b 0a 20 20 7d  .pStmt, 0));.  }
3230: 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26  .  db_finalize(&
3240: 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65  s);.}../*.** Exe
3250: 63 75 74 65 20 61 20 71 75 65 72 79 2e 20 20 52  cute a query.  R
3260: 65 74 75 72 6e 20 74 68 65 20 66 69 72 73 74 20  eturn the first 
3270: 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 66 69  column of the fi
3280: 72 73 74 20 72 6f 77 0a 2a 2a 20 6f 66 20 74 68  rst row.** of th
3290: 65 20 72 65 73 75 6c 74 20 73 65 74 20 61 73 20  e result set as 
32a0: 61 20 73 74 72 69 6e 67 2e 20 20 53 70 61 63 65  a string.  Space
32b0: 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 73 74 72   to hold the str
32c0: 69 6e 67 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e  ing is.** obtain
32d0: 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29  ed from malloc()
32e0: 2e 20 20 49 66 20 74 68 65 20 72 65 73 75 6c 74  .  If the result
32f0: 20 73 65 74 20 69 73 20 65 6d 70 74 79 2c 20 72   set is empty, r
3300: 65 74 75 72 6e 0a 2a 2a 20 7a 44 65 66 61 75 6c  eturn.** zDefaul
3310: 74 20 69 6e 73 74 65 61 64 2e 0a 2a 2f 0a 63 68  t instead..*/.ch
3320: 61 72 20 2a 64 62 5f 74 65 78 74 28 63 68 61 72  ar *db_text(char
3330: 20 2a 7a 44 65 66 61 75 6c 74 2c 20 63 6f 6e 73   *zDefault, cons
3340: 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 2e 2e  t char *zSql, ..
3350: 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70  .){.  va_list ap
3360: 3b 0a 20 20 53 74 6d 74 20 73 3b 0a 20 20 63 68  ;.  Stmt s;.  ch
3370: 61 72 20 2a 7a 20 3d 20 7a 44 65 66 61 75 6c 74  ar *z = zDefault
3380: 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c  ;.  va_start(ap,
3390: 20 7a 53 71 6c 29 3b 0a 20 20 64 62 5f 76 70 72   zSql);.  db_vpr
33a0: 65 70 61 72 65 28 26 73 2c 20 7a 53 71 6c 2c 20  epare(&s, zSql, 
33b0: 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70  ap);.  va_end(ap
33c0: 29 3b 0a 20 20 69 66 28 20 64 62 5f 73 74 65 70  );.  if( db_step
33d0: 28 26 73 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  (&s)==SQLITE_ROW
33e0: 20 29 7b 0a 20 20 20 20 7a 20 3d 20 6d 70 72 69   ){.    z = mpri
33f0: 6e 74 66 28 22 25 73 22 2c 20 73 71 6c 69 74 65  ntf("%s", sqlite
3400: 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 2e  3_column_text(s.
3410: 70 53 74 6d 74 2c 20 30 29 29 3b 0a 20 20 7d 0a  pStmt, 0));.  }.
3420: 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73    db_finalize(&s
3430: 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d  );.  return z;.}
3440: 0a 0a 23 69 66 64 65 66 20 5f 5f 4d 49 4e 47 57  ..#ifdef __MINGW
3450: 33 32 5f 5f 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65  32__./*.** These
3460: 20 72 6f 75 74 69 6e 65 73 20 28 63 6f 70 69 65   routines (copie
3470: 64 20 6f 75 74 20 6f 66 20 74 68 65 20 6f 73 5f  d out of the os_
3480: 77 69 6e 2e 63 20 64 72 69 76 65 72 20 66 6f 72  win.c driver for
3490: 20 53 51 4c 69 74 65 29 20 63 6f 6e 76 65 72 74   SQLite) convert
34a0: 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 20 73 74  .** character st
34b0: 72 69 6e 67 73 20 69 6e 20 76 61 72 69 6f 75 73  rings in various
34c0: 20 6d 69 63 72 6f 73 6f 66 74 20 6d 75 6c 74 69   microsoft multi
34d0: 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20  -byte character 
34e0: 66 6f 72 6d 61 74 73 0a 2a 2a 20 69 6e 74 6f 20  formats.** into 
34f0: 55 54 46 2d 38 2e 20 20 46 6f 73 73 69 6c 20 61  UTF-8.  Fossil a
3500: 6e 64 20 53 51 4c 69 74 65 20 61 6c 77 61 79 73  nd SQLite always
3510: 20 75 73 65 20 6f 6e 6c 79 20 55 54 46 2d 38 20   use only UTF-8 
3520: 69 6e 74 65 72 6e 61 6c 6c 79 2e 20 20 54 68 65  internally.  The
3530: 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 61  se.** routines a
3540: 72 65 20 6e 65 65 64 65 64 20 69 6e 20 6f 72 64  re needed in ord
3550: 65 72 20 74 6f 20 63 6f 6e 76 65 72 74 20 66 72  er to convert fr
3560: 6f 6d 20 74 68 65 20 64 65 66 61 75 6c 74 20 63  om the default c
3570: 68 61 72 61 63 74 65 72 20 73 65 74 0a 2a 2a 20  haracter set.** 
3580: 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65  currently in use
3590: 20 62 79 20 77 69 6e 64 6f 77 73 20 69 6e 74 6f   by windows into
35a0: 20 55 54 46 2d 38 20 77 68 65 6e 20 73 74 72 69   UTF-8 when stri
35b0: 6e 67 73 20 61 72 65 20 69 6d 70 6f 72 74 65 64  ngs are imported
35c0: 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 6f 75 74   from.** the out
35d0: 73 69 64 65 20 77 6f 72 6c 64 2e 0a 2a 2f 0a 2f  side world..*/./
35e0: 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 69 63  *.** Convert mic
35f0: 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 20 74  rosoft unicode t
3600: 6f 20 55 54 46 2d 38 2e 20 20 53 70 61 63 65 20  o UTF-8.  Space 
3610: 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75  to hold the retu
3620: 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 0a 2a  rned string is.*
3630: 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  * obtained from 
3640: 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 43 6f 70  malloc()..** Cop
3650: 69 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ied from sqlite3
3660: 2e 63 20 61 73 20 69 73 20 28 70 65 74 72 29 0a  .c as is (petr).
3670: 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a  */.static char *
3680: 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 63 6f  unicodeToUtf8(co
3690: 6e 73 74 20 57 43 48 41 52 20 2a 7a 57 69 64 65  nst WCHAR *zWide
36a0: 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74  Filename){.  int
36b0: 20 6e 42 79 74 65 3b 0a 20 20 63 68 61 72 20 2a   nByte;.  char *
36c0: 7a 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20 20 6e 42  zFilename;..  nB
36d0: 79 74 65 20 3d 20 57 69 64 65 43 68 61 72 54 6f  yte = WideCharTo
36e0: 4d 75 6c 74 69 42 79 74 65 28 43 50 5f 55 54 46  MultiByte(CP_UTF
36f0: 38 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c 65 6e  8, 0, zWideFilen
3700: 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 30 2c 20 30  ame, -1, 0, 0, 0
3710: 2c 20 30 29 3b 0a 20 20 7a 46 69 6c 65 6e 61 6d  , 0);.  zFilenam
3720: 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74  e = malloc( nByt
3730: 65 20 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65  e );.  if( zFile
3740: 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72  name==0 ){.    r
3750: 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e  eturn 0;.  }.  n
3760: 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 72 54  Byte = WideCharT
3770: 6f 4d 75 6c 74 69 42 79 74 65 28 43 50 5f 55 54  oMultiByte(CP_UT
3780: 46 38 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c 65  F8, 0, zWideFile
3790: 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46 69 6c 65 6e  name, -1, zFilen
37a0: 61 6d 65 2c 20 6e 42 79 74 65 2c 0a 20 20 20 20  ame, nByte,.    
37b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37c0: 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 29 3b            0, 0);
37d0: 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3d 3d 20  .  if( nByte == 
37e0: 30 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 46  0 ){.    free(zF
37f0: 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 46  ilename);.    zF
3800: 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d  ilename = 0;.  }
3810: 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e  .  return zFilen
3820: 61 6d 65 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 43 6f 6e  ame;.}./*.** Con
3830: 76 65 72 74 20 61 6e 20 61 6e 73 69 20 73 74 72  vert an ansi str
3840: 69 6e 67 20 74 6f 20 6d 69 63 72 6f 73 6f 66 74  ing to microsoft
3850: 20 75 6e 69 63 6f 64 65 2c 20 62 61 73 65 64 20   unicode, based 
3860: 6f 6e 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e  on the.** curren
3870: 74 20 63 6f 64 65 70 61 67 65 20 73 65 74 74 69  t codepage setti
3880: 6e 67 73 20 66 6f 72 20 66 69 6c 65 20 61 70 69  ngs for file api
3890: 73 2e 0a 2a 2a 20 0a 2a 2a 20 53 70 61 63 65 20  s..** .** Space 
38a0: 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75  to hold the retu
38b0: 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f  rned string is o
38c0: 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20  btained.** from 
38d0: 6d 61 6c 6c 6f 63 2e 0a 2a 2f 0a 73 74 61 74 69  malloc..*/.stati
38e0: 63 20 57 43 48 41 52 20 2a 6d 62 63 73 54 6f 55  c WCHAR *mbcsToU
38f0: 6e 69 63 6f 64 65 28 63 6f 6e 73 74 20 63 68 61  nicode(const cha
3900: 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20  r *zFilename){. 
3910: 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 57 43   int nByte;.  WC
3920: 48 41 52 20 2a 7a 4d 62 63 73 46 69 6c 65 6e 61  HAR *zMbcsFilena
3930: 6d 65 3b 0a 20 20 69 6e 74 20 63 6f 64 65 70 61  me;.  int codepa
3940: 67 65 20 3d 20 43 50 5f 41 43 50 3b 0a 0a 20 20  ge = CP_ACP;..  
3950: 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69 42 79 74  nByte = MultiByt
3960: 65 54 6f 57 69 64 65 43 68 61 72 28 63 6f 64 65  eToWideChar(code
3970: 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65 6e 61  page, 0, zFilena
3980: 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 30 29 2a  me, -1, NULL,0)*
3990: 73 69 7a 65 6f 66 28 57 43 48 41 52 29 3b 0a 20  sizeof(WCHAR);. 
39a0: 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 20 3d   zMbcsFilename =
39b0: 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2a 73   malloc( nByte*s
39c0: 69 7a 65 6f 66 28 7a 4d 62 63 73 46 69 6c 65 6e  izeof(zMbcsFilen
39d0: 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28  ame[0]) );.  if(
39e0: 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 3d 3d   zMbcsFilename==
39f0: 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
3a00: 30 3b 0a 20 20 7d 0a 0a 20 20 6e 42 79 74 65 20  0;.  }..  nByte 
3a10: 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 64  = MultiByteToWid
3a20: 65 43 68 61 72 28 63 6f 64 65 70 61 67 65 2c 20  eChar(codepage, 
3a30: 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31  0, zFilename, -1
3a40: 2c 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 2c  , zMbcsFilename,
3a50: 20 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 6e   nByte);.  if( n
3a60: 42 79 74 65 3d 3d 30 20 29 7b 0a 20 20 20 20 66  Byte==0 ){.    f
3a70: 72 65 65 28 7a 4d 62 63 73 46 69 6c 65 6e 61 6d  ree(zMbcsFilenam
3a80: 65 29 3b 0a 20 20 20 20 7a 4d 62 63 73 46 69 6c  e);.    zMbcsFil
3a90: 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20  ename = 0;.  }. 
3aa0: 20 72 65 74 75 72 6e 20 7a 4d 62 63 73 46 69 6c   return zMbcsFil
3ab0: 65 6e 61 6d 65 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 43  ename;.}./*.** C
3ac0: 6f 6e 76 65 72 74 20 6d 75 6c 74 69 62 79 74 65  onvert multibyte
3ad0: 20 63 68 61 72 61 63 74 65 72 20 73 74 72 69 6e   character strin
3ae0: 67 20 74 6f 20 55 54 46 2d 38 2e 20 20 53 70 61  g to UTF-8.  Spa
3af0: 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a  ce to hold the.*
3b00: 2a 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e  * returned strin
3b10: 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72  g is obtained fr
3b20: 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a  om malloc()..*/.
3b30: 73 74 61 74 69 63 20 63 68 61 72 20 2a 6d 62 63  static char *mbc
3b40: 73 54 6f 55 74 66 38 28 63 6f 6e 73 74 20 63 68  sToUtf8(const ch
3b50: 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a  ar *zFilename){.
3b60: 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d    char *zFilenam
3b70: 65 55 74 66 38 3b 0a 20 20 57 43 48 41 52 20 2a  eUtf8;.  WCHAR *
3b80: 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20 7a 54 6d  zTmpWide;..  zTm
3b90: 70 57 69 64 65 20 3d 20 6d 62 63 73 54 6f 55 6e  pWide = mbcsToUn
3ba0: 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29  icode(zFilename)
3bb0: 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57 69 64 65  ;.  if( zTmpWide
3bc0: 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
3bd0: 6e 20 30 3b 0a 20 20 7d 0a 20 20 0a 20 20 7a 46  n 0;.  }.  .  zF
3be0: 69 6c 65 6e 61 6d 65 55 74 66 38 20 3d 20 75 6e  ilenameUtf8 = un
3bf0: 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 6d 70  icodeToUtf8(zTmp
3c00: 57 69 64 65 29 3b 0a 20 20 66 72 65 65 28 7a 54  Wide);.  free(zT
3c10: 6d 70 57 69 64 65 29 3b 0a 20 20 72 65 74 75 72  mpWide);.  retur
3c20: 6e 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 3b  n zFilenameUtf8;
3c30: 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 5f 4d  .}.#endif /* __M
3c40: 49 4e 47 57 33 32 5f 5f 20 2a 2f 0a 0a 0a 2f 2a  INGW32__ */.../*
3c50: 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61  .** Initialize a
3c60: 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 66 69   new database fi
3c70: 6c 65 20 77 69 74 68 20 74 68 65 20 67 69 76 65  le with the give
3c80: 6e 20 73 63 68 65 6d 61 2e 20 20 49 66 20 61 6e  n schema.  If an
3c90: 79 74 68 69 6e 67 0a 2a 2a 20 67 6f 65 73 20 77  ything.** goes w
3ca0: 72 6f 6e 67 2c 20 63 61 6c 6c 20 64 62 5f 65 72  rong, call db_er
3cb0: 72 28 29 20 74 6f 20 65 78 69 74 2e 0a 2a 2f 0a  r() to exit..*/.
3cc0: 76 6f 69 64 20 64 62 5f 69 6e 69 74 5f 64 61 74  void db_init_dat
3cd0: 61 62 61 73 65 28 0a 20 20 63 6f 6e 73 74 20 63  abase(.  const c
3ce0: 68 61 72 20 2a 7a 46 69 6c 65 4e 61 6d 65 2c 20  har *zFileName, 
3cf0: 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74    /* Name of dat
3d00: 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 63 72  abase file to cr
3d10: 65 61 74 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  eate */.  const 
3d20: 63 68 61 72 20 2a 7a 53 63 68 65 6d 61 2c 20 20  char *zSchema,  
3d30: 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 72 74     /* First part
3d40: 20 6f 66 20 73 63 68 65 6d 61 20 2a 2f 0a 20 20   of schema */.  
3d50: 2e 2e 2e 20 20 20 20 20 20 20 20 20 20 20 20 20  ...             
3d60: 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 69           /* Addi
3d70: 74 69 6f 6e 61 6c 20 53 51 4c 20 74 6f 20 72 75  tional SQL to ru
3d80: 6e 2e 20 20 54 65 72 6d 69 6e 61 74 65 20 77 69  n.  Terminate wi
3d90: 74 68 20 4e 55 4c 4c 2e 20 2a 2f 0a 29 7b 0a 20  th NULL. */.){. 
3da0: 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20   sqlite3 *db;.  
3db0: 69 6e 74 20 72 63 3b 0a 20 20 63 6f 6e 73 74 20  int rc;.  const 
3dc0: 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 76 61  char *zSql;.  va
3dd0: 5f 6c 69 73 74 20 61 70 3b 0a 0a 23 69 66 64 65  _list ap;..#ifde
3de0: 66 20 5f 5f 4d 49 4e 47 57 33 32 5f 5f 0a 20 20  f __MINGW32__.  
3df0: 7a 46 69 6c 65 4e 61 6d 65 20 3d 20 6d 62 63 73  zFileName = mbcs
3e00: 54 6f 55 74 66 38 28 7a 46 69 6c 65 4e 61 6d 65  ToUtf8(zFileName
3e10: 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 63 20 3d  );.#endif.  rc =
3e20: 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 7a 46   sqlite3_open(zF
3e30: 69 6c 65 4e 61 6d 65 2c 20 26 64 62 29 3b 0a 20  ileName, &db);. 
3e40: 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
3e50: 4f 4b 20 29 7b 0a 20 20 20 20 64 62 5f 65 72 72  OK ){.    db_err
3e60: 28 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28  (sqlite3_errmsg(
3e70: 64 62 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  db));.  }.  sqli
3e80: 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74  te3_busy_timeout
3e90: 28 64 62 2c 20 35 30 30 30 29 3b 0a 20 20 73 71  (db, 5000);.  sq
3ea0: 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 22  lite3_exec(db, "
3eb0: 42 45 47 49 4e 20 45 58 43 4c 55 53 49 56 45 22  BEGIN EXCLUSIVE"
3ec0: 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 72 63  , 0, 0, 0);.  rc
3ed0: 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28   = sqlite3_exec(
3ee0: 64 62 2c 20 7a 53 63 68 65 6d 61 2c 20 30 2c 20  db, zSchema, 0, 
3ef0: 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21  0, 0);.  if( rc!
3f00: 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
3f10: 20 20 64 62 5f 65 72 72 28 73 71 6c 69 74 65 33    db_err(sqlite3
3f20: 5f 65 72 72 6d 73 67 28 64 62 29 29 3b 0a 20 20  _errmsg(db));.  
3f30: 7d 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c  }.  va_start(ap,
3f40: 20 7a 53 63 68 65 6d 61 29 3b 0a 20 20 77 68 69   zSchema);.  whi
3f50: 6c 65 28 20 28 7a 53 71 6c 20 3d 20 76 61 5f 61  le( (zSql = va_a
3f60: 72 67 28 61 70 2c 20 63 6f 6e 73 74 20 63 68 61  rg(ap, const cha
3f70: 72 2a 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 72  r*))!=0 ){.    r
3f80: 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  c = sqlite3_exec
3f90: 28 64 62 2c 20 7a 53 71 6c 2c 20 30 2c 20 30 2c  (db, zSql, 0, 0,
3fa0: 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21   0);.    if( rc!
3fb0: 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
3fc0: 20 20 20 20 64 62 5f 65 72 72 28 73 71 6c 69 74      db_err(sqlit
3fd0: 65 33 5f 65 72 72 6d 73 67 28 64 62 29 29 3b 0a  e3_errmsg(db));.
3fe0: 20 20 20 20 7d 0a 20 20 7d 0a 20 20 76 61 5f 65      }.  }.  va_e
3ff0: 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 65  nd(ap);.  sqlite
4000: 33 5f 65 78 65 63 28 64 62 2c 20 22 43 4f 4d 4d  3_exec(db, "COMM
4010: 49 54 22 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  IT", 0, 0, 0);. 
4020: 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 64   sqlite3_close(d
4030: 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65  b);.}../*.** Ope
4040: 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c  n a database fil
4050: 65 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69  e.  Return a poi
4060: 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20  nter to the new 
4070: 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e  database.** conn
4080: 65 63 74 69 6f 6e 2e 20 20 41 6e 20 65 72 72 6f  ection.  An erro
4090: 72 20 72 65 73 75 6c 74 73 20 69 6e 20 70 72 6f  r results in pro
40a0: 63 65 73 73 20 61 62 6f 72 74 2e 0a 2a 2f 0a 73  cess abort..*/.s
40b0: 74 61 74 69 63 20 73 71 6c 69 74 65 33 20 2a 6f  tatic sqlite3 *o
40c0: 70 65 6e 44 61 74 61 62 61 73 65 28 63 6f 6e 73  penDatabase(cons
40d0: 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 29  t char *zDbName)
40e0: 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 63 6f  {.  int rc;.  co
40f0: 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73 3b 0a  nst char *zVfs;.
4100: 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a    sqlite3 *db;..
4110: 20 20 7a 56 66 73 20 3d 20 67 65 74 65 6e 76 28    zVfs = getenv(
4120: 22 46 4f 53 53 49 4c 5f 56 46 53 22 29 3b 0a 23  "FOSSIL_VFS");.#
4130: 69 66 64 65 66 20 5f 5f 4d 49 4e 47 57 33 32 5f  ifdef __MINGW32_
4140: 5f 0a 20 20 7a 44 62 4e 61 6d 65 20 3d 20 6d 62  _.  zDbName = mb
4150: 63 73 54 6f 55 74 66 38 28 7a 44 62 4e 61 6d 65  csToUtf8(zDbName
4160: 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 63 20 3d  );.#endif.  rc =
4170: 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32   sqlite3_open_v2
4180: 28 0a 20 20 20 20 20 20 20 7a 44 62 4e 61 6d 65  (.       zDbName
4190: 2c 20 26 64 62 2c 0a 20 20 20 20 20 20 20 53 51  , &db,.       SQ
41a0: 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
41b0: 49 54 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45  ITE | SQLITE_OPE
41c0: 4e 5f 43 52 45 41 54 45 2c 0a 20 20 20 20 20 20  N_CREATE,.      
41d0: 20 7a 56 66 73 0a 20 20 29 3b 0a 20 20 69 66 28   zVfs.  );.  if(
41e0: 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
41f0: 7b 0a 20 20 20 20 64 62 5f 65 72 72 28 73 71 6c  {.    db_err(sql
4200: 69 74 65 33 5f 65 72 72 6d 73 67 28 64 62 29 29  ite3_errmsg(db))
4210: 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
4220: 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 64 62 2c  busy_timeout(db,
4230: 20 35 30 30 30 29 3b 0a 20 20 72 65 74 75 72 6e   5000);.  return
4240: 20 64 62 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 7a   db;.}.../*.** z
4250: 44 62 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61  DbName is the na
4260: 6d 65 20 6f 66 20 61 20 64 61 74 61 62 61 73 65  me of a database
4270: 20 66 69 6c 65 2e 20 20 49 66 20 6e 6f 20 6f 74   file.  If no ot
4280: 68 65 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  her database.** 
4290: 66 69 6c 65 20 69 73 20 6f 70 65 6e 2c 20 74 68  file is open, th
42a0: 65 6e 20 6f 70 65 6e 20 74 68 69 73 20 6f 6e 65  en open this one
42b0: 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 64 61  .  If another da
42c0: 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 0a 2a  tabase file is.*
42d0: 2a 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 2c 20  * already open, 
42e0: 74 68 65 6e 20 61 74 74 61 63 68 20 7a 44 62 4e  then attach zDbN
42f0: 61 6d 65 20 75 73 69 6e 67 20 74 68 65 20 6e 61  ame using the na
4300: 6d 65 20 7a 4c 61 62 65 6c 2e 0a 2a 2f 0a 76 6f  me zLabel..*/.vo
4310: 69 64 20 64 62 5f 6f 70 65 6e 5f 6f 72 5f 61 74  id db_open_or_at
4320: 74 61 63 68 28 63 6f 6e 73 74 20 63 68 61 72 20  tach(const char 
4330: 2a 7a 44 62 4e 61 6d 65 2c 20 63 6f 6e 73 74 20  *zDbName, const 
4340: 63 68 61 72 20 2a 7a 4c 61 62 65 6c 29 7b 0a 20  char *zLabel){. 
4350: 20 69 66 28 20 21 67 2e 64 62 20 29 7b 0a 20 20   if( !g.db ){.  
4360: 20 20 67 2e 64 62 20 3d 20 6f 70 65 6e 44 61 74    g.db = openDat
4370: 61 62 61 73 65 28 7a 44 62 4e 61 6d 65 29 3b 0a  abase(zDbName);.
4380: 20 20 20 20 64 62 5f 63 6f 6e 6e 65 63 74 69 6f      db_connectio
4390: 6e 5f 69 6e 69 74 28 29 3b 0a 20 20 7d 65 6c 73  n_init();.  }els
43a0: 65 7b 0a 23 69 66 64 65 66 20 5f 5f 4d 49 4e 47  e{.#ifdef __MING
43b0: 57 33 32 5f 5f 0a 20 20 20 20 7a 44 62 4e 61 6d  W32__.    zDbNam
43c0: 65 20 3d 20 6d 62 63 73 54 6f 55 74 66 38 28 7a  e = mbcsToUtf8(z
43d0: 44 62 4e 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a  DbName);.#endif.
43e0: 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65      db_multi_exe
43f0: 63 28 22 41 54 54 41 43 48 20 44 41 54 41 42 41  c("ATTACH DATABA
4400: 53 45 20 25 51 20 41 53 20 25 73 22 2c 20 7a 44  SE %Q AS %s", zD
4410: 62 4e 61 6d 65 2c 20 7a 4c 61 62 65 6c 29 3b 0a  bName, zLabel);.
4420: 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65    }.}../*.** Ope
4430: 6e 20 74 68 65 20 75 73 65 72 20 64 61 74 61 62  n the user datab
4440: 61 73 65 20 69 6e 20 22 7e 2f 2e 66 6f 73 73 69  ase in "~/.fossi
4450: 6c 22 2e 20 20 43 72 65 61 74 65 20 74 68 65 20  l".  Create the 
4460: 64 61 74 61 62 61 73 65 20 61 6e 65 77 20 69 66  database anew if
4470: 0a 2a 2a 20 69 74 20 64 6f 65 73 20 6e 6f 74 20  .** it does not 
4480: 61 6c 72 65 61 64 79 20 65 78 69 73 74 2e 0a 2a  already exist..*
4490: 2a 0a 2a 2a 20 49 66 20 74 68 65 20 75 73 65 41  *.** If the useA
44a0: 74 74 61 63 68 20 66 6c 61 67 20 69 73 20 30 20  ttach flag is 0 
44b0: 28 74 68 65 20 75 73 75 61 6c 20 63 61 73 65 29  (the usual case)
44c0: 20 74 68 65 6e 20 74 68 65 20 75 73 65 72 20 64   then the user d
44d0: 61 74 61 62 61 73 65 20 69 73 0a 2a 2a 20 6f 70  atabase is.** op
44e0: 65 6e 65 64 20 6f 6e 20 61 20 73 65 70 61 72 61  ened on a separa
44f0: 74 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  te database conn
4500: 65 63 74 69 6f 6e 20 67 2e 64 62 43 6f 6e 66 69  ection g.dbConfi
4510: 67 2e 20 20 54 68 69 73 20 70 72 65 76 65 6e 74  g.  This prevent
4520: 73 0a 2a 2a 20 74 68 65 20 7e 2f 2e 66 6f 73 73  s.** the ~/.foss
4530: 69 6c 20 64 61 74 61 62 61 73 65 20 66 72 6f 6d  il database from
4540: 20 62 65 63 6f 6d 69 6e 67 20 6c 6f 63 6b 65 64   becoming locked
4550: 20 6f 6e 20 6c 6f 6e 67 20 63 68 65 63 6b 2d 69   on long check-i
4560: 6e 20 6f 72 20 73 79 6e 63 0a 2a 2a 20 6f 70 65  n or sync.** ope
4570: 72 61 74 69 6f 6e 73 20 77 68 69 63 68 20 68 6f  rations which ho
4580: 6c 64 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20  ld an exclusive 
4590: 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49 6e  transaction.  In
45a0: 20 61 20 66 65 77 20 63 61 73 65 73 2c 20 74 68   a few cases, th
45b0: 6f 75 67 68 2c 0a 2a 2a 20 69 74 20 69 73 20 63  ough,.** it is c
45c0: 6f 6e 76 65 6e 69 65 6e 74 20 66 6f 72 20 74 68  onvenient for th
45d0: 65 20 7e 2f 2e 66 6f 73 73 69 6c 20 74 6f 20 62  e ~/.fossil to b
45e0: 65 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68  e attached to th
45f0: 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 0a  e main database.
4600: 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 73 6f  ** connection so
4610: 20 74 68 61 74 20 77 65 20 63 61 6e 20 6a 6f 69   that we can joi
4620: 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 76 61  n between the va
4630: 72 69 6f 75 73 20 64 61 74 61 62 61 73 65 73 2e  rious databases.
4640: 20 20 49 6e 20 74 68 61 74 0a 2a 2a 20 63 61 73    In that.** cas
4650: 65 2c 20 69 6e 76 6f 6b 65 20 74 68 69 73 20 72  e, invoke this r
4660: 6f 75 74 69 6e 65 20 77 69 74 68 20 75 73 65 41  outine with useA
4670: 74 74 61 63 68 20 61 73 20 31 2e 0a 2a 2f 0a 76  ttach as 1..*/.v
4680: 6f 69 64 20 64 62 5f 6f 70 65 6e 5f 63 6f 6e 66  oid db_open_conf
4690: 69 67 28 69 6e 74 20 75 73 65 41 74 74 61 63 68  ig(int useAttach
46a0: 29 7b 0a 20 20 63 68 61 72 20 2a 7a 44 62 4e 61  ){.  char *zDbNa
46b0: 6d 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  me;.  const char
46c0: 20 2a 7a 48 6f 6d 65 3b 0a 20 20 69 66 28 20 67   *zHome;.  if( g
46d0: 2e 63 6f 6e 66 69 67 4f 70 65 6e 20 29 20 72 65  .configOpen ) re
46e0: 74 75 72 6e 3b 0a 23 69 66 64 65 66 20 5f 5f 4d  turn;.#ifdef __M
46f0: 49 4e 47 57 33 32 5f 5f 0a 20 20 7a 48 6f 6d 65  INGW32__.  zHome
4700: 20 3d 20 67 65 74 65 6e 76 28 22 4c 4f 43 41 4c   = getenv("LOCAL
4710: 41 50 50 44 41 54 41 22 29 3b 0a 20 20 69 66 28  APPDATA");.  if(
4720: 20 7a 48 6f 6d 65 3d 3d 30 20 29 7b 0a 20 20 20   zHome==0 ){.   
4730: 20 7a 48 6f 6d 65 20 3d 20 67 65 74 65 6e 76 28   zHome = getenv(
4740: 22 41 50 50 44 41 54 41 22 29 3b 0a 20 20 20 20  "APPDATA");.    
4750: 69 66 28 20 7a 48 6f 6d 65 3d 3d 30 20 29 7b 0a  if( zHome==0 ){.
4760: 20 20 20 20 20 20 7a 48 6f 6d 65 20 3d 20 67 65        zHome = ge
4770: 74 65 6e 76 28 22 48 4f 4d 45 50 41 54 48 22 29  tenv("HOMEPATH")
4780: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
4790: 28 20 7a 48 6f 6d 65 3d 3d 30 20 29 7b 0a 20 20  ( zHome==0 ){.  
47a0: 20 20 64 62 5f 65 72 72 28 22 63 61 6e 6e 6f 74    db_err("cannot
47b0: 20 6c 6f 63 61 74 65 20 68 6f 6d 65 20 64 69 72   locate home dir
47c0: 65 63 74 6f 72 79 20 2d 20 22 0a 20 20 20 20 20  ectory - ".     
47d0: 20 20 20 20 20 20 22 70 6c 65 61 73 65 20 73 65        "please se
47e0: 74 20 74 68 65 20 48 4f 4d 45 50 41 54 48 20 65  t the HOMEPATH e
47f0: 6e 76 69 72 6f 6e 6d 65 6e 74 20 76 61 72 69 61  nvironment varia
4800: 62 6c 65 22 29 3b 0a 20 20 7d 0a 23 65 6c 73 65  ble");.  }.#else
4810: 0a 20 20 7a 48 6f 6d 65 20 3d 20 67 65 74 65 6e  .  zHome = geten
4820: 76 28 22 48 4f 4d 45 22 29 3b 0a 20 20 69 66 28  v("HOME");.  if(
4830: 20 7a 48 6f 6d 65 3d 3d 30 20 29 7b 0a 20 20 20   zHome==0 ){.   
4840: 20 64 62 5f 65 72 72 28 22 63 61 6e 6e 6f 74 20   db_err("cannot 
4850: 6c 6f 63 61 74 65 20 68 6f 6d 65 20 64 69 72 65  locate home dire
4860: 63 74 6f 72 79 20 2d 20 22 0a 20 20 20 20 20 20  ctory - ".      
4870: 20 20 20 20 20 22 70 6c 65 61 73 65 20 73 65 74       "please set
4880: 20 74 68 65 20 48 4f 4d 45 20 65 6e 76 69 72 6f   the HOME enviro
4890: 6e 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 22 29  nment variable")
48a0: 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66  ;.  }.#endif.#if
48b0: 64 65 66 20 5f 5f 4d 49 4e 47 57 33 32 5f 5f 0a  def __MINGW32__.
48c0: 20 20 2f 2a 20 2e 20 66 69 6c 65 6e 61 6d 65 73    /* . filenames
48d0: 20 67 69 76 65 20 73 6f 6d 65 20 77 69 6e 64 6f   give some windo
48e0: 77 20 73 79 73 74 65 6d 73 20 70 72 6f 62 6c 65  w systems proble
48f0: 6d 73 20 61 6e 64 20 6d 61 6e 79 20 61 70 70 73  ms and many apps
4900: 20 70 72 6f 62 6c 65 6d 73 20 2a 2f 0a 20 20 7a   problems */.  z
4910: 44 62 4e 61 6d 65 20 3d 20 6d 70 72 69 6e 74 66  DbName = mprintf
4920: 28 22 25 2f 2f 5f 66 6f 73 73 69 6c 22 2c 20 7a  ("%//_fossil", z
4930: 48 6f 6d 65 29 3b 0a 23 65 6c 73 65 0a 20 20 7a  Home);.#else.  z
4940: 44 62 4e 61 6d 65 20 3d 20 6d 70 72 69 6e 74 66  DbName = mprintf
4950: 28 22 25 73 2f 2e 66 6f 73 73 69 6c 22 2c 20 7a  ("%s/.fossil", z
4960: 48 6f 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20  Home);.#endif.  
4970: 69 66 28 20 66 69 6c 65 5f 73 69 7a 65 28 7a 44  if( file_size(zD
4980: 62 4e 61 6d 65 29 3c 31 30 32 34 2a 33 20 29 7b  bName)<1024*3 ){
4990: 0a 20 20 20 20 64 62 5f 69 6e 69 74 5f 64 61 74  .    db_init_dat
49a0: 61 62 61 73 65 28 7a 44 62 4e 61 6d 65 2c 20 7a  abase(zDbName, z
49b0: 43 6f 6e 66 69 67 53 63 68 65 6d 61 2c 20 28 63  ConfigSchema, (c
49c0: 68 61 72 2a 29 30 29 3b 0a 20 20 7d 0a 20 20 67  har*)0);.  }.  g
49d0: 2e 75 73 65 41 74 74 61 63 68 20 3d 20 75 73 65  .useAttach = use
49e0: 41 74 74 61 63 68 3b 0a 20 20 69 66 28 20 75 73  Attach;.  if( us
49f0: 65 41 74 74 61 63 68 20 29 7b 0a 20 20 20 20 64  eAttach ){.    d
4a00: 62 5f 6f 70 65 6e 5f 6f 72 5f 61 74 74 61 63 68  b_open_or_attach
4a10: 28 7a 44 62 4e 61 6d 65 2c 20 22 63 6f 6e 66 69  (zDbName, "confi
4a20: 67 64 62 22 29 3b 0a 20 20 20 20 67 2e 64 62 43  gdb");.    g.dbC
4a30: 6f 6e 66 69 67 20 3d 20 30 3b 0a 20 20 7d 65 6c  onfig = 0;.  }el
4a40: 73 65 7b 0a 20 20 20 20 67 2e 64 62 43 6f 6e 66  se{.    g.dbConf
4a50: 69 67 20 3d 20 6f 70 65 6e 44 61 74 61 62 61 73  ig = openDatabas
4a60: 65 28 7a 44 62 4e 61 6d 65 29 3b 0a 20 20 7d 0a  e(zDbName);.  }.
4a70: 20 20 67 2e 63 6f 6e 66 69 67 4f 70 65 6e 20 3d    g.configOpen =
4a80: 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20   1;.}../*.** If 
4a90: 7a 44 62 4e 61 6d 65 20 69 73 20 61 20 76 61 6c  zDbName is a val
4aa0: 69 64 20 6c 6f 63 61 6c 20 64 61 74 61 62 61 73  id local databas
4ab0: 65 20 66 69 6c 65 2c 20 6f 70 65 6e 20 69 74 20  e file, open it 
4ac0: 61 6e 64 20 72 65 74 75 72 6e 0a 2a 2a 20 74 72  and return.** tr
4ad0: 75 65 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f  ue.  If it is no
4ae0: 74 20 61 20 76 61 6c 69 64 20 6c 6f 63 61 6c 20  t a valid local 
4af0: 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 72  database file, r
4b00: 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74  eturn 0..*/.stat
4b10: 69 63 20 69 6e 74 20 69 73 56 61 6c 69 64 4c 6f  ic int isValidLo
4b20: 63 61 6c 44 62 28 63 6f 6e 73 74 20 63 68 61 72  calDb(const char
4b30: 20 2a 7a 44 62 4e 61 6d 65 29 7b 0a 20 20 69 36   *zDbName){.  i6
4b40: 34 20 6c 73 69 7a 65 3b 0a 20 20 69 6e 74 20 72  4 lsize;.  int r
4b50: 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  c;.  sqlite3_stm
4b60: 74 20 2a 70 53 74 6d 74 3b 0a 0a 20 20 69 66 28  t *pStmt;..  if(
4b70: 20 61 63 63 65 73 73 28 7a 44 62 4e 61 6d 65 2c   access(zDbName,
4b80: 20 46 5f 4f 4b 29 20 29 20 72 65 74 75 72 6e 20   F_OK) ) return 
4b90: 30 3b 0a 20 20 6c 73 69 7a 65 20 3d 20 66 69 6c  0;.  lsize = fil
4ba0: 65 5f 73 69 7a 65 28 7a 44 62 4e 61 6d 65 29 3b  e_size(zDbName);
4bb0: 0a 20 20 69 66 28 20 6c 73 69 7a 65 25 31 30 32  .  if( lsize%102
4bc0: 34 21 3d 30 20 7c 7c 20 6c 73 69 7a 65 3c 34 30  4!=0 || lsize<40
4bd0: 39 36 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  96 ) return 0;. 
4be0: 20 64 62 5f 6f 70 65 6e 5f 6f 72 5f 61 74 74 61   db_open_or_atta
4bf0: 63 68 28 7a 44 62 4e 61 6d 65 2c 20 22 6c 6f 63  ch(zDbName, "loc
4c00: 61 6c 64 62 22 29 3b 0a 20 20 67 2e 6c 6f 63 61  aldb");.  g.loca
4c10: 6c 4f 70 65 6e 20 3d 20 31 3b 0a 20 20 64 62 5f  lOpen = 1;.  db_
4c20: 6f 70 65 6e 5f 63 6f 6e 66 69 67 28 30 29 3b 0a  open_config(0);.
4c30: 20 20 64 62 5f 6f 70 65 6e 5f 72 65 70 6f 73 69    db_open_reposi
4c40: 74 6f 72 79 28 30 29 3b 0a 0a 20 20 2f 2a 20 49  tory(0);..  /* I
4c50: 66 20 74 68 65 20 22 6d 74 69 6d 65 22 20 63 6f  f the "mtime" co
4c60: 6c 75 6d 6e 20 69 73 20 6d 69 73 73 69 6e 67 20  lumn is missing 
4c70: 66 72 6f 6d 20 74 68 65 20 76 66 69 6c 65 20 74  from the vfile t
4c80: 61 62 6c 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20  able, then.  ** 
4c90: 61 64 64 20 69 74 20 6e 6f 77 2e 20 20 20 54 68  add it now.   Th
4ca0: 69 73 20 63 6f 64 65 20 61 64 64 65 64 20 6f 6e  is code added on
4cb0: 20 32 30 30 38 2d 31 32 2d 30 36 2e 20 20 41 66   2008-12-06.  Af
4cc0: 74 65 72 20 61 6c 6c 20 75 73 65 72 73 20 68 61  ter all users ha
4cd0: 76 65 0a 20 20 2a 2a 20 75 70 67 72 61 64 65 64  ve.  ** upgraded
4ce0: 2c 20 74 68 69 73 20 63 6f 64 65 20 63 61 6e 20  , this code can 
4cf0: 62 65 20 73 61 66 65 6c 79 20 64 65 6c 65 74 65  be safely delete
4d00: 64 2e 20 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20  d. .  */.  rc = 
4d10: 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
4d20: 67 2e 64 62 2c 20 22 53 45 4c 45 43 54 20 6d 74  g.db, "SELECT mt
4d30: 69 6d 65 20 46 52 4f 4d 20 76 66 69 6c 65 22 2c  ime FROM vfile",
4d40: 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b   -1, &pStmt, 0);
4d50: 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  .  sqlite3_final
4d60: 69 7a 65 28 70 53 74 6d 74 29 3b 0a 20 20 69 66  ize(pStmt);.  if
4d70: 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 52 52  ( rc==SQLITE_ERR
4d80: 4f 52 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OR ){.    sqlite
4d90: 33 5f 65 78 65 63 28 67 2e 64 62 2c 20 22 41 4c  3_exec(g.db, "AL
4da0: 54 45 52 20 54 41 42 4c 45 20 76 66 69 6c 65 20  TER TABLE vfile 
4db0: 41 44 44 20 43 4f 4c 55 4d 4e 20 6d 74 69 6d 65  ADD COLUMN mtime
4dc0: 20 49 4e 54 45 47 45 52 22 2c 20 30 2c 20 30 2c   INTEGER", 0, 0,
4dd0: 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49   0);.  }..  /* I
4de0: 66 20 74 68 65 20 22 6f 72 69 67 6e 61 6d 65 22  f the "origname"
4df0: 20 63 6f 6c 75 6d 6e 20 69 73 20 6d 69 73 73 69   column is missi
4e00: 6e 67 20 66 72 6f 6d 20 74 68 65 20 76 66 69 6c  ng from the vfil
4e10: 65 20 74 61 62 6c 65 2c 20 74 68 65 6e 0a 20 20  e table, then.  
4e20: 2a 2a 20 61 64 64 20 69 74 20 6e 6f 77 2e 20 20  ** add it now.  
4e30: 20 54 68 69 73 20 63 6f 64 65 20 61 64 64 65 64   This code added
4e40: 20 6f 6e 20 32 30 30 38 2d 31 31 2d 30 39 2e 20   on 2008-11-09. 
4e50: 20 41 66 74 65 72 20 61 6c 6c 20 75 73 65 72 73   After all users
4e60: 20 68 61 76 65 0a 20 20 2a 2a 20 75 70 67 72 61   have.  ** upgra
4e70: 64 65 64 2c 20 74 68 69 73 20 63 6f 64 65 20 63  ded, this code c
4e80: 61 6e 20 62 65 20 73 61 66 65 6c 79 20 64 65 6c  an be safely del
4e90: 65 74 65 64 2e 20 0a 20 20 2a 2f 0a 20 20 72 63  eted. .  */.  rc
4ea0: 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61   = sqlite3_prepa
4eb0: 72 65 28 67 2e 64 62 2c 20 22 53 45 4c 45 43 54  re(g.db, "SELECT
4ec0: 20 6f 72 69 67 6e 61 6d 65 20 46 52 4f 4d 20 76   origname FROM v
4ed0: 66 69 6c 65 22 2c 20 2d 31 2c 20 26 70 53 74 6d  file", -1, &pStm
4ee0: 74 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  t, 0);.  sqlite3
4ef0: 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29  _finalize(pStmt)
4f00: 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
4f10: 54 45 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  TE_ERROR ){.    
4f20: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 67 2e 64  sqlite3_exec(g.d
4f30: 62 2c 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20  b, "ALTER TABLE 
4f40: 76 66 69 6c 65 20 41 44 44 20 43 4f 4c 55 4d 4e  vfile ADD COLUMN
4f50: 20 6f 72 69 67 6e 61 6d 65 20 54 45 58 54 22 2c   origname TEXT",
4f60: 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 0a   0, 0, 0);.  }..
4f70: 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f    return 1;.}../
4f80: 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65 20  *.** Locate the 
4f90: 72 6f 6f 74 20 64 69 72 65 63 74 6f 72 79 20 6f  root directory o
4fa0: 66 20 74 68 65 20 6c 6f 63 61 6c 20 72 65 70 6f  f the local repo
4fb0: 73 69 74 6f 72 79 20 74 72 65 65 2e 20 20 54 68  sitory tree.  Th
4fc0: 65 20 72 6f 6f 74 0a 2a 2a 20 64 69 72 65 63 74  e root.** direct
4fd0: 6f 72 79 20 69 73 20 66 6f 75 6e 64 20 62 79 20  ory is found by 
4fe0: 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 61 20  searching for a 
4ff0: 66 69 6c 65 20 6e 61 6d 65 64 20 22 5f 46 4f 53  file named "_FOS
5000: 53 49 4c 5f 22 20 6f 72 20 22 2e 66 6f 73 22 0a  SIL_" or ".fos".
5010: 2a 2a 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  ** that contains
5020: 20 61 20 76 61 6c 69 64 20 72 65 70 6f 73 69 74   a valid reposit
5030: 6f 72 79 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a  ory database..**
5040: 0a 2a 2a 20 49 66 20 6e 6f 20 76 61 6c 69 64 20  .** If no valid 
5050: 5f 46 4f 53 53 49 4c 5f 20 6f 72 20 2e 66 6f 73  _FOSSIL_ or .fos
5060: 20 66 69 6c 65 20 69 73 20 66 6f 75 6e 64 2c 20   file is found, 
5070: 77 65 20 6d 6f 76 65 20 75 70 20 6f 6e 65 20 6c  we move up one l
5080: 65 76 65 6c 20 61 6e 64 20 0a 2a 2a 20 74 72 79  evel and .** try
5090: 20 61 67 61 69 6e 2e 20 4f 6e 63 65 20 74 68 65   again. Once the
50a0: 20 66 69 6c 65 20 69 73 20 66 6f 75 6e 64 2c 20   file is found, 
50b0: 74 68 65 20 67 2e 7a 4c 6f 63 61 6c 52 6f 6f 74  the g.zLocalRoot
50c0: 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74   variable is set
50d0: 0a 2a 2a 20 74 6f 20 74 68 65 20 72 6f 6f 74 20  .** to the root 
50e0: 6f 66 20 74 68 65 20 72 65 70 6f 73 69 74 6f 72  of the repositor
50f0: 79 20 74 72 65 65 20 61 6e 64 20 74 68 69 73 20  y tree and this 
5100: 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
5110: 31 2e 20 20 49 66 0a 2a 2a 20 6e 6f 20 64 61 74  1.  If.** no dat
5120: 61 62 61 73 65 20 69 73 20 66 6f 75 6e 64 2c 20  abase is found, 
5130: 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  then this routin
5140: 65 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a  e return 0..**.*
5150: 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
5160: 6c 77 61 79 73 20 6f 70 65 6e 73 20 74 68 65 20  lways opens the 
5170: 75 73 65 72 20 64 61 74 61 62 61 73 65 20 72 65  user database re
5180: 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74  gardless of whet
5190: 68 65 72 20 6f 72 0a 2a 2a 20 6e 6f 74 20 74 68  her or.** not th
51a0: 65 20 72 65 70 6f 73 69 74 6f 72 79 20 64 61 74  e repository dat
51b0: 61 62 61 73 65 20 69 73 20 66 6f 75 6e 64 2e 20  abase is found. 
51c0: 20 49 66 20 74 68 65 20 5f 46 4f 53 53 49 4c 5f   If the _FOSSIL_
51d0: 20 6f 72 20 2e 66 6f 73 20 66 69 6c 65 0a 2a 2a   or .fos file.**
51e0: 20 69 73 20 66 6f 75 6e 64 2c 20 69 74 20 69 73   is found, it is
51f0: 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68 65   attached to the
5200: 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 63   open database c
5210: 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 6f 2e 0a 2a  onnection too..*
5220: 2f 0a 69 6e 74 20 64 62 5f 6f 70 65 6e 5f 6c 6f  /.int db_open_lo
5230: 63 61 6c 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74  cal(void){.  int
5240: 20 69 2c 20 6e 3b 0a 20 20 63 68 61 72 20 7a 50   i, n;.  char zP
5250: 77 64 5b 32 30 30 30 5d 3b 0a 20 20 63 68 61 72  wd[2000];.  char
5260: 20 2a 7a 50 77 64 43 6f 6e 76 3b 0a 20 20 73 74   *zPwdConv;.  st
5270: 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
5280: 2a 61 44 62 4e 61 6d 65 5b 5d 20 3d 20 7b 20 22  *aDbName[] = { "
5290: 2f 5f 46 4f 53 53 49 4c 5f 22 2c 20 22 2f 2e 66  /_FOSSIL_", "/.f
52a0: 6f 73 22 20 7d 3b 0a 20 20 0a 20 20 69 66 28 20  os" };.  .  if( 
52b0: 67 2e 6c 6f 63 61 6c 4f 70 65 6e 29 20 72 65 74  g.localOpen) ret
52c0: 75 72 6e 20 31 3b 0a 20 20 69 66 28 20 67 65 74  urn 1;.  if( get
52d0: 63 77 64 28 7a 50 77 64 2c 20 73 69 7a 65 6f 66  cwd(zPwd, sizeof
52e0: 28 7a 50 77 64 29 2d 32 30 29 3d 3d 30 20 29 7b  (zPwd)-20)==0 ){
52f0: 0a 20 20 20 20 64 62 5f 65 72 72 28 22 70 77 64  .    db_err("pwd
5300: 20 74 6f 6f 20 62 69 67 3a 20 6d 61 78 20 25 64   too big: max %d
5310: 22 2c 20 73 69 7a 65 6f 66 28 7a 50 77 64 29 2d  ", sizeof(zPwd)-
5320: 32 30 29 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20 73  20);.  }.  n = s
5330: 74 72 6c 65 6e 28 7a 50 77 64 29 3b 0a 20 20 7a  trlen(zPwd);.  z
5340: 50 77 64 43 6f 6e 76 20 3d 20 6d 70 72 69 6e 74  PwdConv = mprint
5350: 66 28 22 25 2f 22 2c 20 7a 50 77 64 29 3b 0a 20  f("%/", zPwd);. 
5360: 20 73 74 72 6e 63 70 79 28 7a 50 77 64 2c 20 7a   strncpy(zPwd, z
5370: 50 77 64 43 6f 6e 76 2c 20 32 30 30 30 2d 32 30  PwdConv, 2000-20
5380: 29 3b 0a 20 20 66 72 65 65 28 7a 50 77 64 43 6f  );.  free(zPwdCo
5390: 6e 76 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e 3e  nv);.  while( n>
53a0: 30 20 29 7b 0a 20 20 20 20 69 66 28 20 61 63 63  0 ){.    if( acc
53b0: 65 73 73 28 7a 50 77 64 2c 20 57 5f 4f 4b 29 20  ess(zPwd, W_OK) 
53c0: 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 66 6f 72  ) break;.    for
53d0: 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61  (i=0; i<sizeof(a
53e0: 44 62 4e 61 6d 65 29 2f 73 69 7a 65 6f 66 28 61  DbName)/sizeof(a
53f0: 44 62 4e 61 6d 65 5b 30 5d 29 3b 20 69 2b 2b 29  DbName[0]); i++)
5400: 7b 0a 20 20 20 20 20 20 73 74 72 63 70 79 28 26  {.      strcpy(&
5410: 7a 50 77 64 5b 6e 5d 2c 20 61 44 62 4e 61 6d 65  zPwd[n], aDbName
5420: 5b 69 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  [i]);.      if( 
5430: 69 73 56 61 6c 69 64 4c 6f 63 61 6c 44 62 28 7a  isValidLocalDb(z
5440: 50 77 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20  Pwd) ){.        
5450: 2f 2a 20 46 6f 75 6e 64 20 61 20 76 61 6c 69 64  /* Found a valid
5460: 20 63 68 65 63 6b 6f 75 74 20 64 61 74 61 62 61   checkout databa
5470: 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 20  se file */.     
5480: 20 20 20 7a 50 77 64 5b 6e 5d 20 3d 20 30 3b 0a     zPwd[n] = 0;.
5490: 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 6e          while( n
54a0: 3e 31 20 26 26 20 7a 50 77 64 5b 6e 2d 31 5d 3d  >1 && zPwd[n-1]=
54b0: 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='/' ){.        
54c0: 20 20 6e 2d 2d 3b 0a 20 20 20 20 20 20 20 20 20    n--;.         
54d0: 20 7a 50 77 64 5b 6e 5d 20 3d 20 30 3b 0a 20 20   zPwd[n] = 0;.  
54e0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
54f0: 67 2e 7a 4c 6f 63 61 6c 52 6f 6f 74 20 3d 20 6d  g.zLocalRoot = m
5500: 70 72 69 6e 74 66 28 22 25 73 2f 22 2c 20 7a 50  printf("%s/", zP
5510: 77 64 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  wd);.        ret
5520: 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 1;.      }. 
5530: 20 20 20 7d 0a 20 20 20 20 6e 2d 2d 3b 0a 20 20     }.    n--;.  
5540: 20 20 77 68 69 6c 65 28 20 6e 3e 30 20 26 26 20    while( n>0 && 
5550: 7a 50 77 64 5b 6e 5d 21 3d 27 2f 27 20 29 7b 20  zPwd[n]!='/' ){ 
5560: 6e 2d 2d 3b 20 7d 0a 20 20 20 20 77 68 69 6c 65  n--; }.    while
5570: 28 20 6e 3e 30 20 26 26 20 7a 50 77 64 5b 6e 2d  ( n>0 && zPwd[n-
5580: 31 5d 3d 3d 27 2f 27 20 29 7b 20 6e 2d 2d 3b 20  1]=='/' ){ n--; 
5590: 7d 0a 20 20 20 20 7a 50 77 64 5b 6e 5d 20 3d 20  }.    zPwd[n] = 
55a0: 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 20 63  0;.  }..  /* A c
55b0: 68 65 63 6b 6f 75 74 20 64 61 74 61 62 61 73 65  heckout database
55c0: 20 66 69 6c 65 20 63 6f 75 6c 64 20 6e 6f 74 20   file could not 
55d0: 62 65 20 66 6f 75 6e 64 20 2a 2f 0a 20 20 72 65  be found */.  re
55e0: 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
55f0: 20 4f 70 65 6e 20 74 68 65 20 72 65 70 6f 73 69   Open the reposi
5600: 74 6f 72 79 20 64 61 74 61 62 61 73 65 20 67 69  tory database gi
5610: 76 65 6e 20 62 79 20 7a 44 62 4e 61 6d 65 2e 20  ven by zDbName. 
5620: 20 49 66 20 7a 44 62 4e 61 6d 65 3d 3d 4e 55 4c   If zDbName==NUL
5630: 4c 20 74 68 65 6e 0a 2a 2a 20 67 65 74 20 74 68  L then.** get th
5640: 65 20 6e 61 6d 65 20 66 72 6f 6d 20 74 68 65 20  e name from the 
5650: 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 6c 6f 63  already open loc
5660: 61 6c 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a  al database..*/.
5670: 76 6f 69 64 20 64 62 5f 6f 70 65 6e 5f 72 65 70  void db_open_rep
5680: 6f 73 69 74 6f 72 79 28 63 6f 6e 73 74 20 63 68  ository(const ch
5690: 61 72 20 2a 7a 44 62 4e 61 6d 65 29 7b 0a 20 20  ar *zDbName){.  
56a0: 69 66 28 20 67 2e 72 65 70 6f 73 69 74 6f 72 79  if( g.repository
56b0: 4f 70 65 6e 20 29 20 72 65 74 75 72 6e 3b 0a 20  Open ) return;. 
56c0: 20 69 66 28 20 7a 44 62 4e 61 6d 65 3d 3d 30 20   if( zDbName==0 
56d0: 29 7b 0a 20 20 20 20 69 66 28 20 67 2e 6c 6f 63  ){.    if( g.loc
56e0: 61 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20 20 20  alOpen ){.      
56f0: 7a 44 62 4e 61 6d 65 20 3d 20 64 62 5f 6c 67 65  zDbName = db_lge
5700: 74 28 22 72 65 70 6f 73 69 74 6f 72 79 22 2c 20  t("repository", 
5710: 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  0);.    }.    if
5720: 28 20 7a 44 62 4e 61 6d 65 3d 3d 30 20 29 7b 0a  ( zDbName==0 ){.
5730: 20 20 20 20 20 20 64 62 5f 65 72 72 28 22 75 6e        db_err("un
5740: 61 62 6c 65 20 74 6f 20 66 69 6e 64 20 74 68 65  able to find the
5750: 20 6e 61 6d 65 20 6f 66 20 61 20 72 65 70 6f 73   name of a repos
5760: 69 74 6f 72 79 20 64 61 74 61 62 61 73 65 22 29  itory database")
5770: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
5780: 28 20 61 63 63 65 73 73 28 7a 44 62 4e 61 6d 65  ( access(zDbName
5790: 2c 20 52 5f 4f 4b 29 20 7c 7c 20 66 69 6c 65 5f  , R_OK) || file_
57a0: 73 69 7a 65 28 7a 44 62 4e 61 6d 65 29 3c 31 30  size(zDbName)<10
57b0: 32 34 20 29 7b 0a 20 20 20 20 69 66 28 20 61 63  24 ){.    if( ac
57c0: 63 65 73 73 28 7a 44 62 4e 61 6d 65 2c 20 30 29  cess(zDbName, 0)
57d0: 20 29 7b 0a 20 20 20 20 20 20 66 6f 73 73 69 6c   ){.      fossil
57e0: 5f 70 61 6e 69 63 28 22 72 65 70 6f 73 69 74 6f  _panic("reposito
57f0: 72 79 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  ry does not exis
5800: 74 20 6f 72 22 0a 20 20 20 20 20 20 20 20 20 20  t or".          
5810: 20 20 20 20 20 20 20 20 20 22 20 69 73 20 69 6e           " is in
5820: 20 61 6e 20 75 6e 72 65 61 64 61 62 6c 65 20 64   an unreadable d
5830: 69 72 65 63 74 6f 72 79 3a 20 25 73 22 2c 20 7a  irectory: %s", z
5840: 44 62 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c  DbName);.    }el
5850: 73 65 20 69 66 28 20 61 63 63 65 73 73 28 7a 44  se if( access(zD
5860: 62 4e 61 6d 65 2c 20 52 5f 4f 4b 29 20 29 7b 0a  bName, R_OK) ){.
5870: 20 20 20 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e        fossil_pan
5880: 69 63 28 22 72 65 61 64 20 70 65 72 6d 69 73 73  ic("read permiss
5890: 69 6f 6e 20 64 65 6e 69 65 64 20 66 6f 72 20 72  ion denied for r
58a0: 65 70 6f 73 69 74 6f 72 79 20 25 73 22 2c 20 7a  epository %s", z
58b0: 44 62 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c  DbName);.    }el
58c0: 73 65 7b 0a 20 20 20 20 20 20 66 6f 73 73 69 6c  se{.      fossil
58d0: 5f 70 61 6e 69 63 28 22 6e 6f 74 20 61 20 76 61  _panic("not a va
58e0: 6c 69 64 20 72 65 70 6f 73 69 74 6f 72 79 3a 20  lid repository: 
58f0: 25 73 22 2c 20 7a 44 62 4e 61 6d 65 29 3b 0a 20  %s", zDbName);. 
5900: 20 20 20 7d 0a 20 20 7d 0a 20 20 64 62 5f 6f 70     }.  }.  db_op
5910: 65 6e 5f 6f 72 5f 61 74 74 61 63 68 28 7a 44 62  en_or_attach(zDb
5920: 4e 61 6d 65 2c 20 22 72 65 70 6f 73 69 74 6f 72  Name, "repositor
5930: 79 22 29 3b 0a 20 20 67 2e 72 65 70 6f 73 69 74  y");.  g.reposit
5940: 6f 72 79 4f 70 65 6e 20 3d 20 31 3b 0a 20 20 67  oryOpen = 1;.  g
5950: 2e 7a 52 65 70 6f 73 69 74 6f 72 79 4e 61 6d 65  .zRepositoryName
5960: 20 3d 20 6d 70 72 69 6e 74 66 28 22 25 73 22 2c   = mprintf("%s",
5970: 20 7a 44 62 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a   zDbName);.}../*
5980: 0a 2a 2a 20 54 72 79 20 74 6f 20 66 69 6e 64 20  .** Try to find 
5990: 74 68 65 20 72 65 70 6f 73 69 74 6f 72 79 20 61  the repository a
59a0: 6e 64 20 6f 70 65 6e 20 69 74 2e 20 20 55 73 65  nd open it.  Use
59b0: 20 74 68 65 20 2d 52 20 6f 72 20 2d 2d 72 65 70   the -R or --rep
59c0: 6f 73 69 74 6f 72 79 0a 2a 2a 20 6f 70 74 69 6f  ository.** optio
59d0: 6e 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20  n to locate the 
59e0: 72 65 70 6f 73 69 74 6f 72 79 2e 20 20 49 66 20  repository.  If 
59f0: 6e 6f 20 73 75 63 68 20 6f 70 74 69 6f 6e 20 69  no such option i
5a00: 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 65  s available, the
5a10: 6e 0a 2a 2a 20 75 73 65 20 74 68 65 20 72 65 70  n.** use the rep
5a20: 6f 73 69 74 6f 72 79 20 6f 66 20 74 68 65 20 6f  ository of the o
5a30: 70 65 6e 20 63 68 65 63 6b 6f 75 74 20 69 66 20  pen checkout if 
5a40: 74 68 65 72 65 20 69 73 20 6f 6e 65 2e 0a 2a 2a  there is one..**
5a50: 0a 2a 2a 20 45 72 72 6f 72 20 6f 75 74 20 69 66  .** Error out if
5a60: 20 74 68 65 20 72 65 70 6f 73 69 74 6f 72 79 20   the repository 
5a70: 63 61 6e 6e 6f 74 20 62 65 20 6f 70 65 6e 65 64  cannot be opened
5a80: 2e 0a 2a 2f 0a 76 6f 69 64 20 64 62 5f 66 69 6e  ..*/.void db_fin
5a90: 64 5f 61 6e 64 5f 6f 70 65 6e 5f 72 65 70 6f 73  d_and_open_repos
5aa0: 69 74 6f 72 79 28 69 6e 74 20 65 72 72 49 66 4e  itory(int errIfN
5ab0: 6f 74 46 6f 75 6e 64 29 7b 0a 20 20 63 6f 6e 73  otFound){.  cons
5ac0: 74 20 63 68 61 72 20 2a 7a 52 65 70 20 3d 20 66  t char *zRep = f
5ad0: 69 6e 64 5f 6f 70 74 69 6f 6e 28 22 72 65 70 6f  ind_option("repo
5ae0: 73 69 74 6f 72 79 22 2c 20 22 52 22 2c 20 31 29  sitory", "R", 1)
5af0: 3b 0a 20 20 69 66 28 20 7a 52 65 70 3d 3d 30 20  ;.  if( zRep==0 
5b00: 29 7b 0a 20 20 20 20 69 66 28 20 64 62 5f 6f 70  ){.    if( db_op
5b10: 65 6e 5f 6c 6f 63 61 6c 28 29 3d 3d 30 20 29 7b  en_local()==0 ){
5b20: 0a 20 20 20 20 20 20 67 6f 74 6f 20 72 65 70 5f  .      goto rep_
5b30: 6e 6f 74 5f 66 6f 75 6e 64 3b 0a 20 20 20 20 7d  not_found;.    }
5b40: 0a 20 20 20 20 7a 52 65 70 20 3d 20 64 62 5f 6c  .    zRep = db_l
5b50: 67 65 74 28 22 72 65 70 6f 73 69 74 6f 72 79 22  get("repository"
5b60: 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 7a 52  , 0);.    if( zR
5b70: 65 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67  ep==0 ){.      g
5b80: 6f 74 6f 20 72 65 70 5f 6e 6f 74 5f 66 6f 75 6e  oto rep_not_foun
5b90: 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64  d;.    }.  }.  d
5ba0: 62 5f 6f 70 65 6e 5f 72 65 70 6f 73 69 74 6f 72  b_open_repositor
5bb0: 79 28 7a 52 65 70 29 3b 0a 20 20 69 66 28 20 67  y(zRep);.  if( g
5bc0: 2e 72 65 70 6f 73 69 74 6f 72 79 4f 70 65 6e 20  .repositoryOpen 
5bd0: 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
5be0: 20 7d 0a 72 65 70 5f 6e 6f 74 5f 66 6f 75 6e 64   }.rep_not_found
5bf0: 3a 0a 20 20 69 66 28 20 65 72 72 49 66 4e 6f 74  :.  if( errIfNot
5c00: 46 6f 75 6e 64 20 29 7b 0a 20 20 20 20 66 6f 73  Found ){.    fos
5c10: 73 69 6c 5f 66 61 74 61 6c 28 22 75 73 65 20 2d  sil_fatal("use -
5c20: 2d 72 65 70 6f 73 69 74 6f 72 79 20 6f 72 20 2d  -repository or -
5c30: 52 20 74 6f 20 73 70 65 63 69 66 69 63 20 74 68  R to specific th
5c40: 65 20 72 65 70 6f 73 69 74 6f 72 79 20 64 61 74  e repository dat
5c50: 61 62 61 73 65 22 29 3b 0a 20 20 7d 0a 7d 0a 0a  abase");.  }.}..
5c60: 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 6c  /*.** Open the l
5c70: 6f 63 61 6c 20 64 61 74 61 62 61 73 65 2e 20 20  ocal database.  
5c80: 49 66 20 75 6e 61 62 6c 65 2c 20 65 78 69 74 20  If unable, exit 
5c90: 77 69 74 68 20 61 6e 20 65 72 72 6f 72 2e 0a 2a  with an error..*
5ca0: 2f 0a 76 6f 69 64 20 64 62 5f 6d 75 73 74 5f 62  /.void db_must_b
5cb0: 65 5f 77 69 74 68 69 6e 5f 74 72 65 65 28 76 6f  e_within_tree(vo
5cc0: 69 64 29 7b 0a 20 20 69 66 28 20 64 62 5f 6f 70  id){.  if( db_op
5cd0: 65 6e 5f 6c 6f 63 61 6c 28 29 3d 3d 30 20 29 7b  en_local()==0 ){
5ce0: 0a 20 20 20 20 66 6f 73 73 69 6c 5f 66 61 74 61  .    fossil_fata
5cf0: 6c 28 22 6e 6f 74 20 77 69 74 68 69 6e 20 61 6e  l("not within an
5d00: 20 6f 70 65 6e 20 63 68 65 63 6b 6f 75 74 22 29   open checkout")
5d10: 3b 0a 20 20 7d 0a 20 20 64 62 5f 6f 70 65 6e 5f  ;.  }.  db_open_
5d20: 72 65 70 6f 73 69 74 6f 72 79 28 30 29 3b 0a 7d  repository(0);.}
5d30: 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68  ../*.** Close th
5d40: 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
5d50: 63 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 64 20 64  ction..*/.void d
5d60: 62 5f 63 6c 6f 73 65 28 76 6f 69 64 29 7b 0a 20  b_close(void){. 
5d70: 20 69 66 28 20 67 2e 64 62 3d 3d 30 20 29 20 72   if( g.db==0 ) r
5d80: 65 74 75 72 6e 3b 0a 20 20 77 68 69 6c 65 28 20  eturn;.  while( 
5d90: 70 41 6c 6c 53 74 6d 74 20 29 7b 0a 20 20 20 20  pAllStmt ){.    
5da0: 64 62 5f 66 69 6e 61 6c 69 7a 65 28 70 41 6c 6c  db_finalize(pAll
5db0: 53 74 6d 74 29 3b 0a 20 20 7d 0a 20 20 67 2e 72  Stmt);.  }.  g.r
5dc0: 65 70 6f 73 69 74 6f 72 79 4f 70 65 6e 20 3d 20  epositoryOpen = 
5dd0: 30 3b 0a 20 20 67 2e 6c 6f 63 61 6c 4f 70 65 6e  0;.  g.localOpen
5de0: 20 3d 20 30 3b 0a 20 20 67 2e 63 6f 6e 66 69 67   = 0;.  g.config
5df0: 4f 70 65 6e 20 3d 20 30 3b 0a 20 20 73 71 6c 69  Open = 0;.  sqli
5e00: 74 65 33 5f 63 6c 6f 73 65 28 67 2e 64 62 29 3b  te3_close(g.db);
5e10: 0a 20 20 67 2e 64 62 20 3d 20 30 3b 0a 7d 0a 0a  .  g.db = 0;.}..
5e20: 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
5e30: 6e 65 77 20 65 6d 70 74 79 20 72 65 70 6f 73 69  new empty reposi
5e40: 74 6f 72 79 20 64 61 74 61 62 61 73 65 20 77 69  tory database wi
5e50: 74 68 20 74 68 65 20 67 69 76 65 6e 20 6e 61 6d  th the given nam
5e60: 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74 68  e..**.** Only th
5e70: 65 20 73 63 68 65 6d 61 20 69 73 20 69 6e 69 74  e schema is init
5e80: 69 61 6c 69 7a 65 64 2e 20 20 54 68 65 20 72 65  ialized.  The re
5e90: 71 75 69 72 65 64 20 56 41 52 20 74 61 62 6c 65  quired VAR table
5ea0: 73 20 65 6e 74 72 69 65 73 0a 2a 2a 20 61 72 65  s entries.** are
5eb0: 20 6e 6f 74 20 73 65 74 20 62 79 20 74 68 69 73   not set by this
5ec0: 20 72 6f 75 74 69 6e 65 20 61 6e 64 20 6d 75 73   routine and mus
5ed0: 74 20 62 65 20 73 65 74 20 73 65 70 61 72 61 74  t be set separat
5ee0: 65 6c 79 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20  ely in order.** 
5ef0: 74 6f 20 6d 61 6b 65 20 74 68 65 20 6e 65 77 20  to make the new 
5f00: 66 69 6c 65 20 61 20 76 61 6c 69 64 20 64 61 74  file a valid dat
5f10: 61 62 61 73 65 2e 0a 2a 2f 0a 76 6f 69 64 20 64  abase..*/.void d
5f20: 62 5f 63 72 65 61 74 65 5f 72 65 70 6f 73 69 74  b_create_reposit
5f30: 6f 72 79 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ory(const char *
5f40: 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 64 62  zFilename){.  db
5f50: 5f 69 6e 69 74 5f 64 61 74 61 62 61 73 65 28 0a  _init_database(.
5f60: 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 2c 0a       zFilename,.
5f70: 20 20 20 20 20 7a 52 65 70 6f 73 69 74 6f 72 79       zRepository
5f80: 53 63 68 65 6d 61 31 2c 0a 20 20 20 20 20 7a 52  Schema1,.     zR
5f90: 65 70 6f 73 69 74 6f 72 79 53 63 68 65 6d 61 32  epositorySchema2
5fa0: 2c 0a 20 20 20 20 20 28 63 68 61 72 2a 29 30 0a  ,.     (char*)0.
5fb0: 20 20 29 3b 0a 20 20 69 73 4e 65 77 52 65 70 6f    );.  isNewRepo
5fc0: 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43   = 1;.}../*.** C
5fd0: 72 65 61 74 65 20 74 68 65 20 64 65 66 61 75 6c  reate the defaul
5fe0: 74 20 75 73 65 72 20 61 63 63 6f 75 6e 74 73 20  t user accounts 
5ff0: 69 6e 20 74 68 65 20 55 53 45 52 20 74 61 62 6c  in the USER tabl
6000: 65 2e 0a 2a 2f 0a 76 6f 69 64 20 64 62 5f 63 72  e..*/.void db_cr
6010: 65 61 74 65 5f 64 65 66 61 75 6c 74 5f 75 73 65  eate_default_use
6020: 72 73 28 69 6e 74 20 73 65 74 75 70 55 73 65 72  rs(int setupUser
6030: 4f 6e 6c 79 29 7b 0a 20 20 63 6f 6e 73 74 20 63  Only){.  const c
6040: 68 61 72 20 2a 7a 55 73 65 72 3b 0a 20 20 7a 55  har *zUser;.  zU
6050: 73 65 72 20 3d 20 64 62 5f 67 65 74 28 22 64 65  ser = db_get("de
6060: 66 61 75 6c 74 2d 75 73 65 72 22 2c 20 30 29 3b  fault-user", 0);
6070: 0a 20 20 69 66 28 20 7a 55 73 65 72 3d 3d 30 20  .  if( zUser==0 
6080: 29 7b 0a 23 69 66 64 65 66 20 5f 5f 4d 49 4e 47  ){.#ifdef __MING
6090: 57 33 32 5f 5f 0a 20 20 20 20 7a 55 73 65 72 20  W32__.    zUser 
60a0: 3d 20 67 65 74 65 6e 76 28 22 55 53 45 52 4e 41  = getenv("USERNA
60b0: 4d 45 22 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20  ME");.#else.    
60c0: 7a 55 73 65 72 20 3d 20 67 65 74 65 6e 76 28 22  zUser = getenv("
60d0: 55 53 45 52 22 29 3b 0a 23 65 6e 64 69 66 0a 20  USER");.#endif. 
60e0: 20 7d 0a 20 20 69 66 28 20 7a 55 73 65 72 3d 3d   }.  if( zUser==
60f0: 30 20 29 7b 0a 20 20 20 20 7a 55 73 65 72 20 3d  0 ){.    zUser =
6100: 20 22 72 6f 6f 74 22 3b 0a 20 20 7d 0a 20 20 64   "root";.  }.  d
6110: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20  b_multi_exec(.  
6120: 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20     "INSERT INTO 
6130: 75 73 65 72 28 6c 6f 67 69 6e 2c 20 70 77 2c 20  user(login, pw, 
6140: 63 61 70 2c 20 69 6e 66 6f 29 22 0a 20 20 20 20  cap, info)".    
6150: 20 22 56 41 4c 55 45 53 28 25 51 2c 6c 6f 77 65   "VALUES(%Q,lowe
6160: 72 28 68 65 78 28 72 61 6e 64 6f 6d 62 6c 6f 62  r(hex(randomblob
6170: 28 33 29 29 29 2c 27 73 27 2c 27 27 29 22 2c 20  (3))),'s','')", 
6180: 7a 55 73 65 72 0a 20 20 29 3b 0a 20 20 69 66 28  zUser.  );.  if(
6190: 20 21 73 65 74 75 70 55 73 65 72 4f 6e 6c 79 20   !setupUserOnly 
61a0: 29 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f  ){.    db_multi_
61b0: 65 78 65 63 28 0a 20 20 20 20 20 20 20 22 49 4e  exec(.       "IN
61c0: 53 45 52 54 20 49 4e 54 4f 20 75 73 65 72 28 6c  SERT INTO user(l
61d0: 6f 67 69 6e 2c 70 77 2c 63 61 70 2c 69 6e 66 6f  ogin,pw,cap,info
61e0: 29 22 0a 20 20 20 20 20 20 20 22 20 20 20 56 41  )".       "   VA
61f0: 4c 55 45 53 28 27 61 6e 6f 6e 79 6d 6f 75 73 27  LUES('anonymous'
6200: 2c 27 61 6e 6f 6e 79 6d 6f 75 73 27 2c 27 67 68  ,'anonymous','gh
6210: 6d 6e 63 7a 27 2c 27 41 6e 6f 6e 27 29 3b 22 0a  mncz','Anon');".
6220: 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20 49         "INSERT I
6230: 4e 54 4f 20 75 73 65 72 28 6c 6f 67 69 6e 2c 70  NTO user(login,p
6240: 77 2c 63 61 70 2c 69 6e 66 6f 29 22 0a 20 20 20  w,cap,info)".   
6250: 20 20 20 20 22 20 20 20 56 41 4c 55 45 53 28 27      "   VALUES('
6260: 6e 6f 62 6f 64 79 27 2c 27 27 2c 27 6a 6f 72 27  nobody','','jor'
6270: 2c 27 4e 6f 62 6f 64 79 27 29 3b 22 0a 20 20 20  ,'Nobody');".   
6280: 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f      "INSERT INTO
6290: 20 75 73 65 72 28 6c 6f 67 69 6e 2c 70 77 2c 63   user(login,pw,c
62a0: 61 70 2c 69 6e 66 6f 29 22 0a 20 20 20 20 20 20  ap,info)".      
62b0: 20 22 20 20 20 56 41 4c 55 45 53 28 27 64 65 76   "   VALUES('dev
62c0: 65 6c 6f 70 65 72 27 2c 27 27 2c 27 64 65 69 27  eloper','','dei'
62d0: 2c 27 44 65 76 27 29 3b 22 0a 20 20 20 20 20 20  ,'Dev');".      
62e0: 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 75 73   "INSERT INTO us
62f0: 65 72 28 6c 6f 67 69 6e 2c 70 77 2c 63 61 70 2c  er(login,pw,cap,
6300: 69 6e 66 6f 29 22 0a 20 20 20 20 20 20 20 22 20  info)".       " 
6310: 20 20 56 41 4c 55 45 53 28 27 72 65 61 64 65 72    VALUES('reader
6320: 27 2c 27 27 2c 27 6b 70 74 77 27 2c 27 52 65 61  ','','kptw','Rea
6330: 64 65 72 27 29 3b 22 0a 20 20 20 20 29 3b 0a 20  der');".    );. 
6340: 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c 6c   }.}../*.** Fill
6350: 20 61 6e 20 65 6d 70 74 79 20 72 65 70 6f 73 69   an empty reposi
6360: 74 6f 72 79 20 64 61 74 61 62 61 73 65 20 77 69  tory database wi
6370: 74 68 20 74 68 65 20 62 61 73 69 63 20 69 6e 66  th the basic inf
6380: 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 0a 2a  ormation for a.*
6390: 2a 20 72 65 70 6f 73 69 74 6f 72 79 2e 20 54 68  * repository. Th
63a0: 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 73  is function is s
63b0: 68 61 72 65 64 20 62 65 74 77 65 65 6e 20 27 63  hared between 'c
63c0: 72 65 61 74 65 5f 72 65 70 6f 73 69 74 6f 72 79  reate_repository
63d0: 5f 63 6d 64 27 0a 2a 2a 20 28 27 6e 65 77 27 29  _cmd'.** ('new')
63e0: 20 61 6e 64 20 27 72 65 63 6f 6e 73 74 72 75 63   and 'reconstruc
63f0: 74 5f 63 6d 64 27 20 28 27 72 65 63 6f 6e 73 74  t_cmd' ('reconst
6400: 72 75 63 74 27 29 2c 20 62 6f 74 68 20 6f 66 20  ruct'), both of 
6410: 77 68 69 63 68 20 63 72 65 61 74 65 0a 2a 2a 20  which create.** 
6420: 6e 65 77 20 72 65 70 6f 73 69 74 6f 72 69 65 73  new repositories
6430: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 49 6e 69  ..**.** The zIni
6440: 74 69 61 6c 44 61 74 65 20 70 61 72 61 6d 65 74  tialDate paramet
6450: 65 72 20 64 65 74 65 72 6d 69 6e 65 73 20 74 68  er determines th
6460: 65 20 64 61 74 65 20 6f 66 20 74 68 65 20 69 6e  e date of the in
6470: 69 74 69 61 6c 20 63 68 65 63 6b 2d 69 6e 0a 2a  itial check-in.*
6480: 2a 20 74 68 61 74 20 69 73 20 61 75 74 6f 6d 61  * that is automa
6490: 74 69 63 61 6c 6c 79 20 63 72 65 61 74 65 64 2e  tically created.
64a0: 20 20 49 66 20 7a 49 6e 69 74 69 61 6c 44 61 74    If zInitialDat
64b0: 65 20 69 73 20 30 20 74 68 65 6e 20 6e 6f 20 69  e is 0 then no i
64c0: 6e 69 74 69 61 6c 0a 2a 2a 20 63 68 65 63 6b 2d  nitial.** check-
64d0: 69 6e 20 69 73 20 63 72 65 61 74 65 64 2e 20 54  in is created. T
64e0: 68 65 20 6d 61 6b 65 53 65 72 76 65 72 43 6f 64  he makeServerCod
64f0: 65 73 20 66 6c 61 67 20 64 65 74 65 72 6d 69 6e  es flag determin
6500: 65 73 20 77 68 65 74 68 65 72 20 6f 72 0a 2a 2a  es whether or.**
6510: 20 6e 6f 74 20 73 65 72 76 65 72 20 61 6e 64 20   not server and 
6520: 70 72 6f 6a 65 63 74 20 63 6f 64 65 73 20 61 72  project codes ar
6530: 65 20 69 6e 76 65 6e 74 65 64 20 66 6f 72 20 74  e invented for t
6540: 68 69 73 20 72 65 70 6f 73 69 74 6f 72 79 2e 0a  his repository..
6550: 2a 2f 0a 76 6f 69 64 20 64 62 5f 69 6e 69 74 69  */.void db_initi
6560: 61 6c 5f 73 65 74 75 70 20 28 63 6f 6e 73 74 20  al_setup (const 
6570: 63 68 61 72 20 2a 7a 49 6e 69 74 69 61 6c 44 61  char *zInitialDa
6580: 74 65 2c 20 69 6e 74 20 6d 61 6b 65 53 65 72 76  te, int makeServ
6590: 65 72 43 6f 64 65 73 29 7b 0a 20 20 63 68 61 72  erCodes){.  char
65a0: 20 2a 7a 44 61 74 65 3b 0a 20 20 42 6c 6f 62 20   *zDate;.  Blob 
65b0: 68 61 73 68 3b 0a 20 20 42 6c 6f 62 20 6d 61 6e  hash;.  Blob man
65c0: 69 66 65 73 74 3b 0a 0a 20 20 64 62 5f 73 65 74  ifest;..  db_set
65d0: 28 22 63 6f 6e 74 65 6e 74 2d 73 63 68 65 6d 61  ("content-schema
65e0: 22 2c 20 43 4f 4e 54 45 4e 54 5f 53 43 48 45 4d  ", CONTENT_SCHEM
65f0: 41 2c 20 30 29 3b 0a 20 20 64 62 5f 73 65 74 28  A, 0);.  db_set(
6600: 22 61 75 78 2d 73 63 68 65 6d 61 22 2c 20 41 55  "aux-schema", AU
6610: 58 5f 53 43 48 45 4d 41 2c 20 30 29 3b 0a 20 20  X_SCHEMA, 0);.  
6620: 69 66 28 20 6d 61 6b 65 53 65 72 76 65 72 43 6f  if( makeServerCo
6630: 64 65 73 20 29 7b 0a 20 20 20 20 64 62 5f 6d 75  des ){.    db_mu
6640: 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20 20 20  lti_exec(.      
6650: 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 6f 6e  "INSERT INTO con
6660: 66 69 67 28 6e 61 6d 65 2c 76 61 6c 75 65 29 22  fig(name,value)"
6670: 0a 20 20 20 20 20 20 22 20 56 41 4c 55 45 53 28  .      " VALUES(
6680: 27 73 65 72 76 65 72 2d 63 6f 64 65 27 2c 20 6c  'server-code', l
6690: 6f 77 65 72 28 68 65 78 28 72 61 6e 64 6f 6d 62  ower(hex(randomb
66a0: 6c 6f 62 28 32 30 29 29 29 29 3b 22 0a 20 20 20  lob(20))));".   
66b0: 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20     "INSERT INTO 
66c0: 63 6f 6e 66 69 67 28 6e 61 6d 65 2c 76 61 6c 75  config(name,valu
66d0: 65 29 22 0a 20 20 20 20 20 20 22 20 56 41 4c 55  e)".      " VALU
66e0: 45 53 28 27 70 72 6f 6a 65 63 74 2d 63 6f 64 65  ES('project-code
66f0: 27 2c 20 6c 6f 77 65 72 28 68 65 78 28 72 61 6e  ', lower(hex(ran
6700: 64 6f 6d 62 6c 6f 62 28 32 30 29 29 29 29 3b 22  domblob(20))));"
6710: 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 20 20 69 66  .    );.  }.  if
6720: 28 20 21 64 62 5f 69 73 5f 67 6c 6f 62 61 6c 28  ( !db_is_global(
6730: 22 61 75 74 6f 73 79 6e 63 22 29 20 29 20 64 62  "autosync") ) db
6740: 5f 73 65 74 5f 69 6e 74 28 22 61 75 74 6f 73 79  _set_int("autosy
6750: 6e 63 22 2c 20 31 2c 20 30 29 3b 0a 20 20 69 66  nc", 1, 0);.  if
6760: 28 20 21 64 62 5f 69 73 5f 67 6c 6f 62 61 6c 28  ( !db_is_global(
6770: 22 6c 6f 63 61 6c 61 75 74 68 22 29 20 29 20 64  "localauth") ) d
6780: 62 5f 73 65 74 5f 69 6e 74 28 22 6c 6f 63 61 6c  b_set_int("local
6790: 61 75 74 68 22 2c 20 30 2c 20 30 29 3b 0a 20 20  auth", 0, 0);.  
67a0: 64 62 5f 63 72 65 61 74 65 5f 64 65 66 61 75 6c  db_create_defaul
67b0: 74 5f 75 73 65 72 73 28 30 29 3b 0a 20 20 75 73  t_users(0);.  us
67c0: 65 72 5f 73 65 6c 65 63 74 28 29 3b 0a 0a 20 20  er_select();..  
67d0: 69 66 28 20 7a 49 6e 69 74 69 61 6c 44 61 74 65  if( zInitialDate
67e0: 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 69 64 3b   ){.    int rid;
67f0: 0a 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26  .    blob_zero(&
6800: 6d 61 6e 69 66 65 73 74 29 3b 0a 20 20 20 20 62  manifest);.    b
6810: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 6d 61 6e  lob_appendf(&man
6820: 69 66 65 73 74 2c 20 22 43 20 69 6e 69 74 69 61  ifest, "C initia
6830: 6c 5c 5c 73 65 6d 70 74 79 5c 5c 73 63 68 65 63  l\\sempty\\schec
6840: 6b 2d 69 6e 5c 6e 22 29 3b 0a 20 20 20 20 7a 44  k-in\n");.    zD
6850: 61 74 65 20 3d 20 64 62 5f 74 65 78 74 28 30 2c  ate = db_text(0,
6860: 20 22 53 45 4c 45 43 54 20 64 61 74 65 74 69 6d   "SELECT datetim
6870: 65 28 25 51 29 22 2c 20 7a 49 6e 69 74 69 61 6c  e(%Q)", zInitial
6880: 44 61 74 65 29 3b 0a 20 20 20 20 7a 44 61 74 65  Date);.    zDate
6890: 5b 31 30 5d 3d 27 54 27 3b 0a 20 20 20 20 62 6c  [10]='T';.    bl
68a0: 6f 62 5f 61 70 70 65 6e 64 66 28 26 6d 61 6e 69  ob_appendf(&mani
68b0: 66 65 73 74 2c 20 22 44 20 25 73 5c 6e 22 2c 20  fest, "D %s\n", 
68c0: 7a 44 61 74 65 29 3b 0a 20 20 20 20 62 6c 6f 62  zDate);.    blob
68d0: 5f 61 70 70 65 6e 64 66 28 26 6d 61 6e 69 66 65  _appendf(&manife
68e0: 73 74 2c 20 22 50 5c 6e 22 29 3b 0a 20 20 20 20  st, "P\n");.    
68f0: 6d 64 35 73 75 6d 5f 69 6e 69 74 28 29 3b 0a 20  md5sum_init();. 
6900: 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28     blob_appendf(
6910: 26 6d 61 6e 69 66 65 73 74 2c 20 22 52 20 25 73  &manifest, "R %s
6920: 5c 6e 22 2c 20 6d 64 35 73 75 6d 5f 66 69 6e 69  \n", md5sum_fini
6930: 73 68 28 30 29 29 3b 0a 20 20 20 20 62 6c 6f 62  sh(0));.    blob
6940: 5f 61 70 70 65 6e 64 66 28 26 6d 61 6e 69 66 65  _appendf(&manife
6950: 73 74 2c 20 22 54 20 2a 62 72 61 6e 63 68 20 2a  st, "T *branch *
6960: 20 74 72 75 6e 6b 5c 6e 22 29 3b 0a 20 20 20 20   trunk\n");.    
6970: 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 6d 61  blob_appendf(&ma
6980: 6e 69 66 65 73 74 2c 20 22 54 20 2a 73 79 6d 2d  nifest, "T *sym-
6990: 74 72 75 6e 6b 20 2a 5c 6e 22 29 3b 0a 20 20 20  trunk *\n");.   
69a0: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 6d   blob_appendf(&m
69b0: 61 6e 69 66 65 73 74 2c 20 22 55 20 25 46 5c 6e  anifest, "U %F\n
69c0: 22 2c 20 67 2e 7a 4c 6f 67 69 6e 29 3b 0a 20 20  ", g.zLogin);.  
69d0: 20 20 6d 64 35 73 75 6d 5f 62 6c 6f 62 28 26 6d    md5sum_blob(&m
69e0: 61 6e 69 66 65 73 74 2c 20 26 68 61 73 68 29 3b  anifest, &hash);
69f0: 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64  .    blob_append
6a00: 66 28 26 6d 61 6e 69 66 65 73 74 2c 20 22 5a 20  f(&manifest, "Z 
6a10: 25 62 5c 6e 22 2c 20 26 68 61 73 68 29 3b 0a 20  %b\n", &hash);. 
6a20: 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 68     blob_reset(&h
6a30: 61 73 68 29 3b 0a 20 20 20 20 72 69 64 20 3d 20  ash);.    rid = 
6a40: 63 6f 6e 74 65 6e 74 5f 70 75 74 28 26 6d 61 6e  content_put(&man
6a50: 69 66 65 73 74 2c 20 30 2c 20 30 29 3b 0a 20 20  ifest, 0, 0);.  
6a60: 20 20 6d 61 6e 69 66 65 73 74 5f 63 72 6f 73 73    manifest_cross
6a70: 6c 69 6e 6b 28 72 69 64 2c 20 26 6d 61 6e 69 66  link(rid, &manif
6a80: 65 73 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  est);.  }.}../*.
6a90: 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 6e 65 77 0a  ** COMMAND: new.
6aa0: 2a 2a 0a 2a 2a 20 55 73 61 67 65 3a 20 25 66 6f  **.** Usage: %fo
6ab0: 73 73 69 6c 20 6e 65 77 20 46 49 4c 45 4e 41 4d  ssil new FILENAM
6ac0: 45 0a 2a 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  E.**.** Create a
6ad0: 20 72 65 70 6f 73 69 74 6f 72 79 20 66 6f 72 20   repository for 
6ae0: 61 20 6e 65 77 20 70 72 6f 6a 65 63 74 20 69 6e  a new project in
6af0: 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 64 20   the file named 
6b00: 46 49 4c 45 4e 41 4d 45 2e 0a 2a 2a 20 54 68 69  FILENAME..** Thi
6b10: 73 20 63 6f 6d 6d 61 6e 64 20 69 73 20 64 69 73  s command is dis
6b20: 74 69 6e 63 74 20 66 72 6f 6d 20 22 63 6c 6f 6e  tinct from "clon
6b30: 65 22 2e 20 20 54 68 65 20 22 63 6c 6f 6e 65 22  e".  The "clone"
6b40: 20 63 6f 6d 6d 61 6e 64 20 6d 61 6b 65 73 0a 2a   command makes.*
6b50: 2a 20 61 20 63 6f 70 79 20 6f 66 20 61 6e 20 65  * a copy of an e
6b60: 78 69 73 74 69 6e 67 20 70 72 6f 6a 65 63 74 2e  xisting project.
6b70: 20 20 54 68 69 73 20 63 6f 6d 6d 61 6e 64 20 73    This command s
6b80: 74 61 72 74 73 20 61 20 6e 65 77 20 70 72 6f 6a  tarts a new proj
6b90: 65 63 74 2e 0a 2a 2f 0a 76 6f 69 64 20 63 72 65  ect..*/.void cre
6ba0: 61 74 65 5f 72 65 70 6f 73 69 74 6f 72 79 5f 63  ate_repository_c
6bb0: 6d 64 28 76 6f 69 64 29 7b 0a 20 20 63 68 61 72  md(void){.  char
6bc0: 20 2a 7a 50 61 73 73 77 6f 72 64 3b 0a 20 20 63   *zPassword;.  c
6bd0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65  onst char *zDate
6be0: 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61  ;          /* Da
6bf0: 74 65 20 6f 66 20 74 68 65 20 69 6e 69 74 69 61  te of the initia
6c00: 6c 20 63 68 65 63 6b 2d 69 6e 20 2a 2f 0a 0a 20  l check-in */.. 
6c10: 20 7a 44 61 74 65 20 3d 20 66 69 6e 64 5f 6f 70   zDate = find_op
6c20: 74 69 6f 6e 28 22 64 61 74 65 2d 6f 76 65 72 72  tion("date-overr
6c30: 69 64 65 22 2c 30 2c 31 29 3b 0a 20 20 69 66 28  ide",0,1);.  if(
6c40: 20 7a 44 61 74 65 3d 3d 30 20 29 20 7a 44 61 74   zDate==0 ) zDat
6c50: 65 20 3d 20 22 6e 6f 77 22 3b 0a 20 20 69 66 28  e = "now";.  if(
6c60: 20 67 2e 61 72 67 63 21 3d 33 20 29 7b 0a 20 20   g.argc!=3 ){.  
6c70: 20 20 75 73 61 67 65 28 22 52 45 50 4f 53 49 54    usage("REPOSIT
6c80: 4f 52 59 2d 4e 41 4d 45 22 29 3b 0a 20 20 7d 0a  ORY-NAME");.  }.
6c90: 20 20 64 62 5f 63 72 65 61 74 65 5f 72 65 70 6f    db_create_repo
6ca0: 73 69 74 6f 72 79 28 67 2e 61 72 67 76 5b 32 5d  sitory(g.argv[2]
6cb0: 29 3b 0a 20 20 64 62 5f 6f 70 65 6e 5f 72 65 70  );.  db_open_rep
6cc0: 6f 73 69 74 6f 72 79 28 67 2e 61 72 67 76 5b 32  ository(g.argv[2
6cd0: 5d 29 3b 0a 20 20 64 62 5f 6f 70 65 6e 5f 63 6f  ]);.  db_open_co
6ce0: 6e 66 69 67 28 30 29 3b 0a 20 20 64 62 5f 62 65  nfig(0);.  db_be
6cf0: 67 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28  gin_transaction(
6d00: 29 3b 0a 20 20 64 62 5f 69 6e 69 74 69 61 6c 5f  );.  db_initial_
6d10: 73 65 74 75 70 28 7a 44 61 74 65 2c 20 31 29 3b  setup(zDate, 1);
6d20: 0a 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61  .  db_end_transa
6d30: 63 74 69 6f 6e 28 30 29 3b 0a 20 20 70 72 69 6e  ction(0);.  prin
6d40: 74 66 28 22 70 72 6f 6a 65 63 74 2d 69 64 3a 20  tf("project-id: 
6d50: 25 73 5c 6e 22 2c 20 64 62 5f 67 65 74 28 22 70  %s\n", db_get("p
6d60: 72 6f 6a 65 63 74 2d 63 6f 64 65 22 2c 20 30 29  roject-code", 0)
6d70: 29 3b 0a 20 20 70 72 69 6e 74 66 28 22 73 65 72  );.  printf("ser
6d80: 76 65 72 2d 69 64 3a 20 20 25 73 5c 6e 22 2c 20  ver-id:  %s\n", 
6d90: 64 62 5f 67 65 74 28 22 73 65 72 76 65 72 2d 63  db_get("server-c
6da0: 6f 64 65 22 2c 20 30 29 29 3b 0a 20 20 7a 50 61  ode", 0));.  zPa
6db0: 73 73 77 6f 72 64 20 3d 20 64 62 5f 74 65 78 74  ssword = db_text
6dc0: 28 30 2c 20 22 53 45 4c 45 43 54 20 70 77 20 46  (0, "SELECT pw F
6dd0: 52 4f 4d 20 75 73 65 72 20 57 48 45 52 45 20 6c  ROM user WHERE l
6de0: 6f 67 69 6e 3d 25 51 22 2c 20 67 2e 7a 4c 6f 67  ogin=%Q", g.zLog
6df0: 69 6e 29 3b 0a 20 20 70 72 69 6e 74 66 28 22 61  in);.  printf("a
6e00: 64 6d 69 6e 2d 75 73 65 72 3a 20 25 73 20 28 69  dmin-user: %s (i
6e10: 6e 69 74 69 61 6c 20 70 61 73 73 77 6f 72 64 20  nitial password 
6e20: 69 73 20 5c 22 25 73 5c 22 29 5c 6e 22 2c 20 67  is \"%s\")\n", g
6e30: 2e 7a 4c 6f 67 69 6e 2c 20 7a 50 61 73 73 77 6f  .zLogin, zPasswo
6e40: 72 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51  rd);.}../*.** SQ
6e50: 4c 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20  L functions for 
6e60: 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2a 0a 2a 2a  debugging..**.**
6e70: 20 54 68 65 20 70 72 69 6e 74 28 29 20 66 75 6e   The print() fun
6e80: 63 74 69 6f 6e 20 77 72 69 74 65 73 20 69 74 73  ction writes its
6e90: 20 61 72 67 75 6d 65 6e 74 73 20 6f 6e 20 73 74   arguments on st
6ea0: 64 6f 75 74 2c 20 62 75 74 20 6f 6e 6c 79 0a 2a  dout, but only.*
6eb0: 2a 20 69 66 20 74 68 65 20 2d 73 71 6c 70 72 69  * if the -sqlpri
6ec0: 6e 74 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20  nt command-line 
6ed0: 6f 70 74 69 6f 6e 20 69 73 20 74 75 72 6e 65 64  option is turned
6ee0: 20 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76   on..*/.static v
6ef0: 6f 69 64 20 64 62 5f 73 71 6c 5f 70 72 69 6e 74  oid db_sql_print
6f00: 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
6f10: 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
6f20: 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
6f30: 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
6f40: 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  .){.  int i;.  i
6f50: 66 28 20 67 2e 66 53 71 6c 50 72 69 6e 74 20 29  f( g.fSqlPrint )
6f60: 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  {.    for(i=0; i
6f70: 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20  <argc; i++){.   
6f80: 20 20 20 63 68 61 72 20 63 20 3d 20 69 3d 3d 61     char c = i==a
6f90: 72 67 63 2d 31 20 3f 20 27 5c 6e 27 20 3a 20 27  rgc-1 ? '\n' : '
6fa0: 20 27 3b 0a 20 20 20 20 20 20 70 72 69 6e 74 66   ';.      printf
6fb0: 28 22 25 73 25 63 22 2c 20 73 71 6c 69 74 65 33  ("%s%c", sqlite3
6fc0: 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
6fd0: 5b 69 5d 29 2c 20 63 29 3b 0a 20 20 20 20 7d 0a  [i]), c);.    }.
6fe0: 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69    }.}.static voi
6ff0: 64 20 64 62 5f 73 71 6c 5f 74 72 61 63 65 28 76  d db_sql_trace(v
7000: 6f 69 64 20 2a 6e 6f 74 55 73 65 64 2c 20 63 6f  oid *notUsed, co
7010: 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 29 7b  nst char *zSql){
7020: 0a 20 20 70 72 69 6e 74 66 28 22 25 73 5c 6e 22  .  printf("%s\n"
7030: 2c 20 7a 53 71 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  , zSql);.}../*.*
7040: 2a 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62  * This is used b
7050: 79 20 74 68 65 20 5b 63 6f 6d 6d 69 74 5d 20 63  y the [commit] c
7060: 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 52 65  ommand..**.** Re
7070: 74 75 72 6e 20 74 72 75 65 20 69 66 20 65 69 74  turn true if eit
7080: 68 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61  her:.**.**     a
7090: 29 20 47 6c 6f 62 61 6c 2e 61 43 6f 6d 6d 69 74  ) Global.aCommit
70a0: 46 69 6c 65 20 69 73 20 4e 55 4c 4c 2c 20 6f 72  File is NULL, or
70b0: 0a 2a 2a 20 20 20 20 20 62 29 20 47 6c 6f 62 61  .**     b) Globa
70c0: 6c 2e 61 43 6f 6d 6d 69 74 46 69 6c 65 20 63 6f  l.aCommitFile co
70d0: 6e 74 61 69 6e 73 20 74 68 65 20 69 6e 74 65 67  ntains the integ
70e0: 65 72 20 70 61 73 73 65 64 20 61 73 20 61 6e 20  er passed as an 
70f0: 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  argument..**.** 
7100: 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72 6e  Otherwise return
7110: 20 66 61 6c 73 65 2e 0a 2a 2f 0a 73 74 61 74 69   false..*/.stati
7120: 63 20 76 6f 69 64 20 66 69 6c 65 5f 69 73 5f 73  c void file_is_s
7130: 65 6c 65 63 74 65 64 28 0a 20 20 73 71 6c 69 74  elected(.  sqlit
7140: 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
7150: 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c  ext,.  int argc,
7160: 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
7170: 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 61 73 73   **argv.){.  ass
7180: 65 72 74 28 61 72 67 63 3d 3d 31 29 3b 0a 20 20  ert(argc==1);.  
7190: 69 66 28 20 67 2e 61 43 6f 6d 6d 69 74 46 69 6c  if( g.aCommitFil
71a0: 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 49 64  e ){.    int iId
71b0: 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
71c0: 5f 69 6e 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20  _int(argv[0]);. 
71d0: 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 66     int ii;.    f
71e0: 6f 72 28 69 69 3d 30 3b 20 67 2e 61 43 6f 6d 6d  or(ii=0; g.aComm
71f0: 69 74 46 69 6c 65 5b 69 69 5d 3b 20 69 69 2b 2b  itFile[ii]; ii++
7200: 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 49 64  ){.      if( iId
7210: 3d 3d 67 2e 61 43 6f 6d 6d 69 74 46 69 6c 65 5b  ==g.aCommitFile[
7220: 69 69 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ii] ){.        s
7230: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
7240: 74 28 63 6f 6e 74 65 78 74 2c 20 31 29 3b 0a 20  t(context, 1);. 
7250: 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
7260: 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
7270: 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
7280: 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b  int(context, 0);
7290: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
72a0: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
72b0: 28 63 6f 6e 74 65 78 74 2c 20 31 29 3b 0a 20 20  (context, 1);.  
72c0: 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  }.}../*.** Conve
72d0: 72 74 20 74 68 65 20 69 6e 70 75 74 20 73 74 72  rt the input str
72e0: 69 6e 67 20 69 6e 74 6f 20 61 6e 20 53 48 41 31  ing into an SHA1
72f0: 2e 20 20 4d 61 6b 65 20 61 20 6e 6f 74 61 74 69  .  Make a notati
7300: 6f 6e 20 69 6e 20 74 68 65 0a 2a 2a 20 43 4f 4e  on in the.** CON
7310: 43 45 41 4c 45 44 20 74 61 62 6c 65 20 73 6f 20  CEALED table so 
7320: 74 68 61 74 20 74 68 65 20 68 61 73 68 20 63 61  that the hash ca
7330: 6e 20 62 65 20 75 6e 64 6f 20 75 73 69 6e 67 20  n be undo using 
7340: 74 68 65 20 64 62 5f 72 65 76 65 61 6c 28 29 0a  the db_reveal().
7350: 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 74 20 73  ** function at s
7360: 6f 6d 65 20 6c 61 74 65 72 20 74 69 6d 65 2e 0a  ome later time..
7370: 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20  **.** The value 
7380: 72 65 74 75 72 6e 65 64 20 69 73 20 73 74 6f 72  returned is stor
7390: 65 64 20 69 6e 20 73 74 61 74 69 63 20 73 70 61  ed in static spa
73a0: 63 65 20 61 6e 64 20 77 69 6c 6c 20 62 65 20 6f  ce and will be o
73b0: 76 65 72 77 72 69 74 74 65 6e 0a 2a 2a 20 6f 6e  verwritten.** on
73c0: 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
73d0: 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 43 6f 6e  s..**.** If zCon
73e0: 74 65 6e 74 20 69 73 20 61 6c 72 65 61 64 79 20  tent is already 
73f0: 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 53 48  a well-formed SH
7400: 41 31 20 68 61 73 68 2c 20 74 68 65 6e 20 72 65  A1 hash, then re
7410: 74 75 72 6e 20 61 20 63 6f 70 79 0a 2a 2a 20 6f  turn a copy.** o
7420: 66 20 74 68 61 74 20 68 61 73 68 2c 20 6e 6f 74  f that hash, not
7430: 20 61 20 68 61 73 68 20 6f 66 20 74 68 65 20 68   a hash of the h
7440: 61 73 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43  ash..**.** The C
7450: 4f 4e 43 45 41 4c 45 44 20 74 61 62 6c 65 20 69  ONCEALED table i
7460: 73 20 6d 65 61 6e 74 20 74 6f 20 6f 62 73 63 75  s meant to obscu
7470: 72 65 20 65 6d 61 69 6c 20 61 64 64 72 65 73 73  re email address
7480: 65 73 2e 20 20 45 76 65 72 79 20 76 61 6c 69 64  es.  Every valid
7490: 0a 2a 2a 20 65 6d 61 69 6c 20 61 64 64 72 65 73  .** email addres
74a0: 73 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 61  s will contain a
74b0: 20 22 40 22 20 63 68 61 72 61 63 74 65 72 20 61   "@" character a
74c0: 6e 64 20 22 40 22 20 69 73 20 6e 6f 74 20 76 61  nd "@" is not va
74d0: 6c 69 64 20 77 69 74 68 69 6e 0a 2a 2a 20 61 6e  lid within.** an
74e0: 20 53 48 41 31 20 68 61 73 68 20 73 6f 20 74 68   SHA1 hash so th
74f0: 65 72 65 20 69 73 20 6e 6f 20 63 68 61 6e 63 65  ere is no chance
7500: 20 74 68 61 74 20 61 20 76 61 6c 69 64 20 65 6d   that a valid em
7510: 61 69 6c 20 61 64 64 72 65 73 73 20 77 69 6c 6c  ail address will
7520: 20 67 6f 0a 2a 2a 20 75 6e 63 6f 6e 63 65 61 6c   go.** unconceal
7530: 65 64 2e 0a 2a 2f 0a 63 68 61 72 20 2a 64 62 5f  ed..*/.char *db_
7540: 63 6f 6e 63 65 61 6c 28 63 6f 6e 73 74 20 63 68  conceal(const ch
7550: 61 72 20 2a 7a 43 6f 6e 74 65 6e 74 2c 20 69 6e  ar *zContent, in
7560: 74 20 6e 29 7b 0a 20 20 73 74 61 74 69 63 20 63  t n){.  static c
7570: 68 61 72 20 7a 48 61 73 68 5b 34 32 5d 3b 0a 20  har zHash[42];. 
7580: 20 42 6c 6f 62 20 6f 75 74 3b 0a 20 20 69 66 28   Blob out;.  if(
7590: 20 6e 3d 3d 34 30 20 26 26 20 76 61 6c 69 64 61   n==40 && valida
75a0: 74 65 31 36 28 7a 43 6f 6e 74 65 6e 74 2c 20 6e  te16(zContent, n
75b0: 29 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28  ) ){.    memcpy(
75c0: 7a 48 61 73 68 2c 20 7a 43 6f 6e 74 65 6e 74 2c  zHash, zContent,
75d0: 20 6e 29 3b 0a 20 20 20 20 7a 48 61 73 68 5b 6e   n);.    zHash[n
75e0: 5d 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ] = 0;.  }else{.
75f0: 20 20 20 20 73 68 61 31 73 75 6d 5f 73 74 65 70      sha1sum_step
7600: 5f 74 65 78 74 28 7a 43 6f 6e 74 65 6e 74 2c 20  _text(zContent, 
7610: 6e 29 3b 0a 20 20 20 20 73 68 61 31 73 75 6d 5f  n);.    sha1sum_
7620: 66 69 6e 69 73 68 28 26 6f 75 74 29 3b 0a 20 20  finish(&out);.  
7630: 20 20 73 74 72 63 70 79 28 7a 48 61 73 68 2c 20    strcpy(zHash, 
7640: 62 6c 6f 62 5f 73 74 72 28 26 6f 75 74 29 29 3b  blob_str(&out));
7650: 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28  .    blob_reset(
7660: 26 6f 75 74 29 3b 0a 20 20 20 20 64 62 5f 6d 75  &out);.    db_mu
7670: 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20 20 20  lti_exec(.      
7680: 20 22 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f   "INSERT OR IGNO
7690: 52 45 20 49 4e 54 4f 20 63 6f 6e 63 65 61 6c 65  RE INTO conceale
76a0: 64 20 56 41 4c 55 45 53 28 25 51 2c 25 23 51 29  d VALUES(%Q,%#Q)
76b0: 22 2c 0a 20 20 20 20 20 20 20 7a 48 61 73 68 2c  ",.       zHash,
76c0: 20 6e 2c 20 7a 43 6f 6e 74 65 6e 74 0a 20 20 20   n, zContent.   
76d0: 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   );.  }.  return
76e0: 20 7a 48 61 73 68 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   zHash;.}../*.**
76f0: 20 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f 6f 6b   Attempt to look
7700: 20 75 70 20 74 68 65 20 69 6e 70 75 74 20 69 6e   up the input in
7710: 20 74 68 65 20 43 4f 4e 43 45 41 4c 45 44 20 74   the CONCEALED t
7720: 61 62 6c 65 2e 20 20 49 66 20 66 6f 75 6e 64 2c  able.  If found,
7730: 0a 2a 2a 20 61 6e 64 20 69 66 20 74 68 65 20 6f  .** and if the o
7740: 6b 52 64 41 64 64 72 20 70 65 72 6d 69 73 73 69  kRdAddr permissi
7750: 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 20 74 68  on is enabled th
7760: 65 6e 20 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a  en return the.**
7770: 20 6f 72 69 67 69 6e 61 6c 20 76 61 6c 75 65 20   original value 
7780: 66 6f 72 20 77 68 69 63 68 20 74 68 65 20 69 6e  for which the in
7790: 70 75 74 20 69 73 20 61 20 68 61 73 68 2e 20 20  put is a hash.  
77a0: 49 66 20 6f 6b 52 64 41 64 64 72 20 69 73 0a 2a  If okRdAddr is.*
77b0: 2a 20 66 61 6c 73 65 20 6f 72 20 69 66 20 74 68  * false or if th
77c0: 65 20 6c 6f 6f 6b 75 70 20 66 61 69 6c 73 2c 20  e lookup fails, 
77d0: 72 65 74 75 72 6e 20 74 68 65 20 6f 72 69 67 69  return the origi
77e0: 6e 61 6c 20 73 74 72 69 6e 67 20 63 6f 6e 74 65  nal string conte
77f0: 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 65 69 74  nt..**.** In eit
7800: 68 65 72 20 63 61 73 65 2c 20 74 68 65 20 73 74  her case, the st
7810: 72 69 6e 67 20 72 65 74 75 72 6e 65 64 20 69 73  ring returned is
7820: 20 73 74 6f 72 65 64 20 69 6e 20 73 70 61 63 65   stored in space
7830: 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f   obtained.** fro
7840: 6d 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 73 68 6f  m malloc and sho
7850: 75 6c 64 20 62 65 20 66 72 65 65 64 20 62 79 20  uld be freed by 
7860: 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63  the calling func
7870: 74 69 6f 6e 2e 0a 2a 2f 0a 63 68 61 72 20 2a 64  tion..*/.char *d
7880: 62 5f 72 65 76 65 61 6c 28 63 6f 6e 73 74 20 63  b_reveal(const c
7890: 68 61 72 20 2a 7a 4b 65 79 29 7b 0a 20 20 63 68  har *zKey){.  ch
78a0: 61 72 20 2a 7a 4f 75 74 3b 0a 20 20 69 66 28 20  ar *zOut;.  if( 
78b0: 67 2e 6f 6b 52 64 41 64 64 72 20 29 7b 0a 20 20  g.okRdAddr ){.  
78c0: 20 20 7a 4f 75 74 20 3d 20 64 62 5f 74 65 78 74    zOut = db_text
78d0: 28 30 2c 20 22 53 45 4c 45 43 54 20 63 6f 6e 74  (0, "SELECT cont
78e0: 65 6e 74 20 46 52 4f 4d 20 63 6f 6e 63 65 61 6c  ent FROM conceal
78f0: 65 64 20 57 48 45 52 45 20 68 61 73 68 3d 25 51  ed WHERE hash=%Q
7900: 22 2c 20 7a 4b 65 79 29 3b 0a 20 20 7d 65 6c 73  ", zKey);.  }els
7910: 65 7b 0a 20 20 20 20 7a 4f 75 74 20 3d 20 30 3b  e{.    zOut = 0;
7920: 0a 20 20 7d 0a 20 20 69 66 28 20 7a 4f 75 74 3d  .  }.  if( zOut=
7930: 3d 30 20 29 7b 0a 20 20 20 20 7a 4f 75 74 20 3d  =0 ){.    zOut =
7940: 20 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 20 7a   mprintf("%s", z
7950: 4b 65 79 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Key);.  }.  retu
7960: 72 6e 20 7a 4f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn zOut;.}../*.*
7970: 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
7980: 72 65 67 69 73 74 65 72 73 20 61 75 78 69 6c 69  registers auxili
7990: 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 77 68  ary functions wh
79a0: 65 6e 20 74 68 65 20 53 51 4c 69 74 65 0a 2a 2a  en the SQLite.**
79b0: 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
79c0: 74 69 6f 6e 20 69 73 20 66 69 72 73 74 20 65 73  tion is first es
79d0: 74 61 62 6c 69 73 68 65 64 2e 0a 2a 2f 0a 4c 4f  tablished..*/.LO
79e0: 43 41 4c 20 76 6f 69 64 20 64 62 5f 63 6f 6e 6e  CAL void db_conn
79f0: 65 63 74 69 6f 6e 5f 69 6e 69 74 28 76 6f 69 64  ection_init(void
7a00: 29 7b 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20  ){.  static int 
7a10: 6f 6e 63 65 20 3d 20 31 3b 0a 20 20 69 66 28 20  once = 1;.  if( 
7a20: 6f 6e 63 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  once ){.    sqli
7a30: 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
7a40: 69 6f 6e 28 67 2e 64 62 2c 20 22 70 72 69 6e 74  ion(g.db, "print
7a50: 22 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54  ", -1, SQLITE_UT
7a60: 46 38 2c 20 30 2c 64 62 5f 73 71 6c 5f 70 72 69  F8, 0,db_sql_pri
7a70: 6e 74 2c 30 2c 30 29 3b 0a 20 20 20 20 73 71 6c  nt,0,0);.    sql
7a80: 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
7a90: 74 69 6f 6e 28 0a 20 20 20 20 20 20 67 2e 64 62  tion(.      g.db
7aa0: 2c 20 22 66 69 6c 65 5f 69 73 5f 73 65 6c 65 63  , "file_is_selec
7ab0: 74 65 64 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f  ted", 1, SQLITE_
7ac0: 55 54 46 38 2c 20 30 2c 20 66 69 6c 65 5f 69 73  UTF8, 0, file_is
7ad0: 5f 73 65 6c 65 63 74 65 64 2c 30 2c 30 0a 20 20  _selected,0,0.  
7ae0: 20 20 29 3b 0a 20 20 20 20 69 66 28 20 67 2e 66    );.    if( g.f
7af0: 53 71 6c 54 72 61 63 65 20 29 7b 0a 20 20 20 20  SqlTrace ){.    
7b00: 20 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28    sqlite3_trace(
7b10: 67 2e 64 62 2c 20 64 62 5f 73 71 6c 5f 74 72 61  g.db, db_sql_tra
7b20: 63 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  ce, 0);.    }.  
7b30: 20 20 6f 6e 63 65 20 3d 20 30 3b 0a 20 20 7d 0a    once = 0;.  }.
7b40: 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
7b50: 74 72 75 65 20 69 66 20 74 68 65 20 73 74 72 69  true if the stri
7b60: 6e 67 20 7a 56 61 6c 20 72 65 70 72 65 73 65 6e  ng zVal represen
7b70: 74 73 20 22 74 72 75 65 22 20 28 6f 72 20 22 66  ts "true" (or "f
7b80: 61 6c 73 65 22 29 2e 0a 2a 2f 0a 69 6e 74 20 69  alse")..*/.int i
7b90: 73 5f 74 72 75 74 68 28 63 6f 6e 73 74 20 63 68  s_truth(const ch
7ba0: 61 72 20 2a 7a 56 61 6c 29 7b 0a 20 20 73 74 61  ar *zVal){.  sta
7bb0: 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
7bc0: 61 7a 4f 6e 5b 5d 20 3d 20 7b 20 22 6f 6e 22 2c  azOn[] = { "on",
7bd0: 20 22 79 65 73 22 2c 20 22 74 72 75 65 22 2c 20   "yes", "true", 
7be0: 22 31 22 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a  "1" };.  int i;.
7bf0: 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a    for(i=0; i<siz
7c00: 65 6f 66 28 61 7a 4f 6e 29 2f 73 69 7a 65 6f 66  eof(azOn)/sizeof
7c10: 28 61 7a 4f 6e 5b 30 5d 29 3b 20 69 2b 2b 29 7b  (azOn[0]); i++){
7c20: 0a 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28  .    if( strcmp(
7c30: 7a 56 61 6c 2c 61 7a 4f 6e 5b 69 5d 29 3d 3d 30  zVal,azOn[i])==0
7c40: 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d   ) return 1;.  }
7c50: 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 69  .  return 0;.}.i
7c60: 6e 74 20 69 73 5f 66 61 6c 73 65 28 63 6f 6e 73  nt is_false(cons
7c70: 74 20 63 68 61 72 20 2a 7a 56 61 6c 29 7b 0a 20  t char *zVal){. 
7c80: 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68   static const ch
7c90: 61 72 20 2a 61 7a 4f 66 66 5b 5d 20 3d 20 7b 20  ar *azOff[] = { 
7ca0: 22 6f 66 66 22 2c 20 22 6e 6f 22 2c 20 22 66 61  "off", "no", "fa
7cb0: 6c 73 65 22 2c 20 22 30 22 20 7d 3b 0a 20 20 69  lse", "0" };.  i
7cc0: 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nt i;.  for(i=0;
7cd0: 20 69 3c 73 69 7a 65 6f 66 28 61 7a 4f 66 66 29   i<sizeof(azOff)
7ce0: 2f 73 69 7a 65 6f 66 28 61 7a 4f 66 66 5b 30 5d  /sizeof(azOff[0]
7cf0: 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  ); i++){.    if(
7d00: 20 73 74 72 63 6d 70 28 7a 56 61 6c 2c 61 7a 4f   strcmp(zVal,azO
7d10: 66 66 5b 69 5d 29 3d 3d 30 20 29 20 72 65 74 75  ff[i])==0 ) retu
7d20: 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rn 1;.  }.  retu
7d30: 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  rn 0;.}../*.** S
7d40: 77 61 70 20 74 68 65 20 67 2e 64 62 20 61 6e 64  wap the g.db and
7d50: 20 67 2e 64 62 43 6f 6e 66 69 67 20 63 6f 6e 6e   g.dbConfig conn
7d60: 65 63 74 69 6f 6e 73 20 73 6f 20 74 68 61 74 20  ections so that 
7d70: 74 68 65 20 76 61 72 69 6f 75 73 20 64 62 5f 2a  the various db_*
7d80: 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 77 6f 72   routines.** wor
7d90: 6b 20 6f 6e 20 74 68 65 20 7e 2f 2e 66 6f 73 73  k on the ~/.foss
7da0: 69 6c 20 64 61 74 61 62 61 73 65 20 69 6e 73 74  il database inst
7db0: 65 61 64 20 6f 66 20 6f 6e 20 74 68 65 20 72 65  ead of on the re
7dc0: 70 6f 73 69 74 6f 72 79 20 64 61 74 61 62 61 73  pository databas
7dd0: 65 2e 0a 2a 2a 20 42 65 20 73 75 72 65 20 74 6f  e..** Be sure to
7de0: 20 73 77 61 70 20 74 68 65 6d 20 62 61 63 6b 20   swap them back 
7df0: 61 66 74 65 72 20 64 6f 69 6e 67 20 74 68 65 20  after doing the 
7e00: 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  operation..**.**
7e10: 20 49 66 20 67 2e 75 73 65 41 74 74 61 63 68 20   If g.useAttach 
7e20: 74 68 61 74 20 6d 65 61 6e 73 20 74 68 65 20 7e  that means the ~
7e30: 2f 2e 66 6f 73 73 69 6c 20 64 61 74 61 62 61 73  /.fossil databas
7e40: 65 20 77 61 73 20 6f 70 65 6e 65 64 20 77 69 74  e was opened wit
7e50: 68 0a 2a 2a 20 74 68 65 20 75 73 65 41 74 74 61  h.** the useAtta
7e60: 63 68 20 66 6c 61 67 20 73 65 74 20 74 6f 20 31  ch flag set to 1
7e70: 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73 65 20  .  In that case 
7e80: 6e 6f 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 73 77  no connection sw
7e90: 61 70 20 69 73 20 72 65 71 75 69 72 65 64 0a 2a  ap is required.*
7ea0: 2a 20 73 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  * so this routin
7eb0: 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  e is a no-op..*/
7ec0: 0a 76 6f 69 64 20 64 62 5f 73 77 61 70 5f 63 6f  .void db_swap_co
7ed0: 6e 6e 65 63 74 69 6f 6e 73 28 76 6f 69 64 29 7b  nnections(void){
7ee0: 0a 20 20 69 66 28 20 21 67 2e 75 73 65 41 74 74  .  if( !g.useAtt
7ef0: 61 63 68 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ach ){.    sqlit
7f00: 65 33 20 2a 64 62 54 65 6d 70 20 3d 20 67 2e 64  e3 *dbTemp = g.d
7f10: 62 3b 0a 20 20 20 20 67 2e 64 62 20 3d 20 67 2e  b;.    g.db = g.
7f20: 64 62 43 6f 6e 66 69 67 3b 0a 20 20 20 20 67 2e  dbConfig;.    g.
7f30: 64 62 43 6f 6e 66 69 67 20 3d 20 64 62 54 65 6d  dbConfig = dbTem
7f40: 70 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  p;.  }.}../*.** 
7f50: 47 65 74 20 61 6e 64 20 73 65 74 20 76 61 6c 75  Get and set valu
7f60: 65 73 20 66 72 6f 6d 20 74 68 65 20 43 4f 4e 46  es from the CONF
7f70: 49 47 2c 20 47 4c 4f 42 41 4c 5f 43 4f 4e 46 49  IG, GLOBAL_CONFI
7f80: 47 20 61 6e 64 20 56 56 41 52 20 74 61 62 6c 65  G and VVAR table
7f90: 20 69 6e 20 74 68 65 0a 2a 2a 20 72 65 70 6f 73   in the.** repos
7fa0: 69 74 6f 72 79 20 61 6e 64 20 6c 6f 63 61 6c 20  itory and local 
7fb0: 64 61 74 61 62 61 73 65 73 2e 0a 2a 2f 0a 63 68  databases..*/.ch
7fc0: 61 72 20 2a 64 62 5f 67 65 74 28 63 6f 6e 73 74  ar *db_get(const
7fd0: 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 68   char *zName, ch
7fe0: 61 72 20 2a 7a 44 65 66 61 75 6c 74 29 7b 0a 20  ar *zDefault){. 
7ff0: 20 63 68 61 72 20 2a 7a 20 3d 20 30 3b 0a 20 20   char *z = 0;.  
8000: 69 66 28 20 67 2e 72 65 70 6f 73 69 74 6f 72 79  if( g.repository
8010: 4f 70 65 6e 20 29 7b 0a 20 20 20 20 7a 20 3d 20  Open ){.    z = 
8020: 64 62 5f 74 65 78 74 28 30 2c 20 22 53 45 4c 45  db_text(0, "SELE
8030: 43 54 20 76 61 6c 75 65 20 46 52 4f 4d 20 63 6f  CT value FROM co
8040: 6e 66 69 67 20 57 48 45 52 45 20 6e 61 6d 65 3d  nfig WHERE name=
8050: 25 51 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 7d  %Q", zName);.  }
8060: 0a 20 20 69 66 28 20 7a 3d 3d 30 20 26 26 20 67  .  if( z==0 && g
8070: 2e 63 6f 6e 66 69 67 4f 70 65 6e 20 29 7b 0a 20  .configOpen ){. 
8080: 20 20 20 64 62 5f 73 77 61 70 5f 63 6f 6e 6e 65     db_swap_conne
8090: 63 74 69 6f 6e 73 28 29 3b 0a 20 20 20 20 7a 20  ctions();.    z 
80a0: 3d 20 64 62 5f 74 65 78 74 28 30 2c 20 22 53 45  = db_text(0, "SE
80b0: 4c 45 43 54 20 76 61 6c 75 65 20 46 52 4f 4d 20  LECT value FROM 
80c0: 67 6c 6f 62 61 6c 5f 63 6f 6e 66 69 67 20 57 48  global_config WH
80d0: 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e  ERE name=%Q", zN
80e0: 61 6d 65 29 3b 0a 20 20 20 20 64 62 5f 73 77 61  ame);.    db_swa
80f0: 70 5f 63 6f 6e 6e 65 63 74 69 6f 6e 73 28 29 3b  p_connections();
8100: 0a 20 20 7d 0a 20 20 69 66 28 20 7a 3d 3d 30 20  .  }.  if( z==0 
8110: 29 7b 0a 20 20 20 20 7a 20 3d 20 7a 44 65 66 61  ){.    z = zDefa
8120: 75 6c 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ult;.  }.  retur
8130: 6e 20 7a 3b 0a 7d 0a 76 6f 69 64 20 64 62 5f 73  n z;.}.void db_s
8140: 65 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  et(const char *z
8150: 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  Name, const char
8160: 20 2a 7a 56 61 6c 75 65 2c 20 69 6e 74 20 67 6c   *zValue, int gl
8170: 6f 62 61 6c 46 6c 61 67 29 7b 0a 20 20 64 62 5f  obalFlag){.  db_
8180: 62 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f  begin_transactio
8190: 6e 28 29 3b 0a 20 20 69 66 28 20 67 6c 6f 62 61  n();.  if( globa
81a0: 6c 46 6c 61 67 20 29 7b 0a 20 20 20 20 64 62 5f  lFlag ){.    db_
81b0: 73 77 61 70 5f 63 6f 6e 6e 65 63 74 69 6f 6e 73  swap_connections
81c0: 28 29 3b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69  ();.    db_multi
81d0: 5f 65 78 65 63 28 22 52 45 50 4c 41 43 45 20 49  _exec("REPLACE I
81e0: 4e 54 4f 20 67 6c 6f 62 61 6c 5f 63 6f 6e 66 69  NTO global_confi
81f0: 67 28 6e 61 6d 65 2c 76 61 6c 75 65 29 20 56 41  g(name,value) VA
8200: 4c 55 45 53 28 25 51 2c 25 51 29 22 2c 0a 20 20  LUES(%Q,%Q)",.  
8210: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8220: 20 7a 4e 61 6d 65 2c 20 7a 56 61 6c 75 65 29 3b   zName, zValue);
8230: 0a 20 20 20 20 64 62 5f 73 77 61 70 5f 63 6f 6e  .    db_swap_con
8240: 6e 65 63 74 69 6f 6e 73 28 29 3b 0a 20 20 7d 65  nections();.  }e
8250: 6c 73 65 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74  lse{.    db_mult
8260: 69 5f 65 78 65 63 28 22 52 45 50 4c 41 43 45 20  i_exec("REPLACE 
8270: 49 4e 54 4f 20 63 6f 6e 66 69 67 28 6e 61 6d 65  INTO config(name
8280: 2c 76 61 6c 75 65 29 20 56 41 4c 55 45 53 28 25  ,value) VALUES(%
8290: 51 2c 25 51 29 22 2c 0a 20 20 20 20 20 20 20 20  Q,%Q)",.        
82a0: 20 20 20 20 20 20 20 20 20 20 20 7a 4e 61 6d 65             zName
82b0: 2c 20 7a 56 61 6c 75 65 29 3b 0a 20 20 7d 0a 20  , zValue);.  }. 
82c0: 20 69 66 28 20 67 6c 6f 62 61 6c 46 6c 61 67 20   if( globalFlag 
82d0: 26 26 20 67 2e 72 65 70 6f 73 69 74 6f 72 79 4f  && g.repositoryO
82e0: 70 65 6e 20 29 7b 0a 20 20 20 20 64 62 5f 6d 75  pen ){.    db_mu
82f0: 6c 74 69 5f 65 78 65 63 28 22 44 45 4c 45 54 45  lti_exec("DELETE
8300: 20 46 52 4f 4d 20 63 6f 6e 66 69 67 20 57 48 45   FROM config WHE
8310: 52 45 20 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e 61  RE name=%Q", zNa
8320: 6d 65 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 65 6e  me);.  }.  db_en
8330: 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29  d_transaction(0)
8340: 3b 0a 7d 0a 76 6f 69 64 20 64 62 5f 75 6e 73 65  ;.}.void db_unse
8350: 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  t(const char *zN
8360: 61 6d 65 2c 20 69 6e 74 20 67 6c 6f 62 61 6c 46  ame, int globalF
8370: 6c 61 67 29 7b 0a 20 20 64 62 5f 62 65 67 69 6e  lag){.  db_begin
8380: 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29 3b 0a  _transaction();.
8390: 20 20 69 66 28 20 67 6c 6f 62 61 6c 46 6c 61 67    if( globalFlag
83a0: 20 29 7b 0a 20 20 20 20 64 62 5f 73 77 61 70 5f   ){.    db_swap_
83b0: 63 6f 6e 6e 65 63 74 69 6f 6e 73 28 29 3b 0a 20  connections();. 
83c0: 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63     db_multi_exec
83d0: 28 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 67 6c  ("DELETE FROM gl
83e0: 6f 62 61 6c 5f 63 6f 6e 66 69 67 20 57 48 45 52  obal_config WHER
83f0: 45 20 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d  E name=%Q", zNam
8400: 65 29 3b 0a 20 20 20 20 64 62 5f 73 77 61 70 5f  e);.    db_swap_
8410: 63 6f 6e 6e 65 63 74 69 6f 6e 73 28 29 3b 0a 20  connections();. 
8420: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 5f 6d   }else{.    db_m
8430: 75 6c 74 69 5f 65 78 65 63 28 22 44 45 4c 45 54  ulti_exec("DELET
8440: 45 20 46 52 4f 4d 20 63 6f 6e 66 69 67 20 57 48  E FROM config WH
8450: 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e  ERE name=%Q", zN
8460: 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ame);.  }.  if( 
8470: 67 6c 6f 62 61 6c 46 6c 61 67 20 26 26 20 67 2e  globalFlag && g.
8480: 72 65 70 6f 73 69 74 6f 72 79 4f 70 65 6e 20 29  repositoryOpen )
8490: 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65  {.    db_multi_e
84a0: 78 65 63 28 22 44 45 4c 45 54 45 20 46 52 4f 4d  xec("DELETE FROM
84b0: 20 63 6f 6e 66 69 67 20 57 48 45 52 45 20 6e 61   config WHERE na
84c0: 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d 65 29 3b 0a  me=%Q", zName);.
84d0: 20 20 7d 0a 20 20 64 62 5f 65 6e 64 5f 74 72 61    }.  db_end_tra
84e0: 6e 73 61 63 74 69 6f 6e 28 30 29 3b 0a 7d 0a 69  nsaction(0);.}.i
84f0: 6e 74 20 64 62 5f 69 73 5f 67 6c 6f 62 61 6c 28  nt db_is_global(
8500: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
8510: 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30  e){.  int rc = 0
8520: 3b 0a 20 20 69 66 28 20 67 2e 63 6f 6e 66 69 67  ;.  if( g.config
8530: 4f 70 65 6e 20 29 7b 0a 20 20 20 20 64 62 5f 73  Open ){.    db_s
8540: 77 61 70 5f 63 6f 6e 6e 65 63 74 69 6f 6e 73 28  wap_connections(
8550: 29 3b 0a 20 20 20 20 72 63 20 3d 20 64 62 5f 65  );.    rc = db_e
8560: 78 69 73 74 73 28 22 53 45 4c 45 43 54 20 31 20  xists("SELECT 1 
8570: 46 52 4f 4d 20 67 6c 6f 62 61 6c 5f 63 6f 6e 66  FROM global_conf
8580: 69 67 20 57 48 45 52 45 20 6e 61 6d 65 3d 25 51  ig WHERE name=%Q
8590: 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 64  ", zName);.    d
85a0: 62 5f 73 77 61 70 5f 63 6f 6e 6e 65 63 74 69 6f  b_swap_connectio
85b0: 6e 73 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ns();.  }.  retu
85c0: 72 6e 20 72 63 3b 0a 7d 0a 69 6e 74 20 64 62 5f  rn rc;.}.int db_
85d0: 67 65 74 5f 69 6e 74 28 63 6f 6e 73 74 20 63 68  get_int(const ch
85e0: 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 64  ar *zName, int d
85f0: 66 6c 74 29 7b 0a 20 20 69 6e 74 20 76 20 3d 20  flt){.  int v = 
8600: 64 66 6c 74 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  dflt;.  int rc;.
8610: 20 20 69 66 28 20 67 2e 72 65 70 6f 73 69 74 6f    if( g.reposito
8620: 72 79 4f 70 65 6e 20 29 7b 0a 20 20 20 20 53 74  ryOpen ){.    St
8630: 6d 74 20 71 3b 0a 20 20 20 20 64 62 5f 70 72 65  mt q;.    db_pre
8640: 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45 43 54  pare(&q, "SELECT
8650: 20 76 61 6c 75 65 20 46 52 4f 4d 20 63 6f 6e 66   value FROM conf
8660: 69 67 20 57 48 45 52 45 20 6e 61 6d 65 3d 25 51  ig WHERE name=%Q
8670: 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72  ", zName);.    r
8680: 63 20 3d 20 64 62 5f 73 74 65 70 28 26 71 29 3b  c = db_step(&q);
8690: 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
86a0: 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20  ITE_ROW ){.     
86b0: 20 76 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69   v = db_column_i
86c0: 6e 74 28 26 71 2c 20 30 29 3b 0a 20 20 20 20 7d  nt(&q, 0);.    }
86d0: 0a 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65  .    db_finalize
86e0: 28 26 71 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  (&q);.  }else{. 
86f0: 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44     rc = SQLITE_D
8700: 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  ONE;.  }.  if( r
8710: 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 26  c==SQLITE_DONE &
8720: 26 20 67 2e 63 6f 6e 66 69 67 4f 70 65 6e 20 29  & g.configOpen )
8730: 7b 0a 20 20 20 20 64 62 5f 73 77 61 70 5f 63 6f  {.    db_swap_co
8740: 6e 6e 65 63 74 69 6f 6e 73 28 29 3b 0a 20 20 20  nnections();.   
8750: 20 76 20 3d 20 64 62 5f 69 6e 74 28 64 66 6c 74   v = db_int(dflt
8760: 2c 20 22 53 45 4c 45 43 54 20 76 61 6c 75 65 20  , "SELECT value 
8770: 46 52 4f 4d 20 67 6c 6f 62 61 6c 5f 63 6f 6e 66  FROM global_conf
8780: 69 67 20 57 48 45 52 45 20 6e 61 6d 65 3d 25 51  ig WHERE name=%Q
8790: 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 64  ", zName);.    d
87a0: 62 5f 73 77 61 70 5f 63 6f 6e 6e 65 63 74 69 6f  b_swap_connectio
87b0: 6e 73 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ns();.  }.  retu
87c0: 72 6e 20 76 3b 0a 7d 0a 76 6f 69 64 20 64 62 5f  rn v;.}.void db_
87d0: 73 65 74 5f 69 6e 74 28 63 6f 6e 73 74 20 63 68  set_int(const ch
87e0: 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 76  ar *zName, int v
87f0: 61 6c 75 65 2c 20 69 6e 74 20 67 6c 6f 62 61 6c  alue, int global
8800: 46 6c 61 67 29 7b 0a 20 20 69 66 28 20 67 6c 6f  Flag){.  if( glo
8810: 62 61 6c 46 6c 61 67 20 29 7b 0a 20 20 20 20 64  balFlag ){.    d
8820: 62 5f 73 77 61 70 5f 63 6f 6e 6e 65 63 74 69 6f  b_swap_connectio
8830: 6e 73 28 29 3b 0a 20 20 20 20 64 62 5f 6d 75 6c  ns();.    db_mul
8840: 74 69 5f 65 78 65 63 28 22 52 45 50 4c 41 43 45  ti_exec("REPLACE
8850: 20 49 4e 54 4f 20 67 6c 6f 62 61 6c 5f 63 6f 6e   INTO global_con
8860: 66 69 67 28 6e 61 6d 65 2c 76 61 6c 75 65 29 20  fig(name,value) 
8870: 56 41 4c 55 45 53 28 25 51 2c 25 64 29 22 2c 0a  VALUES(%Q,%d)",.
8880: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8890: 20 20 7a 4e 61 6d 65 2c 20 76 61 6c 75 65 29 3b    zName, value);
88a0: 0a 20 20 20 20 64 62 5f 73 77 61 70 5f 63 6f 6e  .    db_swap_con
88b0: 6e 65 63 74 69 6f 6e 73 28 29 3b 0a 20 20 7d 65  nections();.  }e
88c0: 6c 73 65 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74  lse{.    db_mult
88d0: 69 5f 65 78 65 63 28 22 52 45 50 4c 41 43 45 20  i_exec("REPLACE 
88e0: 49 4e 54 4f 20 63 6f 6e 66 69 67 28 6e 61 6d 65  INTO config(name
88f0: 2c 76 61 6c 75 65 29 20 56 41 4c 55 45 53 28 25  ,value) VALUES(%
8900: 51 2c 25 64 29 22 2c 0a 20 20 20 20 20 20 20 20  Q,%d)",.        
8910: 20 20 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 2c            zName,
8920: 20 76 61 6c 75 65 29 3b 0a 20 20 7d 0a 20 20 69   value);.  }.  i
8930: 66 28 20 67 6c 6f 62 61 6c 46 6c 61 67 20 26 26  f( globalFlag &&
8940: 20 67 2e 72 65 70 6f 73 69 74 6f 72 79 4f 70 65   g.repositoryOpe
8950: 6e 20 29 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74  n ){.    db_mult
8960: 69 5f 65 78 65 63 28 22 44 45 4c 45 54 45 20 46  i_exec("DELETE F
8970: 52 4f 4d 20 63 6f 6e 66 69 67 20 57 48 45 52 45  ROM config WHERE
8980: 20 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d 65   name=%Q", zName
8990: 29 3b 0a 20 20 7d 0a 7d 0a 69 6e 74 20 64 62 5f  );.  }.}.int db_
89a0: 67 65 74 5f 62 6f 6f 6c 65 61 6e 28 63 6f 6e 73  get_boolean(cons
89b0: 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69  t char *zName, i
89c0: 6e 74 20 64 66 6c 74 29 7b 0a 20 20 63 68 61 72  nt dflt){.  char
89d0: 20 2a 7a 56 61 6c 20 3d 20 64 62 5f 67 65 74 28   *zVal = db_get(
89e0: 7a 4e 61 6d 65 2c 20 64 66 6c 74 20 3f 20 22 6f  zName, dflt ? "o
89f0: 6e 22 20 3a 20 22 6f 66 66 22 29 3b 0a 20 20 69  n" : "off");.  i
8a00: 66 28 20 69 73 5f 74 72 75 74 68 28 7a 56 61 6c  f( is_truth(zVal
8a10: 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  ) ) return 1;.  
8a20: 69 66 28 20 69 73 5f 66 61 6c 73 65 28 7a 56 61  if( is_false(zVa
8a30: 6c 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  l) ) return 0;. 
8a40: 20 72 65 74 75 72 6e 20 64 66 6c 74 3b 0a 7d 0a   return dflt;.}.
8a50: 63 68 61 72 20 2a 64 62 5f 6c 67 65 74 28 63 6f  char *db_lget(co
8a60: 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
8a70: 20 63 68 61 72 20 2a 7a 44 65 66 61 75 6c 74 29   char *zDefault)
8a80: 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 5f 74 65  {.  return db_te
8a90: 78 74 28 28 63 68 61 72 2a 29 7a 44 65 66 61 75  xt((char*)zDefau
8aa0: 6c 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  lt,.            
8ab0: 20 20 20 20 20 22 53 45 4c 45 43 54 20 76 61 6c       "SELECT val
8ac0: 75 65 20 46 52 4f 4d 20 76 76 61 72 20 57 48 45  ue FROM vvar WHE
8ad0: 52 45 20 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e 61  RE name=%Q", zNa
8ae0: 6d 65 29 3b 0a 7d 0a 76 6f 69 64 20 64 62 5f 6c  me);.}.void db_l
8af0: 73 65 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  set(const char *
8b00: 7a 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61  zName, const cha
8b10: 72 20 2a 7a 56 61 6c 75 65 29 7b 0a 20 20 64 62  r *zValue){.  db
8b20: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 52 45 50  _multi_exec("REP
8b30: 4c 41 43 45 20 49 4e 54 4f 20 76 76 61 72 28 6e  LACE INTO vvar(n
8b40: 61 6d 65 2c 76 61 6c 75 65 29 20 56 41 4c 55 45  ame,value) VALUE
8b50: 53 28 25 51 2c 25 51 29 22 2c 20 7a 4e 61 6d 65  S(%Q,%Q)", zName
8b60: 2c 20 7a 56 61 6c 75 65 29 3b 0a 7d 0a 69 6e 74  , zValue);.}.int
8b70: 20 64 62 5f 6c 67 65 74 5f 69 6e 74 28 63 6f 6e   db_lget_int(con
8b80: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
8b90: 69 6e 74 20 64 66 6c 74 29 7b 0a 20 20 72 65 74  int dflt){.  ret
8ba0: 75 72 6e 20 64 62 5f 69 6e 74 28 64 66 6c 74 2c  urn db_int(dflt,
8bb0: 20 22 53 45 4c 45 43 54 20 76 61 6c 75 65 20 46   "SELECT value F
8bc0: 52 4f 4d 20 76 76 61 72 20 57 48 45 52 45 20 6e  ROM vvar WHERE n
8bd0: 61 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d 65 29 3b  ame=%Q", zName);
8be0: 0a 7d 0a 76 6f 69 64 20 64 62 5f 6c 73 65 74 5f  .}.void db_lset_
8bf0: 69 6e 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  int(const char *
8c00: 7a 4e 61 6d 65 2c 20 69 6e 74 20 76 61 6c 75 65  zName, int value
8c10: 29 7b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78  ){.  db_multi_ex
8c20: 65 63 28 22 52 45 50 4c 41 43 45 20 49 4e 54 4f  ec("REPLACE INTO
8c30: 20 76 76 61 72 28 6e 61 6d 65 2c 76 61 6c 75 65   vvar(name,value
8c40: 29 20 56 41 4c 55 45 53 28 25 51 2c 25 64 29 22  ) VALUES(%Q,%d)"
8c50: 2c 20 7a 4e 61 6d 65 2c 20 76 61 6c 75 65 29 3b  , zName, value);
8c60: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64  .}../*.** Record
8c70: 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 6c   the name of a l
8c80: 6f 63 61 6c 20 72 65 70 6f 73 69 74 6f 72 79 20  ocal repository 
8c90: 69 6e 20 74 68 65 20 67 6c 6f 62 61 6c 5f 63 6f  in the global_co
8ca0: 6e 66 69 67 28 29 20 64 61 74 61 62 61 73 65 2e  nfig() database.
8cb0: 0a 2a 2a 20 54 68 65 20 72 65 70 6f 73 74 69 72  .** The repostir
8cc0: 6f 79 20 66 69 6c 65 6e 61 6d 65 20 25 73 20 69  oy filename %s i
8cd0: 73 20 72 65 63 6f 72 64 65 64 20 61 73 20 61 6e  s recorded as an
8ce0: 20 65 6e 74 72 79 20 77 69 74 68 20 61 20 22 6e   entry with a "n
8cf0: 61 6d 65 22 20 66 69 65 6c 64 0a 2a 2a 20 6f 66  ame" field.** of
8d00: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   the following f
8d10: 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  orm:.**.**      
8d20: 20 72 65 70 6f 3a 25 73 0a 2a 2a 0a 2a 2a 20 54   repo:%s.**.** T
8d30: 68 65 20 76 61 6c 75 65 20 66 69 65 6c 64 20 69  he value field i
8d40: 73 20 73 65 74 20 74 6f 20 31 2e 0a 2a 2f 0a 76  s set to 1..*/.v
8d50: 6f 69 64 20 64 62 5f 72 65 63 6f 72 64 5f 72 65  oid db_record_re
8d60: 70 6f 73 69 74 6f 72 79 5f 66 69 6c 65 6e 61 6d  pository_filenam
8d70: 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  e(const char *zN
8d80: 61 6d 65 29 7b 0a 20 20 42 6c 6f 62 20 66 75 6c  ame){.  Blob ful
8d90: 6c 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d  l;.  if( zName==
8da0: 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21 67 2e  0 ){.    if( !g.
8db0: 6c 6f 63 61 6c 4f 70 65 6e 20 29 20 72 65 74 75  localOpen ) retu
8dc0: 72 6e 3b 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20  rn;.    zName = 
8dd0: 64 62 5f 6c 67 65 74 28 22 72 65 70 6f 73 69 74  db_lget("reposit
8de0: 6f 72 79 22 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  ory", 0);.  }.  
8df0: 66 69 6c 65 5f 63 61 6e 6f 6e 69 63 61 6c 5f 6e  file_canonical_n
8e00: 61 6d 65 28 7a 4e 61 6d 65 2c 20 26 66 75 6c 6c  ame(zName, &full
8e10: 29 3b 0a 20 20 64 62 5f 73 77 61 70 5f 63 6f 6e  );.  db_swap_con
8e20: 6e 65 63 74 69 6f 6e 73 28 29 3b 0a 20 20 64 62  nections();.  db
8e30: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20  _multi_exec(.   
8e40: 20 20 22 49 4e 53 45 52 54 20 4f 52 20 49 47 4e    "INSERT OR IGN
8e50: 4f 52 45 20 49 4e 54 4f 20 67 6c 6f 62 61 6c 5f  ORE INTO global_
8e60: 63 6f 6e 66 69 67 28 6e 61 6d 65 2c 76 61 6c 75  config(name,valu
8e70: 65 29 22 0a 20 20 20 20 20 22 56 41 4c 55 45 53  e)".     "VALUES
8e80: 28 27 72 65 70 6f 3a 25 71 27 2c 31 29 22 2c 0a  ('repo:%q',1)",.
8e90: 20 20 20 20 20 62 6c 6f 62 5f 73 74 72 28 26 66       blob_str(&f
8ea0: 75 6c 6c 29 0a 20 20 29 3b 0a 20 20 64 62 5f 73  ull).  );.  db_s
8eb0: 77 61 70 5f 63 6f 6e 6e 65 63 74 69 6f 6e 73 28  wap_connections(
8ec0: 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28  );.  blob_reset(
8ed0: 26 66 75 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  &full);.}../*.**
8ee0: 20 43 4f 4d 4d 41 4e 44 3a 20 6f 70 65 6e 0a 2a   COMMAND: open.*
8ef0: 2a 0a 2a 2a 20 55 73 61 67 65 3a 20 25 66 6f 73  *.** Usage: %fos
8f00: 73 69 6c 20 6f 70 65 6e 20 46 49 4c 45 4e 41 4d  sil open FILENAM
8f10: 45 20 3f 56 45 52 53 49 4f 4e 3f 20 3f 2d 2d 6b  E ?VERSION? ?--k
8f20: 65 65 70 3f 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20  eep?.**.** Open 
8f30: 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20  a connection to 
8f40: 74 68 65 20 6c 6f 63 61 6c 20 72 65 70 6f 73 69  the local reposi
8f50: 74 6f 72 79 20 69 6e 20 46 49 4c 45 4e 41 4d 45  tory in FILENAME
8f60: 2e 20 20 41 20 63 68 65 63 6b 6f 75 74 0a 2a 2a  .  A checkout.**
8f70: 20 66 6f 72 20 74 68 65 20 72 65 70 6f 73 69 74   for the reposit
8f80: 6f 72 79 20 69 73 20 63 72 65 61 74 65 64 20 77  ory is created w
8f90: 69 74 68 20 69 74 73 20 72 6f 6f 74 20 61 74 20  ith its root at 
8fa0: 74 68 65 20 77 6f 72 6b 69 6e 67 20 64 69 72 65  the working dire
8fb0: 63 74 6f 72 79 2e 0a 2a 2a 20 49 66 20 56 45 52  ctory..** If VER
8fc0: 53 49 4f 4e 20 69 73 20 73 70 65 63 69 66 69 65  SION is specifie
8fd0: 64 20 74 68 65 6e 20 74 68 61 74 20 76 65 72 73  d then that vers
8fe0: 69 6f 6e 20 69 73 20 63 68 65 63 6b 65 64 20 6f  ion is checked o
8ff0: 75 74 2e 20 20 4f 74 68 65 72 77 69 73 65 0a 2a  ut.  Otherwise.*
9000: 2a 20 74 68 65 20 6c 61 74 65 73 74 20 76 65 72  * the latest ver
9010: 73 69 6f 6e 20 69 73 20 63 68 65 63 6b 65 64 20  sion is checked 
9020: 6f 75 74 2e 20 20 4e 6f 20 66 69 6c 65 73 20 6f  out.  No files o
9030: 74 68 65 72 20 74 68 61 6e 20 22 6d 61 6e 69 66  ther than "manif
9040: 65 73 74 22 0a 2a 2a 20 61 6e 64 20 22 6d 61 6e  est".** and "man
9050: 69 66 65 73 74 2e 75 75 69 64 22 20 61 72 65 20  ifest.uuid" are 
9060: 6d 6f 64 69 66 69 65 64 20 69 66 20 74 68 65 20  modified if the 
9070: 2d 2d 6b 65 65 70 20 6f 70 74 69 6f 6e 20 69 73  --keep option is
9080: 20 70 72 65 73 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20   present..**.** 
9090: 53 65 65 20 61 6c 73 6f 20 74 68 65 20 22 63 6c  See also the "cl
90a0: 6f 73 65 22 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f  ose" command..*/
90b0: 0a 76 6f 69 64 20 63 6d 64 5f 6f 70 65 6e 28 76  .void cmd_open(v
90c0: 6f 69 64 29 7b 0a 20 20 42 6c 6f 62 20 70 61 74  oid){.  Blob pat
90d0: 68 3b 0a 20 20 69 6e 74 20 76 69 64 3b 0a 20 20  h;.  int vid;.  
90e0: 69 6e 74 20 6b 65 65 70 46 6c 61 67 3b 0a 20 20  int keepFlag;.  
90f0: 73 74 61 74 69 63 20 63 68 61 72 20 2a 61 7a 4e  static char *azN
9100: 65 77 41 72 67 76 5b 5d 20 3d 20 7b 20 30 2c 20  ewArgv[] = { 0, 
9110: 22 63 68 65 63 6b 6f 75 74 22 2c 20 22 2d 2d 6c  "checkout", "--l
9120: 61 74 65 73 74 22 2c 20 30 2c 20 30 2c 20 30 20  atest", 0, 0, 0 
9130: 7d 3b 0a 20 20 75 72 6c 5f 70 72 6f 78 79 5f 6f  };.  url_proxy_o
9140: 70 74 69 6f 6e 73 28 29 3b 0a 20 20 6b 65 65 70  ptions();.  keep
9150: 46 6c 61 67 20 3d 20 66 69 6e 64 5f 6f 70 74 69  Flag = find_opti
9160: 6f 6e 28 22 6b 65 65 70 22 2c 30 2c 30 29 21 3d  on("keep",0,0)!=
9170: 30 3b 0a 20 20 69 66 28 20 67 2e 61 72 67 63 21  0;.  if( g.argc!
9180: 3d 33 20 26 26 20 67 2e 61 72 67 63 21 3d 34 20  =3 && g.argc!=4 
9190: 29 7b 0a 20 20 20 20 75 73 61 67 65 28 22 52 45  ){.    usage("RE
91a0: 50 4f 53 49 54 4f 52 59 2d 46 49 4c 45 4e 41 4d  POSITORY-FILENAM
91b0: 45 20 3f 56 45 52 53 49 4f 4e 3f 22 29 3b 0a 20  E ?VERSION?");. 
91c0: 20 7d 0a 20 20 69 66 28 20 64 62 5f 6f 70 65 6e   }.  if( db_open
91d0: 5f 6c 6f 63 61 6c 28 29 20 29 7b 0a 20 20 20 20  _local() ){.    
91e0: 66 6f 73 73 69 6c 5f 70 61 6e 69 63 28 22 61 6c  fossil_panic("al
91f0: 72 65 61 64 79 20 77 69 74 68 69 6e 20 61 6e 20  ready within an 
9200: 6f 70 65 6e 20 74 72 65 65 20 72 6f 6f 74 65 64  open tree rooted
9210: 20 61 74 20 25 73 22 2c 20 67 2e 7a 4c 6f 63 61   at %s", g.zLoca
9220: 6c 52 6f 6f 74 29 3b 0a 20 20 7d 0a 20 20 66 69  lRoot);.  }.  fi
9230: 6c 65 5f 63 61 6e 6f 6e 69 63 61 6c 5f 6e 61 6d  le_canonical_nam
9240: 65 28 67 2e 61 72 67 76 5b 32 5d 2c 20 26 70 61  e(g.argv[2], &pa
9250: 74 68 29 3b 0a 20 20 64 62 5f 6f 70 65 6e 5f 72  th);.  db_open_r
9260: 65 70 6f 73 69 74 6f 72 79 28 62 6c 6f 62 5f 73  epository(blob_s
9270: 74 72 28 26 70 61 74 68 29 29 3b 0a 20 20 64 62  tr(&path));.  db
9280: 5f 69 6e 69 74 5f 64 61 74 61 62 61 73 65 28 22  _init_database("
9290: 2e 2f 5f 46 4f 53 53 49 4c 5f 22 2c 20 7a 4c 6f  ./_FOSSIL_", zLo
92a0: 63 61 6c 53 63 68 65 6d 61 2c 20 28 63 68 61 72  calSchema, (char
92b0: 2a 29 30 29 3b 0a 20 20 64 62 5f 6f 70 65 6e 5f  *)0);.  db_open_
92c0: 6c 6f 63 61 6c 28 29 3b 0a 20 20 64 62 5f 6c 73  local();.  db_ls
92d0: 65 74 28 22 72 65 70 6f 73 69 74 6f 72 79 22 2c  et("repository",
92e0: 20 62 6c 6f 62 5f 73 74 72 28 26 70 61 74 68 29   blob_str(&path)
92f0: 29 3b 0a 20 20 64 62 5f 72 65 63 6f 72 64 5f 72  );.  db_record_r
9300: 65 70 6f 73 69 74 6f 72 79 5f 66 69 6c 65 6e 61  epository_filena
9310: 6d 65 28 62 6c 6f 62 5f 73 74 72 28 26 70 61 74  me(blob_str(&pat
9320: 68 29 29 3b 0a 20 20 76 69 64 20 3d 20 64 62 5f  h));.  vid = db_
9330: 69 6e 74 28 30 2c 20 22 53 45 4c 45 43 54 20 70  int(0, "SELECT p
9340: 69 64 20 46 52 4f 4d 20 70 6c 69 6e 6b 20 79 22  id FROM plink y"
9350: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
9360: 20 20 20 22 20 57 48 45 52 45 20 4e 4f 54 20 45     " WHERE NOT E
9370: 58 49 53 54 53 28 53 45 4c 45 43 54 20 31 20 46  XISTS(SELECT 1 F
9380: 52 4f 4d 20 70 6c 69 6e 6b 20 78 20 57 48 45 52  ROM plink x WHER
9390: 45 20 78 2e 63 69 64 3d 79 2e 70 69 64 29 22 29  E x.cid=y.pid)")
93a0: 3b 0a 20 20 69 66 28 20 76 69 64 3d 3d 30 20 29  ;.  if( vid==0 )
93b0: 7b 0a 20 20 20 20 64 62 5f 6c 73 65 74 5f 69 6e  {.    db_lset_in
93c0: 74 28 22 63 68 65 63 6b 6f 75 74 22 2c 20 31 29  t("checkout", 1)
93d0: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63  ;.  }else{.    c
93e0: 68 61 72 20 2a 2a 6f 6c 64 41 72 67 76 20 3d 20  har **oldArgv = 
93f0: 67 2e 61 72 67 76 3b 0a 20 20 20 20 69 6e 74 20  g.argv;.    int 
9400: 6f 6c 64 41 72 67 63 20 3d 20 67 2e 61 72 67 63  oldArgc = g.argc
9410: 3b 0a 20 20 20 20 64 62 5f 6c 73 65 74 5f 69 6e  ;.    db_lset_in
9420: 74 28 22 63 68 65 63 6b 6f 75 74 22 2c 20 76 69  t("checkout", vi
9430: 64 29 3b 0a 20 20 20 20 61 7a 4e 65 77 41 72 67  d);.    azNewArg
9440: 76 5b 30 5d 20 3d 20 67 2e 61 72 67 76 5b 30 5d  v[0] = g.argv[0]
9450: 3b 0a 20 20 20 20 67 2e 61 72 67 76 20 3d 20 61  ;.    g.argv = a
9460: 7a 4e 65 77 41 72 67 76 3b 0a 20 20 20 20 67 2e  zNewArgv;.    g.
9470: 61 72 67 63 20 3d 20 33 3b 0a 20 20 20 20 69 66  argc = 3;.    if
9480: 28 20 6f 6c 64 41 72 67 63 3d 3d 34 20 29 7b 0a  ( oldArgc==4 ){.
9490: 20 20 20 20 20 20 61 7a 4e 65 77 41 72 67 76 5b        azNewArgv[
94a0: 67 2e 61 72 67 63 2d 31 5d 20 3d 20 6f 6c 64 41  g.argc-1] = oldA
94b0: 72 67 76 5b 33 5d 3b 0a 20 20 20 20 7d 0a 20 20  rgv[3];.    }.  
94c0: 20 20 69 66 28 20 6b 65 65 70 46 6c 61 67 20 29    if( keepFlag )
94d0: 7b 0a 20 20 20 20 20 20 61 7a 4e 65 77 41 72 67  {.      azNewArg
94e0: 76 5b 67 2e 61 72 67 63 2b 2b 5d 20 3d 20 22 2d  v[g.argc++] = "-
94f0: 2d 6b 65 65 70 22 3b 0a 20 20 20 20 7d 0a 20 20  -keep";.    }.  
9500: 20 20 63 68 65 63 6b 6f 75 74 5f 63 6d 64 28 29    checkout_cmd()
9510: 3b 0a 20 20 20 20 67 2e 61 72 67 63 20 3d 20 32  ;.    g.argc = 2
9520: 3b 0a 20 20 20 20 69 6e 66 6f 5f 63 6d 64 28 29  ;.    info_cmd()
9530: 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  ;.  }.}../*.** P
9540: 72 69 6e 74 20 74 68 65 20 76 61 6c 75 65 20 6f  rint the value o
9550: 66 20 61 20 73 65 74 74 69 6e 67 20 6e 61 6d 65  f a setting name
9560: 64 20 7a 4e 61 6d 65 0a 2a 2f 0a 73 74 61 74 69  d zName.*/.stati
9570: 63 20 76 6f 69 64 20 70 72 69 6e 74 5f 73 65 74  c void print_set
9580: 74 69 6e 67 28 63 6f 6e 73 74 20 63 68 61 72 20  ting(const char 
9590: 2a 7a 4e 61 6d 65 29 7b 0a 20 20 53 74 6d 74 20  *zName){.  Stmt 
95a0: 71 3b 0a 20 20 69 66 28 20 67 2e 72 65 70 6f 73  q;.  if( g.repos
95b0: 69 74 6f 72 79 4f 70 65 6e 20 29 7b 0a 20 20 20  itoryOpen ){.   
95c0: 20 64 62 5f 70 72 65 70 61 72 65 28 26 71 2c 0a   db_prepare(&q,.
95d0: 20 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 27         "SELECT '
95e0: 28 6c 6f 63 61 6c 29 27 2c 20 76 61 6c 75 65 20  (local)', value 
95f0: 46 52 4f 4d 20 63 6f 6e 66 69 67 20 57 48 45 52  FROM config WHER
9600: 45 20 6e 61 6d 65 3d 25 51 22 0a 20 20 20 20 20  E name=%Q".     
9610: 20 20 22 20 55 4e 49 4f 4e 20 41 4c 4c 20 22 0a    " UNION ALL ".
9620: 20 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 27         "SELECT '
9630: 28 67 6c 6f 62 61 6c 29 27 2c 20 76 61 6c 75 65  (global)', value
9640: 20 46 52 4f 4d 20 67 6c 6f 62 61 6c 5f 63 6f 6e   FROM global_con
9650: 66 69 67 20 57 48 45 52 45 20 6e 61 6d 65 3d 25  fig WHERE name=%
9660: 51 22 2c 0a 20 20 20 20 20 20 20 7a 4e 61 6d 65  Q",.       zName
9670: 2c 20 7a 4e 61 6d 65 0a 20 20 20 20 29 3b 0a 20  , zName.    );. 
9680: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 5f 70   }else{.    db_p
9690: 72 65 70 61 72 65 28 26 71 2c 0a 20 20 20 20 20  repare(&q,.     
96a0: 20 22 53 45 4c 45 43 54 20 27 28 67 6c 6f 62 61   "SELECT '(globa
96b0: 6c 29 27 2c 20 76 61 6c 75 65 20 46 52 4f 4d 20  l)', value FROM 
96c0: 67 6c 6f 62 61 6c 5f 63 6f 6e 66 69 67 20 57 48  global_config WH
96d0: 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c 0a 20 20  ERE name=%Q",.  
96e0: 20 20 20 20 7a 4e 61 6d 65 0a 20 20 20 20 29 3b      zName.    );
96f0: 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 5f 73 74  .  }.  if( db_st
9700: 65 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52  ep(&q)==SQLITE_R
9710: 4f 57 20 29 7b 0a 20 20 20 20 70 72 69 6e 74 66  OW ){.    printf
9720: 28 22 25 2d 32 30 73 20 25 2d 38 73 20 25 73 5c  ("%-20s %-8s %s\
9730: 6e 22 2c 20 7a 4e 61 6d 65 2c 20 64 62 5f 63 6f  n", zName, db_co
9740: 6c 75 6d 6e 5f 74 65 78 74 28 26 71 2c 20 30 29  lumn_text(&q, 0)
9750: 2c 0a 20 20 20 20 20 20 20 20 64 62 5f 63 6f 6c  ,.        db_col
9760: 75 6d 6e 5f 74 65 78 74 28 26 71 2c 20 31 29 29  umn_text(&q, 1))
9770: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
9780: 72 69 6e 74 66 28 22 25 2d 32 30 73 5c 6e 22 2c  rintf("%-20s\n",
9790: 20 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 64   zName);.  }.  d
97a0: 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a  b_finalize(&q);.
97b0: 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e  }.../*.** COMMAN
97c0: 44 3a 20 73 65 74 74 69 6e 67 73 0a 2a 2a 20 43  D: settings.** C
97d0: 4f 4d 4d 41 4e 44 3a 20 75 6e 73 65 74 0a 2a 2a  OMMAND: unset.**
97e0: 20 25 66 6f 73 73 69 6c 20 73 65 74 74 69 6e 67   %fossil setting
97f0: 20 3f 50 52 4f 50 45 52 54 59 3f 20 3f 56 41 4c   ?PROPERTY? ?VAL
9800: 55 45 3f 20 3f 2d 67 6c 6f 62 61 6c 3f 0a 2a 2a  UE? ?-global?.**
9810: 20 25 66 6f 73 73 69 6c 20 75 6e 73 65 74 20 50   %fossil unset P
9820: 52 4f 50 45 52 54 59 20 3f 2d 67 6c 6f 62 61 6c  ROPERTY ?-global
9830: 3f 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 73 65 74  ?.**.** The "set
9840: 74 69 6e 67 22 20 63 6f 6d 6d 61 6e 64 20 77 69  ting" command wi
9850: 74 68 20 6e 6f 20 61 72 67 75 6d 65 6e 74 73 20  th no arguments 
9860: 6c 69 73 74 73 20 61 6c 6c 20 70 72 6f 70 65 72  lists all proper
9870: 74 69 65 73 20 61 6e 64 20 74 68 65 69 72 0a 2a  ties and their.*
9880: 2a 20 76 61 6c 75 65 73 2e 20 20 57 69 74 68 20  * values.  With 
9890: 6a 75 73 74 20 61 20 70 72 6f 70 65 72 74 79 20  just a property 
98a0: 6e 61 6d 65 20 69 74 20 73 68 6f 77 73 20 74 68  name it shows th
98b0: 65 20 76 61 6c 75 65 20 6f 66 20 74 68 61 74 20  e value of that 
98c0: 70 72 6f 70 65 72 74 79 2e 0a 2a 2a 20 57 69 74  property..** Wit
98d0: 68 20 61 20 76 61 6c 75 65 20 61 72 67 75 6d 65  h a value argume
98e0: 6e 74 20 69 74 20 63 68 61 6e 67 65 73 20 74 68  nt it changes th
98f0: 65 20 70 72 6f 70 65 72 74 79 20 66 6f 72 20 74  e property for t
9900: 68 65 20 63 75 72 72 65 6e 74 20 72 65 70 6f 73  he current repos
9910: 69 74 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  itory..**.** The
9920: 20 22 75 6e 73 65 74 22 20 63 6f 6d 6d 61 6e 64   "unset" command
9930: 20 63 6c 65 61 72 73 20 61 20 70 72 6f 70 65 72   clears a proper
9940: 74 79 20 73 65 74 74 69 6e 67 2e 0a 2a 2a 0a 2a  ty setting..**.*
9950: 2a 20 20 20 20 61 75 74 6f 73 79 6e 63 20 20 20  *    autosync   
9960: 20 20 20 20 20 20 49 66 20 65 6e 61 62 6c 65 64        If enabled
9970: 2c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  , automatically 
9980: 70 75 6c 6c 20 70 72 69 6f 72 20 74 6f 0a 2a 2a  pull prior to.**
9990: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
99a0: 20 20 20 20 20 63 6f 6d 6d 69 74 20 6f 72 20 75       commit or u
99b0: 70 64 61 74 65 20 61 6e 64 20 61 75 74 6f 6d 61  pdate and automa
99c0: 74 69 63 61 6c 6c 79 20 70 75 73 68 0a 2a 2a 20  tically push.** 
99d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
99e0: 20 20 20 20 61 66 74 65 72 20 63 6f 6d 6d 69 74      after commit
99f0: 20 6f 72 20 74 61 67 20 6f 72 20 62 72 61 6e 63   or tag or branc
9a00: 68 20 63 72 65 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  h creation..**.*
9a10: 2a 20 20 20 20 64 69 66 66 2d 63 6f 6d 6d 61 6e  *    diff-comman
9a20: 64 20 20 20 20 20 45 78 74 65 72 6e 61 6c 20 63  d     External c
9a30: 6f 6d 6d 61 6e 64 20 74 6f 20 72 75 6e 20 77 68  ommand to run wh
9a40: 65 6e 20 70 65 72 66 6f 72 6d 69 6e 67 20 61 20  en performing a 
9a50: 64 69 66 66 2e 0a 2a 2a 20 20 20 20 20 20 20 20  diff..**        
9a60: 20 20 20 20 20 20 20 20 20 20 20 20 20 49 66 20               If 
9a70: 75 6e 64 65 66 69 6e 65 64 2c 20 74 68 65 20 69  undefined, the i
9a80: 6e 74 65 72 6e 61 6c 20 74 65 78 74 20 64 69 66  nternal text dif
9a90: 66 20 77 69 6c 6c 20 62 65 20 75 73 65 64 2e 0a  f will be used..
9aa0: 2a 2a 0a 2a 2a 20 20 20 20 65 64 69 74 6f 72 20  **.**    editor 
9ab0: 20 20 20 20 20 20 20 20 20 20 54 65 78 74 20 65            Text e
9ac0: 64 69 74 6f 72 20 63 6f 6d 6d 61 6e 64 20 75 73  ditor command us
9ad0: 65 64 20 66 6f 72 20 63 68 65 63 6b 2d 69 6e 20  ed for check-in 
9ae0: 63 6f 6d 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20  comments..**.** 
9af0: 20 20 20 68 74 74 70 2d 70 6f 72 74 20 20 20 20     http-port    
9b00: 20 20 20 20 54 68 65 20 54 43 50 2f 49 50 20 70      The TCP/IP p
9b10: 6f 72 74 20 6e 75 6d 62 65 72 20 74 6f 20 75 73  ort number to us
9b20: 65 20 62 79 20 74 68 65 20 22 73 65 72 76 65 72  e by the "server
9b30: 22 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  ".**            
9b40: 20 20 20 20 20 20 20 20 20 61 6e 64 20 22 75 69           and "ui
9b50: 22 20 63 6f 6d 6d 61 6e 64 73 2e 20 20 44 65 66  " commands.  Def
9b60: 61 75 6c 74 3a 20 38 30 38 30 0a 2a 2a 0a 2a 2a  ault: 8080.**.**
9b70: 20 20 20 20 67 64 69 66 66 2d 63 6f 6d 6d 61 6e      gdiff-comman
9b80: 64 20 20 20 20 45 78 74 65 72 6e 61 6c 20 63 6f  d    External co
9b90: 6d 6d 61 6e 64 20 74 6f 20 72 75 6e 20 77 68 65  mmand to run whe
9ba0: 6e 20 70 65 72 66 6f 72 6d 69 6e 67 20 61 20 67  n performing a g
9bb0: 72 61 70 68 69 63 61 6c 0a 2a 2a 20 20 20 20 20  raphical.**     
9bc0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9bd0: 64 69 66 66 2e 20 49 66 20 75 6e 64 65 66 69 6e  diff. If undefin
9be0: 65 64 2c 20 74 65 78 74 20 64 69 66 66 20 77 69  ed, text diff wi
9bf0: 6c 6c 20 62 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a  ll be used..**.*
9c00: 2a 20 20 20 20 6c 6f 63 61 6c 61 75 74 68 20 20  *    localauth  
9c10: 20 20 20 20 20 20 49 66 20 65 6e 61 62 6c 65 64        If enabled
9c20: 2c 20 72 65 71 75 69 72 65 20 74 68 61 74 20 48  , require that H
9c30: 54 54 50 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  TTP connections 
9c40: 66 72 6f 6d 0a 2a 2a 20 20 20 20 20 20 20 20 20  from.**         
9c50: 20 20 20 20 20 20 20 20 20 20 20 20 31 32 37 2e              127.
9c60: 30 2e 30 2e 31 20 62 65 20 61 75 74 68 65 6e 74  0.0.1 be authent
9c70: 69 63 61 74 65 64 20 62 79 20 70 61 73 73 77 6f  icated by passwo
9c80: 72 64 2e 20 20 49 66 0a 2a 2a 20 20 20 20 20 20  rd.  If.**      
9c90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66                 f
9ca0: 61 6c 73 65 2c 20 61 6c 6c 20 48 54 54 50 20 72  alse, all HTTP r
9cb0: 65 71 75 65 73 74 73 20 66 72 6f 6d 20 6c 6f 63  equests from loc
9cc0: 61 6c 68 6f 73 74 20 68 61 76 65 0a 2a 2a 20 20  alhost have.**  
9cd0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9ce0: 20 20 20 75 6e 72 65 73 74 72 69 63 74 65 64 20     unrestricted 
9cf0: 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 72 65  access to the re
9d00: 70 6f 73 69 74 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20  pository..**.** 
9d10: 20 20 20 63 6c 65 61 72 73 69 67 6e 20 20 20 20     clearsign    
9d20: 20 20 20 20 57 68 65 6e 20 65 6e 61 62 6c 65 64      When enabled
9d30: 20 28 74 68 65 20 64 65 66 61 75 6c 74 29 2c 20   (the default), 
9d40: 66 6f 73 73 69 6c 20 77 69 6c 6c 20 61 74 74 65  fossil will atte
9d50: 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20  mpt to.**       
9d60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 69                si
9d70: 67 6e 20 61 6c 6c 20 63 6f 6d 6d 69 74 73 20 77  gn all commits w
9d80: 69 74 68 20 67 70 67 2e 20 20 57 68 65 6e 20 64  ith gpg.  When d
9d90: 69 73 61 62 6c 65 64 2c 20 63 6f 6d 6d 69 74 73  isabled, commits
9da0: 20 77 69 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20   will.**        
9db0: 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65 20               be 
9dc0: 75 6e 73 69 67 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  unsigned..**.** 
9dd0: 20 20 20 70 67 70 2d 63 6f 6d 6d 61 6e 64 20 20     pgp-command  
9de0: 20 20 20 20 43 6f 6d 6d 61 6e 64 20 75 73 65 64      Command used
9df0: 20 74 6f 20 63 6c 65 61 72 2d 73 69 67 6e 20 6d   to clear-sign m
9e00: 61 6e 69 66 65 73 74 73 20 61 74 20 63 68 65 63  anifests at chec
9e10: 6b 2d 69 6e 2e 0a 2a 2a 20 20 20 20 20 20 20 20  k-in..**        
9e20: 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65               The
9e30: 20 64 65 66 61 75 6c 74 20 69 73 20 22 67 70 67   default is "gpg
9e40: 20 2d 2d 63 6c 65 61 72 73 69 67 6e 20 2d 6f 20   --clearsign -o 
9e50: 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 6d 74 69 6d  "..**.**    mtim
9e60: 65 2d 63 68 61 6e 67 65 73 20 20 20 20 55 73 65  e-changes    Use
9e70: 20 66 69 6c 65 20 6d 6f 64 69 66 69 63 61 74 69   file modificati
9e80: 6f 6e 20 74 69 6d 65 73 20 28 6d 74 69 6d 65 73  on times (mtimes
9e90: 29 20 74 6f 20 64 65 74 65 63 74 20 77 68 65 6e  ) to detect when
9ea0: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
9eb0: 20 20 20 20 20 20 20 20 66 69 6c 65 73 20 68 61          files ha
9ec0: 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64  ve been modified
9ed0: 2e 20 20 0a 2a 2a 0a 2a 2a 20 20 20 20 70 72 6f  .  .**.**    pro
9ee0: 78 79 20 20 20 20 20 20 20 20 20 20 20 20 55 52  xy            UR
9ef0: 4c 20 6f 66 20 74 68 65 20 48 54 54 50 20 70 72  L of the HTTP pr
9f00: 6f 78 79 2e 20 20 49 66 20 75 6e 64 65 66 69 6e  oxy.  If undefin
9f10: 65 64 20 6f 72 20 22 6f 66 66 22 20 74 68 65 6e  ed or "off" then
9f20: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
9f30: 20 20 20 20 20 20 20 20 74 68 65 20 22 68 74 74          the "htt
9f40: 70 5f 70 72 6f 78 79 22 20 65 6e 76 69 72 6f 6e  p_proxy" environ
9f50: 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 20 69 73  ment variable is
9f60: 20 63 6f 6e 73 75 6c 74 65 64 2e 0a 2a 2a 20 20   consulted..**  
9f70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9f80: 20 20 20 49 66 20 74 68 65 20 68 74 74 70 5f 70     If the http_p
9f90: 72 6f 78 79 20 65 6e 76 69 72 6f 6e 6d 65 6e 74  roxy environment
9fa0: 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 6e 64   variable is und
9fb0: 65 66 69 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20  efined.**       
9fc0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68                th
9fd0: 65 6e 20 61 20 64 69 72 65 63 74 20 48 54 54 50  en a direct HTTP
9fe0: 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 75   connection is u
9ff0: 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 77 65  sed..**.**    we
a000: 62 2d 62 72 6f 77 73 65 72 20 20 20 20 20 20 41  b-browser      A
a010: 20 73 68 65 6c 6c 20 63 6f 6d 6d 61 6e 64 20 75   shell command u
a020: 73 65 64 20 74 6f 20 6c 61 75 6e 63 68 20 79 6f  sed to launch yo
a030: 75 72 20 70 72 65 66 65 72 72 65 64 0a 2a 2a 20  ur preferred.** 
a040: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a050: 20 20 20 20 77 65 62 20 62 72 6f 77 73 65 72 20      web browser 
a060: 77 68 65 6e 20 67 69 76 65 6e 20 61 20 55 52 4c  when given a URL
a070: 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e   as an argument.
a080: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
a090: 20 20 20 20 20 20 20 20 44 65 66 61 75 6c 74 73          Defaults
a0a0: 20 74 6f 20 22 73 74 61 72 74 22 20 6f 6e 20 77   to "start" on w
a0b0: 69 6e 64 6f 77 73 2c 20 22 6f 70 65 6e 22 20 6f  indows, "open" o
a0c0: 6e 20 4d 61 63 2c 0a 2a 2a 20 20 20 20 20 20 20  n Mac,.**       
a0d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6e                an
a0e0: 64 20 22 66 69 72 65 66 6f 78 22 20 6f 6e 20 55  d "firefox" on U
a0f0: 6e 69 78 2e 0a 2a 2f 0a 76 6f 69 64 20 73 65 74  nix..*/.void set
a100: 74 69 6e 67 5f 63 6d 64 28 76 6f 69 64 29 7b 0a  ting_cmd(void){.
a110: 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
a120: 68 61 72 20 2a 61 7a 4e 61 6d 65 5b 5d 20 3d 20  har *azName[] = 
a130: 7b 0a 20 20 20 20 22 61 75 74 6f 73 79 6e 63 22  {.    "autosync"
a140: 2c 0a 20 20 20 20 22 64 69 66 66 2d 63 6f 6d 6d  ,.    "diff-comm
a150: 61 6e 64 22 2c 0a 20 20 20 20 22 65 64 69 74 6f  and",.    "edito
a160: 72 22 2c 0a 20 20 20 20 22 67 64 69 66 66 2d 63  r",.    "gdiff-c
a170: 6f 6d 6d 61 6e 64 22 2c 0a 20 20 20 20 22 68 74  ommand",.    "ht
a180: 74 70 2d 70 6f 72 74 22 2c 0a 20 20 20 20 22 6c  tp-port",.    "l
a190: 6f 63 61 6c 61 75 74 68 22 2c 0a 20 20 20 20 22  ocalauth",.    "
a1a0: 63 6c 65 61 72 73 69 67 6e 22 2c 0a 20 20 20 20  clearsign",.    
a1b0: 22 70 67 70 2d 63 6f 6d 6d 61 6e 64 22 2c 0a 20  "pgp-command",. 
a1c0: 20 20 20 22 6d 74 69 6d 65 2d 63 68 61 6e 67 65     "mtime-change
a1d0: 73 22 2c 0a 20 20 20 20 22 70 72 6f 78 79 22 2c  s",.    "proxy",
a1e0: 0a 20 20 20 20 22 77 65 62 2d 62 72 6f 77 73 65  .    "web-browse
a1f0: 72 22 2c 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69  r",.  };.  int i
a200: 3b 0a 20 20 69 6e 74 20 67 6c 6f 62 61 6c 46 6c  ;.  int globalFl
a210: 61 67 20 3d 20 66 69 6e 64 5f 6f 70 74 69 6f 6e  ag = find_option
a220: 28 22 67 6c 6f 62 61 6c 22 2c 22 67 22 2c 30 29  ("global","g",0)
a230: 21 3d 30 3b 0a 20 20 69 6e 74 20 75 6e 73 65 74  !=0;.  int unset
a240: 46 6c 61 67 20 3d 20 67 2e 61 72 67 76 5b 31 5d  Flag = g.argv[1]
a250: 5b 30 5d 3d 3d 27 75 27 3b 0a 20 20 64 62 5f 6f  [0]=='u';.  db_o
a260: 70 65 6e 5f 63 6f 6e 66 69 67 28 31 29 3b 0a 20  pen_config(1);. 
a270: 20 64 62 5f 66 69 6e 64 5f 61 6e 64 5f 6f 70 65   db_find_and_ope
a280: 6e 5f 72 65 70 6f 73 69 74 6f 72 79 28 30 29 3b  n_repository(0);
a290: 0a 20 20 69 66 28 20 21 67 2e 72 65 70 6f 73 69  .  if( !g.reposi
a2a0: 74 6f 72 79 4f 70 65 6e 20 29 7b 0a 20 20 20 20  toryOpen ){.    
a2b0: 67 6c 6f 62 61 6c 46 6c 61 67 20 3d 20 31 3b 0a  globalFlag = 1;.
a2c0: 20 20 7d 0a 20 20 69 66 28 20 75 6e 73 65 74 46    }.  if( unsetF
a2d0: 6c 61 67 20 26 26 20 67 2e 61 72 67 63 21 3d 33  lag && g.argc!=3
a2e0: 20 29 7b 0a 20 20 20 20 75 73 61 67 65 28 22 50   ){.    usage("P
a2f0: 52 4f 50 45 52 54 59 20 3f 2d 67 6c 6f 62 61 6c  ROPERTY ?-global
a300: 3f 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67  ?");.  }.  if( g
a310: 2e 61 72 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20  .argc==2 ){.    
a320: 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f  for(i=0; i<sizeo
a330: 66 28 61 7a 4e 61 6d 65 29 2f 73 69 7a 65 6f 66  f(azName)/sizeof
a340: 28 61 7a 4e 61 6d 65 5b 30 5d 29 3b 20 69 2b 2b  (azName[0]); i++
a350: 29 7b 0a 20 20 20 20 20 20 70 72 69 6e 74 5f 73  ){.      print_s
a360: 65 74 74 69 6e 67 28 61 7a 4e 61 6d 65 5b 69 5d  etting(azName[i]
a370: 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
a380: 20 69 66 28 20 67 2e 61 72 67 63 3d 3d 33 20 7c   if( g.argc==3 |
a390: 7c 20 67 2e 61 72 67 63 3d 3d 34 20 29 7b 0a 20  | g.argc==4 ){. 
a3a0: 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
a3b0: 4e 61 6d 65 20 3d 20 67 2e 61 72 67 76 5b 32 5d  Name = g.argv[2]
a3c0: 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 74  ;.    int n = st
a3d0: 72 6c 65 6e 28 7a 4e 61 6d 65 29 3b 0a 20 20 20  rlen(zName);.   
a3e0: 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65   for(i=0; i<size
a3f0: 6f 66 28 61 7a 4e 61 6d 65 29 2f 73 69 7a 65 6f  of(azName)/sizeo
a400: 66 28 61 7a 4e 61 6d 65 5b 30 5d 29 3b 20 69 2b  f(azName[0]); i+
a410: 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 74  +){.      if( st
a420: 72 6e 63 6d 70 28 61 7a 4e 61 6d 65 5b 69 5d 2c  rncmp(azName[i],
a430: 20 7a 4e 61 6d 65 2c 20 6e 29 3d 3d 30 20 29 20   zName, n)==0 ) 
a440: 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
a450: 20 69 66 28 20 69 3e 3d 73 69 7a 65 6f 66 28 61   if( i>=sizeof(a
a460: 7a 4e 61 6d 65 29 2f 73 69 7a 65 6f 66 28 61 7a  zName)/sizeof(az
a470: 4e 61 6d 65 5b 30 5d 29 20 29 7b 0a 20 20 20 20  Name[0]) ){.    
a480: 20 20 66 6f 73 73 69 6c 5f 66 61 74 61 6c 28 22    fossil_fatal("
a490: 6e 6f 20 73 75 63 68 20 73 65 74 74 69 6e 67 3a  no such setting:
a4a0: 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20   %s", zName);.  
a4b0: 20 20 7d 0a 20 20 20 20 69 66 28 20 75 6e 73 65    }.    if( unse
a4c0: 74 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 64  tFlag ){.      d
a4d0: 62 5f 75 6e 73 65 74 28 61 7a 4e 61 6d 65 5b 69  b_unset(azName[i
a4e0: 5d 2c 20 67 6c 6f 62 61 6c 46 6c 61 67 29 3b 0a  ], globalFlag);.
a4f0: 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 67 2e      }else if( g.
a500: 61 72 67 63 3d 3d 34 20 29 7b 0a 20 20 20 20 20  argc==4 ){.     
a510: 20 64 62 5f 73 65 74 28 61 7a 4e 61 6d 65 5b 69   db_set(azName[i
a520: 5d 2c 20 67 2e 61 72 67 76 5b 33 5d 2c 20 67 6c  ], g.argv[3], gl
a530: 6f 62 61 6c 46 6c 61 67 29 3b 0a 20 20 20 20 7d  obalFlag);.    }
a540: 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 72 69 6e  else{.      prin
a550: 74 5f 73 65 74 74 69 6e 67 28 61 7a 4e 61 6d 65  t_setting(azName
a560: 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  [i]);.    }.  }e
a570: 6c 73 65 7b 0a 20 20 20 20 75 73 61 67 65 28 22  lse{.    usage("
a580: 3f 50 52 4f 50 45 52 54 59 3f 20 3f 56 41 4c 55  ?PROPERTY? ?VALU
a590: 45 3f 22 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  E?");.  }.}../*.
a5a0: 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  ** SQL function 
a5b0: 74 6f 20 72 65 6e 64 65 72 20 61 20 55 55 49 44  to render a UUID
a5c0: 20 61 73 20 61 20 68 79 70 65 72 6c 69 6e 6b 20   as a hyperlink 
a5d0: 74 6f 20 61 20 70 61 67 65 20 64 65 73 63 72 69  to a page descri
a5e0: 62 69 6e 67 0a 2a 2a 20 74 68 61 74 20 55 55 49  bing.** that UUI
a5f0: 44 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  D..*/.static voi
a600: 64 20 68 79 70 65 72 6c 69 6e 6b 55 75 69 64 46  d hyperlinkUuidF
a610: 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
a620: 6f 6e 74 65 78 74 20 2a 70 43 78 74 2c 20 20 20  ontext *pCxt,   
a630: 20 20 2f 2a 20 66 75 6e 63 74 69 6f 6e 20 63 6f    /* function co
a640: 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 61  ntext */.  int a
a650: 72 67 63 2c 20 20 20 20 20 20 20 20 20 20 20 20  rgc,            
a660: 20 20 20 20 20 20 2f 2a 20 6e 75 6d 62 65 72 20        /* number 
a670: 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  of arguments to 
a680: 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  the function */.
a690: 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
a6a0: 2a 2a 61 72 67 76 20 20 20 20 20 20 20 2f 2a 20  **argv       /* 
a6b0: 76 61 6c 75 65 73 20 6f 66 20 61 6c 6c 20 66 75  values of all fu
a6c0: 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73  nction arguments
a6d0: 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63   */.){.  const c
a6e0: 68 61 72 20 2a 7a 55 75 69 64 3b 20 20 20 20 20  har *zUuid;     
a6f0: 20 20 20 20 2f 2a 20 54 68 65 20 55 55 49 44 20      /* The UUID 
a700: 74 6f 20 72 65 6e 64 65 72 20 2a 2f 0a 20 20 63  to render */.  c
a710: 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20  har *z;         
a720: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6e            /* Ren
a730: 64 65 72 65 64 20 48 54 4d 4c 20 74 65 78 74 20  dered HTML text 
a740: 2a 2f 0a 0a 20 20 7a 55 75 69 64 20 3d 20 28 63  */..  zUuid = (c
a750: 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74  onst char*)sqlit
a760: 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
a770: 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 67 2e  gv[0]);.  if( g.
a780: 6f 6b 48 69 73 74 6f 72 79 20 26 26 20 7a 55 75  okHistory && zUu
a790: 69 64 20 26 26 20 73 74 72 6c 65 6e 28 7a 55 75  id && strlen(zUu
a7a0: 69 64 29 3e 3d 31 30 20 29 7b 0a 20 20 20 20 7a  id)>=10 ){.    z
a7b0: 20 3d 20 6d 70 72 69 6e 74 66 28 22 3c 74 74 3e   = mprintf("<tt>
a7c0: 3c 61 20 68 72 65 66 3d 27 25 73 2f 69 6e 66 6f  <a href='%s/info
a7d0: 2f 25 74 27 3e 3c 73 70 61 6e 20 73 74 79 6c 65  /%t'><span style
a7e0: 3d 27 66 6f 6e 74 2d 73 69 7a 65 3a 31 2e 35 65  ='font-size:1.5e
a7f0: 6d 27 3e 22 0a 20 20 20 20 20 20 20 20 20 20 20  m'>".           
a800: 20 20 20 20 20 22 25 23 68 3c 2f 73 70 61 6e 3e       "%#h</span>
a810: 25 68 3c 2f 61 3e 3c 2f 74 74 3e 22 2c 0a 20 20  %h</a></tt>",.  
a820: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 67 2e                g.
a830: 7a 42 61 73 65 55 52 4c 2c 20 7a 55 75 69 64 2c  zBaseURL, zUuid,
a840: 20 31 30 2c 20 7a 55 75 69 64 2c 20 26 7a 55 75   10, zUuid, &zUu
a850: 69 64 5b 31 30 5d 29 3b 0a 20 20 20 20 73 71 6c  id[10]);.    sql
a860: 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
a870: 28 70 43 78 74 2c 20 7a 2c 20 2d 31 2c 20 66 72  (pCxt, z, -1, fr
a880: 65 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ee);.  }else{.  
a890: 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
a8a0: 5f 74 65 78 74 28 70 43 78 74 2c 20 7a 55 75 69  _text(pCxt, zUui
a8b0: 64 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52  d, -1, SQLITE_TR
a8c0: 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a  ANSIENT);.  }.}.
a8d0: 0a 2f 2a 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74  ./*.** SQL funct
a8e0: 69 6f 6e 20 74 6f 20 72 65 6e 64 65 72 20 61 20  ion to render a 
a8f0: 54 41 47 49 44 20 61 73 20 61 20 68 79 70 65 72  TAGID as a hyper
a900: 6c 69 6e 6b 20 74 6f 20 61 20 70 61 67 65 20 64  link to a page d
a910: 65 73 63 72 69 62 69 6e 67 0a 2a 2a 20 74 68 61  escribing.** tha
a920: 74 20 74 61 67 2e 0a 2a 2f 0a 73 74 61 74 69 63  t tag..*/.static
a930: 20 76 6f 69 64 20 68 79 70 65 72 6c 69 6e 6b 54   void hyperlinkT
a940: 61 67 69 64 46 75 6e 63 28 0a 20 20 73 71 6c 69  agidFunc(.  sqli
a950: 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 78  te3_context *pCx
a960: 74 2c 20 20 20 20 20 2f 2a 20 66 75 6e 63 74 69  t,     /* functi
a970: 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  on context */.  
a980: 69 6e 74 20 61 72 67 63 2c 20 20 20 20 20 20 20  int argc,       
a990: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 75             /* nu
a9a0: 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
a9b0: 73 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f  s to the functio
a9c0: 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76  n */.  sqlite3_v
a9d0: 61 6c 75 65 20 2a 2a 61 72 67 76 20 20 20 20 20  alue **argv     
a9e0: 20 20 2f 2a 20 76 61 6c 75 65 73 20 6f 66 20 61    /* values of a
a9f0: 6c 6c 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75  ll function argu
aa00: 6d 65 6e 74 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e  ments */.){.  in
aa10: 74 20 74 61 67 69 64 3b 20 20 20 20 20 20 20 20  t tagid;        
aa20: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
aa30: 74 61 67 69 64 20 74 6f 20 72 65 6e 64 65 72 20  tagid to render 
aa40: 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20 20 20  */.  char *z;   
aa50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aa60: 2f 2a 20 72 65 6e 64 65 72 65 64 20 68 74 6d 6c  /* rendered html
aa70: 20 74 65 78 74 20 2a 2f 0a 0a 20 20 74 61 67 69   text */..  tagi
aa80: 64 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  d = sqlite3_valu
aa90: 65 5f 69 6e 74 28 61 72 67 76 5b 30 5d 29 3b 0a  e_int(argv[0]);.
aaa0: 20 20 69 66 28 20 67 2e 6f 6b 48 69 73 74 6f 72    if( g.okHistor
aab0: 79 20 29 7b 0a 20 20 20 20 7a 20 3d 20 6d 70 72  y ){.    z = mpr
aac0: 69 6e 74 66 28 22 3c 61 20 68 72 65 66 3d 27 25  intf("<a href='%
aad0: 73 2f 74 61 67 76 69 65 77 3f 74 61 67 69 64 3d  s/tagview?tagid=
aae0: 25 64 27 3e 25 64 3c 2f 61 3e 22 2c 20 0a 20 20  %d'>%d</a>", .  
aaf0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ab00: 67 2e 7a 42 61 73 65 55 52 4c 2c 20 74 61 67 69  g.zBaseURL, tagi
ab10: 64 2c 20 74 61 67 69 64 29 3b 0a 20 20 7d 65 6c  d, tagid);.  }el
ab20: 73 65 7b 0a 20 20 20 20 7a 20 3d 20 6d 70 72 69  se{.    z = mpri
ab30: 6e 74 66 28 22 25 64 22 2c 20 74 61 67 69 64 29  ntf("%d", tagid)
ab40: 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
ab50: 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43 78 74  result_text(pCxt
ab60: 2c 20 7a 2c 20 2d 31 2c 20 66 72 65 65 29 3b 0a  , z, -1, free);.
ab70: 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 20 66 75 6e  }../*.** SQL fun
ab80: 63 74 69 6f 6e 20 74 6f 20 72 65 6e 64 65 72 20  ction to render 
ab90: 61 20 54 41 47 4e 41 4d 45 20 61 73 20 61 20 68  a TAGNAME as a h
aba0: 79 70 65 72 6c 69 6e 6b 20 74 6f 20 61 20 70 61  yperlink to a pa
abb0: 67 65 20 64 65 73 63 72 69 62 69 6e 67 0a 2a 2a  ge describing.**
abc0: 20 74 68 61 74 20 74 61 67 2e 0a 2a 2f 0a 73 74   that tag..*/.st
abd0: 61 74 69 63 20 76 6f 69 64 20 68 79 70 65 72 6c  atic void hyperl
abe0: 69 6e 6b 54 61 67 6e 61 6d 65 46 75 6e 63 28 0a  inkTagnameFunc(.
abf0: 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
ac00: 74 20 2a 70 43 78 74 2c 20 20 20 20 20 2f 2a 20  t *pCxt,     /* 
ac10: 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74  function context
ac20: 20 2a 2f 0a 20 20 69 6e 74 20 61 72 67 63 2c 20   */.  int argc, 
ac30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac40: 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 20 61 72   /* number of ar
ac50: 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66  guments to the f
ac60: 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c  unction */.  sql
ac70: 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
ac80: 76 20 20 20 20 20 20 20 2f 2a 20 76 61 6c 75 65  v       /* value
ac90: 73 20 6f 66 20 61 6c 6c 20 66 75 6e 63 74 69 6f  s of all functio
aca0: 6e 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 29  n arguments */.)
acb0: 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  {.  const char *
acc0: 7a 54 61 67 3b 20 20 20 20 20 20 20 20 20 20 2f  zTag;          /
acd0: 2a 20 54 68 65 20 74 61 67 20 74 6f 20 72 65 6e  * The tag to ren
ace0: 64 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  der */.  char *z
acf0: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
ad00: 20 20 20 20 2f 2a 20 72 65 6e 64 65 72 65 64 20      /* rendered 
ad10: 68 74 6d 6c 20 74 65 78 74 20 2a 2f 0a 0a 20 20  html text */..  
ad20: 7a 54 61 67 20 3d 20 28 63 6f 6e 73 74 20 63 68  zTag = (const ch
ad30: 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar*)sqlite3_valu
ad40: 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
ad50: 0a 20 20 69 66 28 20 67 2e 6f 6b 48 69 73 74 6f  .  if( g.okHisto
ad60: 72 79 20 29 7b 0a 20 20 20 20 7a 20 3d 20 6d 70  ry ){.    z = mp
ad70: 72 69 6e 74 66 28 22 3c 61 20 68 72 65 66 3d 27  rintf("<a href='
ad80: 25 73 2f 74 61 67 76 69 65 77 3f 6e 61 6d 65 3d  %s/tagview?name=
ad90: 25 54 26 72 61 77 3d 79 27 3e 25 68 3c 2f 61 3e  %T&raw=y'>%h</a>
ada0: 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  ", .            
adb0: 20 20 20 20 20 20 67 2e 7a 42 61 73 65 55 52 4c        g.zBaseURL
adc0: 2c 20 7a 54 61 67 2c 20 7a 54 61 67 29 3b 0a 20  , zTag, zTag);. 
add0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20   }else{.    z = 
ade0: 6d 70 72 69 6e 74 66 28 22 25 68 22 2c 20 7a 54  mprintf("%h", zT
adf0: 61 67 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ag);.  }.  sqlit
ae00: 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70  e3_result_text(p
ae10: 43 78 74 2c 20 7a 2c 20 2d 31 2c 20 66 72 65 65  Cxt, z, -1, free
ae20: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 20  );.}../*.** SQL 
ae30: 66 75 6e 63 74 69 6f 6e 20 74 6f 20 65 73 63 61  function to esca
ae40: 70 65 20 61 6c 6c 20 63 68 61 72 61 63 74 65 72  pe all character
ae50: 73 20 69 6e 20 61 20 73 74 72 69 6e 67 20 74 68  s in a string th
ae60: 61 74 20 68 61 76 65 20 73 70 65 63 69 61 6c 0a  at have special.
ae70: 2a 2a 20 6d 65 61 6e 69 6e 67 20 74 6f 20 48 54  ** meaning to HT
ae80: 4d 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ML..*/.static vo
ae90: 69 64 20 68 74 6d 6c 69 7a 65 46 75 6e 63 28 0a  id htmlizeFunc(.
aea0: 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
aeb0: 74 20 2a 70 43 78 74 2c 20 20 20 20 20 2f 2a 20  t *pCxt,     /* 
aec0: 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74  function context
aed0: 20 2a 2f 0a 20 20 69 6e 74 20 61 72 67 63 2c 20   */.  int argc, 
aee0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aef0: 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 20 61 72   /* number of ar
af00: 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66  guments to the f
af10: 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c  unction */.  sql
af20: 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
af30: 76 20 20 20 20 20 20 20 2f 2a 20 76 61 6c 75 65  v       /* value
af40: 73 20 6f 66 20 61 6c 6c 20 66 75 6e 63 74 69 6f  s of all functio
af50: 6e 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 29  n arguments */.)
af60: 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  {.  const char *
af70: 7a 54 65 78 74 3b 20 20 20 20 20 20 20 20 20 2f  zText;         /
af80: 2a 20 54 65 78 74 20 74 6f 20 62 65 20 68 74 6d  * Text to be htm
af90: 6c 69 7a 65 64 20 2a 2f 0a 20 20 63 68 61 72 20  lized */.  char 
afa0: 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *z;             
afb0: 20 20 20 20 20 20 2f 2a 20 72 65 6e 64 65 72 65        /* rendere
afc0: 64 20 68 74 6d 6c 20 74 65 78 74 20 2a 2f 0a 0a  d html text */..
afd0: 20 20 7a 54 65 78 74 20 3d 20 28 63 6f 6e 73 74    zText = (const
afe0: 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76   char*)sqlite3_v
aff0: 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
b000: 5d 29 3b 0a 20 20 7a 20 3d 20 68 74 6d 6c 69 7a  ]);.  z = htmliz
b010: 65 28 7a 54 65 78 74 2c 20 2d 31 29 3b 0a 20 20  e(zText, -1);.  
b020: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
b030: 65 78 74 28 70 43 78 74 2c 20 7a 2c 20 2d 31 2c  ext(pCxt, z, -1,
b040: 20 66 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   free);.}../*.**
b050: 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
b060: 20 61 20 68 65 6c 70 65 72 20 74 6f 20 72 75 6e   a helper to run
b070: 20 61 6e 20 53 51 4c 20 71 75 65 72 79 20 61 6e   an SQL query an
b080: 64 20 74 61 62 6c 65 2d 69 7a 65 20 74 68 65 0a  d table-ize the.
b090: 2a 2a 20 72 65 73 75 6c 74 73 2e 0a 2a 2a 0a 2a  ** results..**.*
b0a0: 2a 20 54 68 65 20 7a 53 71 6c 20 70 61 72 61 6d  * The zSql param
b0b0: 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 20 61  eter should be a
b0c0: 20 73 69 6e 67 6c 65 2c 20 63 6f 6d 70 6c 65 74   single, complet
b0d0: 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  e SQL statement.
b0e0: 0a 2a 2a 20 54 61 62 6c 65 69 7a 65 64 20 6f 75  .** Tableized ou
b0f0: 74 70 75 74 20 6f 66 20 74 68 65 20 53 51 4c 20  tput of the SQL 
b100: 73 74 61 74 65 6d 65 6e 74 20 69 73 20 72 65 6e  statement is ren
b110: 64 65 72 65 64 20 62 61 63 6b 20 74 6f 20 74 68  dered back to th
b120: 65 20 63 6c 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  e client..**.** 
b130: 54 68 65 20 69 73 53 61 66 65 20 66 6c 61 67 20  The isSafe flag 
b140: 69 73 20 74 72 75 65 20 69 66 20 61 6c 6c 20 71  is true if all q
b150: 75 65 72 79 20 72 65 73 75 6c 74 73 20 68 61 76  uery results hav
b160: 65 20 62 65 65 6e 20 70 72 6f 63 65 73 73 65 64  e been processed
b170: 20 0a 2a 2a 20 62 79 20 72 6f 75 74 69 6e 65 73   .** by routines
b180: 20 73 75 63 68 20 61 73 0a 2a 2a 0a 2a 2a 20 20   such as.**.**  
b190: 20 20 20 20 20 20 6c 69 6e 6b 75 75 69 64 28 29        linkuuid()
b1a0: 0a 2a 2a 20 20 20 20 20 20 20 20 6c 69 6e 6b 74  .**        linkt
b1b0: 61 67 69 64 28 29 0a 2a 2a 20 20 20 20 20 20 20  agid().**       
b1c0: 20 6c 69 6e 6b 74 61 67 6e 61 6d 65 28 29 0a 2a   linktagname().*
b1d0: 2a 20 20 20 20 20 20 20 20 68 74 6d 6c 69 7a 65  *        htmlize
b1e0: 28 29 0a 2a 2a 0a 2a 2a 20 61 6e 64 20 61 72 65  ().**.** and are
b1f0: 20 74 68 65 72 65 66 6f 72 65 20 73 61 66 65 20   therefore safe 
b200: 66 6f 72 20 64 69 72 65 63 74 20 72 65 6e 64 65  for direct rende
b210: 72 69 6e 67 2e 20 20 49 66 20 69 73 53 61 66 65  ring.  If isSafe
b220: 20 69 73 20 66 61 6c 73 65 2c 0a 2a 2a 20 74 68   is false,.** th
b230: 65 6e 20 61 6c 6c 20 63 68 61 72 61 63 74 65 72  en all character
b240: 73 20 69 6e 20 74 68 65 20 71 75 65 72 79 20 72  s in the query r
b250: 65 73 75 6c 74 20 74 68 61 74 20 68 61 76 65 20  esult that have 
b260: 73 70 65 63 69 61 6c 20 6d 65 61 6e 69 6e 67 0a  special meaning.
b270: 2a 2a 20 74 6f 20 48 54 4d 4c 20 61 72 65 20 65  ** to HTML are e
b280: 73 63 61 70 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65  scaped..**.** Re
b290: 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 20  turns SQLITE_OK 
b2a0: 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 61  on success and a
b2b0: 6e 79 20 6f 74 68 65 72 20 76 61 6c 75 65 20 6f  ny other value o
b2c0: 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 69 6e 74 20  n error..*/.int 
b2d0: 64 62 5f 67 65 6e 65 72 69 63 5f 71 75 65 72 79  db_generic_query
b2e0: 5f 76 69 65 77 28 63 6f 6e 73 74 20 63 68 61 72  _view(const char
b2f0: 20 2a 7a 53 71 6c 2c 20 69 6e 74 20 69 73 53 61   *zSql, int isSa
b300: 66 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73  fe){.  sqlite3_s
b310: 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 69 6e  tmt *pStmt;.  in
b320: 74 20 72 63 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c  t rc;.  int nCol
b330: 2c 20 69 3b 0a 20 20 69 6e 74 20 6e 52 6f 77 3b  , i;.  int nRow;
b340: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
b350: 52 6f 77 3b 0a 20 20 73 74 61 74 69 63 20 69 6e  Row;.  static in
b360: 74 20 6f 6e 63 65 20 3d 20 31 3b 0a 0a 20 20 2f  t once = 1;..  /
b370: 2a 20 49 6e 73 74 61 6c 6c 20 74 68 65 20 73 70  * Install the sp
b380: 65 63 69 61 6c 20 66 75 6e 63 74 69 6f 6e 73 20  ecial functions 
b390: 6f 6e 20 74 68 65 20 66 69 72 73 74 20 63 61 6c  on the first cal
b3a0: 6c 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  l to this routin
b3b0: 65 20 2a 2f 0a 20 20 69 66 28 20 6f 6e 63 65 20  e */.  if( once 
b3c0: 29 7b 0a 20 20 20 20 6f 6e 63 65 20 3d 20 30 3b  ){.    once = 0;
b3d0: 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
b3e0: 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 67 2e 64  ate_function(g.d
b3f0: 62 2c 20 22 6c 69 6e 6b 75 75 69 64 22 2c 20 31  b, "linkuuid", 1
b400: 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30  , SQLITE_UTF8, 0
b410: 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
b420: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 68                 h
b430: 79 70 65 72 6c 69 6e 6b 55 75 69 64 46 75 6e 63  yperlinkUuidFunc
b440: 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
b450: 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
b460: 74 69 6f 6e 28 67 2e 64 62 2c 20 22 6c 69 6e 6b  tion(g.db, "link
b470: 74 61 67 69 64 22 2c 20 31 2c 20 53 51 4c 49 54  tagid", 1, SQLIT
b480: 45 5f 55 54 46 38 2c 20 30 2c 20 0a 20 20 20 20  E_UTF8, 0, .    
b490: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b4a0: 20 20 20 20 20 20 20 20 68 79 70 65 72 6c 69 6e          hyperlin
b4b0: 6b 54 61 67 69 64 46 75 6e 63 2c 20 30 2c 20 30  kTagidFunc, 0, 0
b4c0: 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
b4d0: 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 67  reate_function(g
b4e0: 2e 64 62 2c 20 22 6c 69 6e 6b 74 61 67 6e 61 6d  .db, "linktagnam
b4f0: 65 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 55 54  e", 1, SQLITE_UT
b500: 46 38 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20  F8, 0, .        
b510: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b520: 20 20 20 20 68 79 70 65 72 6c 69 6e 6b 54 61 67      hyperlinkTag
b530: 6e 61 6d 65 46 75 6e 63 2c 20 30 2c 20 30 29 3b  nameFunc, 0, 0);
b540: 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
b550: 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 67 2e 64  ate_function(g.d
b560: 62 2c 20 22 68 74 6d 6c 69 7a 65 22 2c 20 31 2c  b, "htmlize", 1,
b570: 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c   SQLITE_UTF8, 0,
b580: 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
b590: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 68 74                ht
b5a0: 6d 6c 69 7a 65 46 75 6e 63 2c 20 30 2c 20 30 29  mlizeFunc, 0, 0)
b5b0: 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  ;.  }..  /*.  **
b5c0: 20 55 73 65 20 73 71 6c 69 74 65 33 5f 73 74 6d   Use sqlite3_stm
b5d0: 74 20 64 69 72 65 63 74 6c 79 20 72 61 74 68 65  t directly rathe
b5e0: 72 20 74 68 61 6e 20 67 6f 69 6e 67 20 74 68 72  r than going thr
b5f0: 6f 75 67 68 20 64 62 5f 70 72 65 70 61 72 65 28  ough db_prepare(
b600: 29 2c 0a 20 20 2a 2a 20 73 6f 20 74 68 61 74 20  ),.  ** so that 
b610: 77 65 20 63 61 6e 20 74 72 65 61 74 20 65 72 72  we can treat err
b620: 6f 72 73 20 61 20 6e 6f 6e 2d 66 61 74 61 6c 2e  ors a non-fatal.
b630: 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  .  */.  rc = sql
b640: 69 74 65 33 5f 70 72 65 70 61 72 65 28 67 2e 64  ite3_prepare(g.d
b650: 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53  b, zSql, -1, &pS
b660: 74 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 53  tmt, 0);.  if( S
b670: 51 4c 49 54 45 5f 4f 4b 20 21 3d 20 72 63 20 29  QLITE_OK != rc )
b680: 7b 0a 20 20 20 20 40 20 3c 73 70 61 6e 20 73 74  {.    @ <span st
b690: 79 6c 65 3d 27 63 6f 6c 6f 72 3a 72 65 64 27 3e  yle='color:red'>
b6a0: 64 62 5f 67 65 6e 65 72 69 63 5f 71 75 65 72 79  db_generic_query
b6b0: 5f 76 69 65 77 28 29 20 53 51 4c 20 65 72 72 6f  _view() SQL erro
b6c0: 72 3a 0a 20 20 20 20 40 20 25 68 28 73 71 6c 69  r:.    @ %h(sqli
b6d0: 74 65 33 5f 65 72 72 6d 73 67 28 67 2e 64 62 29  te3_errmsg(g.db)
b6e0: 29 3c 2f 73 70 61 6e 3e 0a 20 20 20 20 72 65 74  )</span>.    ret
b6f0: 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 6e 43  urn rc;.  }.  nC
b700: 6f 6c 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  ol = sqlite3_col
b710: 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29  umn_count(pStmt)
b720: 3b 0a 20 20 40 20 3c 74 61 62 6c 65 20 63 6c 61  ;.  @ <table cla
b730: 73 73 3d 27 66 6f 73 73 69 6c 5f 64 62 5f 67 65  ss='fossil_db_ge
b740: 6e 65 72 69 63 5f 71 75 65 72 79 5f 76 69 65 77  neric_query_view
b750: 27 3e 3c 74 62 6f 64 79 3e 0a 20 20 40 20 3c 74  '><tbody>.  @ <t
b760: 72 20 63 6c 61 73 73 3d 27 68 65 61 64 65 72 27  r class='header'
b770: 3e 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  >.  for(i=0; i<n
b780: 43 6f 6c 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 40  Col; ++i){.    @
b790: 20 3c 74 64 3e 25 68 28 73 71 6c 69 74 65 33 5f   <td>%h(sqlite3_
b7a0: 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 70 53 74 6d  column_name(pStm
b7b0: 74 2c 69 29 29 3c 2f 74 64 3e 0a 20 20 7d 0a 20  t,i))</td>.  }. 
b7c0: 20 40 20 3c 2f 74 72 3e 0a 0a 20 20 6e 52 6f 77   @ </tr>..  nRow
b7d0: 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 53   = 0;.  while( S
b7e0: 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74  QLITE_ROW==sqlit
b7f0: 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29  e3_step(pStmt) )
b800: 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
b810: 20 2a 61 7a 43 6c 61 73 73 5b 5d 20 3d 20 7b 20   *azClass[] = { 
b820: 22 65 76 65 6e 22 2c 20 22 6f 64 64 22 20 7d 3b  "even", "odd" };
b830: 0a 20 20 20 20 40 20 3c 74 72 20 63 6c 61 73 73  .    @ <tr class
b840: 3d 27 25 73 28 61 7a 43 6c 61 73 73 5b 28 6e 52  ='%s(azClass[(nR
b850: 6f 77 2b 2b 29 26 31 5d 29 27 3e 0a 20 20 20 20  ow++)&1])'>.    
b860: 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f    for(i=0; i<nCo
b870: 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  l; i++){.       
b880: 20 7a 52 6f 77 20 3d 20 28 63 68 61 72 20 63 6f   zRow = (char co
b890: 6e 73 74 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c  nst*)sqlite3_col
b8a0: 75 6d 6e 5f 74 65 78 74 28 70 53 74 6d 74 2c 69  umn_text(pStmt,i
b8b0: 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  );.        if( i
b8c0: 73 53 61 66 65 20 29 7b 0a 20 20 20 20 20 20 20  sSafe ){.       
b8d0: 20 20 20 40 20 3c 74 64 3e 25 73 28 7a 52 6f 77     @ <td>%s(zRow
b8e0: 29 3c 2f 74 64 3e 0a 20 20 20 20 20 20 20 20 7d  )</td>.        }
b8f0: 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
b900: 40 20 3c 74 64 3e 25 68 28 7a 52 6f 77 29 3c 2f  @ <td>%h(zRow)</
b910: 74 64 3e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  td>.        }.  
b920: 20 20 20 20 7d 0a 20 20 20 20 40 20 3c 2f 74 72      }.    @ </tr
b930: 3e 0a 20 20 7d 0a 20 20 40 20 3c 2f 74 62 6f 64  >.  }.  @ </tbod
b940: 79 3e 3c 2f 74 61 62 6c 65 3e 0a 20 20 73 71 6c  y></table>.  sql
b950: 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53  ite3_finalize(pS
b960: 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  tmt);.  return S
b970: 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a              QLITE_OK;.}.