Commit 66be4ac7 authored by dave griffiths's avatar dave griffiths
Browse files

pack changes and sql indices

parent ff0ecb6a
...@@ -37,22 +37,45 @@ ...@@ -37,22 +37,45 @@
(define (upgrade-table db name) (define (upgrade-table db name)
(db-exec db (string-append "alter table " name " add version integer"))) (db-exec db (string-append "alter table " name " add version integer"))
(db-exec db (string-append "create index if not exists index_" table "_entity on " table "_entity (unique_id)"))
(db-exec db (string-append "create index if not exists index_" table "_attribute on " table "_attribute (entity_type)"))
(db-exec db (string-append "create index if not exists index_" table "_value_varchar on " table "_value_varchar (entity_id,attribute_id)"))
(db-exec db (string-append "create index if not exists index_" table "_value_int on " table "_value_int (entity_id,attribute_id)"))
(db-exec db (string-append "create index if not exists index_" table "_value_real on " table "_value_real (entity_id,attribute_id)"))
(db-exec db (string-append "create index if not exists index_" table "_value_file on " table "_value_file (entity_id,attribute_id)"))
)
;; create eav tables (add types as required) ;; create eav tables (add types as required)
(define (setup db table) (define (setup db table)
(msg "db setup") (msg "db setup")
(db-exec db (string-append "create table " table "_entity ( entity_id integer primary key autoincrement, entity_type varchar(256), unique_id varchar(256), dirty integer, version integer)")) (db-exec db (string-append "create table " table "_entity ( entity_id integer primary key autoincrement, entity_type varchar(256), unique_id varchar(256), dirty integer, version integer)"))
(db-exec db (string-append "create index if not exists index_" table "_entity on " table "_entity (unique_id)"))
(db-exec db (string-append "create table " table "_attribute ( id integer primary key autoincrement, attribute_id varchar(256), entity_type varchar(256), attribute_type varchar(256))")) (db-exec db (string-append "create table " table "_attribute ( id integer primary key autoincrement, attribute_id varchar(256), entity_type varchar(256), attribute_type varchar(256))"))
(db-exec db (string-append "create index if not exists index_" table "_attribute on " table "_attribute (entity_type)"))
(db-exec db (string-append "create table " table "_value_varchar ( id integer primary key autoincrement, entity_id integer, attribute_id varchar(255), value varchar(4096), dirty integer, version integer)")) (db-exec db (string-append "create table " table "_value_varchar ( id integer primary key autoincrement, entity_id integer, attribute_id varchar(255), value varchar(4096), dirty integer, version integer)"))
(upgrade-table db (string-append table "_value_varchar")) (upgrade-table db (string-append table "_value_varchar"))
(db-exec db (string-append "create index if not exists index_" table "_value_varchar on " table "_value_varchar (entity_id,attribute_id)"))
(db-exec db (string-append "create table " table "_value_int ( id integer primary key autoincrement, entity_id integer, attribute_id varchar(255), value integer, dirty integer, version integer)")) (db-exec db (string-append "create table " table "_value_int ( id integer primary key autoincrement, entity_id integer, attribute_id varchar(255), value integer, dirty integer, version integer)"))
(upgrade-table db (string-append table "_value_int")) (upgrade-table db (string-append table "_value_int"))
(db-exec db (string-append "create index if not exists index_" table "_value_int on " table "_value_int (entity_id,attribute_id)"))
(db-exec db (string-append "create table " table "_value_real ( id integer primary key autoincrement, entity_id integer, attribute_id varchar(255), value real, dirty integer, version integer)")) (db-exec db (string-append "create table " table "_value_real ( id integer primary key autoincrement, entity_id integer, attribute_id varchar(255), value real, dirty integer, version integer)"))
(upgrade-table db (string-append table "_value_real")) (upgrade-table db (string-append table "_value_real"))
(db-exec db (string-append "create index if not exists index_" table "_value_real on " table "_value_real (entity_id,attribute_id)"))
(db-exec db (string-append "create table " table "_value_file ( id integer primary key autoincrement, entity_id integer, attribute_id varchar(255), value varchar(4096), dirty integer, version integer)")) (db-exec db (string-append "create table " table "_value_file ( id integer primary key autoincrement, entity_id integer, attribute_id varchar(255), value varchar(4096), dirty integer, version integer)"))
(upgrade-table db (string-append table "_value_file"))) (upgrade-table db (string-append table "_value_file"))
(db-exec db (string-append "create index if not exists index_" table "_value_file on " table "_value_file (entity_id,attribute_id)"))
)
(define (validate db) (define (validate db)
......
INDIV SEX PACK BORN LITTER ID Sex Pack Birth Date Estimated birth date
DF130 F 11 11/10/2008 D0803 DM126 M 11 08-May-08
DF139 F 11 11/10/2008 D0803 DM165 M 11 14-Oct-09
DF169 F 11 12/05/2010 D1002 DM168 M 11 28-Dec-09
DF190 F 11 22/10/2011 D1107 DM170 M 11 12-May-10
DF200 F 11 16/05/2012 D1202 DM171 M 11 12-May-10
DF201 F 11 16/05/2012 D1202 DM179 M 11 23-Jul-10
DF208 F 11 16/05/2012 D1202 DM180 M 11 23-Jul-10
DF216 F 11 06/10/2012 D1204 DM189 M 11 02-Aug-11
DF222 F 11 26/06/2013 D1302 DF190 F 11 22-Oct-11
DF223 F 11 26/06/2013 D1302 DM192 M 11 22-Oct-11
DF225 F 11 26/06/2013 D1302 DM193 M 11 22-Oct-11
DF226 F 11 26/06/2013 D1302 DM194 M 11 22-Oct-11
DM126 M 11 08/05/2008 D0801 DM198 M 11 16-May-12
DM165 M 11 14/10/2009 D0904 DF200 F 11 16-May-12
DM168 M 11 28/12/2009 D0905 DF201 F 11 16-May-12
DM170 M 11 12/05/2010 D1002 DM203 M 11 16-May-12
DM171 M 11 12/05/2010 D1002 DM204 M 11 16-May-12
DM179 M 11 23/07/2010 D1003 DM206 M 11 16-May-12
DM180 M 11 23/07/2010 D1003 DF208 F 11 16-May-12
DM189 M 11 02/08/2011 D1106 DM209 M 11 16-May-12
DM192 M 11 22/10/2011 D1107 DM212 M 11 06-Oct-12
DM193 M 11 22/10/2011 D1107 DM213 M 11 06-Oct-12
DM194 M 11 22/10/2011 D1107 DM215 M 11 06-Oct-12
DM198 M 11 16/05/2012 D1202 DF216 F 11 06-Oct-12
DM202 M 11 16/05/2012 D1202 DM220 M 11 16-Apr-13
DM203 M 11 16/05/2012 D1202 DF222 F 11 26-Jun-13
DM204 M 11 16/05/2012 D1202 DF223 F 11 26-Jun-13
DM206 M 11 16/05/2012 D1202 SM009 M 17 15-Sep-08
DM209 M 11 16/05/2012 D1202 SM013 M 17 15-Sep-08
DM212 M 11 06/10/2012 D1204 SF017 F 17 01-Jan-10 Y
DM213 M 11 06/10/2012 D1204 SM018 M 17 01-Jan-10 Y
DM215 M 11 06/10/2012 D1204 SM021 M 17 11-May-11
DM220 M 11 16/04/2013 D1301 SF024 F 17 01-Jan-10 Y
DM224 M 11 26/06/2013 D1302 SF034 F 17 05-Nov-12
VM107 M 14 28/07/2009 V0904 SF038 F 17 05-Nov-12
YF011 F 14 22/08/2013 SM039 M 17 05-Nov-12
YF097 F 14 16/11/2010 Y1004 HM069 M 17 31-Oct-03
YM096 M 14 16/11/2010 Y1004 HM092 M 17 19-Apr-05
UF019 F 15 01/08/2011 U1103 UM020 M 18 01-Aug-11
VF033 F 15 26/04/2006 XM003 M 19 01-Jan-12 Y
VF137 F 15 15/10/2012 V1203 XF004 F 19 01-Jan-12 Y
VF138 F 15 27/04/2013 V1301 XF005 F 19 01-Jan-12 Y
VF139 F 15 19/08/2013 XM006 M 19 01-Jan-12 Y
VF140 F 15 19/08/2013 XM007 M 19 01-Jan-12 Y
VF141 F 15 19/08/2013 XF008 F 19 01-Jan-13 Y
VF142 F 15 19/08/2013 XM009 M 19 01-Jan-14 Y
VF143 F 15 19/08/2013 XF010 F 19 01-Jan-14 Y
VM096 M 15 09/10/2008 V0803 XF012 F 19 01-Jan-14 Y
VM123 M 15 01/11/2010 V1005 XF016 F 19 04-Apr-14
VM135 M 15 15/10/2012 V1203 XM017 M 19 04-Apr-14
VM136 M 15 15/10/2012 V1203 XM018 M 19 04-Apr-14
HM069 M 17 31/10/2003 H0304 XF020 F 19 16-Jun-14
HM092 M 17 19/04/2005 H0502 XM021 M 19 16-Jun-14
SF017 F 17 23/08/2010 XF022 F 19 16-Jun-14
SF024 F 17 13/07/2011 XM023 M 19 16-Jun-14
SF034 F 17 05/11/2012 S1205 YM024 M 19 02-May-07
SF038 F 17 05/11/2012 S1205 XM024 M 19 16-Jun-14
SF041 F 17 26/04/2013 S1301 XM025 M 19 16-Jun-14
SF045 F 17 26/04/2013 S1301 XF026 F 19 16-Jun-14
SM009 M 17 15/09/2008 S0804 YM031 M 19 25-Jun-07
SM013 M 17 15/09/2008 S0804 BM216 M 1B 27-Mar-04
SM018 M 17 23/08/2010 BM410 M 1B 15-Sep-08
SM021 M 17 11/05/2011 S1101 BM436 M 1B 25-Mar-09
SM028 M 17 09/09/2011 S1102 BM438 M 1B 25-Mar-09
SM037 M 17 05/11/2012 S1205 BM455 M 1B 20-Jun-09
SM039 M 17 05/11/2012 S1205 BF473 F 1B 22-Jan-10
UM020 M 18 01/08/2011 U1103 BF484 F 1B 07-May-10
BF473 F 1B 22/01/2010 B1001 BM500 M 1B 09-Dec-10
BF484 F 1B 07/05/2010 B1003 BF535 F 1B 02-Jul-11
BF487 F 1B 07/05/2010 B1003 BM558 M 1B 20-Apr-12
BF535 F 1B 02/07/2011 B1103 BF561 F 1B 20-Apr-12
BF561 F 1B 20/04/2012 B1202 BM564 M 1B 20-Apr-12
BF566 F 1B 20/04/2012 B1202 BF566 F 1B 20-Apr-12
BF585 F 1B 27/06/2012 B1203 BF589 F 1B 27-Jun-12
BF589 F 1B 27/06/2012 B1203 BF593 F 1B 17-Sep-12
BF593 F 1B 17/09/2012 B1204 BM620 M 1B 03-Jul-13
BF616 F 1B 03/07/2013 B1304 BF622 F 1B 03-Jul-13
BF617 F 1B 03/07/2013 B1304 BF627 F 1B 03-Jul-13
BF619 F 1B 03/07/2013 B1304 BM638 M 1B 10-Oct-13
BF621 F 1B 03/07/2013 B1304 BM643 M 1B 10-Jun-14
BF622 F 1B 03/07/2013 B1304 BF645 F 1B 10-Jun-14
BF624 F 1B 03/07/2013 B1304 BM646 M 1B 10-Jun-14
BF625 F 1B 03/07/2013 B1304 BF647 F 1B 10-Jun-14
BF627 F 1B 03/07/2013 B1304 BP653 P 1B 25-Jul-14
BM216 M 1B 27/03/2004 B0402 BP654 P 1B 25-Jul-14
BM310 M 1B 20/07/2006 B0604 HM117 M 1H 17-May-06
BM410 M 1B 15/09/2008 B0805 HF125 F 1H 30-Nov-06
BM436 M 1B 25/03/2009 B0902A HF178 F 1H 14-Oct-08
BM438 M 1B 25/03/2009 B0902A HM180 M 1H 14-Oct-08
BM455 M 1B 20/06/2009 B0903 HF182 F 1H 12-Mar-09
BM500 M 1B 09/12/2010 B1006 HM195 M 1H 18-Dec-09
BM558 M 1B 20/04/2012 B1202 HM216 M 1H 10-Nov-10
BM559 M 1B 20/04/2012 B1202 HM217 M 1H 10-Nov-10
BM562 M 1B 20/04/2012 B1202 HF220 F 1H 10-Nov-10
BM564 M 1B 20/04/2012 B1202 HM222 M 1H 10-Nov-10
BM580 M 1B 27/06/2012 B1203 HM223 M 1H 10-Nov-10
BM609 M 1B 29/04/2013 B1303 HF240 F 1H 23-Apr-12
BM614 M 1B 03/07/2013 B1304 HM241 M 1H 23-Apr-12
BM615 M 1B 03/07/2013 B1304 HF242 F 1H 23-Apr-12
BM618 M 1B 03/07/2013 B1304 HM246 M 1H 23-Apr-12
BM620 M 1B 03/07/2013 B1304 HF248 F 1H 27-Jun-12
BM623 M 1B 03/07/2013 B1304 HF260 F 1H 06-Jun-13
BM626 M 1B 03/07/2013 B1304 HF261 F 1H 06-Jun-13
HF125 F 1H 30/11/2006 H0605 HM275 M 1H 05-Oct-13
HF178 F 1H 14/10/2008 H0804 HF276 F 1H 11-Apr-14
HF182 F 1H 12/03/2009 H0901 HF285 F 1H 18-Jun-14
HF220 F 1H 10/11/2010 H1004 TF121 F 2 01-Jan-04 Y
HF240 F 1H 23/04/2012 H1202 TF125 F 2 01-Jan-04 Y
HF242 F 1H 23/04/2012 H1202 TM233 M 2 30-Jul-08
HF248 F 1H 27/06/2012 H1203 TM236 M 2 02-Oct-08
HF251 F 1H 27/06/2012 H1203 TF249 F 2 02-Oct-08
HF260 F 1H 06/06/2013 H1303 TM272 M 2 02-Feb-10
HF261 F 1H 06/06/2013 H1303 TM279 M 2 15-Apr-10
HM117 M 1H 17/05/2006 H0602 TM283 M 2 24-Jun-10
HM180 M 1H 14/10/2008 H0804 TF284 F 2 24-Jun-10
HM195 M 1H 18/12/2009 H0905 TF285 F 2 24-Jun-10
HM216 M 1H 10/11/2010 H1004 TM286 M 2 24-Jun-10
HM217 M 1H 10/11/2010 H1004 TF297 F 2 21-Apr-12
HM222 M 1H 10/11/2010 H1004 TF298 F 2 21-Apr-12
HM223 M 1H 10/11/2010 H1004 TF299 F 2 21-Apr-12
HM241 M 1H 23/04/2012 H1202 TM300 M 2 21-Apr-12
HM243 M 1H 23/04/2012 H1202 TM304 M 2 21-Apr-12
HM246 M 1H 23/04/2012 H1202 TM307 M 2 21-Apr-12
HM253 M 1H 27/06/2012 H1203 TM312 M 2 21-Apr-12
TF249 F 2 02/10/2008 T0804 TM317 M 2 06-Jul-12
TF284 F 2 24/06/2010 T1003 TF319 F 2 06-Jul-12
TF285 F 2 24/06/2010 T1003 TF321 F 2 06-Jul-12
TF297 F 2 21/04/2012 T1202 TF322 F 2 06-Jul-12
TF298 F 2 21/04/2012 T1202 TM323 M 2 06-Jul-12
TF299 F 2 21/04/2012 T1202 TF334 F 2 26-Jan-13
TF319 F 2 06/07/2012 T1203 TM335 M 2 26-Jan-13
TF321 F 2 06/07/2012 T1203 TM363 M 2 28-Sep-13
TF322 F 2 06/07/2012 T1203 TF372 F 2 25-Jul-14
TF332 F 2 26/01/2013 T1301 TM373 M 2 25-Jul-14
TF334 F 2 26/01/2013 T1301 TF374 F 2 25-Jul-14
TF336 F 2 26/01/2013 T1301 TF376 F 2 25-Jul-14
TM233 M 2 30/07/2008 T0803 TF377 F 2 25-Jul-14
TM236 M 2 02/10/2008 T0804 TM378 M 2 25-Jul-14
TM272 M 2 02/02/2010 T1001 TM379 M 2 25-Jul-14
TM279 M 2 15/04/2010 T1002 TM380 M 2 25-Jul-14
TM283 M 2 24/06/2010 T1003 TF381 F 2 25-Jul-14
TM286 M 2 24/06/2010 T1003 TM382 M 2 25-Jul-14
TM300 M 2 21/04/2012 T1202 TM383 M 2 25-Jul-14
TM304 M 2 21/04/2012 T1202 VM096 M 21 09-Oct-08
TM307 M 2 21/04/2012 T1202 VM135 M 21 15-Oct-12
TM309 M 2 21/04/2012 T1202 VM136 M 21 15-Oct-12
TM312 M 2 21/04/2012 T1202 VF139 F 21 01-Jan-12 Y
TM317 M 2 06/07/2012 T1203 VF140 F 21 01-Jan-12 Y
TM323 M 2 06/07/2012 T1203 VF141 F 21 01-Jan-12 Y
TM335 M 2 26/01/2013 T1301 VF142 F 21 01-Jan-12 Y
TM339 M 2 08/04/2013 T1302 VF143 F 21 01-Jan-12 Y
TP355 P 2 28/09/2013 T1305 VF152 F 21 05-May-14
TP356 P 2 28/09/2013 T1305 VF153 F 21 05-May-14
TP357 P 2 28/09/2013 T1305 VF154 F 21 05-May-14
TP358 P 2 28/09/2013 T1305 VM155 M 21 05-May-14
TP359 P 2 28/09/2013 T1305 VF156 F 21 05-May-14
TP360 P 2 28/09/2013 T1305 FM176 M 4B 01-Jan-11 Y
TP361 P 2 28/09/2013 T1305 FM178 M 4B 01-Jan-11 Y
TP362 P 2 28/09/2013 T1305 FM188 M 4B 07-Apr-11
TP363 P 2 28/09/2013 T1305 FM191 M 4B 07-Apr-11
FF166 F 4B 10/01/2010 F1001 FF196 F 4B 28-Oct-11
FF196 F 4B 28/10/2011 F1104 FF206 F 4B 01-Jan-12 Y
FM176 M 4B 06/12/2010 FF207 F 4B 01-Jan-12 Y
FM178 M 4B 06/12/2010 FF208 F 4B 01-Jan-12 Y
FM188 M 4B 07/04/2011 FF209 F 4B 01-Jan-12 Y
FM191 M 4B 07/04/2011 MM002 M 4E 01-Jan-12 Y
FM199 M 4B 18/02/2013 F1301 MM004 M 4E 01-Jan-12 Y
FF165 F 4E 10/01/2010 F1001 MM007 M 4E 20-Jun-14
FF187 F 4E 07/04/2011 FF165 F 4E 10-Jan-10
FF190 F 4E 07/04/2011 FF187 F 4E 07-Apr-11
MM002 M 4E 27/03/2012 FF190 F 4E 07-Apr-11
MM004 M 4E 22/08/2012 GM049 M 7A 22-Oct-07
GF092 F 7A 06/04/2011 G1101 GM060 M 7A 05-May-09
GF093 F 7A 06/04/2011 G1101 GM061 M 7A 05-May-09
GF096 F 7A 12/07/2011 G1102 GM065 M 7A 23-Jul-09
GF100 F 7A 22/10/2011 G1103 GM080 M 7A 26-Apr-10
GF106 F 7A 16/06/2012 G1201 GM082 M 7A 26-Apr-10
GF118 F 7A 22/04/2013 G1301 GM087 M 7A 28-Nov-10
GM035 M 7A 12/05/2006 G0602 GM089 M 7A 28-Nov-10
GM049 M 7A 22/10/2007 GM090 M 7A 06-Apr-11
GM060 M 7A 05/05/2009 G0902 GF092 F 7A 06-Apr-11
GM061 M 7A 05/05/2009 G0902 GF093 F 7A 06-Apr-11
GM065 M 7A 23/07/2009 G0903 GF096 F 7A 12-Jul-11
GM080 M 7A 26/04/2010 G1002 GF100 F 7A 22-Oct-11
GM082 M 7A 26/04/2010 G1002 GM105 M 7A 16-Jun-12
GM087 M 7A 28/11/2010 G1004 GF106 F 7A 16-Jun-12
GM089 M 7A 28/11/2010 G1004 GM112 M 7A 01-Dec-12
GM090 M 7A 06/04/2011 G1101 GM115 M 7A 01-Dec-12
GM097 M 7A 12/07/2011 G1102 GM117 M 7A 22-Apr-13
GM099 M 7A 22/10/2011 G1103
GM105 M 7A 16/06/2012 G1201
GM110 M 7A 01/12/2012 G1203
GM111 M 7A 01/12/2012 G1203
GM112 M 7A 01/12/2012 G1203
GM114 M 7A 01/12/2012 G1203
GM115 M 7A 01/12/2012 G1203
GM117 M 7A 22/04/2013 G1301
...@@ -74,8 +74,9 @@ ...@@ -74,8 +74,9 @@
(define (insert-mongooses db table l) (define (insert-mongooses db table l)
(map (map
(lambda (i) (lambda (i)
(msg (length i))
(let ((pack (car (db-all-where db table "pack" (list "name" "varchar" (list-ref i 2))))) (let ((pack (car (db-all-where db table "pack" (list "name" "varchar" (list-ref i 2)))))
(date (string-split (list-ref i 3) '(#\/)))) (date (string-split (list-ref i 3) '(#\-))))
(msg i) (msg i)
(insert-entity db table "mongoose" "sys" (insert-entity db table "mongoose" "sys"
(list (list
...@@ -83,7 +84,7 @@ ...@@ -83,7 +84,7 @@
(ktv "gender" "varchar" (ktv "gender" "varchar"
(if (equal? (list-ref i 1) "F") "Female" "Male")) (if (equal? (list-ref i 1) "F") "Female" "Male"))
(ktv "pack-id" "varchar" (ktv-get pack "unique_id")) (ktv "pack-id" "varchar" (ktv-get pack "unique_id"))
(ktv "litter-code" "varchar" (if (eq? (length i) 5) (list-ref i 4) "")) (ktv "litter-code" "varchar" "")
(ktv "chip-code" "varchar" "") (ktv "chip-code" "varchar" "")
(ktv "dob" "varchar" (string-append (ktv "dob" "varchar" (string-append
(list-ref date 2) "-" (list-ref date 2) "-"
...@@ -107,5 +108,5 @@ ...@@ -107,5 +108,5 @@
(define (write-db db table path) (define (write-db db table path)
(insert-packs db table (list "11" "14" "15" "17" "18" "1B" "1H" "2" "4B" "4E" "7A")) (insert-packs db table (list "11" "14" "15" "17" "18" "19" "1B" "1H" "2" "21" "4B" "4E" "7A"))
(insert-csv db table path)) (insert-csv db table path))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment