|
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\CreateInterface; |
|
17
|
|
|
use chillerlan\Database\Query\StatementAbstract; |
|
18
|
|
|
use chillerlan\Database\Query\CreateDatabaseInterface; |
|
19
|
|
|
use chillerlan\Database\Query\CreateTableInterface; |
|
20
|
|
|
/** |
|
21
|
|
|
* @property \chillerlan\Database\Query\SelectInterface $select |
|
22
|
|
|
* @property \chillerlan\Database\Query\InsertInterface $insert |
|
23
|
|
|
* @property \chillerlan\Database\Query\UpdateInterface $update |
|
24
|
|
|
* @property \chillerlan\Database\Query\DeleteInterface $delete |
|
25
|
|
|
* @property \chillerlan\Database\Query\CreateInterface $create |
|
26
|
|
|
*/ |
|
27
|
|
|
class DBQuery{ |
|
28
|
|
|
|
|
29
|
|
|
const DIALECTS = [ |
|
30
|
|
|
'mysql' => 'MySQL', |
|
31
|
|
|
'pgsql' => 'Postgres', |
|
32
|
|
|
'sqlite' => 'SQLite', |
|
33
|
|
|
'firebird' => 'Firebird', |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var \chillerlan\Database\Drivers\DBDriverInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $DBDriver; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $dialect; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* DBQuery constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param \chillerlan\Database\Drivers\DBDriverInterface $DBDriver |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct(DBDriverInterface $DBDriver){ |
|
52
|
|
|
$this->DBDriver = $DBDriver; |
|
53
|
|
|
|
|
54
|
|
|
$this->dialect = __NAMESPACE__.'\\Query\\Dialects\\'.self::DIALECTS[$this->DBDriver->dialect]; |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $name |
|
59
|
|
|
* |
|
60
|
|
|
* @return \chillerlan\Database\Query\StatementInterface|null |
|
|
|
|
|
|
61
|
|
|
*/ |
|
62
|
|
|
public function __get(string $name){ |
|
63
|
|
|
$name = strtolower($name); |
|
64
|
|
|
|
|
65
|
|
|
if(in_array($name, ['select', 'insert', 'update', 'delete'])){ |
|
66
|
|
|
$class = $this->dialect.'\\'.ucfirst($name); |
|
67
|
|
|
return new $class($this->DBDriver, $this->dialect); |
|
68
|
|
|
} |
|
69
|
|
|
elseif($name === 'create'){ |
|
70
|
|
|
|
|
71
|
|
|
return new class($this->DBDriver, $this->dialect) extends StatementAbstract implements CreateInterface{ |
|
72
|
|
|
|
|
73
|
|
|
public function database(string $dbname = null):CreateDatabaseInterface{ |
|
74
|
|
|
return $this->getStatementClass('CreateDatabase')->name($dbname); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function table(string $tablename = null):CreateTableInterface{ |
|
78
|
|
|
return $this->getStatementClass('CreateTable')->name($tablename); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function index():CreateInterface{ |
|
82
|
|
|
// TODO: Implement index() method. |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function view():CreateInterface{ |
|
86
|
|
|
// TODO: Implement view() method. |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function trigger():CreateInterface{ |
|
90
|
|
|
// TODO: Implement trigger() method. |
|
91
|
|
|
} |
|
92
|
|
|
}; |
|
93
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return null; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
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: