3.4.0-rc1
[phpmyadmin/thilanka.git] / libraries / import / ldi.php
blobe6eb56d233c0fae474e626f5a5b7c07886c1631b
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * CSV import plugin for phpMyAdmin
6 * @package phpMyAdmin-Import
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
15 if ($plugin_param !== 'table') {
16 return;
19 if (isset($plugin_list)) {
20 if ($GLOBALS['cfg']['Import']['ldi_local_option'] == 'auto') {
21 $GLOBALS['cfg']['Import']['ldi_local_option'] = FALSE;
23 $result = PMA_DBI_try_query('SHOW VARIABLES LIKE \'local\\_infile\';');
24 if ($result != FALSE && PMA_DBI_num_rows($result) > 0) {
25 $tmp = PMA_DBI_fetch_row($result);
26 if ($tmp[1] == 'ON') {
27 $GLOBALS['cfg']['Import']['ldi_local_option'] = TRUE;
30 PMA_DBI_free_result($result);
31 unset($result);
33 $plugin_list['ldi'] = array(
34 'text' => __('CSV using LOAD DATA'),
35 'extension' => 'ldi', // This is nonsense, however we want to default to our parser for csv
36 'options' => array(
37 array('type' => 'begin_group', 'name' => 'general_opts'),
38 array('type' => 'bool', 'name' => 'replace', 'text' => __('Replace table data with file')),
39 array('type' => 'bool', 'name' => 'ignore', 'text' => __('Do not abort on INSERT error')),
40 array('type' => 'text', 'name' => 'terminated', 'text' => __('Columns terminated by'), 'size' => 2, 'len' => 2),
41 array('type' => 'text', 'name' => 'enclosed', 'text' => __('Columns enclosed by'), 'size' => 2, 'len' => 2),
42 array('type' => 'text', 'name' => 'escaped', 'text' => __('Columns escaped by'), 'size' => 2, 'len' => 2),
43 array('type' => 'text', 'name' => 'new_line', 'text' => __('Lines terminated by'), 'size' => 2),
44 array('type' => 'text', 'name' => 'columns', 'text' => __('Column names')),
45 array('type' => 'bool', 'name' => 'local_option', 'text' => __('Use LOCAL keyword')),
46 array('type' => 'end_group')
48 'options_text' => __('Options'),
50 /* We do not define function when plugin is just queried for information above */
51 return;
54 if ($import_file == 'none' || $compression != 'none' || $charset_conversion) {
55 // We handle only some kind of data!
56 $message = PMA_Message::error(__('This plugin does not support compressed imports!'));
57 $error = TRUE;
58 return;
61 $sql = 'LOAD DATA';
62 if (isset($ldi_local_option)) {
63 $sql .= ' LOCAL';
65 $sql .= ' INFILE \'' . PMA_sqlAddslashes($import_file) . '\'';
66 if (isset($ldi_replace)) {
67 $sql .= ' REPLACE';
68 } elseif (isset($ldi_ignore)) {
69 $sql .= ' IGNORE';
71 $sql .= ' INTO TABLE ' . PMA_backquote($table);
73 if (strlen($ldi_terminated) > 0) {
74 $sql .= ' FIELDS TERMINATED BY \'' . $ldi_terminated . '\'';
76 if (strlen($ldi_enclosed) > 0) {
77 $sql .= ' ENCLOSED BY \'' . PMA_sqlAddslashes($ldi_enclosed) . '\'';
79 if (strlen($ldi_escaped) > 0) {
80 $sql .= ' ESCAPED BY \'' . PMA_sqlAddslashes($ldi_escaped) . '\'';
82 if (strlen($ldi_new_line) > 0){
83 if ($ldi_new_line == 'auto') {
84 $ldi_new_line = PMA_whichCrlf() == "\n" ? '\n' : '\r\n';
86 $sql .= ' LINES TERMINATED BY \'' . $ldi_new_line . '\'';
88 if ($skip_queries > 0) {
89 $sql .= ' IGNORE ' . $skip_queries . ' LINES';
90 $skip_queries = 0;
92 if (strlen($ldi_columns) > 0) {
93 $sql .= ' (';
94 $tmp = preg_split('/,( ?)/', $ldi_columns);
95 $cnt_tmp = count($tmp);
96 for ($i = 0; $i < $cnt_tmp; $i++) {
97 if ($i > 0) {
98 $sql .= ', ';
100 /* Trim also `, if user already included backquoted fields */
101 $sql .= PMA_backquote(trim($tmp[$i], " \t\r\n\0\x0B`"));
102 } // end for
103 $sql .= ')';
106 PMA_importRunQuery($sql, $sql);
107 PMA_importRunQuery();
108 $finished = TRUE;