@@ -393,12 +393,8 @@ public function setPrefix($prefix = '')
393393 * @author Jonas Barascu
394394 * @param [[Type]] $query [[Description]]
395395 */
396- private function queryUnprepared ($ query, $ escapeQuery = false )
396+ private function queryUnprepared ($ query )
397397 {
398- // Should we escape?
399- if ($ escapeQuery ) {
400- $ query = $ this ->mysqli ()->real_escape_string ($ query );
401- }
402398 // Execute query
403399 $ stmt = $ this ->mysqli ()->query ($ query );
404400
@@ -906,76 +902,63 @@ public function join($joinTable, $joinCondition, $joinType = '')
906902 * @param string $importSettings An Array defining the import settings as described in the README.md
907903 * @return boolean
908904 */
909- public function loadData ($ importTable , $ importFile , $ importSettings =
910- Array ("fieldChar " => '; ' , "lineChar " => '\r\n ' , "linesToIgnore " => 1 ))
905+ public function loadData ($ importTable , $ importFile , $ importSettings = null )
911906 {
912907 // Define default success var
913908 $ success = false ;
914909
915910 // We have to check if the file exists
916- if (file_exists ($ importFile )) {
917- // Create default values
918- $ terminateCharField = '; ' ; // Default is ;
919- $ terminateCharLine = PHP_EOL ; // Default \r\n or PHP_EOL (*nix is \n)
920- $ ignoreLines = 1 ; // Default 1
921-
922- // Check the import settings
923- if (gettype ($ importSettings ) == "array " ) {
924- if (isset ($ importSettings ["fieldChar " ])) {
925- $ terminateCharField = $ importSettings ["fieldChar " ];
926- }
927- if (isset ($ importSettings ["lineChar " ])) {
928- $ terminateCharLine = $ importSettings ["lineChar " ];
929- }
930- if (isset ($ importSettings ["linesToIgnore " ])) {
931- $ ignoreLines = $ importSettings ["linesToIgnore " ];
932- }
933- }
934-
935- // Add the prefix to the import table
936- $ table = self ::$ prefix . $ importTable ;
937-
938- // Add 1 more slash to every slash so maria will interpret it as a path
939- $ importFile = str_replace ("\\" , "\\\\" , $ importFile );
940-
941- // Build SQL Syntax
942- $ sqlSyntax = sprintf ('LOAD DATA INFILE \'%s \' INTO TABLE %s ' ,
943- $ importFile , $ table );
944-
945- // FIELDS
946- $ sqlSyntax .= sprintf (' FIELDS TERMINATED BY \'%s \'' , $ terminateCharField );
947- if (isset ($ importSettings ["fieldEnclosure " ])){
948- $ sqlSyntax .= sprintf (' ENCLOSED BY \'%s \'' , $ importSettings ["fieldEnclosure " ]);
949- }
950-
951- // LINES
952- $ sqlSyntax .= sprintf (' LINES TERMINATED BY \'%s \'' , $ terminateCharLine );
953- if (isset ($ importSettings ["lineStarting " ])){
954- $ sqlSyntax .= sprintf (' STARTING BY \'%s \'' , $ importSettings ["lineStarting " ]);
955- }
956-
957- // IGNORE LINES
958- $ sqlSyntax .= sprintf (' IGNORE %d LINES ' , $ ignoreLines );
959-
960- // Exceute the query unprepared because LOAD DATA only works with unprepared statements.
961- $ result = $ this ->queryUnprepared ($ sqlSyntax );
962-
963- // Are there rows modified?
964- if ($ result ) {
965- $ success = true ;
966- }
967- // Something went wrong
968- else {
969- $ success = false ;
970- }
971- }
972- else {
911+ if (!file_exists ($ importFile )) {
973912 // Throw an exception
974913 throw new Exception ("importCSV -> importFile " .$ importFile ." does not exists! " );
914+ return ;
915+ }
916+
917+ // Define the default values
918+ // We will merge it later
919+ $ settings = Array ("fieldChar " => '; ' , "lineChar " => '\r\n ' , "linesToIgnore " => 1 );
920+
921+ // Check the import settings
922+ if (gettype ($ importSettings ) == "array " ) {
923+ // Merge the default array with the custom one
924+ $ settings = array_merge ($ settings , $ importSettings );
925+ }
926+
927+ echo (var_dump ($ settings ));
928+
929+ // Add the prefix to the import table
930+ $ table = self ::$ prefix . $ importTable ;
931+
932+ // Add 1 more slash to every slash so maria will interpret it as a path
933+ $ importFile = str_replace ("\\" , "\\\\" , $ importFile );
934+
935+ // Build SQL Syntax
936+ $ sqlSyntax = sprintf ('LOAD DATA INFILE \'%s \' INTO TABLE %s ' ,
937+ $ importFile , $ table );
938+
939+ // FIELDS
940+ $ sqlSyntax .= sprintf (' FIELDS TERMINATED BY \'%s \'' , $ settings ["fieldChar " ]);
941+ if (isset ($ settings ["fieldEnclosure " ])) {
942+ $ sqlSyntax .= sprintf (' ENCLOSED BY \'%s \'' , $ settings ["fieldEnclosure " ]);
975943 }
976944
945+ // LINES
946+ $ sqlSyntax .= sprintf (' LINES TERMINATED BY \'%s \'' , $ settings ["lineChar " ]);
947+ if (isset ($ settings ["lineStarting " ])) {
948+ $ sqlSyntax .= sprintf (' STARTING BY \'%s \'' , $ settings ["lineStarting " ]);
949+ }
950+
951+ // IGNORE LINES
952+ $ sqlSyntax .= sprintf (' IGNORE %d LINES ' , $ settings ["linesToIgnore " ]);
953+
954+ echo ($ sqlSyntax );
955+
956+ // Exceute the query unprepared because LOAD DATA only works with unprepared statements.
957+ $ result = $ this ->queryUnprepared ($ sqlSyntax );
958+
959+ // Are there rows modified?
977960 // Let the user know if the import failed / succeeded
978- return $ success ;
961+ return ( bool ) $ result ;
979962 }
980963
981964 /**
@@ -989,59 +972,51 @@ public function loadData($importTable, $importFile, $importSettings =
989972 *
990973 * @return boolean Returns true if the import succeeded, false if it failed.
991974 */
992- public function loadXML ($ importTable , $ importFile , $ importSettings = Array ( " linesToIgnore " => 0 ) )
975+ public function loadXml ($ importTable , $ importFile , $ importSettings = null )
993976 {
994977 // Define default success var
995978 $ success = false ;
996979
997980 // We have to check if the file exists
998- if (file_exists ($ importFile )) {
999- // Create default values
1000- $ ignoreLines = 0 ; // Default 0
1001-
1002- // Check the import settings
1003- if (gettype ($ importSettings ) == "array " ) {
1004- if (isset ($ importSettings ["linesToIgnore " ])) {
1005- $ ignoreLines = $ importSettings ["linesToIgnore " ];
1006- }
1007- }
1008-
1009- // Add the prefix to the import table
1010- $ table = self ::$ prefix . $ importTable ;
981+ if (!file_exists ($ importFile )) {
982+ // Does not exists
983+ throw new Exception ("loadXml: Import file does not exists " );
984+ return ;
985+ }
986+
987+
988+ // Create default values
989+ $ settings = Array ("linesToIgnore " => 0 );
1011990
1012- // Add 1 more slash to every slash so maria will interpret it as a path
1013- $ importFile = str_replace ("\\" , "\\\\" , $ importFile );
991+ // Check the import settings
992+ if (gettype ($ importSettings ) == "array " ) {
993+ $ settings = array_merge ($ settings , $ importSettings );
994+ }
1014995
1015- // Build SQL Syntax
1016- $ sqlSyntax = sprintf ('LOAD XML INFILE \'%s \' INTO TABLE %s ' ,
996+ // Add the prefix to the import table
997+ $ table = self ::$ prefix . $ importTable ;
998+
999+ // Add 1 more slash to every slash so maria will interpret it as a path
1000+ $ importFile = str_replace ("\\" , "\\\\" , $ importFile );
1001+
1002+ // Build SQL Syntax
1003+ $ sqlSyntax = sprintf ('LOAD XML INFILE \'%s \' INTO TABLE %s ' ,
10171004 $ importFile , $ table );
1018- // FIELDS
1019- if (isset ($ importSettings ["rowTag " ])) {
1020- $ sqlSyntax .= sprintf (' ROWS IDENTIFIED BY \'%s \'' , $ importSettings ["rowTag " ]);
1021- }
1022-
1023- // IGNORE LINES
1024- $ sqlSyntax .= sprintf (' IGNORE %d LINES ' , $ ignoreLines );
1025-
1026- // Exceute the query unprepared because LOAD XML only works with unprepared statements.
1027- $ result = $ this ->queryUnprepared ($ sqlSyntax );
1028-
1029- // Are there rows modified?
1030- if ($ result ) {
1031- $ success = true ;
1032- }
1033- // Something went wrong
1034- else {
1035- $ success = false ;
1036- }
1037- }
1038- else {
1039- // Throw an exception
1040- throw new Exception ("importXML -> importFile " .$ importFile ." does not exists! " );
1005+
1006+ // FIELDS
1007+ if (isset ($ settings ["rowTag " ])) {
1008+ $ sqlSyntax .= sprintf (' ROWS IDENTIFIED BY \'%s \'' , $ settings ["rowTag " ]);
10411009 }
1010+
1011+ // IGNORE LINES
1012+ $ sqlSyntax .= sprintf (' IGNORE %d LINES ' , $ settings ["linesToIgnore " ]);
1013+
1014+ // Exceute the query unprepared because LOAD XML only works with unprepared statements.
1015+ $ result = $ this ->queryUnprepared ($ sqlSyntax );
10421016
1017+ // Are there rows modified?
10431018 // Let the user know if the import failed / succeeded
1044- return $ success ;
1019+ return ( bool ) $ result ;
10451020 }
10461021
10471022 /**
0 commit comments