convert markdown to sql

实现

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
#! /bin/bash

if [ -z $DBMD ]; then
DBMD=database.md
fi

state=0

cat $DBMD | tr -d "\r" | while read line
do
field=${line%%\ *}
if [ "$field" == '#' ]; then
if [ $state -eq 0 ]; then
dbname=${line#*\ }
echo "CREATE DATABASE IF NOT EXISTS \`$dbname\` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"
echo
echo "USE \`$dbname\`;"
state=1
else
exit 1
fi
elif [ "$field" == '##' ]; then
if [ "${line#*\ }" == 'tables' ]; then
if [ $state -eq 1 ]; then
state=2
else
exit 1
fi
elif [ "${line#*\ }" == 'sql' ]; then
if [[ $state == 2 ]] || [[ $state == 1 ]]; then
state=5
else
exit 1
fi
fi
elif [ "$field" == '###' ]; then
if [ $state -eq 2 ]; then
echo
echo "CREATE TABLE IF NOT EXISTS \`$DBPREF${line#*\ }\` ("
state=3
unset primkey
else
exit 1
fi
elif [ "$field" == '```' ]; then
if [ $state -eq 3 ]; then
state=4
elif [ $state -eq 4 ]; then
if [ ! -z $primkey ]; then
echo " PRIMARY KEY (\`$primkey\`)";
fi
echo ")$TBLATTR;"
state=2
elif [ $state -eq 5 ]; then
echo
state=6
elif [ $state -eq 6 ]; then
state=1
else
exit 1
fi
elif [ $state -eq 4 ]; then
attr='NOT NULL'
if [ -z $primkey ]; then
primkey=$field
attr=$attr' AUTO_INCREMENT'
fi
temp=${line#*\ }
typename=${temp%\ //*}
case $typename in
'int') typename='int(11)';;
'tinyint') typename='tinyint(4)';;
esac
comment=${line#*//\ }
echo " \`$field\` $typename $attr COMMENT '$comment',"
elif [ $state -eq 6 ]; then
echo "$line"
fi
done