-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path.zflai_sqlite_store
116 lines (95 loc) · 4.57 KB
/
.zflai_sqlite_store
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# Copyright (c) 2018 Sebastian Gniazdowski
#
# $1 - database - filename of the definition without .def extension
# prefix in keys of DB_DEFS hash
# $2 - table name
# $3 - array holding entries to store (its name)
setopt localtraps
[[ "$ZFLAI_LIBS_SOURCED" != "1" ]] && source "${ZFLAI_SRC_DIR}/zflai_lib.zsh"
local __db="$1" __table="$2" __array="$3[@]" __table_resolved="$4"
local __db_dir="${DB_DEFS[${__db}_<access>_path]%/}" __db_file="${DB_DEFS[${__db}_<access>_file]}"
local __iobuf __it __it2 __sql __vn __val __ts __tab=$'\t' __nl=$'\n'
local -a __arr
integer __have_table_def=0
local __table_def_param_name
trap "builtin print -p \".exit\"" EXIT
coproc 2>&1 sqlite3
builtin print -r -p '.prompt "" ""'
builtin print -r -p ".open ${__db_dir}/${__db_file}"
local -a __hook_arr_out
[[ -n "${DB_DEFS[${__db}_<hooks>_on_open]}" ]] && {
.zflai_subst_cmds "${${DB_DEFS[${__db}_<hooks>_on_open]#(\!|@|\#)}//(#m)${~_xchg_pat_}/${_xchg_map_[$MATCH]}}"
builtin print -r -p -- "$REPLY"
if [[ "${DB_DEFS[${__db}_<hooks>_on_open]}" = (\!|\#)* ]]; then
.zflai_coproc_error_fetch SQLite 1 '__hook_arr_out[1]' ${${${(M)DB_DEFS[${__db}_<hooks>_on_open]#\!}:+get-tokens-nl}:-whitespace-collapse}
elif [[ "${DB_DEFS[${__db}_<hooks>_on_open]}" = @* ]]; then
.zflai_coproc_error_fetch SQLite 1 __hook_arr_out "get-tokens"
fi
}
[[ -n "${DB_DEFS[${__db}_<hooks>_on_open_sh]}" ]] && (
builtin cd -q "${DB_DEFS[${__db}_<access>_path]}" && \
() { eval "${DB_DEFS[${__db}_<hooks>_on_open_sh]//(#m)${~_xchg_pat_}/${_xchg_map_[$MATCH]}}"; } "${__hook_arr_out[@]}"
)
# Search for definition of the target table
.zflai_get_abstract_table_for "$__db" "$__table" "tspec_" __table_def_param_name __have_table_def || \
.zflai_run_log "SQLite: Warning: No abstract definition of table \`${__table}' (general or for database \`${__db}')"
# Check that table exists
__iobuf=""
builtin print -p ".tables ${__table_resolved}"
IFS='' read -r -t 1 -p __iobuf && [[ "$__iobuf" = "$__table_resolved" ]] || {
# Table doesn't exist, is it defined?
[[ -z "$__table_def_param_name" ]] && {
.zflai_run_log "SQLite: Error: SQLite database \`$__db' doesn't have table \`$__table_resolved', and there is no"
.zflai_run_log "SQLite: abstract definition for this table (i.e. for \`$__table'; see function \`zflai-ctable')."
.zflai_run_log "SQLite: No data was stored, aborting."
.zflai_run_log "SQLite: The related database file is: $__db_dir/$__db_file."
builtin print -p ".exit"
return 1
}
.zflai_run_log "SQLite: Table \`$__table_resolved' doesn't exist, creating.."
local -a keys
keys=( "${(okn@)${(Pk@)__table_def_param_name}}" )
__sql="create table \"${__table_resolved}\"( id integer primary key autoincrement";
for __it in "${keys[@]}"; do
__vn="${__table_def_param_name}[$__it]"
__val="${(P)__vn}"
__it="${__it##[0-9]##-}"
# Got name in $__it, type in $__val
__sql+=", $__it $__val"
done
__sql+=" );"
.zflai_run_log "SQLite: Issuing SQL: $__sql"
builtin print -r -p "$__sql"
.zflai_coproc_error_fetch "SQLite"
}
local -a splitted
for __it in "${(P)__array}"; do
if [[ "$__it" = (#b)[[:blank:]]#[^:]##::[^:]##::[^:]##::([^[:blank:]\|]##)[[:blank:]]#\|[[:blank:]](#c0,1)(*) ]]; then
__ts="${match[1]}"
splitted=( "${(@s:|:)match[2]}" )
__sql="INSERT INTO \"${__table_resolved}\" values( NULL, $__ts "
for __it2 in "${splitted[@]}"; do
__sql+=", \"${${${__it2# }% }//\"/\\\"}\""
done
__sql+=" );"
builtin print -r -p -- "$__sql"
.zflai_coproc_error_fetch "SQLite" 0
else
.zflai_run_log "Incorrect entry passed to SQLite store (skipped): $__it"
fi
done
__hook_arr_out=()
[[ -n "${DB_DEFS[${__db}_<hooks>_on_close]}" ]] && {
.zflai_subst_cmds "${${DB_DEFS[${__db}_<hooks>_on_close]#(\!|@|\#)}//(#m)${~_xchg_pat_}/${_xchg_map_[$MATCH]}}"
builtin print -r -p -- "$REPLY"
if [[ "${DB_DEFS[${__db}_<hooks>_on_close]}" = (\!|\#)* ]]; then
.zflai_coproc_error_fetch SQLite 1 '__hook_arr_out[1]' ${${${(M)DB_DEFS[${__db}_<hooks>_on_close]#\!}:+get-tokens-nl}:-whitespace-collapse}
elif [[ "${DB_DEFS[${__db}_<hooks>_on_close]}" = @* ]]; then
.zflai_coproc_error_fetch SQLite 1 __hook_arr_out "get-tokens"
fi
}
[[ -n "${DB_DEFS[${__db}_<hooks>_on_close_sh]}" ]] && (
builtin cd -q "${DB_DEFS[${__db}_<access>_path]}" && \
() { eval "${DB_DEFS[${__db}_<hooks>_on_close_sh]//(#m)${~_xchg_pat_}/${_xchg_map_[$MATCH]}}"; } "${__hook_arr_out[@]}"
)
# vim:ft=zsh:et