-
Notifications
You must be signed in to change notification settings - Fork 1
/
mydb
executable file
·71 lines (58 loc) · 1.45 KB
/
mydb
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
#!/bin/bash
# A very simple private db frame
# first step: create a xxx.bash in ~/.mydb as follow
# db_init() # write any init code in this function, will be auto called when use this db first time
# {
# sql "create table xxx(...)"
# }
# db_xxx() # write cmds you need, format db_<cmd_name>()
# {
# ...
# }
# # add else cmds you need
# second step: use as follow
# mydb xxx cmd args_to_cmd
# xxx is your db name
# cmd is your cmd name
# args_to_cmd will pass to you db_xxx impl
# if you like add follow to ~/.bashrc
# alias db.xxx='mydb xxx'
# now you can call db.xxx cmd ...
if [ $# -lt 2 ]; then
echo "mydb <db> <cmd> [args_to_cmd]" > /dev/stderr
exit 1
fi
BINDIR=`dirname $0`
DB=$1
CMD=$2
DBDIR="$HOME/.mydb"
mkdir -p "$DBDIR"
report_commands()
{
echo "all commands: " > /dev/stderr
grep "db_[_a-zA-Z0-9]*()" "$DBDIR/$DB.bash" | sed 's/^\s*function\s*//' | sed 's/^\s*db_/\t/' | sed 's/()\s*$//' | grep -v "init" > /dev/stderr
}
sql()
{
sqlite "$DBDIR/$DB.db" "$@"
}
if [ ! -f "$DBDIR/$DB.bash" ]; then
echo "db: $DB does not exists!" > /dev/stderr
echo "all configured dbs: " > /dev/stderr
ls "$DBDIR" | sed 's/^/\t/' > /dev/stderr
exit 1
fi
if [ "$CMD" == "init" ]; then
echo "do not allow call init directly." > /dev/stderr
exit 1
fi
if ! grep -q "db_$CMD\(\)" "$DBDIR/$DB.bash"; then
echo "cmd: $CMD does not exists!" > /dev/stderr
report_commands
exit 1
fi
source "$DBDIR/$DB.bash"
if [ ! -f "$DBDIR/$DB.db" ]; then
db_init
fi
db_$CMD "${@:3}"