1 | <?php
|
---|
2 | /* $Id: sql.php,v 2.6.2.1 2004/06/07 11:27:10 rabus Exp $ */
|
---|
3 | // vim: expandtab sw=4 ts=4 sts=4:
|
---|
4 | error_reporting(E_ALL);
|
---|
5 | /**
|
---|
6 | * Set of functions used to build SQL dumps of tables
|
---|
7 | */
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * Returns $table's field types
|
---|
11 | *
|
---|
12 | * @param string the database name
|
---|
13 | * @param string the table name
|
---|
14 | *
|
---|
15 | * @return array the field types; key of array is PMA_backquote
|
---|
16 | * of the field name
|
---|
17 | *
|
---|
18 | * @access public
|
---|
19 | *
|
---|
20 | * This function exists because mysql_field_type() returns 'blob'
|
---|
21 | * even for 'text' fields.
|
---|
22 | */
|
---|
23 | function PMA_fieldTypes($db, $table,$use_backquotes) {
|
---|
24 | PMA_mysql_select_db($db);
|
---|
25 | $table_def = PMA_mysql_query('SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table));
|
---|
26 | while($row = @PMA_mysql_fetch_array($table_def)) {
|
---|
27 | $types[PMA_backquote($row['Field'],$use_backquotes)] = ereg_replace('\\(.*', '', $row['Type']);
|
---|
28 | }
|
---|
29 | return $types;
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Outputs comment
|
---|
34 | *
|
---|
35 | * @param string Text of comment
|
---|
36 | *
|
---|
37 | * @return bool Whether it suceeded
|
---|
38 | */
|
---|
39 | function PMA_exportComment($text) {
|
---|
40 | return PMA_exportOutputHandler('# ' . $text . $GLOBALS['crlf']);
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Outputs export header
|
---|
45 | *
|
---|
46 | * @return bool Whether it suceeded
|
---|
47 | *
|
---|
48 | * @access public
|
---|
49 | */
|
---|
50 | function PMA_exportHeader() {
|
---|
51 | global $crlf;
|
---|
52 | global $cfg;
|
---|
53 |
|
---|
54 | $head = '# phpMyAdmin SQL Dump' . $crlf
|
---|
55 | . '# version ' . PMA_VERSION . $crlf
|
---|
56 | . '# http://www.phpmyadmin.net' . $crlf
|
---|
57 | . '#' . $crlf
|
---|
58 | . '# ' . $GLOBALS['strHost'] . ': ' . $cfg['Server']['host'];
|
---|
59 | if (!empty($cfg['Server']['port'])) {
|
---|
60 | $head .= ':' . $cfg['Server']['port'];
|
---|
61 | }
|
---|
62 | $head .= $crlf
|
---|
63 | . '# ' . $GLOBALS['strGenTime'] . ': ' . PMA_localisedDate() . $crlf
|
---|
64 | . '# ' . $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
|
---|
65 | . '# ' . $GLOBALS['strPHPVersion'] . ': ' . phpversion() . $crlf;
|
---|
66 | return PMA_exportOutputHandler($head);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Outputs create database database
|
---|
71 | *
|
---|
72 | * @param string Database name
|
---|
73 | *
|
---|
74 | * @return bool Whether it suceeded
|
---|
75 | *
|
---|
76 | * @access public
|
---|
77 | */
|
---|
78 | function PMA_exportDBCreate($db) {
|
---|
79 | global $crlf;
|
---|
80 | if (isset($GLOBALS['drop_database'])) {
|
---|
81 | if (!PMA_exportOutputHandler('DROP DATABASE ' . (isset($GLOBALS['use_backquotes']) ? PMA_backquote($db) : $db) . ';' . $crlf)) return FALSE;
|
---|
82 | }
|
---|
83 | $create_query = 'CREATE DATABASE ' . (isset($GLOBALS['use_backquotes']) ? PMA_backquote($db) : $db);
|
---|
84 | if (PMA_MYSQL_INT_VERSION >= 40101) {
|
---|
85 | $collation = PMA_getDbCollation($db);
|
---|
86 | if (strpos($collation, '_')) {
|
---|
87 | $create_query .= ' DEFAULT CHARACTER SET ' . substr($collation, 0, strpos($collation, '_')) . ' COLLATE ' . $collation;
|
---|
88 | } else {
|
---|
89 | $create_query .= ' DEFAULT CHARACTER SET ' . $collation;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | $create_query .= ';' . $crlf;
|
---|
93 | if (!PMA_exportOutputHandler($create_query)) return FALSE;
|
---|
94 | return PMA_exportOutputHandler('USE ' . $db . ';' . $crlf);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Outputs database header
|
---|
99 | *
|
---|
100 | * @param string Database name
|
---|
101 | *
|
---|
102 | * @return bool Whether it suceeded
|
---|
103 | *
|
---|
104 | * @access public
|
---|
105 | */
|
---|
106 | function PMA_exportDBHeader($db) {
|
---|
107 | global $crlf;
|
---|
108 | $head = '# ' . $crlf
|
---|
109 | . '# ' . $GLOBALS['strDatabase'] . ': ' . (isset($GLOBALS['use_backquotes']) ? PMA_backquote($db) : '\'' . $db . '\''). $crlf
|
---|
110 | . '# ' . $crlf;
|
---|
111 | return PMA_exportOutputHandler($head);
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Outputs database footer
|
---|
116 | *
|
---|
117 | * @param string Database name
|
---|
118 | *
|
---|
119 | * @return bool Whether it suceeded
|
---|
120 | *
|
---|
121 | * @access public
|
---|
122 | */
|
---|
123 | function PMA_exportDBFooter($db) {
|
---|
124 | if (isset($GLOBALS['sql_constraints'])) return PMA_exportOutputHandler($GLOBALS['sql_constraints']);
|
---|
125 | return TRUE;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Returns $table's CREATE definition
|
---|
130 | *
|
---|
131 | * @param string the database name
|
---|
132 | * @param string the table name
|
---|
133 | * @param string the end of line sequence
|
---|
134 | * @param string the url to go back in case of error
|
---|
135 | * @param boolean whether to include creation/update/check dates
|
---|
136 | *
|
---|
137 | * @return string resulting schema
|
---|
138 | *
|
---|
139 | * @global boolean whether to add 'drop' statements or not
|
---|
140 | * @global boolean whether to use backquotes to allow the use of special
|
---|
141 | * characters in database, table and fields names or not
|
---|
142 | *
|
---|
143 | * @access public
|
---|
144 | */
|
---|
145 | function PMA_getTableDef($db, $table, $crlf, $error_url, $show_dates = false)
|
---|
146 | {
|
---|
147 | global $drop;
|
---|
148 | global $use_backquotes;
|
---|
149 | global $cfgRelation;
|
---|
150 | global $sql_constraints;
|
---|
151 |
|
---|
152 | $schema_create = '';
|
---|
153 | $auto_increment = '';
|
---|
154 | $new_crlf = $crlf;
|
---|
155 |
|
---|
156 |
|
---|
157 | $result = PMA_mysql_query('SHOW TABLE STATUS FROM ' . PMA_backquote($db) . ' LIKE \'' . PMA_sqlAddslashes($table) . '\'');
|
---|
158 | if ($result != FALSE) {
|
---|
159 | if (mysql_num_rows($result) > 0) {
|
---|
160 | $tmpres = PMA_mysql_fetch_array($result);
|
---|
161 | if (isset($GLOBALS['auto_increment']) && !empty($tmpres['Auto_increment'])) {
|
---|
162 | $auto_increment .= ' AUTO_INCREMENT=' . $tmpres['Auto_increment'] . ' ';
|
---|
163 | }
|
---|
164 |
|
---|
165 | if ($show_dates && isset($tmpres['Create_time']) && !empty($tmpres['Create_time'])) {
|
---|
166 | $schema_create .= '# ' . $GLOBALS['strStatCreateTime'] . ': ' . PMA_localisedDate(strtotime($tmpres['Create_time'])) . $crlf;
|
---|
167 | $new_crlf = '#' . $crlf . $crlf;
|
---|
168 | }
|
---|
169 |
|
---|
170 | if ($show_dates && isset($tmpres['Update_time']) && !empty($tmpres['Update_time'])) {
|
---|
171 | $schema_create .= '# ' . $GLOBALS['strStatUpdateTime'] . ': ' . PMA_localisedDate(strtotime($tmpres['Update_time'])) . $crlf;
|
---|
172 | $new_crlf = '#' . $crlf . $crlf;
|
---|
173 | }
|
---|
174 |
|
---|
175 | if ($show_dates && isset($tmpres['Check_time']) && !empty($tmpres['Check_time'])) {
|
---|
176 | $schema_create .= '# ' . $GLOBALS['strStatCheckTime'] . ': ' . PMA_localisedDate(strtotime($tmpres['Check_time'])) . $crlf;
|
---|
177 | $new_crlf = '#' . $crlf . $crlf;
|
---|
178 | }
|
---|
179 | mysql_free_result($result);
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | $schema_create .= $new_crlf;
|
---|
184 |
|
---|
185 | if (!empty($drop)) {
|
---|
186 | $schema_create .= 'DROP TABLE IF EXISTS ' . PMA_backquote($table, $use_backquotes) . ';' . $crlf;
|
---|
187 | }
|
---|
188 |
|
---|
189 | // Steve Alberty's patch for complete table dump,
|
---|
190 | // Whether to quote table and fields names or not
|
---|
191 | if ($use_backquotes) {
|
---|
192 | PMA_mysql_query('SET SQL_QUOTE_SHOW_CREATE = 1');
|
---|
193 | } else {
|
---|
194 | PMA_mysql_query('SET SQL_QUOTE_SHOW_CREATE = 0');
|
---|
195 | }
|
---|
196 | $result = PMA_mysql_query('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table));
|
---|
197 | if ($result != FALSE && mysql_num_rows($result) > 0) {
|
---|
198 | $tmpres = PMA_mysql_fetch_array($result);
|
---|
199 | // Fix for case problems with winwin, thanks to
|
---|
200 | // Pawe³ Szczepañski <pauluz at users.sourceforge.net>
|
---|
201 | $pos = strpos($tmpres[1], ' (');
|
---|
202 |
|
---|
203 | // Fix a problem with older versions of mysql
|
---|
204 | // Find the first opening parenthesys, i.e. that after the name
|
---|
205 | // of the table
|
---|
206 | $pos2 = strpos($tmpres[1], '(');
|
---|
207 | // Old mysql did not insert a space after table name
|
---|
208 | // in query "show create table ..."!
|
---|
209 | if ($pos2 != $pos + 1)
|
---|
210 | {
|
---|
211 | // This is the real position of the first character after
|
---|
212 | // the name of the table
|
---|
213 | $pos = $pos2;
|
---|
214 | // Old mysql did not even put newlines and indentation...
|
---|
215 | $tmpres[1] = str_replace(",", ",\n ", $tmpres[1]);
|
---|
216 | }
|
---|
217 |
|
---|
218 | $tmpres[1] = substr($tmpres[1], 0, 13)
|
---|
219 | . (($use_backquotes) ? PMA_backquote($tmpres[0]) : $tmpres[0])
|
---|
220 | . substr($tmpres[1], $pos);
|
---|
221 | $tmpres[1] = str_replace("\n", $crlf, $tmpres[1]);
|
---|
222 | if (preg_match_all('((,\r?\n[\s]*(CONSTRAINT|FOREIGN[\s]*KEY)[^\r\n]+)+)', $tmpres[1], $regs)) {
|
---|
223 | if (!isset($sql_constraints)) {
|
---|
224 | if (isset($GLOBALS['no_constraints_comments'])) {
|
---|
225 | $sql_constraints = '';
|
---|
226 | } else {
|
---|
227 | $sql_constraints = $crlf . '#' . $crlf
|
---|
228 | . '# ' . $GLOBALS['strConstraintsForDumped'] . $crlf
|
---|
229 | . '#' . $crlf;
|
---|
230 | }
|
---|
231 | }
|
---|
232 | if (!isset($GLOBALS['no_constraints_comments'])) {
|
---|
233 | $sql_constraints .= $crlf .'#' . $crlf .'# ' . $GLOBALS['strConstraintsForTable'] . ' ' . PMA_backquote($table) . $crlf . '#' . $crlf;
|
---|
234 | }
|
---|
235 | $sql_constraints .= 'ALTER TABLE ' . PMA_backquote($table) . $crlf
|
---|
236 | . preg_replace('/(,\r?\n|^)([\s]*)(CONSTRAINT|FOREIGN[\s]*KEY)/', '\1\2ADD \3', substr($regs[0][0], 2))
|
---|
237 | . ";\n";
|
---|
238 | $tmpres[1] = preg_replace('((,\r?\n[\s]*(CONSTRAINT|FOREIGN[\s]*KEY)[^\r\n]+)+)', '', $tmpres[1]);
|
---|
239 | }
|
---|
240 | $schema_create .= $tmpres[1];
|
---|
241 | }
|
---|
242 |
|
---|
243 | $schema_create .= $auto_increment;
|
---|
244 |
|
---|
245 |
|
---|
246 | mysql_free_result($result);
|
---|
247 | return $schema_create;
|
---|
248 | } // end of the 'PMA_getTableDef()' function
|
---|
249 |
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * Returns $table's comments, relations etc.
|
---|
253 | *
|
---|
254 | * @param string the database name
|
---|
255 | * @param string the table name
|
---|
256 | * @param string the end of line sequence
|
---|
257 | * @param boolean whether to include relation comments
|
---|
258 | * @param boolean whether to include column comments
|
---|
259 | * @param boolean whether to include mime comments
|
---|
260 | *
|
---|
261 | * @return string resulting comments
|
---|
262 | *
|
---|
263 | * @access public
|
---|
264 | */
|
---|
265 | function PMA_getTableComments($db, $table, $crlf, $do_relation = false, $do_comments = false, $do_mime = false)
|
---|
266 | {
|
---|
267 | global $cfgRelation;
|
---|
268 | global $use_backquotes;
|
---|
269 | global $sql_constraints;
|
---|
270 |
|
---|
271 | $schema_create = '';
|
---|
272 |
|
---|
273 | if ($do_comments && $cfgRelation['commwork']) {
|
---|
274 | if (!($comments_map = PMA_getComments($db, $table))) unset($comments_map);
|
---|
275 | }
|
---|
276 |
|
---|
277 | // Check if we can use Relations (Mike Beck)
|
---|
278 | if ($do_relation && !empty($cfgRelation['relation'])) {
|
---|
279 | // Find which tables are related with the current one and write it in
|
---|
280 | // an array
|
---|
281 | $res_rel = PMA_getForeigners($db, $table);
|
---|
282 |
|
---|
283 | if ($res_rel && count($res_rel) > 0) {
|
---|
284 | $have_rel = TRUE;
|
---|
285 | } else {
|
---|
286 | $have_rel = FALSE;
|
---|
287 | }
|
---|
288 | }
|
---|
289 | else {
|
---|
290 | $have_rel = FALSE;
|
---|
291 | } // end if
|
---|
292 |
|
---|
293 | if ($do_mime && $cfgRelation['mimework']) {
|
---|
294 | if (!($mime_map = PMA_getMIME($db, $table, true))) unset($mime_map);
|
---|
295 | }
|
---|
296 |
|
---|
297 | if (isset($comments_map) && count($comments_map) > 0) {
|
---|
298 | $schema_create .= $crlf . '#' . $crlf . '# COMMENTS FOR TABLE ' . PMA_backquote($table, $use_backquotes) . ':' . $crlf;
|
---|
299 | foreach($comments_map AS $comment_field => $comment) {
|
---|
300 | $schema_create .= '# ' . PMA_backquote($comment_field, $use_backquotes) . $crlf . '# ' . PMA_backquote($comment, $use_backquotes) . $crlf;
|
---|
301 | }
|
---|
302 | $schema_create .= '#' . $crlf;
|
---|
303 | }
|
---|
304 |
|
---|
305 | if (isset($mime_map) && count($mime_map) > 0) {
|
---|
306 | $schema_create .= $crlf . '#' . $crlf . '# MIME TYPES FOR TABLE ' . PMA_backquote($table, $use_backquotes) . ':' . $crlf;
|
---|
307 | @reset($mime_map);
|
---|
308 | foreach($mime_map AS $mime_field => $mime) {
|
---|
309 | $schema_create .= '# ' . PMA_backquote($mime_field, $use_backquotes) . $crlf . '# ' . PMA_backquote($mime['mimetype'], $use_backquotes) . $crlf;
|
---|
310 | }
|
---|
311 | $schema_create .= '#' . $crlf;
|
---|
312 | }
|
---|
313 |
|
---|
314 | if ($have_rel) {
|
---|
315 | $schema_create .= $crlf . '#' . $crlf . '# RELATIONS FOR TABLE ' . PMA_backquote($table, $use_backquotes) . ':' . $crlf;
|
---|
316 | foreach($res_rel AS $rel_field => $rel) {
|
---|
317 | $schema_create .= '# ' . PMA_backquote($rel_field, $use_backquotes) . $crlf . '# ' . PMA_backquote($rel['foreign_table'], $use_backquotes) . ' -> ' . PMA_backquote($rel['foreign_field'], $use_backquotes) . $crlf;
|
---|
318 | }
|
---|
319 | $schema_create .= '#' . $crlf;
|
---|
320 | }
|
---|
321 |
|
---|
322 | return $schema_create;
|
---|
323 |
|
---|
324 | } // end of the 'PMA_getTableComments()' function
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * Outputs table's structure
|
---|
328 | *
|
---|
329 | * @param string the database name
|
---|
330 | * @param string the table name
|
---|
331 | * @param string the end of line sequence
|
---|
332 | * @param string the url to go back in case of error
|
---|
333 | * @param boolean whether to include relation comments
|
---|
334 | * @param boolean whether to include column comments
|
---|
335 | * @param boolean whether to include mime comments
|
---|
336 | *
|
---|
337 | * @return bool Whether it suceeded
|
---|
338 | *
|
---|
339 | * @access public
|
---|
340 | */
|
---|
341 | function PMA_exportStructure($db, $table, $crlf, $error_url, $relation = FALSE, $comments = FALSE, $mime = FALSE, $dates = FALSE) {
|
---|
342 | $formatted_table_name = (isset($GLOBALS['use_backquotes']))
|
---|
343 | ? PMA_backquote($table)
|
---|
344 | : '\'' . $table . '\'';
|
---|
345 | $dump = $crlf
|
---|
346 | . '# --------------------------------------------------------' . $crlf
|
---|
347 | . $crlf . '#' . $crlf
|
---|
348 | . '# ' . $GLOBALS['strTableStructure'] . ' ' . $formatted_table_name . $crlf
|
---|
349 | . '#' . $crlf
|
---|
350 | . PMA_getTableDef($db, $table, $crlf, $error_url, $dates) . ';' . $crlf
|
---|
351 | . PMA_getTableComments($db, $table, $crlf, $relation, $comments, $mime);
|
---|
352 |
|
---|
353 |
|
---|
354 | return PMA_exportOutputHandler($dump);
|
---|
355 | }
|
---|
356 |
|
---|
357 | /**
|
---|
358 | * Dispatches between the versions of 'getTableContent' to use depending
|
---|
359 | * on the php version
|
---|
360 | *
|
---|
361 | * @param string the database name
|
---|
362 | * @param string the table name
|
---|
363 | * @param string the end of line sequence
|
---|
364 | * @param string the url to go back in case of error
|
---|
365 | * @param string SQL query for obtaining data
|
---|
366 | *
|
---|
367 | * @return bool Whether it suceeded
|
---|
368 | *
|
---|
369 | * @global boolean whether to use backquotes to allow the use of special
|
---|
370 | * characters in database, table and fields names or not
|
---|
371 | * @global integer the number of records
|
---|
372 | * @global integer the current record position
|
---|
373 | *
|
---|
374 | * @access public
|
---|
375 | *
|
---|
376 | * @see PMA_getTableContentFast(), PMA_getTableContentOld()
|
---|
377 | *
|
---|
378 | * @author staybyte
|
---|
379 | */
|
---|
380 | function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
|
---|
381 | {
|
---|
382 | global $use_backquotes;
|
---|
383 | global $rows_cnt;
|
---|
384 | global $current_row;
|
---|
385 |
|
---|
386 | $formatted_table_name = (isset($GLOBALS['use_backquotes']))
|
---|
387 | ? PMA_backquote($table)
|
---|
388 | : '\'' . $table . '\'';
|
---|
389 | $head = $crlf
|
---|
390 | . '#' . $crlf
|
---|
391 | . '# ' . $GLOBALS['strDumpingData'] . ' ' . $formatted_table_name . $crlf
|
---|
392 | . '#' . $crlf .$crlf;
|
---|
393 |
|
---|
394 | if (!PMA_exportOutputHandler($head)) return FALSE;
|
---|
395 |
|
---|
396 | $buffer = '';
|
---|
397 |
|
---|
398 | $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', $sql_query, '', $error_url);
|
---|
399 | if ($result != FALSE) {
|
---|
400 | $fields_cnt = mysql_num_fields($result);
|
---|
401 | $rows_cnt = mysql_num_rows($result);
|
---|
402 |
|
---|
403 | // get the real types of the table's fields (in an array)
|
---|
404 | // the key of the array is the backquoted field name
|
---|
405 | $field_types = PMA_fieldTypes($db,$table,$use_backquotes);
|
---|
406 |
|
---|
407 | // analyze the query to get the true column names, not the aliases
|
---|
408 | // (this fixes an undefined index, also if Complete inserts
|
---|
409 | // are used, we did not get the true column name in case of aliases)
|
---|
410 | $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($sql_query));
|
---|
411 |
|
---|
412 | // Checks whether the field is an integer or not
|
---|
413 | for ($j = 0; $j < $fields_cnt; $j++) {
|
---|
414 |
|
---|
415 | if (isset($analyzed_sql[0]['select_expr'][$j]['column'])) {
|
---|
416 | $field_set[$j] = PMA_backquote($analyzed_sql[0]['select_expr'][$j]['column'], $use_backquotes);
|
---|
417 | } else {
|
---|
418 | $field_set[$j] = PMA_backquote(PMA_mysql_field_name($result, $j), $use_backquotes);
|
---|
419 | }
|
---|
420 |
|
---|
421 | $type = $field_types[$field_set[$j]];
|
---|
422 |
|
---|
423 | if ($type == 'tinyint' || $type == 'smallint' || $type == 'mediumint' || $type == 'int' ||
|
---|
424 | $type == 'bigint' || (PMA_MYSQL_INT_VERSION < 40100 && $type == 'timestamp')) {
|
---|
425 | $field_num[$j] = TRUE;
|
---|
426 | } else {
|
---|
427 | $field_num[$j] = FALSE;
|
---|
428 | }
|
---|
429 | // blob
|
---|
430 | if ($type == 'blob' || $type == 'mediumblob' || $type == 'longblob' || $type == 'tinyblob') {
|
---|
431 | $field_blob[$j] = TRUE;
|
---|
432 | } else {
|
---|
433 | $field_blob[$j] = FALSE;
|
---|
434 | }
|
---|
435 | } // end for
|
---|
436 |
|
---|
437 | if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'update') {
|
---|
438 | // update
|
---|
439 | $schema_insert = 'UPDATE ' . PMA_backquote($table, $use_backquotes) . ' SET ';
|
---|
440 | $fields_no = count($field_set);
|
---|
441 | } else {
|
---|
442 | // insert or replace
|
---|
443 | if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'replace') {
|
---|
444 | $sql_command = 'REPLACE';
|
---|
445 | } else {
|
---|
446 | $sql_command = 'INSERT';
|
---|
447 | }
|
---|
448 |
|
---|
449 | // delayed inserts?
|
---|
450 | if (isset($GLOBALS['delayed'])) {
|
---|
451 | $insert_delayed = ' DELAYED';
|
---|
452 | } else {
|
---|
453 | $insert_delayed = '';
|
---|
454 | }
|
---|
455 |
|
---|
456 | // Sets the scheme
|
---|
457 | if (isset($GLOBALS['showcolumns'])) {
|
---|
458 | $fields = implode(', ', $field_set);
|
---|
459 | $schema_insert = $sql_command . $insert_delayed .' INTO ' . PMA_backquote($table, $use_backquotes)
|
---|
460 | . ' (' . $fields . ') VALUES (';
|
---|
461 | } else {
|
---|
462 | $schema_insert = $sql_command . $insert_delayed .' INTO ' . PMA_backquote($table, $use_backquotes)
|
---|
463 | . ' VALUES (';
|
---|
464 | }
|
---|
465 | }
|
---|
466 |
|
---|
467 | $search = array("\x00", "\x0a", "\x0d", "\x1a"); //\x08\\x09, not required
|
---|
468 | $replace = array('\0', '\n', '\r', '\Z');
|
---|
469 | $current_row = 0;
|
---|
470 |
|
---|
471 | while ($row = PMA_mysql_fetch_row($result)) {
|
---|
472 | $current_row++;
|
---|
473 | for ($j = 0; $j < $fields_cnt; $j++) {
|
---|
474 | if (!isset($row[$j])) {
|
---|
475 | $values[] = 'NULL';
|
---|
476 | } else if ($row[$j] == '0' || $row[$j] != '') {
|
---|
477 | // a number
|
---|
478 | if ($field_num[$j]) {
|
---|
479 | $values[] = $row[$j];
|
---|
480 | // a not empty blob
|
---|
481 | } else if ($field_blob[$j] && !empty($row[$j])) {
|
---|
482 | $values[] = '0x' . bin2hex($row[$j]);
|
---|
483 | // a string
|
---|
484 | } else {
|
---|
485 | $values[] = "'" . str_replace($search, $replace, PMA_sqlAddslashes($row[$j])) . "'";
|
---|
486 | }
|
---|
487 | } else {
|
---|
488 | $values[] = "''";
|
---|
489 | } // end if
|
---|
490 | } // end for
|
---|
491 |
|
---|
492 | // should we make update?
|
---|
493 | if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'update') {
|
---|
494 |
|
---|
495 | $insert_line = $schema_insert;
|
---|
496 | for ($i = 0; $i < $fields_no; $i++) {
|
---|
497 | if ($i > 0) {
|
---|
498 | $insert_line .= ', ';
|
---|
499 | }
|
---|
500 | $insert_line .= $field_set[$i] . ' = ' . $values[$i];
|
---|
501 | }
|
---|
502 |
|
---|
503 | } else {
|
---|
504 |
|
---|
505 | // Extended inserts case
|
---|
506 | if (isset($GLOBALS['extended_ins'])) {
|
---|
507 | if ($current_row == 1) {
|
---|
508 | $insert_line = $schema_insert . implode(', ', $values) . ')';
|
---|
509 | } else {
|
---|
510 | $insert_line = '(' . implode(', ', $values) . ')';
|
---|
511 | }
|
---|
512 | }
|
---|
513 | // Other inserts case
|
---|
514 | else {
|
---|
515 | $insert_line = $schema_insert . implode(', ', $values) . ')';
|
---|
516 | }
|
---|
517 | }
|
---|
518 | unset($values);
|
---|
519 |
|
---|
520 | if (!PMA_exportOutputHandler($insert_line . ((isset($GLOBALS['extended_ins']) && ($current_row < $rows_cnt)) ? ',' : ';') . $crlf)) return FALSE;
|
---|
521 |
|
---|
522 | } // end while
|
---|
523 | } // end if ($result != FALSE)
|
---|
524 | mysql_free_result($result);
|
---|
525 |
|
---|
526 | return TRUE;
|
---|
527 | } // end of the 'PMA_exportData()' function
|
---|
528 | ?>
|
---|