2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * CSV import plugin for phpMyAdmin
6 * @package phpMyAdmin-Import
8 if (! defined('PHPMYADMIN')) {
15 if ($plugin_param !== 'table') {
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);
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
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 */
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!'));
62 if (isset($ldi_local_option)) {
65 $sql .= ' INFILE \'' . PMA_sqlAddslashes($import_file) . '\'';
66 if (isset($ldi_replace)) {
68 } elseif (isset($ldi_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';
92 if (strlen($ldi_columns) > 0) {
94 $tmp = preg_split('/,( ?)/', $ldi_columns);
95 $cnt_tmp = count($tmp);
96 for ($i = 0; $i < $cnt_tmp; $i++
) {
100 /* Trim also `, if user already included backquoted fields */
101 $sql .= PMA_backquote(trim($tmp[$i], " \t\r\n\0\x0B`"));
106 PMA_importRunQuery($sql, $sql);
107 PMA_importRunQuery();