Skip to content

Commit aac174b

Browse files
author
Radovan Janjic
committed
New functionality
- added import (ON DUPLICATE KEY UPDATE) CSV data in table
1 parent 72e0817 commit aac174b

File tree

3 files changed

+77
-31
lines changed

3 files changed

+77
-31
lines changed

MySQL_wrapper.class.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,54 @@ function importCSV2Table($file, $table, $delimiter = ',', $enclosure = '"', $esc
410410
return ($this->query($sql, $this->link)) ? $this->affected : FALSE;
411411
}
412412

413+
/** Imports (ON DUPLICATE KEY UPDATE) CSV data in Table with possibility to update rows while import.
414+
* @param string $file - CSV File path
415+
* @param string $table - Table name
416+
* @param string $delimiter - COLUMNS TERMINATED BY (Default: ',')
417+
* @param string $enclosure - OPTIONALLY ENCLOSED BY (Default: '"')
418+
* @param string $escape - ESCAPED BY (Default: '\')
419+
* @param integer $ignore - Number of ignored rows (Default: 1)
420+
* @param array $update - If row fields needed to be updated eg date format or increment (SQL format only @FIELD is variable with content of that field in CSV row) $update = array('SOME_DATE' => 'STR_TO_DATE(@SOME_DATE, "%d/%m/%Y")', 'SOME_INCREMENT' => '@SOME_INCREMENT + 1')
421+
* @param string $getColumnsFrom - Get Columns Names from (file or table) - this is important if there is update while inserting (Default: file)
422+
* @param string $newLine - New line delimiter (Default: \n)
423+
* @param resource $link - Link identifier
424+
* @return number of inserted rows or false
425+
*/
426+
function importUpdateCSV2Table($file, $table, $delimiter = ',', $enclosure = '"', $escape = '\\', $ignore = 1, $update = array(), $getColumnsFrom = 'file', $newLine = '\n', $link = 0) {
427+
$this->link = $link ? $link : $this->link;
428+
429+
$tmp_name = "tmp_{$table}_" . rand();
430+
431+
// Create tmp table
432+
$this->query("CREATE TEMPORARY TABLE `{$tmp_name}` LIKE `{$table}`;", $this->link);
433+
// Drop indexes from tmp table
434+
$this->query("SHOW INDEX FROM `{$tmp_name}`;", $this->link);
435+
if($this->affected){
436+
$drop = array();
437+
while($row = $this->fetchArray()){
438+
$drop[] = "DROP INDEX `{$row['Key_name']}`";
439+
}
440+
$this->freeResult();
441+
$this->query("ALTER TABLE `{$tmp_name}` " . implode(', ', $drop) . ";", $this->link);
442+
}
443+
444+
// Import to tmp
445+
$this->importCSV2Table($file, $tmp_name, $delimiter, $enclosure, $escape, $ignore, $update, $getColumnsFrom, $newLine, $this->link);
446+
447+
// Copy data
448+
$cols = array();
449+
foreach ($this->getColumns($table) as $c) {
450+
$cols[] = "`{$c}` = VALUES(`{$c}`)";
451+
}
452+
$this->query("INSERT INTO `{$table}` SELECT * FROM {$tmp_name} ON DUPLICATE KEY UPDATE " . implode(', ', $cols) . ";", $this->link);
453+
$i = $this->affected;
454+
455+
// Drop tmp table
456+
$this->query("DROP TEMPORARY TABLE `{$tmp_name}`;", $this->link);
457+
458+
return ($i) ? $i : FALSE;
459+
}
460+
413461
/** Export table data to CSV file.
414462
* @param string $table - Table name
415463
* @param string $file - CSV File path

example.php

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,6 @@
1616
define('PASS', '');
1717
define('DB', 'test');
1818

19-
//////////////////////////////////////////////////////////////////
20-
$db = new MySQL_wrapper(HOST, USER, PASS, DB);
21-
$db->connect();
22-
23-
$db->dropTable('csv_to_table_test');
24-
$db->createTableFromCSV('test_files/countrylist.csv', 'csv_to_table_test', ',', '"', '\\', 1, array(), 'file', '\r\n');
25-
26-
$db->dropTable('csv_to_table_test_no_column_names');
27-
$db->createTableFromCSV('test_files/countrylist1.csv', 'csv_to_table_test_no_column_names', ',', '"', '\\', 0, array(), 'generate', '\r\n');
28-
29-
/** Create table from CSV file and imports CSV data to Table with possibility to update rows while import.
30-
* @param string $file - CSV File path
31-
* @param string $table - Table name
32-
* @param string $delimiter - COLUMNS TERMINATED BY (Default: ',')
33-
* @param string $enclosure - OPTIONALLY ENCLOSED BY (Default: '"')
34-
* @param string $escape - ESCAPED BY (Default: '\')
35-
* @param integer $ignore - Number of ignored rows (Default: 1)
36-
* @param array $update - If row fields needed to be updated eg date format or increment (SQL format only @FIELD is variable with content of that field in CSV row) $update = array('SOME_DATE' => 'STR_TO_DATE(@SOME_DATE, "%d/%m/%Y")', 'SOME_INCREMENT' => '@SOME_INCREMENT + 1')
37-
* @param string $getColumnsFrom - Get Columns Names from (file or generate) - this is important if there is update while inserting (Default: file)
38-
* @param string $newLine - New line delimiter (Default: \n)
39-
* @param resource $link - Link identifier
40-
* @return number of inserted rows or false
41-
*/
42-
// function createTableFromCSV($file, $table, $delimiter = ',', $enclosure = '"', $escape = '\\', $ignore = 1, $update = array(), $getColumnsFrom = 'file', $newLine = '\r\n', $link = 0)
43-
44-
$db->close();
45-
exit;
46-
///////////////////////////////////////////////////////////////////
47-
4819
// create test table
4920
$db = new MySQL_wrapper(HOST, USER, PASS, DB);
5021
$db->connect();
@@ -588,6 +559,33 @@
588559
///////////////////////////////////////////////////////////////////////////////////////////
589560

