]> pd.if.org Git - zpackage/blob - zpm-log
add where clause argument to findpkg
[zpackage] / zpm-log
1 #!/bin/sh
2
3 #-T timestamp
4 #-a action
5 #-t target
6 #-i insert, otherwise search
7
8 op=search
9 action=
10 json=0
11
12 pkgfile=${ZPMDB:-/var/lib/zpm/local.db}
13
14 while getopts f:t:a:T:ij opt; do
15         case $opt in
16                 f) pkgfile="$OPTARG" ;;
17                 t) target="$OPTARG" ;;
18                 a) action="$OPTARG" ;;
19                 T) timestamp="$OPTARG" ;;
20                 i) op=insert ;;
21                 j) json=1 ;;
22                 *) printf '%s unknown option %s\n' "$0" "$opt" ; exit 1 ;;
23         esac
24 done
25 shift $((OPTIND - 1))
26
27 if [ $op = 'insert' ]; then
28 {
29         cols='action,target,info';
30         if [ -n "$timestamp" ]; then
31                 cols='ts,action,target,info'
32         fi
33
34         printf "insert into zpmlog (%s)\n" $cols
35         printf 'values(';
36         if [ -n "$timestamp" ]; then
37                 printf "'%s', " "$timestamp"
38         fi
39         if [ -n "$action" ]; then
40                 printf "'%s', " "$action"
41         else
42                 printf 'NULL,'
43         fi
44         if [ -n "$target" ]; then
45                 printf "'%s', " "$target"
46         else
47                 printf 'NULL,'
48         fi
49         printf "'%s')" "$*"
50         printf ';\n'
51 } | zpm shell $pkgfile
52
53 fi
54
55 cols='ts,target,action,info'
56
57 if [ $op = 'search' ]; then
58
59 if [ $json -eq 1 ]; then
60         cols="json_group_array(json_object('ts',ts,'target',target,'action',action,'info',info))"
61 fi
62
63 {
64         printf ".separator ' '\n"
65         printf 'select %s from zpmlog where\n' "$cols";
66         if [ -n "$timestamp" ]; then
67                 printf "ts >= '%s' and " "$timestamp"
68         fi
69
70         if [ -n "$action" ]; then
71                 printf "action like '%s' and " "$action"
72         fi
73         if [ -n "$target" ]; then
74                 printf "target like '%s' and " "$target"
75         fi
76         printf "true order by ts;\n"
77 } | zpm shell $pkgfile
78
79 fi