TYPO3 CMS  TYPO3_6-2
AdodbPreparedStatement.php
Go to the documentation of this file.
1 <?php
3 
18 
28 
33 
37  protected $query;
38 
42  protected $queryComponents;
43 
47  protected $parameters;
48 
52  protected $recordSet;
53 
61  public function __construct($query, array $queryComponents, \TYPO3\CMS\Dbal\Database\DatabaseConnection $databaseConnection) {
62  $this->databaseConnection = $databaseConnection;
63  $this->query = $query;
64  $this->queryComponents = $queryComponents;
65  $this->parameters = array();
66  }
67 
73  public function prepare() {
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  return TRUE;
86  }
87 
97  public function bind_param($types, $var1, $_ = NULL) {
98  $numberOfVariables = strlen($types);
99  if (func_num_args() !== $numberOfVariables + 1) {
100  return FALSE;
101  }
102 
103  $this->parameters = array(
104  array(
105  'type' => $types{0},
106  'value' => $var1
107  ),
108  );
109  for ($i = 1; $i < $numberOfVariables; $i++) {
110  $this->parameters[] = array(
111  'type' => $types{$i},
112  'value' => func_get_arg($i + 1),
113  );
114  }
115 
116  return TRUE;
117  }
118 
124  public function reset() {
125  return TRUE;
126  }
127 
133  public function execute() {
134  $queryParts = $this->queryComponents['queryParts'];
135  $numberOfParameters = count($this->parameters);
136  for ($i = 0; $i < $numberOfParameters; $i++) {
137  $value = $this->parameters[$i]['value'];
138  switch ($this->parameters[$i]['type']) {
139  case 's':
140  if ($value !== NULL) {
141  $value = $this->databaseConnection->fullQuoteStr($value, $this->queryComponents['ORIG_tableName']);
142  }
143  break;
144  case 'i':
145  $value = (int)$value;
146  break;
147  default:
148  // Same error as in \TYPO3\CMS\Core\Database\PreparedStatement::execute()
149  throw new \InvalidArgumentException(sprintf('Unknown type %s used for parameter %s.', $this->parameters[$i]['type'], $i + 1), 1281859196);
150  }
151 
152  $queryParts[$i * 2 + 1] = $value;
153  }
154 
155  // Standard query from now on
156  $query = implode('', $queryParts);
157 
158  $limit = $this->queryComponents['LIMIT'];
159  if ($this->databaseConnection->runningADOdbDriver('postgres')) {
160  // Possibly rewrite the LIMIT to be PostgreSQL-compatible
161  $splitLimit = GeneralUtility::intExplode(',', $limit);
162  // Splitting the limit values:
163  if ($splitLimit[1]) {
164  // If there are two parameters, do mapping differently than otherwise:
165  $numRows = $splitLimit[1];
166  $offset = $splitLimit[0];
167  $limit = $numRows . ' OFFSET ' . $offset;
168  }
169  }
170  if ($limit !== '') {
171  $splitLimit = GeneralUtility::intExplode(',', $limit);
172  // Splitting the limit values:
173  if ($splitLimit[1]) {
174  // If there are two parameters, do mapping differently than otherwise:
175  $numRows = $splitLimit[1];
176  $offset = $splitLimit[0];
177  } else {
178  $numRows = $splitLimit[0];
179  $offset = 0;
180  }
181  $this->recordSet = $this->databaseConnection->handlerInstance[$this->databaseConnection->lastHandlerKey]->SelectLimit($query, $numRows, $offset);
182  $this->databaseConnection->lastQuery = $this->recordSet->sql;
183  } else {
184  $this->databaseConnection->lastQuery = $query;
185  $this->recordSet = $this->databaseConnection->handlerInstance[$this->databaseConnection->lastHandlerKey]->_Execute($this->databaseConnection->lastQuery);
186  }
187 
188  if ($this->recordSet !== FALSE) {
189  $success = TRUE;
190  $this->recordSet->TYPO3_DBAL_handlerType = 'adodb';
191  // Setting handler type in result object (for later recognition!)
192  //$this->recordSet->TYPO3_DBAL_tableList = $queryComponents['ORIG_tableName'];
193  } else {
194  $success = FALSE;
195  }
196 
197  return $success;
198  }
199 
205  public function fetch_fields() {
206  return $this->recordSet !== FALSE ? $this->recordSet->_fieldobjects : array();
207  }
208 
214  public function fetch() {
215  $row = $this->databaseConnection->sql_fetch_assoc($this->recordSet);
216  return $row;
217  }
218 
225  public function data_seek($offset) {
226  return $this->databaseConnection->sql_data_seek($this->recordSet, $offset);
227  }
228 
234  public function close() {
235  return $this->databaseConnection->sql_free_result($this->recordSet);
236  }
237 
245  public function __get($name) {
246  switch ($name) {
247  case 'errno':
248  $output = $this->databaseConnection->sql_errno();
249  break;
250  case 'error':
251  $output = $this->databaseConnection->sql_error();
252  break;
253  case 'num_rows':
254  $output = $this->databaseConnection->sql_num_rows($this->recordSet);
255  break;
256  default:
257  throw new \RuntimeException('Cannot access property ' . $name, 1394631927);
258  }
259  return $output;
260  }
261 
262 }
static intExplode($delimiter, $string, $removeEmptyValues=FALSE, $limit=0)
__construct($query, array $queryComponents, \TYPO3\CMS\Dbal\Database\DatabaseConnection $databaseConnection)