TYPO3 CMS  TYPO3_7-6
AdodbPreparedStatement.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 
26 {
31 
35  protected $query;
36 
40  protected $queryComponents;
41 
45  protected $parameters;
46 
50  protected $recordSet;
51 
59  public function __construct($query, array $queryComponents, \TYPO3\CMS\Dbal\Database\DatabaseConnection $databaseConnection)
60  {
61  $this->databaseConnection = $databaseConnection;
62  $this->query = $query;
63  $this->queryComponents = $queryComponents;
64  $this->parameters = [];
65  }
66 
72  public function prepare()
73  {
74  // @todo actually prepare the query with ADOdb, if supported by the underlying DBMS
75  // see: http://phplens.com/lens/adodb/docs-adodb.htm#prepare
76  return true;
77  }
78 
84  public function store_result()
85  {
86  return true;
87  }
88 
98  public function bind_param($types, $var1, $_ = null)
99  {
100  $numberOfVariables = strlen($types);
101  if (func_num_args() !== $numberOfVariables + 1) {
102  return false;
103  }
104 
105  $this->parameters = [
106  [
107  'type' => $types{0},
108  'value' => $var1
109  ],
110  ];
111  for ($i = 1; $i < $numberOfVariables; $i++) {
112  $this->parameters[] = [
113  'type' => $types{$i},
114  'value' => func_get_arg($i + 1),
115  ];
116  }
117 
118  return true;
119  }
120 
126  public function reset()
127  {
128  return true;
129  }
130 
136  public function execute()
137  {
138  $queryParts = $this->queryComponents['queryParts'];
139  $numberOfParameters = count($this->parameters);
140  for ($i = 0; $i < $numberOfParameters; $i++) {
141  $value = $this->parameters[$i]['value'];
142  switch ($this->parameters[$i]['type']) {
143  case 's':
144  if ($value !== null) {
145  $value = $this->databaseConnection->fullQuoteStr($value, $this->queryComponents['ORIG_tableName']);
146  }
147  break;
148  case 'i':
149  $value = (int)$value;
150  break;
151  default:
152  // Same error as in \TYPO3\CMS\Core\Database\PreparedStatement::execute()
153  throw new \InvalidArgumentException(sprintf('Unknown type %s used for parameter %s.', $this->parameters[$i]['type'], $i + 1), 1281859196);
154  }
155 
156  $queryParts[$i * 2 + 1] = $value;
157  }
158 
159  // Standard query from now on
160  $query = implode('', $queryParts);
161 
162  $limit = $this->queryComponents['LIMIT'];
163  if ($this->databaseConnection->runningADOdbDriver('postgres')) {
164  // Possibly rewrite the LIMIT to be PostgreSQL-compatible
165  $splitLimit = GeneralUtility::intExplode(',', $limit);
166  // Splitting the limit values:
167  if ($splitLimit[1]) {
168  // If there are two parameters, do mapping differently than otherwise:
169  $numRows = $splitLimit[1];
170  $offset = $splitLimit[0];
171  $limit = $numRows . ' OFFSET ' . $offset;
172  }
173  }
174  if ($limit !== '') {
175  $splitLimit = GeneralUtility::intExplode(',', $limit);
176  // Splitting the limit values:
177  if ($splitLimit[1]) {
178  // If there are two parameters, do mapping differently than otherwise:
179  $numRows = $splitLimit[1];
180  $offset = $splitLimit[0];
181  } else {
182  $numRows = $splitLimit[0];
183  $offset = 0;
184  }
185  $this->recordSet = $this->databaseConnection->handlerInstance[$this->databaseConnection->lastHandlerKey]->SelectLimit($query, $numRows, $offset);
186  $this->databaseConnection->lastQuery = $this->recordSet->sql;
187  } else {
188  $this->databaseConnection->lastQuery = $query;
189  $this->recordSet = $this->databaseConnection->handlerInstance[$this->databaseConnection->lastHandlerKey]->_Execute($this->databaseConnection->lastQuery);
190  }
191 
192  if ($this->recordSet !== false) {
193  $success = true;
194  $this->recordSet->TYPO3_DBAL_handlerType = 'adodb';
195  // Setting handler type in result object (for later recognition!)
196  //$this->recordSet->TYPO3_DBAL_tableList = $queryComponents['ORIG_tableName'];
197  } else {
198  $success = false;
199  }
200 
201  return $success;
202  }
203 
209  public function fetch_fields()
210  {
211  return $this->recordSet !== false ? $this->recordSet->_fieldobjects : [];
212  }
213 
219  public function fetch()
220  {
221  $row = $this->databaseConnection->sql_fetch_assoc($this->recordSet);
222  return $row;
223  }
224 
231  public function data_seek($offset)
232  {
233  return $this->databaseConnection->sql_data_seek($this->recordSet, $offset);
234  }
235 
241  public function close()
242  {
243  return $this->databaseConnection->sql_free_result($this->recordSet);
244  }
245 
253  public function __get($name)
254  {
255  switch ($name) {
256  case 'errno':
257  $output = $this->databaseConnection->sql_errno();
258  break;
259  case 'error':
260  $output = $this->databaseConnection->sql_error();
261  break;
262  case 'num_rows':
263  $output = $this->databaseConnection->sql_num_rows($this->recordSet);
264  break;
265  default:
266  throw new \RuntimeException('Cannot access property ' . $name, 1394631927);
267  }
268  return $output;
269  }
270 }
static intExplode($delimiter, $string, $removeEmptyValues=false, $limit=0)
__construct($query, array $queryComponents, \TYPO3\CMS\Dbal\Database\DatabaseConnection $databaseConnection)