2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Wrappers for Drizzle extension classes
6 * Drizzle extension exposes libdrizzle functions and requires user to have it in
7 * mind while using them.
8 * This wrapper is not complete and hides a lot of original functionality,
9 * but allows for easy usage of the drizzle PHP extension.
11 * @package PhpMyAdmin-DBI
14 if (! defined('PHPMYADMIN')) {
19 * Workaround for crashing module
23 * @todo drizzle module segfaults while freeing resources, often.
24 * This allows at least for some development
26 function PMA_drizzleShutdownFlush()
30 register_shutdown_function('PMA_drizzleShutdownFlush');
33 * Wrapper for Drizzle class
35 * @package PhpMyAdmin-DBI
38 class PMA_Drizzle
extends Drizzle
41 * Fetch mode: result rows contain column names
43 const FETCH_ASSOC
= 1;
45 * Fetch mode: result rows contain only numeric indices
49 * Fetch mode: result rows have both column names and numeric indices
54 * Result buffering: entire result set is buffered upon execution
56 const BUFFER_RESULT
= 1;
58 * Result buffering: buffering occurs only on row level
65 public function __construct()
67 parent
::__construct();
71 * Creates a new database conection using TCP
73 * @param string $host Drizzle host
74 * @param integer $port Drizzle port
75 * @param string $user username
76 * @param string $password password
77 * @param string $db database name
78 * @param integer $options connection options
80 * @return PMA_DrizzleCon
82 public function addTcp($host, $port, $user, $password, $db, $options)
84 $dcon = parent
::addTcp($host, $port, $user, $password, $db, $options);
85 return $dcon instanceof DrizzleCon
86 ?
new PMA_DrizzleCon($dcon)
91 * Creates a new connection using unix domain socket
93 * @param string $uds socket
94 * @param string $user username
95 * @param string $password password
96 * @param string $db database name
97 * @param integer $options connection options
99 * @return PMA_DrizzleCon
101 public function addUds($uds, $user, $password, $db, $options)
103 $dcon = parent
::addUds($uds, $user, $password, $db, $options);
104 return $dcon instanceof DrizzleCon
105 ?
new PMA_DrizzleCon($dcon)
111 * Wrapper around DrizzleCon class
113 * Its main task is to wrap results with PMA_DrizzleResult class
115 * @package PhpMyAdmin-DBI
116 * @subpackage Drizzle
121 * Instance of DrizzleCon class
127 * Result of the most recent query
128 * @var PMA_DrizzleResult
130 private $_lastResult;
135 * @param DrizzleCon $dcon connection handle
139 public function __construct(DrizzleCon
$dcon)
141 $this->_dcon
= $dcon;
145 * Executes given query. Opens database connection if not already done.
147 * @param string $query query to execute
148 * @param int $bufferMode PMA_Drizzle::BUFFER_RESULT,PMA_Drizzle::BUFFER_ROW
149 * @param int $fetchMode PMA_Drizzle::FETCH_ASSOC, PMA_Drizzle::FETCH_NUM
150 * or PMA_Drizzle::FETCH_BOTH
152 * @return PMA_DrizzleResult
154 public function query($query, $bufferMode = PMA_Drizzle
::BUFFER_RESULT
,
155 $fetchMode = PMA_Drizzle
::FETCH_ASSOC
157 $result = $this->_dcon
->query($query);
158 if ($result instanceof DrizzleResult
) {
159 $this->_lastResult
= new PMA_DrizzleResult(
160 $result, $bufferMode, $fetchMode
162 return $this->_lastResult
;
168 * Returns the number of rows affected by last query
172 public function affectedRows()
174 return $this->_lastResult
175 ?
$this->_lastResult
->affectedRows()
180 * Pass calls of undefined methods to DrizzleCon object
182 * @param string $method method name
183 * @param mixed $args method parameters
187 public function __call($method, $args)
189 return call_user_func_array(array($this->_dcon
, $method), $args);
193 * Returns original Drizzle connection object
197 public function getConnectionObject()
204 * Wrapper around DrizzleResult.
206 * Allows for reading result rows as an associative array and hides complexity
209 * @package PhpMyAdmin-DBI
210 * @subpackage Drizzle
212 class PMA_DrizzleResult
215 * Instamce of DrizzleResult class
228 private $_bufferMode;
232 * @var DrizzleColumn[]
234 private $_columns = null;
236 * Cached column names
239 private $_columnNames = null;
244 * @param DrizzleResult $dresult result handler
245 * @param int $bufferMode buffering mode
246 * @param int $fetchMode fetching mode
248 public function __construct(DrizzleResult
$dresult, $bufferMode, $fetchMode)
250 $this->_dresult
= $dresult;
251 $this->_bufferMode
= $bufferMode;
252 $this->_fetchMode
= $fetchMode;
254 if ($this->_bufferMode
== PMA_Drizzle
::BUFFER_RESULT
) {
255 $this->_dresult
->buffer();
262 * @param int $fetchMode fetch mode
266 public function setFetchMode($fetchMode)
268 $this->_fetchMode
= $fetchMode;
272 * Reads information about columns contained in current result
273 * set into {@see $_columns} and {@see $_columnNames} arrays
277 private function _readColumns()
279 $this->_columns
= array();
280 $this->_columnNames
= array();
281 if ($this->_bufferMode
== PMA_Drizzle
::BUFFER_RESULT
) {
282 while (($column = $this->_dresult
->columnNext()) !== null) {
283 $this->_columns
[] = $column;
284 $this->_columnNames
[] = $column->name();
287 while (($column = $this->_dresult
->columnRead()) !== null) {
288 $this->_columns
[] = $column;
289 $this->_columnNames
[] = $column->name();
295 * Returns columns in current result
297 * @return DrizzleColumn[]
299 public function getColumns()
301 if (! $this->_columns
) {
302 $this->_readColumns();
304 return $this->_columns
;
308 * Returns number if columns in result
312 public function numColumns()
314 return $this->_dresult
->columnCount();
318 * Transforms result row to conform to current fetch mode
320 * @param mixed &$row row to process
321 * @param int $fetchMode fetch mode
325 private function _transformResultRow(&$row, $fetchMode)
331 switch ($fetchMode) {
332 case PMA_Drizzle
::FETCH_ASSOC
:
333 $row = array_combine($this->_columnNames
, $row);
335 case PMA_Drizzle
::FETCH_BOTH
:
336 $length = count($row);
337 for ($i = 0; $i < $length; $i++
) {
338 $row[$this->_columnNames
[$i]] = $row[$i];
347 * Fetches next for from this result set
349 * @param int $fetchMode fetch mode to use, if not given the default one is used
353 public function fetchRow($fetchMode = null)
355 // read column names on first fetch, only buffered results
356 // allow for reading it later
357 if (! $this->_columns
) {
358 $this->_readColumns();
360 if ($fetchMode === null) {
361 $fetchMode = $this->_fetchMode
;
364 switch ($this->_bufferMode
) {
365 case PMA_Drizzle
::BUFFER_RESULT
:
366 $row = $this->_dresult
->rowNext();
368 case PMA_Drizzle
::BUFFER_ROW
:
369 $row = $this->_dresult
->rowBuffer();
372 $this->_transformResultRow($row, $fetchMode);
377 * Adjusts the result pointer to an arbitrary row in buffered result
379 * @param integer $row_index where to seek
383 public function seek($row_index)
385 if ($this->_bufferMode
!= PMA_Drizzle
::BUFFER_RESULT
) {
387 __("Can't seek in an unbuffered result set"), E_USER_WARNING
391 // rowSeek always returns NULL (drizzle extension v.0.5, API v.7)
392 if ($row_index >= 0 && $row_index < $this->_dresult
->rowCount()) {
393 $this->_dresult
->rowSeek($row_index);
400 * Returns the number of rows in buffered result set
404 public function numRows()
406 if ($this->_bufferMode
!= PMA_Drizzle
::BUFFER_RESULT
) {
408 __("Can't count rows in an unbuffered result set"), E_USER_WARNING
412 return $this->_dresult
->rowCount();
416 * Returns the number of rows affected by query
420 public function affectedRows()
422 return $this->_dresult
->affectedRows();
426 * Frees resources taken by this result
430 public function free()
432 unset($this->_columns
);
433 unset($this->_columnNames
);
434 drizzle_result_free($this->_dresult
);
435 unset($this->_dresult
);