590561
// Example 21
562+
// Import CSV to Table
563+
///////////////////////////////////////////////////////////////////////////////////////////
564+
$db = new MySQL_wrapper(HOST, USER, PASS, DB);
565+
// Connect
566+
$db->connect();
567+
// Import and update all data
568+
$db->importUpdateCSV2Table('test_files/countrylist.csv', 'csv_to_table_test', ',', '"', '\\', 1, array(), 'file', '\r\n');
569+
// More options
570+
/** Imports (ON DUPLICATE KEY UPDATE) CSV data in Table with possibility to update rows while import.
571+
* @param string $file - CSV File path
572+
* @param string $table - Table name
573+
* @param string $delimiter - COLUMNS TERMINATED BY (Default: ',')
574+
* @param string $enclosure - OPTIONALLY ENCLOSED BY (Default: '"')
575+
* @param string $escape - ESCAPED BY (Defaul: '\')
576+
* @param integer $ignore - Number of ignored rows (Default: 1)
577+
* @param array $update - If row fields needed to be updated eg date format or increment (SQL format only @FIELD is variable with content of that field in CSV row) $update = array('SOME_DATE' => 'STR_TO_DATE(@SOME_DATE, "%d/%m/%Y")', 'SOME_INCREMENT' => '@SOME_INCREMENT + 1')
578+
* @param string $getColumnsFrom - Get Columns Names from (file or table) - this is important if there is update while inserting (Default: file)
579+
* @param string $newLine - New line detelimiter (Default: \n)
580+
* @param resource $link - link identifier
581+
* @return number of inserted rows or false
582+
*/
583+
// $db->importUpdateCSV2Table($file, $table, $delimiter = ',', $enclosure = '"', $escape = '\\', $ignore = 1, $update = array(), $getColumnsFrom = 'file', $newLine = '\n', $link = 0)
584+
// Close connection
585+
$db->close();
586+
///////////////////////////////////////////////////////////////////////////////////////////
587+
588+
// Example 22
591589
// Transactions
592590
///////////////////////////////////////////////////////////////////////////////////////////
593591
$db = new MySQL_wrapper(HOST, USER, PASS, DB);
@@ -609,7 +607,7 @@
609607
// Close connection
610608
$db->close();
611609

612-
// Example 22
610+
// Example 23
613611
// String Replace Table Columns
614612
///////////////////////////////////////////////////////////////////////////////////////////
615613
$db = new MySQL_wrapper(HOST, USER, PASS, DB);

log-mysql.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
2013-10-10 22:49:45 - QUERY -> EXEC -> 0.00292397 -> SHOW COLUMNS FROM `table`;
44
2013-10-10 22:49:45 - ERROR -> NO -> 1064 - DESC -> You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '!@#$' at line 1
55
2013-10-10 23:06:34 - QUERY -> EXEC -> 0.00465322 -> SHOW COLUMNS FROM `table`;
6-
2013-10-10 23:06:34 - ERROR -> NO -> 1064 - DESC -> You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '!@#$' at line 1
6+
2013-10-10 23:06:34 - ERROR -> NO -> 1064 - DESC -> You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '!@#$' at line 1

0 commit comments

Comments
 (0)