1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class DBQuery |
4
|
|
|
* |
5
|
|
|
* @filesource DBQuery.php |
6
|
|
|
* @created 03.06.2017 |
7
|
|
|
* @package chillerlan\Database |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2017 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\Database; |
14
|
|
|
|
15
|
|
|
use chillerlan\Database\Drivers\DBDriverInterface; |
16
|
|
|
use chillerlan\Database\Query\AlterInterface; |
17
|
|
|
use chillerlan\Database\Query\CreateInterface; |
18
|
|
|
use chillerlan\Database\Query\DropInterface; |
19
|
|
|
use chillerlan\Database\Query\StatementAbstract; |
20
|
|
|
use chillerlan\Database\Query\CreateDatabaseInterface; |
21
|
|
|
use chillerlan\Database\Query\CreateTableInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @property \chillerlan\Database\Query\SelectInterface $select |
25
|
|
|
* @property \chillerlan\Database\Query\InsertInterface $insert |
26
|
|
|
* @property \chillerlan\Database\Query\UpdateInterface $update |
27
|
|
|
* @property \chillerlan\Database\Query\DeleteInterface $delete |
28
|
|
|
* @property \chillerlan\Database\Query\CreateInterface $create |
29
|
|
|
* @property \chillerlan\Database\Query\DropInterface $drop |
30
|
|
|
* @property \chillerlan\Database\Query\AlterInterface $alter |
31
|
|
|
*/ |
32
|
|
|
class DBQuery{ |
33
|
|
|
|
34
|
|
|
const DIALECTS = [ |
35
|
|
|
'mysql' => 'MySQL', |
36
|
|
|
'pgsql' => 'Postgres', |
37
|
|
|
'sqlite' => 'SQLite', |
38
|
|
|
'firebird' => 'Firebird', |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \chillerlan\Database\Drivers\DBDriverInterface |
43
|
|
|
*/ |
44
|
|
|
protected $DBDriver; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $dialect; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* DBQuery constructor. |
53
|
|
|
* |
54
|
|
|
* @param \chillerlan\Database\Drivers\DBDriverInterface $DBDriver |
55
|
|
|
*/ |
56
|
|
|
public function __construct(DBDriverInterface $DBDriver){ |
57
|
|
|
$this->DBDriver = $DBDriver; |
58
|
|
|
|
59
|
|
|
$this->dialect = __NAMESPACE__.'\\Query\\Dialects\\'.self::DIALECTS[$this->DBDriver->dialect]; |
|
|
|
|
60
|
|
|
|
61
|
|
|
mb_internal_encoding('UTF-8'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $name |
66
|
|
|
* |
67
|
|
|
* @return \chillerlan\Database\Query\StatementInterface|null |
|
|
|
|
68
|
|
|
*/ |
69
|
|
|
public function __get(string $name){ |
70
|
|
|
$name = strtolower($name); |
71
|
|
|
|
72
|
|
|
if(in_array($name, ['select', 'insert', 'update', 'delete'])){ |
73
|
|
|
$class = $this->dialect.'\\'.ucfirst($name); |
74
|
|
|
return new $class($this->DBDriver, $this->dialect); |
75
|
|
|
} |
76
|
|
|
elseif($name === 'create'){ |
77
|
|
|
|
78
|
|
|
return new class($this->DBDriver, $this->dialect) extends StatementAbstract implements CreateInterface{ |
79
|
|
|
|
80
|
|
|
public function database(string $dbname = null):CreateDatabaseInterface{ |
81
|
|
|
return $this->getStatementClass('CreateDatabase')->name($dbname); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function table(string $tablename = null):CreateTableInterface{ |
85
|
|
|
return $this->getStatementClass('CreateTable')->name($tablename); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
# public function index():CreateInterface{} |
|
|
|
|
89
|
|
|
# public function view():CreateInterface{} |
90
|
|
|
# public function trigger():CreateInterface{} |
91
|
|
|
|
92
|
|
|
}; |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
elseif($name === 'alter'){ |
96
|
|
|
|
97
|
|
|
return new class($this->DBDriver, $this->dialect) extends StatementAbstract implements AlterInterface{ |
98
|
|
|
|
99
|
|
|
}; |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
elseif($name === 'drop'){ |
103
|
|
|
|
104
|
|
|
return new class($this->DBDriver, $this->dialect) extends StatementAbstract implements DropInterface{ |
105
|
|
|
|
106
|
|
|
}; |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return null; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: