PageRenderTime 38ms CodeModel.GetById 4ms RepoModel.GetById 0ms app.codeStats 0ms

/code/ryzom/tools/server/www/webtt/cake/libs/model/datasources/dbo/dbo_mysql.php

https://bitbucket.org/mattraykowski/ryzomcore_demoshard
PHP | 808 lines | 505 code | 57 blank | 246 comment | 137 complexity | aee44d07a7dbc7eee92958a18e01718e MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * MySQL layer for DBO
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake
  16. * @subpackage cake.cake.libs.model.datasources.dbo
  17. * @since CakePHP(tm) v 0.10.5.1790
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. /**
  21. * Provides common base for MySQL & MySQLi connections
  22. *
  23. * @package cake
  24. * @subpackage cake.cake.libs.model.datasources.dbo
  25. */
  26. class DboMysqlBase extends DboSource {
  27. /**
  28. * Description property.
  29. *
  30. * @var string
  31. */
  32. var $description = "MySQL DBO Base Driver";
  33. /**
  34. * Start quote
  35. *
  36. * @var string
  37. */
  38. var $startQuote = "`";
  39. /**
  40. * End quote
  41. *
  42. * @var string
  43. */
  44. var $endQuote = "`";
  45. /**
  46. * use alias for update and delete. Set to true if version >= 4.1
  47. *
  48. * @var boolean
  49. * @access protected
  50. */
  51. var $_useAlias = true;
  52. /**
  53. * Index of basic SQL commands
  54. *
  55. * @var array
  56. * @access protected
  57. */
  58. var $_commands = array(
  59. 'begin' => 'START TRANSACTION',
  60. 'commit' => 'COMMIT',
  61. 'rollback' => 'ROLLBACK'
  62. );
  63. /**
  64. * List of engine specific additional field parameters used on table creating
  65. *
  66. * @var array
  67. * @access public
  68. */
  69. var $fieldParameters = array(
  70. 'charset' => array('value' => 'CHARACTER SET', 'quote' => false, 'join' => ' ', 'column' => false, 'position' => 'beforeDefault'),
  71. 'collate' => array('value' => 'COLLATE', 'quote' => false, 'join' => ' ', 'column' => 'Collation', 'position' => 'beforeDefault'),
  72. 'comment' => array('value' => 'COMMENT', 'quote' => true, 'join' => ' ', 'column' => 'Comment', 'position' => 'afterDefault')
  73. );
  74. /**
  75. * List of table engine specific parameters used on table creating
  76. *
  77. * @var array
  78. * @access public
  79. */
  80. var $tableParameters = array(
  81. 'charset' => array('value' => 'DEFAULT CHARSET', 'quote' => false, 'join' => '=', 'column' => 'charset'),
  82. 'collate' => array('value' => 'COLLATE', 'quote' => false, 'join' => '=', 'column' => 'Collation'),
  83. 'engine' => array('value' => 'ENGINE', 'quote' => false, 'join' => '=', 'column' => 'Engine')
  84. );
  85. /**
  86. * MySQL column definition
  87. *
  88. * @var array
  89. */
  90. var $columns = array(
  91. 'primary_key' => array('name' => 'NOT NULL AUTO_INCREMENT'),
  92. 'string' => array('name' => 'varchar', 'limit' => '255'),
  93. 'text' => array('name' => 'text'),
  94. 'integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'),
  95. 'float' => array('name' => 'float', 'formatter' => 'floatval'),
  96. 'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
  97. 'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
  98. 'time' => array('name' => 'time', 'format' => 'H:i:s', 'formatter' => 'date'),
  99. 'date' => array('name' => 'date', 'format' => 'Y-m-d', 'formatter' => 'date'),
  100. 'binary' => array('name' => 'blob'),
  101. 'boolean' => array('name' => 'tinyint', 'limit' => '1')
  102. );
  103. /**
  104. * Returns an array of the fields in given table name.
  105. *
  106. * @param string $tableName Name of database table to inspect
  107. * @return array Fields in table. Keys are name and type
  108. */
  109. function describe(&$model) {
  110. $cache = parent::describe($model);
  111. if ($cache != null) {
  112. return $cache;
  113. }
  114. $fields = false;
  115. $cols = $this->query('SHOW FULL COLUMNS FROM ' . $this->fullTableName($model));
  116. foreach ($cols as $column) {
  117. $colKey = array_keys($column);
  118. if (isset($column[$colKey[0]]) && !isset($column[0])) {
  119. $column[0] = $column[$colKey[0]];
  120. }
  121. if (isset($column[0])) {
  122. $fields[$column[0]['Field']] = array(
  123. 'type' => $this->column($column[0]['Type']),
  124. 'null' => ($column[0]['Null'] == 'YES' ? true : false),
  125. 'default' => $column[0]['Default'],
  126. 'length' => $this->length($column[0]['Type']),
  127. );
  128. if (!empty($column[0]['Key']) && isset($this->index[$column[0]['Key']])) {
  129. $fields[$column[0]['Field']]['key'] = $this->index[$column[0]['Key']];
  130. }
  131. foreach ($this->fieldParameters as $name => $value) {
  132. if (!empty($column[0][$value['column']])) {
  133. $fields[$column[0]['Field']][$name] = $column[0][$value['column']];
  134. }
  135. }
  136. if (isset($fields[$column[0]['Field']]['collate'])) {
  137. $charset = $this->getCharsetName($fields[$column[0]['Field']]['collate']);
  138. if ($charset) {
  139. $fields[$column[0]['Field']]['charset'] = $charset;
  140. }
  141. }
  142. }
  143. }
  144. $this->__cacheDescription($this->fullTableName($model, false), $fields);
  145. return $fields;
  146. }
  147. /**
  148. * Generates and executes an SQL UPDATE statement for given model, fields, and values.
  149. *
  150. * @param Model $model
  151. * @param array $fields
  152. * @param array $values
  153. * @param mixed $conditions
  154. * @return array
  155. */
  156. function update(&$model, $fields = array(), $values = null, $conditions = null) {
  157. if (!$this->_useAlias) {
  158. return parent::update($model, $fields, $values, $conditions);
  159. }
  160. if ($values == null) {
  161. $combined = $fields;
  162. } else {
  163. $combined = array_combine($fields, $values);
  164. }
  165. $alias = $joins = false;
  166. $fields = $this->_prepareUpdateFields($model, $combined, empty($conditions), !empty($conditions));
  167. $fields = implode(', ', $fields);
  168. $table = $this->fullTableName($model);
  169. if (!empty($conditions)) {
  170. $alias = $this->name($model->alias);
  171. if ($model->name == $model->alias) {
  172. $joins = implode(' ', $this->_getJoins($model));
  173. }
  174. }
  175. $conditions = $this->conditions($this->defaultConditions($model, $conditions, $alias), true, true, $model);
  176. if ($conditions === false) {
  177. return false;
  178. }
  179. if (!$this->execute($this->renderStatement('update', compact('table', 'alias', 'joins', 'fields', 'conditions')))) {
  180. $model->onError();
  181. return false;
  182. }
  183. return true;
  184. }
  185. /**
  186. * Generates and executes an SQL DELETE statement for given id/conditions on given model.
  187. *
  188. * @param Model $model
  189. * @param mixed $conditions
  190. * @return boolean Success
  191. */
  192. function delete(&$model, $conditions = null) {
  193. if (!$this->_useAlias) {
  194. return parent::delete($model, $conditions);
  195. }
  196. $alias = $this->name($model->alias);
  197. $table = $this->fullTableName($model);
  198. $joins = implode(' ', $this->_getJoins($model));
  199. if (empty($conditions)) {
  200. $alias = $joins = false;
  201. }
  202. $complexConditions = false;
  203. foreach ((array)$conditions as $key => $value) {
  204. if (strpos($key, $model->alias) === false) {
  205. $complexConditions = true;
  206. break;
  207. }
  208. }
  209. if (!$complexConditions) {
  210. $joins = false;
  211. }
  212. $conditions = $this->conditions($this->defaultConditions($model, $conditions, $alias), true, true, $model);
  213. if ($conditions === false) {
  214. return false;
  215. }
  216. if ($this->execute($this->renderStatement('delete', compact('alias', 'table', 'joins', 'conditions'))) === false) {
  217. $model->onError();
  218. return false;
  219. }
  220. return true;
  221. }
  222. /**
  223. * Sets the database encoding
  224. *
  225. * @param string $enc Database encoding
  226. */
  227. function setEncoding($enc) {
  228. return $this->_execute('SET NAMES ' . $enc) != false;
  229. }
  230. /**
  231. * Returns an array of the indexes in given datasource name.
  232. *
  233. * @param string $model Name of model to inspect
  234. * @return array Fields in table. Keys are column and unique
  235. */
  236. function index($model) {
  237. $index = array();
  238. $table = $this->fullTableName($model);
  239. if ($table) {
  240. $indexes = $this->query('SHOW INDEX FROM ' . $table);
  241. if (isset($indexes[0]['STATISTICS'])) {
  242. $keys = Set::extract($indexes, '{n}.STATISTICS');
  243. } else {
  244. $keys = Set::extract($indexes, '{n}.0');
  245. }
  246. foreach ($keys as $i => $key) {
  247. if (!isset($index[$key['Key_name']])) {
  248. $col = array();
  249. $index[$key['Key_name']]['column'] = $key['Column_name'];
  250. $index[$key['Key_name']]['unique'] = intval($key['Non_unique'] == 0);
  251. } else {
  252. if (!is_array($index[$key['Key_name']]['column'])) {
  253. $col[] = $index[$key['Key_name']]['column'];
  254. }
  255. $col[] = $key['Column_name'];
  256. $index[$key['Key_name']]['column'] = $col;
  257. }
  258. }
  259. }
  260. return $index;
  261. }
  262. /**
  263. * Generate a MySQL Alter Table syntax for the given Schema comparison
  264. *
  265. * @param array $compare Result of a CakeSchema::compare()
  266. * @return array Array of alter statements to make.
  267. */
  268. function alterSchema($compare, $table = null) {
  269. if (!is_array($compare)) {
  270. return false;
  271. }
  272. $out = '';
  273. $colList = array();
  274. foreach ($compare as $curTable => $types) {
  275. $indexes = $tableParameters = $colList = array();
  276. if (!$table || $table == $curTable) {
  277. $out .= 'ALTER TABLE ' . $this->fullTableName($curTable) . " \n";
  278. foreach ($types as $type => $column) {
  279. if (isset($column['indexes'])) {
  280. $indexes[$type] = $column['indexes'];
  281. unset($column['indexes']);
  282. }
  283. if (isset($column['tableParameters'])) {
  284. $tableParameters[$type] = $column['tableParameters'];
  285. unset($column['tableParameters']);
  286. }
  287. switch ($type) {
  288. case 'add':
  289. foreach ($column as $field => $col) {
  290. $col['name'] = $field;
  291. $alter = 'ADD ' . $this->buildColumn($col);
  292. if (isset($col['after'])) {
  293. $alter .= ' AFTER ' . $this->name($col['after']);
  294. }
  295. $colList[] = $alter;
  296. }
  297. break;
  298. case 'drop':
  299. foreach ($column as $field => $col) {
  300. $col['name'] = $field;
  301. $colList[] = 'DROP ' . $this->name($field);
  302. }
  303. break;
  304. case 'change':
  305. foreach ($column as $field => $col) {
  306. if (!isset($col['name'])) {
  307. $col['name'] = $field;
  308. }
  309. $colList[] = 'CHANGE ' . $this->name($field) . ' ' . $this->buildColumn($col);
  310. }
  311. break;
  312. }
  313. }
  314. $colList = array_merge($colList, $this->_alterIndexes($curTable, $indexes));
  315. $colList = array_merge($colList, $this->_alterTableParameters($curTable, $tableParameters));
  316. $out .= "\t" . join(",\n\t", $colList) . ";\n\n";
  317. }
  318. }
  319. return $out;
  320. }
  321. /**
  322. * Generate a MySQL "drop table" statement for the given Schema object
  323. *
  324. * @param object $schema An instance of a subclass of CakeSchema
  325. * @param string $table Optional. If specified only the table name given will be generated.
  326. * Otherwise, all tables defined in the schema are generated.
  327. * @return string
  328. */
  329. function dropSchema($schema, $table = null) {
  330. if (!is_a($schema, 'CakeSchema')) {
  331. trigger_error(__('Invalid schema object', true), E_USER_WARNING);
  332. return null;
  333. }
  334. $out = '';
  335. foreach ($schema->tables as $curTable => $columns) {
  336. if (!$table || $table == $curTable) {
  337. $out .= 'DROP TABLE IF EXISTS ' . $this->fullTableName($curTable) . ";\n";
  338. }
  339. }
  340. return $out;
  341. }
  342. /**
  343. * Generate MySQL table parameter alteration statementes for a table.
  344. *
  345. * @param string $table Table to alter parameters for.
  346. * @param array $parameters Parameters to add & drop.
  347. * @return array Array of table property alteration statementes.
  348. * @todo Implement this method.
  349. */
  350. function _alterTableParameters($table, $parameters) {
  351. if (isset($parameters['change'])) {
  352. return $this->buildTableParameters($parameters['change']);
  353. }
  354. return array();
  355. }
  356. /**
  357. * Generate MySQL index alteration statements for a table.
  358. *
  359. * @param string $table Table to alter indexes for
  360. * @param array $new Indexes to add and drop
  361. * @return array Index alteration statements
  362. */
  363. function _alterIndexes($table, $indexes) {
  364. $alter = array();
  365. if (isset($indexes['drop'])) {
  366. foreach($indexes['drop'] as $name => $value) {
  367. $out = 'DROP ';
  368. if ($name == 'PRIMARY') {
  369. $out .= 'PRIMARY KEY';
  370. } else {
  371. $out .= 'KEY ' . $name;
  372. }
  373. $alter[] = $out;
  374. }
  375. }
  376. if (isset($indexes['add'])) {
  377. foreach ($indexes['add'] as $name => $value) {
  378. $out = 'ADD ';
  379. if ($name == 'PRIMARY') {
  380. $out .= 'PRIMARY ';
  381. $name = null;
  382. } else {
  383. if (!empty($value['unique'])) {
  384. $out .= 'UNIQUE ';
  385. }
  386. }
  387. if (is_array($value['column'])) {
  388. $out .= 'KEY '. $name .' (' . implode(', ', array_map(array(&$this, 'name'), $value['column'])) . ')';
  389. } else {
  390. $out .= 'KEY '. $name .' (' . $this->name($value['column']) . ')';
  391. }
  392. $alter[] = $out;
  393. }
  394. }
  395. return $alter;
  396. }
  397. /**
  398. * Inserts multiple values into a table
  399. *
  400. * @param string $table
  401. * @param string $fields
  402. * @param array $values
  403. */
  404. function insertMulti($table, $fields, $values) {
  405. $table = $this->fullTableName($table);
  406. if (is_array($fields)) {
  407. $fields = implode(', ', array_map(array(&$this, 'name'), $fields));
  408. }
  409. $values = implode(', ', $values);
  410. $this->query("INSERT INTO {$table} ({$fields}) VALUES {$values}");
  411. }
  412. /**
  413. * Returns an detailed array of sources (tables) in the database.
  414. *
  415. * @param string $name Table name to get parameters
  416. * @return array Array of tablenames in the database
  417. */
  418. function listDetailedSources($name = null) {
  419. $condition = '';
  420. if (is_string($name)) {
  421. $condition = ' LIKE ' . $this->value($name);
  422. }
  423. $result = $this->query('SHOW TABLE STATUS FROM ' . $this->name($this->config['database']) . $condition . ';');
  424. if (!$result) {
  425. return array();
  426. } else {
  427. $tables = array();
  428. foreach ($result as $row) {
  429. $tables[$row['TABLES']['Name']] = $row['TABLES'];
  430. if (!empty($row['TABLES']['Collation'])) {
  431. $charset = $this->getCharsetName($row['TABLES']['Collation']);
  432. if ($charset) {
  433. $tables[$row['TABLES']['Name']]['charset'] = $charset;
  434. }
  435. }
  436. }
  437. if (is_string($name)) {
  438. return $tables[$name];
  439. }
  440. return $tables;
  441. }
  442. }
  443. /**
  444. * Converts database-layer column types to basic types
  445. *
  446. * @param string $real Real database-layer column type (i.e. "varchar(255)")
  447. * @return string Abstract column type (i.e. "string")
  448. */
  449. function column($real) {
  450. if (is_array($real)) {
  451. $col = $real['name'];
  452. if (isset($real['limit'])) {
  453. $col .= '('.$real['limit'].')';
  454. }
  455. return $col;
  456. }
  457. $col = str_replace(')', '', $real);
  458. $limit = $this->length($real);
  459. if (strpos($col, '(') !== false) {
  460. list($col, $vals) = explode('(', $col);
  461. }
  462. if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) {
  463. return $col;
  464. }
  465. if (($col == 'tinyint' && $limit == 1) || $col == 'boolean') {
  466. return 'boolean';
  467. }
  468. if (strpos($col, 'int') !== false) {
  469. return 'integer';
  470. }
  471. if (strpos($col, 'char') !== false || $col == 'tinytext') {
  472. return 'string';
  473. }
  474. if (strpos($col, 'text') !== false) {
  475. return 'text';
  476. }
  477. if (strpos($col, 'blob') !== false || $col == 'binary') {
  478. return 'binary';
  479. }
  480. if (strpos($col, 'float') !== false || strpos($col, 'double') !== false || strpos($col, 'decimal') !== false) {
  481. return 'float';
  482. }
  483. if (strpos($col, 'enum') !== false) {
  484. return "enum($vals)";
  485. }
  486. return 'text';
  487. }
  488. }
  489. /**
  490. * MySQL DBO driver object
  491. *
  492. * Provides connection and SQL generation for MySQL RDMS
  493. *
  494. * @package cake
  495. * @subpackage cake.cake.libs.model.datasources.dbo
  496. */
  497. class DboMysql extends DboMysqlBase {
  498. /**
  499. * Datasource description
  500. *
  501. * @var string
  502. */
  503. var $description = "MySQL DBO Driver";
  504. /**
  505. * Base configuration settings for MySQL driver
  506. *
  507. * @var array
  508. */
  509. var $_baseConfig = array(
  510. 'persistent' => true,
  511. 'host' => 'localhost',
  512. 'login' => 'root',
  513. 'password' => '',
  514. 'database' => 'cake',
  515. 'port' => '3306'
  516. );
  517. /**
  518. * Connects to the database using options in the given configuration array.
  519. *
  520. * @return boolean True if the database could be connected, else false
  521. */
  522. function connect() {
  523. $config = $this->config;
  524. $this->connected = false;
  525. if (!$config['persistent']) {
  526. $this->connection = mysql_connect($config['host'] . ':' . $config['port'], $config['login'], $config['password'], true);
  527. $config['connect'] = 'mysql_connect';
  528. } else {
  529. $this->connection = mysql_pconnect($config['host'] . ':' . $config['port'], $config['login'], $config['password']);
  530. }
  531. if (mysql_select_db($config['database'], $this->connection)) {
  532. $this->connected = true;
  533. }
  534. if (!empty($config['encoding'])) {
  535. $this->setEncoding($config['encoding']);
  536. }
  537. $this->_useAlias = (bool)version_compare(mysql_get_server_info($this->connection), "4.1", ">=");
  538. return $this->connected;
  539. }
  540. /**
  541. * Check whether the MySQL extension is installed/loaded
  542. *
  543. * @return boolean
  544. */
  545. function enabled() {
  546. return extension_loaded('mysql');
  547. }
  548. /**
  549. * Disconnects from database.
  550. *
  551. * @return boolean True if the database could be disconnected, else false
  552. */
  553. function disconnect() {
  554. if (isset($this->results) && is_resource($this->results)) {
  555. mysql_free_result($this->results);
  556. }
  557. $this->connected = !@mysql_close($this->connection);
  558. return !$this->connected;
  559. }
  560. /**
  561. * Executes given SQL statement.
  562. *
  563. * @param string $sql SQL statement
  564. * @return resource Result resource identifier
  565. * @access protected
  566. */
  567. function _execute($sql) {
  568. return mysql_query($sql, $this->connection);
  569. }
  570. /**
  571. * Returns an array of sources (tables) in the database.
  572. *
  573. * @return array Array of tablenames in the database
  574. */
  575. function listSources() {
  576. $cache = parent::listSources();
  577. if ($cache != null) {
  578. return $cache;
  579. }
  580. $result = $this->_execute('SHOW TABLES FROM ' . $this->name($this->config['database']) . ';');
  581. if (!$result) {
  582. return array();
  583. } else {
  584. $tables = array();
  585. while ($line = mysql_fetch_row($result)) {
  586. $tables[] = $line[0];
  587. }
  588. parent::listSources($tables);
  589. return $tables;
  590. }
  591. }
  592. /**
  593. * Returns a quoted and escaped string of $data for use in an SQL statement.
  594. *
  595. * @param string $data String to be prepared for use in an SQL statement
  596. * @param string $column The column into which this data will be inserted
  597. * @param boolean $safe Whether or not numeric data should be handled automagically if no column data is provided
  598. * @return string Quoted and escaped data
  599. */
  600. function value($data, $column = null, $safe = false) {
  601. $parent = parent::value($data, $column, $safe);
  602. if ($parent != null) {
  603. return $parent;
  604. }
  605. if ($data === null || (is_array($data) && empty($data))) {
  606. return 'NULL';
  607. }
  608. if ($data === '' && $column !== 'integer' && $column !== 'float' && $column !== 'boolean') {
  609. return "''";
  610. }
  611. if (empty($column)) {
  612. $column = $this->introspectType($data);
  613. }
  614. switch ($column) {
  615. case 'boolean':
  616. return $this->boolean((bool)$data);
  617. break;
  618. case 'integer':
  619. case 'float':
  620. if ($data === '') {
  621. return 'NULL';
  622. }
  623. if (is_float($data)) {
  624. return sprintf('%F', $data);
  625. }
  626. if ((is_int($data) || $data === '0') || (
  627. is_numeric($data) && strpos($data, ',') === false &&
  628. $data[0] != '0' && strpos($data, 'e') === false)
  629. ) {
  630. return $data;
  631. }
  632. default:
  633. return "'" . mysql_real_escape_string($data, $this->connection) . "'";
  634. break;
  635. }
  636. }
  637. /**
  638. * Returns a formatted error message from previous database operation.
  639. *
  640. * @return string Error message with error number
  641. */
  642. function lastError() {
  643. if (mysql_errno($this->connection)) {
  644. return mysql_errno($this->connection).': '.mysql_error($this->connection);
  645. }
  646. return null;
  647. }
  648. /**
  649. * Returns number of affected rows in previous database operation. If no previous operation exists,
  650. * this returns false.
  651. *
  652. * @return integer Number of affected rows
  653. */
  654. function lastAffected() {
  655. if ($this->_result) {
  656. return mysql_affected_rows($this->connection);
  657. }
  658. return null;
  659. }
  660. /**
  661. * Returns number of rows in previous resultset. If no previous resultset exists,
  662. * this returns false.
  663. *
  664. * @return integer Number of rows in resultset
  665. */
  666. function lastNumRows() {
  667. if ($this->hasResult()) {
  668. return mysql_num_rows($this->_result);
  669. }
  670. return null;
  671. }
  672. /**
  673. * Returns the ID generated from the previous INSERT operation.
  674. *
  675. * @param unknown_type $source
  676. * @return in
  677. */
  678. function lastInsertId($source = null) {
  679. $id = $this->fetchRow('SELECT LAST_INSERT_ID() AS insertID', false);
  680. if ($id !== false && !empty($id) && !empty($id[0]) && isset($id[0]['insertID'])) {
  681. return $id[0]['insertID'];
  682. }
  683. return null;
  684. }
  685. /**
  686. * Enter description here...
  687. *
  688. * @param unknown_type $results
  689. */
  690. function resultSet(&$results) {
  691. if (isset($this->results) && is_resource($this->results) && $this->results != $results) {
  692. mysql_free_result($this->results);
  693. }
  694. $this->results =& $results;
  695. $this->map = array();
  696. $numFields = mysql_num_fields($results);
  697. $index = 0;
  698. $j = 0;
  699. while ($j < $numFields) {
  700. $column = mysql_fetch_field($results, $j);
  701. if (!empty($column->table) && strpos($column->name, $this->virtualFieldSeparator) === false) {
  702. $this->map[$index++] = array($column->table, $column->name);
  703. } else {
  704. $this->map[$index++] = array(0, $column->name);
  705. }
  706. $j++;
  707. }
  708. }
  709. /**
  710. * Fetches the next row from the current result set
  711. *
  712. * @return unknown
  713. */
  714. function fetchResult() {
  715. if ($row = mysql_fetch_row($this->results)) {
  716. $resultRow = array();
  717. $i = 0;
  718. foreach ($row as $index => $field) {
  719. list($table, $column) = $this->map[$index];
  720. $resultRow[$table][$column] = $row[$index];
  721. $i++;
  722. }
  723. return $resultRow;
  724. } else {
  725. return false;
  726. }
  727. }
  728. /**
  729. * Gets the database encoding
  730. *
  731. * @return string The database encoding
  732. */
  733. function getEncoding() {
  734. return mysql_client_encoding($this->connection);
  735. }
  736. /**
  737. * Query charset by collation
  738. *
  739. * @param string $name Collation name
  740. * @return string Character set name
  741. */
  742. function getCharsetName($name) {
  743. if ((bool)version_compare(mysql_get_server_info($this->connection), "5", ">=")) {
  744. $cols = $this->query('SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.COLLATIONS WHERE COLLATION_NAME= ' . $this->value($name) . ';');
  745. if (isset($cols[0]['COLLATIONS']['CHARACTER_SET_NAME'])) {
  746. return $cols[0]['COLLATIONS']['CHARACTER_SET_NAME'];
  747. }
  748. }
  749. return false;
  750. }
  751. }