Hex Artifact Content
Not logged in

Artifact f365dd4ce99006281aa6c1fb2c85db021f12ed55:

File src/db.c part of check-in [623a9129c8] - Update to the latest SQLite. On the "Ticket" page, if "nobody" is not allowed to create new tickets but "anonymous" is, then offer a hyperlink to log in as anonymous. Change the default settings to be more secure. by drh on 2009-07-06 23:53:18.

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 7a 44 62  b);.}../*.** zDb
4040: 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65  Name is the name
4050: 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 66   of a database f
4060: 69 6c 65 2e 20 20 49 66 20 6e 6f 20 6f 74 68 65  ile.  If no othe
4070: 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69  r database.** fi
4080: 6c 65 20 69 73 20 6f 70 65 6e 2c 20 74 68 65 6e  le is open, then
4090: 20 6f 70 65 6e 20 74 68 69 73 20 6f 6e 65 2e 20   open this one. 
40a0: 20 49 66 20 61 6e 6f 74 68 65 72 20 64 61 74 61   If another data
40b0: 62 61 73 65 20 66 69 6c 65 20 69 73 0a 2a 2a 20  base file is.** 
40c0: 61 6c 72 65 61 64 79 20 6f 70 65 6e 2c 20 74 68  already open, th
40d0: 65 6e 20 61 74 74 61 63 68 20 7a 44 62 4e 61 6d  en attach zDbNam
40e0: 65 20 75 73 69 6e 67 20 74 68 65 20 6e 61 6d 65  e using the name
40f0: 20 7a 4c 61 62 65 6c 2e 0a 2a 2f 0a 76 6f 69 64   zLabel..*/.void
4100: 20 64 62 5f 6f 70 65 6e 5f 6f 72 5f 61 74 74 61   db_open_or_atta
4110: 63 68 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  ch(const char *z
4120: 44 62 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68  DbName, const ch
4130: 61 72 20 2a 7a 4c 61 62 65 6c 29 7b 0a 23 69 66  ar *zLabel){.#if
4140: 64 65 66 20 5f 5f 4d 49 4e 47 57 33 32 5f 5f 0a  def __MINGW32__.
4150: 20 20 7a 44 62 4e 61 6d 65 20 3d 20 6d 62 63 73    zDbName = mbcs
4160: 54 6f 55 74 66 38 28 7a 44 62 4e 61 6d 65 29 3b  ToUtf8(zDbName);
4170: 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 21 67  .#endif.  if( !g
4180: 2e 64 62 20 29 7b 0a 20 20 20 20 69 6e 74 20 72  .db ){.    int r
4190: 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  c = sqlite3_open
41a0: 28 7a 44 62 4e 61 6d 65 2c 20 26 67 2e 64 62 29  (zDbName, &g.db)
41b0: 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
41c0: 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
41d0: 20 64 62 5f 65 72 72 28 73 71 6c 69 74 65 33 5f   db_err(sqlite3_
41e0: 65 72 72 6d 73 67 28 67 2e 64 62 29 29 3b 0a 20  errmsg(g.db));. 
41f0: 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
4200: 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 67 2e  _busy_timeout(g.
4210: 64 62 2c 20 35 30 30 30 29 3b 0a 20 20 20 20 64  db, 5000);.    d
4220: 62 5f 63 6f 6e 6e 65 63 74 69 6f 6e 5f 69 6e 69  b_connection_ini
4230: 74 28 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  t();.  }else{.  
4240: 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28    db_multi_exec(
4250: 22 41 54 54 41 43 48 20 44 41 54 41 42 41 53 45  "ATTACH DATABASE
4260: 20 25 51 20 41 53 20 25 73 22 2c 20 7a 44 62 4e   %Q AS %s", zDbN
4270: 61 6d 65 2c 20 7a 4c 61 62 65 6c 29 3b 0a 20 20  ame, zLabel);.  
4280: 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20  }.}../*.** Open 
4290: 74 68 65 20 75 73 65 72 20 64 61 74 61 62 61 73  the user databas
42a0: 65 20 69 6e 20 22 7e 2f 2e 66 6f 73 73 69 6c 22  e in "~/.fossil"
42b0: 2e 20 20 43 72 65 61 74 65 20 74 68 65 20 64 61  .  Create the da
42c0: 74 61 62 61 73 65 20 61 6e 65 77 20 69 66 0a 2a  tabase anew if.*
42d0: 2a 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c  * it does not al
42e0: 72 65 61 64 79 20 65 78 69 73 74 2e 0a 2a 2f 0a  ready exist..*/.
42f0: 76 6f 69 64 20 64 62 5f 6f 70 65 6e 5f 63 6f 6e  void db_open_con
4300: 66 69 67 28 76 6f 69 64 29 7b 0a 20 20 63 68 61  fig(void){.  cha
4310: 72 20 2a 7a 44 62 4e 61 6d 65 3b 0a 20 20 63 6f  r *zDbName;.  co
4320: 6e 73 74 20 63 68 61 72 20 2a 7a 48 6f 6d 65 3b  nst char *zHome;
4330: 0a 20 20 69 66 28 20 67 2e 63 6f 6e 66 69 67 4f  .  if( g.configO
4340: 70 65 6e 20 29 20 72 65 74 75 72 6e 3b 0a 23 69  pen ) return;.#i
4350: 66 64 65 66 20 5f 5f 4d 49 4e 47 57 33 32 5f 5f  fdef __MINGW32__
4360: 0a 20 20 7a 48 6f 6d 65 20 3d 20 67 65 74 65 6e  .  zHome = geten
4370: 76 28 22 4c 4f 43 41 4c 41 50 50 44 41 54 41 22  v("LOCALAPPDATA"
4380: 29 3b 0a 20 20 69 66 28 20 7a 48 6f 6d 65 3d 3d  );.  if( zHome==
4390: 30 20 29 7b 0a 20 20 20 20 7a 48 6f 6d 65 20 3d  0 ){.    zHome =
43a0: 20 67 65 74 65 6e 76 28 22 41 50 50 44 41 54 41   getenv("APPDATA
43b0: 22 29 3b 0a 20 20 20 20 69 66 28 20 7a 48 6f 6d  ");.    if( zHom
43c0: 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 7a 48  e==0 ){.      zH
43d0: 6f 6d 65 20 3d 20 67 65 74 65 6e 76 28 22 48 4f  ome = getenv("HO
43e0: 4d 45 50 41 54 48 22 29 3b 0a 20 20 20 20 7d 0a  MEPATH");.    }.
43f0: 20 20 7d 0a 23 65 6c 73 65 0a 20 20 7a 48 6f 6d    }.#else.  zHom
4400: 65 20 3d 20 67 65 74 65 6e 76 28 22 48 4f 4d 45  e = getenv("HOME
4410: 22 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28  ");.#endif.  if(
4420: 20 7a 48 6f 6d 65 3d 3d 30 20 29 7b 0a 20 20 20   zHome==0 ){.   
4430: 20 64 62 5f 65 72 72 28 22 63 61 6e 6e 6f 74 20   db_err("cannot 
4440: 6c 6f 63 61 74 65 20 68 6f 6d 65 20 64 69 72 65  locate home dire
4450: 63 74 6f 72 79 22 29 3b 0a 20 20 7d 0a 23 69 66  ctory");.  }.#if
4460: 64 65 66 20 5f 5f 4d 49 4e 47 57 33 32 5f 5f 0a  def __MINGW32__.
4470: 20 20 2f 2a 20 2e 20 66 69 6c 65 6e 61 6d 65 73    /* . filenames
4480: 20 67 69 76 65 20 73 6f 6d 65 20 77 69 6e 64 6f   give some windo
4490: 77 20 73 79 73 74 65 6d 73 20 70 72 6f 62 6c 65  w systems proble
44a0: 6d 73 20 61 6e 64 20 6d 61 6e 79 20 61 70 70 73  ms and many apps
44b0: 20 70 72 6f 62 6c 65 6d 73 20 2a 2f 0a 20 20 7a   problems */.  z
44c0: 44 62 4e 61 6d 65 20 3d 20 6d 70 72 69 6e 74 66  DbName = mprintf
44d0: 28 22 25 2f 2f 5f 66 6f 73 73 69 6c 22 2c 20 7a  ("%//_fossil", z
44e0: 48 6f 6d 65 29 3b 0a 23 65 6c 73 65 0a 20 20 7a  Home);.#else.  z
44f0: 44 62 4e 61 6d 65 20 3d 20 6d 70 72 69 6e 74 66  DbName = mprintf
4500: 28 22 25 73 2f 2e 66 6f 73 73 69 6c 22 2c 20 7a  ("%s/.fossil", z
4510: 48 6f 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20  Home);.#endif.  
4520: 69 66 28 20 66 69 6c 65 5f 73 69 7a 65 28 7a 44  if( file_size(zD
4530: 62 4e 61 6d 65 29 3c 31 30 32 34 2a 33 20 29 7b  bName)<1024*3 ){
4540: 0a 20 20 20 20 64 62 5f 69 6e 69 74 5f 64 61 74  .    db_init_dat
4550: 61 62 61 73 65 28 7a 44 62 4e 61 6d 65 2c 20 7a  abase(zDbName, z
4560: 43 6f 6e 66 69 67 53 63 68 65 6d 61 2c 20 28 63  ConfigSchema, (c
4570: 68 61 72 2a 29 30 29 3b 0a 20 20 7d 0a 20 20 64  har*)0);.  }.  d
4580: 62 5f 6f 70 65 6e 5f 6f 72 5f 61 74 74 61 63 68  b_open_or_attach
4590: 28 7a 44 62 4e 61 6d 65 2c 20 22 63 6f 6e 66 69  (zDbName, "confi
45a0: 67 64 62 22 29 3b 0a 20 20 67 2e 63 6f 6e 66 69  gdb");.  g.confi
45b0: 67 4f 70 65 6e 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a  gOpen = 1;.}../*
45c0: 0a 2a 2a 20 49 66 20 7a 44 62 4e 61 6d 65 20 69  .** If zDbName i
45d0: 73 20 61 20 76 61 6c 69 64 20 6c 6f 63 61 6c 20  s a valid local 
45e0: 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 6f  database file, o
45f0: 70 65 6e 20 69 74 20 61 6e 64 20 72 65 74 75 72  pen it and retur
4600: 6e 0a 2a 2a 20 74 72 75 65 2e 20 20 49 66 20 69  n.** true.  If i
4610: 74 20 69 73 20 6e 6f 74 20 61 20 76 61 6c 69 64  t is not a valid
4620: 20 6c 6f 63 61 6c 20 64 61 74 61 62 61 73 65 20   local database 
4630: 66 69 6c 65 2c 20 72 65 74 75 72 6e 20 30 2e 0a  file, return 0..
4640: 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73  */.static int is
4650: 56 61 6c 69 64 4c 6f 63 61 6c 44 62 28 63 6f 6e  ValidLocalDb(con
4660: 73 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65  st char *zDbName
4670: 29 7b 0a 20 20 69 36 34 20 6c 73 69 7a 65 3b 0a  ){.  i64 lsize;.
4680: 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69    int rc;.  sqli
4690: 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b  te3_stmt *pStmt;
46a0: 0a 0a 20 20 69 66 28 20 61 63 63 65 73 73 28 7a  ..  if( access(z
46b0: 44 62 4e 61 6d 65 2c 20 46 5f 4f 4b 29 20 29 20  DbName, F_OK) ) 
46c0: 72 65 74 75 72 6e 20 30 3b 0a 20 20 6c 73 69 7a  return 0;.  lsiz
46d0: 65 20 3d 20 66 69 6c 65 5f 73 69 7a 65 28 7a 44  e = file_size(zD
46e0: 62 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 6c 73  bName);.  if( ls
46f0: 69 7a 65 25 31 30 32 34 21 3d 30 20 7c 7c 20 6c  ize%1024!=0 || l
4700: 73 69 7a 65 3c 34 30 39 36 20 29 20 72 65 74 75  size<4096 ) retu
4710: 72 6e 20 30 3b 0a 20 20 64 62 5f 6f 70 65 6e 5f  rn 0;.  db_open_
4720: 6f 72 5f 61 74 74 61 63 68 28 7a 44 62 4e 61 6d  or_attach(zDbNam
4730: 65 2c 20 22 6c 6f 63 61 6c 64 62 22 29 3b 0a 20  e, "localdb");. 
4740: 20 67 2e 6c 6f 63 61 6c 4f 70 65 6e 20 3d 20 31   g.localOpen = 1
4750: 3b 0a 20 20 64 62 5f 6f 70 65 6e 5f 63 6f 6e 66  ;.  db_open_conf
4760: 69 67 28 29 3b 0a 20 20 64 62 5f 6f 70 65 6e 5f  ig();.  db_open_
4770: 72 65 70 6f 73 69 74 6f 72 79 28 30 29 3b 0a 0a  repository(0);..
4780: 20 20 2f 2a 20 49 66 20 74 68 65 20 22 6d 74 69    /* If the "mti
4790: 6d 65 22 20 63 6f 6c 75 6d 6e 20 69 73 20 6d 69  me" column is mi
47a0: 73 73 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 76  ssing from the v
47b0: 66 69 6c 65 20 74 61 62 6c 65 2c 20 74 68 65 6e  file table, then
47c0: 0a 20 20 2a 2a 20 61 64 64 20 69 74 20 6e 6f 77  .  ** add it now
47d0: 2e 20 20 20 54 68 69 73 20 63 6f 64 65 20 61 64  .   This code ad
47e0: 64 65 64 20 6f 6e 20 32 30 30 38 2d 31 32 2d 30  ded on 2008-12-0
47f0: 36 2e 20 20 41 66 74 65 72 20 61 6c 6c 20 75 73  6.  After all us
4800: 65 72 73 20 68 61 76 65 0a 20 20 2a 2a 20 75 70  ers have.  ** up
4810: 67 72 61 64 65 64 2c 20 74 68 69 73 20 63 6f 64  graded, this cod
4820: 65 20 63 61 6e 20 62 65 20 73 61 66 65 6c 79 20  e can be safely 
4830: 64 65 6c 65 74 65 64 2e 20 0a 20 20 2a 2f 0a 20  deleted. .  */. 
4840: 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72   rc = sqlite3_pr
4850: 65 70 61 72 65 28 67 2e 64 62 2c 20 22 53 45 4c  epare(g.db, "SEL
4860: 45 43 54 20 6d 74 69 6d 65 20 46 52 4f 4d 20 76  ECT mtime FROM v
4870: 66 69 6c 65 22 2c 20 2d 31 2c 20 26 70 53 74 6d  file", -1, &pStm
4880: 74 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  t, 0);.  sqlite3
4890: 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29  _finalize(pStmt)
48a0: 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
48b0: 54 45 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  TE_ERROR ){.    
48c0: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 67 2e 64  sqlite3_exec(g.d
48d0: 62 2c 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20  b, "ALTER TABLE 
48e0: 76 66 69 6c 65 20 41 44 44 20 43 4f 4c 55 4d 4e  vfile ADD COLUMN
48f0: 20 6d 74 69 6d 65 20 49 4e 54 45 47 45 52 22 2c   mtime INTEGER",
4900: 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 0a   0, 0, 0);.  }..
4910: 20 20 2f 2a 20 49 66 20 74 68 65 20 22 6f 72 69    /* If the "ori
4920: 67 6e 61 6d 65 22 20 63 6f 6c 75 6d 6e 20 69 73  gname" column is
4930: 20 6d 69 73 73 69 6e 67 20 66 72 6f 6d 20 74 68   missing from th
4940: 65 20 76 66 69 6c 65 20 74 61 62 6c 65 2c 20 74  e vfile table, t
4950: 68 65 6e 0a 20 20 2a 2a 20 61 64 64 20 69 74 20  hen.  ** add it 
4960: 6e 6f 77 2e 20 20 20 54 68 69 73 20 63 6f 64 65  now.   This code
4970: 20 61 64 64 65 64 20 6f 6e 20 32 30 30 38 2d 31   added on 2008-1
4980: 31 2d 30 39 2e 20 20 41 66 74 65 72 20 61 6c 6c  1-09.  After all
4990: 20 75 73 65 72 73 20 68 61 76 65 0a 20 20 2a 2a   users have.  **
49a0: 20 75 70 67 72 61 64 65 64 2c 20 74 68 69 73 20   upgraded, this 
49b0: 63 6f 64 65 20 63 61 6e 20 62 65 20 73 61 66 65  code can be safe
49c0: 6c 79 20 64 65 6c 65 74 65 64 2e 20 0a 20 20 2a  ly deleted. .  *
49d0: 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  /.  rc = sqlite3
49e0: 5f 70 72 65 70 61 72 65 28 67 2e 64 62 2c 20 22  _prepare(g.db, "
49f0: 53 45 4c 45 43 54 20 6f 72 69 67 6e 61 6d 65 20  SELECT origname 
4a00: 46 52 4f 4d 20 76 66 69 6c 65 22 2c 20 2d 31 2c  FROM vfile", -1,
4a10: 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 73   &pStmt, 0);.  s
4a20: 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
4a30: 70 53 74 6d 74 29 3b 0a 20 20 69 66 28 20 72 63  pStmt);.  if( rc
4a40: 3d 3d 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 29  ==SQLITE_ERROR )
4a50: 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 65 78  {.    sqlite3_ex
4a60: 65 63 28 67 2e 64 62 2c 20 22 41 4c 54 45 52 20  ec(g.db, "ALTER 
4a70: 54 41 42 4c 45 20 76 66 69 6c 65 20 41 44 44 20  TABLE vfile ADD 
4a80: 43 4f 4c 55 4d 4e 20 6f 72 69 67 6e 61 6d 65 20  COLUMN origname 
4a90: 54 45 58 54 22 2c 20 30 2c 20 30 2c 20 30 29 3b  TEXT", 0, 0, 0);
4aa0: 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 31  .  }..  return 1
4ab0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74  ;.}../*.** Locat
4ac0: 65 20 74 68 65 20 72 6f 6f 74 20 64 69 72 65 63  e the root direc
4ad0: 74 6f 72 79 20 6f 66 20 74 68 65 20 6c 6f 63 61  tory of the loca
4ae0: 6c 20 72 65 70 6f 73 69 74 6f 72 79 20 74 72 65  l repository tre
4af0: 65 2e 20 20 54 68 65 20 72 6f 6f 74 0a 2a 2a 20  e.  The root.** 
4b00: 64 69 72 65 63 74 6f 72 79 20 69 73 20 66 6f 75  directory is fou
4b10: 6e 64 20 62 79 20 73 65 61 72 63 68 69 6e 67 20  nd by searching 
4b20: 66 6f 72 20 61 20 66 69 6c 65 20 6e 61 6d 65 64  for a file named
4b30: 20 22 5f 46 4f 53 53 49 4c 5f 22 20 6f 72 20 22   "_FOSSIL_" or "
4b40: 2e 66 6f 73 22 0a 2a 2a 20 74 68 61 74 20 63 6f  .fos".** that co
4b50: 6e 74 61 69 6e 73 20 61 20 76 61 6c 69 64 20 72  ntains a valid r
4b60: 65 70 6f 73 69 74 6f 72 79 20 64 61 74 61 62 61  epository databa
4b70: 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20  se..**.** If no 
4b80: 76 61 6c 69 64 20 5f 46 4f 53 53 49 4c 5f 20 6f  valid _FOSSIL_ o
4b90: 72 20 2e 66 6f 73 20 66 69 6c 65 20 69 73 20 66  r .fos file is f
4ba0: 6f 75 6e 64 2c 20 77 65 20 6d 6f 76 65 20 75 70  ound, we move up
4bb0: 20 6f 6e 65 20 6c 65 76 65 6c 20 61 6e 64 20 0a   one level and .
4bc0: 2a 2a 20 74 72 79 20 61 67 61 69 6e 2e 20 4f 6e  ** try again. On
4bd0: 63 65 20 74 68 65 20 66 69 6c 65 20 69 73 20 66  ce the file is f
4be0: 6f 75 6e 64 2c 20 74 68 65 20 67 2e 7a 4c 6f 63  ound, the g.zLoc
4bf0: 61 6c 52 6f 6f 74 20 76 61 72 69 61 62 6c 65 20  alRoot variable 
4c00: 69 73 20 73 65 74 0a 2a 2a 20 74 6f 20 74 68 65  is set.** to the
4c10: 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 72 65 70   root of the rep
4c20: 6f 73 69 74 6f 72 79 20 74 72 65 65 20 61 6e 64  ository tree and
4c30: 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
4c40: 74 75 72 6e 73 20 31 2e 20 20 49 66 0a 2a 2a 20  turns 1.  If.** 
4c50: 6e 6f 20 64 61 74 61 62 61 73 65 20 69 73 20 66  no database is f
4c60: 6f 75 6e 64 2c 20 74 68 65 6e 20 74 68 69 73 20  ound, then this 
4c70: 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 20 30  routine return 0
4c80: 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
4c90: 74 69 6e 65 20 61 6c 77 61 79 73 20 6f 70 65 6e  tine always open
4ca0: 73 20 74 68 65 20 75 73 65 72 20 64 61 74 61 62  s the user datab
4cb0: 61 73 65 20 72 65 67 61 72 64 6c 65 73 73 20 6f  ase regardless o
4cc0: 66 20 77 68 65 74 68 65 72 20 6f 72 0a 2a 2a 20  f whether or.** 
4cd0: 6e 6f 74 20 74 68 65 20 72 65 70 6f 73 69 74 6f  not the reposito
4ce0: 72 79 20 64 61 74 61 62 61 73 65 20 69 73 20 66  ry database is f
4cf0: 6f 75 6e 64 2e 20 20 49 66 20 74 68 65 20 5f 46  ound.  If the _F
4d00: 4f 53 53 49 4c 5f 20 6f 72 20 2e 66 6f 73 20 66  OSSIL_ or .fos f
4d10: 69 6c 65 0a 2a 2a 20 69 73 20 66 6f 75 6e 64 2c  ile.** is found,
4d20: 20 69 74 20 69 73 20 61 74 74 61 63 68 65 64 20   it is attached 
4d30: 74 6f 20 74 68 65 20 6f 70 65 6e 20 64 61 74 61  to the open data
4d40: 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
4d50: 74 6f 6f 2e 0a 2a 2f 0a 69 6e 74 20 64 62 5f 6f  too..*/.int db_o
4d60: 70 65 6e 5f 6c 6f 63 61 6c 28 76 6f 69 64 29 7b  pen_local(void){
4d70: 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 63  .  int i, n;.  c
4d80: 68 61 72 20 7a 50 77 64 5b 32 30 30 30 5d 3b 0a  har zPwd[2000];.
4d90: 20 20 63 68 61 72 20 2a 7a 50 77 64 43 6f 6e 76    char *zPwdConv
4da0: 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.  static const
4db0: 20 63 68 61 72 20 2a 61 44 62 4e 61 6d 65 5b 5d   char *aDbName[]
4dc0: 20 3d 20 7b 20 22 2f 5f 46 4f 53 53 49 4c 5f 22   = { "/_FOSSIL_"
4dd0: 2c 20 22 2f 2e 66 6f 73 22 20 7d 3b 0a 20 20 0a  , "/.fos" };.  .
4de0: 20 20 69 66 28 20 67 2e 6c 6f 63 61 6c 4f 70 65    if( g.localOpe
4df0: 6e 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 69  n) return 1;.  i
4e00: 66 28 20 67 65 74 63 77 64 28 7a 50 77 64 2c 20  f( getcwd(zPwd, 
4e10: 73 69 7a 65 6f 66 28 7a 50 77 64 29 2d 32 30 29  sizeof(zPwd)-20)
4e20: 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62 5f 65 72  ==0 ){.    db_er
4e30: 72 28 22 70 77 64 20 74 6f 6f 20 62 69 67 3a 20  r("pwd too big: 
4e40: 6d 61 78 20 25 64 22 2c 20 73 69 7a 65 6f 66 28  max %d", sizeof(
4e50: 7a 50 77 64 29 2d 32 30 29 3b 0a 20 20 7d 0a 20  zPwd)-20);.  }. 
4e60: 20 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 50 77 64   n = strlen(zPwd
4e70: 29 3b 0a 20 20 7a 50 77 64 43 6f 6e 76 20 3d 20  );.  zPwdConv = 
4e80: 6d 70 72 69 6e 74 66 28 22 25 2f 22 2c 20 7a 50  mprintf("%/", zP
4e90: 77 64 29 3b 0a 20 20 73 74 72 6e 63 70 79 28 7a  wd);.  strncpy(z
4ea0: 50 77 64 2c 20 7a 50 77 64 43 6f 6e 76 2c 20 32  Pwd, zPwdConv, 2
4eb0: 30 30 30 2d 32 30 29 3b 0a 20 20 66 72 65 65 28  000-20);.  free(
4ec0: 7a 50 77 64 43 6f 6e 76 29 3b 0a 20 20 77 68 69  zPwdConv);.  whi
4ed0: 6c 65 28 20 6e 3e 30 20 29 7b 0a 20 20 20 20 69  le( n>0 ){.    i
4ee0: 66 28 20 61 63 63 65 73 73 28 7a 50 77 64 2c 20  f( access(zPwd, 
4ef0: 57 5f 4f 4b 29 20 29 20 62 72 65 61 6b 3b 0a 20  W_OK) ) break;. 
4f00: 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69     for(i=0; i<si
4f10: 7a 65 6f 66 28 61 44 62 4e 61 6d 65 29 2f 73 69  zeof(aDbName)/si
4f20: 7a 65 6f 66 28 61 44 62 4e 61 6d 65 5b 30 5d 29  zeof(aDbName[0])
4f30: 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 74  ; i++){.      st
4f40: 72 63 70 79 28 26 7a 50 77 64 5b 6e 5d 2c 20 61  rcpy(&zPwd[n], a
4f50: 44 62 4e 61 6d 65 5b 69 5d 29 3b 0a 20 20 20 20  DbName[i]);.    
4f60: 20 20 69 66 28 20 69 73 56 61 6c 69 64 4c 6f 63    if( isValidLoc
4f70: 61 6c 44 62 28 7a 50 77 64 29 20 29 7b 0a 20 20  alDb(zPwd) ){.  
4f80: 20 20 20 20 20 20 2f 2a 20 46 6f 75 6e 64 20 61        /* Found a
4f90: 20 76 61 6c 69 64 20 63 68 65 63 6b 6f 75 74 20   valid checkout 
4fa0: 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
4fb0: 0a 20 20 20 20 20 20 20 20 7a 50 77 64 5b 6e 5d  .        zPwd[n]
4fc0: 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 77 68   = 0;.        wh
4fd0: 69 6c 65 28 20 6e 3e 31 20 26 26 20 7a 50 77 64  ile( n>1 && zPwd
4fe0: 5b 6e 2d 31 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20  [n-1]=='/' ){.  
4ff0: 20 20 20 20 20 20 20 20 6e 2d 2d 3b 0a 20 20 20          n--;.   
5000: 20 20 20 20 20 20 20 7a 50 77 64 5b 6e 5d 20 3d         zPwd[n] =
5010: 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   0;.        }.  
5020: 20 20 20 20 20 20 67 2e 7a 4c 6f 63 61 6c 52 6f        g.zLocalRo
5030: 6f 74 20 3d 20 6d 70 72 69 6e 74 66 28 22 25 73  ot = mprintf("%s
5040: 2f 22 2c 20 7a 50 77 64 29 3b 0a 20 20 20 20 20  /", zPwd);.     
5050: 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
5060: 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 6e     }.    }.    n
5070: 2d 2d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 6e  --;.    while( n
5080: 3e 30 20 26 26 20 7a 50 77 64 5b 6e 5d 21 3d 27  >0 && zPwd[n]!='
5090: 2f 27 20 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 20  /' ){ n--; }.   
50a0: 20 77 68 69 6c 65 28 20 6e 3e 30 20 26 26 20 7a   while( n>0 && z
50b0: 50 77 64 5b 6e 2d 31 5d 3d 3d 27 2f 27 20 29 7b  Pwd[n-1]=='/' ){
50c0: 20 6e 2d 2d 3b 20 7d 0a 20 20 20 20 7a 50 77 64   n--; }.    zPwd
50d0: 5b 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  [n] = 0;.  }..  
50e0: 2f 2a 20 41 20 63 68 65 63 6b 6f 75 74 20 64 61  /* A checkout da
50f0: 74 61 62 61 73 65 20 66 69 6c 65 20 63 6f 75 6c  tabase file coul
5100: 64 20 6e 6f 74 20 62 65 20 66 6f 75 6e 64 20 2a  d not be found *
5110: 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  /.  return 0;.}.
5120: 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20  ./*.** Open the 
5130: 72 65 70 6f 73 69 74 6f 72 79 20 64 61 74 61 62  repository datab
5140: 61 73 65 20 67 69 76 65 6e 20 62 79 20 7a 44 62  ase given by zDb
5150: 4e 61 6d 65 2e 20 20 49 66 20 7a 44 62 4e 61 6d  Name.  If zDbNam
5160: 65 3d 3d 4e 55 4c 4c 20 74 68 65 6e 0a 2a 2a 20  e==NULL then.** 
5170: 67 65 74 20 74 68 65 20 6e 61 6d 65 20 66 72 6f  get the name fro
5180: 6d 20 74 68 65 20 61 6c 72 65 61 64 79 20 6f 70  m the already op
5190: 65 6e 20 6c 6f 63 61 6c 20 64 61 74 61 62 61 73  en local databas
51a0: 65 2e 0a 2a 2f 0a 76 6f 69 64 20 64 62 5f 6f 70  e..*/.void db_op
51b0: 65 6e 5f 72 65 70 6f 73 69 74 6f 72 79 28 63 6f  en_repository(co
51c0: 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d  nst char *zDbNam
51d0: 65 29 7b 0a 20 20 69 66 28 20 67 2e 72 65 70 6f  e){.  if( g.repo
51e0: 73 69 74 6f 72 79 4f 70 65 6e 20 29 20 72 65 74  sitoryOpen ) ret
51f0: 75 72 6e 3b 0a 20 20 69 66 28 20 7a 44 62 4e 61  urn;.  if( zDbNa
5200: 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  me==0 ){.    if(
5210: 20 67 2e 6c 6f 63 61 6c 4f 70 65 6e 20 29 7b 0a   g.localOpen ){.
5220: 20 20 20 20 20 20 7a 44 62 4e 61 6d 65 20 3d 20        zDbName = 
5230: 64 62 5f 6c 67 65 74 28 22 72 65 70 6f 73 69 74  db_lget("reposit
5240: 6f 72 79 22 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  ory", 0);.    }.
5250: 20 20 20 20 69 66 28 20 7a 44 62 4e 61 6d 65 3d      if( zDbName=
5260: 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 62 5f 65  =0 ){.      db_e
5270: 72 72 28 22 75 6e 61 62 6c 65 20 74 6f 20 66 69  rr("unable to fi
5280: 6e 64 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61  nd the name of a
5290: 20 72 65 70 6f 73 69 74 6f 72 79 20 64 61 74 61   repository data
52a0: 62 61 73 65 22 29 3b 0a 20 20 20 20 7d 0a 20 20  base");.    }.  
52b0: 7d 0a 20 20 69 66 28 20 61 63 63 65 73 73 28 7a  }.  if( access(z
52c0: 44 62 4e 61 6d 65 2c 20 52 5f 4f 4b 29 20 7c 7c  DbName, R_OK) ||
52d0: 20 66 69 6c 65 5f 73 69 7a 65 28 7a 44 62 4e 61   file_size(zDbNa
52e0: 6d 65 29 3c 31 30 32 34 20 29 7b 0a 20 20 20 20  me)<1024 ){.    
52f0: 69 66 28 20 61 63 63 65 73 73 28 7a 44 62 4e 61  if( access(zDbNa
5300: 6d 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20  me, 0) ){.      
5310: 66 6f 73 73 69 6c 5f 70 61 6e 69 63 28 22 72 65  fossil_panic("re
5320: 70 6f 73 69 74 6f 72 79 20 64 6f 65 73 20 6e 6f  pository does no
5330: 74 20 65 78 69 73 74 73 20 6f 72 22 0a 20 20 20  t exists or".   
5340: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5350: 22 20 69 73 20 69 6e 20 61 6e 20 75 6e 72 65 61  " is in an unrea
5360: 64 61 62 6c 65 20 64 69 72 65 63 74 6f 72 79 3a  dable directory:
5370: 20 25 73 22 2c 20 7a 44 62 4e 61 6d 65 29 3b 0a   %s", zDbName);.
5380: 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 61 63      }else if( ac
5390: 63 65 73 73 28 7a 44 62 4e 61 6d 65 2c 20 52 5f  cess(zDbName, R_
53a0: 4f 4b 29 20 29 7b 0a 20 20 20 20 20 20 66 6f 73  OK) ){.      fos
53b0: 73 69 6c 5f 70 61 6e 69 63 28 22 72 65 61 64 20  sil_panic("read 
53c0: 70 65 72 6d 69 73 73 69 6f 6e 20 64 65 6e 69 65  permission denie
53d0: 64 20 66 6f 72 20 72 65 70 6f 73 69 74 6f 72 79  d for repository
53e0: 20 25 73 22 2c 20 7a 44 62 4e 61 6d 65 29 3b 0a   %s", zDbName);.
53f0: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
5400: 20 66 6f 73 73 69 6c 5f 70 61 6e 69 63 28 22 6e   fossil_panic("n
5410: 6f 74 20 61 20 76 61 6c 69 64 20 72 65 70 6f 73  ot a valid repos
5420: 69 74 6f 72 79 3a 20 25 73 22 2c 20 7a 44 62 4e  itory: %s", zDbN
5430: 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ame);.    }.  }.
5440: 20 20 64 62 5f 6f 70 65 6e 5f 6f 72 5f 61 74 74    db_open_or_att
5450: 61 63 68 28 7a 44 62 4e 61 6d 65 2c 20 22 72 65  ach(zDbName, "re
5460: 70 6f 73 69 74 6f 72 79 22 29 3b 0a 20 20 67 2e  pository");.  g.
5470: 72 65 70 6f 73 69 74 6f 72 79 4f 70 65 6e 20 3d  repositoryOpen =
5480: 20 31 3b 0a 20 20 67 2e 7a 52 65 70 6f 73 69 74   1;.  g.zReposit
5490: 6f 72 79 4e 61 6d 65 20 3d 20 6d 70 72 69 6e 74  oryName = mprint
54a0: 66 28 22 25 73 22 2c 20 7a 44 62 4e 61 6d 65 29  f("%s", zDbName)
54b0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74  ;.}../*.** Try t
54c0: 6f 20 66 69 6e 64 20 74 68 65 20 72 65 70 6f 73  o find the repos
54d0: 69 74 6f 72 79 20 61 6e 64 20 6f 70 65 6e 20 69  itory and open i
54e0: 74 2e 20 20 55 73 65 20 74 68 65 20 2d 52 20 6f  t.  Use the -R o
54f0: 72 20 2d 2d 72 65 70 6f 73 69 74 6f 72 79 0a 2a  r --repository.*
5500: 2a 20 6f 70 74 69 6f 6e 20 74 6f 20 6c 6f 63 61  * option to loca
5510: 74 65 20 74 68 65 20 72 65 70 6f 73 69 74 6f 72  te the repositor
5520: 79 2e 20 20 49 66 20 6e 6f 20 73 75 63 68 20 6f  y.  If no such o
5530: 70 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62  ption is availab
5540: 6c 65 2c 20 74 68 65 6e 0a 2a 2a 20 75 73 65 20  le, then.** use 
5550: 74 68 65 20 72 65 70 6f 73 69 74 6f 72 79 20 6f  the repository o
5560: 66 20 74 68 65 20 6f 70 65 6e 20 63 68 65 63 6b  f the open check
5570: 6f 75 74 20 69 66 20 74 68 65 72 65 20 69 73 20  out if there is 
5580: 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 45 72 72 6f 72  one..**.** Error
5590: 20 6f 75 74 20 69 66 20 74 68 65 20 72 65 70 6f   out if the repo
55a0: 73 69 74 6f 72 79 20 63 61 6e 6e 6f 74 20 62 65  sitory cannot be
55b0: 20 6f 70 65 6e 65 64 2e 0a 2a 2f 0a 76 6f 69 64   opened..*/.void
55c0: 20 64 62 5f 66 69 6e 64 5f 61 6e 64 5f 6f 70 65   db_find_and_ope
55d0: 6e 5f 72 65 70 6f 73 69 74 6f 72 79 28 69 6e 74  n_repository(int
55e0: 20 65 72 72 49 66 4e 6f 74 46 6f 75 6e 64 29 7b   errIfNotFound){
55f0: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
5600: 52 65 70 20 3d 20 66 69 6e 64 5f 6f 70 74 69 6f  Rep = find_optio
5610: 6e 28 22 72 65 70 6f 73 69 74 6f 72 79 22 2c 20  n("repository", 
5620: 22 52 22 2c 20 31 29 3b 0a 20 20 69 66 28 20 7a  "R", 1);.  if( z
5630: 52 65 70 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  Rep==0 ){.    if
5640: 28 20 64 62 5f 6f 70 65 6e 5f 6c 6f 63 61 6c 28  ( db_open_local(
5650: 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f  )==0 ){.      go
5660: 74 6f 20 72 65 70 5f 6e 6f 74 5f 66 6f 75 6e 64  to rep_not_found
5670: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 52 65 70  ;.    }.    zRep
5680: 20 3d 20 64 62 5f 6c 67 65 74 28 22 72 65 70 6f   = db_lget("repo
5690: 73 69 74 6f 72 79 22 2c 20 30 29 3b 0a 20 20 20  sitory", 0);.   
56a0: 20 69 66 28 20 7a 52 65 70 3d 3d 30 20 29 7b 0a   if( zRep==0 ){.
56b0: 20 20 20 20 20 20 67 6f 74 6f 20 72 65 70 5f 6e        goto rep_n
56c0: 6f 74 5f 66 6f 75 6e 64 3b 0a 20 20 20 20 7d 0a  ot_found;.    }.
56d0: 20 20 7d 0a 20 20 64 62 5f 6f 70 65 6e 5f 72 65    }.  db_open_re
56e0: 70 6f 73 69 74 6f 72 79 28 7a 52 65 70 29 3b 0a  pository(zRep);.
56f0: 20 20 69 66 28 20 67 2e 72 65 70 6f 73 69 74 6f    if( g.reposito
5700: 72 79 4f 70 65 6e 20 29 7b 0a 20 20 20 20 72 65  ryOpen ){.    re
5710: 74 75 72 6e 3b 0a 20 20 7d 0a 72 65 70 5f 6e 6f  turn;.  }.rep_no
5720: 74 5f 66 6f 75 6e 64 3a 0a 20 20 69 66 28 20 65  t_found:.  if( e
5730: 72 72 49 66 4e 6f 74 46 6f 75 6e 64 20 29 7b 0a  rrIfNotFound ){.
5740: 20 20 20 20 66 6f 73 73 69 6c 5f 66 61 74 61 6c      fossil_fatal
5750: 28 22 75 73 65 20 2d 2d 72 65 70 6f 73 69 74 6f  ("use --reposito
5760: 72 79 20 6f 72 20 2d 52 20 74 6f 20 73 70 65 63  ry or -R to spec
5770: 69 66 69 63 20 74 68 65 20 72 65 70 6f 73 69 74  ific the reposit
5780: 6f 72 79 20 64 61 74 61 62 61 73 65 22 29 3b 0a  ory database");.
5790: 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65    }.}../*.** Ope
57a0: 6e 20 74 68 65 20 6c 6f 63 61 6c 20 64 61 74 61  n the local data
57b0: 62 61 73 65 2e 20 20 49 66 20 75 6e 61 62 6c 65  base.  If unable
57c0: 2c 20 65 78 69 74 20 77 69 74 68 20 61 6e 20 65  , exit with an e
57d0: 72 72 6f 72 2e 0a 2a 2f 0a 76 6f 69 64 20 64 62  rror..*/.void db
57e0: 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f  _must_be_within_
57f0: 74 72 65 65 28 76 6f 69 64 29 7b 0a 20 20 69 66  tree(void){.  if
5800: 28 20 64 62 5f 6f 70 65 6e 5f 6c 6f 63 61 6c 28  ( db_open_local(
5810: 29 3d 3d 30 20 29 7b 0a 20 20 20 20 66 6f 73 73  )==0 ){.    foss
5820: 69 6c 5f 66 61 74 61 6c 28 22 6e 6f 74 20 77 69  il_fatal("not wi
5830: 74 68 69 6e 20 61 6e 20 6f 70 65 6e 20 63 68 65  thin an open che
5840: 63 6b 6f 75 74 22 29 3b 0a 20 20 7d 0a 20 20 64  ckout");.  }.  d
5850: 62 5f 6f 70 65 6e 5f 72 65 70 6f 73 69 74 6f 72  b_open_repositor
5860: 79 28 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  y(0);.}../*.** C
5870: 6c 6f 73 65 20 74 68 65 20 64 61 74 61 62 61 73  lose the databas
5880: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f  e connection..*/
5890: 0a 76 6f 69 64 20 64 62 5f 63 6c 6f 73 65 28 76  .void db_close(v
58a0: 6f 69 64 29 7b 0a 20 20 69 66 28 20 67 2e 64 62  oid){.  if( g.db
58b0: 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
58c0: 77 68 69 6c 65 28 20 70 41 6c 6c 53 74 6d 74 20  while( pAllStmt 
58d0: 29 7b 0a 20 20 20 20 64 62 5f 66 69 6e 61 6c 69  ){.    db_finali
58e0: 7a 65 28 70 41 6c 6c 53 74 6d 74 29 3b 0a 20 20  ze(pAllStmt);.  
58f0: 7d 0a 20 20 67 2e 72 65 70 6f 73 69 74 6f 72 79  }.  g.repository
5900: 4f 70 65 6e 20 3d 20 30 3b 0a 20 20 67 2e 6c 6f  Open = 0;.  g.lo
5910: 63 61 6c 4f 70 65 6e 20 3d 20 30 3b 0a 20 20 67  calOpen = 0;.  g
5920: 2e 63 6f 6e 66 69 67 4f 70 65 6e 20 3d 20 30 3b  .configOpen = 0;
5930: 0a 20 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65  .  sqlite3_close
5940: 28 67 2e 64 62 29 3b 0a 20 20 67 2e 64 62 20 3d  (g.db);.  g.db =
5950: 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72   0;.}.../*.** Cr
5960: 65 61 74 65 20 61 20 6e 65 77 20 65 6d 70 74 79  eate a new empty
5970: 20 72 65 70 6f 73 69 74 6f 72 79 20 64 61 74 61   repository data
5980: 62 61 73 65 20 77 69 74 68 20 74 68 65 20 67 69  base with the gi
5990: 76 65 6e 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20  ven name..**.** 
59a0: 4f 6e 6c 79 20 74 68 65 20 73 63 68 65 6d 61 20  Only the schema 
59b0: 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20  is initialized. 
59c0: 20 54 68 65 20 72 65 71 75 69 72 65 64 20 56 41   The required VA
59d0: 52 20 74 61 62 6c 65 73 20 65 6e 74 72 69 65 73  R tables entries
59e0: 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 73 65 74 20  .** are not set 
59f0: 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  by this routine 
5a00: 61 6e 64 20 6d 75 73 74 20 62 65 20 73 65 74 20  and must be set 
5a10: 73 65 70 61 72 61 74 65 6c 79 20 69 6e 20 6f 72  separately in or
5a20: 64 65 72 0a 2a 2a 20 74 6f 20 6d 61 6b 65 20 74  der.** to make t
5a30: 68 65 20 6e 65 77 20 66 69 6c 65 20 61 20 76 61  he new file a va
5a40: 6c 69 64 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f  lid database..*/
5a50: 0a 76 6f 69 64 20 64 62 5f 63 72 65 61 74 65 5f  .void db_create_
5a60: 72 65 70 6f 73 69 74 6f 72 79 28 63 6f 6e 73 74  repository(const
5a70: 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
5a80: 29 7b 0a 20 20 64 62 5f 69 6e 69 74 5f 64 61 74  ){.  db_init_dat
5a90: 61 62 61 73 65 28 0a 20 20 20 20 20 7a 46 69 6c  abase(.     zFil
5aa0: 65 6e 61 6d 65 2c 0a 20 20 20 20 20 7a 52 65 70  ename,.     zRep
5ab0: 6f 73 69 74 6f 72 79 53 63 68 65 6d 61 31 2c 0a  ositorySchema1,.
5ac0: 20 20 20 20 20 7a 52 65 70 6f 73 69 74 6f 72 79       zRepository
5ad0: 53 63 68 65 6d 61 32 2c 0a 20 20 20 20 20 28 63  Schema2,.     (c
5ae0: 68 61 72 2a 29 30 0a 20 20 29 3b 0a 20 20 69 73  har*)0.  );.  is
5af0: 4e 65 77 52 65 70 6f 20 3d 20 31 3b 0a 7d 0a 0a  NewRepo = 1;.}..
5b00: 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74 68 65  /*.** Create the
5b10: 20 64 65 66 61 75 6c 74 20 75 73 65 72 20 61 63   default user ac
5b20: 63 6f 75 6e 74 73 20 69 6e 20 74 68 65 20 55 53  counts in the US
5b30: 45 52 20 74 61 62 6c 65 2e 0a 2a 2f 0a 76 6f 69  ER table..*/.voi
5b40: 64 20 64 62 5f 63 72 65 61 74 65 5f 64 65 66 61  d db_create_defa
5b50: 75 6c 74 5f 75 73 65 72 73 28 69 6e 74 20 73 65  ult_users(int se
5b60: 74 75 70 55 73 65 72 4f 6e 6c 79 29 7b 0a 20 20  tupUserOnly){.  
5b70: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 55 73 65  const char *zUse
5b80: 72 3b 0a 20 20 7a 55 73 65 72 20 3d 20 64 62 5f  r;.  zUser = db_
5b90: 67 65 74 28 22 64 65 66 61 75 6c 74 2d 75 73 65  get("default-use
5ba0: 72 22 2c 20 30 29 3b 0a 20 20 69 66 28 20 7a 55  r", 0);.  if( zU
5bb0: 73 65 72 3d 3d 30 20 29 7b 0a 23 69 66 64 65 66  ser==0 ){.#ifdef
5bc0: 20 5f 5f 4d 49 4e 47 57 33 32 5f 5f 0a 20 20 20   __MINGW32__.   
5bd0: 20 7a 55 73 65 72 20 3d 20 67 65 74 65 6e 76 28   zUser = getenv(
5be0: 22 55 53 45 52 4e 41 4d 45 22 29 3b 0a 23 65 6c  "USERNAME");.#el
5bf0: 73 65 0a 20 20 20 20 7a 55 73 65 72 20 3d 20 67  se.    zUser = g
5c00: 65 74 65 6e 76 28 22 55 53 45 52 22 29 3b 0a 23  etenv("USER");.#
5c10: 65 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20  endif.  }.  if( 
5c20: 7a 55 73 65 72 3d 3d 30 20 29 7b 0a 20 20 20 20  zUser==0 ){.    
5c30: 7a 55 73 65 72 20 3d 20 22 72 6f 6f 74 22 3b 0a  zUser = "root";.
5c40: 20 20 7d 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65    }.  db_multi_e
5c50: 78 65 63 28 0a 20 20 20 20 20 22 49 4e 53 45 52  xec(.     "INSER
5c60: 54 20 49 4e 54 4f 20 75 73 65 72 28 6c 6f 67 69  T INTO user(logi
5c70: 6e 2c 20 70 77 2c 20 63 61 70 2c 20 69 6e 66 6f  n, pw, cap, info
5c80: 29 22 0a 20 20 20 20 20 22 56 41 4c 55 45 53 28  )".     "VALUES(
5c90: 25 51 2c 6c 6f 77 65 72 28 68 65 78 28 72 61 6e  %Q,lower(hex(ran
5ca0: 64 6f 6d 62 6c 6f 62 28 33 29 29 29 2c 27 73 27  domblob(3))),'s'
5cb0: 2c 27 27 29 22 2c 20 7a 55 73 65 72 0a 20 20 29  ,'')", zUser.  )
5cc0: 3b 0a 20 20 69 66 28 20 21 73 65 74 75 70 55 73  ;.  if( !setupUs
5cd0: 65 72 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 64 62  erOnly ){.    db
5ce0: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20  _multi_exec(.   
5cf0: 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f      "INSERT INTO
5d00: 20 75 73 65 72 28 6c 6f 67 69 6e 2c 70 77 2c 63   user(login,pw,c
5d10: 61 70 2c 69 6e 66 6f 29 22 0a 20 20 20 20 20 20  ap,info)".      
5d20: 20 22 20 20 20 56 41 4c 55 45 53 28 27 61 6e 6f   "   VALUES('ano
5d30: 6e 79 6d 6f 75 73 27 2c 27 61 6e 6f 6e 79 6d 6f  nymous','anonymo
5d40: 75 73 27 2c 27 67 68 6d 6e 63 7a 27 2c 27 41 6e  us','ghmncz','An
5d50: 6f 6e 27 29 3b 22 0a 20 20 20 20 20 20 20 22 49  on');".       "I
5d60: 4e 53 45 52 54 20 49 4e 54 4f 20 75 73 65 72 28  NSERT INTO user(
5d70: 6c 6f 67 69 6e 2c 70 77 2c 63 61 70 2c 69 6e 66  login,pw,cap,inf
5d80: 6f 29 22 0a 20 20 20 20 20 20 20 22 20 20 20 56  o)".       "   V
5d90: 41 4c 55 45 53 28 27 6e 6f 62 6f 64 79 27 2c 27  ALUES('nobody','
5da0: 27 2c 27 6a 6f 72 27 2c 27 4e 6f 62 6f 64 79 27  ','jor','Nobody'
5db0: 29 3b 22 0a 20 20 20 20 20 20 20 22 49 4e 53 45  );".       "INSE
5dc0: 52 54 20 49 4e 54 4f 20 75 73 65 72 28 6c 6f 67  RT INTO user(log
5dd0: 69 6e 2c 70 77 2c 63 61 70 2c 69 6e 66 6f 29 22  in,pw,cap,info)"
5de0: 0a 20 20 20 20 20 20 20 22 20 20 20 56 41 4c 55  .       "   VALU
5df0: 45 53 28 27 64 65 76 65 6c 6f 70 65 72 27 2c 27  ES('developer','
5e00: 27 2c 27 64 65 69 27 2c 27 44 65 76 27 29 3b 22  ','dei','Dev');"
5e10: 0a 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20  .       "INSERT 
5e20: 49 4e 54 4f 20 75 73 65 72 28 6c 6f 67 69 6e 2c  INTO user(login,
5e30: 70 77 2c 63 61 70 2c 69 6e 66 6f 29 22 0a 20 20  pw,cap,info)".  
5e40: 20 20 20 20 20 22 20 20 20 56 41 4c 55 45 53 28       "   VALUES(
5e50: 27 72 65 61 64 65 72 27 2c 27 27 2c 27 6b 70 74  'reader','','kpt
5e60: 77 27 2c 27 52 65 61 64 65 72 27 29 3b 22 0a 20  w','Reader');". 
5e70: 20 20 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a     );.  }.}../*.
5e80: 2a 2a 20 46 69 6c 6c 20 61 6e 20 65 6d 70 74 79  ** Fill an empty
5e90: 20 72 65 70 6f 73 69 74 6f 72 79 20 64 61 74 61   repository data
5ea0: 62 61 73 65 20 77 69 74 68 20 74 68 65 20 62 61  base with the ba
5eb0: 73 69 63 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  sic information 
5ec0: 66 6f 72 20 61 0a 2a 2a 20 72 65 70 6f 73 69 74  for a.** reposit
5ed0: 6f 72 79 2e 20 54 68 69 73 20 66 75 6e 63 74 69  ory. This functi
5ee0: 6f 6e 20 69 73 20 73 68 61 72 65 64 20 62 65 74  on is shared bet
5ef0: 77 65 65 6e 20 27 63 72 65 61 74 65 5f 72 65 70  ween 'create_rep
5f00: 6f 73 69 74 6f 72 79 5f 63 6d 64 27 0a 2a 2a 20  ository_cmd'.** 
5f10: 28 27 6e 65 77 27 29 20 61 6e 64 20 27 72 65 63  ('new') and 'rec
5f20: 6f 6e 73 74 72 75 63 74 5f 63 6d 64 27 20 28 27  onstruct_cmd' ('
5f30: 72 65 63 6f 6e 73 74 72 75 63 74 27 29 2c 20 62  reconstruct'), b
5f40: 6f 74 68 20 6f 66 20 77 68 69 63 68 20 63 72 65  oth of which cre
5f50: 61 74 65 0a 2a 2a 20 6e 65 77 20 72 65 70 6f 73  ate.** new repos
5f60: 69 74 6f 72 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 54  itories..**.** T
5f70: 68 65 20 6d 61 6b 65 49 6e 69 74 69 61 6c 56 65  he makeInitialVe
5f80: 72 73 69 6f 6e 20 66 6c 61 67 20 64 65 74 65 72  rsion flag deter
5f90: 6d 69 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72  mines whether or
5fa0: 20 6e 6f 74 20 61 6e 20 69 6e 69 74 69 61 6c 0a   not an initial.
5fb0: 2a 2a 20 6d 61 6e 69 66 65 73 74 20 69 73 20 63  ** manifest is c
5fc0: 72 65 61 74 65 64 2e 20 20 54 68 65 20 6d 61 6b  reated.  The mak
5fd0: 65 53 65 72 76 65 72 43 6f 64 65 73 20 66 6c 61  eServerCodes fla
5fe0: 67 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65  g determines whe
5ff0: 74 68 65 72 20 6f 72 0a 2a 2a 20 6e 6f 74 20 73  ther or.** not s
6000: 65 72 76 65 72 20 61 6e 64 20 70 72 6f 6a 65 63  erver and projec
6010: 74 20 63 6f 64 65 73 20 61 72 65 20 69 6e 76 65  t codes are inve
6020: 6e 74 65 64 20 66 6f 72 20 74 68 69 73 20 72 65  nted for this re
6030: 70 6f 73 69 74 6f 72 79 2e 0a 2a 2f 0a 76 6f 69  pository..*/.voi
6040: 64 20 64 62 5f 69 6e 69 74 69 61 6c 5f 73 65 74  d db_initial_set
6050: 75 70 20 28 69 6e 74 20 6d 61 6b 65 49 6e 69 74  up (int makeInit
6060: 69 61 6c 56 65 72 73 69 6f 6e 2c 20 69 6e 74 20  ialVersion, int 
6070: 6d 61 6b 65 53 65 72 76 65 72 43 6f 64 65 73 29  makeServerCodes)
6080: 7b 0a 20 20 63 68 61 72 20 2a 7a 44 61 74 65 3b  {.  char *zDate;
6090: 0a 20 20 42 6c 6f 62 20 68 61 73 68 3b 0a 20 20  .  Blob hash;.  
60a0: 42 6c 6f 62 20 6d 61 6e 69 66 65 73 74 3b 0a 0a  Blob manifest;..
60b0: 20 20 64 62 5f 73 65 74 28 22 63 6f 6e 74 65 6e    db_set("conten
60c0: 74 2d 73 63 68 65 6d 61 22 2c 20 43 4f 4e 54 45  t-schema", CONTE
60d0: 4e 54 5f 53 43 48 45 4d 41 2c 20 30 29 3b 0a 20  NT_SCHEMA, 0);. 
60e0: 20 64 62 5f 73 65 74 28 22 61 75 78 2d 73 63 68   db_set("aux-sch
60f0: 65 6d 61 22 2c 20 41 55 58 5f 53 43 48 45 4d 41  ema", AUX_SCHEMA
6100: 2c 20 30 29 3b 0a 20 20 69 66 28 20 6d 61 6b 65  , 0);.  if( make
6110: 53 65 72 76 65 72 43 6f 64 65 73 20 29 7b 0a 20  ServerCodes ){. 
6120: 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63     db_multi_exec
6130: 28 0a 20 20 20 20 20 20 22 49 4e 53 45 52 54 20  (.      "INSERT 
6140: 49 4e 54 4f 20 63 6f 6e 66 69 67 28 6e 61 6d 65  INTO config(name
6150: 2c 76 61 6c 75 65 29 22 0a 20 20 20 20 20 20 22  ,value)".      "
6160: 20 56 41 4c 55 45 53 28 27 73 65 72 76 65 72 2d   VALUES('server-
6170: 63 6f 64 65 27 2c 20 6c 6f 77 65 72 28 68 65 78  code', lower(hex
6180: 28 72 61 6e 64 6f 6d 62 6c 6f 62 28 32 30 29 29  (randomblob(20))
6190: 29 29 3b 22 0a 20 20 20 20 20 20 22 49 4e 53 45  ));".      "INSE
61a0: 52 54 20 49 4e 54 4f 20 63 6f 6e 66 69 67 28 6e  RT INTO config(n
61b0: 61 6d 65 2c 76 61 6c 75 65 29 22 0a 20 20 20 20  ame,value)".    
61c0: 20 20 22 20 56 41 4c 55 45 53 28 27 70 72 6f 6a    " VALUES('proj
61d0: 65 63 74 2d 63 6f 64 65 27 2c 20 6c 6f 77 65 72  ect-code', lower
61e0: 28 68 65 78 28 72 61 6e 64 6f 6d 62 6c 6f 62 28  (hex(randomblob(
61f0: 32 30 29 29 29 29 3b 22 0a 20 20 20 20 29 3b 0a  20))));".    );.
6200: 20 20 7d 0a 20 20 69 66 28 20 21 64 62 5f 69 73    }.  if( !db_is
6210: 5f 67 6c 6f 62 61 6c 28 22 61 75 74 6f 73 79 6e  _global("autosyn
6220: 63 22 29 20 29 20 64 62 5f 73 65 74 5f 69 6e 74  c") ) db_set_int
6230: 28 22 61 75 74 6f 73 79 6e 63 22 2c 20 31 2c 20  ("autosync", 1, 
6240: 30 29 3b 0a 20 20 69 66 28 20 21 64 62 5f 69 73  0);.  if( !db_is
6250: 5f 67 6c 6f 62 61 6c 28 22 6c 6f 63 61 6c 61 75  _global("localau
6260: 74 68 22 29 20 29 20 64 62 5f 73 65 74 5f 69 6e  th") ) db_set_in
6270: 74 28 22 6c 6f 63 61 6c 61 75 74 68 22 2c 20 30  t("localauth", 0
6280: 2c 20 30 29 3b 0a 20 20 64 62 5f 63 72 65 61 74  , 0);.  db_creat
6290: 65 5f 64 65 66 61 75 6c 74 5f 75 73 65 72 73 28  e_default_users(
62a0: 30 29 3b 0a 20 20 75 73 65 72 5f 73 65 6c 65 63  0);.  user_selec
62b0: 74 28 29 3b 0a 0a 20 20 69 66 20 28 6d 61 6b 65  t();..  if (make
62c0: 49 6e 69 74 69 61 6c 56 65 72 73 69 6f 6e 29 7b  InitialVersion){
62d0: 0a 20 20 20 20 69 6e 74 20 72 69 64 3b 0a 20 20  .    int rid;.  
62e0: 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 6d 61 6e    blob_zero(&man
62f0: 69 66 65 73 74 29 3b 0a 20 20 20 20 62 6c 6f 62  ifest);.    blob
6300: 5f 61 70 70 65 6e 64 66 28 26 6d 61 6e 69 66 65  _appendf(&manife
6310: 73 74 2c 20 22 43 20 69 6e 69 74 69 61 6c 5c 5c  st, "C initial\\
6320: 73 65 6d 70 74 79 5c 5c 73 63 68 65 63 6b 2d 69  sempty\\scheck-i
6330: 6e 5c 6e 22 29 3b 0a 20 20 20 20 7a 44 61 74 65  n\n");.    zDate
6340: 20 3d 20 64 62 5f 74 65 78 74 28 30 2c 20 22 53   = db_text(0, "S
6350: 45 4c 45 43 54 20 64 61 74 65 74 69 6d 65 28 27  ELECT datetime('
6360: 6e 6f 77 27 29 22 29 3b 0a 20 20 20 20 7a 44 61  now')");.    zDa
6370: 74 65 5b 31 30 5d 3d 27 54 27 3b 0a 20 20 20 20  te[10]='T';.    
6380: 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 6d 61  blob_appendf(&ma
6390: 6e 69 66 65 73 74 2c 20 22 44 20 25 73 5c 6e 22  nifest, "D %s\n"
63a0: 2c 20 7a 44 61 74 65 29 3b 0a 20 20 20 20 62 6c  , zDate);.    bl
63b0: 6f 62 5f 61 70 70 65 6e 64 66 28 26 6d 61 6e 69  ob_appendf(&mani
63c0: 66 65 73 74 2c 20 22 50 5c 6e 22 29 3b 0a 20 20  fest, "P\n");.  
63d0: 20 20 6d 64 35 73 75 6d 5f 69 6e 69 74 28 29 3b    md5sum_init();
63e0: 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64  .    blob_append
63f0: 66 28 26 6d 61 6e 69 66 65 73 74 2c 20 22 52 20  f(&manifest, "R 
6400: 25 73 5c 6e 22 2c 20 6d 64 35 73 75 6d 5f 66 69  %s\n", md5sum_fi
6410: 6e 69 73 68 28 30 29 29 3b 0a 20 20 20 20 62 6c  nish(0));.    bl
6420: 6f 62 5f 61 70 70 65 6e 64 66 28 26 6d 61 6e 69  ob_appendf(&mani
6430: 66 65 73 74 2c 20 22 54 20 2a 62 72 61 6e 63 68  fest, "T *branch
6440: 20 2a 20 74 72 75 6e 6b 5c 6e 22 29 3b 0a 20 20   * trunk\n");.  
6450: 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26    blob_appendf(&
6460: 6d 61 6e 69 66 65 73 74 2c 20 22 54 20 2a 73 79  manifest, "T *sy
6470: 6d 2d 74 72 75 6e 6b 20 2a 5c 6e 22 29 3b 0a 20  m-trunk *\n");. 
6480: 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28     blob_appendf(
6490: 26 6d 61 6e 69 66 65 73 74 2c 20 22 55 20 25 46  &manifest, "U %F
64a0: 5c 6e 22 2c 20 67 2e 7a 4c 6f 67 69 6e 29 3b 0a  \n", g.zLogin);.
64b0: 20 20 20 20 6d 64 35 73 75 6d 5f 62 6c 6f 62 28      md5sum_blob(
64c0: 26 6d 61 6e 69 66 65 73 74 2c 20 26 68 61 73 68  &manifest, &hash
64d0: 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65  );.    blob_appe
64e0: 6e 64 66 28 26 6d 61 6e 69 66 65 73 74 2c 20 22  ndf(&manifest, "
64f0: 5a 20 25 62 5c 6e 22 2c 20 26 68 61 73 68 29 3b  Z %b\n", &hash);
6500: 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28  .    blob_reset(
6510: 26 68 61 73 68 29 3b 0a 20 20 20 20 72 69 64 20  &hash);.    rid 
6520: 3d 20 63 6f 6e 74 65 6e 74 5f 70 75 74 28 26 6d  = content_put(&m
6530: 61 6e 69 66 65 73 74 2c 20 30 2c 20 30 29 3b 0a  anifest, 0, 0);.
6540: 20 20 20 20 6d 61 6e 69 66 65 73 74 5f 63 72 6f      manifest_cro
6550: 73 73 6c 69 6e 6b 28 72 69 64 2c 20 26 6d 61 6e  sslink(rid, &man
6560: 69 66 65 73 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ifest);.  }.}../
6570: 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 6e 65  *.** COMMAND: ne
6580: 77 0a 2a 2a 0a 2a 2a 20 55 73 61 67 65 3a 20 25  w.**.** Usage: %
6590: 66 6f 73 73 69 6c 20 6e 65 77 20 46 49 4c 45 4e  fossil new FILEN
65a0: 41 4d 45 0a 2a 2a 0a 2a 2a 20 43 72 65 61 74 65  AME.**.** Create
65b0: 20 61 20 72 65 70 6f 73 69 74 6f 72 79 20 66 6f   a repository fo
65c0: 72 20 61 20 6e 65 77 20 70 72 6f 6a 65 63 74 20  r a new project 
65d0: 69 6e 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65  in the file name
65e0: 64 20 46 49 4c 45 4e 41 4d 45 2e 0a 2a 2a 20 54  d FILENAME..** T
65f0: 68 69 73 20 63 6f 6d 6d 61 6e 64 20 69 73 20 64  his command is d
6600: 69 73 74 69 6e 63 74 20 66 72 6f 6d 20 22 63 6c  istinct from "cl
6610: 6f 6e 65 22 2e 20 20 54 68 65 20 22 63 6c 6f 6e  one".  The "clon
6620: 65 22 20 63 6f 6d 6d 61 6e 64 20 6d 61 6b 65 73  e" command makes
6630: 0a 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20 61 6e  .** a copy of an
6640: 20 65 78 69 73 74 69 6e 67 20 70 72 6f 6a 65 63   existing projec
6650: 74 2e 20 20 54 68 69 73 20 63 6f 6d 6d 61 6e 64  t.  This command
6660: 20 73 74 61 72 74 73 20 61 20 6e 65 77 20 70 72   starts a new pr
6670: 6f 6a 65 63 74 2e 0a 2a 2f 0a 76 6f 69 64 20 63  oject..*/.void c
6680: 72 65 61 74 65 5f 72 65 70 6f 73 69 74 6f 72 79  reate_repository
6690: 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 63 68  _cmd(void){.  ch
66a0: 61 72 20 2a 7a 50 61 73 73 77 6f 72 64 3b 0a 20  ar *zPassword;. 
66b0: 20 69 66 28 20 67 2e 61 72 67 63 21 3d 33 20 29   if( g.argc!=3 )
66c0: 7b 0a 20 20 20 20 75 73 61 67 65 28 22 52 45 50  {.    usage("REP
66d0: 4f 53 49 54 4f 52 59 2d 4e 41 4d 45 22 29 3b 0a  OSITORY-NAME");.
66e0: 20 20 7d 0a 20 20 64 62 5f 63 72 65 61 74 65 5f    }.  db_create_
66f0: 72 65 70 6f 73 69 74 6f 72 79 28 67 2e 61 72 67  repository(g.arg
6700: 76 5b 32 5d 29 3b 0a 20 20 64 62 5f 6f 70 65 6e  v[2]);.  db_open
6710: 5f 72 65 70 6f 73 69 74 6f 72 79 28 67 2e 61 72  _repository(g.ar
6720: 67 76 5b 32 5d 29 3b 0a 20 20 64 62 5f 6f 70 65  gv[2]);.  db_ope
6730: 6e 5f 63 6f 6e 66 69 67 28 29 3b 0a 20 20 64 62  n_config();.  db
6740: 5f 62 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69  _begin_transacti
6750: 6f 6e 28 29 3b 0a 20 20 64 62 5f 69 6e 69 74 69  on();.  db_initi
6760: 61 6c 5f 73 65 74 75 70 28 31 2c 20 31 29 3b 0a  al_setup(1, 1);.
6770: 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63    db_end_transac
6780: 74 69 6f 6e 28 30 29 3b 0a 20 20 70 72 69 6e 74  tion(0);.  print
6790: 66 28 22 70 72 6f 6a 65 63 74 2d 69 64 3a 20 25  f("project-id: %
67a0: 73 5c 6e 22 2c 20 64 62 5f 67 65 74 28 22 70 72  s\n", db_get("pr
67b0: 6f 6a 65 63 74 2d 63 6f 64 65 22 2c 20 30 29 29  oject-code", 0))
67c0: 3b 0a 20 20 70 72 69 6e 74 66 28 22 73 65 72 76  ;.  printf("serv
67d0: 65 72 2d 69 64 3a 20 20 25 73 5c 6e 22 2c 20 64  er-id:  %s\n", d
67e0: 62 5f 67 65 74 28 22 73 65 72 76 65 72 2d 63 6f  b_get("server-co
67f0: 64 65 22 2c 20 30 29 29 3b 0a 20 20 7a 50 61 73  de", 0));.  zPas
6800: 73 77 6f 72 64 20 3d 20 64 62 5f 74 65 78 74 28  sword = db_text(
6810: 30 2c 20 22 53 45 4c 45 43 54 20 70 77 20 46 52  0, "SELECT pw FR
6820: 4f 4d 20 75 73 65 72 20 57 48 45 52 45 20 6c 6f  OM user WHERE lo
6830: 67 69 6e 3d 25 51 22 2c 20 67 2e 7a 4c 6f 67 69  gin=%Q", g.zLogi
6840: 6e 29 3b 0a 20 20 70 72 69 6e 74 66 28 22 61 64  n);.  printf("ad
6850: 6d 69 6e 2d 75 73 65 72 3a 20 25 73 20 28 69 6e  min-user: %s (in
6860: 69 74 69 61 6c 20 70 61 73 73 77 6f 72 64 20 69  itial password i
6870: 73 20 5c 22 25 73 5c 22 29 5c 6e 22 2c 20 67 2e  s \"%s\")\n", g.
6880: 7a 4c 6f 67 69 6e 2c 20 7a 50 61 73 73 77 6f 72  zLogin, zPasswor
6890: 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c  d);.}../*.** SQL
68a0: 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 64   functions for d
68b0: 65 62 75 67 67 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  ebugging..**.** 
68c0: 54 68 65 20 70 72 69 6e 74 28 29 20 66 75 6e 63  The print() func
68d0: 74 69 6f 6e 20 77 72 69 74 65 73 20 69 74 73 20  tion writes its 
68e0: 61 72 67 75 6d 65 6e 74 73 20 6f 6e 20 73 74 64  arguments on std
68f0: 6f 75 74 2c 20 62 75 74 20 6f 6e 6c 79 0a 2a 2a  out, but only.**
6900: 20 69 66 20 74 68 65 20 2d 73 71 6c 70 72 69 6e   if the -sqlprin
6910: 74 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 6f  t command-line o
6920: 70 74 69 6f 6e 20 69 73 20 74 75 72 6e 65 64 20  ption is turned 
6930: 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  on..*/.static vo
6940: 69 64 20 64 62 5f 73 71 6c 5f 70 72 69 6e 74 28  id db_sql_print(
6950: 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
6960: 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
6970: 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
6980: 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
6990: 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66  ){.  int i;.  if
69a0: 28 20 67 2e 66 53 71 6c 50 72 69 6e 74 20 29 7b  ( g.fSqlPrint ){
69b0: 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
69c0: 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  argc; i++){.    
69d0: 20 20 63 68 61 72 20 63 20 3d 20 69 3d 3d 61 72    char c = i==ar
69e0: 67 63 2d 31 20 3f 20 27 5c 6e 27 20 3a 20 27 20  gc-1 ? '\n' : ' 
69f0: 27 3b 0a 20 20 20 20 20 20 70 72 69 6e 74 66 28  ';.      printf(
6a00: 22 25 73 25 63 22 2c 20 73 71 6c 69 74 65 33 5f  "%s%c", sqlite3_
6a10: 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
6a20: 69 5d 29 2c 20 63 29 3b 0a 20 20 20 20 7d 0a 20  i]), c);.    }. 
6a30: 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64   }.}.static void
6a40: 20 64 62 5f 73 71 6c 5f 74 72 61 63 65 28 76 6f   db_sql_trace(vo
6a50: 69 64 20 2a 6e 6f 74 55 73 65 64 2c 20 63 6f 6e  id *notUsed, con
6a60: 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 29 7b 0a  st char *zSql){.
6a70: 20 20 70 72 69 6e 74 66 28 22 25 73 5c 6e 22 2c    printf("%s\n",
6a80: 20 7a 53 71 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   zSql);.}../*.**
6a90: 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79   This is used by
6aa0: 20 74 68 65 20 5b 63 6f 6d 6d 69 74 5d 20 63 6f   the [commit] co
6ab0: 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  mmand..**.** Ret
6ac0: 75 72 6e 20 74 72 75 65 20 69 66 20 65 69 74 68  urn true if eith
6ad0: 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 29  er:.**.**     a)
6ae0: 20 47 6c 6f 62 61 6c 2e 61 43 6f 6d 6d 69 74 46   Global.aCommitF
6af0: 69 6c 65 20 69 73 20 4e 55 4c 4c 2c 20 6f 72 0a  ile is NULL, or.
6b00: 2a 2a 20 20 20 20 20 62 29 20 47 6c 6f 62 61 6c  **     b) Global
6b10: 2e 61 43 6f 6d 6d 69 74 46 69 6c 65 20 63 6f 6e  .aCommitFile con
6b20: 74 61 69 6e 73 20 74 68 65 20 69 6e 74 65 67 65  tains the intege
6b30: 72 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 61  r passed as an a
6b40: 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f  rgument..**.** O
6b50: 74 68 65 72 77 69 73 65 20 72 65 74 75 72 6e 20  therwise return 
6b60: 66 61 6c 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  false..*/.static
6b70: 20 76 6f 69 64 20 66 69 6c 65 5f 69 73 5f 73 65   void file_is_se
6b80: 6c 65 63 74 65 64 28 0a 20 20 73 71 6c 69 74 65  lected(.  sqlite
6b90: 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
6ba0: 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
6bb0: 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
6bc0: 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 61 73 73 65  **argv.){.  asse
6bd0: 72 74 28 61 72 67 63 3d 3d 31 29 3b 0a 20 20 69  rt(argc==1);.  i
6be0: 66 28 20 67 2e 61 43 6f 6d 6d 69 74 46 69 6c 65  f( g.aCommitFile
6bf0: 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 49 64 20   ){.    int iId 
6c00: 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
6c10: 69 6e 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  int(argv[0]);.  
6c20: 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 66 6f    int ii;.    fo
6c30: 72 28 69 69 3d 30 3b 20 67 2e 61 43 6f 6d 6d 69  r(ii=0; g.aCommi
6c40: 74 46 69 6c 65 5b 69 69 5d 3b 20 69 69 2b 2b 29  tFile[ii]; ii++)
6c50: 7b 0a 20 20 20 20 20 20 69 66 28 20 69 49 64 3d  {.      if( iId=
6c60: 3d 67 2e 61 43 6f 6d 6d 69 74 46 69 6c 65 5b 69  =g.aCommitFile[i
6c70: 69 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  i] ){.        sq
6c80: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
6c90: 28 63 6f 6e 74 65 78 74 2c 20 31 29 3b 0a 20 20  (context, 1);.  
6ca0: 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
6cb0: 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
6cc0: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
6cd0: 6e 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a  nt(context, 0);.
6ce0: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
6cf0: 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28  ite3_result_int(
6d00: 63 6f 6e 74 65 78 74 2c 20 31 29 3b 0a 20 20 7d  context, 1);.  }
6d10: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  .}../*.** Conver
6d20: 74 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69  t the input stri
6d30: 6e 67 20 69 6e 74 6f 20 61 6e 20 53 48 41 31 2e  ng into an SHA1.
6d40: 20 20 4d 61 6b 65 20 61 20 6e 6f 74 61 74 69 6f    Make a notatio
6d50: 6e 20 69 6e 20 74 68 65 0a 2a 2a 20 43 4f 4e 43  n in the.** CONC
6d60: 45 41 4c 45 44 20 74 61 62 6c 65 20 73 6f 20 74  EALED table so t
6d70: 68 61 74 20 74 68 65 20 68 61 73 68 20 63 61 6e  hat the hash can
6d80: 20 62 65 20 75 6e 64 6f 20 75 73 69 6e 67 20 74   be undo using t
6d90: 68 65 20 64 62 5f 72 65 76 65 61 6c 28 29 0a 2a  he db_reveal().*
6da0: 2a 20 66 75 6e 63 74 69 6f 6e 20 61 74 20 73 6f  * function at so
6db0: 6d 65 20 6c 61 74 65 72 20 74 69 6d 65 2e 0a 2a  me later time..*
6dc0: 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 72  *.** The value r
6dd0: 65 74 75 72 6e 65 64 20 69 73 20 73 74 6f 72 65  eturned is store
6de0: 64 20 69 6e 20 73 74 61 74 69 63 20 73 70 61 63  d in static spac
6df0: 65 20 61 6e 64 20 77 69 6c 6c 20 62 65 20 6f 76  e and will be ov
6e00: 65 72 77 72 69 74 74 65 6e 0a 2a 2a 20 6f 6e 20  erwritten.** on 
6e10: 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73  subsequent calls
6e20: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 43 6f 6e 74  ..**.** If zCont
6e30: 65 6e 74 20 69 73 20 61 6c 72 65 61 64 79 20 61  ent is already a
6e40: 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 53 48 41   well-formed SHA
6e50: 31 20 68 61 73 68 2c 20 74 68 65 6e 20 72 65 74  1 hash, then ret
6e60: 75 72 6e 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66  urn a copy.** of
6e70: 20 74 68 61 74 20 68 61 73 68 2c 20 6e 6f 74 20   that hash, not 
6e80: 61 20 68 61 73 68 20 6f 66 20 74 68 65 20 68 61  a hash of the ha
6e90: 73 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 4f  sh..**.** The CO
6ea0: 4e 43 45 41 4c 45 44 20 74 61 62 6c 65 20 69 73  NCEALED table is
6eb0: 20 6d 65 61 6e 74 20 74 6f 20 6f 62 73 63 75 72   meant to obscur
6ec0: 65 20 65 6d 61 69 6c 20 61 64 64 72 65 73 73 65  e email addresse
6ed0: 73 2e 20 20 45 76 65 72 79 20 76 61 6c 69 64 0a  s.  Every valid.
6ee0: 2a 2a 20 65 6d 61 69 6c 20 61 64 64 72 65 73 73  ** email address
6ef0: 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 20   will contain a 
6f00: 22 40 22 20 63 68 61 72 61 63 74 65 72 20 61 6e  "@" character an
6f10: 64 20 22 40 22 20 69 73 20 6e 6f 74 20 76 61 6c  d "@" is not val
6f20: 69 64 20 77 69 74 68 69 6e 0a 2a 2a 20 61 6e 20  id within.** an 
6f30: 53 48 41 31 20 68 61 73 68 20 73 6f 20 74 68 65  SHA1 hash so the
6f40: 72 65 20 69 73 20 6e 6f 20 63 68 61 6e 63 65 20  re is no chance 
6f50: 74 68 61 74 20 61 20 76 61 6c 69 64 20 65 6d 61  that a valid ema
6f60: 69 6c 20 61 64 64 72 65 73 73 20 77 69 6c 6c 20  il address will 
6f70: 67 6f 0a 2a 2a 20 75 6e 63 6f 6e 63 65 61 6c 65  go.** unconceale
6f80: 64 2e 0a 2a 2f 0a 63 68 61 72 20 2a 64 62 5f 63  d..*/.char *db_c
6f90: 6f 6e 63 65 61 6c 28 63 6f 6e 73 74 20 63 68 61  onceal(const cha
6fa0: 72 20 2a 7a 43 6f 6e 74 65 6e 74 2c 20 69 6e 74  r *zContent, int
6fb0: 20 6e 29 7b 0a 20 20 73 74 61 74 69 63 20 63 68   n){.  static ch
6fc0: 61 72 20 7a 48 61 73 68 5b 34 32 5d 3b 0a 20 20  ar zHash[42];.  
6fd0: 42 6c 6f 62 20 6f 75 74 3b 0a 20 20 69 66 28 20  Blob out;.  if( 
6fe0: 6e 3d 3d 34 30 20 26 26 20 76 61 6c 69 64 61 74  n==40 && validat
6ff0: 65 31 36 28 7a 43 6f 6e 74 65 6e 74 2c 20 6e 29  e16(zContent, n)
7000: 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a   ){.    memcpy(z
7010: 48 61 73 68 2c 20 7a 43 6f 6e 74 65 6e 74 2c 20  Hash, zContent, 
7020: 6e 29 3b 0a 20 20 20 20 7a 48 61 73 68 5b 6e 5d  n);.    zHash[n]
7030: 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
7040: 20 20 20 73 68 61 31 73 75 6d 5f 73 74 65 70 5f     sha1sum_step_
7050: 74 65 78 74 28 7a 43 6f 6e 74 65 6e 74 2c 20 6e  text(zContent, n
7060: 29 3b 0a 20 20 20 20 73 68 61 31 73 75 6d 5f 66  );.    sha1sum_f
7070: 69 6e 69 73 68 28 26 6f 75 74 29 3b 0a 20 20 20  inish(&out);.   
7080: 20 73 74 72 63 70 79 28 7a 48 61 73 68 2c 20 62   strcpy(zHash, b
7090: 6c 6f 62 5f 73 74 72 28 26 6f 75 74 29 29 3b 0a  lob_str(&out));.
70a0: 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26      blob_reset(&
70b0: 6f 75 74 29 3b 0a 20 20 20 20 64 62 5f 6d 75 6c  out);.    db_mul
70c0: 74 69 5f 65 78 65 63 28 0a 20 20 20 20 20 20 20  ti_exec(.       
70d0: 22 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52  "INSERT OR IGNOR
70e0: 45 20 49 4e 54 4f 20 63 6f 6e 63 65 61 6c 65 64  E INTO concealed
70f0: 20 56 41 4c 55 45 53 28 25 51 2c 25 23 51 29 22   VALUES(%Q,%#Q)"
7100: 2c 0a 20 20 20 20 20 20 20 7a 48 61 73 68 2c 20  ,.       zHash, 
7110: 6e 2c 20 7a 43 6f 6e 74 65 6e 74 0a 20 20 20 20  n, zContent.    
7120: 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
7130: 7a 48 61 73 68 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  zHash;.}../*.** 
7140: 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f 6f 6b 20  Attempt to look 
7150: 75 70 20 74 68 65 20 69 6e 70 75 74 20 69 6e 20  up the input in 
7160: 74 68 65 20 43 4f 4e 43 45 41 4c 45 44 20 74 61  the CONCEALED ta
7170: 62 6c 65 2e 20 20 49 66 20 66 6f 75 6e 64 2c 0a  ble.  If found,.
7180: 2a 2a 20 61 6e 64 20 69 66 20 74 68 65 20 6f 6b  ** and if the ok
7190: 52 64 41 64 64 72 20 70 65 72 6d 69 73 73 69 6f  RdAddr permissio
71a0: 6e 20 69 73 20 65 6e 61 62 6c 65 64 20 74 68 65  n is enabled the
71b0: 6e 20 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20  n return the.** 
71c0: 6f 72 69 67 69 6e 61 6c 20 76 61 6c 75 65 20 66  original value f
71d0: 6f 72 20 77 68 69 63 68 20 74 68 65 20 69 6e 70  or which the inp
71e0: 75 74 20 69 73 20 61 20 68 61 73 68 2e 20 20 49  ut is a hash.  I
71f0: 66 20 6f 6b 52 64 41 64 64 72 20 69 73 0a 2a 2a  f okRdAddr is.**
7200: 20 66 61 6c 73 65 20 6f 72 20 69 66 20 74 68 65   false or if the
7210: 20 6c 6f 6f 6b 75 70 20 66 61 69 6c 73 2c 20 72   lookup fails, r
7220: 65 74 75 72 6e 20 74 68 65 20 6f 72 69 67 69 6e  eturn the origin
7230: 61 6c 20 73 74 72 69 6e 67 20 63 6f 6e 74 65 6e  al string conten
7240: 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 65 69 74 68  t..**.** In eith
7250: 65 72 20 63 61 73 65 2c 20 74 68 65 20 73 74 72  er case, the str
7260: 69 6e 67 20 72 65 74 75 72 6e 65 64 20 69 73 20  ing returned is 
7270: 73 74 6f 72 65 64 20 69 6e 20 73 70 61 63 65 20  stored in space 
7280: 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d  obtained.** from
7290: 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 73 68 6f 75   malloc and shou
72a0: 6c 64 20 62 65 20 66 72 65 65 64 20 62 79 20 74  ld be freed by t
72b0: 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
72c0: 69 6f 6e 2e 0a 2a 2f 0a 63 68 61 72 20 2a 64 62  ion..*/.char *db
72d0: 5f 72 65 76 65 61 6c 28 63 6f 6e 73 74 20 63 68  _reveal(const ch
72e0: 61 72 20 2a 7a 4b 65 79 29 7b 0a 20 20 63 68 61  ar *zKey){.  cha
72f0: 72 20 2a 7a 4f 75 74 3b 0a 20 20 69 66 28 20 67  r *zOut;.  if( g
7300: 2e 6f 6b 52 64 41 64 64 72 20 29 7b 0a 20 20 20  .okRdAddr ){.   
7310: 20 7a 4f 75 74 20 3d 20 64 62 5f 74 65 78 74 28   zOut = db_text(
7320: 30 2c 20 22 53 45 4c 45 43 54 20 63 6f 6e 74 65  0, "SELECT conte
7330: 6e 74 20 46 52 4f 4d 20 63 6f 6e 63 65 61 6c 65  nt FROM conceale
7340: 64 20 57 48 45 52 45 20 68 61 73 68 3d 25 51 22  d WHERE hash=%Q"
7350: 2c 20 7a 4b 65 79 29 3b 0a 20 20 7d 65 6c 73 65  , zKey);.  }else
7360: 7b 0a 20 20 20 20 7a 4f 75 74 20 3d 20 30 3b 0a  {.    zOut = 0;.
7370: 20 20 7d 0a 20 20 69 66 28 20 7a 4f 75 74 3d 3d    }.  if( zOut==
7380: 30 20 29 7b 0a 20 20 20 20 7a 4f 75 74 20 3d 20  0 ){.    zOut = 
7390: 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 20 7a 4b  mprintf("%s", zK
73a0: 65 79 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ey);.  }.  retur
73b0: 6e 20 7a 4f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n zOut;.}../*.**
73c0: 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   This function r
73d0: 65 67 69 73 74 65 72 73 20 61 75 78 69 6c 69 61  egisters auxilia
73e0: 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 77 68 65  ry functions whe
73f0: 6e 20 74 68 65 20 53 51 4c 69 74 65 0a 2a 2a 20  n the SQLite.** 
7400: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
7410: 69 6f 6e 20 69 73 20 66 69 72 73 74 20 65 73 74  ion is first est
7420: 61 62 6c 69 73 68 65 64 2e 0a 2a 2f 0a 4c 4f 43  ablished..*/.LOC
7430: 41 4c 20 76 6f 69 64 20 64 62 5f 63 6f 6e 6e 65  AL void db_conne
7440: 63 74 69 6f 6e 5f 69 6e 69 74 28 76 6f 69 64 29  ction_init(void)
7450: 7b 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 6f  {.  static int o
7460: 6e 63 65 20 3d 20 31 3b 0a 20 20 69 66 28 20 6f  nce = 1;.  if( o
7470: 6e 63 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  nce ){.    sqlit
7480: 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
7490: 6f 6e 28 67 2e 64 62 2c 20 22 70 72 69 6e 74 22  on(g.db, "print"
74a0: 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46  , -1, SQLITE_UTF
74b0: 38 2c 20 30 2c 64 62 5f 73 71 6c 5f 70 72 69 6e  8, 0,db_sql_prin
74c0: 74 2c 30 2c 30 29 3b 0a 20 20 20 20 73 71 6c 69  t,0,0);.    sqli
74d0: 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
74e0: 69 6f 6e 28 0a 20 20 20 20 20 20 67 2e 64 62 2c  ion(.      g.db,
74f0: 20 22 66 69 6c 65 5f 69 73 5f 73 65 6c 65 63 74   "file_is_select
7500: 65 64 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 55  ed", 1, SQLITE_U
7510: 54 46 38 2c 20 30 2c 20 66 69 6c 65 5f 69 73 5f  TF8, 0, file_is_
7520: 73 65 6c 65 63 74 65 64 2c 30 2c 30 0a 20 20 20  selected,0,0.   
7530: 20 29 3b 0a 20 20 20 20 69 66 28 20 67 2e 66 53   );.    if( g.fS
7540: 71 6c 54 72 61 63 65 20 29 7b 0a 20 20 20 20 20  qlTrace ){.     
7550: 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 67   sqlite3_trace(g
7560: 2e 64 62 2c 20 64 62 5f 73 71 6c 5f 74 72 61 63  .db, db_sql_trac
7570: 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20  e, 0);.    }.   
7580: 20 6f 6e 63 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d   once = 0;.  }.}
7590: 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
75a0: 72 75 65 20 69 66 20 74 68 65 20 73 74 72 69 6e  rue if the strin
75b0: 67 20 7a 56 61 6c 20 72 65 70 72 65 73 65 6e 74  g zVal represent
75c0: 73 20 22 74 72 75 65 22 20 28 6f 72 20 22 66 61  s "true" (or "fa
75d0: 6c 73 65 22 29 2e 0a 2a 2f 0a 69 6e 74 20 69 73  lse")..*/.int is
75e0: 5f 74 72 75 74 68 28 63 6f 6e 73 74 20 63 68 61  _truth(const cha
75f0: 72 20 2a 7a 56 61 6c 29 7b 0a 20 20 73 74 61 74  r *zVal){.  stat
7600: 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61  ic const char *a
7610: 7a 4f 6e 5b 5d 20 3d 20 7b 20 22 6f 6e 22 2c 20  zOn[] = { "on", 
7620: 22 79 65 73 22 2c 20 22 74 72 75 65 22 2c 20 22  "yes", "true", "
7630: 31 22 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  1" };.  int i;. 
7640: 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65   for(i=0; i<size
7650: 6f 66 28 61 7a 4f 6e 29 2f 73 69 7a 65 6f 66 28  of(azOn)/sizeof(
7660: 61 7a 4f 6e 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a  azOn[0]); i++){.
7670: 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a      if( strcmp(z
7680: 56 61 6c 2c 61 7a 4f 6e 5b 69 5d 29 3d 3d 30 20  Val,azOn[i])==0 
7690: 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a  ) return 1;.  }.
76a0: 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 69 6e    return 0;.}.in
76b0: 74 20 69 73 5f 66 61 6c 73 65 28 63 6f 6e 73 74  t is_false(const
76c0: 20 63 68 61 72 20 2a 7a 56 61 6c 29 7b 0a 20 20   char *zVal){.  
76d0: 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
76e0: 72 20 2a 61 7a 4f 66 66 5b 5d 20 3d 20 7b 20 22  r *azOff[] = { "
76f0: 6f 66 66 22 2c 20 22 6e 6f 22 2c 20 22 66 61 6c  off", "no", "fal
7700: 73 65 22 2c 20 22 30 22 20 7d 3b 0a 20 20 69 6e  se", "0" };.  in
7710: 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  t i;.  for(i=0; 
7720: 69 3c 73 69 7a 65 6f 66 28 61 7a 4f 66 66 29 2f  i<sizeof(azOff)/
7730: 73 69 7a 65 6f 66 28 61 7a 4f 66 66 5b 30 5d 29  sizeof(azOff[0])
7740: 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
7750: 73 74 72 63 6d 70 28 7a 56 61 6c 2c 61 7a 4f 66  strcmp(zVal,azOf
7760: 66 5b 69 5d 29 3d 3d 30 20 29 20 72 65 74 75 72  f[i])==0 ) retur
7770: 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  n 1;.  }.  retur
7780: 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  n 0;.}../*.** Ge
7790: 74 20 61 6e 64 20 73 65 74 20 76 61 6c 75 65 73  t and set values
77a0: 20 66 72 6f 6d 20 74 68 65 20 43 4f 4e 46 49 47   from the CONFIG
77b0: 2c 20 47 4c 4f 42 41 4c 5f 43 4f 4e 46 49 47 20  , GLOBAL_CONFIG 
77c0: 61 6e 64 20 56 56 41 52 20 74 61 62 6c 65 20 69  and VVAR table i
77d0: 6e 20 74 68 65 0a 2a 2a 20 72 65 70 6f 73 69 74  n the.** reposit
77e0: 6f 72 79 20 61 6e 64 20 6c 6f 63 61 6c 20 64 61  ory and local da
77f0: 74 61 62 61 73 65 73 2e 0a 2a 2f 0a 63 68 61 72  tabases..*/.char
7800: 20 2a 64 62 5f 67 65 74 28 63 6f 6e 73 74 20 63   *db_get(const c
7810: 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 68 61 72  har *zName, char
7820: 20 2a 7a 44 65 66 61 75 6c 74 29 7b 0a 20 20 63   *zDefault){.  c
7830: 68 61 72 20 2a 7a 20 3d 20 30 3b 0a 20 20 69 66  har *z = 0;.  if
7840: 28 20 67 2e 72 65 70 6f 73 69 74 6f 72 79 4f 70  ( g.repositoryOp
7850: 65 6e 20 29 7b 0a 20 20 20 20 7a 20 3d 20 64 62  en ){.    z = db
7860: 5f 74 65 78 74 28 30 2c 20 22 53 45 4c 45 43 54  _text(0, "SELECT
7870: 20 76 61 6c 75 65 20 46 52 4f 4d 20 63 6f 6e 66   value FROM conf
7880: 69 67 20 57 48 45 52 45 20 6e 61 6d 65 3d 25 51  ig WHERE name=%Q
7890: 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20  ", zName);.  }. 
78a0: 20 69 66 28 20 7a 3d 3d 30 20 26 26 20 67 2e 63   if( z==0 && g.c
78b0: 6f 6e 66 69 67 4f 70 65 6e 20 29 7b 0a 20 20 20  onfigOpen ){.   
78c0: 20 7a 20 3d 20 64 62 5f 74 65 78 74 28 30 2c 20   z = db_text(0, 
78d0: 22 53 45 4c 45 43 54 20 76 61 6c 75 65 20 46 52  "SELECT value FR
78e0: 4f 4d 20 67 6c 6f 62 61 6c 5f 63 6f 6e 66 69 67  OM global_config
78f0: 20 57 48 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c   WHERE name=%Q",
7900: 20 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 69   zName);.  }.  i
7910: 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 7a  f( z==0 ){.    z
7920: 20 3d 20 7a 44 65 66 61 75 6c 74 3b 0a 20 20 7d   = zDefault;.  }
7930: 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 76  .  return z;.}.v
7940: 6f 69 64 20 64 62 5f 73 65 74 28 63 6f 6e 73 74  oid db_set(const
7950: 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f   char *zName, co
7960: 6e 73 74 20 63 68 61 72 20 2a 7a 56 61 6c 75 65  nst char *zValue
7970: 2c 20 69 6e 74 20 67 6c 6f 62 61 6c 46 6c 61 67  , int globalFlag
7980: 29 7b 0a 20 20 64 62 5f 62 65 67 69 6e 5f 74 72  ){.  db_begin_tr
7990: 61 6e 73 61 63 74 69 6f 6e 28 29 3b 0a 20 20 64  ansaction();.  d
79a0: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 52 45  b_multi_exec("RE
79b0: 50 4c 41 43 45 20 49 4e 54 4f 20 25 73 63 6f 6e  PLACE INTO %scon
79c0: 66 69 67 28 6e 61 6d 65 2c 76 61 6c 75 65 29 20  fig(name,value) 
79d0: 56 41 4c 55 45 53 28 25 51 2c 25 51 29 22 2c 0a  VALUES(%Q,%Q)",.
79e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
79f0: 20 67 6c 6f 62 61 6c 46 6c 61 67 20 3f 20 22 67   globalFlag ? "g
7a00: 6c 6f 62 61 6c 5f 22 20 3a 20 22 22 2c 20 7a 4e  lobal_" : "", zN
7a10: 61 6d 65 2c 20 7a 56 61 6c 75 65 29 3b 0a 20 20  ame, zValue);.  
7a20: 69 66 28 20 67 6c 6f 62 61 6c 46 6c 61 67 20 26  if( globalFlag &
7a30: 26 20 67 2e 72 65 70 6f 73 69 74 6f 72 79 4f 70  & g.repositoryOp
7a40: 65 6e 20 29 7b 0a 20 20 20 20 64 62 5f 6d 75 6c  en ){.    db_mul
7a50: 74 69 5f 65 78 65 63 28 22 44 45 4c 45 54 45 20  ti_exec("DELETE 
7a60: 46 52 4f 4d 20 63 6f 6e 66 69 67 20 57 48 45 52  FROM config WHER
7a70: 45 20 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d  E name=%Q", zNam
7a80: 65 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 65 6e 64  e);.  }.  db_end
7a90: 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b  _transaction(0);
7aa0: 0a 7d 0a 76 6f 69 64 20 64 62 5f 75 6e 73 65 74  .}.void db_unset
7ab0: 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61  (const char *zNa
7ac0: 6d 65 2c 20 69 6e 74 20 67 6c 6f 62 61 6c 46 6c  me, int globalFl
7ad0: 61 67 29 7b 0a 20 20 64 62 5f 62 65 67 69 6e 5f  ag){.  db_begin_
7ae0: 74 72 61 6e 73 61 63 74 69 6f 6e 28 29 3b 0a 20  transaction();. 
7af0: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22   db_multi_exec("
7b00: 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 73 63 6f  DELETE FROM %sco
7b10: 6e 66 69 67 20 57 48 45 52 45 20 6e 61 6d 65 3d  nfig WHERE name=
7b20: 25 51 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  %Q",.           
7b30: 20 20 20 20 20 20 67 6c 6f 62 61 6c 46 6c 61 67        globalFlag
7b40: 20 3f 20 22 67 6c 6f 62 61 6c 5f 22 20 3a 20 22   ? "global_" : "
7b50: 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28  ", zName);.  if(
7b60: 20 67 6c 6f 62 61 6c 46 6c 61 67 20 26 26 20 67   globalFlag && g
7b70: 2e 72 65 70 6f 73 69 74 6f 72 79 4f 70 65 6e 20  .repositoryOpen 
7b80: 29 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f  ){.    db_multi_
7b90: 65 78 65 63 28 22 44 45 4c 45 54 45 20 46 52 4f  exec("DELETE FRO
7ba0: 4d 20 63 6f 6e 66 69 67 20 57 48 45 52 45 20 6e  M config WHERE n
7bb0: 61 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d 65 29 3b  ame=%Q", zName);
7bc0: 0a 20 20 7d 0a 20 20 64 62 5f 65 6e 64 5f 74 72  .  }.  db_end_tr
7bd0: 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b 0a 7d 0a  ansaction(0);.}.
7be0: 69 6e 74 20 64 62 5f 69 73 5f 67 6c 6f 62 61 6c  int db_is_global
7bf0: 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61  (const char *zNa
7c00: 6d 65 29 7b 0a 20 20 69 66 28 20 67 2e 63 6f 6e  me){.  if( g.con
7c10: 66 69 67 4f 70 65 6e 20 29 7b 0a 20 20 20 20 72  figOpen ){.    r
7c20: 65 74 75 72 6e 20 64 62 5f 65 78 69 73 74 73 28  eturn db_exists(
7c30: 22 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 67  "SELECT 1 FROM g
7c40: 6c 6f 62 61 6c 5f 63 6f 6e 66 69 67 20 57 48 45  lobal_config WHE
7c50: 52 45 20 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e 61  RE name=%Q", zNa
7c60: 6d 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  me);.  }else{.  
7c70: 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
7c80: 7d 0a 69 6e 74 20 64 62 5f 67 65 74 5f 69 6e 74  }.int db_get_int
7c90: 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61  (const char *zNa
7ca0: 6d 65 2c 20 69 6e 74 20 64 66 6c 74 29 7b 0a 20  me, int dflt){. 
7cb0: 20 69 6e 74 20 76 20 3d 20 64 66 6c 74 3b 0a 20   int v = dflt;. 
7cc0: 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 67   int rc;.  if( g
7cd0: 2e 72 65 70 6f 73 69 74 6f 72 79 4f 70 65 6e 20  .repositoryOpen 
7ce0: 29 7b 0a 20 20 20 20 53 74 6d 74 20 71 3b 0a 20  ){.    Stmt q;. 
7cf0: 20 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 71     db_prepare(&q
7d00: 2c 20 22 53 45 4c 45 43 54 20 76 61 6c 75 65 20  , "SELECT value 
7d10: 46 52 4f 4d 20 63 6f 6e 66 69 67 20 57 48 45 52  FROM config WHER
7d20: 45 20 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d  E name=%Q", zNam
7d30: 65 29 3b 0a 20 20 20 20 72 63 20 3d 20 64 62 5f  e);.    rc = db_
7d40: 73 74 65 70 28 26 71 29 3b 0a 20 20 20 20 69 66  step(&q);.    if
7d50: 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc==SQLITE_ROW
7d60: 20 29 7b 0a 20 20 20 20 20 20 76 20 3d 20 64 62   ){.      v = db
7d70: 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20  _column_int(&q, 
7d80: 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62  0);.    }.    db
7d90: 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20  _finalize(&q);. 
7da0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d   }else{.    rc =
7db0: 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20   SQLITE_DONE;.  
7dc0: 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  }.  if( rc==SQLI
7dd0: 54 45 5f 44 4f 4e 45 20 26 26 20 67 2e 63 6f 6e  TE_DONE && g.con
7de0: 66 69 67 4f 70 65 6e 20 29 7b 0a 20 20 20 20 76  figOpen ){.    v
7df0: 20 3d 20 64 62 5f 69 6e 74 28 64 66 6c 74 2c 20   = db_int(dflt, 
7e00: 22 53 45 4c 45 43 54 20 76 61 6c 75 65 20 46 52  "SELECT value FR
7e10: 4f 4d 20 67 6c 6f 62 61 6c 5f 63 6f 6e 66 69 67  OM global_config
7e20: 20 57 48 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c   WHERE name=%Q",
7e30: 20 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 72   zName);.  }.  r
7e40: 65 74 75 72 6e 20 76 3b 0a 7d 0a 76 6f 69 64 20  eturn v;.}.void 
7e50: 64 62 5f 73 65 74 5f 69 6e 74 28 63 6f 6e 73 74  db_set_int(const
7e60: 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e   char *zName, in
7e70: 74 20 76 61 6c 75 65 2c 20 69 6e 74 20 67 6c 6f  t value, int glo
7e80: 62 61 6c 46 6c 61 67 29 7b 0a 20 20 64 62 5f 62  balFlag){.  db_b
7e90: 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e  egin_transaction
7ea0: 28 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65  ();.  db_multi_e
7eb0: 78 65 63 28 22 52 45 50 4c 41 43 45 20 49 4e 54  xec("REPLACE INT
7ec0: 4f 20 25 73 63 6f 6e 66 69 67 28 6e 61 6d 65 2c  O %sconfig(name,
7ed0: 76 61 6c 75 65 29 20 56 41 4c 55 45 53 28 25 51  value) VALUES(%Q
7ee0: 2c 25 64 29 22 2c 0a 20 20 20 20 20 20 20 20 20  ,%d)",.         
7ef0: 20 20 20 20 20 20 20 67 6c 6f 62 61 6c 46 6c 61         globalFla
7f00: 67 20 3f 20 22 67 6c 6f 62 61 6c 5f 22 20 3a 20  g ? "global_" : 
7f10: 22 22 2c 20 7a 4e 61 6d 65 2c 20 76 61 6c 75 65  "", zName, value
7f20: 29 3b 0a 20 20 69 66 28 20 67 6c 6f 62 61 6c 46  );.  if( globalF
7f30: 6c 61 67 20 26 26 20 67 2e 72 65 70 6f 73 69 74  lag && g.reposit
7f40: 6f 72 79 4f 70 65 6e 20 29 7b 0a 20 20 20 20 64  oryOpen ){.    d
7f50: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 44 45  b_multi_exec("DE
7f60: 4c 45 54 45 20 46 52 4f 4d 20 63 6f 6e 66 69 67  LETE FROM config
7f70: 20 57 48 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c   WHERE name=%Q",
7f80: 20 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 64   zName);.  }.  d
7f90: 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  b_end_transactio
7fa0: 6e 28 30 29 3b 0a 7d 0a 69 6e 74 20 64 62 5f 67  n(0);.}.int db_g
7fb0: 65 74 5f 62 6f 6f 6c 65 61 6e 28 63 6f 6e 73 74  et_boolean(const
7fc0: 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e   char *zName, in
7fd0: 74 20 64 66 6c 74 29 7b 0a 20 20 63 68 61 72 20  t dflt){.  char 
7fe0: 2a 7a 56 61 6c 20 3d 20 64 62 5f 67 65 74 28 7a  *zVal = db_get(z
7ff0: 4e 61 6d 65 2c 20 64 66 6c 74 20 3f 20 22 6f 6e  Name, dflt ? "on
8000: 22 20 3a 20 22 6f 66 66 22 29 3b 0a 20 20 69 66  " : "off");.  if
8010: 28 20 69 73 5f 74 72 75 74 68 28 7a 56 61 6c 29  ( is_truth(zVal)
8020: 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 69   ) return 1;.  i
8030: 66 28 20 69 73 5f 66 61 6c 73 65 28 7a 56 61 6c  f( is_false(zVal
8040: 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  ) ) return 0;.  
8050: 72 65 74 75 72 6e 20 64 66 6c 74 3b 0a 7d 0a 63  return dflt;.}.c
8060: 68 61 72 20 2a 64 62 5f 6c 67 65 74 28 63 6f 6e  har *db_lget(con
8070: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
8080: 63 68 61 72 20 2a 7a 44 65 66 61 75 6c 74 29 7b  char *zDefault){
8090: 0a 20 20 72 65 74 75 72 6e 20 64 62 5f 74 65 78  .  return db_tex
80a0: 74 28 28 63 68 61 72 2a 29 7a 44 65 66 61 75 6c  t((char*)zDefaul
80b0: 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
80c0: 20 20 20 20 22 53 45 4c 45 43 54 20 76 61 6c 75      "SELECT valu
80d0: 65 20 46 52 4f 4d 20 76 76 61 72 20 57 48 45 52  e FROM vvar WHER
80e0: 45 20 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d  E name=%Q", zNam
80f0: 65 29 3b 0a 7d 0a 76 6f 69 64 20 64 62 5f 6c 73  e);.}.void db_ls
8100: 65 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  et(const char *z
8110: 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  Name, const char
8120: 20 2a 7a 56 61 6c 75 65 29 7b 0a 20 20 64 62 5f   *zValue){.  db_
8130: 6d 75 6c 74 69 5f 65 78 65 63 28 22 52 45 50 4c  multi_exec("REPL
8140: 41 43 45 20 49 4e 54 4f 20 76 76 61 72 28 6e 61  ACE INTO vvar(na
8150: 6d 65 2c 76 61 6c 75 65 29 20 56 41 4c 55 45 53  me,value) VALUES
8160: 28 25 51 2c 25 51 29 22 2c 20 7a 4e 61 6d 65 2c  (%Q,%Q)", zName,
8170: 20 7a 56 61 6c 75 65 29 3b 0a 7d 0a 69 6e 74 20   zValue);.}.int 
8180: 64 62 5f 6c 67 65 74 5f 69 6e 74 28 63 6f 6e 73  db_lget_int(cons
8190: 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69  t char *zName, i
81a0: 6e 74 20 64 66 6c 74 29 7b 0a 20 20 72 65 74 75  nt dflt){.  retu
81b0: 72 6e 20 64 62 5f 69 6e 74 28 64 66 6c 74 2c 20  rn db_int(dflt, 
81c0: 22 53 45 4c 45 43 54 20 76 61 6c 75 65 20 46 52  "SELECT value FR
81d0: 4f 4d 20 76 76 61 72 20 57 48 45 52 45 20 6e 61  OM vvar WHERE na
81e0: 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d 65 29 3b 0a  me=%Q", zName);.
81f0: 7d 0a 76 6f 69 64 20 64 62 5f 6c 73 65 74 5f 69  }.void db_lset_i
8200: 6e 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  nt(const char *z
8210: 4e 61 6d 65 2c 20 69 6e 74 20 76 61 6c 75 65 29  Name, int value)
8220: 7b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65  {.  db_multi_exe
8230: 63 28 22 52 45 50 4c 41 43 45 20 49 4e 54 4f 20  c("REPLACE INTO 
8240: 76 76 61 72 28 6e 61 6d 65 2c 76 61 6c 75 65 29  vvar(name,value)
8250: 20 56 41 4c 55 45 53 28 25 51 2c 25 64 29 22 2c   VALUES(%Q,%d)",
8260: 20 7a 4e 61 6d 65 2c 20 76 61 6c 75 65 29 3b 0a   zName, value);.
8270: 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 20  }../*.** Record 
8280: 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 6c 6f  the name of a lo
8290: 63 61 6c 20 72 65 70 6f 73 69 74 6f 72 79 20 69  cal repository i
82a0: 6e 20 74 68 65 20 67 6c 6f 62 61 6c 5f 63 6f 6e  n the global_con
82b0: 66 69 67 28 29 20 64 61 74 61 62 61 73 65 2e 0a  fig() database..
82c0: 2a 2a 20 54 68 65 20 72 65 70 6f 73 74 69 72 6f  ** The repostiro
82d0: 79 20 66 69 6c 65 6e 61 6d 65 20 25 73 20 69 73  y filename %s is
82e0: 20 72 65 63 6f 72 64 65 64 20 61 73 20 61 6e 20   recorded as an 
82f0: 65 6e 74 72 79 20 77 69 74 68 20 61 20 22 6e 61  entry with a "na
8300: 6d 65 22 20 66 69 65 6c 64 0a 2a 2a 20 6f 66 20  me" field.** of 
8310: 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f  the following fo
8320: 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  rm:.**.**       
8330: 72 65 70 6f 3a 25 73 0a 2a 2a 0a 2a 2a 20 54 68  repo:%s.**.** Th
8340: 65 20 76 61 6c 75 65 20 66 69 65 6c 64 20 69 73  e value field is
8350: 20 73 65 74 20 74 6f 20 31 2e 0a 2a 2f 0a 76 6f   set to 1..*/.vo
8360: 69 64 20 64 62 5f 72 65 63 6f 72 64 5f 72 65 70  id db_record_rep
8370: 6f 73 69 74 6f 72 79 5f 66 69 6c 65 6e 61 6d 65  ository_filename
8380: 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61  (const char *zNa
8390: 6d 65 29 7b 0a 20 20 42 6c 6f 62 20 66 75 6c 6c  me){.  Blob full
83a0: 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30  ;.  if( zName==0
83b0: 20 29 7b 0a 20 20 20 20 69 66 28 20 21 67 2e 6c   ){.    if( !g.l
83c0: 6f 63 61 6c 4f 70 65 6e 20 29 20 72 65 74 75 72  ocalOpen ) retur
83d0: 6e 3b 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 64  n;.    zName = d
83e0: 62 5f 6c 67 65 74 28 22 72 65 70 6f 73 69 74 6f  b_lget("reposito
83f0: 72 79 22 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 66  ry", 0);.  }.  f
8400: 69 6c 65 5f 63 61 6e 6f 6e 69 63 61 6c 5f 6e 61  ile_canonical_na
8410: 6d 65 28 7a 4e 61 6d 65 2c 20 26 66 75 6c 6c 29  me(zName, &full)
8420: 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65  ;.  db_multi_exe
8430: 63 28 0a 20 20 20 20 20 22 49 4e 53 45 52 54 20  c(.     "INSERT 
8440: 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 67  OR IGNORE INTO g
8450: 6c 6f 62 61 6c 5f 63 6f 6e 66 69 67 28 6e 61 6d  lobal_config(nam
8460: 65 2c 76 61 6c 75 65 29 22 0a 20 20 20 20 20 22  e,value)".     "
8470: 56 41 4c 55 45 53 28 27 72 65 70 6f 3a 25 71 27  VALUES('repo:%q'
8480: 2c 31 29 22 2c 0a 20 20 20 20 20 62 6c 6f 62 5f  ,1)",.     blob_
8490: 73 74 72 28 26 66 75 6c 6c 29 0a 20 20 29 3b 0a  str(&full).  );.
84a0: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 66 75    blob_reset(&fu
84b0: 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f  ll);.}../*.** CO
84c0: 4d 4d 41 4e 44 3a 20 6f 70 65 6e 0a 2a 2a 0a 2a  MMAND: open.**.*
84d0: 2a 20 55 73 61 67 65 3a 20 25 66 6f 73 73 69 6c  * Usage: %fossil
84e0: 20 6f 70 65 6e 20 46 49 4c 45 4e 41 4d 45 20 3f   open FILENAME ?
84f0: 56 45 52 53 49 4f 4e 3f 20 3f 2d 2d 6b 65 65 70  VERSION? ?--keep
8500: 3f 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 63  ?.**.** Open a c
8510: 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 74 68 65  onnection to the
8520: 20 6c 6f 63 61 6c 20 72 65 70 6f 73 69 74 6f 72   local repositor
8530: 79 20 69 6e 20 46 49 4c 45 4e 41 4d 45 2e 20 20  y in FILENAME.  
8540: 41 20 63 68 65 63 6b 6f 75 74 0a 2a 2a 20 66 6f  A checkout.** fo
8550: 72 20 74 68 65 20 72 65 70 6f 73 69 74 6f 72 79  r the repository
8560: 20 69 73 20 63 72 65 61 74 65 64 20 77 69 74 68   is created with
8570: 20 69 74 73 20 72 6f 6f 74 20 61 74 20 74 68 65   its root at the
8580: 20 77 6f 72 6b 69 6e 67 20 64 69 72 65 63 74 6f   working directo
8590: 72 79 2e 0a 2a 2a 20 49 66 20 56 45 52 53 49 4f  ry..** If VERSIO
85a0: 4e 20 69 73 20 73 70 65 63 69 66 69 65 64 20 74  N is specified t
85b0: 68 65 6e 20 74 68 61 74 20 76 65 72 73 69 6f 6e  hen that version
85c0: 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 2e   is checked out.
85d0: 20 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a 20 74    Otherwise.** t
85e0: 68 65 20 6c 61 74 65 73 74 20 76 65 72 73 69 6f  he latest versio
85f0: 6e 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74  n is checked out
8600: 2e 20 20 4e 6f 20 66 69 6c 65 73 20 6f 74 68 65  .  No files othe
8610: 72 20 74 68 61 6e 20 22 6d 61 6e 69 66 65 73 74  r than "manifest
8620: 22 0a 2a 2a 20 61 6e 64 20 22 6d 61 6e 69 66 65  ".** and "manife
8630: 73 74 2e 75 75 69 64 22 20 61 72 65 20 6d 6f 64  st.uuid" are mod
8640: 69 66 69 65 64 20 69 66 20 74 68 65 20 2d 2d 6b  ified if the --k
8650: 65 65 70 20 6f 70 74 69 6f 6e 20 69 73 20 70 72  eep option is pr
8660: 65 73 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  esent..**.** See
8670: 20 61 6c 73 6f 20 74 68 65 20 22 63 6c 6f 73 65   also the "close
8680: 22 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 76 6f  " command..*/.vo
8690: 69 64 20 63 6d 64 5f 6f 70 65 6e 28 76 6f 69 64  id cmd_open(void
86a0: 29 7b 0a 20 20 42 6c 6f 62 20 70 61 74 68 3b 0a  ){.  Blob path;.
86b0: 20 20 69 6e 74 20 76 69 64 3b 0a 20 20 69 6e 74    int vid;.  int
86c0: 20 6b 65 65 70 46 6c 61 67 3b 0a 20 20 73 74 61   keepFlag;.  sta
86d0: 74 69 63 20 63 68 61 72 20 2a 61 7a 4e 65 77 41  tic char *azNewA
86e0: 72 67 76 5b 5d 20 3d 20 7b 20 30 2c 20 22 63 68  rgv[] = { 0, "ch
86f0: 65 63 6b 6f 75 74 22 2c 20 22 2d 2d 6c 61 74 65  eckout", "--late
8700: 73 74 22 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a  st", 0, 0, 0 };.
8710: 20 20 75 72 6c 5f 70 72 6f 78 79 5f 6f 70 74 69    url_proxy_opti
8720: 6f 6e 73 28 29 3b 0a 20 20 6b 65 65 70 46 6c 61  ons();.  keepFla
8730: 67 20 3d 20 66 69 6e 64 5f 6f 70 74 69 6f 6e 28  g = find_option(
8740: 22 6b 65 65 70 22 2c 30 2c 30 29 21 3d 30 3b 0a  "keep",0,0)!=0;.
8750: 20 20 69 66 28 20 67 2e 61 72 67 63 21 3d 33 20    if( g.argc!=3 
8760: 26 26 20 67 2e 61 72 67 63 21 3d 34 20 29 7b 0a  && g.argc!=4 ){.
8770: 20 20 20 20 75 73 61 67 65 28 22 52 45 50 4f 53      usage("REPOS
8780: 49 54 4f 52 59 2d 46 49 4c 45 4e 41 4d 45 20 3f  ITORY-FILENAME ?
8790: 56 45 52 53 49 4f 4e 3f 22 29 3b 0a 20 20 7d 0a  VERSION?");.  }.
87a0: 20 20 69 66 28 20 64 62 5f 6f 70 65 6e 5f 6c 6f    if( db_open_lo
87b0: 63 61 6c 28 29 20 29 7b 0a 20 20 20 20 66 6f 73  cal() ){.    fos
87c0: 73 69 6c 5f 70 61 6e 69 63 28 22 61 6c 72 65 61  sil_panic("alrea
87d0: 64 79 20 77 69 74 68 69 6e 20 61 6e 20 6f 70 65  dy within an ope
87e0: 6e 20 74 72 65 65 20 72 6f 6f 74 65 64 20 61 74  n tree rooted at
87f0: 20 25 73 22 2c 20 67 2e 7a 4c 6f 63 61 6c 52 6f   %s", g.zLocalRo
8800: 6f 74 29 3b 0a 20 20 7d 0a 20 20 66 69 6c 65 5f  ot);.  }.  file_
8810: 63 61 6e 6f 6e 69 63 61 6c 5f 6e 61 6d 65 28 67  canonical_name(g
8820: 2e 61 72 67 76 5b 32 5d 2c 20 26 70 61 74 68 29  .argv[2], &path)
8830: 3b 0a 20 20 64 62 5f 6f 70 65 6e 5f 72 65 70 6f  ;.  db_open_repo
8840: 73 69 74 6f 72 79 28 62 6c 6f 62 5f 73 74 72 28  sitory(blob_str(
8850: 26 70 61 74 68 29 29 3b 0a 20 20 64 62 5f 69 6e  &path));.  db_in
8860: 69 74 5f 64 61 74 61 62 61 73 65 28 22 2e 2f 5f  it_database("./_
8870: 46 4f 53 53 49 4c 5f 22 2c 20 7a 4c 6f 63 61 6c  FOSSIL_", zLocal
8880: 53 63 68 65 6d 61 2c 20 28 63 68 61 72 2a 29 30  Schema, (char*)0
8890: 29 3b 0a 20 20 64 62 5f 6f 70 65 6e 5f 6c 6f 63  );.  db_open_loc
88a0: 61 6c 28 29 3b 0a 20 20 64 62 5f 6c 73 65 74 28  al();.  db_lset(
88b0: 22 72 65 70 6f 73 69 74 6f 72 79 22 2c 20 62 6c  "repository", bl
88c0: 6f 62 5f 73 74 72 28 26 70 61 74 68 29 29 3b 0a  ob_str(&path));.
88d0: 20 20 64 62 5f 72 65 63 6f 72 64 5f 72 65 70 6f    db_record_repo
88e0: 73 69 74 6f 72 79 5f 66 69 6c 65 6e 61 6d 65 28  sitory_filename(
88f0: 62 6c 6f 62 5f 73 74 72 28 26 70 61 74 68 29 29  blob_str(&path))
8900: 3b 0a 20 20 76 69 64 20 3d 20 64 62 5f 69 6e 74  ;.  vid = db_int
8910: 28 30 2c 20 22 53 45 4c 45 43 54 20 70 69 64 20  (0, "SELECT pid 
8920: 46 52 4f 4d 20 70 6c 69 6e 6b 20 79 22 0a 20 20  FROM plink y".  
8930: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8940: 22 20 57 48 45 52 45 20 4e 4f 54 20 45 58 49 53  " WHERE NOT EXIS
8950: 54 53 28 53 45 4c 45 43 54 20 31 20 46 52 4f 4d  TS(SELECT 1 FROM
8960: 20 70 6c 69 6e 6b 20 78 20 57 48 45 52 45 20 78   plink x WHERE x
8970: 2e 63 69 64 3d 79 2e 70 69 64 29 22 29 3b 0a 20  .cid=y.pid)");. 
8980: 20 69 66 28 20 76 69 64 3d 3d 30 20 29 7b 0a 20   if( vid==0 ){. 
8990: 20 20 20 64 62 5f 6c 73 65 74 5f 69 6e 74 28 22     db_lset_int("
89a0: 63 68 65 63 6b 6f 75 74 22 2c 20 31 29 3b 0a 20  checkout", 1);. 
89b0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72   }else{.    char
89c0: 20 2a 2a 6f 6c 64 41 72 67 76 20 3d 20 67 2e 61   **oldArgv = g.a
89d0: 72 67 76 3b 0a 20 20 20 20 69 6e 74 20 6f 6c 64  rgv;.    int old
89e0: 41 72 67 63 20 3d 20 67 2e 61 72 67 63 3b 0a 20  Argc = g.argc;. 
89f0: 20 20 20 64 62 5f 6c 73 65 74 5f 69 6e 74 28 22     db_lset_int("
8a00: 63 68 65 63 6b 6f 75 74 22 2c 20 76 69 64 29 3b  checkout", vid);
8a10: 0a 20 20 20 20 61 7a 4e 65 77 41 72 67 76 5b 30  .    azNewArgv[0
8a20: 5d 20 3d 20 67 2e 61 72 67 76 5b 30 5d 3b 0a 20  ] = g.argv[0];. 
8a30: 20 20 20 67 2e 61 72 67 76 20 3d 20 61 7a 4e 65     g.argv = azNe
8a40: 77 41 72 67 76 3b 0a 20 20 20 20 67 2e 61 72 67  wArgv;.    g.arg
8a50: 63 20 3d 20 33 3b 0a 20 20 20 20 69 66 28 20 6f  c = 3;.    if( o
8a60: 6c 64 41 72 67 63 3d 3d 34 20 29 7b 0a 20 20 20  ldArgc==4 ){.   
8a70: 20 20 20 61 7a 4e 65 77 41 72 67 76 5b 67 2e 61     azNewArgv[g.a
8a80: 72 67 63 2d 31 5d 20 3d 20 6f 6c 64 41 72 67 76  rgc-1] = oldArgv
8a90: 5b 33 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  [3];.    }.    i
8aa0: 66 28 20 6b 65 65 70 46 6c 61 67 20 29 7b 0a 20  f( keepFlag ){. 
8ab0: 20 20 20 20 20 61 7a 4e 65 77 41 72 67 76 5b 67       azNewArgv[g
8ac0: 2e 61 72 67 63 2b 2b 5d 20 3d 20 22 2d 2d 6b 65  .argc++] = "--ke
8ad0: 65 70 22 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  ep";.    }.    c
8ae0: 68 65 63 6b 6f 75 74 5f 63 6d 64 28 29 3b 0a 20  heckout_cmd();. 
8af0: 20 20 20 67 2e 61 72 67 63 20 3d 20 32 3b 0a 20     g.argc = 2;. 
8b00: 20 20 20 69 6e 66 6f 5f 63 6d 64 28 29 3b 0a 20     info_cmd();. 
8b10: 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e   }.}../*.** Prin
8b20: 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61  t the value of a
8b30: 20 73 65 74 74 69 6e 67 20 6e 61 6d 65 64 20 7a   setting named z
8b40: 4e 61 6d 65 0a 2a 2f 0a 73 74 61 74 69 63 20 76  Name.*/.static v
8b50: 6f 69 64 20 70 72 69 6e 74 5f 73 65 74 74 69 6e  oid print_settin
8b60: 67 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  g(const char *zN
8b70: 61 6d 65 29 7b 0a 20 20 53 74 6d 74 20 71 3b 0a  ame){.  Stmt q;.
8b80: 20 20 69 66 28 20 67 2e 72 65 70 6f 73 69 74 6f    if( g.reposito
8b90: 72 79 4f 70 65 6e 20 29 7b 0a 20 20 20 20 64 62  ryOpen ){.    db
8ba0: 5f 70 72 65 70 61 72 65 28 26 71 2c 0a 20 20 20  _prepare(&q,.   
8bb0: 20 20 20 20 22 53 45 4c 45 43 54 20 27 28 6c 6f      "SELECT '(lo
8bc0: 63 61 6c 29 27 2c 20 76 61 6c 75 65 20 46 52 4f  cal)', value FRO
8bd0: 4d 20 63 6f 6e 66 69 67 20 57 48 45 52 45 20 6e  M config WHERE n
8be0: 61 6d 65 3d 25 51 22 0a 20 20 20 20 20 20 20 22  ame=%Q".       "
8bf0: 20 55 4e 49 4f 4e 20 41 4c 4c 20 22 0a 20 20 20   UNION ALL ".   
8c00: 20 20 20 20 22 53 45 4c 45 43 54 20 27 28 67 6c      "SELECT '(gl
8c10: 6f 62 61 6c 29 27 2c 20 76 61 6c 75 65 20 46 52  obal)', value FR
8c20: 4f 4d 20 67 6c 6f 62 61 6c 5f 63 6f 6e 66 69 67  OM global_config
8c30: 20 57 48 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c   WHERE name=%Q",
8c40: 0a 20 20 20 20 20 20 20 7a 4e 61 6d 65 2c 20 7a  .       zName, z
8c50: 4e 61 6d 65 0a 20 20 20 20 29 3b 0a 20 20 7d 65  Name.    );.  }e
8c60: 6c 73 65 7b 0a 20 20 20 20 64 62 5f 70 72 65 70  lse{.    db_prep
8c70: 61 72 65 28 26 71 2c 0a 20 20 20 20 20 20 22 53  are(&q,.      "S
8c80: 45 4c 45 43 54 20 27 28 67 6c 6f 62 61 6c 29 27  ELECT '(global)'
8c90: 2c 20 76 61 6c 75 65 20 46 52 4f 4d 20 67 6c 6f  , value FROM glo
8ca0: 62 61 6c 5f 63 6f 6e 66 69 67 20 57 48 45 52 45  bal_config WHERE
8cb0: 20 6e 61 6d 65 3d 25 51 22 2c 0a 20 20 20 20 20   name=%Q",.     
8cc0: 20 7a 4e 61 6d 65 0a 20 20 20 20 29 3b 0a 20 20   zName.    );.  
8cd0: 7d 0a 20 20 69 66 28 20 64 62 5f 73 74 65 70 28  }.  if( db_step(
8ce0: 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20  &q)==SQLITE_ROW 
8cf0: 29 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 25  ){.    printf("%
8d00: 2d 32 30 73 20 25 2d 38 73 20 25 73 5c 6e 22 2c  -20s %-8s %s\n",
8d10: 20 7a 4e 61 6d 65 2c 20 64 62 5f 63 6f 6c 75 6d   zName, db_colum
8d20: 6e 5f 74 65 78 74 28 26 71 2c 20 30 29 2c 0a 20  n_text(&q, 0),. 
8d30: 20 20 20 20 20 20 20 64 62 5f 63 6f 6c 75 6d 6e         db_column
8d40: 5f 74 65 78 74 28 26 71 2c 20 31 29 29 3b 0a 20  _text(&q, 1));. 
8d50: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 72 69 6e   }else{.    prin
8d60: 74 66 28 22 25 2d 32 30 73 5c 6e 22 2c 20 7a 4e  tf("%-20s\n", zN
8d70: 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 66  ame);.  }.  db_f
8d80: 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 7d 0a 0a  inalize(&q);.}..
8d90: 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20  ./*.** COMMAND: 
8da0: 73 65 74 74 69 6e 67 73 0a 2a 2a 20 43 4f 4d 4d  settings.** COMM
8db0: 41 4e 44 3a 20 75 6e 73 65 74 0a 2a 2a 20 25 66  AND: unset.** %f
8dc0: 6f 73 73 69 6c 20 73 65 74 74 69 6e 67 20 3f 50  ossil setting ?P
8dd0: 52 4f 50 45 52 54 59 3f 20 3f 56 41 4c 55 45 3f  ROPERTY? ?VALUE?
8de0: 20 3f 2d 67 6c 6f 62 61 6c 3f 0a 2a 2a 20 25 66   ?-global?.** %f
8df0: 6f 73 73 69 6c 20 75 6e 73 65 74 20 50 52 4f 50  ossil unset PROP
8e00: 45 52 54 59 20 3f 2d 67 6c 6f 62 61 6c 3f 0a 2a  ERTY ?-global?.*
8e10: 2a 0a 2a 2a 20 54 68 65 20 22 73 65 74 74 69 6e  *.** The "settin
8e20: 67 22 20 63 6f 6d 6d 61 6e 64 20 77 69 74 68 20  g" command with 
8e30: 6e 6f 20 61 72 67 75 6d 65 6e 74 73 20 6c 69 73  no arguments lis
8e40: 74 73 20 61 6c 6c 20 70 72 6f 70 65 72 74 69 65  ts all propertie
8e50: 73 20 61 6e 64 20 74 68 65 69 72 0a 2a 2a 20 76  s and their.** v
8e60: 61 6c 75 65 73 2e 20 20 57 69 74 68 20 6a 75 73  alues.  With jus
8e70: 74 20 61 20 70 72 6f 70 65 72 74 79 20 6e 61 6d  t a property nam
8e80: 65 20 69 74 20 73 68 6f 77 73 20 74 68 65 20 76  e it shows the v
8e90: 61 6c 75 65 20 6f 66 20 74 68 61 74 20 70 72 6f  alue of that pro
8ea0: 70 65 72 74 79 2e 0a 2a 2a 20 57 69 74 68 20 61  perty..** With a
8eb0: 20 76 61 6c 75 65 20 61 72 67 75 6d 65 6e 74 20   value argument 
8ec0: 69 74 20 63 68 61 6e 67 65 73 20 74 68 65 20 70  it changes the p
8ed0: 72 6f 70 65 72 74 79 20 66 6f 72 20 74 68 65 20  roperty for the 
8ee0: 63 75 72 72 65 6e 74 20 72 65 70 6f 73 69 74 6f  current reposito
8ef0: 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 75  ry..**.** The "u
8f00: 6e 73 65 74 22 20 63 6f 6d 6d 61 6e 64 20 63 6c  nset" command cl
8f10: 65 61 72 73 20 61 20 70 72 6f 70 65 72 74 79 20  ears a property 
8f20: 73 65 74 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20  setting..**.**  
8f30: 20 20 61 75 74 6f 73 79 6e 63 20 20 20 20 20 20    autosync      
8f40: 20 20 20 49 66 20 65 6e 61 62 6c 65 64 2c 20 61     If enabled, a
8f50: 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 70 75 6c  utomatically pul
8f60: 6c 20 70 72 69 6f 72 20 74 6f 0a 2a 2a 20 20 20  l prior to.**   
8f70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8f80: 20 20 63 6f 6d 6d 69 74 20 6f 72 20 75 70 64 61    commit or upda
8f90: 74 65 20 61 6e 64 20 61 75 74 6f 6d 61 74 69 63  te and automatic
8fa0: 61 6c 6c 79 20 70 75 73 68 0a 2a 2a 20 20 20 20  ally push.**    
8fb0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8fc0: 20 61 66 74 65 72 20 63 6f 6d 6d 69 74 20 6f 72   after commit or
8fd0: 20 74 61 67 20 6f 72 20 62 72 61 6e 63 68 20 63   tag or branch c
8fe0: 72 65 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20  reation..**.**  
8ff0: 20 20 64 69 66 66 2d 63 6f 6d 6d 61 6e 64 20 20    diff-command  
9000: 20 20 20 45 78 74 65 72 6e 61 6c 20 63 6f 6d 6d     External comm
9010: 61 6e 64 20 74 6f 20 72 75 6e 20 77 68 65 6e 20  and to run when 
9020: 70 65 72 66 6f 72 6d 69 6e 67 20 61 20 64 69 66  performing a dif
9030: 66 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  f..**           
9040: 20 20 20 20 20 20 20 20 20 20 49 66 20 75 6e 64            If und
9050: 65 66 69 6e 65 64 2c 20 74 68 65 20 69 6e 74 65  efined, the inte
9060: 72 6e 61 6c 20 74 65 78 74 20 64 69 66 66 20 77  rnal text diff w
9070: 69 6c 6c 20 62 65 20 75 73 65 64 2e 0a 2a 2a 0a  ill be used..**.
9080: 2a 2a 20 20 20 20 65 64 69 74 6f 72 20 20 20 20  **    editor    
9090: 20 20 20 20 20 20 20 54 65 78 74 20 65 64 69 74         Text edit
90a0: 6f 72 20 63 6f 6d 6d 61 6e 64 20 75 73 65 64 20  or command used 
90b0: 66 6f 72 20 63 68 65 63 6b 2d 69 6e 20 63 6f 6d  for check-in com
90c0: 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  ments..**.**    
90d0: 68 74 74 70 2d 70 6f 72 74 20 20 20 20 20 20 20  http-port       
90e0: 20 54 68 65 20 54 43 50 2f 49 50 20 70 6f 72 74   The TCP/IP port
90f0: 20 6e 75 6d 62 65 72 20 74 6f 20 75 73 65 20 62   number to use b
9100: 79 20 74 68 65 20 22 73 65 72 76 65 72 22 0a 2a  y the "server".*
9110: 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
9120: 20 20 20 20 20 20 61 6e 64 20 22 75 69 22 20 63        and "ui" c
9130: 6f 6d 6d 61 6e 64 73 2e 20 20 44 65 66 61 75 6c  ommands.  Defaul
9140: 74 3a 20 38 30 38 30 0a 2a 2a 0a 2a 2a 20 20 20  t: 8080.**.**   
9150: 20 67 64 69 66 66 2d 63 6f 6d 6d 61 6e 64 20 20   gdiff-command  
9160: 20 20 45 78 74 65 72 6e 61 6c 20 63 6f 6d 6d 61    External comma
9170: 6e 64 20 74 6f 20 72 75 6e 20 77 68 65 6e 20 70  nd to run when p
9180: 65 72 66 6f 72 6d 69 6e 67 20 61 20 67 72 61 70  erforming a grap
9190: 68 69 63 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20  hical.**        
91a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 64 69 66               dif
91b0: 66 2e 20 49 66 20 75 6e 64 65 66 69 6e 65 64 2c  f. If undefined,
91c0: 20 74 65 78 74 20 64 69 66 66 20 77 69 6c 6c 20   text diff will 
91d0: 62 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 20  be used..**.**  
91e0: 20 20 6c 6f 63 61 6c 61 75 74 68 20 20 20 20 20    localauth     
91f0: 20 20 20 49 66 20 65 6e 61 62 6c 65 64 2c 20 72     If enabled, r
9200: 65 71 75 69 72 65 20 74 68 61 74 20 48 54 54 50  equire that HTTP
9210: 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 72 6f   connections fro
9220: 6d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  m.**            
9230: 20 20 20 20 20 20 20 20 20 31 32 37 2e 30 2e 30           127.0.0
9240: 2e 31 20 62 65 20 61 75 74 68 65 6e 74 69 63 61  .1 be authentica
9250: 74 65 64 20 62 79 20 70 61 73 73 77 6f 72 64 2e  ted by password.
9260: 20 20 49 66 0a 2a 2a 20 20 20 20 20 20 20 20 20    If.**         
9270: 20 20 20 20 20 20 20 20 20 20 20 20 66 61 6c 73              fals
9280: 65 2c 20 61 6c 6c 20 48 54 54 50 20 72 65 71 75  e, all HTTP requ
9290: 65 73 74 73 20 66 72 6f 6d 20 6c 6f 63 61 6c 68  ests from localh
92a0: 6f 73 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 20  ost have.**     
92b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
92c0: 75 6e 72 65 73 74 72 69 63 74 65 64 20 61 63 63  unrestricted acc
92d0: 65 73 73 20 74 6f 20 74 68 65 20 72 65 70 6f 73  ess to the repos
92e0: 69 74 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  itory..**.**    
92f0: 63 6c 65 61 72 73 69 67 6e 20 20 20 20 20 20 20  clearsign       
9300: 20 57 68 65 6e 20 65 6e 61 62 6c 65 64 20 28 74   When enabled (t
9310: 68 65 20 64 65 66 61 75 6c 74 29 2c 20 66 6f 73  he default), fos
9320: 73 69 6c 20 77 69 6c 6c 20 61 74 74 65 6d 70 74  sil will attempt
9330: 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   to.**          
9340: 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 20             sign 
9350: 61 6c 6c 20 63 6f 6d 6d 69 74 73 20 77 69 74 68  all commits with
9360: 20 67 70 67 2e 20 20 57 68 65 6e 20 64 69 73 61   gpg.  When disa
9370: 62 6c 65 64 2c 20 63 6f 6d 6d 69 74 73 20 77 69  bled, commits wi
9380: 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ll.**           
9390: 20 20 20 20 20 20 20 20 20 20 62 65 20 75 6e 73            be uns
93a0: 69 67 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  igned..**.**    
93b0: 70 67 70 2d 63 6f 6d 6d 61 6e 64 20 20 20 20 20  pgp-command     
93c0: 20 43 6f 6d 6d 61 6e 64 20 75 73 65 64 20 74 6f   Command used to
93d0: 20 63 6c 65 61 72 2d 73 69 67 6e 20 6d 61 6e 69   clear-sign mani
93e0: 66 65 73 74 73 20 61 74 20 63 68 65 63 6b 2d 69  fests at check-i
93f0: 6e 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  n..**           
9400: 20 20 20 20 20 20 20 20 20 20 54 68 65 20 64 65            The de
9410: 66 61 75 6c 74 20 69 73 20 22 67 70 67 20 2d 2d  fault is "gpg --
9420: 63 6c 65 61 72 73 69 67 6e 20 2d 6f 20 22 2e 0a  clearsign -o "..
9430: 2a 2a 0a 2a 2a 20 20 20 20 6d 74 69 6d 65 2d 63  **.**    mtime-c
9440: 68 61 6e 67 65 73 20 20 20 20 55 73 65 20 66 69  hanges    Use fi
9450: 6c 65 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20  le modification 
9460: 74 69 6d 65 73 20 28 6d 74 69 6d 65 73 29 20 74  times (mtimes) t
9470: 6f 20 64 65 74 65 63 74 20 77 68 65 6e 0a 2a 2a  o detect when.**
9480: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9490: 20 20 20 20 20 66 69 6c 65 73 20 68 61 76 65 20       files have 
94a0: 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20 20  been modified.  
94b0: 0a 2a 2a 0a 2a 2a 20 20 20 20 70 72 6f 78 79 20  .**.**    proxy 
94c0: 20 20 20 20 20 20 20 20 20 20 20 55 52 4c 20 6f             URL o
94d0: 66 20 74 68 65 20 48 54 54 50 20 70 72 6f 78 79  f the HTTP proxy
94e0: 2e 20 20 49 66 20 75 6e 64 65 66 69 6e 65 64 20  .  If undefined 
94f0: 6f 72 20 22 6f 66 66 22 20 74 68 65 6e 0a 2a 2a  or "off" then.**
9500: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9510: 20 20 20 20 20 74 68 65 20 22 68 74 74 70 5f 70       the "http_p
9520: 72 6f 78 79 22 20 65 6e 76 69 72 6f 6e 6d 65 6e  roxy" environmen
9530: 74 20 76 61 72 69 61 62 6c 65 20 69 73 20 63 6f  t variable is co
9540: 6e 73 75 6c 74 65 64 2e 0a 2a 2a 20 20 20 20 20  nsulted..**     
9550: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9560: 49 66 20 74 68 65 20 68 74 74 70 5f 70 72 6f 78  If the http_prox
9570: 79 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 76 61  y environment va
9580: 72 69 61 62 6c 65 20 69 73 20 75 6e 64 65 66 69  riable is undefi
9590: 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ned.**          
95a0: 20 20 20 20 20 20 20 20 20 20 20 74 68 65 6e 20             then 
95b0: 61 20 64 69 72 65 63 74 20 48 54 54 50 20 63 6f  a direct HTTP co
95c0: 6e 6e 65 63 74 69 6f 6e 20 69 73 20 75 73 65 64  nnection is used
95d0: 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 77 65 62 2d 62  ..**.**    web-b
95e0: 72 6f 77 73 65 72 20 20 20 20 20 20 41 20 73 68  rowser      A sh
95f0: 65 6c 6c 20 63 6f 6d 6d 61 6e 64 20 75 73 65 64  ell command used
9600: 20 74 6f 20 6c 61 75 6e 63 68 20 79 6f 75 72 20   to launch your 
9610: 70 72 65 66 65 72 72 65 64 0a 2a 2a 20 20 20 20  preferred.**    
9620: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9630: 20 77 65 62 20 62 72 6f 77 73 65 72 20 77 68 65   web browser whe
9640: 6e 20 67 69 76 65 6e 20 61 20 55 52 4c 20 61 73  n given a URL as
9650: 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a   an argument..**
9660: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9670: 20 20 20 20 20 44 65 66 61 75 6c 74 73 20 74 6f       Defaults to
9680: 20 22 73 74 61 72 74 22 20 6f 6e 20 77 69 6e 64   "start" on wind
9690: 6f 77 73 2c 20 22 6f 70 65 6e 22 20 6f 6e 20 4d  ows, "open" on M
96a0: 61 63 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ac,.**          
96b0: 20 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 22             and "
96c0: 66 69 72 65 66 6f 78 22 20 6f 6e 20 55 6e 69 78  firefox" on Unix
96d0: 2e 0a 2a 2f 0a 76 6f 69 64 20 73 65 74 74 69 6e  ..*/.void settin
96e0: 67 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 73  g_cmd(void){.  s
96f0: 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
9700: 20 2a 61 7a 4e 61 6d 65 5b 5d 20 3d 20 7b 0a 20   *azName[] = {. 
9710: 20 20 20 22 61 75 74 6f 73 79 6e 63 22 2c 0a 20     "autosync",. 
9720: 20 20 20 22 64 69 66 66 2d 63 6f 6d 6d 61 6e 64     "diff-command
9730: 22 2c 0a 20 20 20 20 22 65 64 69 74 6f 72 22 2c  ",.    "editor",
9740: 0a 20 20 20 20 22 67 64 69 66 66 2d 63 6f 6d 6d  .    "gdiff-comm
9750: 61 6e 64 22 2c 0a 20 20 20 20 22 68 74 74 70 2d  and",.    "http-
9760: 70 6f 72 74 22 2c 0a 20 20 20 20 22 6c 6f 63 61  port",.    "loca
9770: 6c 61 75 74 68 22 2c 0a 20 20 20 20 22 63 6c 65  lauth",.    "cle
9780: 61 72 73 69 67 6e 22 2c 0a 20 20 20 20 22 70 67  arsign",.    "pg
9790: 70 2d 63 6f 6d 6d 61 6e 64 22 2c 0a 20 20 20 20  p-command",.    
97a0: 22 6d 74 69 6d 65 2d 63 68 61 6e 67 65 73 22 2c  "mtime-changes",
97b0: 0a 20 20 20 20 22 70 72 6f 78 79 22 2c 0a 20 20  .    "proxy",.  
97c0: 20 20 22 77 65 62 2d 62 72 6f 77 73 65 72 22 2c    "web-browser",
97d0: 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  .  };.  int i;. 
97e0: 20 69 6e 74 20 67 6c 6f 62 61 6c 46 6c 61 67 20   int globalFlag 
97f0: 3d 20 66 69 6e 64 5f 6f 70 74 69 6f 6e 28 22 67  = find_option("g
9800: 6c 6f 62 61 6c 22 2c 22 67 22 2c 30 29 21 3d 30  lobal","g",0)!=0
9810: 3b 0a 20 20 69 6e 74 20 75 6e 73 65 74 46 6c 61  ;.  int unsetFla
9820: 67 20 3d 20 67 2e 61 72 67 76 5b 31 5d 5b 30 5d  g = g.argv[1][0]
9830: 3d 3d 27 75 27 3b 0a 20 20 64 62 5f 66 69 6e 64  =='u';.  db_find
9840: 5f 61 6e 64 5f 6f 70 65 6e 5f 72 65 70 6f 73 69  _and_open_reposi
9850: 74 6f 72 79 28 30 29 3b 0a 20 20 69 66 28 20 21  tory(0);.  if( !
9860: 67 2e 72 65 70 6f 73 69 74 6f 72 79 4f 70 65 6e  g.repositoryOpen
9870: 20 29 7b 0a 20 20 20 20 67 6c 6f 62 61 6c 46 6c   ){.    globalFl
9880: 61 67 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 64 62  ag = 1;.  }.  db
9890: 5f 6f 70 65 6e 5f 63 6f 6e 66 69 67 28 29 3b 0a  _open_config();.
98a0: 20 20 69 66 28 20 75 6e 73 65 74 46 6c 61 67 20    if( unsetFlag 
98b0: 26 26 20 67 2e 61 72 67 63 21 3d 33 20 29 7b 0a  && g.argc!=3 ){.
98c0: 20 20 20 20 75 73 61 67 65 28 22 50 52 4f 50 45      usage("PROPE
98d0: 52 54 59 20 3f 2d 67 6c 6f 62 61 6c 3f 22 29 3b  RTY ?-global?");
98e0: 0a 20 20 7d 0a 20 20 69 66 28 20 67 2e 61 72 67  .  }.  if( g.arg
98f0: 63 3d 3d 32 20 29 7b 0a 20 20 20 20 66 6f 72 28  c==2 ){.    for(
9900: 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 7a  i=0; i<sizeof(az
9910: 4e 61 6d 65 29 2f 73 69 7a 65 6f 66 28 61 7a 4e  Name)/sizeof(azN
9920: 61 6d 65 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20  ame[0]); i++){. 
9930: 20 20 20 20 20 70 72 69 6e 74 5f 73 65 74 74 69       print_setti
9940: 6e 67 28 61 7a 4e 61 6d 65 5b 69 5d 29 3b 0a 20  ng(azName[i]);. 
9950: 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
9960: 20 67 2e 61 72 67 63 3d 3d 33 20 7c 7c 20 67 2e   g.argc==3 || g.
9970: 61 72 67 63 3d 3d 34 20 29 7b 0a 20 20 20 20 63  argc==4 ){.    c
9980: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
9990: 20 3d 20 67 2e 61 72 67 76 5b 32 5d 3b 0a 20 20   = g.argv[2];.  
99a0: 20 20 69 6e 74 20 6e 20 3d 20 73 74 72 6c 65 6e    int n = strlen
99b0: 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 66 6f 72  (zName);.    for
99c0: 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61  (i=0; i<sizeof(a
99d0: 7a 4e 61 6d 65 29 2f 73 69 7a 65 6f 66 28 61 7a  zName)/sizeof(az
99e0: 4e 61 6d 65 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a  Name[0]); i++){.
99f0: 20 20 20 20 20 20 69 66 28 20 73 74 72 6e 63 6d        if( strncm
9a00: 70 28 61 7a 4e 61 6d 65 5b 69 5d 2c 20 7a 4e 61  p(azName[i], zNa
9a10: 6d 65 2c 20 6e 29 3d 3d 30 20 29 20 62 72 65 61  me, n)==0 ) brea
9a20: 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  k;.    }.    if(
9a30: 20 69 3e 3d 73 69 7a 65 6f 66 28 61 7a 4e 61 6d   i>=sizeof(azNam
9a40: 65 29 2f 73 69 7a 65 6f 66 28 61 7a 4e 61 6d 65  e)/sizeof(azName
9a50: 5b 30 5d 29 20 29 7b 0a 20 20 20 20 20 20 66 6f  [0]) ){.      fo
9a60: 73 73 69 6c 5f 66 61 74 61 6c 28 22 6e 6f 20 73  ssil_fatal("no s
9a70: 75 63 68 20 73 65 74 74 69 6e 67 3a 20 25 73 22  uch setting: %s"
9a80: 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a  , zName);.    }.
9a90: 20 20 20 20 69 66 28 20 75 6e 73 65 74 46 6c 61      if( unsetFla
9aa0: 67 20 29 7b 0a 20 20 20 20 20 20 64 62 5f 75 6e  g ){.      db_un
9ab0: 73 65 74 28 61 7a 4e 61 6d 65 5b 69 5d 2c 20 67  set(azName[i], g
9ac0: 6c 6f 62 61 6c 46 6c 61 67 29 3b 0a 20 20 20 20  lobalFlag);.    
9ad0: 7d 65 6c 73 65 20 69 66 28 20 67 2e 61 72 67 63  }else if( g.argc
9ae0: 3d 3d 34 20 29 7b 0a 20 20 20 20 20 20 64 62 5f  ==4 ){.      db_
9af0: 73 65 74 28 61 7a 4e 61 6d 65 5b 69 5d 2c 20 67  set(azName[i], g
9b00: 2e 61 72 67 76 5b 33 5d 2c 20 67 6c 6f 62 61 6c  .argv[3], global
9b10: 46 6c 61 67 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Flag);.    }else
9b20: 7b 0a 20 20 20 20 20 20 70 72 69 6e 74 5f 73 65  {.      print_se
9b30: 74 74 69 6e 67 28 61 7a 4e 61 6d 65 5b 69 5d 29  tting(azName[i])
9b40: 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
9b50: 0a 20 20 20 20 75 73 61 67 65 28 22 3f 50 52 4f  .    usage("?PRO
9b60: 50 45 52 54 59 3f 20 3f 56 41 4c 55 45 3f 22 29  PERTY? ?VALUE?")
9b70: 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  ;.  }.}../*.** S
9b80: 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 72  QL function to r
9b90: 65 6e 64 65 72 20 61 20 55 55 49 44 20 61 73 20  ender a UUID as 
9ba0: 61 20 68 79 70 65 72 6c 69 6e 6b 20 74 6f 20 61  a hyperlink to a
9bb0: 20 70 61 67 65 20 64 65 73 63 72 69 62 69 6e 67   page describing
9bc0: 0a 2a 2a 20 74 68 61 74 20 55 55 49 44 2e 0a 2a  .** that UUID..*
9bd0: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 79  /.static void hy
9be0: 70 65 72 6c 69 6e 6b 55 75 69 64 46 75 6e 63 28  perlinkUuidFunc(
9bf0: 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
9c00: 78 74 20 2a 70 43 78 74 2c 20 20 20 20 20 2f 2a  xt *pCxt,     /*
9c10: 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78   function contex
9c20: 74 20 2a 2f 0a 20 20 69 6e 74 20 61 72 67 63 2c  t */.  int argc,
9c30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9c40: 20 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 20 61    /* number of a
9c50: 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20  rguments to the 
9c60: 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71  function */.  sq
9c70: 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
9c80: 67 76 20 20 20 20 20 20 20 2f 2a 20 76 61 6c 75  gv       /* valu
9c90: 65 73 20 6f 66 20 61 6c 6c 20 66 75 6e 63 74 69  es of all functi
9ca0: 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a  on arguments */.
9cb0: 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
9cc0: 2a 7a 55 75 69 64 3b 20 20 20 20 20 20 20 20 20  *zUuid;         
9cd0: 2f 2a 20 54 68 65 20 55 55 49 44 20 74 6f 20 72  /* The UUID to r
9ce0: 65 6e 64 65 72 20 2a 2f 0a 20 20 63 68 61 72 20  ender */.  char 
9cf0: 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *z;             
9d00: 20 20 20 20 20 20 2f 2a 20 52 65 6e 64 65 72 65        /* Rendere
9d10: 64 20 48 54 4d 4c 20 74 65 78 74 20 2a 2f 0a 0a  d HTML text */..
9d20: 20 20 7a 55 75 69 64 20 3d 20 28 63 6f 6e 73 74    zUuid = (const
9d30: 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76   char*)sqlite3_v
9d40: 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
9d50: 5d 29 3b 0a 20 20 69 66 28 20 67 2e 6f 6b 48 69  ]);.  if( g.okHi
9d60: 73 74 6f 72 79 20 26 26 20 7a 55 75 69 64 20 26  story && zUuid &
9d70: 26 20 73 74 72 6c 65 6e 28 7a 55 75 69 64 29 3e  & strlen(zUuid)>
9d80: 3d 31 30 20 29 7b 0a 20 20 20 20 7a 20 3d 20 6d  =10 ){.    z = m
9d90: 70 72 69 6e 74 66 28 22 3c 74 74 3e 3c 61 20 68  printf("<tt><a h
9da0: 72 65 66 3d 27 25 73 2f 69 6e 66 6f 2f 25 74 27  ref='%s/info/%t'
9db0: 3e 3c 73 70 61 6e 20 73 74 79 6c 65 3d 27 66 6f  ><span style='fo
9dc0: 6e 74 2d 73 69 7a 65 3a 31 2e 35 65 6d 27 3e 22  nt-size:1.5em'>"
9dd0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
9de0: 20 22 25 23 68 3c 2f 73 70 61 6e 3e 25 68 3c 2f   "%#h</span>%h</
9df0: 61 3e 3c 2f 74 74 3e 22 2c 0a 20 20 20 20 20 20  a></tt>",.      
9e00: 20 20 20 20 20 20 20 20 20 20 67 2e 7a 42 61 73            g.zBas
9e10: 65 55 52 4c 2c 20 7a 55 75 69 64 2c 20 31 30 2c  eURL, zUuid, 10,
9e20: 20 7a 55 75 69 64 2c 20 26 7a 55 75 69 64 5b 31   zUuid, &zUuid[1
9e30: 30 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  0]);.    sqlite3
9e40: 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43 78  _result_text(pCx
9e50: 74 2c 20 7a 2c 20 2d 31 2c 20 66 72 65 65 29 3b  t, z, -1, free);
9e60: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
9e70: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
9e80: 74 28 70 43 78 74 2c 20 7a 55 75 69 64 2c 20 2d  t(pCxt, zUuid, -
9e90: 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  1, SQLITE_TRANSI
9ea0: 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ENT);.  }.}../*.
9eb0: 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  ** SQL function 
9ec0: 74 6f 20 72 65 6e 64 65 72 20 61 20 54 41 47 49  to render a TAGI
9ed0: 44 20 61 73 20 61 20 68 79 70 65 72 6c 69 6e 6b  D as a hyperlink
9ee0: 20 74 6f 20 61 20 70 61 67 65 20 64 65 73 63 72   to a page descr
9ef0: 69 62 69 6e 67 0a 2a 2a 20 74 68 61 74 20 74 61  ibing.** that ta
9f00: 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  g..*/.static voi
9f10: 64 20 68 79 70 65 72 6c 69 6e 6b 54 61 67 69 64  d hyperlinkTagid
9f20: 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
9f30: 63 6f 6e 74 65 78 74 20 2a 70 43 78 74 2c 20 20  context *pCxt,  
9f40: 20 20 20 2f 2a 20 66 75 6e 63 74 69 6f 6e 20 63     /* function c
9f50: 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20  ontext */.  int 
9f60: 61 72 67 63 2c 20 20 20 20 20 20 20 20 20 20 20  argc,           
9f70: 20 20 20 20 20 20 20 2f 2a 20 6e 75 6d 62 65 72         /* number
9f80: 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f   of arguments to
9f90: 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f   the function */
9fa0: 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
9fb0: 20 2a 2a 61 72 67 76 20 20 20 20 20 20 20 2f 2a   **argv       /*
9fc0: 20 76 61 6c 75 65 73 20 6f 66 20 61 6c 6c 20 66   values of all f
9fd0: 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74  unction argument
9fe0: 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 74 61  s */.){.  int ta
9ff0: 67 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  gid;            
a000: 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 67 69       /* The tagi
a010: 64 20 74 6f 20 72 65 6e 64 65 72 20 2a 2f 0a 20  d to render */. 
a020: 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20   char *z;       
a030: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 72              /* r
a040: 65 6e 64 65 72 65 64 20 68 74 6d 6c 20 74 65 78  endered html tex
a050: 74 20 2a 2f 0a 0a 20 20 74 61 67 69 64 20 3d 20  t */..  tagid = 
a060: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
a070: 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66  t(argv[0]);.  if
a080: 28 20 67 2e 6f 6b 48 69 73 74 6f 72 79 20 29 7b  ( g.okHistory ){
a090: 0a 20 20 20 20 7a 20 3d 20 6d 70 72 69 6e 74 66  .    z = mprintf
a0a0: 28 22 3c 61 20 68 72 65 66 3d 27 25 73 2f 74 61  ("<a href='%s/ta
a0b0: 67 76 69 65 77 3f 74 61 67 69 64 3d 25 64 27 3e  gview?tagid=%d'>
a0c0: 25 64 3c 2f 61 3e 22 2c 20 0a 20 20 20 20 20 20  %d</a>", .      
a0d0: 20 20 20 20 20 20 20 20 20 20 20 20 67 2e 7a 42              g.zB
a0e0: 61 73 65 55 52 4c 2c 20 74 61 67 69 64 2c 20 74  aseURL, tagid, t
a0f0: 61 67 69 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  agid);.  }else{.
a100: 20 20 20 20 7a 20 3d 20 6d 70 72 69 6e 74 66 28      z = mprintf(
a110: 22 25 64 22 2c 20 74 61 67 69 64 29 3b 0a 20 20  "%d", tagid);.  
a120: 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  }.  sqlite3_resu
a130: 6c 74 5f 74 65 78 74 28 70 43 78 74 2c 20 7a 2c  lt_text(pCxt, z,
a140: 20 2d 31 2c 20 66 72 65 65 29 3b 0a 7d 0a 0a 2f   -1, free);.}../
a150: 2a 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  *.** SQL functio
a160: 6e 20 74 6f 20 72 65 6e 64 65 72 20 61 20 54 41  n to render a TA
a170: 47 4e 41 4d 45 20 61 73 20 61 20 68 79 70 65 72  GNAME as a hyper
a180: 6c 69 6e 6b 20 74 6f 20 61 20 70 61 67 65 20 64  link to a page d
a190: 65 73 63 72 69 62 69 6e 67 0a 2a 2a 20 74 68 61  escribing.** tha
a1a0: 74 20 74 61 67 2e 0a 2a 2f 0a 73 74 61 74 69 63  t tag..*/.static
a1b0: 20 76 6f 69 64 20 68 79 70 65 72 6c 69 6e 6b 54   void hyperlinkT
a1c0: 61 67 6e 61 6d 65 46 75 6e 63 28 0a 20 20 73 71  agnameFunc(.  sq
a1d0: 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
a1e0: 43 78 74 2c 20 20 20 20 20 2f 2a 20 66 75 6e 63  Cxt,     /* func
a1f0: 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  tion context */.
a200: 20 20 69 6e 74 20 61 72 67 63 2c 20 20 20 20 20    int argc,     
a210: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a220: 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  number of argume
a230: 6e 74 73 20 74 6f 20 74 68 65 20 66 75 6e 63 74  nts to the funct
a240: 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ion */.  sqlite3
a250: 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 20 20 20  _value **argv   
a260: 20 20 20 20 2f 2a 20 76 61 6c 75 65 73 20 6f 66      /* values of
a270: 20 61 6c 6c 20 66 75 6e 63 74 69 6f 6e 20 61 72   all function ar
a280: 67 75 6d 65 6e 74 73 20 2a 2f 0a 29 7b 0a 20 20  guments */.){.  
a290: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 67  const char *zTag
a2a0: 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ;          /* Th
a2b0: 65 20 74 61 67 20 74 6f 20 72 65 6e 64 65 72 20  e tag to render 
a2c0: 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20 20 20  */.  char *z;   
a2d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2e0: 2f 2a 20 72 65 6e 64 65 72 65 64 20 68 74 6d 6c  /* rendered html
a2f0: 20 74 65 78 74 20 2a 2f 0a 0a 20 20 7a 54 61 67   text */..  zTag
a300: 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29   = (const char*)
a310: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
a320: 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69  xt(argv[0]);.  i
a330: 66 28 20 67 2e 6f 6b 48 69 73 74 6f 72 79 20 29  f( g.okHistory )
a340: 7b 0a 20 20 20 20 7a 20 3d 20 6d 70 72 69 6e 74  {.    z = mprint
a350: 66 28 22 3c 61 20 68 72 65 66 3d 27 25 73 2f 74  f("<a href='%s/t
a360: 61 67 76 69 65 77 3f 6e 61 6d 65 3d 25 54 26 72  agview?name=%T&r
a370: 61 77 3d 79 27 3e 25 68 3c 2f 61 3e 22 2c 20 0a  aw=y'>%h</a>", .
a380: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a390: 20 20 67 2e 7a 42 61 73 65 55 52 4c 2c 20 7a 54    g.zBaseURL, zT
a3a0: 61 67 2c 20 7a 54 61 67 29 3b 0a 20 20 7d 65 6c  ag, zTag);.  }el
a3b0: 73 65 7b 0a 20 20 20 20 7a 20 3d 20 6d 70 72 69  se{.    z = mpri
a3c0: 6e 74 66 28 22 25 68 22 2c 20 7a 54 61 67 29 3b  ntf("%h", zTag);
a3d0: 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72  .  }.  sqlite3_r
a3e0: 65 73 75 6c 74 5f 74 65 78 74 28 70 43 78 74 2c  esult_text(pCxt,
a3f0: 20 7a 2c 20 2d 31 2c 20 66 72 65 65 29 3b 0a 7d   z, -1, free);.}
a400: 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 20 66 75 6e 63  ../*.** SQL func
a410: 74 69 6f 6e 20 74 6f 20 65 73 63 61 70 65 20 61  tion to escape a
a420: 6c 6c 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  ll characters in
a430: 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 68   a string that h
a440: 61 76 65 20 73 70 65 63 69 61 6c 0a 2a 2a 20 6d  ave special.** m
a450: 65 61 6e 69 6e 67 20 74 6f 20 48 54 4d 4c 2e 0a  eaning to HTML..
a460: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68  */.static void h
a470: 74 6d 6c 69 7a 65 46 75 6e 63 28 0a 20 20 73 71  tmlizeFunc(.  sq
a480: 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
a490: 43 78 74 2c 20 20 20 20 20 2f 2a 20 66 75 6e 63  Cxt,     /* func
a4a0: 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  tion context */.
a4b0: 20 20 69 6e 74 20 61 72 67 63 2c 20 20 20 20 20    int argc,     
a4c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a4d0: 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  number of argume
a4e0: 6e 74 73 20 74 6f 20 74 68 65 20 66 75 6e 63 74  nts to the funct
a4f0: 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ion */.  sqlite3
a500: 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 20 20 20  _value **argv   
a510: 20 20 20 20 2f 2a 20 76 61 6c 75 65 73 20 6f 66      /* values of
a520: 20 61 6c 6c 20 66 75 6e 63 74 69 6f 6e 20 61 72   all function ar
a530: 67 75 6d 65 6e 74 73 20 2a 2f 0a 29 7b 0a 20 20  guments */.){.  
a540: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 65 78  const char *zTex
a550: 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65  t;         /* Te
a560: 78 74 20 74 6f 20 62 65 20 68 74 6d 6c 69 7a 65  xt to be htmlize
a570: 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20  d */.  char *z; 
a580: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a590: 20 20 2f 2a 20 72 65 6e 64 65 72 65 64 20 68 74    /* rendered ht
a5a0: 6d 6c 20 74 65 78 74 20 2a 2f 0a 0a 20 20 7a 54  ml text */..  zT
a5b0: 65 78 74 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  ext = (const cha
a5c0: 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  r*)sqlite3_value
a5d0: 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
a5e0: 20 20 7a 20 3d 20 68 74 6d 6c 69 7a 65 28 7a 54    z = htmlize(zT
a5f0: 65 78 74 2c 20 2d 31 29 3b 0a 20 20 73 71 6c 69  ext, -1);.  sqli
a600: 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
a610: 70 43 78 74 2c 20 7a 2c 20 2d 31 2c 20 66 72 65  pCxt, z, -1, fre
a620: 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  e);.}../*.** Thi
a630: 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 68  s routine is a h
a640: 65 6c 70 65 72 20 74 6f 20 72 75 6e 20 61 6e 20  elper to run an 
a650: 53 51 4c 20 71 75 65 72 79 20 61 6e 64 20 74 61  SQL query and ta
a660: 62 6c 65 2d 69 7a 65 20 74 68 65 0a 2a 2a 20 72  ble-ize the.** r
a670: 65 73 75 6c 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  esults..**.** Th
a680: 65 20 7a 53 71 6c 20 70 61 72 61 6d 65 74 65 72  e zSql parameter
a690: 20 73 68 6f 75 6c 64 20 62 65 20 61 20 73 69 6e   should be a sin
a6a0: 67 6c 65 2c 20 63 6f 6d 70 6c 65 74 65 20 53 51  gle, complete SQ
a6b0: 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20  L statement..** 
a6c0: 54 61 62 6c 65 69 7a 65 64 20 6f 75 74 70 75 74  Tableized output
a6d0: 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61 74   of the SQL stat
a6e0: 65 6d 65 6e 74 20 69 73 20 72 65 6e 64 65 72 65  ement is rendere
a6f0: 64 20 62 61 63 6b 20 74 6f 20 74 68 65 20 63 6c  d back to the cl
a700: 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ient..**.** The 
a710: 69 73 53 61 66 65 20 66 6c 61 67 20 69 73 20 74  isSafe flag is t
a720: 72 75 65 20 69 66 20 61 6c 6c 20 71 75 65 72 79  rue if all query
a730: 20 72 65 73 75 6c 74 73 20 68 61 76 65 20 62 65   results have be
a740: 65 6e 20 70 72 6f 63 65 73 73 65 64 20 0a 2a 2a  en processed .**
a750: 20 62 79 20 72 6f 75 74 69 6e 65 73 20 73 75 63   by routines suc
a760: 68 20 61 73 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  h as.**.**      
a770: 20 20 6c 69 6e 6b 75 75 69 64 28 29 0a 2a 2a 20    linkuuid().** 
a780: 20 20 20 20 20 20 20 6c 69 6e 6b 74 61 67 69 64         linktagid
a790: 28 29 0a 2a 2a 20 20 20 20 20 20 20 20 6c 69 6e  ().**        lin
a7a0: 6b 74 61 67 6e 61 6d 65 28 29 0a 2a 2a 20 20 20  ktagname().**   
a7b0: 20 20 20 20 20 68 74 6d 6c 69 7a 65 28 29 0a 2a       htmlize().*
a7c0: 2a 0a 2a 2a 20 61 6e 64 20 61 72 65 20 74 68 65  *.** and are the
a7d0: 72 65 66 6f 72 65 20 73 61 66 65 20 66 6f 72 20  refore safe for 
a7e0: 64 69 72 65 63 74 20 72 65 6e 64 65 72 69 6e 67  direct rendering
a7f0: 2e 20 20 49 66 20 69 73 53 61 66 65 20 69 73 20  .  If isSafe is 
a800: 66 61 6c 73 65 2c 0a 2a 2a 20 74 68 65 6e 20 61  false,.** then a
a810: 6c 6c 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  ll characters in
a820: 20 74 68 65 20 71 75 65 72 79 20 72 65 73 75 6c   the query resul
a830: 74 20 74 68 61 74 20 68 61 76 65 20 73 70 65 63  t that have spec
a840: 69 61 6c 20 6d 65 61 6e 69 6e 67 0a 2a 2a 20 74  ial meaning.** t
a850: 6f 20 48 54 4d 4c 20 61 72 65 20 65 73 63 61 70  o HTML are escap
a860: 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  ed..**.** Return
a870: 73 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  s SQLITE_OK on s
a880: 75 63 63 65 73 73 20 61 6e 64 20 61 6e 79 20 6f  uccess and any o
a890: 74 68 65 72 20 76 61 6c 75 65 20 6f 6e 20 65 72  ther value on er
a8a0: 72 6f 72 2e 0a 2a 2f 0a 69 6e 74 20 64 62 5f 67  ror..*/.int db_g
a8b0: 65 6e 65 72 69 63 5f 71 75 65 72 79 5f 76 69 65  eneric_query_vie
a8c0: 77 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53  w(const char *zS
a8d0: 71 6c 2c 20 69 6e 74 20 69 73 53 61 66 65 29 7b  ql, int isSafe){
a8e0: 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
a8f0: 2a 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 72 63  *pStmt;.  int rc
a900: 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 2c 20 69 3b  ;.  int nCol, i;
a910: 0a 20 20 69 6e 74 20 6e 52 6f 77 3b 0a 20 20 63  .  int nRow;.  c
a920: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 6f 77 3b  onst char *zRow;
a930: 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 6f 6e  .  static int on
a940: 63 65 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 49 6e  ce = 1;..  /* In
a950: 73 74 61 6c 6c 20 74 68 65 20 73 70 65 63 69 61  stall the specia
a960: 6c 20 66 75 6e 63 74 69 6f 6e 73 20 6f 6e 20 74  l functions on t
a970: 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f  he first call to
a980: 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 2a 2f   this routine */
a990: 0a 20 20 69 66 28 20 6f 6e 63 65 20 29 7b 0a 20  .  if( once ){. 
a9a0: 20 20 20 6f 6e 63 65 20 3d 20 30 3b 0a 20 20 20     once = 0;.   
a9b0: 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
a9c0: 66 75 6e 63 74 69 6f 6e 28 67 2e 64 62 2c 20 22  function(g.db, "
a9d0: 6c 69 6e 6b 75 75 69 64 22 2c 20 31 2c 20 53 51  linkuuid", 1, SQ
a9e0: 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 0a 20  LITE_UTF8, 0, . 
a9f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aa00: 20 20 20 20 20 20 20 20 20 20 20 68 79 70 65 72             hyper
aa10: 6c 69 6e 6b 55 75 69 64 46 75 6e 63 2c 20 30 2c  linkUuidFunc, 0,
aa20: 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
aa30: 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
aa40: 28 67 2e 64 62 2c 20 22 6c 69 6e 6b 74 61 67 69  (g.db, "linktagi
aa50: 64 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 55 54  d", 1, SQLITE_UT
aa60: 46 38 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20  F8, 0, .        
aa70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aa80: 20 20 20 20 68 79 70 65 72 6c 69 6e 6b 54 61 67      hyperlinkTag
aa90: 69 64 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 20  idFunc, 0, 0);. 
aaa0: 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
aab0: 65 5f 66 75 6e 63 74 69 6f 6e 28 67 2e 64 62 2c  e_function(g.db,
aac0: 20 22 6c 69 6e 6b 74 61 67 6e 61 6d 65 22 2c 20   "linktagname", 
aad0: 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  1, SQLITE_UTF8, 
aae0: 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  0, .            
aaf0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ab00: 68 79 70 65 72 6c 69 6e 6b 54 61 67 6e 61 6d 65  hyperlinkTagname
ab10: 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Func, 0, 0);.   
ab20: 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
ab30: 66 75 6e 63 74 69 6f 6e 28 67 2e 64 62 2c 20 22  function(g.db, "
ab40: 68 74 6d 6c 69 7a 65 22 2c 20 31 2c 20 53 51 4c  htmlize", 1, SQL
ab50: 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 0a 20 20  ITE_UTF8, 0, .  
ab60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ab70: 20 20 20 20 20 20 20 20 20 20 68 74 6d 6c 69 7a            htmliz
ab80: 65 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 20 20  eFunc, 0, 0);.  
ab90: 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 55 73 65  }..  /*.  ** Use
aba0: 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 64 69   sqlite3_stmt di
abb0: 72 65 63 74 6c 79 20 72 61 74 68 65 72 20 74 68  rectly rather th
abc0: 61 6e 20 67 6f 69 6e 67 20 74 68 72 6f 75 67 68  an going through
abd0: 20 64 62 5f 70 72 65 70 61 72 65 28 29 2c 0a 20   db_prepare(),. 
abe0: 20 2a 2a 20 73 6f 20 74 68 61 74 20 77 65 20 63   ** so that we c
abf0: 61 6e 20 74 72 65 61 74 20 65 72 72 6f 72 73 20  an treat errors 
ac00: 61 20 6e 6f 6e 2d 66 61 74 61 6c 2e 0a 20 20 2a  a non-fatal..  *
ac10: 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  /.  rc = sqlite3
ac20: 5f 70 72 65 70 61 72 65 28 67 2e 64 62 2c 20 7a  _prepare(g.db, z
ac30: 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c  Sql, -1, &pStmt,
ac40: 20 30 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54   0);.  if( SQLIT
ac50: 45 5f 4f 4b 20 21 3d 20 72 63 20 29 7b 0a 20 20  E_OK != rc ){.  
ac60: 20 20 40 20 3c 73 70 61 6e 20 73 74 79 6c 65 3d    @ <span style=
ac70: 27 63 6f 6c 6f 72 3a 72 65 64 27 3e 64 62 5f 67  'color:red'>db_g
ac80: 65 6e 65 72 69 63 5f 71 75 65 72 79 5f 76 69 65  eneric_query_vie
ac90: 77 28 29 20 53 51 4c 20 65 72 72 6f 72 3a 0a 20  w() SQL error:. 
aca0: 20 20 20 40 20 25 68 28 73 71 6c 69 74 65 33 5f     @ %h(sqlite3_
acb0: 65 72 72 6d 73 67 28 67 2e 64 62 29 29 3c 2f 73  errmsg(g.db))</s
acc0: 70 61 6e 3e 0a 20 20 20 20 72 65 74 75 72 6e 20  pan>.    return 
acd0: 72 63 3b 0a 20 20 7d 0a 20 20 6e 43 6f 6c 20 3d  rc;.  }.  nCol =
ace0: 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
acf0: 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20 20  count(pStmt);.  
ad00: 40 20 3c 74 61 62 6c 65 20 63 6c 61 73 73 3d 27  @ <table class='
ad10: 66 6f 73 73 69 6c 5f 64 62 5f 67 65 6e 65 72 69  fossil_db_generi
ad20: 63 5f 71 75 65 72 79 5f 76 69 65 77 27 3e 3c 74  c_query_view'><t
ad30: 62 6f 64 79 3e 0a 20 20 40 20 3c 74 72 20 63 6c  body>.  @ <tr cl
ad40: 61 73 73 3d 27 68 65 61 64 65 72 27 3e 0a 20 20  ass='header'>.  
ad50: 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b  for(i=0; i<nCol;
ad60: 20 2b 2b 69 29 7b 0a 20 20 20 20 40 20 3c 74 64   ++i){.    @ <td
ad70: 3e 25 68 28 73 71 6c 69 74 65 33 5f 63 6f 6c 75  >%h(sqlite3_colu
ad80: 6d 6e 5f 6e 61 6d 65 28 70 53 74 6d 74 2c 69 29  mn_name(pStmt,i)
ad90: 29 3c 2f 74 64 3e 0a 20 20 7d 0a 20 20 40 20 3c  )</td>.  }.  @ <
ada0: 2f 74 72 3e 0a 0a 20 20 6e 52 6f 77 20 3d 20 30  /tr>..  nRow = 0
adb0: 3b 0a 20 20 77 68 69 6c 65 28 20 53 51 4c 49 54  ;.  while( SQLIT
adc0: 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73  E_ROW==sqlite3_s
add0: 74 65 70 28 70 53 74 6d 74 29 20 29 7b 0a 20 20  tep(pStmt) ){.  
ade0: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 7a    const char *az
adf0: 43 6c 61 73 73 5b 5d 20 3d 20 7b 20 22 65 76 65  Class[] = { "eve
ae00: 6e 22 2c 20 22 6f 64 64 22 20 7d 3b 0a 20 20 20  n", "odd" };.   
ae10: 20 40 20 3c 74 72 20 63 6c 61 73 73 3d 27 25 73   @ <tr class='%s
ae20: 28 61 7a 43 6c 61 73 73 5b 28 6e 52 6f 77 2b 2b  (azClass[(nRow++
ae30: 29 26 31 5d 29 27 3e 0a 20 20 20 20 20 20 66 6f  )&1])'>.      fo
ae40: 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69  r(i=0; i<nCol; i
ae50: 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 7a 52 6f  ++){.        zRo
ae60: 77 20 3d 20 28 63 68 61 72 20 63 6f 6e 73 74 2a  w = (char const*
ae70: 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  )sqlite3_column_
ae80: 74 65 78 74 28 70 53 74 6d 74 2c 69 29 3b 0a 20  text(pStmt,i);. 
ae90: 20 20 20 20 20 20 20 69 66 28 20 69 73 53 61 66         if( isSaf
aea0: 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 40  e ){.          @
aeb0: 20 3c 74 64 3e 25 73 28 7a 52 6f 77 29 3c 2f 74   <td>%s(zRow)</t
aec0: 64 3e 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  d>.        }else
aed0: 7b 0a 20 20 20 20 20 20 20 20 20 20 40 20 3c 74  {.          @ <t
aee0: 64 3e 25 68 28 7a 52 6f 77 29 3c 2f 74 64 3e 0a  d>%h(zRow)</td>.
aef0: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
af00: 7d 0a 20 20 20 20 40 20 3c 2f 74 72 3e 0a 20 20  }.    @ </tr>.  
af10: 7d 0a 20 20 40 20 3c 2f 74 62 6f 64 79 3e 3c 2f  }.  @ </tbody></
af20: 74 61 62 6c 65 3e 0a 20 20 73 71 6c 69 74 65 33  table>.  sqlite3
af30: 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29  _finalize(pStmt)
af40: 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
af50: 45 5f 4f 4b 3b 0a 7d 0a                          E_OK;.}.