|
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 void |
|
43
|
|
|
*/ |
|
44
|
|
|
public function configure($what) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->loadConfiguration($what); |
|
47
|
|
|
parent::setOptions($this->config); |
|
|
|
|
|
|
48
|
|
|
$this->setDefaultsFromConfiguration(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Update builder settings from active configuration. |
|
55
|
|
|
* |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
|
|
private function setDefaultsFromConfiguration() |
|
59
|
|
|
{ |
|
60
|
|
|
if ($this->options['dsn']) { |
|
61
|
|
|
$dsn = explode(':', $this->options['dsn']); |
|
62
|
|
|
$this->setSQLDialect($dsn[0]); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->setTablePrefix($this->options['table_prefix']); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Execute a SQL-query and ignore the resultset. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $query the SQL statement |
|
74
|
|
|
* @param array $params the params array |
|
75
|
|
|
* |
|
76
|
|
|
* @return boolean returns TRUE on success or FALSE on failure. |
|
77
|
|
|
*/ |
|
78
|
|
|
public function execute($query = null, $params = []) |
|
79
|
|
|
{ |
|
80
|
|
|
if (is_array($query)) { |
|
81
|
|
|
$params = $query; |
|
82
|
|
|
$query = null; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if (!$query) { |
|
|
|
|
|
|
86
|
|
|
$query = $this->getSQL(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
var_dump($query); |
|
|
|
|
|
|
90
|
|
|
var_dump($params); |
|
91
|
|
|
|
|
92
|
|
|
return parent::execute($query, $params); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
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 theSoncalls the wrong method in the parent class.