Skip to content

Commit 487c461

Browse files
committed
For Issue #436: (Fixed now!)
Added the loadData Method Added a private method for executing unprepared statements
1 parent b145912 commit 487c461

File tree

1 file changed

+114
-1
lines changed

1 file changed

+114
-1
lines changed

MysqliDb.php

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,32 @@ public function setPrefix($prefix = '')
373373
return $this;
374374
}
375375

376+
/**
377+
* Pushes a unprepared statement to the mysqli stack.
378+
* WARNING: Use with caution.
379+
* This method does not escape strings by default so make sure you'll never use it in production.
380+
*
381+
* @author Jonas Barascu
382+
* @param [[Type]] $query [[Description]]
383+
*/
384+
private function queryUnprepared($query, $escapeQuery = false)
385+
{
386+
// Should we escape?
387+
if($escapeQuery) {
388+
$query = $this->mysqli()->real_escape_string($query);
389+
}
390+
// Execute query
391+
$stmt = $this->mysqli()->query($query);
392+
393+
// Failed?
394+
if(!$stmt){
395+
throw new Exception("Unprepared Query Failed, ERRNO: ".$this->mysqli()->errno." (".$this->mysqli()->error.")");
396+
};
397+
398+
// return stmt for future use
399+
return $stmt;
400+
}
401+
376402
/**
377403
* Execute raw SQL query.
378404
*
@@ -856,6 +882,93 @@ public function join($joinTable, $joinCondition, $joinType = '')
856882

857883
return $this;
858884
}
885+
886+
887+
/**
888+
* This is a basic method which allows you to import raw .CSV data into a table
889+
* Please check out http://dev.mysql.com/doc/refman/5.7/en/load-data.html for a valid .csv file.
890+
891+
* @author Jonas Barascu (Noneatme)
892+
* @param string $importTable The database table where the data will be imported into.
893+
* @param string $importFile The file to be imported. Please use double backslashes \\ and make sure you
894+
* use an absolute path.
895+
* @param string $terminateCharField The char which will be used to separate the data in a row.
896+
* @param string $terminateCharLine The char which marks the EOL. (PHP_EOL is also possible)
897+
* @param string $ignoreLines The ammount of lines to ignore. Useful if your #0 row marks the data structure.
898+
*
899+
* @return boolean
900+
*/
901+
public function loadData($importTable, $importFile, $importSettings =
902+
Array("fieldChar" => ';', "lineChar" => '\r\n', "linesToIgnore" => 1))
903+
{
904+
// Define default success var
905+
$success = false;
906+
907+
// We have to check if the file exists
908+
if(file_exists($importFile)) {
909+
// Create default values
910+
$terminateCharField = ';'; // Default is ;
911+
$terminateCharLine = PHP_EOL; // Default \r\n or PHP_EOL (*nix is \n)
912+
$ignoreLines = 1; // Default 1
913+
914+
// Check the import settings
915+
if(gettype($importSettings) == "array") {
916+
if(isset($importSettings["fieldChar"])) {
917+
$terminateCharField = $importSettings["fieldChar"];
918+
}
919+
if(isset($importSettings["lineChar"])) {
920+
$terminateCharLine = $importSettings["lineChar"];
921+
}
922+
if(isset($importSettings["linesToIgnore"])) {
923+
$ignoreLines = $importSettings["linesToIgnore"];
924+
}
925+
}
926+
927+
// Add the prefix to the import table
928+
$table = self::$prefix . $importTable;
929+
930+
// Add 1 more slash to every slash so maria will interpret it as a path
931+
$importFile = str_replace("\\", "\\\\", $importFile);
932+
933+
// Build SQL Syntax
934+
$sqlSyntax = sprintf('LOAD DATA INFILE \'%s\' INTO TABLE %s',
935+
$importFile, $table);
936+
937+
// FIELDS
938+
$sqlSyntax .= sprintf(' FIELDS TERMINATED BY \'%s\'', $terminateCharField);
939+
if(isset($importSettings["fieldEnclosure"])){
940+
$sqlSyntax .= sprintf(' ENCLOSED BY \'%s\'', $importSettings["fieldEnclosure"]);
941+
}
942+
943+
// LINES
944+
$sqlSyntax .= sprintf(' LINES TERMINATED BY \'%s\'', $terminateCharLine);
945+
if(isset($importSettings["lineStarting"])){
946+
$sqlSyntax .= sprintf(' STARTING BY \'%s\'', $importSettings["lineStarting"]);
947+
}
948+
949+
// IGNORE LINES
950+
$sqlSyntax .= sprintf(' IGNORE %d LINES', $ignoreLines);
951+
952+
// Exceute the query unprepared because LOAD DATA only works with unprepared statements.
953+
$result = $this->queryUnprepared($sqlSyntax);
954+
955+
// Are there rows modified?
956+
if($result) {
957+
$success = true;
958+
}
959+
// Something went wrong
960+
else {
961+
$success = false;
962+
}
963+
}
964+
else {
965+
// Throw an exception
966+
throw new Exception("importCSV -> importFile ".$importFile." does not exists!");
967+
}
968+
969+
// Let the user know if the import failed / succeeded
970+
return $success;
971+
}
859972

860973
/**
861974
* This method allows you to specify multiple (method chaining optional) ORDER BY statements for SQL queries.
@@ -1908,4 +2021,4 @@ public function paginate ($table, $page, $fields = null) {
19082021
}
19092022
}
19102023

1911-
// END class
2024+
// END class

0 commit comments

Comments
 (0)