Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Optimize some constructs

  • Loading branch information...
commit 374659f463a3d12de325844fee7ad4465a3e1e66 1 parent 375e31a
Olivier Poitrey authored

Showing 1 changed file with 19 additions and 11 deletions. Show diff stats Hide diff stats

  1. +19 11 MyDBD/PreparedStatement.php
30 MyDBD/PreparedStatement.php
@@ -111,16 +111,16 @@ public function prepare()
111 111 {
112 112 $types = '';
113 113
114   - foreach ($args as $type)
  114 + for ($i = 0, $total = count($args); $i < $total; $i++)
115 115 {
116   - switch($type)
  116 + switch($args[$i])
117 117 {
118 118 case 'string': $types .= 's'; break;
119 119 case 'integer': $types .= 'i'; break;
120 120 case 'double': $types .= 'd'; break;
121 121 case 'blob': $types .= 'b'; break;
122 122 default:
123   - throw new InvalidArgumentException('Invalid type: ' . $type);
  123 + throw new InvalidArgumentException('Invalid type: ' . $args[$i]);
124 124 }
125 125 }
126 126
@@ -165,12 +165,15 @@ public function execute()
165 165 {
166 166 $params = null;
167 167
168   - if (is_null($this->preparedQuery))
  168 + if (null === $this->preparedQuery)
169 169 {
170 170 throw new SQLNotPreparedStatementException('Cannot execute a not prepared statement.');
171 171 }
172 172
173   - if ($this->options['query_log']) $start = microtime(true);
  173 + if ($queryLog = $this->options['query_log'])
  174 + {
  175 + $start = microtime(true);
  176 + }
174 177
175 178 if ($this->stmt->param_count > 0)
176 179 {
@@ -189,14 +192,17 @@ public function execute()
189 192 $this->stmt->store_result();
190 193 $this->handleErrors($this->preparedQuery, $params);
191 194
192   - if ($this->options['query_log']) MyDBD_Logger::log('execute', $this->preparedQuery, (isset($params) ? $params : null), microtime(true) - $start);
  195 + if ($queryLog)
  196 + {
  197 + MyDBD_Logger::log('execute', $this->preparedQuery, (isset($params) ? $params : null), microtime(true) - $start);
  198 + }
193 199
194 200 if ($metadata = $this->stmt->result_metadata())
195 201 {
196 202 // integrity problem due to mysqli design limitation
197 203 // you can't store several result set on several executed query
198 204 // from the same prepared statment
199   - if (!isset($this->resultSet))
  205 + if (null === $this->resultSet)
200 206 {
201 207 $this->resultSet = new MyDBD_StatementResultSet($this->stmt, $metadata, $this->options);
202 208 }
@@ -245,13 +251,15 @@ protected function bindParams(array $params)
245 251 );
246 252 }
247 253
248   - if (is_null($this->boundParams))
  254 + if (null === $this->boundParams)
249 255 {
250 256 // try some auto-detection
251 257 $types = '';
252 258
253   - foreach ($params as $param)
  259 + for ($i = 0, $total = $this->stmt->param_count; $i < $total; $i++)
254 260 {
  261 + $param = $params[$i];
  262 +
255 263 if (is_double($param))
256 264 {
257 265 $types .= 'd';
@@ -269,7 +277,7 @@ protected function bindParams(array $params)
269 277 $this->bindVariables($types);
270 278 }
271 279
272   - for ($i = 0; $i < count($params); $i++)
  280 + for ($i = 0, $total = count($params); $i < $total; $i++)
273 281 {
274 282 $this->bindedData[$i] = $params[$i];
275 283 }
@@ -293,7 +301,7 @@ protected function bindVariables($types)
293 301
294 302 $this->boundParams = array_fill(0, $this->stmt->param_count, null);
295 303 $args = array($this->stmt, $types);
296   - for ($i = 0; $i < count($this->boundParams); $i++)
  304 + for (($total = count($this->boundParams)) && $i = 0; $i < $total; $i++)
297 305 {
298 306 $args[$i + 2] = &$this->bindedData[$i];
299 307 }

0 comments on commit 374659f

Please sign in to comment.
Something went wrong with that request. Please try again.