1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Database; |
4
|
|
|
|
5
|
|
|
use \Anax\Common\ConfigureInterface; |
6
|
|
|
use \Anax\Common\ConfigureTrait; |
7
|
|
|
use \Anax\Database\Exception\BuildException; |
8
|
|
|
use \Anax\Database\QueryBuilderTrait; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Build SQL queries by method calling. |
12
|
|
|
*/ |
13
|
|
|
class DatabaseQueryBuilder extends Database implements ConfigureInterface |
14
|
|
|
{ |
15
|
|
|
use QueryBuilderTrait, |
16
|
|
|
ConfigureTrait { |
17
|
|
|
configure as protected loadConfiguration; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constructor creating a PDO object connecting to a choosen database. |
24
|
|
|
* |
25
|
|
|
* @param array $options containing details for connecting to the database. |
26
|
|
|
*/ |
27
|
|
|
public function __construct($options = []) |
28
|
|
|
{ |
29
|
|
|
parent::__construct($options); |
30
|
|
|
$this->setDefaultsFromConfiguration(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Load and apply configurations. |
37
|
|
|
* |
38
|
|
|
* @param array|string $what is an array with key/value config options |
39
|
|
|
* or a file to be included which returns such |
40
|
|
|
* an array. |
41
|
|
|
* |
42
|
|
|
* @return self |
43
|
|
|
*/ |
44
|
1 |
|
public function configure($what) |
45
|
|
|
{ |
46
|
1 |
|
$this->loadConfiguration($what); |
47
|
1 |
|
parent::setOptions($this->config); |
|
|
|
|
48
|
1 |
|
$this->setDefaultsFromConfiguration(); |
49
|
1 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Update builder settings from active configuration. |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
1 |
|
private function setDefaultsFromConfiguration() |
60
|
|
|
{ |
61
|
1 |
|
if ($this->options['dsn']) { |
62
|
|
|
$dsn = explode(':', $this->options['dsn']); |
63
|
|
|
$this->setSQLDialect($dsn[0]); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
$this->setTablePrefix($this->options['table_prefix']); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Execute a SQL-query and ignore the resultset. |
73
|
|
|
* |
74
|
|
|
* @param string $query the SQL statement |
75
|
|
|
* @param array $params the params array |
76
|
|
|
* |
77
|
|
|
* @return boolean returns TRUE on success or FALSE on failure. |
78
|
|
|
*/ |
79
|
3 |
|
public function execute($query = null, $params = []) |
80
|
|
|
{ |
81
|
3 |
|
if (is_array($query)) { |
82
|
1 |
|
$params = $query; |
83
|
1 |
|
$query = null; |
84
|
1 |
|
} |
85
|
|
|
|
86
|
3 |
|
if (!$query) { |
|
|
|
|
87
|
3 |
|
$query = $this->getSQL(); |
88
|
3 |
|
} |
89
|
|
|
|
90
|
3 |
|
return parent::execute($query, $params); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.