1 | <?php |
||
35 | abstract class StatementAbstract extends ObjectAbstract implements StatementInterface |
||
36 | { |
||
37 | use DriverAwareTrait; |
||
38 | |||
39 | /** |
||
40 | * prepared statement |
||
41 | * |
||
42 | * @var mixed |
||
43 | * @access protected |
||
44 | */ |
||
45 | protected $prepared; |
||
46 | |||
47 | /** |
||
48 | * Result prototype |
||
49 | * |
||
50 | * @var ResultInterface |
||
51 | * @access protected |
||
52 | */ |
||
53 | protected $result_prototype; |
||
54 | |||
55 | /** |
||
56 | * @var ResultInterface |
||
57 | * @access protected |
||
58 | */ |
||
59 | protected $result; |
||
60 | |||
61 | /** |
||
62 | * Desctructor |
||
63 | * |
||
64 | * @access public |
||
65 | */ |
||
66 | public function __destruct() |
||
70 | |||
71 | /** |
||
72 | * {@inheritDoc} |
||
73 | */ |
||
74 | public function prepare(/*# string */ $sql)/*# : bool */ |
||
75 | { |
||
76 | // close previous prepared sql if any |
||
77 | $this->close(); |
||
78 | |||
79 | // prepare sql |
||
80 | try { |
||
81 | $res = $this->realPrepare($this->getDriver()->getLink(), $sql); |
||
82 | if (false !== $res) { |
||
83 | $this->prepared = $res; |
||
84 | $this->getDriver()->getProfiler()->setSql($sql); |
||
85 | return true; |
||
86 | } |
||
87 | } catch (\Exception $e) { |
||
88 | throw new RuntimeException($e->getMessage(), (int) $e->getCode(), $e); |
||
89 | } |
||
90 | throw new RuntimeException( |
||
91 | Message::get(Message::DB_STMT_PREPARE_FAIL, $sql), |
||
92 | Message::DB_STMT_PREPARE_FAIL |
||
93 | ); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * {@inheritDoc} |
||
98 | */ |
||
99 | public function execute(array $parameters = [])/*# : bool */ |
||
100 | { |
||
101 | $this->checkPreparation(); |
||
102 | $this->getDriver()->getProfiler()->setParameters($parameters)->startWatch(); |
||
103 | |||
104 | try { |
||
105 | if ($this->realExecute($parameters)) { |
||
106 | $result = clone $this->result_prototype; |
||
107 | $this->result = $result($this->prepared); |
||
108 | $this->getDriver()->getProfiler()->stopWatch(); |
||
109 | return true; |
||
110 | } |
||
111 | } catch (\Exception $e) { |
||
112 | throw new RuntimeException($e->getMessage(), (int) $e->getCode(), $e); |
||
113 | } |
||
114 | throw new RuntimeException(Message::get( |
||
115 | Message::DB_STMT_EXECUTE_FAIL, |
||
116 | $this->getDriver()->getProfiler()->getSql()), |
||
117 | Message::DB_STMT_EXECUTE_FAIL |
||
118 | ); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * {@inheritDoc} |
||
123 | */ |
||
124 | public function getResult()/*# : ResultInterface */ |
||
134 | |||
135 | /** |
||
136 | * {@inheritDoc} |
||
137 | */ |
||
138 | public function close() |
||
149 | |||
150 | /** |
||
151 | * Throw exception if not prepared |
||
152 | * |
||
153 | * @throws RuntimeException |
||
154 | * @access protected |
||
155 | */ |
||
156 | protected function checkPreparation() |
||
169 | |||
170 | /** |
||
171 | * Close resultset if any |
||
172 | * |
||
173 | * @access protected |
||
174 | */ |
||
175 | protected function closeResult() |
||
182 | |||
183 | /** |
||
184 | * Driver specific prepare statement |
||
185 | * |
||
186 | * @param mixed $link db link resource |
||
187 | * @param string $sql |
||
188 | * @return mixed |
||
189 | * @throws RuntimeException |
||
190 | * @access protected |
||
191 | */ |
||
192 | abstract protected function realPrepare($link, /*# string */ $sql); |
||
193 | |||
194 | /** |
||
195 | * Driver specific statement execution |
||
196 | * |
||
197 | * @param array $parameters |
||
198 | * @return bool |
||
199 | * @throws RuntimeException |
||
200 | * @access protected |
||
201 | */ |
||
202 | abstract protected function realExecute(array $parameters)/*# : bool */; |
||
203 | |||
204 | /** |
||
205 | * Close statement |
||
206 | * |
||
207 | * @param mixed prepared low-level statement |
||
208 | * @access protected |
||
209 | */ |
||
210 | abstract protected function realClose($stmt); |
||
211 | } |
||
212 |