2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Interface to the Drizzle extension
6 * WARNING - EXPERIMENTAL, never use in production, drizzle module segfaults often and when you least expect it to
8 * TODO: This file and drizzle-wrappers.lib.php should be devoid of any segault related hacks.
9 * TODO: Crashing versions of drizzle module and/or libdrizzle should be blacklisted
11 * @package PhpMyAdmin-DBI-Drizzle
13 if (! defined('PHPMYADMIN')) {
17 require_once './libraries/logging.lib.php';
18 require_once './libraries/dbi/drizzle-wrappers.lib.php';
23 if (!defined('PMA_MYSQL_CLIENT_API')) {
24 define('PMA_MYSQL_CLIENT_API', (int)drizzle_version());
28 * Helper function for connecting to the database server
30 * @param PMA_Drizzle $drizzle
35 * @param string $password
38 * @return PMA_DrizzleCon
40 function PMA_DBI_real_connect($drizzle, $host, $port, $uds, $user, $password, $db = null, $options = DRIZZLE_CON_NONE
)
43 $con = $drizzle->addUds($uds, $user, $password, $db, $options);
45 $con = $drizzle->addTcp($host, $port, $user, $password, $db, $options);
52 * connects to the database server
54 * @param string $user drizzle user name
55 * @param string $password drizzle user password
56 * @param bool $is_controluser
57 * @param array $server host/port/socket
58 * @param bool $auxiliary_connection (when true, don't go back to login if connection fails)
59 * @return mixed false on error or a mysqli object on success
61 function PMA_DBI_connect($user, $password, $is_controluser = false, $server = null, $auxiliary_connection = false)
66 $server_port = (empty($server['port']))
68 : (int)$server['port'];
69 $server_socket = (empty($server['socket']))
72 $server['host'] = (empty($server['host']))
76 $server_port = (empty($cfg['Server']['port']))
78 : (int) $cfg['Server']['port'];
79 $server_socket = (empty($cfg['Server']['socket']))
81 : $cfg['Server']['socket'];
84 if (strtolower($GLOBALS['cfg']['Server']['connect_type']) == 'tcp') {
85 $GLOBALS['cfg']['Server']['socket'] = '';
88 $drizzle = new PMA_Drizzle();
92 /* Optionally compress connection */
93 if ($GLOBALS['cfg']['Server']['compress']) {
94 $client_flags |
= DRIZZLE_CAPABILITIES_COMPRESS
;
97 /* Optionally enable SSL */
98 if ($GLOBALS['cfg']['Server']['ssl']) {
99 $client_flags |
= DRIZZLE_CAPABILITIES_SSL
;
103 $link = @PMA_DBI_real_connect
($drizzle, $cfg['Server']['host'], $server_port, $server_socket, $user, $password, false, $client_flags);
104 // Retry with empty password if we're allowed to
105 if ($link == false && isset($cfg['Server']['nopassword']) && $cfg['Server']['nopassword'] && !$is_controluser) {
106 $link = @PMA_DBI_real_connect
($drizzle, $cfg['Server']['host'], $server_port, $server_socket, $user, null, false, $client_flags);
109 $link = @PMA_DBI_real_connect
($drizzle, $server['host'], $server_port, $server_socket, $user, $password);
112 if ($link == false) {
113 if ($is_controluser) {
114 trigger_error(__('Connection for controluser as defined in your configuration failed.'), E_USER_WARNING
);
117 // we could be calling PMA_DBI_connect() to connect to another
118 // server, for example in the Synchronize feature, so do not
119 // go back to main login if it fails
120 if (! $auxiliary_connection) {
121 PMA_log_user($user, 'drizzle-denied');
127 PMA_DBI_postConnect($link, $is_controluser);
134 * selects given database
136 * @param string $dbname database name to select
137 * @param PMA_DrizzleCom $link connection object
140 function PMA_DBI_select_db($dbname, $link = null)
143 if (isset($GLOBALS['userlink'])) {
144 $link = $GLOBALS['userlink'];
149 return $link->selectDb($dbname);
153 * runs a query and returns the result
155 * @param string $query query to execute
156 * @param PMA_DrizzleCon $link connection object
157 * @param int $options
158 * @return PMA_DrizzleResult
160 function PMA_DBI_real_query($query, $link, $options)
162 $buffer_mode = $options & PMA_DBI_QUERY_UNBUFFERED
163 ? PMA_Drizzle
::BUFFER_ROW
164 : PMA_Drizzle
::BUFFER_RESULT
;
165 $res = $link->query($query, $buffer_mode);
170 * returns array of rows with associative and numeric keys from $result
172 * @param PMA_DrizzleResult $result
175 function PMA_DBI_fetch_array($result)
177 return $result->fetchRow(PMA_Drizzle
::FETCH_BOTH
);
181 * returns array of rows with associative keys from $result
183 * @param PMA_DrizzleResult $result
186 function PMA_DBI_fetch_assoc($result)
188 return $result->fetchRow(PMA_Drizzle
::FETCH_ASSOC
);
192 * returns array of rows with numeric keys from $result
194 * @param PMA_DrizzleResult $result
197 function PMA_DBI_fetch_row($result)
199 return $result->fetchRow(PMA_Drizzle
::FETCH_NUM
);
203 * Adjusts the result pointer to an arbitrary row in the result
205 * @param PMA_DrizzleResult $result
207 * @return boolean true on success, false on failure
209 function PMA_DBI_data_seek($result, $offset)
211 return $result->seek($offset);
215 * Frees memory associated with the result
217 * @param PMA_DrizzleResult $result
219 function PMA_DBI_free_result($result)
221 if ($result instanceof PMA_DrizzleResult
) {
227 * Check if there are any more query results from a multi query
231 function PMA_DBI_more_results() {
232 // N.B.: PHP's 'mysql' extension does not support
233 // multi_queries so this function will always
234 // return false. Use the 'mysqli' extension, if
235 // you need support for multi_queries.
240 * Prepare next result from multi_query
244 function PMA_DBI_next_result() {
245 // N.B.: PHP's 'mysql' extension does not support
246 // multi_queries so this function will always
247 // return false. Use the 'mysqli' extension, if
248 // you need support for multi_queries.
253 * Returns a string representing the type of connection used
254 * @param PMA_DrizzleCon $link connection object
255 * @return string type of connection used
257 function PMA_DBI_get_host_info($link = null)
259 if (null === $link) {
260 if (isset($GLOBALS['userlink'])) {
261 $link = $GLOBALS['userlink'];
268 ?
$link->host() . ':' . $link->port() . ' via TCP/IP'
269 : 'Localhost via UNIX socket';
274 * Returns the version of the Drizzle protocol used
275 * @param PMA_DrizzleCon $link connection object
276 * @return int version of the Drizzle protocol used
278 function PMA_DBI_get_proto_info($link = null)
280 if (null === $link) {
281 if (isset($GLOBALS['userlink'])) {
282 $link = $GLOBALS['userlink'];
288 return $link->protocolVersion();
292 * returns a string that represents the client library version
293 * @return string Drizzle client library version
295 function PMA_DBI_get_client_info()
297 return 'libdrizzle (Drizzle ' . drizzle_version() . ')';
301 * returns last error message or false if no errors occured
303 * @param PMA_DrizzleCon $link connection object
304 * @return string|bool $error or false
306 function PMA_DBI_getError($link = null)
308 $GLOBALS['errno'] = 0;
310 /* Treat false same as null because of controllink */
311 if ($link === false) {
315 if (null === $link && isset($GLOBALS['userlink'])) {
316 $link =& $GLOBALS['userlink'];
317 // Do not stop now. We still can get the error code
318 // with mysqli_connect_errno()
323 if (null !== $link) {
324 $error_number = drizzle_con_errno($link->getConnectionObject());
325 $error_message = drizzle_con_error($link->getConnectionObject());
327 $error_number = drizzle_errno();
328 $error_message = drizzle_error();
330 if (0 == $error_number) {
334 // keep the error number for further check after the call to PMA_DBI_getError()
335 $GLOBALS['errno'] = $error_number;
337 return PMA_DBI_formatError($error_number, $error_message);
341 * returns the number of rows returned by last query
343 * @param PMA_DrizzleResult $result
346 function PMA_DBI_num_rows($result)
348 // see the note for PMA_DBI_try_query();
349 if (!is_bool($result)) {
350 return @$result->numRows();
357 * returns last inserted auto_increment id for given $link or $GLOBALS['userlink']
359 * @param PMA_DrizzleCon $link connection object
362 function PMA_DBI_insert_id($link = null)
365 if (isset($GLOBALS['userlink'])) {
366 $link = $GLOBALS['userlink'];
372 // copied from mysql and mysqli
374 // When no controluser is defined, using mysqli_insert_id($link)
375 // does not always return the last insert id due to a mixup with
376 // the tracking mechanism, but this works:
377 return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link);
378 // Curiously, this problem does not happen with the mysql extension but
379 // there is another problem with BIGINT primary keys so PMA_DBI_insert_id()
380 // in the mysql extension also uses this logic.
384 * returns the number of rows affected by last query
386 * @param PMA_DrizzleResult $link connection object
387 * @param bool $get_from_cache
390 function PMA_DBI_affected_rows($link = null, $get_from_cache = true)
393 if (isset($GLOBALS['userlink'])) {
394 $link = $GLOBALS['userlink'];
399 if ($get_from_cache) {
400 return $GLOBALS['cached_affected_rows'];
402 return $link->affectedRows();
407 * returns metainfo for fields in $result
409 * @param PMA_DrizzleResult $result
410 * @return array meta info for fields in $result
412 function PMA_DBI_get_fields_meta($result)
414 // Build an associative array for a type look up
416 /*$typeAr[DRIZZLE_COLUMN_TYPE_DECIMAL] = 'real';
417 $typeAr[DRIZZLE_COLUMN_TYPE_NEWDECIMAL] = 'real';
418 $typeAr[DRIZZLE_COLUMN_TYPE_BIT] = 'int';
419 $typeAr[DRIZZLE_COLUMN_TYPE_TINY] = 'int';
420 $typeAr[DRIZZLE_COLUMN_TYPE_SHORT] = 'int';
421 $typeAr[DRIZZLE_COLUMN_TYPE_LONG] = 'int';
422 $typeAr[DRIZZLE_COLUMN_TYPE_FLOAT] = 'real';
423 $typeAr[DRIZZLE_COLUMN_TYPE_DOUBLE] = 'real';
424 $typeAr[DRIZZLE_COLUMN_TYPE_NULL] = 'null';
425 $typeAr[DRIZZLE_COLUMN_TYPE_TIMESTAMP] = 'timestamp';
426 $typeAr[DRIZZLE_COLUMN_TYPE_LONGLONG] = 'int';
427 $typeAr[DRIZZLE_COLUMN_TYPE_INT24] = 'int';
428 $typeAr[DRIZZLE_COLUMN_TYPE_DATE] = 'date';
429 $typeAr[DRIZZLE_COLUMN_TYPE_TIME] = 'date';
430 $typeAr[DRIZZLE_COLUMN_TYPE_DATETIME] = 'datetime';
431 $typeAr[DRIZZLE_COLUMN_TYPE_YEAR] = 'year';
432 $typeAr[DRIZZLE_COLUMN_TYPE_NEWDATE] = 'date';
433 $typeAr[DRIZZLE_COLUMN_TYPE_ENUM] = 'unknown';
434 $typeAr[DRIZZLE_COLUMN_TYPE_SET] = 'unknown';
435 $typeAr[DRIZZLE_COLUMN_TYPE_VIRTUAL] = 'unknown';
436 $typeAr[DRIZZLE_COLUMN_TYPE_TINY_BLOB] = 'blob';
437 $typeAr[DRIZZLE_COLUMN_TYPE_MEDIUM_BLOB] = 'blob';
438 $typeAr[DRIZZLE_COLUMN_TYPE_LONG_BLOB] = 'blob';
439 $typeAr[DRIZZLE_COLUMN_TYPE_BLOB] = 'blob';
440 $typeAr[DRIZZLE_COLUMN_TYPE_VAR_STRING] = 'string';
441 $typeAr[DRIZZLE_COLUMN_TYPE_VARCHAR] = 'string';
442 $typeAr[DRIZZLE_COLUMN_TYPE_STRING] = 'string';
443 $typeAr[DRIZZLE_COLUMN_TYPE_GEOMETRY] = 'geometry';*/
445 $typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_BLOB
] = 'blob';
446 $typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_DATE
] = 'date';
447 $typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_DATETIME
] = 'datetime';
448 $typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_DOUBLE
] = 'real';
449 $typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_ENUM
] = 'unknown';
450 $typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_LONG
] = 'int';
451 $typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_LONGLONG
] = 'int';
452 $typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_MAX
] = 'unknown';
453 $typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_NULL
] = 'null';
454 $typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_TIMESTAMP
] = 'timestamp';
455 $typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_TINY
] = 'int';
456 $typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_VARCHAR
] = 'string';
458 // array of DrizzleColumn
459 $columns = $result->getColumns();
460 // columns in a standarized format
461 $std_columns = array();
463 foreach ($columns as $k => $column) {
465 $c->name
= $column->name();
466 $c->orgname
= $column->origName();
467 $c->table
= $column->table();
468 $c->orgtable
= $column->origTable();
469 $c->def
= $column->defaultValue();
470 $c->db
= $column->db();
471 $c->catalog
= $column->catalog();
472 // $column->maxSize() returns always 0 while size() seems
473 // to return a correct value (drizzle extension v.0.5, API v.7)
474 $c->max_length
= $column->size();
475 $c->decimals
= $column->decimals();
476 $c->charsetnr
= $column->charset();
477 $c->type
= $typeAr[$column->typeDrizzle()];
478 $c->_type
= $column->type();
479 $c->flags
= PMA_DBI_field_flags($result, $k);
480 $c->_flags
= $column->flags();
482 $c->multiple_key
= (int) (bool) ($c->_flags
& DRIZZLE_COLUMN_FLAGS_MULTIPLE_KEY
);
483 $c->primary_key
= (int) (bool) ($c->_flags
& DRIZZLE_COLUMN_FLAGS_PRI_KEY
);
484 $c->unique_key
= (int) (bool) ($c->_flags
& DRIZZLE_COLUMN_FLAGS_UNIQUE_KEY
);
485 $c->not_null
= (int) (bool) ($c->_flags
& DRIZZLE_COLUMN_FLAGS_NOT_NULL
);
486 $c->unsigned
= (int) (bool) ($c->_flags
& DRIZZLE_COLUMN_FLAGS_UNSIGNED
);
487 $c->zerofill
= (int) (bool) ($c->_flags
& DRIZZLE_COLUMN_FLAGS_ZEROFILL
);
488 $c->numeric = (int) (bool) ($c->_flags
& DRIZZLE_COLUMN_FLAGS_NUM
);
489 $c->blob
= (int) (bool) ($c->_flags
& DRIZZLE_COLUMN_FLAGS_BLOB
);
498 * return number of fields in given $result
500 * @param PMA_DrizzleResult $result
501 * @return int field count
503 function PMA_DBI_num_fields($result)
505 return $result->numColumns();
509 * returns the length of the given field $i in $result
511 * @param PMA_DrizzleResult $result
512 * @param int $i field
513 * @return int length of field
515 function PMA_DBI_field_len($result, $i)
517 $colums = $result->getColumns();
518 return $colums[$i]->size();
522 * returns name of $i. field in $result
524 * @param PMA_DrizzleResult $result
525 * @param int $i field
526 * @return string name of $i. field in $result
528 function PMA_DBI_field_name($result, $i)
530 $colums = $result->getColumns();
531 return $colums[$i]->name();
535 * returns concatenated string of human readable field flags
537 * @param PMA_DrizzleResult $result
538 * @param int $i field
539 * @return string field flags
541 function PMA_DBI_field_flags($result, $i)
543 $columns = $result->getColumns();
545 $type = $f->typeDrizzle();
546 $charsetnr = $f->charset();
549 if ($f & DRIZZLE_COLUMN_FLAGS_UNIQUE_KEY
) {
552 if ($f & DRIZZLE_COLUMN_FLAGS_NUM
) {
555 if ($f & DRIZZLE_COLUMN_FLAGS_PART_KEY
) {
556 $flags .= 'part_key ';
558 if ($f & DRIZZLE_COLUMN_FLAGS_SET
) {
561 if ($f & DRIZZLE_COLUMN_FLAGS_TIMESTAMP
) {
562 $flags .= 'timestamp ';
564 if ($f & DRIZZLE_COLUMN_FLAGS_AUTO_INCREMENT
) {
565 $flags .= 'auto_increment ';
567 if ($f & DRIZZLE_COLUMN_FLAGS_ENUM
) {
570 // See http://dev.mysql.com/doc/refman/6.0/en/c-api-datatypes.html:
571 // to determine if a string is binary, we should not use MYSQLI_BINARY_FLAG
572 // but instead the charsetnr member of the MYSQL_FIELD
573 // structure. Watch out: some types like DATE returns 63 in charsetnr
574 // so we have to check also the type.
575 // Unfortunately there is no equivalent in the mysql extension.
576 if (($type == DRIZZLE_COLUMN_TYPE_DRIZZLE_BLOB ||
$type == DRIZZLE_COLUMN_TYPE_DRIZZLE_VARCHAR
) && 63 == $charsetnr) {
579 if ($f & DRIZZLE_COLUMN_FLAGS_ZEROFILL
) {
580 $flags .= 'zerofill ';
582 if ($f & DRIZZLE_COLUMN_FLAGS_UNSIGNED
) {
583 $flags .= 'unsigned ';
585 if ($f & DRIZZLE_COLUMN_FLAGS_BLOB
) {
588 if ($f & DRIZZLE_COLUMN_FLAGS_MULTIPLE_KEY
) {
589 $flags .= 'multiple_key ';
591 if ($f & DRIZZLE_COLUMN_FLAGS_UNIQUE_KEY
) {
592 $flags .= 'unique_key ';
594 if ($f & DRIZZLE_COLUMN_FLAGS_PRI_KEY
) {
595 $flags .= 'primary_key ';
597 if ($f & DRIZZLE_COLUMN_FLAGS_NOT_NULL
) {
598 $flags .= 'not_null ';