Showing 1 changed files with 48 additions and 44 deletions
+48 -44
analyse-votes-AN
... ...
@@ -9,6 +9,10 @@ done
9 9
 
10 10
 IFS_=$IFS
11 11
 
12
+function sqlite_request () {
13
+    sqlite3 "$in_ram_database" <<< "$1"
14
+}
15
+
12 16
 function create_database () {
13 17
     declare -A groupes
14 18
     if test -z "${groupes[@]}"; then
... ...
@@ -16,33 +20,33 @@ function create_database () {
16 20
         exit 1
17 21
     fi
18 22
 
19
-    sqlite3 "$in_ram_database" <<< "create table if not exists votes (id integer primary key, nom text)"
20
-    sqlite3 "$in_ram_database" <<< "create table if not exists url (id integer primary key autoincrement, url text)"
21
-    sqlite3 "$in_ram_database" <<< "create table if not exists députés (id integer primary key autoincrement, nom text, groupe integer)"
22
-    sqlite3 "$in_ram_database" <<< "create table if not exists groupes (id integer primary key autoincrement, nom text, nom_court text)"
23
-    sqlite3 "$in_ram_database" <<< "create table if not exists scrutins (num integer primary key, date text not null, intitulé text non null, adoption boolean, url integer)"
24
-    sqlite3 "$in_ram_database" <<< "create table if not exists dépouillement (député integer not null, scrutin integer not null, groupe integer not null, vote integer not null)"
23
+    sqlite_request "create table if not exists votes (id integer primary key, nom text)"
24
+    sqlite_request "create table if not exists url (id integer primary key autoincrement, url text)"
25
+    sqlite_request "create table if not exists députés (id integer primary key autoincrement, nom text, groupe integer)"
26
+    sqlite_request "create table if not exists groupes (id integer primary key autoincrement, nom text, nom_court text)"
27
+    sqlite_request "create table if not exists scrutins (num integer primary key, date text not null, intitulé text non null, adoption boolean, url integer)"
28
+    sqlite_request "create table if not exists dépouillement (député integer not null, scrutin integer not null, groupe integer not null, vote integer not null)"
25 29
 
26 30
     v_id=0
27 31
     for v in Pour Contre Abstention Non-votant; do
28
-        if test -z $(sqlite3 "$in_ram_database" <<< "select nom from votes where id is $v_id"); then
29
-            sqlite3 "$in_ram_database" <<< "insert into votes values ($v_id, '$v')"
32
+        if test -z $(sqlite_request "select nom from votes where id is $v_id"); then
33
+            sqlite_request "insert into votes values ($v_id, '$v')"
30 34
         else
31
-            test -z $(sqlite3 "$in_ram_database" <<< "select nom from votes where id is $v_id and nom is '$v'") \
32
-                && sqlite3 "$in_ram_database" <<< "update votes set nom = '$v' where id is $v_id)"
35
+            test -z $(sqlite_request "select nom from votes where id is $v_id and nom is '$v'") \
36
+                && sqlite_request "update votes set nom = '$v' where id is $v_id)"
33 37
         fi
34 38
         let v_id++
35 39
     done
36 40
     unset v_id v
37 41
 
38 42
     for g in ${!groupes[@]}; do
39
-        test -z $(sqlite3 "$in_ram_database" <<< "select id from groupes where nom is '${groupes[$g]}' and nom_court is '$g'") \
40
-            && sqlite3 "$in_ram_database" <<< "insert into groupes (nom, nom_court) values ('${groupes[$g]}', '$g')"
43
+        test -z $(sqlite_request "select id from groupes where nom is '${groupes[$g]}' and nom_court is '$g'") \
44
+            && sqlite_request "insert into groupes (nom, nom_court) values ('${groupes[$g]}', '$g')"
41 45
     done
42 46
     unset g groupes
43 47
 
44
-    test -z $(sqlite3 "$in_ram_database" <<< "select id from url where id = 0") \
45
-        && sqlite3 "$in_ram_database" <<< "insert into url values (0, '')"
48
+    test -z $(sqlite_request "select id from url where id = 0") \
49
+        && sqlite_request "insert into url values (0, '')"
46 50
 }
47 51
 
48 52
 function update_database () {
... ...
@@ -50,7 +54,7 @@ function update_database () {
50 54
     tempfile="/dev/shm/scrutin.$$"
51 55
     progress=0
52 56
     first_=$first
53
-    first=$(sqlite3 "$in_ram_database" <<< "select count(num) from scrutins")
57
+    first=$(sqlite_request "select count(num) from scrutins")
54 58
     if test ${first:-0} -lt $last; then
55 59
         echo "récupération des scrutins n°$((${first:-0}+1)) à n°$last dans "$database" (à conserver autant que possible)" >&2
56 60
 
... ...
@@ -93,26 +97,26 @@ function update_database () {
93 97
             grep -q 'e a a' <<< "$adoption" && adoption=1 || adoption=0
94 98
 
95 99
             url=$(awk -F'|' "/^$scrutin\|/{print \$2}" "$url_database")
96
-            id_url=$(sqlite3 "$in_ram_database" <<< "select id from url where url is '$url'")
100
+            id_url=$(sqlite_request "select id from url where url is '$url'")
97 101
             if test -z "$id_url"; then
98
-                sqlite3 "$in_ram_database" <<< "insert into url (url) values ('$url')"
99
-                id_url=$(sqlite3 "$in_ram_database" <<< "select id from url where url is '$url'")
102
+                sqlite_request "insert into url (url) values ('$url')"
103
+                id_url=$(sqlite_request "select id from url where url is '$url'")
100 104
             fi
101 105
 
102
-            sqlite3 "$in_ram_database" <<< "insert into scrutins values ($scrutin, '$date', \"${title//\"}\", $adoption, ${id_url:-0})"
106
+            sqlite_request "insert into scrutins values ($scrutin, '$date', \"${title//\"}\", $adoption, ${id_url:-0})"
103 107
 
104
-            for v in $(sqlite3 "$in_ram_database" <<< "select * from votes"); do
105
-                for g in $(sqlite3 "$in_ram_database" <<< "select id,nom from groupes"); do
108
+            for v in $(sqlite_request "select * from votes"); do
109
+                for g in $(sqlite_request "select id,nom from groupes"); do
106 110
                     for d in $(sed -rn '/<p class="nomgroupe">'${g#*|}' <span class="block topmargin">/,/<div class="TTgroupe topmargin-lg">/p' $tempfile \
107 111
                         | sed -rn '/<p class="typevote">'${v#*|}':/,/<.div>/p' \
108 112
                         | sed 's,</li>,\n,g' \
109 113
                         | sed -rn '/<p class="typevote">/d; s,^\s*<li>\s*,,; s,&nbsp;, ,g; s/^\s*//; s/M(me|\.) //; s/ \(.*$//; s,<b>,,; s,</b>,,p'); do
110
-                        d_id=$(sqlite3 "$in_ram_database" <<< "select id from députés where nom is \"$d\" and groupe is ${g%|*}")
114
+                        d_id=$(sqlite_request "select id from députés where nom is \"$d\" and groupe is ${g%|*}")
111 115
                         if test -z "$d_id"; then
112
-                            sqlite3 "$in_ram_database" <<< "insert into députés (nom, groupe) values (\"$d\", ${g%|*})"
113
-                            d_id=$(sqlite3 "$in_ram_database" <<< "select id from députés where nom is \"$d\" and groupe is ${g%|*}")
116
+                            sqlite_request "insert into députés (nom, groupe) values (\"$d\", ${g%|*})"
117
+                            d_id=$(sqlite_request "select id from députés where nom is \"$d\" and groupe is ${g%|*}")
114 118
                         fi
115
-                        sqlite3 "$in_ram_database" <<< "insert into dépouillement values ($d_id, $scrutin, ${g%|*}, ${v%|*})"
119
+                        sqlite_request "insert into dépouillement values ($d_id, $scrutin, ${g%|*}, ${v%|*})"
116 120
                     done
117 121
                 done
118 122
             done
... ...
@@ -135,7 +139,7 @@ function write_comparaison () {
135 139
     result="comparaisons ${groupe[0]} avec ${groupe_ref:-GDR}${dossier:+ - ${dossier}}"
136 140
     content="/dev/shm/$result/content.xml"
137 141
     id_cols="Scrutin Date Titre Adoption"
138
-    typevotes=$(sqlite3 "$in_ram_database" <<< "select nom from votes")
142
+    typevotes=$(sqlite_request "select nom from votes")
139 143
     nb_cols=$(( $(wc -w <<< $id_cols) + $(wc -w <<< $typevotes) * ${#groupe[@]} ))
140 144
     last_col=$(awk -v n=$nb_cols 'BEGIN{printf("%c%c", n < 27 ? "" : int(n/26) + 64, (n % 26) + (n % 26 == 0 ? 26 : 0) + 64)}' | tr -d '\0')
141 145
 
... ...
@@ -234,7 +238,7 @@ EOcontent
234 238
     IFS=$'\n'
235 239
     for scrutin in $(eval ${seq:-seq $first $last}); do
236 240
 
237
-        data=$(sqlite3 "$in_ram_database" <<< "select date,intitulé,adoption,url.url from scrutins inner join url on scrutins.url = url.id where num is $scrutin")
241
+        data=$(sqlite_request "select date,intitulé,adoption,url.url from scrutins inner join url on scrutins.url = url.id where num is $scrutin")
238 242
         date=$(cut -d'|' -sf 1 <<< $data)
239 243
         title=$(cut -d'|' -sf 2 <<< $data)
240 244
         adoption=$(cut -d'|' -sf 3 <<< $data)
... ...
@@ -269,7 +273,7 @@ EOcontent
269 273
             </table:table-cell>
270 274
 EOcontent
271 275
         for typevote in 0 1 2 3; do
272
-            votes=$(sqlite3 "$in_ram_database" <<< "select
276
+            votes=$(sqlite_request "select
273 277
                                         count(député)
274 278
                                      from
275 279
                                         dépouillement
... ...
@@ -285,7 +289,7 @@ EOcontent
285 289
                 </table:table-cell>
286 290
 EOcontent
287 291
             for (( g = 1; g < ${#groupe_id[@]}; g++ )); do
288
-                votes=$(sqlite3 "$in_ram_database" <<< "select
292
+                votes=$(sqlite_request "select
289 293
                                             count(député)
290 294
                                          from
291 295
                                             dépouillement
... ...
@@ -491,7 +495,7 @@ else
491 495
 fi
492 496
 
493 497
 for (( g = 0; g < ${#groupe[@]}; g++ )); do
494
-    groupe_id[$g]=$(sqlite3 "$in_ram_database" <<< "select id from groupes where nom_court is '${groupe[$g]}'")
498
+    groupe_id[$g]=$(sqlite_request "select id from groupes where nom_court is '${groupe[$g]}'")
495 499
     if test -z "${groupe_id[$g]}"; then
496 500
         echo "groupe ${groupe[$g]} inconnu" >&2
497 501
         exit 1
... ...
@@ -499,8 +503,8 @@ for (( g = 0; g < ${#groupe[@]}; g++ )); do
499 503
 done
500 504
 
501 505
 if test "$periode" = $true_flag; then
502
-    first=$(sqlite3 "$in_ram_database" <<< "select num from scrutins where date like '% du ${periode_value%:*}' order by num asc" | head -1)
503
-    last=$(sqlite3 "$in_ram_database" <<< "select num from scrutins where date like '% du ${periode_value#*:}' order by num asc" | tail -1)
506
+    first=$(sqlite_request "select num from scrutins where date like '% du ${periode_value%:*}' order by num asc" | head -1)
507
+    last=$(sqlite_request "select num from scrutins where date like '% du ${periode_value#*:}' order by num asc" | tail -1)
504 508
     test -z "$first" && echo "date de début inconnue: ${periode_value#*:}" >&2 && rm -f $in_ram_database && exit 1
505 509
     test -z "$last" && echo "date de fin inconnue: ${periode_value%:*}" >&2 && rm -f $in_ram_database && exit 1
506 510
 elif test "$dossier" != $true_flag; then
... ...
@@ -512,7 +516,7 @@ elif test "$dossier" != $true_flag; then
512 516
 fi
513 517
 
514 518
 if test "$liste_dossiers" = $true_flag; then
515
-    sqlite3 "$in_ram_database" <<< "select printf('%s - %s', id, url) from url" | sed 's,https*://.*/dossiers/,,; s/_/ /g; s/.asp$//'
519
+    sqlite_request "select printf('%s - %s', id, url) from url" | sed 's,https*://.*/dossiers/,,; s/_/ /g; s/.asp$//'
516 520
     exit
517 521
 fi
518 522
 
... ...
@@ -527,26 +531,26 @@ fi
527 531
 
528 532
 if test "$liste_deputes" = $true_flag; then
529 533
     if test -n "$liste_deputes_value"; then
530
-        sqlite3 "$in_ram_database" <<< "select printf('%s - %s', députés.nom, groupes.nom_court) from députés inner join groupes on groupes.id = députés.groupe where groupes.nom_court is '$liste_deputes_value'"
534
+        sqlite_request "select printf('%s - %s', députés.nom, groupes.nom_court) from députés inner join groupes on groupes.id = députés.groupe where groupes.nom_court is '$liste_deputes_value'"
531 535
     else
532
-        sqlite3 "$in_ram_database" <<< "select printf('%s - %s', députés.nom, groupes.nom_court) from députés inner join groupes on groupes.id = députés.groupe order by groupes.nom_court asc"
536
+        sqlite_request "select printf('%s - %s', députés.nom, groupes.nom_court) from députés inner join groupes on groupes.id = députés.groupe order by groupes.nom_court asc"
533 537
     fi
534 538
     exit
535 539
 fi
536 540
 
537 541
 if test "$depute" = $true_flag; then
538 542
     if test -n "$nom"; then
539
-        match=$(sqlite3 "$in_ram_database" <<< "select count(députés.id) from députés inner join groupes on groupes.id = députés.groupe where députés.nom like '%$nom%' and groupes.nom_court is '$groupe' collate nocase")
543
+        match=$(sqlite_request "select count(députés.id) from députés inner join groupes on groupes.id = députés.groupe where députés.nom like '%$nom%' and groupes.nom_court is '$groupe' collate nocase")
540 544
         if test $match -ne 1; then
541 545
             if test $match -eq 0; then
542 546
                 echo "pas de député correspondant dans le groupe $groupe"
543 547
             else 
544 548
                 echo "plusieurs députés correspondent:"
545
-                sqlite3 "$in_ram_database" <<< "select députés.nom from députés inner join groupes on groupes.id = députés.groupe where députés.nom like '%$nom%' and groupes.nom_court is '$groupe' collate nocase"
549
+                sqlite_request "select députés.nom from députés inner join groupes on groupes.id = députés.groupe where députés.nom like '%$nom%' and groupes.nom_court is '$groupe' collate nocase"
546 550
             fi
547 551
             exit 1
548 552
         else
549
-            nom=$(sqlite3 "$in_ram_database" <<< "select députés.id,députés.nom from députés inner join groupes on groupes.id = députés.groupe where députés.nom like '%$nom%' and groupes.nom_court is '$groupe' collate nocase")
553
+            nom=$(sqlite_request "select députés.id,députés.nom from députés inner join groupes on groupes.id = députés.groupe where députés.nom like '%$nom%' and groupes.nom_court is '$groupe' collate nocase")
550 554
             groupe[0]="${nom#*|} (${groupe[0]})"
551 555
         fi
552 556
     fi
... ...
@@ -555,18 +559,18 @@ fi
555 559
 if test "$dossier" = $true_flag; then
556 560
     if test -z "$dossier_value"; then
557 561
         IFS=$'\n'
558
-        select dossier in $(sqlite3 "$in_ram_database" <<< "select url from url" | sed 's,^.*/dossiers/,,; s/_/ /g; s/.asp$//'); do
562
+        select dossier in $(sqlite_request "select url from url" | sed 's,^.*/dossiers/,,; s/_/ /g; s/.asp$//'); do
559 563
             if test -n "$dossier"; then
560
-                seq="sqlite3 \"$in_ram_database\" <<< \"select num from scrutins inner join url on url.id = scrutins.url where url.url like '%/dossiers/${dossier// /_}%' order by num asc\""
561
-                qty=$(sqlite3 "$in_ram_database" <<< "select count(num) from scrutins inner join url on url.id = scrutins.url where url.url like '%/dossiers/${dossier// /_}%' order by num asc")
564
+                seq="sqlite_request \"select num from scrutins inner join url on url.id = scrutins.url where url.url like '%/dossiers/${dossier// /_}%' order by num asc\""
565
+                qty=$(sqlite_request "select count(num) from scrutins inner join url on url.id = scrutins.url where url.url like '%/dossiers/${dossier// /_}%' order by num asc")
562 566
                 break
563 567
             fi
564 568
         done
565 569
         IFS=$IFS_
566 570
     else
567
-        seq="sqlite3 \"$in_ram_database\" <<< \"select num from scrutins inner join url on url.id = scrutins.url where url.id is $dossier_value order by num asc\""
568
-        qty=$(sqlite3 "$in_ram_database" <<< "select count(num) from scrutins inner join url on url.id = scrutins.url where url.id is $dossier_value order by num asc")
569
-        dossier=$(sqlite3 "$in_ram_database" <<< "select url from url where id is $dossier_value" | sed 's,^.*/dossiers/,,; s/_/ /g; s/.asp$//')
571
+        seq="sqlite_request \"select num from scrutins inner join url on url.id = scrutins.url where url.id is $dossier_value order by num asc\""
572
+        qty=$(sqlite_request "select count(num) from scrutins inner join url on url.id = scrutins.url where url.id is $dossier_value order by num asc")
573
+        dossier=$(sqlite_request "select url from url where id is $dossier_value" | sed 's,^.*/dossiers/,,; s/_/ /g; s/.asp$//')
570 574
     fi
571 575
 fi
572 576