2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * CSV import plugin for phpMyAdmin
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 if (PMA_MYSQL_INT_VERSION
< 32349) {
24 $GLOBALS['cfg']['Import']['ldi_local_option'] = TRUE;
27 if (PMA_MYSQL_INT_VERSION
> 40003) {
28 $result = PMA_DBI_try_query('SHOW VARIABLES LIKE \'local\\_infile\';');
29 if ($result != FALSE && PMA_DBI_num_rows($result) > 0) {
30 $tmp = PMA_DBI_fetch_row($result);
31 if ($tmp[1] == 'ON') {
32 $GLOBALS['cfg']['Import']['ldi_local_option'] = TRUE;
35 PMA_DBI_free_result($result);
39 $plugin_list['ldi'] = array(
41 'extension' => 'ldi', // This is nonsense, however we want to default to our parser for csv
43 array('type' => 'bool', 'name' => 'replace', 'text' => 'strReplaceTable'),
44 array('type' => 'bool', 'name' => 'ignore', 'text' => 'strIgnoreDuplicates'),
45 array('type' => 'text', 'name' => 'terminated', 'text' => 'strFieldsTerminatedBy', 'size' => 2, 'len' => 2),
46 array('type' => 'text', 'name' => 'enclosed', 'text' => 'strFieldsEnclosedBy', 'size' => 2, 'len' => 2),
47 array('type' => 'text', 'name' => 'escaped', 'text' => 'strFieldsEscapedBy', 'size' => 2, 'len' => 2),
48 array('type' => 'text', 'name' => 'new_line', 'text' => 'strLinesTerminatedBy', 'size' => 2),
49 array('type' => 'text', 'name' => 'columns', 'text' => 'strColumnNames'),
50 array('type' => 'bool', 'name' => 'local_option', 'text' => 'strLDILocal'),
52 'options_text' => 'strOptions',
54 /* We do not define function when plugin is just queried for information above */
58 if ($import_file == 'none' ||
$compression != 'none' ||
$charset_conversion) {
59 // We handle only some kind of data!
60 $message = $strInvalidLDIImport;
61 $show_error_header = TRUE;
67 if (isset($ldi_local_option)) {
70 $sql .= ' INFILE \'' . PMA_sqlAddslashes($import_file) . '\'';
71 if (isset($ldi_replace)) {
73 } elseif (isset($ldi_ignore)) {
76 $sql .= ' INTO TABLE ' . PMA_backquote($table);
78 if (strlen($ldi_terminated) > 0) {
79 $sql .= ' FIELDS TERMINATED BY \'' . $ldi_terminated . '\'';
81 if (strlen($ldi_enclosed) > 0) {
82 $sql .= ' ENCLOSED BY \'' . PMA_sqlAddslashes($ldi_enclosed) . '\'';
84 if (strlen($ldi_escaped) > 0) {
85 $sql .= ' ESCAPED BY \'' . PMA_sqlAddslashes($ldi_escaped) . '\'';
87 if (strlen($ldi_new_line) > 0){
88 if ($ldi_new_line == 'auto') {
89 $ldi_new_line = PMA_whichCrlf() == "\n" ?
'\n' : '\r\n';
91 $sql .= ' LINES TERMINATED BY \'' . $ldi_new_line . '\'';
93 if ($skip_queries > 0) {
94 $sql .= ' IGNORE ' . $skip_queries . ' LINES';
97 if (strlen($ldi_columns) > 0) {
99 $tmp = split(',( ?)', $ldi_columns);
100 $cnt_tmp = count($tmp);
101 for ($i = 0; $i < $cnt_tmp; $i++
) {
105 $sql .= PMA_backquote(trim($tmp[$i]));
110 PMA_importRunQuery($sql, $sql);
111 PMA_importRunQuery();