Created
April 15, 2010 05:16
-
-
Save slywalker/366703 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::import('Core', 'DboMysql'); | |
// var $default = array( | |
// 'driver' => 'mysql_innodb', | |
// 'persistent' => false, | |
// 'host' => 'localhost', | |
// 'login' => 'user', | |
// 'password' => 'password', | |
// 'database' => 'database_name', | |
// 'prefix' => '', | |
// 'encoding' => 'utf8', | |
// 'ignore' => array('field_name'), | |
// ); | |
class DboMysqlInnodb extends DboMysql { | |
/** | |
* Returns an array of the fields in given table name. | |
* | |
* @param string $tableName Name of database table to inspect | |
* @return array Fields in table. Keys are name and type | |
*/ | |
function describe(&$model) { | |
$cache = DataSource::describe($model); | |
if ($cache != null) { | |
return $cache; | |
} | |
$fields = false; | |
$cols = $this->query('DESCRIBE ' . $this->fullTableName($model)); | |
foreach ($cols as $column) { | |
$colKey = array_keys($column); | |
if (isset($column[$colKey[0]]) && !isset($column[0])) { | |
$column[0] = $column[$colKey[0]]; | |
} | |
if (isset($column[0])) { | |
if (!empty($this->config['ignore']) && in_array($column[0]['Field'], $this->config['ignore'])) { | |
continue; | |
} | |
$fields[$column[0]['Field']] = array( | |
'type' => $this->column($column[0]['Type']), | |
'null' => ($column[0]['Null'] == 'YES' ? true : false), | |
'default' => $column[0]['Default'], | |
'length' => $this->length($column[0]['Type']), | |
); | |
if (!empty($column[0]['Key']) && isset($this->index[$column[0]['Key']])) { | |
$fields[$column[0]['Field']]['key'] = $this->index[$column[0]['Key']]; | |
} | |
} | |
} | |
$this->__cacheDescription($this->fullTableName($model, false), $fields); | |
return $fields; | |
} | |
/** | |
* Returns an array of the indexes in given datasource name. | |
* | |
* @param string $model Name of model to inspect | |
* @return array Fields in table. Keys are column and unique | |
*/ | |
function index($model) { | |
$index = array(); | |
$table = $this->fullTableName($model); | |
if ($table) { | |
$indexes = $this->query('SHOW INDEX FROM ' . $table); | |
if (isset($indexes[0]['STATISTICS'])) { | |
$keys = Set::extract($indexes, '{n}.STATISTICS'); | |
} else { | |
$keys = Set::extract($indexes, '{n}.0'); | |
} | |
foreach ($keys as $i => $key) { | |
if (!empty($this->config['ignore']) && in_array($key['Column_name'], $this->config['ignore'])) { | |
continue; | |
} | |
if (!isset($index[$key['Key_name']])) { | |
$col = array(); | |
$index[$key['Key_name']]['column'] = $key['Column_name']; | |
$index[$key['Key_name']]['unique'] = intval($key['Non_unique'] == 0); | |
} else { | |
if (!is_array($index[$key['Key_name']]['column'])) { | |
$col[] = $index[$key['Key_name']]['column']; | |
} | |
$col[] = $key['Column_name']; | |
$index[$key['Key_name']]['column'] = $col; | |
} | |
} | |
} | |
return $index; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment