Skip to content

Commit 21bd122

Browse files
committed
wip
1 parent eb53e6b commit 21bd122

File tree

13 files changed

+180
-220
lines changed

13 files changed

+180
-220
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
}
1111
],
1212
"minimum-stability": "stable",
13-
"require": {},
13+
"require": {
14+
"php": ">=7"
15+
},
1416
"autoload": {
1517
"psr-4": {
1618
"LajosBencz\\StreamParseSql\\": "src/LajosBencz/StreamParseSql"

src/LajosBencz/StreamParseSql/Buffer.php

Lines changed: 0 additions & 66 deletions
This file was deleted.

src/LajosBencz/StreamParseSql/Exception/FileLockException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
namespace LajosBencz\StreamParseSql\Exception;
55

66

7-
use LajosBencz\StreamParseSql\Exception;
87
use Throwable;
8+
use LajosBencz\StreamParseSql\Exception;
99

1010
class FileLockException extends Exception
1111
{

src/LajosBencz/StreamParseSql/Exception/FileReadException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
namespace LajosBencz\StreamParseSql\Exception;
55

66

7-
use LajosBencz\StreamParseSql\Exception;
87
use Throwable;
8+
use LajosBencz\StreamParseSql\Exception;
99

1010
class FileReadException extends Exception
1111
{

src/LajosBencz/StreamParseSql/Exception/ParseException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace LajosBencz\StreamParseSql\Exception;
44

55

6-
use LajosBencz\StreamParseSql\Exception;
76
use Throwable;
7+
use LajosBencz\StreamParseSql\Exception;
88

99
class ParseException extends Exception
1010
{

src/LajosBencz/StreamParseSql/StreamParseSql.php

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,22 @@ class StreamParseSql
1212

1313
const DEFAULT_CHUNK_SIZE = 4096;
1414

15+
/** @var string */
1516
protected $_filePath = '';
1617

18+
/** @var string */
1719
protected $_delimiter = self::DEFAULT_DELIMITER;
1820

21+
/** @var int */
1922
protected $_chunkSize = self::DEFAULT_CHUNK_SIZE;
2023

21-
public static function Assemble(Token ...$tokens): string
22-
{
23-
$sql = '';
24-
foreach($tokens as $t) {
25-
$sql.= $t->content;
26-
}
27-
$sql = preg_replace('/[\s\t]+/', ' ', $sql);
28-
if(!preg_match('/;[\r\n\s]*$/', $sql)) {
29-
$sql.= ';';
30-
}
31-
return $sql;
32-
}
33-
24+
/**
25+
* Parses commands from given file command-by-command
26+
* @param string $filePath Path to large file with SQL commands
27+
* @param string $delimiter Command delimiter used in the SQL file
28+
* @param int $chunkSize Size of the read chunk
29+
* @throws Exception\FileReadException
30+
*/
3431
public function __construct(string $filePath, string $delimiter = self::DEFAULT_DELIMITER, int $chunkSize = self::DEFAULT_CHUNK_SIZE)
3532
{
3633
$this->setDelimiter($delimiter);
@@ -41,24 +38,43 @@ public function __construct(string $filePath, string $delimiter = self::DEFAULT_
4138
$this->_chunkSize = $chunkSize;
4239
}
4340

41+
/**
42+
* Path to the large SQL file
43+
* @return string
44+
*/
4445
public function getFilePath(): string
4546
{
4647
return $this->_filePath;
4748
}
4849

50+
/**
51+
* Sets the command delimiter used in the SQL file
52+
* @param string $delimiter
53+
*/
4954
public function setDelimiter(string $delimiter = self::DEFAULT_DELIMITER): void
5055
{
5156
$this->_delimiter = $delimiter;
5257
}
5358

59+
/**
60+
* Returns the command delimiter used in the SQL file
61+
* @return string
62+
*/
5463
public function getDelimiter(): string
5564
{
5665
return $this->_delimiter;
5766
}
5867

68+
/**
69+
* Will yield a single SQL command at a time until the file is read to the end
70+
* @return string[]|Generator
71+
* @throws Exception\FileLockException
72+
* @throws Exception\FileReadException
73+
* @throws Exception\ParseException
74+
*/
5975
public function parse(): Generator
6076
{
61-
$tokenizer = new Tokenizer;
77+
$tokenizer = new Tokenizer($this->getDelimiter());
6278
$tokens = [];
6379
$filePath = $this->getFilePath();
6480
$fh = fopen($filePath, 'r');
@@ -71,15 +87,15 @@ public function parse(): Generator
7187
while (!feof($fh)) {
7288
$line = fread($fh, $this->_chunkSize);
7389
foreach($tokenizer->append($line, feof($fh)) as $token) {
74-
switch($token->pattern->name) {
90+
switch($token->type) {
7591
case 'COMMENT_SINGLE':
7692
case 'COMMENT_MULTI':
7793
case 'NEWLINE':
7894
break;
7995
default:
8096
$tokens[] = $token;
81-
if($token->pattern->name == 'DELIMITER') {
82-
yield self::Assemble(...$tokens);
97+
if($token->type == 'DELIMITER') {
98+
yield Token::Assemble(...$tokens);
8399
$tokens = [];
84100
}
85101
break;
@@ -89,7 +105,7 @@ public function parse(): Generator
89105
flock($fh, LOCK_UN);
90106
fclose($fh);
91107
if(count($tokens) > 0) {
92-
yield self::Assemble(...$tokens);
108+
yield Token::Assemble(...$tokens);
93109
}
94110
}
95111
}
Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,43 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace LajosBencz\StreamParseSql;
45

56

67
class Token
78
{
8-
public $pattern = null;
9+
/**
10+
* Assembles a list of tokens to an SQL command
11+
* @param Token ...$tokens
12+
* @return string
13+
*/
14+
public static function Assemble(Token ...$tokens): string
15+
{
16+
$sql = '';
17+
foreach($tokens as $t) {
18+
$sql.= $t->content;
19+
}
20+
$sql = preg_replace('/[\s\t]+/', ' ', $sql);
21+
if(!preg_match('/;[\r\n\s]*$/', $sql)) {
22+
$sql.= ';';
23+
}
24+
return $sql;
25+
}
26+
27+
/** @var string */
28+
public $type = '';
29+
30+
/** @var string */
931
public $content = '';
10-
public $offset = 0;
11-
public $length = 0;
1232

13-
public function __construct(TokenPattern $pattern, string $content, int $offset, int $length)
33+
/**
34+
* Represents a token in memory
35+
* @param string $type
36+
* @param string $content
37+
*/
38+
public function __construct(string $type, string $content)
1439
{
15-
$this->pattern = $pattern;
40+
$this->type = $type;
1641
$this->content = $content;
17-
$this->offset = $offset;
18-
$this->length = $length;
1942
}
2043
}

src/LajosBencz/StreamParseSql/TokenPattern.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)