Skip to content

Commit 5ff94cc

Browse files
committed
Added loadXML and tests
1 parent 487c461 commit 5ff94cc

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

MysqliDb.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,65 @@ public function loadData($importTable, $importFile, $importSettings =
969969
// Let the user know if the import failed / succeeded
970970
return $success;
971971
}
972+
973+
974+
public function loadXML($importTable, $importFile, $importSettings =
975+
Array("linesToIgnore" => 0))
976+
{
977+
// Define default success var
978+
$success = false;
979+
980+
// We have to check if the file exists
981+
if(file_exists($importFile)) {
982+
// Create default values
983+
$ignoreLines = 0; // Default 0
984+
985+
// Check the import settings
986+
if(gettype($importSettings) == "array") {
987+
988+
if(isset($importSettings["linesToIgnore"])) {
989+
$ignoreLines = $importSettings["linesToIgnore"];
990+
}
991+
}
992+
993+
// Add the prefix to the import table
994+
$table = self::$prefix . $importTable;
995+
996+
// Add 1 more slash to every slash so maria will interpret it as a path
997+
$importFile = str_replace("\\", "\\\\", $importFile);
998+
999+
// Build SQL Syntax
1000+
$sqlSyntax = sprintf('LOAD XML INFILE \'%s\' INTO TABLE %s',
1001+
$importFile, $table);
1002+
1003+
// FIELDS
1004+
if(isset($importSettings["rowTag"])) {
1005+
$sqlSyntax .= sprintf(' ROWS IDENTIFIED BY \'%s\'', $importSettings["rowTag"]);
1006+
}
1007+
1008+
// IGNORE LINES
1009+
$sqlSyntax .= sprintf(' IGNORE %d LINES', $ignoreLines);
1010+
1011+
// Exceute the query unprepared because LOAD XML only works with unprepared statements.
1012+
$result = $this->queryUnprepared($sqlSyntax);
1013+
1014+
// Are there rows modified?
1015+
if($result) {
1016+
$success = true;
1017+
}
1018+
// Something went wrong
1019+
else {
1020+
$success = false;
1021+
}
1022+
}
1023+
else {
1024+
// Throw an exception
1025+
throw new Exception("importXML -> importFile ".$importFile." does not exists!");
1026+
}
1027+
1028+
// Let the user know if the import failed / succeeded
1029+
return $success;
1030+
}
9721031

9731032
/**
9741033
* This method allows you to specify multiple (method chaining optional) ORDER BY statements for SQL queries.

0 commit comments

Comments
 (0)