forked from matriphe/mssql2mysql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmssql2mysql.php
261 lines (204 loc) · 6.33 KB
/
mssql2mysql.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/*
* stAn: this fork allows you to simply replicate MS SQL into Mysql on latest php7 using php-pdo and php-pdo_odbc (FreeTds + odbc)
* follow this to setup the connector http://stackoverflow.com/questions/20163776/connect-php-to-mssql-via-pdo-odbc
* tested on php7 provided by ppa:/ondrej-php on Ubuntu 12.04, 14.04 and 16.04 https://launchpad.net/~ondrej/+archive/ubuntu/php
* SOURCE: MS SQL
*/
//define('MSSQL_HOST','mssql_host');
define('MSSQL_SERVERNAME','servername from freeTDS config'); //section name from /etc/freetds/freetds.conf and /etc/odbc.ini
define('MSSQL_USER','mssql_user');
define('MSSQL_PASSWORD','mssql_password');
define('MSSQL_DATABASE','mssql_database');
/*
* DESTINATION: MySQL
*/
define('MYSQL_HOST', 'mysql_host');
define('MYSQL_USER', 'mysql_user');
define('MYSQL_PASSWORD','mysql_password');
define('MYSQL_DATABASE','mysql_database');
/*
* STOP EDITING!
*/
set_time_limit(0);
function addQuote($string)
{
return "'".$string."'";
}
function addTilde($string)
{
return "`".$string."`";
}
// Connect MS SQL
try {
$mssql = new PDO('odbc:DRIVER=freetds;SERVERNAME='.MSSQL_SERVERNAME.';DATABASE=' . MSSQL_DATABASE, MSSQL_USER, MSSQL_PASSWORD) or die("Couldn't connect to SQL Server on '".MSSQL_HOST."'' user '".MSSQL_USER."'\n");
}
catch (Exception $e) {
echo $e."\n";
die(1);
}
echo "=> Connected to Source MS SQL Server on '".MSSQL_SERVERNAME."'\n";
// Connect to MySQL
$mysqli = new mysqli(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE);
if ($mysqli->connect_error) {
echo 'Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error."\n";
die(1);
}
else {
echo "\n=> Connected to Source MySQL Server on ".MYSQL_HOST."\n";
}
// Select MySQL Database
$mssql_tables = array();
// Get MS SQL tables
$sql = "SELECT * FROM sys.Tables;";
$qe = $mssql->prepare($sql);
$qe->execute();
echo "\n=> Getting tables..\n";
while ($row = $qe->fetch(PDO::FETCH_ASSOC))
{
array_push($mssql_tables, $row['name']);
}
// Get MS SQL Views
$sql = "SELECT * FROM sys.Views;";
$qe = $mssql->prepare($sql);
$qe->execute();
echo "\n=> Getting Views..\n";
while ($row = $qe->fetch(PDO::FETCH_ASSOC))
{
array_push($mssql_tables, $row['name']);
}
echo "==> Found ". number_format(count($mssql_tables),0,',','.') ." tables\n\n";
// Get Table Structures
if (!empty($mssql_tables))
{
$i = 1;
foreach ($mssql_tables as $table)
{
echo '====> '.$i.'. '.$table."\n";
echo "=====> Getting info table ".$table." from SQL Server\n";
$sql = "select * from information_schema.columns where table_name = '".$table."'";
$qe = $mssql->prepare($sql);
$res = $qe->execute();
if ($res)
{
$mssql_tables[$table] = array();
$mysql = "DROP TABLE IF EXISTS `".$table."`";
$mysqli->query($mysql);
$mysql = "CREATE TABLE `".$table."`";
$strctsql = $fields = array();
while ($row = $qe->fetch(PDO::FETCH_ASSOC))
{
//print_r($row); echo "\n";
array_push($mssql_tables[$table], $row);
switch ($row['DATA_TYPE']) {
case 'bit':
case 'tinyint':
case 'smallint':
case 'int':
case 'bigint':
$data_type = $row['DATA_TYPE'].(!empty($row['NUMERIC_PRECISION']) ? '('.$row['NUMERIC_PRECISION'].')' : '' );
break;
case 'money':
$data_type = 'decimal(19,4)';
break;
case 'smallmoney':
$data_type = 'decimal(10,4)';
break;
case 'real':
case 'float':
case 'decimal':
case 'numeric':
$data_type = $row['DATA_TYPE'].(!empty($row['NUMERIC_PRECISION']) ? '('.$row['NUMERIC_PRECISION'].(!empty($row['NUMERIC_SCALE']) ? ','.$row['NUMERIC_SCALE'] : '').')' : '' );
break;
case 'date':
case 'datetime':
case 'timestamp':
case 'time':
$data_type = $row['DATA_TYPE'];
case 'datetime2':
case 'datetimeoffset':
case 'smalldatetime':
$data_type = 'datetime';
break;
case 'nchar':
case 'char':
$data_type = 'char'.(!empty($row['CHARACTER_MAXIMUM_LENGTH']) && $row['CHARACTER_MAXIMUM_LENGTH'] > 0 ? '('.$row['CHARACTER_MAXIMUM_LENGTH'].')' : '(255)' );
break;
case 'nvarchar':
case 'varchar':
$data_type = 'varchar'.(!empty($row['CHARACTER_MAXIMUM_LENGTH']) && $row['CHARACTER_MAXIMUM_LENGTH'] > 0 ? '('.$row['CHARACTER_MAXIMUM_LENGTH'].')' : '(255)' );
break;
case 'ntext':
case 'text':
$data_type = 'text';
break;
case 'binary':
case 'varbinary':
$data_type = $data_type = $row['DATA_TYPE'];
case 'image':
$data_type = 'blob';
break;
case 'uniqueidentifier':
$data_type = 'char(36)';//'CHAR(36) NOT NULL';
break;
case 'cursor':
case 'hierarchyid':
case 'sql_variant':
case 'table':
case 'xml':
default:
$data_type = false;
break;
}
if (!empty($data_type))
{
$ssql = "`".$row['COLUMN_NAME']."` ".$data_type." ".($row['IS_NULLABLE'] == 'YES' ? 'NULL' : 'NOT NULL');
array_push($strctsql, $ssql);
array_push($fields, $row['COLUMN_NAME']);
}
}
$mysql .= "(".implode(',', $strctsql).");";
echo "======> Creating table ".$table." on MySQL... ";
$q = $mysqli->query($mysql);
echo (($q) ? 'Success':'Failed!'."\n".$mysql."\n")."\n";
echo "=====> Getting data from table ".$table." on SQL Server\n";
$sql = "SELECT * FROM ".$table.' where 1=1';
$qe = $mssql->prepare($sql);
$qe->execute();
$numrow = $qe->rowCount();
echo "======> Found ".number_format($numrow,0,',','.')." rows\n";
if ($numrow)
{
echo "=====> Inserting to table ".$table." on MySQL\n";
$numdata = 0;
if (!empty($fields))
{
$sfield = array_map('addTilde', $fields);
while ($qrow = $qe->fetch(PDO::FETCH_ASSOC))
{
$datas = array();
foreach ($fields as $field)
{
$ddata = (!empty($qrow[$field])) ? $qrow[$field] : '';
array_push($datas,"'".$mysqli->real_escape_string($ddata)."'");
}
if (!empty($datas))
{
$mysql = "INSERT INTO `".$table."` (".implode(',',$sfield).") VALUES (".implode(',',$datas).");";
$q = $mysqli->query($mysql);
$numdata += ($q ? 1 : 0 );
}
}
}
echo "======> ".number_format($numdata,0,',','.')." data inserted\n\n";
}
}
$i++;
}
}
echo "Done!\n";
$qe = null;
$mssql = null;
$mysqli->close();
$mysqli = null;