-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.php
More file actions
296 lines (253 loc) · 8.49 KB
/
Copy pathexport.php
File metadata and controls
296 lines (253 loc) · 8.49 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
set_time_limit(0);
ini_set('memory_limit', '1024M');
class ExportConnection
{
private $server_name = "localhost";
private $username = "root";
private $password = "";
private $port = "3306";
private $db_name = "";
private $conn = null;
private $sql_migration_query = "";
private $tables = [];
private $column_names = [];
private $columns_obj = [];
private $insertion_column_names_cahced;
public function __construct($server_name, $username, $password, $port, $db_name)
{
$this->server_name = $server_name;
$this->username = $username;
$this->password = $password;
$this->port = $port;
$this->db_name = $db_name;
$this->createConnection();
}
private function createConnection()
{
if ($this->conn != null) {
return;
}
$this->conn = new mysqli($this->server_name, $this->username, $this->password, $this->db_name, $this->port);
if ($this->conn->connect_error) {
die("Connection failed:" . $this->conn->connect_error);
}
}
function getConn()
{
return $this->conn;
}
private function getColumnDefaultDesc($field, $default)
{
if ($default != "") {
return "DEFAULT '" . $default . "'";
}
return "";
}
private function getColumnExtraDesc($field, $extra)
{
return $extra;
}
function getColumnKeyDesc($field, $key)
{
if ($key == "PRI") {
return "PRIMARY KEY";
} else if ($key == "UNI") {
return "UNIQUE";
}/* else if ($key == "MUL") {
return "index(" . $field . ")";
}*/
return "";
}
function getRowsInRange($table_name, $limit, $offset)
{
$query = "SELECT * FROM $table_name LIMIT $limit OFFSET $offset";
return $this->executeQueryFetch($query);
}
function getTableInfo($table_name)
{
$query = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='$table_name'";
return $this->executeQueryFetch($query);
}
function executeQuery(string $query)
{
$result = $this->conn->query($query);
if (!$result) die(mysqli_error($this->conn));
return $result;
}
function executeQueryFetch(string $query)
{
$result = $this->executeQuery($query)->fetch_all();
return $result;
}
function exportTables(array $tables = [])
{
try {
if (!$this->dbExist($this->db_name)) {
die("Invalid Database Name");
}
if (count($tables) > 0) {
['isError' => $isError, 'errMsg' => $errMsg] = $this->tablesExist($tables);
if ($isError) die($errMsg);
} else {
$tables = $this->fetchAllTables();
}
$this->tables = $tables;
$this->createMigrationSchema();
$this->createSqlFile();
return $this->sql_migration_query;
} catch (\Exception $e) {
throw new Exception($e->getMessage());
}
}
function createJsonFile($query)
{
if ($query == "" || $query == null) return;
$filename = 'queries.json';
$dir = './' . $filename;
$data = [
"query" => $query,
'execute' => false,
'success' => false
];
if (!file_exists($dir)) {
$queries = [];
array_push($queries, $data);
$handle = fopen($filename, "w+");
fwrite($handle, json_encode($queries));
fclose($handle);
} else {
// get and push to qurery
$prevqueries = (array) json_decode(file_get_contents($dir));
array_push($prevqueries, $data);
$handle = fopen($filename, "w");
fwrite($handle, json_encode($prevqueries));
fclose($handle);
}
}
function createSqlFile()
{
$handle = fopen($this->db_name . time() . '.sql', 'w+');
fwrite($handle, $this->sql_migration_query);
fclose($handle);
}
function createMigrationSchema()
{
$this->sql_migration_query = "";
foreach ($this->tables as $table) {
$table = is_array($table) ? $table[0] : $table;
$tbl_schema = $this->createTableSchema($table);
$this->sql_migration_query .= "\n\n" . $tbl_schema . "\n\n\n";
$this->createJsonFile($tbl_schema);
$tbl_insert_schema = $this->createInsertTableSchema($table);
$this->sql_migration_query .= "\n\n" . $tbl_insert_schema . "\n\n\n";
$this->createJsonFile($tbl_insert_schema);
}
}
private function createTableSchema($table_name)
{
$query = "CREATE TABLE IF NOT EXISTS `$table_name`(\n";
$this->insertion_column_names_cahced = "";
$rows = $this->getTableHeader($table_name);
$rows_count = count($rows);
foreach ($rows as $key => $val) {
$col = new MySQLColumns();
$col->Field = $val[0];
$col->Type = $val[1];
$col->Null = $val[2];
$col->Key = $val[3];
$col->Default = $val[4];
$col->Extra = $val[5];
array_push($this->columns_obj, $col);
array_push($this->column_names, $col->Field);
$query .= " `" . $col->Field . "` " . $col->Type . " " . $this->getColumnExtraDesc($col->Field, $col->Extra) . "" . (!$col->Null ? "NOT NULL" : "") . " " . $this->getColumnDefaultDesc($col->Field, $col->Default) . " " . $this->getColumnKeyDesc($col->Field, $col->Key) . ($key < $rows_count - 1 ? "," : "") . "\n";
$this->insertion_column_names_cahced .= "`" . $col->Field . "`" . ($key < $rows_count - 1 ? "," : "");
}
$query .= ");";
return $query;
}
private function createInsertTableSchema($table_name)
{
$table_info = $this->getTableInfo($table_name)[0];
$table_rows = $table_info[7];
if ($table_rows == 0) return "";
$default_limit = 500;
$default_offset = 0;
$run_count = ceil($table_rows / $default_limit);
$table_data = [];
$query = "INSERT INTO `$table_name` (" .
$this->insertion_column_names_cahced . ") VALUES ";
$track = 0;
for ($i = 0; $i <= $run_count; $i++) {
if ($i == 1) {
$default_offset = $default_limit;
} else if ($i > 1) {
$default_offset += $default_limit;
}
$get_data_in_range = $this->getRowsInRange($table_name, $default_limit, $default_offset);
for ($j = 0; $j < count($get_data_in_range); $j++) {
$track++;
$query .= "(";
$curr = $get_data_in_range[$j];
for ($k = 0; $k < count($curr); $k++) {
$col_val = 'NULL';
if ($curr[$k] != null && $curr[$k] != "") {
$col_val = "'" . $curr[$k] . "'";
}
$query .= $col_val . ($k == count($curr) - 1 ? "" : ",") . "";
}
$query .= ")" . ($track == $table_rows ? ";" : ", \n") . "";
}
}
return $query;
}
/**
* @param array $tables
*
* @return array ['isError' => $isError, 'errMsg' => $errMsg]
*/
function tablesExist(array $tables)
{
foreach ($tables as $table) {
$query = "DESCRIBE $table";
$tb = $this->executeQueryFetch($query);
if (!count($tb) > 0) {
return ['isError' => true, 'errMsg' => 'Invalid table ' . $table];
}
}
}
function dbExist($db_name)
{
$db = $this->executeQueryFetch("SHOW DATABASES LIKE '$db_name'");
return count($db) > 0 ? true : false;
}
function fetchAllDatabases()
{
$result_row = $this->executeQueryFetch("SHOW DATABASES;");
return $result_row;
}
function fetchAllTables()
{
$result_row = $this->executeQueryFetch("SHOW TABLES");
$tables = [];
foreach ($result_row as $key => $val) {
array_push($tables, $val);
}
return $tables;
}
function getTableHeader($table_name)
{
$query = "DESCRIBE $table_name";
$result_row = $this->executeQueryFetch($query);
return $result_row;
}
}
class MySQLColumns
{
public $Field;
public $Type;
public $Null;
public $Key;
public $Default;
public $Extra;
}