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 37 20 44 2e 20 52 69 63 68 (c) 2007 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 20 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c ** .** You shoul
01f0: 64 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20 d have received
0200: 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e a copy of the GN
0210: 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63 U General Public
0220: 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e .** License alon
0230: 67 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72 g with this libr
0240: 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69 ary; if not, wri
0250: 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65 te to the.** Fre
0260: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 e Software Found
0270: 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20 ation, Inc., 59
0280: 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53 Temple Place - S
0290: 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73 uite 330,.** Bos
02a0: 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31 ton, MA 02111-1
02b0: 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20 307, USA..**.**
02c0: 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69 Author contact i
02d0: 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20 nformation:.**
02e0: 20 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a drh@hwaci.com.*
02f0: 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68 * http://www.h
0300: 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a waci.com/drh/.**
0310: 0a 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 2a ****************
0360: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
0370: 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 contains code u
0380: 73 65 64 20 74 6f 20 6d 61 6e 61 67 65 20 74 61 sed to manage ta
0390: 67 73 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 22 gs.*/.#include "
03a0: 63 6f 6e 66 69 67 2e 68 22 0a 23 69 6e 63 6c 75 config.h".#inclu
03b0: 64 65 20 22 74 61 67 2e 68 22 0a 23 69 6e 63 6c de "tag.h".#incl
03c0: 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e 0a 0a ude <assert.h>..
03d0: 2f 2a 0a 2a 2a 20 50 72 6f 70 61 67 61 74 65 20 /*.** Propagate
03e0: 74 68 65 20 74 61 67 20 67 69 76 65 6e 20 62 79 the tag given by
03f0: 20 74 61 67 69 64 20 74 6f 20 74 68 65 20 63 68 tagid to the ch
0400: 69 6c 64 72 65 6e 20 6f 66 20 70 69 64 2e 0a 2a ildren of pid..*
0410: 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
0420: 65 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 e assumes that t
0430: 61 67 69 64 20 69 73 20 61 20 74 61 67 20 74 68 agid is a tag th
0440: 61 74 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 at should be.**
0450: 70 72 6f 70 61 67 61 74 65 64 20 61 6e 64 20 74 propagated and t
0460: 68 61 74 20 74 68 65 20 74 61 67 20 69 73 20 61 hat the tag is a
0470: 6c 72 65 61 64 79 20 70 72 65 73 65 6e 74 20 69 lready present i
0480: 6e 20 70 69 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 n pid..**.** If
0490: 61 64 64 46 6c 61 67 20 69 73 20 74 72 75 65 20 addFlag is true
04a0: 74 68 65 6e 20 74 68 65 20 74 61 67 20 69 73 20 then the tag is
04b0: 61 64 64 65 64 2e 20 20 49 66 20 66 61 6c 73 65 added. If false
04c0: 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 74 61 , then the.** ta
04d0: 67 20 69 73 20 72 65 6d 6f 76 65 64 2e 0a 2a 2f g is removed..*/
04e0: 0a 76 6f 69 64 20 74 61 67 5f 70 72 6f 70 61 67 .void tag_propag
04f0: 61 74 65 28 0a 20 20 69 6e 74 20 70 69 64 2c 20 ate(. int pid,
0500: 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 70 61 /* Propa
0510: 67 61 74 65 20 74 68 65 20 74 61 67 20 74 6f 20 gate the tag to
0520: 63 68 69 6c 64 72 65 6e 20 6f 66 20 74 68 69 73 children of this
0530: 20 6e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 74 node */. int t
0540: 61 67 69 64 2c 20 20 20 20 20 20 20 2f 2a 20 54 agid, /* T
0550: 61 67 20 74 6f 20 70 72 6f 70 61 67 61 74 65 20 ag to propagate
0560: 2a 2f 0a 20 20 69 6e 74 20 61 64 64 46 6c 61 67 */. int addFlag
0570: 2c 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f , /* True to
0580: 20 61 64 64 20 74 68 65 20 74 61 67 2e 20 46 61 add the tag. Fa
0590: 6c 73 65 20 74 6f 20 64 65 6c 65 74 65 20 69 74 lse to delete it
05a0: 2e 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 6d 74 . */. double mt
05b0: 69 6d 65 20 20 20 20 20 2f 2a 20 54 69 6d 65 73 ime /* Times
05c0: 74 61 6d 70 20 6f 6e 20 74 68 65 20 74 61 67 20 tamp on the tag
05d0: 2a 2f 0a 29 7b 0a 20 20 50 51 75 65 75 65 20 71 */.){. PQueue q
05e0: 75 65 75 65 3b 0a 20 20 53 74 6d 74 20 73 2c 20 ueue;. Stmt s,
05f0: 69 6e 73 3b 0a 20 20 70 71 75 65 75 65 5f 69 6e ins;. pqueue_in
0600: 69 74 28 26 71 75 65 75 65 29 3b 0a 20 20 70 71 it(&queue);. pq
0610: 75 65 75 65 5f 69 6e 73 65 72 74 28 26 71 75 65 ueue_insert(&que
0620: 75 65 2c 20 70 69 64 2c 20 30 2e 30 29 3b 0a 20 ue, pid, 0.0);.
0630: 20 64 62 5f 70 72 65 70 61 72 65 28 26 73 2c 20 db_prepare(&s,
0640: 0a 20 20 20 20 20 22 53 45 4c 45 43 54 20 63 69 . "SELECT ci
0650: 64 2c 20 6d 74 69 6d 65 2c 20 63 6f 61 6c 65 73 d, mtime, coales
0660: 63 65 28 73 72 63 69 64 3d 30 20 41 4e 44 20 6d ce(srcid=0 AND m
0670: 74 69 6d 65 3c 3a 6d 74 69 6d 65 2c 20 25 64 29 time<:mtime, %d)
0680: 20 41 53 20 64 6f 69 74 22 0a 20 20 20 20 20 22 AS doit". "
0690: 20 20 46 52 4f 4d 20 70 6c 69 6e 6b 20 4c 45 46 FROM plink LEF
06a0: 54 20 4a 4f 49 4e 20 74 61 67 78 72 65 66 20 4f T JOIN tagxref O
06b0: 4e 20 63 69 64 3d 72 69 64 20 41 4e 44 20 74 61 N cid=rid AND ta
06c0: 67 69 64 3d 25 64 22 0a 20 20 20 20 20 22 20 57 gid=%d". " W
06d0: 48 45 52 45 20 70 69 64 3d 3a 70 69 64 20 41 4e HERE pid=:pid AN
06e0: 44 20 69 73 70 72 69 6d 22 2c 0a 20 20 20 20 20 D isprim",.
06f0: 61 64 64 46 6c 61 67 2c 20 74 61 67 69 64 0a 20 addFlag, tagid.
0700: 20 29 3b 0a 20 20 64 62 5f 62 69 6e 64 5f 64 6f );. db_bind_do
0710: 75 62 6c 65 28 26 73 2c 20 22 3a 6d 74 69 6d 65 uble(&s, ":mtime
0720: 22 2c 20 6d 74 69 6d 65 29 3b 0a 20 20 69 66 28 ", mtime);. if(
0730: 20 61 64 64 46 6c 61 67 20 29 7b 0a 20 20 20 20 addFlag ){.
0740: 64 62 5f 70 72 65 70 61 72 65 28 26 69 6e 73 2c db_prepare(&ins,
0750: 0a 20 20 20 20 20 20 20 22 52 45 50 4c 41 43 45 . "REPLACE
0760: 20 49 4e 54 4f 20 74 61 67 78 72 65 66 28 74 61 INTO tagxref(ta
0770: 67 69 64 2c 20 61 64 64 46 6c 61 67 2c 20 73 72 gid, addFlag, sr
0780: 63 69 64 2c 20 6d 74 69 6d 65 2c 20 72 69 64 29 cid, mtime, rid)
0790: 22 0a 20 20 20 20 20 20 20 22 56 41 4c 55 45 53 ". "VALUES
07a0: 28 25 64 2c 31 2c 30 2c 3a 6d 74 69 6d 65 2c 3a (%d,1,0,:mtime,:
07b0: 72 69 64 29 22 2c 0a 20 20 20 20 20 20 20 74 61 rid)",. ta
07c0: 67 69 64 0a 20 20 20 20 29 3b 0a 20 20 20 20 64 gid. );. d
07d0: 62 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 28 26 69 b_bind_double(&i
07e0: 6e 73 2c 20 22 3a 6d 74 69 6d 65 22 2c 20 6d 74 ns, ":mtime", mt
07f0: 69 6d 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ime);. }else{.
0800: 20 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 69 db_prepare(&i
0810: 6e 73 2c 0a 20 20 20 20 20 20 20 22 44 45 4c 45 ns,. "DELE
0820: 54 45 20 46 52 4f 4d 20 74 61 67 78 72 65 66 20 TE FROM tagxref
0830: 57 48 45 52 45 20 74 61 67 69 64 3d 25 64 20 41 WHERE tagid=%d A
0840: 4e 44 20 72 69 64 3d 3a 72 69 64 22 2c 20 74 61 ND rid=:rid", ta
0850: 67 69 64 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 20 gid. );. }.
0860: 20 77 68 69 6c 65 28 20 28 70 69 64 20 3d 20 70 while( (pid = p
0870: 71 75 65 75 65 5f 65 78 74 72 61 63 74 28 26 71 queue_extract(&q
0880: 75 65 75 65 29 29 21 3d 30 20 29 7b 0a 20 20 20 ueue))!=0 ){.
0890: 20 64 62 5f 62 69 6e 64 5f 69 6e 74 28 26 73 2c db_bind_int(&s,
08a0: 20 22 3a 70 69 64 22 2c 20 70 69 64 29 3b 0a 20 ":pid", pid);.
08b0: 20 20 20 77 68 69 6c 65 28 20 64 62 5f 73 74 65 while( db_ste
08c0: 70 28 26 73 29 3d 3d 53 51 4c 49 54 45 5f 52 4f p(&s)==SQLITE_RO
08d0: 57 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 W ){. int d
08e0: 6f 69 74 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f oit = db_column_
08f0: 69 6e 74 28 26 73 2c 20 32 29 3b 0a 20 20 20 20 int(&s, 2);.
0900: 20 20 69 66 28 20 64 6f 69 74 20 29 7b 0a 20 20 if( doit ){.
0910: 20 20 20 20 20 20 69 6e 74 20 63 69 64 20 3d 20 int cid =
0920: 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 73 db_column_int(&s
0930: 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 64 6f , 0);. do
0940: 75 62 6c 65 20 6d 74 69 6d 65 20 3d 20 64 62 5f uble mtime = db_
0950: 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 26 73 column_double(&s
0960: 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 70 71 , 1);. pq
0970: 75 65 75 65 5f 69 6e 73 65 72 74 28 26 71 75 65 ueue_insert(&que
0980: 75 65 2c 20 63 69 64 2c 20 6d 74 69 6d 65 29 3b ue, cid, mtime);
0990: 0a 20 20 20 20 20 20 20 20 64 62 5f 62 69 6e 64 . db_bind
09a0: 5f 69 6e 74 28 26 69 6e 73 2c 20 22 3a 72 69 64 _int(&ins, ":rid
09b0: 22 2c 20 63 69 64 29 3b 0a 20 20 20 20 20 20 20 ", cid);.
09c0: 20 64 62 5f 73 74 65 70 28 26 69 6e 73 29 3b 0a db_step(&ins);.
09d0: 20 20 20 20 20 20 20 20 64 62 5f 72 65 73 65 74 db_reset
09e0: 28 26 69 6e 73 29 3b 0a 20 20 20 20 20 20 7d 0a (&ins);. }.
09f0: 20 20 20 20 7d 0a 20 20 20 20 64 62 5f 72 65 73 }. db_res
0a00: 65 74 28 26 73 29 3b 0a 20 20 7d 0a 20 20 70 71 et(&s);. }. pq
0a10: 75 65 75 65 5f 63 6c 65 61 72 28 26 71 75 65 75 ueue_clear(&queu
0a20: 65 29 3b 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a e);. db_finaliz
0a30: 65 28 26 69 6e 73 29 3b 0a 20 20 64 62 5f 66 69 e(&ins);. db_fi
0a40: 6e 61 6c 69 7a 65 28 26 73 29 3b 0a 7d 0a 0a 2f nalize(&s);.}../
0a50: 2a 0a 2a 2a 20 50 72 6f 70 61 67 61 74 65 20 61 *.** Propagate a
0a60: 6c 6c 20 70 72 6f 70 61 67 61 74 61 62 6c 65 20 ll propagatable
0a70: 74 61 67 73 20 69 6e 20 70 69 64 20 74 6f 20 69 tags in pid to i
0a80: 74 73 20 63 68 69 6c 64 72 65 6e 2e 0a 2a 2f 0a ts children..*/.
0a90: 76 6f 69 64 20 74 61 67 5f 70 72 6f 70 61 67 61 void tag_propaga
0aa0: 74 65 5f 61 6c 6c 28 69 6e 74 20 70 69 64 29 7b te_all(int pid){
0ab0: 0a 20 20 53 74 6d 74 20 71 3b 0a 20 20 64 62 5f . Stmt q;. db_
0ac0: 70 72 65 70 61 72 65 28 26 71 2c 0a 20 20 20 20 prepare(&q,.
0ad0: 20 22 53 45 4c 45 43 54 20 74 61 67 69 64 2c 20 "SELECT tagid,
0ae0: 61 64 64 66 6c 61 67 2c 20 6d 74 69 6d 65 20 46 addflag, mtime F
0af0: 52 4f 4d 20 74 61 67 78 72 65 66 22 0a 20 20 20 ROM tagxref".
0b00: 20 20 22 20 57 48 45 52 45 20 72 69 64 3d 25 64 " WHERE rid=%d
0b10: 22 0a 20 20 20 20 20 22 20 20 20 41 4e 44 20 28 ". " AND (
0b20: 53 45 4c 45 43 54 20 74 61 67 6e 61 6d 65 20 46 SELECT tagname F
0b30: 52 4f 4d 20 74 61 67 20 57 48 45 52 45 20 74 61 ROM tag WHERE ta
0b40: 67 69 64 3d 74 61 67 78 72 65 66 2e 74 61 67 69 gid=tagxref.tagi
0b50: 64 29 20 4c 49 4b 45 20 27 62 72 25 27 22 2c 0a d) LIKE 'br%'",.
0b60: 20 20 20 20 20 70 69 64 0a 20 20 29 3b 0a 20 20 pid. );.
0b70: 77 68 69 6c 65 28 20 64 62 5f 73 74 65 70 28 26 while( db_step(&
0b80: 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 q)==SQLITE_ROW )
0b90: 7b 0a 20 20 20 20 69 6e 74 20 74 61 67 69 64 20 {. int tagid
0ba0: 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 = db_column_int(
0bb0: 26 71 2c 20 30 29 3b 0a 20 20 20 20 69 6e 74 20 &q, 0);. int
0bc0: 61 64 64 66 6c 61 67 20 3d 20 64 62 5f 63 6f 6c addflag = db_col
0bd0: 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 31 29 3b 0a umn_int(&q, 1);.
0be0: 20 20 20 20 64 6f 75 62 6c 65 20 6d 74 69 6d 65 double mtime
0bf0: 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 = db_column_dou
0c00: 62 6c 65 28 26 71 2c 20 32 29 3b 0a 20 20 20 20 ble(&q, 2);.
0c10: 74 61 67 5f 70 72 6f 70 61 67 61 74 65 28 70 69 tag_propagate(pi
0c20: 64 2c 20 74 61 67 69 64 2c 20 61 64 64 66 6c 61 d, tagid, addfla
0c30: 67 2c 20 6d 74 69 6d 65 29 3b 0a 20 20 7d 0a 20 g, mtime);. }.
0c40: 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 db_finalize(&q)
0c50: 3b 0a 7d 0a ;.}.