]> pd.if.org Git - zpackage/blob - zpm-log
fix possible memory leak in uncompress
[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 verbose=0
12
13 pkgfile=${ZPMDB:-/var/lib/zpm/local.db}
14
15 order=asc
16
17 while getopts f:t:a:T:ijvr opt; do
18         case $opt in
19                 f) pkgfile="$OPTARG" ;;
20                 t) target="$OPTARG" ;;
21                 a) action="$OPTARG" ;;
22                 T) timestamp="$OPTARG" ;;
23                 i) op=insert ;;
24                 j) json=1 ;;
25                 v) verbose=1 ;;
26                 r) order=desc ;;
27                 *) printf '%s unknown option %s\n' "$0" "$opt" ; exit 1 ;;
28         esac
29 done
30 shift $((OPTIND - 1))
31
32 if [ $op = 'insert' ]; then
33 {
34         cols='action,target,info';
35         if [ -n "$timestamp" ]; then
36                 cols='ts,action,target,info'
37         fi
38
39         printf "insert into zpmlog (%s)\n" $cols
40         printf 'values(';
41         if [ -n "$timestamp" ]; then
42                 printf "'%s', " "$timestamp"
43         fi
44         if [ -n "$action" ]; then
45                 printf "'%s', " "$action"
46         else
47                 printf 'NULL,'
48         fi
49         if [ -n "$target" ]; then
50                 printf "'%s', " "$target"
51         else
52                 printf 'NULL,'
53         fi
54         printf "'%s')" "$*"
55         printf ';\n'
56 } | zpm shell $pkgfile
57
58 if [ $verbose -ne 0 ]; then
59         printf "%s %s %s\n" "$action" "$target" "$*" 1>&2
60 fi
61
62 fi
63
64 cols='ts,target,action,info'
65
66 if [ $op = 'search' ]; then
67
68 if [ $json -eq 1 ]; then
69         cols="json_group_array(json_object('ts',ts,'target',target,'action',action,'info',info))"
70 fi
71
72 {
73         printf ".separator ' '\n"
74         printf 'select %s from zpmlog where\n' "$cols";
75         if [ -n "$timestamp" ]; then
76                 printf "ts >= '%s' and " "$timestamp"
77         fi
78
79         if [ -n "$action" ]; then
80                 printf "action like '%s' and " "$action"
81         fi
82         if [ -n "$target" ]; then
83                 printf "target like '%s' and " "$target"
84         fi
85         printf "true order by ts $order;\n"
86 } | zpm shell $pkgfile
87
88 fi