convert-tsql 2.58 KB
Newer Older
1
2
#!/bin/bash
# Database translation/creation script
3
# Copyright (C) 2009-2016, AllWorldIT
4
# Copyright (C) 2008, LinuxRulz
5
#
6
7
8
9
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
10
#
11
12
13
14
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
15
#
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

database="$1"
file="$2"
prefix="$3"


# Display usage info
display_usage() {
    echo "Usage: $0 <database type> <file> [prefix]"
    echo
    echo "Valid database types:"
30
    echo "      mysql   - For MySQL v5.5+"
31
32
    echo "      pgsql   - For PostgreSQL"
    echo "      sqlite  - For SQLite v3"
33
    echo
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    exit
}


# Check we have our params
if [ -z "$database" -o -z "$file" ]
then
    display_usage
fi



# Check file exists
if [ ! -f "$file" ]
then
    echo "ERROR: Cannot open file '$file'"
    exit 1
fi

# Check what we converting for
case "$database" in
55
	"mysql")
Robert Anderson's avatar
Robert Anderson committed
56
57
58
59
60
		sed \
			-e "s/@PREFIX@/$prefix/g" \
			-e 's/@PRELOAD@/SET FOREIGN_KEY_CHECKS=0;/' \
			-e 's/@POSTLOAD@/SET FOREIGN_KEY_CHECKS=1;/' \
			-e 's/@CREATE_TABLE_SUFFIX@/ENGINE=InnoDB CHARACTER SET latin1 COLLATE latin1_bin/' \
61
			-e 's/@SERIAL_TYPE@/SERIAL/' \
62
63
			-e 's/@BIGINT_UNSIGNED@/BIGINT UNSIGNED/' \
			-e 's/@INT_UNSIGNED@/INT UNSIGNED/' \
64
65
66
67
68
69
70
71
72
73
74
			-e 's/@TRACK_KEY_LEN@/512/' \
			-e 's/@SERIAL_REF_TYPE@/BIGINT UNSIGNED/' < "$file"
		;;

	"pgsql")
		sed \
			-e "s/@PREFIX@/$prefix/g" \
			-e 's/@PRELOAD@/SET CONSTRAINTS ALL DEFERRED;/' \
			-e 's/@POSTLOAD@//' \
			-e 's/@CREATE_TABLE_SUFFIX@//' \
			-e 's/@SERIAL_TYPE@/SERIAL PRIMARY KEY/' \
75
76
			-e 's/@BIGINT_UNSIGNED@/INT8/' \
			-e 's/@INT_UNSIGNED@/INT8/' \
77
78
79
80
81
82
83
84
85
86
87
			-e 's/@TRACK_KEY_LEN@/512/' \
			-e 's/@SERIAL_REF_TYPE@/INT8/' < "$file"
		;;

	"sqlite")
		sed \
			-e "s/@PREFIX@/$prefix/g" \
			-e 's/@PRELOAD@//' \
			-e 's/@POSTLOAD@//' \
			-e 's/@CREATE_TABLE_SUFFIX@//' \
			-e 's/@SERIAL_TYPE@/INTEGER PRIMARY KEY AUTOINCREMENT/' \
88
89
			-e 's/@BIGINT_UNSIGNED@/INT8/' \
			-e 's/@INT_UNSIGNED@/INT8/' \
90
91
92
93
94
95
96
97
98
99
100
101
			-e 's/@TRACK_KEY_LEN@/512/' \
			-e 's/@SERIAL_REF_TYPE@/INT8/' < "$file"
		;;

	*)
	    echo "ERROR: Invalid database type '$database'"
	    exit 1
	    ;;

esac