Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / dbi / drizzle-wrappers.lib.php
blob430a78d97f5608587665224169b987e2b8ccc6f9
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
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
12 * @subpackage Drizzle
14 if (! defined('PHPMYADMIN')) {
15 exit;
18 /**
19 * Workaround for crashing module
21 * @return void
23 * @todo drizzle module segfaults while freeing resources, often.
24 * This allows at least for some development
26 function PMA_drizzleShutdownFlush()
28 flush();
30 register_shutdown_function('PMA_drizzleShutdownFlush');
32 /**
33 * Wrapper for Drizzle class
35 * @package PhpMyAdmin-DBI
36 * @subpackage Drizzle
38 class PMA_Drizzle extends Drizzle
40 /**
41 * Fetch mode: result rows contain column names
43 const FETCH_ASSOC = 1;
44 /**
45 * Fetch mode: result rows contain only numeric indices
47 const FETCH_NUM = 2;
48 /**
49 * Fetch mode: result rows have both column names and numeric indices
51 const FETCH_BOTH = 3;
53 /**
54 * Result buffering: entire result set is buffered upon execution
56 const BUFFER_RESULT = 1;
57 /**
58 * Result buffering: buffering occurs only on row level
60 const BUFFER_ROW = 2;
62 /**
63 * Constructor
65 public function __construct()
67 parent::__construct();
70 /**
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)
87 : $dcon;
90 /**
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)
106 : $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
118 class PMA_DrizzleCon
121 * Instance of DrizzleCon class
122 * @var DrizzleCon
124 private $_dcon;
127 * Result of the most recent query
128 * @var PMA_DrizzleResult
130 private $_lastResult;
133 * Constructor
135 * @param DrizzleCon $dcon connection handle
137 * @return void
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;
164 return $result;
168 * Returns the number of rows affected by last query
170 * @return int|false
172 public function affectedRows()
174 return $this->_lastResult
175 ? $this->_lastResult->affectedRows()
176 : false;
180 * Pass calls of undefined methods to DrizzleCon object
182 * @param string $method method name
183 * @param mixed $args method parameters
185 * @return mixed
187 public function __call($method, $args)
189 return call_user_func_array(array($this->_dcon, $method), $args);
193 * Returns original Drizzle connection object
195 * @return DrizzleCon
197 public function getConnectionObject()
199 return $this->_dcon;
204 * Wrapper around DrizzleResult.
206 * Allows for reading result rows as an associative array and hides complexity
207 * behind buffering.
209 * @package PhpMyAdmin-DBI
210 * @subpackage Drizzle
212 class PMA_DrizzleResult
215 * Instamce of DrizzleResult class
216 * @var DrizzleResult
218 private $_dresult;
220 * Fetch mode
221 * @var int
223 private $_fetchMode;
225 * Buffering mode
226 * @var int
228 private $_bufferMode;
231 * Cached column data
232 * @var DrizzleColumn[]
234 private $_columns = null;
236 * Cached column names
237 * @var string[]
239 private $_columnNames = null;
242 * Constructor
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();
260 * Sets fetch mode
262 * @param int $fetchMode fetch mode
264 * @return void
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
275 * @return void
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();
286 } else {
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
310 * @return int
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
323 * @return void
325 private function _transformResultRow(&$row, $fetchMode)
327 if (!$row) {
328 return;
331 switch ($fetchMode) {
332 case PMA_Drizzle::FETCH_ASSOC:
333 $row = array_combine($this->_columnNames, $row);
334 break;
335 case PMA_Drizzle::FETCH_BOTH:
336 $length = count($row);
337 for ($i = 0; $i < $length; $i++) {
338 $row[$this->_columnNames[$i]] = $row[$i];
340 break;
341 default:
342 break;
347 * Fetches next for from this result set
349 * @param int $fetchMode fetch mode to use, if not given the default one is used
351 * @return array|null
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;
363 $row = null;
364 switch ($this->_bufferMode) {
365 case PMA_Drizzle::BUFFER_RESULT:
366 $row = $this->_dresult->rowNext();
367 break;
368 case PMA_Drizzle::BUFFER_ROW:
369 $row = $this->_dresult->rowBuffer();
370 break;
372 $this->_transformResultRow($row, $fetchMode);
373 return $row;
377 * Adjusts the result pointer to an arbitrary row in buffered result
379 * @param integer $row_index where to seek
381 * @return bool
383 public function seek($row_index)
385 if ($this->_bufferMode != PMA_Drizzle::BUFFER_RESULT) {
386 trigger_error(
387 __("Can't seek in an unbuffered result set"), E_USER_WARNING
389 return false;
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);
394 return true;
396 return false;
400 * Returns the number of rows in buffered result set
402 * @return int|false
404 public function numRows()
406 if ($this->_bufferMode != PMA_Drizzle::BUFFER_RESULT) {
407 trigger_error(
408 __("Can't count rows in an unbuffered result set"), E_USER_WARNING
410 return false;
412 return $this->_dresult->rowCount();
416 * Returns the number of rows affected by query
418 * @return int|false
420 public function affectedRows()
422 return $this->_dresult->affectedRows();
426 * Frees resources taken by this result
428 * @return void
430 public function free()
432 unset($this->_columns);
433 unset($this->_columnNames);
434 drizzle_result_free($this->_dresult);
435 unset($this->_dresult);