-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpdev_rename.in
106 lines (101 loc) · 2.42 KB
/
mpdev_rename.in
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
#!/bin/sh
#
# $Log: mpdev_rename.in,v $
# Revision 1.5 2022-11-29 09:48:53+05:30 Cprogrammer
# prevent wrapping of lines after 60 characters
#
# Revision 1.4 2021-09-30 20:31:35+05:30 Cprogrammer
# use -noheader option to prevent .sqlitrc settings messing with results
#
# Revision 1.3 2020-09-08 21:43:05+05:30 Cprogrammer
# fixed usage script
#
# Revision 1.2 2020-08-05 21:15:00+05:30 Cprogrammer
# replaced sed with SQL REPLACE
#
# Revision 1.1 2020-07-19 18:20:04+05:30 Cprogrammer
# Initial revision
#
#
# $Id: mpdev_rename.in,v 1.5 2022-11-29 09:48:53+05:30 Cprogrammer Exp mbhangui $
#
usage()
{
prog_args=""
cat <<EOF
Usage: mpdev_rename [OPTIONS]
mpdev_rename --sticker-db=path|stats-db=path --pattern=patt --query | --string=str --replacement=repl
echo "patt is any string that can be given in a SQL 'WHERE' clause"
echo "str is string to replace"
echo "repl is replacement string"
EOF
echo "Press ENTER for options, Cntrl C to quit" 1>&2
read key
exit $1
}
################################# Main ##################################
if test $# -eq 0; then
usage 1
fi
sticker_db=""
stats_db=""
query=0
while test $# -gt 0; do
case "$1" in
-*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'`
;;
*) optarg=
;;
esac
case "$1" in
--sticker-db=*)
sticker_db=$optarg
dbpath=$optarg
table=sticker
;;
--stats-db=*)
stats_db=$optarg
dbpath=$optarg
table=song
;;
--query)
query=1
;;
--pattern=*)
patt="$optarg"
;;
--string=*)
str="$optarg"
;;
--replacement=*)
repl="$optarg"
;;
*)
echo "invalid option [$1]" 1>&2
usage 1
;;
esac
shift
done
if [ -z "$sticker_db" -a -z "$stats_db" ] ; then
echo "rename_fields: You must specify --sticker-db or --stats-db" 1>&2
usage 1
fi
if [ -n "$sticker_db" -a -n "$stats_db" ] ; then
echo "rename_fields: You cannot specify both --sticker-db and --stats-db" 1>&2
usage 1
fi
if [ $query -eq 0 ] ; then
if [ -z "$str" -o -z "$repl" ] ; then
echo "You have to specify string to replace and replacement string or specify --query" 1>&2
usage 1
fi
fi
if [ $query -eq 1 ] ; then
echo "executing sqlite3 -noheader --list --batch $dbpath \"SELECT uri FROM $table WHERE uri LIKE '$patt'\"" 1>&2
sqlite3 -noheader --list -batch $dbpath "SELECT uri FROM $table WHERE uri LIKE '$patt'"
else
echo "UPDATE $table SET uri=REPLACE(uri, '$str', '$repl') WHERE uri LIKE '$patt'" 1>&2
sqlite3 -batch $dbpath \
"UPDATE $table SET uri=REPLACE(uri, '$str', '$repl') WHERE uri LIKE '$patt'"
fi