Skip to content

Commit 91beb07

Browse files
committedJun 28, 2016
Fixed up formatting + removed gitignore + added tests for testing the import functions
1 parent dae696f commit 91beb07

File tree

3 files changed

+68
-17
lines changed

3 files changed

+68
-17
lines changed
 

‎.gitignore

-5
This file was deleted.

‎MysqliDb.php

+7-12
Original file line numberDiff line numberDiff line change
@@ -903,11 +903,7 @@ public function join($joinTable, $joinCondition, $joinType = '')
903903
* @author Jonas Barascu (Noneatme)
904904
* @param string $importTable The database table where the data will be imported into.
905905
* @param string $importFile The file to be imported. Please use double backslashes \\ and make sure you
906-
* use an absolute path.
907-
* @param string $terminateCharField The char which will be used to separate the data in a row.
908-
* @param string $terminateCharLine The char which marks the EOL. (PHP_EOL is also possible)
909-
* @param string $ignoreLines The ammount of lines to ignore. Useful if your #0 row marks the data structure.
910-
*
906+
* @param string $importSettings An Array defining the import settings as described in the README.md
911907
* @return boolean
912908
*/
913909
public function loadData($importTable, $importFile, $importSettings =
@@ -987,8 +983,10 @@ public function loadData($importTable, $importFile, $importSettings =
987983
* Check out the LOAD XML syntax for your MySQL server.
988984
*
989985
* @author Jonas Barascu
990-
* @param string $importTable The table in which the data will be imported to.
991-
* @param string $importFile The file which contains the .XML data.
986+
* @param string $importTable The table in which the data will be imported to.
987+
* @param string $importFile The file which contains the .XML data.
988+
* @param string $importSettings An Array defining the import settings as described in the README.md
989+
*
992990
* @return boolean Returns true if the import succeeded, false if it failed.
993991
*/
994992
public function loadXML($importTable, $importFile, $importSettings = Array("linesToIgnore" => 0))
@@ -1003,7 +1001,6 @@ public function loadXML($importTable, $importFile, $importSettings = Array("line
10031001

10041002
// Check the import settings
10051003
if(gettype($importSettings) == "array") {
1006-
10071004
if(isset($importSettings["linesToIgnore"])) {
10081005
$ignoreLines = $importSettings["linesToIgnore"];
10091006
}
@@ -1018,7 +1015,6 @@ public function loadXML($importTable, $importFile, $importSettings = Array("line
10181015
// Build SQL Syntax
10191016
$sqlSyntax = sprintf('LOAD XML INFILE \'%s\' INTO TABLE %s',
10201017
$importFile, $table);
1021-
10221018
// FIELDS
10231019
if(isset($importSettings["rowTag"])) {
10241020
$sqlSyntax .= sprintf(' ROWS IDENTIFIED BY \'%s\'', $importSettings["rowTag"]);
@@ -1158,11 +1154,10 @@ public function lock($table)
11581154
}
11591155
}
11601156
}
1161-
else
1162-
{
1157+
else{
11631158
// Build the table prefix
11641159
$table = self::$prefix . $table;
1165-
1160+
11661161
// Build the query
11671162
$this->_query = "LOCK TABLES ".$table." ".$this->_tableLockMethod;
11681163
}

‎tests/dataimport/test.import.php

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
// TEST HAS BEEN DONE USING PHP 7.0
3+
// Basic Stuff
4+
error_reporting (E_ALL|E_STRICT);
5+
require_once ("../../MysqliDb.php");
6+
require_once ("../../dbObject.php");
7+
// Create database context
8+
$db = new MysqliDb ('localdev', 'noneatme', '12345', 'db_test');
9+
10+
// Import CSV
11+
echo "Testing: File Not Found", PHP_EOL;
12+
// File not Found
13+
goto test_filenotfound;
14+
// File Not Found Test
15+
test_filenotfound:
16+
try
17+
{
18+
// It should throw an exception
19+
$db->loadData("users", "datanew.csv");
20+
}
21+
catch(Exception $e)
22+
{
23+
echo "Test 1 Succeeded!", PHP_EOL;
24+
// goto newtest
25+
goto test_import1;
26+
}
27+
test_import1:
28+
{
29+
try
30+
{
31+
// Import the CSV
32+
$db->loadData("users", // Table
33+
"D:\\DEV\\git\\PHP-MySQLi-Database-Class\\tests\\dataimport\\data.csv",
34+
Array("fieldEnclosure" => '', "lineStarting" => ''));
35+
echo "Test 2 Succeeded!", PHP_EOL;
36+
37+
goto test_import2;
38+
}
39+
catch(Exception $e)
40+
{
41+
echo($e);
42+
}
43+
}
44+
test_import2:
45+
{
46+
try
47+
{
48+
$db->setLockMethod("WRITE")->lock(array("users", "log"));
49+
50+
$db->loadXML("users",
51+
"D:\\DEV\\git\\PHP-MySQLi-Database-Class\\tests\\dataimport\\data.xml");
52+
echo "Test 3 Succeeded!", PHP_EOL;
53+
54+
$db->unlock();
55+
56+
}
57+
catch(Exception $e)
58+
{
59+
echo($e);
60+
}
61+
}

0 commit comments

Comments
 (0)
Please sign in to comment.