Skip to content

Commit a967b6c

Browse files
committed
Changed directory structure to psr-4
1 parent de8645f commit a967b6c

File tree

17 files changed

+231
-4
lines changed

17 files changed

+231
-4
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"email": "[email protected]"
88
}],
99
"require-dev": {
10+
"php": ">= 5.4"
1011
"phpunit/phpunit": "~4.0"
1112
},
1213
"autoload": {

src/Builder/Delete.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Kir\MySQL\Builder;
33

4+
use Kir\MySQL\Tools\AliasReplacer;
5+
46
class Delete extends Statement {
57
/**
68
* @var string
@@ -44,7 +46,7 @@ public function __toString() {
4446
throw new Exception('Specify a table-name');
4547
}
4648

47-
$sqlTable = $this->table;
49+
$sqlTable = (new AliasReplacer($this->db()->getAliasRegistry()))->replace($this->table);
4850
$queryArr = array();
4951
$queryArr[] = "DELETE "."FROM\n\t{$sqlTable}\n";
5052

src/Builder/Insert.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Kir\MySQL\Builder;
33

4+
use Kir\MySQL\Tools\AliasReplacer;
45
use UnexpectedValueException;
56

67
class Insert extends InsertUpdateStatement {
@@ -190,9 +191,11 @@ public function __toString() {
190191
throw new Exception('No field-data found');
191192
}
192193

194+
$tableName = (new AliasReplacer($this->db()->getAliasRegistry()))->replace($this->table);
195+
193196
$queryArr = array();
194197
$ignoreStr = $this->ignore ? ' IGNORE' : '';
195-
$queryArr[] = "INSERT{$ignoreStr} INTO\n\t{$this->table}\n";
198+
$queryArr[] = "INSERT{$ignoreStr} INTO\n\t{$tableName}\n";
196199
$queryArr[] = "SET\n{$insertData}\n";
197200
if($updateData) {
198201
$queryArr[] = "{$updateData}\n";

src/Builder/Select.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Kir\MySQL\Builder;
33

4+
use Kir\MySQL\Tools\AliasReplacer;
5+
46
class Select extends Statement {
57
/**
68
* @var string[]
@@ -427,6 +429,7 @@ private function buildTableName($alias, $name) {
427429
$name = join("\n", $lines);
428430
$name = '(' . trim(rtrim(trim($name), ';')) . ')';
429431
}
432+
$name = (new AliasReplacer($this->db()->getAliasRegistry()))->replace($name);
430433
return sprintf("%s %s", $name, $alias);
431434
}
432435

src/Builder/Update.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Kir\MySQL\Builder;
33

4+
use Kir\MySQL\Tools\AliasReplacer;
5+
46
class Update extends InsertUpdateStatement {
57
/**
68
* @var array
@@ -99,8 +101,10 @@ public function __toString() {
99101
throw new Exception('No field-data found');
100102
}
101103

104+
$tableName = (new AliasReplacer($this->db()->getAliasRegistry()))->replace($this->table);
105+
102106
$queryArr = array();
103-
$queryArr[] = "UPDATE\n\t{$this->table}\nSET\n{$sqlFields}\n";
107+
$queryArr[] = "UPDATE\n\t{$tableName}\nSET\n{$sqlFields}\n";
104108

105109
if (!empty($this->where)) {
106110
$sqlWhere = join("\n\tAND\n\t", $this->where);

src/Database.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33

44
use Kir\MySQL\Builder\Exception;
55
use Kir\MySQL\Builder\RunnableSelect;
6+
use Kir\MySQL\Tools\AliasRegistry;
67

78
interface Database {
9+
/**
10+
* @return AliasRegistry
11+
*/
12+
public function getAliasRegistry();
13+
814
/**
915
* @param string $query
1016
* @throws Exception

src/Databases/MySQL.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,39 @@
44
use Kir\MySQL\Builder;
55
use Kir\MySQL\Builder\Exception;
66
use Kir\MySQL\Database;
7+
use Kir\MySQL\Tools\AliasRegistry;
78
use UnexpectedValueException;
89
use Kir\MySQL\Builder\RunnableSelect;
910

11+
/**
12+
*/
1013
class MySQL implements Database {
1114
/**
1215
* @var array
1316
*/
1417
private static $tableFields = array();
15-
1618
/**
1719
* @var \PDO
1820
*/
1921
private $pdo;
22+
/**
23+
* @var AliasRegistry
24+
*/
25+
private $aliasRegistry;
2026

2127
/**
2228
* @param \PDO $pdo
2329
*/
2430
public function __construct(\PDO $pdo) {
2531
$this->pdo = $pdo;
32+
$this->aliasRegistry = new AliasRegistry();
33+
}
34+
35+
/**
36+
* @return AliasRegistry
37+
*/
38+
public function getAliasRegistry() {
39+
return $this->aliasRegistry;
2640
}
2741

2842
/**

src/Tools/AliasRegistry.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
namespace Kir\MySQL\Tools;
3+
4+
class AliasRegistry {
5+
/**
6+
* @var string[]
7+
*/
8+
private $aliases = array();
9+
10+
/**
11+
* @param string $alias
12+
* @param string $string
13+
* @return $this
14+
*/
15+
public function add($alias, $string) {
16+
$this->aliases[$alias] = $string;
17+
return $this;
18+
}
19+
20+
/**
21+
* @param string $alias
22+
* @throws \Exception
23+
* @return string
24+
*/
25+
public function get($alias) {
26+
if(!array_key_exists($alias, $this->aliases)) {
27+
throw new \Exception("Alias not found: {$alias}");
28+
}
29+
return $this->aliases[$alias];
30+
}
31+
32+
/**
33+
* @return string[]
34+
*/
35+
public function getAll() {
36+
return $this->aliases;
37+
}
38+
}

src/Tools/AliasReplacer.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace Kir\MySQL\Tools;
3+
4+
class AliasReplacer {
5+
/**
6+
* @var AliasRegistry
7+
*/
8+
private $aliasRegistry;
9+
10+
/**
11+
* @param AliasRegistry $aliasRegistry
12+
*/
13+
public function __construct(AliasRegistry $aliasRegistry) {
14+
$this->aliasRegistry = $aliasRegistry;
15+
}
16+
17+
/**
18+
* @param string $name
19+
* @return string
20+
*/
21+
public function replace($name) {
22+
$fn = function ($values) {
23+
$alias = $values[1];
24+
$part = $values[2];
25+
$string = $this->aliasRegistry->get($alias);
26+
return $string . $part;
27+
};
28+
return preg_replace_callback('/^(\\w+)#(\\w+)$/', $fn, $name);
29+
}
30+
}

tests.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<testsuite name="Streams">
99
<file>tests/Builder/SelectTest.php</file>
1010
<file>tests/Builder/UpdateTest.php</file>
11+
<file>tests/Builder/InsertTest.php</file>
12+
<file>tests/Builder/DeleteTest.php</file>
1113
</testsuite>
1214
</testsuites>
1315
</phpunit>

0 commit comments

Comments
 (0)