Skip to content

Commit 93170ce

Browse files
committed
SqlDocument: added method save()
1 parent 3c500a2 commit 93170ce

6 files changed

Lines changed: 80 additions & 0 deletions

File tree

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Generate SQL:
100100
```php
101101
$sql->toSql($driver); // returns string
102102
$sql->getSqlQueries($driver); // returns string[]
103+
$sql->save($file, $driver); // saves SQL into file
103104
```
104105

105106
## Supported database

src/SqlDocument.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,30 @@ public function toSql(IDriver $driver)
6767
}
6868

6969

70+
/**
71+
* @param string
72+
* @param IDriver
73+
* @return void
74+
* @throws IOException
75+
*/
76+
public function save($file, IDriver $driver)
77+
{
78+
// create directory
79+
$dir = dirname($file);
80+
81+
if (!is_dir($dir) && !@mkdir($dir, 0777, TRUE) && !is_dir($dir)) { // @ - dir may already exist
82+
throw new IOException("Unable to create directory '$dir'.");
83+
}
84+
85+
// write file
86+
$content = $this->toSql($driver);
87+
88+
if (@file_put_contents($file, $content) === FALSE) { // @ is escalated to exception
89+
throw new IOException("Unable to write file '$file'.");
90+
}
91+
}
92+
93+
7094
/**
7195
* @param string
7296
* @param array

src/exceptions.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class InvalidArgumentException extends Exception
1818
}
1919

2020

21+
class IOException extends Exception
22+
{
23+
}
24+
25+
2126
class NotImplementedException extends Exception
2227
{
2328
}

tests/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/tmp/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use CzProject\SqlGenerator\Drivers;
4+
use CzProject\SqlGenerator\SqlDocument;
5+
use CzProject\SqlGenerator\Statements\IndexDefinition;
6+
use Tester\Assert;
7+
8+
require __DIR__ . '/../bootstrap.php';
9+
10+
define('TEMP_DIR', prepareTempDir());
11+
12+
13+
test(function () {
14+
15+
$sql = new SqlDocument;
16+
$driver = new Drivers\MysqlDriver;
17+
$file = TEMP_DIR . '/subdir/file.sql';
18+
19+
$sql->command('SET NAMES "utf8mb4"');
20+
$sql->save($file, $driver);
21+
22+
Assert::same(implode("\n", array(
23+
'SET NAMES "utf8mb4";',
24+
'',
25+
)), file_get_contents($file));
26+
27+
});
28+
29+
30+
test(function () {
31+
32+
$sql = new SqlDocument;
33+
$sql->command('SET NAMES "utf8mb4"');
34+
$file = TEMP_DIR . '/subdir';
35+
36+
Assert::exception(function () use ($sql, $file) {
37+
$sql->save($file, new Drivers\MysqlDriver);
38+
}, 'CzProject\SqlGenerator\IOException', "Unable to write file '$file'.");
39+
40+
});

tests/bootstrap.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,12 @@ function test($cb)
99
{
1010
$cb();
1111
}
12+
13+
14+
function prepareTempDir()
15+
{
16+
$tempDir = __DIR__ . '/tmp/' . getmypid();
17+
mkdir(dirname($tempDir), 0777, TRUE);
18+
Tester\Helpers::purge($tempDir);
19+
return $tempDir;
20+
}

0 commit comments

Comments
 (0)