1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Query |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Query\Traits; |
16
|
|
|
|
17
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
18
|
|
|
use Phossa2\Query\Interfaces\BuilderInterface; |
19
|
|
|
use Phossa2\Query\Interfaces\StatementInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* StatementAbstract |
23
|
|
|
* |
24
|
|
|
* @package Phossa2\Query |
25
|
|
|
* @author Hong Zhang <[email protected]> |
26
|
|
|
* @see ObjectAbstract |
27
|
|
|
* @see StatementInterface |
28
|
|
|
* @version 2.0.0 |
29
|
|
|
* @since 2.0.0 added |
30
|
|
|
*/ |
31
|
|
|
abstract class StatementAbstract extends ObjectAbstract implements StatementInterface |
32
|
|
|
{ |
33
|
|
|
use SettingsTrait, BuilderAwareTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* value bindings |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
* @access protected |
40
|
|
|
*/ |
41
|
|
|
protected $bindings; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Statement type, 'SELECT' etc. |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
* @access protected |
48
|
|
|
*/ |
49
|
|
|
protected $type = ''; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* clause configs,[name => prefix] |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
* @access protected |
56
|
|
|
*/ |
57
|
|
|
protected $configs = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Constructor |
61
|
|
|
* |
62
|
|
|
* @param BuilderInterface $builder |
63
|
|
|
* @access public |
64
|
|
|
*/ |
65
|
|
|
public function __construct(BuilderInterface $builder) |
66
|
|
|
{ |
67
|
|
|
$this->setBuilder($builder); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritDoc} |
72
|
|
|
*/ |
73
|
|
|
public function getStatement(array $settings = [])/*# : string */ |
74
|
|
|
{ |
75
|
|
|
// combine settings |
76
|
|
|
$settings = $this->combineSettings($settings); |
77
|
|
|
|
78
|
|
|
// build current sql |
79
|
|
|
$sql = $this->buildSql($settings); |
80
|
|
|
|
81
|
|
|
// replace with ?, :name or real values |
82
|
|
|
return $this->bindValues($sql, $settings); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritDoc} |
87
|
|
|
*/ |
88
|
|
|
public function getBindings()/*# : array */ |
89
|
|
|
{ |
90
|
|
|
return $this->bindings; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritDoc} |
95
|
|
|
*/ |
96
|
|
|
public function __toString()/*# : string */ |
97
|
|
|
{ |
98
|
|
|
return $this->getStatement([ |
99
|
|
|
'positionedParam' => false, |
100
|
|
|
'namedParam' => false, |
101
|
|
|
]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Combine builder/statement and provided settings |
106
|
|
|
* |
107
|
|
|
* @param array $settings statement specific settings |
108
|
|
|
* @return array |
109
|
|
|
* @access protected |
110
|
|
|
*/ |
111
|
|
|
protected function combineSettings(array $settings)/*# : array */ |
112
|
|
|
{ |
113
|
|
|
return array_replace( |
114
|
|
|
$this->getBuilder()->getSettings(), // builder settings |
115
|
|
|
$this->getBuilder()->getDialect()->dialectSettings(), // dialect |
116
|
|
|
$settings, // user settings |
117
|
|
|
$this->getSettings() // statement settings |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Build SQL statement clauses |
123
|
|
|
* |
124
|
|
|
* @param array $settings |
125
|
|
|
* @param string |
126
|
|
|
* @access protected |
127
|
|
|
*/ |
128
|
|
|
protected function buildSql(array $settings)/*# : string */ |
129
|
|
|
{ |
130
|
|
|
$result = $this->type; |
131
|
|
|
$settings['join'] = $settings['seperator'] . $settings['indent']; |
132
|
|
|
foreach ($this->configs as $clause => $prefix) { |
133
|
|
|
$method = 'build' . ucfirst(strtolower($clause)); |
134
|
|
|
$result .= $this->{$method}($prefix, $settings); |
135
|
|
|
} |
136
|
|
|
return $result; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Replace placeholders in the sql with '?', ':named' or real value |
141
|
|
|
* |
142
|
|
|
* @param string $sql |
143
|
|
|
* @param array $settings |
144
|
|
|
* @return string |
145
|
|
|
* @access protected |
146
|
|
|
*/ |
147
|
|
|
protected function bindValues(/*# string */ $sql, array $settings)/*# : string */ |
148
|
|
|
{ |
149
|
|
|
// init bindings |
150
|
|
|
$this->bindings = []; |
151
|
|
|
|
152
|
|
|
// bind values |
153
|
|
|
return $this->getBuilder()->getParameter() |
154
|
|
|
->bindValues($sql, $this->bindings, $settings); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|