250 |
kaklik |
1 |
<?php |
|
|
2 |
/* $Id: sql.php,v 2.53.2.1 2006/05/05 09:20:37 nijel Exp $ */ |
|
|
3 |
// vim: expandtab sw=4 ts=4 sts=4: |
|
|
4 |
/** |
|
|
5 |
* Set of functions used to build SQL dumps of tables |
|
|
6 |
*/ |
|
|
7 |
|
|
|
8 |
/** |
|
|
9 |
* Marker for comments, -- is needed for ANSI SQL. |
|
|
10 |
*/ |
|
|
11 |
$GLOBALS['comment_marker'] = '-- '; |
|
|
12 |
|
|
|
13 |
/** |
|
|
14 |
* Avoids undefined variables, use NULL so isset() returns false |
|
|
15 |
*/ |
|
|
16 |
if ( ! isset( $use_backquotes ) ) { |
|
|
17 |
$use_backquotes = null; |
|
|
18 |
} |
|
|
19 |
|
|
|
20 |
/** |
|
|
21 |
* Outputs comment |
|
|
22 |
* |
|
|
23 |
* @param string Text of comment |
|
|
24 |
* |
|
|
25 |
* @return bool Whether it suceeded |
|
|
26 |
*/ |
|
|
27 |
function PMA_exportComment($text) |
|
|
28 |
{ |
|
|
29 |
return PMA_exportOutputHandler($GLOBALS['comment_marker'] . $text . $GLOBALS['crlf']); |
|
|
30 |
} |
|
|
31 |
|
|
|
32 |
/** |
|
|
33 |
* Outputs export footer |
|
|
34 |
* |
|
|
35 |
* @return bool Whether it suceeded |
|
|
36 |
* |
|
|
37 |
* @access public |
|
|
38 |
*/ |
|
|
39 |
function PMA_exportFooter() |
|
|
40 |
{ |
|
|
41 |
global $crlf; |
|
|
42 |
|
|
|
43 |
$foot = ''; |
|
|
44 |
|
|
|
45 |
if (isset($GLOBALS['disable_fk'])) { |
|
|
46 |
$foot .= $crlf . 'SET FOREIGN_KEY_CHECKS=1;' . $crlf; |
|
|
47 |
} |
|
|
48 |
|
|
|
49 |
if (isset($GLOBALS['use_transaction'])) { |
|
|
50 |
$foot .= $crlf . 'COMMIT;' . $crlf; |
|
|
51 |
} |
|
|
52 |
|
|
|
53 |
return PMA_exportOutputHandler($foot); |
|
|
54 |
} |
|
|
55 |
|
|
|
56 |
/** |
|
|
57 |
* Outputs export header |
|
|
58 |
* |
|
|
59 |
* @return bool Whether it suceeded |
|
|
60 |
* |
|
|
61 |
* @access public |
|
|
62 |
*/ |
|
|
63 |
function PMA_exportHeader() |
|
|
64 |
{ |
|
|
65 |
global $crlf; |
|
|
66 |
global $cfg; |
|
|
67 |
|
|
|
68 |
if (PMA_MYSQL_INT_VERSION >= 40100 && isset($GLOBALS['sql_compat']) && $GLOBALS['sql_compat'] != 'NONE') { |
|
|
69 |
PMA_DBI_try_query('SET SQL_MODE="' . $GLOBALS['sql_compat'] . '"'); |
|
|
70 |
} |
|
|
71 |
|
|
|
72 |
$head = $GLOBALS['comment_marker'] . 'phpMyAdmin SQL Dump' . $crlf |
|
|
73 |
. $GLOBALS['comment_marker'] . 'version ' . PMA_VERSION . $crlf |
|
|
74 |
. $GLOBALS['comment_marker'] . 'http://www.phpmyadmin.net' . $crlf |
|
|
75 |
. $GLOBALS['comment_marker'] . $crlf |
|
|
76 |
. $GLOBALS['comment_marker'] . $GLOBALS['strHost'] . ': ' . $cfg['Server']['host']; |
|
|
77 |
if (!empty($cfg['Server']['port'])) { |
|
|
78 |
$head .= ':' . $cfg['Server']['port']; |
|
|
79 |
} |
|
|
80 |
$head .= $crlf |
|
|
81 |
. $GLOBALS['comment_marker'] . $GLOBALS['strGenTime'] . ': ' . PMA_localisedDate() . $crlf |
|
|
82 |
. $GLOBALS['comment_marker'] . $GLOBALS['strServerVersion'] . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 3) . $crlf |
|
|
83 |
. $GLOBALS['comment_marker'] . $GLOBALS['strPHPVersion'] . ': ' . phpversion() . $crlf; |
|
|
84 |
|
|
|
85 |
if (isset($GLOBALS['header_comment']) && !empty($GLOBALS['header_comment'])) { |
|
|
86 |
$lines = explode('\n', $GLOBALS['header_comment']); |
|
|
87 |
$head .= $GLOBALS['comment_marker'] . $crlf |
|
|
88 |
. $GLOBALS['comment_marker'] . implode($crlf . $GLOBALS['comment_marker'], $lines) . $crlf |
|
|
89 |
. $GLOBALS['comment_marker'] . $crlf; |
|
|
90 |
} |
|
|
91 |
|
|
|
92 |
if (isset($GLOBALS['disable_fk'])) { |
|
|
93 |
$head .= $crlf . 'SET FOREIGN_KEY_CHECKS=0;' . $crlf; |
|
|
94 |
} |
|
|
95 |
|
|
|
96 |
if (isset($GLOBALS['use_transaction'])) { |
|
|
97 |
$head .= $crlf .'SET AUTOCOMMIT=0;' . $crlf |
|
|
98 |
. 'START TRANSACTION;' . $crlf . $crlf; |
|
|
99 |
} |
|
|
100 |
|
|
|
101 |
return PMA_exportOutputHandler($head); |
|
|
102 |
} |
|
|
103 |
|
|
|
104 |
/** |
|
|
105 |
* Outputs create database database |
|
|
106 |
* |
|
|
107 |
* @param string Database name |
|
|
108 |
* |
|
|
109 |
* @return bool Whether it suceeded |
|
|
110 |
* |
|
|
111 |
* @access public |
|
|
112 |
*/ |
|
|
113 |
function PMA_exportDBCreate($db) |
|
|
114 |
{ |
|
|
115 |
global $crlf; |
|
|
116 |
if (isset($GLOBALS['drop_database'])) { |
|
|
117 |
if (!PMA_exportOutputHandler('DROP DATABASE ' . (isset($GLOBALS['use_backquotes']) ? PMA_backquote($db) : $db) . ';' . $crlf)) { |
|
|
118 |
return FALSE; |
|
|
119 |
} |
|
|
120 |
} |
|
|
121 |
$create_query = 'CREATE DATABASE ' . (isset($GLOBALS['use_backquotes']) ? PMA_backquote($db) : $db); |
|
|
122 |
if (PMA_MYSQL_INT_VERSION >= 40101) { |
|
|
123 |
$collation = PMA_getDbCollation($db); |
|
|
124 |
if (strpos($collation, '_')) { |
|
|
125 |
$create_query .= ' DEFAULT CHARACTER SET ' . substr($collation, 0, strpos($collation, '_')) . ' COLLATE ' . $collation; |
|
|
126 |
} else { |
|
|
127 |
$create_query .= ' DEFAULT CHARACTER SET ' . $collation; |
|
|
128 |
} |
|
|
129 |
} |
|
|
130 |
$create_query .= ';' . $crlf; |
|
|
131 |
if (!PMA_exportOutputHandler($create_query)) { |
|
|
132 |
return FALSE; |
|
|
133 |
} |
|
|
134 |
if (isset($GLOBALS['use_backquotes']) && PMA_MYSQL_INT_VERSION >= 40100 && isset($GLOBALS['sql_compat']) && $GLOBALS['sql_compat'] == 'NONE') { |
|
|
135 |
return PMA_exportOutputHandler('USE ' . PMA_backquote($db) . ';' . $crlf); |
|
|
136 |
} |
|
|
137 |
return PMA_exportOutputHandler('USE ' . $db . ';' . $crlf); |
|
|
138 |
} |
|
|
139 |
|
|
|
140 |
/** |
|
|
141 |
* Outputs database header |
|
|
142 |
* |
|
|
143 |
* @param string Database name |
|
|
144 |
* |
|
|
145 |
* @return bool Whether it suceeded |
|
|
146 |
* |
|
|
147 |
* @access public |
|
|
148 |
*/ |
|
|
149 |
function PMA_exportDBHeader($db) |
|
|
150 |
{ |
|
|
151 |
global $crlf; |
|
|
152 |
$head = $GLOBALS['comment_marker'] . $crlf |
|
|
153 |
. $GLOBALS['comment_marker'] . $GLOBALS['strDatabase'] . ': ' . (isset($GLOBALS['use_backquotes']) ? PMA_backquote($db) : '\'' . $db . '\''). $crlf |
|
|
154 |
. $GLOBALS['comment_marker'] . $crlf; |
|
|
155 |
return PMA_exportOutputHandler($head); |
|
|
156 |
} |
|
|
157 |
|
|
|
158 |
/** |
|
|
159 |
* Outputs database footer |
|
|
160 |
* |
|
|
161 |
* @param string Database name |
|
|
162 |
* |
|
|
163 |
* @return bool Whether it suceeded |
|
|
164 |
* |
|
|
165 |
* @access public |
|
|
166 |
*/ |
|
|
167 |
function PMA_exportDBFooter($db) |
|
|
168 |
{ |
|
|
169 |
$result = TRUE; |
|
|
170 |
if (isset($GLOBALS['sql_constraints'])) { |
|
|
171 |
$result = PMA_exportOutputHandler($GLOBALS['sql_constraints']); |
|
|
172 |
unset($GLOBALS['sql_constraints']); |
|
|
173 |
} |
|
|
174 |
return $result; |
|
|
175 |
} |
|
|
176 |
|
|
|
177 |
/** |
|
|
178 |
* Returns $table's CREATE definition |
|
|
179 |
* |
|
|
180 |
* @param string the database name |
|
|
181 |
* @param string the table name |
|
|
182 |
* @param string the end of line sequence |
|
|
183 |
* @param string the url to go back in case of error |
|
|
184 |
* @param boolean whether to include creation/update/check dates |
|
|
185 |
* |
|
|
186 |
* @return string resulting schema |
|
|
187 |
* |
|
|
188 |
* @global boolean whether to add 'drop' statements or not |
|
|
189 |
* @global boolean whether to use backquotes to allow the use of special |
|
|
190 |
* characters in database, table and fields names or not |
|
|
191 |
* |
|
|
192 |
* @access public |
|
|
193 |
*/ |
|
|
194 |
function PMA_getTableDef($db, $table, $crlf, $error_url, $show_dates = false) |
|
|
195 |
{ |
|
|
196 |
global $drop; |
|
|
197 |
global $use_backquotes; |
|
|
198 |
global $cfgRelation; |
|
|
199 |
global $sql_constraints; |
|
|
200 |
|
|
|
201 |
$schema_create = ''; |
|
|
202 |
$auto_increment = ''; |
|
|
203 |
$new_crlf = $crlf; |
|
|
204 |
|
|
|
205 |
// need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli |
|
|
206 |
$result = PMA_DBI_query('SHOW TABLE STATUS FROM ' . PMA_backquote($db) . ' LIKE \'' . PMA_sqlAddslashes($table) . '\'', null, PMA_DBI_QUERY_STORE); |
|
|
207 |
if ($result != FALSE) { |
|
|
208 |
if (PMA_DBI_num_rows($result) > 0) { |
|
|
209 |
$tmpres = PMA_DBI_fetch_assoc($result); |
|
|
210 |
if (isset($GLOBALS['sql_auto_increment']) && !empty($tmpres['Auto_increment'])) { |
|
|
211 |
$auto_increment .= ' AUTO_INCREMENT=' . $tmpres['Auto_increment'] . ' '; |
|
|
212 |
} |
|
|
213 |
|
|
|
214 |
if ($show_dates && isset($tmpres['Create_time']) && !empty($tmpres['Create_time'])) { |
|
|
215 |
$schema_create .= $GLOBALS['comment_marker'] . $GLOBALS['strStatCreateTime'] . ': ' . PMA_localisedDate(strtotime($tmpres['Create_time'])) . $crlf; |
|
|
216 |
$new_crlf = $GLOBALS['comment_marker'] . $crlf . $crlf; |
|
|
217 |
} |
|
|
218 |
|
|
|
219 |
if ($show_dates && isset($tmpres['Update_time']) && !empty($tmpres['Update_time'])) { |
|
|
220 |
$schema_create .= $GLOBALS['comment_marker'] . $GLOBALS['strStatUpdateTime'] . ': ' . PMA_localisedDate(strtotime($tmpres['Update_time'])) . $crlf; |
|
|
221 |
$new_crlf = $GLOBALS['comment_marker'] . $crlf . $crlf; |
|
|
222 |
} |
|
|
223 |
|
|
|
224 |
if ($show_dates && isset($tmpres['Check_time']) && !empty($tmpres['Check_time'])) { |
|
|
225 |
$schema_create .= $GLOBALS['comment_marker'] . $GLOBALS['strStatCheckTime'] . ': ' . PMA_localisedDate(strtotime($tmpres['Check_time'])) . $crlf; |
|
|
226 |
$new_crlf = $GLOBALS['comment_marker'] . $crlf . $crlf; |
|
|
227 |
} |
|
|
228 |
} |
|
|
229 |
PMA_DBI_free_result($result); |
|
|
230 |
} |
|
|
231 |
|
|
|
232 |
$schema_create .= $new_crlf; |
|
|
233 |
|
|
|
234 |
if (!empty($drop)) { |
|
|
235 |
$schema_create .= 'DROP TABLE IF EXISTS ' . PMA_backquote($table, $use_backquotes) . ';' . $crlf; |
|
|
236 |
} |
|
|
237 |
|
|
|
238 |
// Steve Alberty's patch for complete table dump, |
|
|
239 |
// Whether to quote table and fields names or not |
|
|
240 |
if ($use_backquotes) { |
|
|
241 |
PMA_DBI_query('SET SQL_QUOTE_SHOW_CREATE = 1'); |
|
|
242 |
} else { |
|
|
243 |
PMA_DBI_query('SET SQL_QUOTE_SHOW_CREATE = 0'); |
|
|
244 |
} |
|
|
245 |
|
|
|
246 |
$result = PMA_DBI_query('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table), null, PMA_DBI_QUERY_UNBUFFERED); |
|
|
247 |
if ($result != FALSE && ($row = PMA_DBI_fetch_row($result))) { |
|
|
248 |
$create_query = $row[1]; |
|
|
249 |
unset($row); |
|
|
250 |
|
|
|
251 |
// Convert end of line chars to one that we want (note that MySQL doesn't return query it will accept in all cases) |
|
|
252 |
if (strpos($create_query, "(\r\n ")) { |
|
|
253 |
$create_query = str_replace("\r\n", $crlf, $create_query); |
|
|
254 |
} elseif (strpos($create_query, "(\n ")) { |
|
|
255 |
$create_query = str_replace("\n", $crlf, $create_query); |
|
|
256 |
} elseif (strpos($create_query, "(\r ")) { |
|
|
257 |
$create_query = str_replace("\r", $crlf, $create_query); |
|
|
258 |
} |
|
|
259 |
|
|
|
260 |
// Should we use IF NOT EXISTS? |
|
|
261 |
if (isset($GLOBALS['if_not_exists'])) { |
|
|
262 |
$create_query = preg_replace('/^CREATE TABLE/', 'CREATE TABLE IF NOT EXISTS', $create_query); |
|
|
263 |
} |
|
|
264 |
|
|
|
265 |
// are there any constraints to cut out? |
|
|
266 |
if (preg_match('@CONSTRAINT|FOREIGN[\s]+KEY@', $create_query)) { |
|
|
267 |
|
|
|
268 |
// Split the query into lines, so we can easily handle it. We know lines are separated by $crlf (done few lines above). |
|
|
269 |
$sql_lines = explode($crlf, $create_query); |
|
|
270 |
$sql_count = count($sql_lines); |
|
|
271 |
|
|
|
272 |
// lets find first line with constraints |
|
|
273 |
for ($i = 0; $i < $sql_count; $i++) { |
|
|
274 |
if (preg_match('@^[\s]*(CONSTRAINT|FOREIGN[\s]+KEY)@', $sql_lines[$i])) { |
|
|
275 |
break; |
|
|
276 |
} |
|
|
277 |
} |
|
|
278 |
|
|
|
279 |
// If we really found a constraint |
|
|
280 |
if ($i != $sql_count) { |
|
|
281 |
|
|
|
282 |
// remove , from the end of create statement |
|
|
283 |
$sql_lines[$i - 1] = preg_replace('@,$@', '', $sql_lines[$i - 1]); |
|
|
284 |
|
|
|
285 |
// prepare variable for constraints |
|
|
286 |
if (!isset($sql_constraints)) { |
|
|
287 |
if (isset($GLOBALS['no_constraints_comments'])) { |
|
|
288 |
$sql_constraints = ''; |
|
|
289 |
} else { |
|
|
290 |
$sql_constraints = $crlf . $GLOBALS['comment_marker'] . |
|
|
291 |
$crlf . $GLOBALS['comment_marker'] . $GLOBALS['strConstraintsForDumped'] . |
|
|
292 |
$crlf . $GLOBALS['comment_marker'] . $crlf; |
|
|
293 |
} |
|
|
294 |
} |
|
|
295 |
|
|
|
296 |
// comments for current table |
|
|
297 |
if (!isset($GLOBALS['no_constraints_comments'])) { |
|
|
298 |
$sql_constraints .= $crlf . $GLOBALS['comment_marker'] . |
|
|
299 |
$crlf . $GLOBALS['comment_marker'] . $GLOBALS['strConstraintsForTable'] . ' ' . PMA_backquote($table) . |
|
|
300 |
$crlf . $GLOBALS['comment_marker'] . $crlf; |
|
|
301 |
} |
|
|
302 |
|
|
|
303 |
// let's do the work |
|
|
304 |
$sql_constraints .= 'ALTER TABLE ' . PMA_backquote($table) . $crlf; |
|
|
305 |
|
|
|
306 |
$first = TRUE; |
|
|
307 |
for ($j = $i; $j < $sql_count; $j++) { |
|
|
308 |
if (preg_match('@CONSTRAINT|FOREIGN[\s]+KEY@', $sql_lines[$j])) { |
|
|
309 |
if (!$first) { |
|
|
310 |
$sql_constraints .= $crlf; |
|
|
311 |
} |
|
|
312 |
if (strpos($sql_lines[$j], 'CONSTRAINT') === FALSE) { |
|
|
313 |
$sql_constraints .= preg_replace('/(FOREIGN[\s]+KEY)/', 'ADD \1', $sql_lines[$j]); |
|
|
314 |
} else { |
|
|
315 |
$sql_constraints .= preg_replace('/(CONSTRAINT)/', 'ADD \1', $sql_lines[$j]); |
|
|
316 |
} |
|
|
317 |
$first = FALSE; |
|
|
318 |
} else { |
|
|
319 |
break; |
|
|
320 |
} |
|
|
321 |
} |
|
|
322 |
$sql_constraints .= ';' . $crlf; |
|
|
323 |
$create_query = implode($crlf, array_slice($sql_lines, 0, $i)) . $crlf . implode($crlf, array_slice($sql_lines, $j, $sql_count - 1)); |
|
|
324 |
unset($sql_lines); |
|
|
325 |
} |
|
|
326 |
} |
|
|
327 |
$schema_create .= $create_query; |
|
|
328 |
} |
|
|
329 |
|
|
|
330 |
$schema_create .= $auto_increment; |
|
|
331 |
|
|
|
332 |
PMA_DBI_free_result($result); |
|
|
333 |
return $schema_create; |
|
|
334 |
} // end of the 'PMA_getTableDef()' function |
|
|
335 |
|
|
|
336 |
|
|
|
337 |
/** |
|
|
338 |
* Returns $table's comments, relations etc. |
|
|
339 |
* |
|
|
340 |
* @param string the database name |
|
|
341 |
* @param string the table name |
|
|
342 |
* @param string the end of line sequence |
|
|
343 |
* @param boolean whether to include relation comments |
|
|
344 |
* @param boolean whether to include column comments |
|
|
345 |
* @param boolean whether to include mime comments |
|
|
346 |
* |
|
|
347 |
* @return string resulting comments |
|
|
348 |
* |
|
|
349 |
* @access public |
|
|
350 |
*/ |
|
|
351 |
function PMA_getTableComments($db, $table, $crlf, $do_relation = false, $do_comments = false, $do_mime = false) |
|
|
352 |
{ |
|
|
353 |
global $cfgRelation; |
|
|
354 |
global $use_backquotes; |
|
|
355 |
global $sql_constraints; |
|
|
356 |
|
|
|
357 |
$schema_create = ''; |
|
|
358 |
|
|
|
359 |
// triggered only for MySQL < 4.1.x (pmadb-style comments) |
|
|
360 |
if ($do_comments && $cfgRelation['commwork']) { |
|
|
361 |
if (!($comments_map = PMA_getComments($db, $table))) { |
|
|
362 |
unset($comments_map); |
|
|
363 |
} |
|
|
364 |
} |
|
|
365 |
|
|
|
366 |
// Check if we can use Relations (Mike Beck) |
|
|
367 |
if ($do_relation && !empty($cfgRelation['relation'])) { |
|
|
368 |
// Find which tables are related with the current one and write it in |
|
|
369 |
// an array |
|
|
370 |
$res_rel = PMA_getForeigners($db, $table); |
|
|
371 |
|
|
|
372 |
if ($res_rel && count($res_rel) > 0) { |
|
|
373 |
$have_rel = TRUE; |
|
|
374 |
} else { |
|
|
375 |
$have_rel = FALSE; |
|
|
376 |
} |
|
|
377 |
} else { |
|
|
378 |
$have_rel = FALSE; |
|
|
379 |
} // end if |
|
|
380 |
|
|
|
381 |
if ($do_mime && $cfgRelation['mimework']) { |
|
|
382 |
if (!($mime_map = PMA_getMIME($db, $table, true))) { |
|
|
383 |
unset($mime_map); |
|
|
384 |
} |
|
|
385 |
} |
|
|
386 |
|
|
|
387 |
if (isset($comments_map) && count($comments_map) > 0) { |
|
|
388 |
$schema_create .= $crlf . $GLOBALS['comment_marker'] . $crlf |
|
|
389 |
. $GLOBALS['comment_marker'] . $GLOBALS['strCommentsForTable']. ' ' . PMA_backquote($table, $use_backquotes) . ':' . $crlf; |
|
|
390 |
foreach ($comments_map AS $comment_field => $comment) { |
|
|
391 |
$schema_create .= $GLOBALS['comment_marker'] . ' ' . PMA_backquote($comment_field, $use_backquotes) . $crlf |
|
|
392 |
. $GLOBALS['comment_marker'] . ' ' . PMA_backquote($comment, $use_backquotes) . $crlf; |
|
|
393 |
} |
|
|
394 |
$schema_create .= $GLOBALS['comment_marker'] . $crlf; |
|
|
395 |
} |
|
|
396 |
|
|
|
397 |
if (isset($mime_map) && count($mime_map) > 0) { |
|
|
398 |
$schema_create .= $crlf . $GLOBALS['comment_marker'] . $crlf |
|
|
399 |
. $GLOBALS['comment_marker'] . $GLOBALS['strMIMETypesForTable']. ' ' . PMA_backquote($table, $use_backquotes) . ':' . $crlf; |
|
|
400 |
@reset($mime_map); |
|
|
401 |
foreach ($mime_map AS $mime_field => $mime) { |
|
|
402 |
$schema_create .= $GLOBALS['comment_marker'] . ' ' . PMA_backquote($mime_field, $use_backquotes) . $crlf |
|
|
403 |
. $GLOBALS['comment_marker'] . ' ' . PMA_backquote($mime['mimetype'], $use_backquotes) . $crlf; |
|
|
404 |
} |
|
|
405 |
$schema_create .= $GLOBALS['comment_marker'] . $crlf; |
|
|
406 |
} |
|
|
407 |
|
|
|
408 |
if ($have_rel) { |
|
|
409 |
$schema_create .= $crlf . $GLOBALS['comment_marker'] . $crlf |
|
|
410 |
. $GLOBALS['comment_marker'] . $GLOBALS['strRelationsForTable']. ' ' . PMA_backquote($table, $use_backquotes) . ':' . $crlf; |
|
|
411 |
foreach ($res_rel AS $rel_field => $rel) { |
|
|
412 |
$schema_create .= $GLOBALS['comment_marker'] . ' ' . PMA_backquote($rel_field, $use_backquotes) . $crlf |
|
|
413 |
. $GLOBALS['comment_marker'] . ' ' . PMA_backquote($rel['foreign_table'], $use_backquotes) |
|
|
414 |
. ' -> ' . PMA_backquote($rel['foreign_field'], $use_backquotes) . $crlf; |
|
|
415 |
} |
|
|
416 |
$schema_create .= $GLOBALS['comment_marker'] . $crlf; |
|
|
417 |
} |
|
|
418 |
|
|
|
419 |
return $schema_create; |
|
|
420 |
|
|
|
421 |
} // end of the 'PMA_getTableComments()' function |
|
|
422 |
|
|
|
423 |
/** |
|
|
424 |
* Outputs table's structure |
|
|
425 |
* |
|
|
426 |
* @param string the database name |
|
|
427 |
* @param string the table name |
|
|
428 |
* @param string the end of line sequence |
|
|
429 |
* @param string the url to go back in case of error |
|
|
430 |
* @param boolean whether to include relation comments |
|
|
431 |
* @param boolean whether to include column comments |
|
|
432 |
* @param boolean whether to include mime comments |
|
|
433 |
* |
|
|
434 |
* @return bool Whether it suceeded |
|
|
435 |
* |
|
|
436 |
* @access public |
|
|
437 |
*/ |
|
|
438 |
function PMA_exportStructure($db, $table, $crlf, $error_url, $relation = FALSE, $comments = FALSE, $mime = FALSE, $dates = FALSE) |
|
|
439 |
{ |
|
|
440 |
$formatted_table_name = (isset($GLOBALS['use_backquotes'])) |
|
|
441 |
? PMA_backquote($table) |
|
|
442 |
: '\'' . $table . '\''; |
|
|
443 |
$dump = $crlf |
|
|
444 |
. $GLOBALS['comment_marker'] . '--------------------------------------------------------' . $crlf |
|
|
445 |
. $crlf . $GLOBALS['comment_marker'] . $crlf |
|
|
446 |
. $GLOBALS['comment_marker'] . $GLOBALS['strTableStructure'] . ' ' . $formatted_table_name . $crlf |
|
|
447 |
. $GLOBALS['comment_marker'] . $crlf |
|
|
448 |
. PMA_getTableDef($db, $table, $crlf, $error_url, $dates) . ';' . $crlf |
|
|
449 |
. PMA_getTableComments($db, $table, $crlf, $relation, $comments, $mime); |
|
|
450 |
|
|
|
451 |
|
|
|
452 |
return PMA_exportOutputHandler($dump); |
|
|
453 |
} |
|
|
454 |
|
|
|
455 |
/** |
|
|
456 |
* Dispatches between the versions of 'getTableContent' to use depending |
|
|
457 |
* on the php version |
|
|
458 |
* |
|
|
459 |
* @param string the database name |
|
|
460 |
* @param string the table name |
|
|
461 |
* @param string the end of line sequence |
|
|
462 |
* @param string the url to go back in case of error |
|
|
463 |
* @param string SQL query for obtaining data |
|
|
464 |
* |
|
|
465 |
* @return bool Whether it suceeded |
|
|
466 |
* |
|
|
467 |
* @global boolean whether to use backquotes to allow the use of special |
|
|
468 |
* characters in database, table and fields names or not |
|
|
469 |
* @global integer the number of records |
|
|
470 |
* @global integer the current record position |
|
|
471 |
* |
|
|
472 |
* @access public |
|
|
473 |
* |
|
|
474 |
* @see PMA_getTableContentFast(), PMA_getTableContentOld() |
|
|
475 |
* |
|
|
476 |
* @author staybyte |
|
|
477 |
*/ |
|
|
478 |
function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) |
|
|
479 |
{ |
|
|
480 |
global $use_backquotes; |
|
|
481 |
global $rows_cnt; |
|
|
482 |
global $current_row; |
|
|
483 |
|
|
|
484 |
$formatted_table_name = (isset($GLOBALS['use_backquotes'])) |
|
|
485 |
? PMA_backquote($table) |
|
|
486 |
: '\'' . $table . '\''; |
|
|
487 |
$head = $crlf |
|
|
488 |
. $GLOBALS['comment_marker'] . $crlf |
|
|
489 |
. $GLOBALS['comment_marker'] . $GLOBALS['strDumpingData'] . ' ' . $formatted_table_name . $crlf |
|
|
490 |
. $GLOBALS['comment_marker'] . $crlf .$crlf; |
|
|
491 |
|
|
|
492 |
if (!PMA_exportOutputHandler($head)) { |
|
|
493 |
return FALSE; |
|
|
494 |
} |
|
|
495 |
|
|
|
496 |
$buffer = ''; |
|
|
497 |
|
|
|
498 |
// analyze the query to get the true column names, not the aliases |
|
|
499 |
// (this fixes an undefined index, also if Complete inserts |
|
|
500 |
// are used, we did not get the true column name in case of aliases) |
|
|
501 |
$analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($sql_query)); |
|
|
502 |
|
|
|
503 |
$result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED); |
|
|
504 |
if ($result != FALSE) { |
|
|
505 |
$fields_cnt = PMA_DBI_num_fields($result); |
|
|
506 |
|
|
|
507 |
// Get field information |
|
|
508 |
$fields_meta = PMA_DBI_get_fields_meta($result); |
|
|
509 |
$field_flags = array(); |
|
|
510 |
for ($j = 0; $j < $fields_cnt; $j++) { |
|
|
511 |
$field_flags[$j] = PMA_DBI_field_flags($result, $j); |
|
|
512 |
} |
|
|
513 |
|
|
|
514 |
for ($j = 0; $j < $fields_cnt; $j++) { |
|
|
515 |
if (isset($analyzed_sql[0]['select_expr'][$j]['column'])) { |
|
|
516 |
$field_set[$j] = PMA_backquote($analyzed_sql[0]['select_expr'][$j]['column'], $use_backquotes); |
|
|
517 |
} else { |
|
|
518 |
$field_set[$j] = PMA_backquote($fields_meta[$j]->name, $use_backquotes); |
|
|
519 |
} |
|
|
520 |
} |
|
|
521 |
|
|
|
522 |
if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'update') { |
|
|
523 |
// update |
|
|
524 |
$schema_insert = 'UPDATE '; |
|
|
525 |
if (isset($GLOBALS['sql_ignore'])) { |
|
|
526 |
$schema_insert .= 'IGNORE '; |
|
|
527 |
} |
|
|
528 |
$schema_insert .= PMA_backquote($table, $use_backquotes) . ' SET '; |
|
|
529 |
unset($GLOBALS['extended_ins']); |
|
|
530 |
} else { |
|
|
531 |
// insert or replace |
|
|
532 |
if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'replace') { |
|
|
533 |
$sql_command = 'REPLACE'; |
|
|
534 |
} else { |
|
|
535 |
$sql_command = 'INSERT'; |
|
|
536 |
} |
|
|
537 |
|
|
|
538 |
// delayed inserts? |
|
|
539 |
if (isset($GLOBALS['delayed'])) { |
|
|
540 |
$insert_delayed = ' DELAYED'; |
|
|
541 |
} else { |
|
|
542 |
$insert_delayed = ''; |
|
|
543 |
} |
|
|
544 |
|
|
|
545 |
// insert ignore? |
|
|
546 |
if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'insert' && isset($GLOBALS['sql_ignore'])) { |
|
|
547 |
$insert_delayed .= ' IGNORE'; |
|
|
548 |
} |
|
|
549 |
|
|
|
550 |
// scheme for inserting fields |
|
|
551 |
if (isset($GLOBALS['showcolumns'])) { |
|
|
552 |
$fields = implode(', ', $field_set); |
|
|
553 |
$schema_insert = $sql_command . $insert_delayed .' INTO ' . PMA_backquote($table, $use_backquotes) |
|
|
554 |
. ' (' . $fields . ') VALUES '; |
|
|
555 |
} else { |
|
|
556 |
$schema_insert = $sql_command . $insert_delayed .' INTO ' . PMA_backquote($table, $use_backquotes) |
|
|
557 |
. ' VALUES '; |
|
|
558 |
} |
|
|
559 |
} |
|
|
560 |
|
|
|
561 |
$search = array("\x00", "\x0a", "\x0d", "\x1a"); //\x08\\x09, not required |
|
|
562 |
$replace = array('\0', '\n', '\r', '\Z'); |
|
|
563 |
$current_row = 0; |
|
|
564 |
$query_size = 0; |
|
|
565 |
$separator = isset($GLOBALS['extended_ins']) ? ',' : ';'; |
|
|
566 |
|
|
|
567 |
while ($row = PMA_DBI_fetch_row($result)) { |
|
|
568 |
$current_row++; |
|
|
569 |
for ($j = 0; $j < $fields_cnt; $j++) { |
|
|
570 |
// NULL |
|
|
571 |
if (!isset($row[$j]) || is_null($row[$j])) { |
|
|
572 |
$values[] = 'NULL'; |
|
|
573 |
// a number |
|
|
574 |
// timestamp is numeric on some MySQL 4.1, BLOBs are sometimes numeric |
|
|
575 |
} elseif ($fields_meta[$j]->numeric && $fields_meta[$j]->type != 'timestamp' |
|
|
576 |
&& ! $fields_meta[$j]->blob) { |
|
|
577 |
$values[] = $row[$j]; |
|
|
578 |
// a binary field |
|
|
579 |
// Note: with mysqli, under MySQL 4.1.3, we get the flag |
|
|
580 |
// "binary" for those field types (I don't know why) |
|
|
581 |
} elseif (stristr($field_flags[$j], 'BINARY') |
|
|
582 |
&& isset($GLOBALS['hexforbinary']) |
|
|
583 |
&& $fields_meta[$j]->type != 'datetime' |
|
|
584 |
&& $fields_meta[$j]->type != 'date' |
|
|
585 |
&& $fields_meta[$j]->type != 'time' |
|
|
586 |
&& $fields_meta[$j]->type != 'timestamp' |
|
|
587 |
) { |
|
|
588 |
// empty blobs need to be different, but '0' is also empty :-( |
|
|
589 |
if (empty($row[$j]) && $row[$j] != '0') { |
|
|
590 |
$values[] = '\'\''; |
|
|
591 |
} else { |
|
|
592 |
$values[] = '0x' . bin2hex($row[$j]); |
|
|
593 |
} |
|
|
594 |
// something else -> treat as a string |
|
|
595 |
} else { |
|
|
596 |
$values[] = '\'' . str_replace($search, $replace, PMA_sqlAddslashes($row[$j])) . '\''; |
|
|
597 |
} // end if |
|
|
598 |
} // end for |
|
|
599 |
|
|
|
600 |
// should we make update? |
|
|
601 |
if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'update') { |
|
|
602 |
|
|
|
603 |
$insert_line = $schema_insert; |
|
|
604 |
for ($i = 0; $i < $fields_cnt; $i++) { |
|
|
605 |
if ($i > 0) { |
|
|
606 |
$insert_line .= ', '; |
|
|
607 |
} |
|
|
608 |
$insert_line .= $field_set[$i] . ' = ' . $values[$i]; |
|
|
609 |
} |
|
|
610 |
|
|
|
611 |
$insert_line .= ' WHERE ' . PMA_getUvaCondition($result, $fields_cnt, $fields_meta, $row); |
|
|
612 |
|
|
|
613 |
} else { |
|
|
614 |
|
|
|
615 |
// Extended inserts case |
|
|
616 |
if (isset($GLOBALS['extended_ins'])) { |
|
|
617 |
if ($current_row == 1) { |
|
|
618 |
$insert_line = $schema_insert . '(' . implode(', ', $values) . ')'; |
|
|
619 |
} else { |
|
|
620 |
$insert_line = '(' . implode(', ', $values) . ')'; |
|
|
621 |
if (isset($GLOBALS['max_query_size']) && $GLOBALS['max_query_size'] > 0 && $query_size + strlen($insert_line) > $GLOBALS['max_query_size']) { |
|
|
622 |
if (!PMA_exportOutputHandler(';' . $crlf)) { |
|
|
623 |
return FALSE; |
|
|
624 |
} |
|
|
625 |
$query_size = 0; |
|
|
626 |
$current_row = 1; |
|
|
627 |
$insert_line = $schema_insert . $insert_line; |
|
|
628 |
} |
|
|
629 |
} |
|
|
630 |
$query_size += strlen($insert_line); |
|
|
631 |
} |
|
|
632 |
// Other inserts case |
|
|
633 |
else { |
|
|
634 |
$insert_line = $schema_insert . '(' . implode(', ', $values) . ')'; |
|
|
635 |
} |
|
|
636 |
} |
|
|
637 |
unset($values); |
|
|
638 |
|
|
|
639 |
if (!PMA_exportOutputHandler(($current_row == 1 ? '' : $separator . $crlf) . $insert_line)) { |
|
|
640 |
return FALSE; |
|
|
641 |
} |
|
|
642 |
|
|
|
643 |
} // end while |
|
|
644 |
if ($current_row > 0) { |
|
|
645 |
if (!PMA_exportOutputHandler(';' . $crlf)) { |
|
|
646 |
return FALSE; |
|
|
647 |
} |
|
|
648 |
} |
|
|
649 |
} // end if ($result != FALSE) |
|
|
650 |
PMA_DBI_free_result($result); |
|
|
651 |
|
|
|
652 |
return TRUE; |
|
|
653 |
} // end of the 'PMA_exportData()' function |
|
|
654 |
?> |