1 | <?php |
||
32 | abstract class StatementAbstract extends ObjectAbstract implements StatementInterface |
||
33 | { |
||
34 | use SettingsAwareTrait, BuilderAwareTrait, ExtraClauseTrait, PreviousTrait; |
||
35 | |||
36 | /** |
||
37 | * value bindings |
||
38 | * |
||
39 | * @var array |
||
40 | * @access protected |
||
41 | */ |
||
42 | protected $bindings; |
||
43 | |||
44 | /** |
||
45 | * clause configs,[name => prefix] |
||
46 | * |
||
47 | * @var array |
||
48 | * @access protected |
||
49 | */ |
||
50 | protected $configs = []; |
||
51 | |||
52 | /** |
||
53 | * Constructor |
||
54 | * |
||
55 | * @param BuilderInterface $builder |
||
56 | * @access public |
||
57 | */ |
||
58 | public function __construct(BuilderInterface $builder) |
||
59 | { |
||
60 | $this->setBuilder($builder); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * {@inheritDoc} |
||
65 | */ |
||
66 | public function getStatement(array $settings = [])/*# : string */ |
||
67 | { |
||
68 | // combine settings |
||
69 | $settings = $this->combineSettings($settings); |
||
70 | |||
71 | // build current sql |
||
72 | $sql = $this->buildSql($settings); |
||
73 | |||
74 | // replace with ?, :name or real values |
||
75 | return $this->bindValues($sql, $settings); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Return the sql string (with parameter replaced) |
||
80 | * |
||
81 | * @param array $settings extra settings if any |
||
82 | * @return string |
||
83 | * @access public |
||
84 | * @api |
||
85 | */ |
||
86 | public function getSql(array $settings = [])/*# : string */ |
||
87 | { |
||
88 | $settings['positionedParam'] = false; |
||
89 | $settings['namedParam'] = false; |
||
90 | return $this->getStatement($settings); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * {@inheritDoc} |
||
95 | */ |
||
96 | public function getBindings()/*# : array */ |
||
100 | |||
101 | /** |
||
102 | * {@inheritDoc} |
||
103 | */ |
||
104 | public function __toString()/*# : string */ |
||
108 | |||
109 | /** |
||
110 | * Combine builder/statement and provided settings |
||
111 | * |
||
112 | * @param array $settings statement specific settings |
||
113 | * @return array |
||
114 | * @access protected |
||
115 | */ |
||
116 | protected function combineSettings(array $settings)/*# : array */ |
||
125 | |||
126 | /** |
||
127 | * Build SQL statement clauses |
||
128 | * |
||
129 | * @param array $settings |
||
130 | * @param string |
||
131 | * @access protected |
||
132 | */ |
||
133 | protected function buildSql(array $settings)/*# : string */ |
||
153 | |||
154 | /** |
||
155 | * Replace placeholders in the sql with '?', ':named' or real value |
||
156 | * |
||
157 | * @param string $sql |
||
158 | * @param array $settings |
||
159 | * @return string |
||
160 | * @access protected |
||
161 | */ |
||
162 | protected function bindValues(/*# string */ $sql, array $settings)/*# : string */ |
||
171 | |||
172 | /** |
||
173 | * Clause configs ['name' => 'prefix'] |
||
174 | * |
||
175 | * @return array |
||
176 | * @access protected |
||
177 | */ |
||
178 | abstract protected function getConfigs()/*# : array */; |
||
179 | |||
180 | /** |
||
181 | * Get current statement type. e.g. 'SELECT' etc. |
||
182 | * |
||
183 | * @return string |
||
184 | * @access protected |
||
185 | */ |
||
186 | abstract protected function getType()/*# : string */; |
||
187 | } |
||
188 |