2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * CSV import plugin for phpMyAdmin using LOAD DATA
6 * @package PhpMyAdmin-Import
9 if (! defined('PHPMYADMIN')) {
16 if ($plugin_param !== 'table') {
20 if (isset($plugin_list)) {
21 if ($GLOBALS['cfg']['Import']['ldi_local_option'] == 'auto') {
22 $GLOBALS['cfg']['Import']['ldi_local_option'] = false;
24 $result = PMA_DBI_try_query('SHOW VARIABLES LIKE \'local\\_infile\';');
25 if ($result != false && PMA_DBI_num_rows($result) > 0) {
26 $tmp = PMA_DBI_fetch_row($result);
27 if ($tmp[1] == 'ON') {
28 $GLOBALS['cfg']['Import']['ldi_local_option'] = true;
31 PMA_DBI_free_result($result);
34 $plugin_list['ldi'] = array(
35 'text' => __('CSV using LOAD DATA'),
36 'extension' => 'ldi', // This is nonsense, however we want to default to our parser for csv
38 array('type' => 'begin_group', 'name' => 'general_opts'),
39 array('type' => 'bool', 'name' => 'replace', 'text' => __('Replace table data with file')),
40 array('type' => 'bool', 'name' => 'ignore', 'text' => __('Do not abort on INSERT error')),
41 array('type' => 'text', 'name' => 'terminated', 'text' => __('Columns terminated by'), 'size' => 2, 'len' => 2),
42 array('type' => 'text', 'name' => 'enclosed', 'text' => __('Columns enclosed by'), 'size' => 2, 'len' => 2),
43 array('type' => 'text', 'name' => 'escaped', 'text' => __('Columns escaped by'), 'size' => 2, 'len' => 2),
44 array('type' => 'text', 'name' => 'new_line', 'text' => __('Lines terminated by'), 'size' => 2),
45 array('type' => 'text', 'name' => 'columns', 'text' => __('Column names')),
46 array('type' => 'bool', 'name' => 'local_option', 'text' => __('Use LOCAL keyword')),
47 array('type' => 'end_group')
49 'options_text' => __('Options'),
51 /* We do not define function when plugin is just queried for information above */
55 if ($import_file == 'none' ||
$compression != 'none' ||
$charset_conversion) {
56 // We handle only some kind of data!
57 $message = PMA_Message
::error(__('This plugin does not support compressed imports!'));
63 if (isset($ldi_local_option)) {
66 $sql .= ' INFILE \'' . PMA_sqlAddSlashes($import_file) . '\'';
67 if (isset($ldi_replace)) {
69 } elseif (isset($ldi_ignore)) {
72 $sql .= ' INTO TABLE ' . PMA_backquote($table);
74 if (strlen($ldi_terminated) > 0) {
75 $sql .= ' FIELDS TERMINATED BY \'' . $ldi_terminated . '\'';
77 if (strlen($ldi_enclosed) > 0) {
78 $sql .= ' ENCLOSED BY \'' . PMA_sqlAddSlashes($ldi_enclosed) . '\'';
80 if (strlen($ldi_escaped) > 0) {
81 $sql .= ' ESCAPED BY \'' . PMA_sqlAddSlashes($ldi_escaped) . '\'';
83 if (strlen($ldi_new_line) > 0) {
84 if ($ldi_new_line == 'auto') {
85 $ldi_new_line = PMA_whichCrlf() == "\n" ?
'\n' : '\r\n';
87 $sql .= ' LINES TERMINATED BY \'' . $ldi_new_line . '\'';
89 if ($skip_queries > 0) {
90 $sql .= ' IGNORE ' . $skip_queries . ' LINES';
93 if (strlen($ldi_columns) > 0) {
95 $tmp = preg_split('/,( ?)/', $ldi_columns);
96 $cnt_tmp = count($tmp);
97 for ($i = 0; $i < $cnt_tmp; $i++
) {
101 /* Trim also `, if user already included backquoted fields */
102 $sql .= PMA_backquote(trim($tmp[$i], " \t\r\n\0\x0B`"));
107 PMA_importRunQuery($sql, $sql);
108 PMA_importRunQuery();