-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql-changes.php
140 lines (104 loc) · 3.32 KB
/
sql-changes.php
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
function processLineSection($lines = array()){
$query = implode("\n\t", $lines);
if(preg_match("/^ALTER TABLE \[dbo\].\[([A-z0-9]+)\] (ADD|DROP) CONSTRAINT \[([A-z0-9\_]+)\]/", $lines[0], $matches)){
$tableName = $matches[1];
$type = $matches[2];
$constraint = $matches[3];
$constraintLookupLocation;
// type is foreign key
if(preg_match("/^FK_/", $constraint)){
$constraintLookupLocation = "REFERENTIAL_CONSTRAINTS";
}else if(preg_match("/^PK_/", $constraint)){
$constraintLookupLocation = "TABLE_CONSTRAINTS";
}else{
throw new Error("invalid type $constraint");
}
if($type == "ADD"){
$out = "IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_NAME='$constraint')
BEGIN
$query;
END";
}else if($type == "DROP"){
$out = "IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_NAME='$constraint')
BEGIN
$query;
END";
}
return $out;
}
if(preg_match("/^CREATE TABLE \[dbo\].\[([A-z0-9]+)\]/", $lines[0], $matches)){
$tableName = $matches[1];
$cols = array();
foreach($lines as $k => $v){
if(preg_match("/\[([A-z0-9\_]+)\]\ \[[A-z0-9]+\].*?,$/", $v, $matches)){
array_push($cols, $matches[1]);
}
}
$out = "IF NOT EXISTS(SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = '$tableName')
BEGIN
$query;
END";
return $out;
}
if(preg_match("/DROP TABLE \[dbo\]\.\[([A-z0-9\_]+)\]$/", $lines[0], $matches)){
$tableName = $matches[1];
$out = "IF EXISTS(SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = '$tableName')
BEGIN
$query;
END";
return $out;
}
if(preg_match("/^ALTER TABLE \[dbo\].\[([A-z0-9\_]+)\] ALTER COLUMN \[([A-z0-9\_]+)\] \[varchar\] \(([0-9]+)\) (COLLATE [A-z0-9\_]+)* (NULL)*/", $lines[0], $matches)){
$table = $matches[1];
$col = $matches[2];
$varcharLen = $matches[3];
$collate = $matches[4];
$nullable = $matches[5];
if($nullable == ""){
$isNullable = "NO";
}else{
$isNullable = "YES";
}
$out = "IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='$table' AND COLUMN_NAME='$col' AND DATA_TYPE='varchar' AND CHARACTER_MAXIMUM_LENGTH='$varcharLen' AND IS_NULLABLE = '$isNullable')
BEGIN
$query;
END";
return $out;
}
if(preg_match("/^ALTER TABLE \[dbo\].\[([A-z0-9\_]+)\] ADD[\ \t]*?\n[\ \t]*?\[([A-z0-9]+)\] \[(datetime|int)\] (NULL)*/", $lines[0]."\n".$lines[1], $matches)){
$table = $matches[1];
$col = $matches[2];
$type = $matches[3];
$null = $matches[4] == "" ? "NO" : "YES";
$out = "IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='$table' AND COLUMN_NAME='$col' AND DATA_TYPE='$type' AND IS_NULLABLE = '$null')
BEGIN
$query;
END";
return $out;
}
return $query;
}
$buff = array();
$segments = array();
do{
$line = trim(fgets(STDIN));
if($line == "GO" && count($buff) > 0){
array_push($segments, processLineSection($buff));
$buff = array();
}
if($line != "" && $line != "GO") // skip blank lines
array_push($buff, $line);
} while($line != ".");
$fname = tempnam("/tmp", "sql-") . ".sql";
$h = fopen($fname, "w");
fwrite($h, "\n");
fwrite($h, "--- GENERATED SQL\n\n");
fwrite($h, implode("\nGO\n\n", $segments));
fwrite($h, "\n\n--- END\n\n");
fclose($h);
exec("open $fname");