Skip to content

Commit

Permalink
SqlDocument: added method save()
Browse files Browse the repository at this point in the history
  • Loading branch information
janpecha committed Mar 29, 2019
1 parent 3c500a2 commit 93170ce
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Generate SQL:
```php
$sql->toSql($driver); // returns string
$sql->getSqlQueries($driver); // returns string[]
$sql->save($file, $driver); // saves SQL into file
```

## Supported database
Expand Down
24 changes: 24 additions & 0 deletions src/SqlDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ public function toSql(IDriver $driver)
}


/**
* @param string
* @param IDriver
* @return void
* @throws IOException
*/
public function save($file, IDriver $driver)
{
// create directory
$dir = dirname($file);

if (!is_dir($dir) && !@mkdir($dir, 0777, TRUE) && !is_dir($dir)) { // @ - dir may already exist
throw new IOException("Unable to create directory '$dir'.");
}

// write file
$content = $this->toSql($driver);

if (@file_put_contents($file, $content) === FALSE) { // @ is escalated to exception
throw new IOException("Unable to write file '$file'.");
}
}


/**
* @param string
* @param array
Expand Down
5 changes: 5 additions & 0 deletions src/exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class InvalidArgumentException extends Exception
}


class IOException extends Exception
{
}


class NotImplementedException extends Exception
{
}
Expand Down
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/tmp/
40 changes: 40 additions & 0 deletions tests/SqlGenerator/SqlDocument.save.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use CzProject\SqlGenerator\Drivers;
use CzProject\SqlGenerator\SqlDocument;
use CzProject\SqlGenerator\Statements\IndexDefinition;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

define('TEMP_DIR', prepareTempDir());


test(function () {

$sql = new SqlDocument;
$driver = new Drivers\MysqlDriver;
$file = TEMP_DIR . '/subdir/file.sql';

$sql->command('SET NAMES "utf8mb4"');
$sql->save($file, $driver);

Assert::same(implode("\n", array(
'SET NAMES "utf8mb4";',
'',
)), file_get_contents($file));

});


test(function () {

$sql = new SqlDocument;
$sql->command('SET NAMES "utf8mb4"');
$file = TEMP_DIR . '/subdir';

Assert::exception(function () use ($sql, $file) {
$sql->save($file, new Drivers\MysqlDriver);
}, 'CzProject\SqlGenerator\IOException', "Unable to write file '$file'.");

});
9 changes: 9 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ function test($cb)
{
$cb();
}


function prepareTempDir()
{
$tempDir = __DIR__ . '/tmp/' . getmypid();
mkdir(dirname($tempDir), 0777, TRUE);
Tester\Helpers::purge($tempDir);
return $tempDir;
}

0 comments on commit 93170ce

Please sign in to comment.