Skip to content

Commit

Permalink
#3: added PgBabylon\PDO::copyFromFile and PgBabylon\PDO::copyToFile m…
Browse files Browse the repository at this point in the history
…ethods.
  • Loading branch information
rtshome committed Oct 19, 2015
1 parent aca334d commit 0d60e96
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/PDO.php
Expand Up @@ -116,4 +116,41 @@ public function query($statement ,$fetch_class=null , $classname=null)

return $s;
}

/**
* Copies data from file specified by filename into table table_name using delimiter as fields delimiter and fields list
*
* @param $table_name
* @param $filename
* @param string $delimiter If null, tab is used
* @param string $null_as If null, a field with \N is interpreted as NULL
* @param array $fields Array with table field names to import from the file. If null, all fields of the table are selected
* @return bool
*/
public function copyFromFile($table_name, $filename ,$delimiter = null, $null_as = null, $fields = null)
{
if(!is_null($fields) && is_array($fields))
$fields = implode(",", $fields);

return $this->pgsqlCopyFromFile($table_name, $filename, $delimiter, $null_as, $fields);
}

/**
* Copies data from table into file specified by filename using delimiter as fields delimiter and fields list
*
* @param string $table_name It can be a table or a query (in this case it must enclosed between () parentheses)
* @param string $filename
* @param string $delimiter If null, tab is used
* @param string $null_as If null, a field with \N is interpreted as NULL
* @param array $fields Array with table field names to export to the file. If null, all fields of the table are selected
* @return bool
*/
public function copyToFile($table_name, $filename, $delimiter = null, $null_as = null, $fields = null)
{
if(!is_null($fields) && is_array($fields))
$fields = implode(",", $fields);

return $this->pgsqlCopyToFile($table_name, $filename, $delimiter, $null_as, $fields);
}

}
46 changes: 46 additions & 0 deletions tests/DBTests/CopyFromFileTest.php
@@ -0,0 +1,46 @@
<?php
use PgBabylon\PDO;

class CopyFromFileTest extends PHPUnit_Framework_TestCase
{
public function testDataType()
{
if (skipTest()) {
$this->markTestSkipped();
return;
}

if(getDB()->exec("CREATE TABLE copy_from_test(field1 TEXT NOT NULL, field2 INTEGER, field3 DATE)") === false) {
$this->markTestSkipped("Create table for copy_from_test failed");
return;
}

$data = [];
for($i=0;$i<100;$i++)
{
if($i != 50)
$data[] = "text_{$i}\t{$i}";
else
$data[] = "text_{$i}\t\\N";
}

if(!file_put_contents(__TESTS_TEMP_DIR__ . '/copy_from_test.csv', implode("\n", $data))) {
$this->markTestSkipped("Unable to save the file needed for copy from test");
return;
}

$this->assertTrue(getDB()->copyFromFile(
"copy_from_test",
__TESTS_TEMP_DIR__ . '/copy_from_test.csv',
null,
null,
['field1', 'field2']
));

$r = getDB()->query("SELECT count(field1) AS f1, count(field2) AS f2 FROM copy_from_test")
->fetch(PDO::FETCH_ASSOC);

$this->assertEquals($r['f1'], 100);
$this->assertEquals($r['f2'], 99);
}
}
55 changes: 55 additions & 0 deletions tests/DBTests/CopyToFileTest.php
@@ -0,0 +1,55 @@
<?php
use PgBabylon\PDO;

class CopyToFileTest extends PHPUnit_Framework_TestCase
{
public function testDataType()
{
if (skipTest()) {
$this->markTestSkipped();
return;
}

if(getDB()->exec("CREATE TABLE copy_to_test(field1 TEXT NOT NULL, field2 INTEGER, field3 DATE)") === false) {
$this->markTestSkipped("Create table for copy_to_test failed");
return;
}

$data = [];
$s = getDB()->prepare("INSERT INTO copy_to_test(field1,field2) VALUES (:field1, :field2)");
for($i=0;$i<100;$i++)
{
if($i != 50)
{
$data[] = "text_{$i}\t{$i}";
$s->execute([
":field1" => "text_{$i}",
":field2" => $i
]);
}
else
{
$data[] = "text_{$i}\t\\N";
$s->execute([
":field1" => "text_{$i}",
":field2" => null
]);
}
}

$this->assertTrue(getDB()->copyToFile(
"copy_from_test",
__TESTS_TEMP_DIR__ . '/copy_to_test.csv',
null,
null,
['field1', 'field2']
));

if(!($f = file_get_contents(__TESTS_TEMP_DIR__ . '/copy_to_test.csv'))) {
$this->markTestSkipped("Unable to load the file needed for copy to test");
return;
}

$this->assertSame(implode("\n", $data) . "\n", $f);
}
}

0 comments on commit 0d60e96

Please sign in to comment.