Skip to content

Commit

Permalink
Adding new property Model::schemaName
Browse files Browse the repository at this point in the history
Just like physical table name, the schema name is an important information
that should be available in models.  The property will be populated accordingly
by the model's datasource.
  • Loading branch information
rchavik committed Nov 25, 2011
1 parent 7e790aa commit 144b556
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 16 deletions.
6 changes: 3 additions & 3 deletions lib/Cake/Model/CakeSchema.php
Expand Up @@ -250,7 +250,7 @@ public function read($options = array()) {
$Object = ClassRegistry::init(array('class' => $model, 'ds' => $connection));
$db = $Object->getDataSource();
if (is_object($Object) && $Object->useTable !== false) {
$fulltable = $table = $db->fullTableName($Object, false);
$fulltable = $table = $db->fullTableName($Object, false, false);
if ($prefix && strpos($table, $prefix) !== 0) {
continue;
}
Expand All @@ -270,7 +270,7 @@ public function read($options = array()) {
$class = $assocData['with'];
}
if (is_object($Object->$class)) {
$withTable = $db->fullTableName($Object->$class, false);
$withTable = $db->fullTableName($Object->$class, false, false);
if ($prefix && strpos($withTable, $prefix) !== 0) {
continue;
}
Expand Down Expand Up @@ -307,7 +307,7 @@ public function read($options = array()) {
'aros', 'acos', 'aros_acos', Configure::read('Session.table'), 'i18n'
);

$fulltable = $db->fullTableName($Object, false);
$fulltable = $db->fullTableName($Object, false, false);

if (in_array($table, $systemTables)) {
$tables[$Object->table] = $this->_columns($Object);
Expand Down
10 changes: 10 additions & 0 deletions lib/Cake/Model/Datasource/DataSource.php
Expand Up @@ -406,6 +406,16 @@ public function resolveKey(Model $model, $key) {
return $model->alias . $key;
}

/**
* Returns the schema name. Override this in subclasses.
*
* @return string schema name
* @access public
*/
public function getSchemaName() {
return false;
}

/**
* Closes the current datasource.
*
Expand Down
10 changes: 10 additions & 0 deletions lib/Cake/Model/Datasource/Database/Mysql.php
Expand Up @@ -685,4 +685,14 @@ public function column($real) {
}
return 'text';
}

/**
* Gets the schema name
*
* @return string The schema name
*/
public function getSchemaName() {
return $this->config['database'];
}

}
23 changes: 17 additions & 6 deletions lib/Cake/Model/Datasource/Database/Postgres.php
Expand Up @@ -189,7 +189,7 @@ public function listSources($data = null) {
*/
public function describe($model) {
$fields = parent::describe($model);
$table = $this->fullTableName($model, false);
$table = $this->fullTableName($model, false, false);
$this->_sequenceMap[$table] = array();
$cols = null;

Expand Down Expand Up @@ -283,7 +283,7 @@ public function lastInsertId($source = null, $field = 'id') {
*/
public function getSequence($table, $field = 'id') {
if (is_object($table)) {
$table = $this->fullTableName($table, false);
$table = $this->fullTableName($table, false, false);
}
if (isset($this->_sequenceMap[$table]) && isset($this->_sequenceMap[$table][$field])) {
return $this->_sequenceMap[$table][$field];
Expand All @@ -301,18 +301,19 @@ public function getSequence($table, $field = 'id') {
* @return boolean SQL TRUNCATE TABLE statement, false if not applicable.
*/
public function truncate($table, $reset = 0) {
$table = $this->fullTableName($table, false);
$table = $this->fullTableName($table, false, false);
if (!isset($this->_sequenceMap[$table])) {
$cache = $this->cacheSources;
$this->cacheSources = false;
$this->describe($table);
$this->cacheSources = $cache;
}
if ($this->execute('DELETE FROM ' . $this->fullTableName($table))) {
$table = $this->fullTableName($table, false);
$schema = $this->config['schema'];
$table = $this->fullTableName($table, false, false);
if (isset($this->_sequenceMap[$table]) && $reset !== 1) {
foreach ($this->_sequenceMap[$table] as $field => $sequence) {
$this->_execute("ALTER SEQUENCE \"{$sequence}\" RESTART WITH 1");
$this->_execute("ALTER SEQUENCE \"{$schema}\".\"{$sequence}\" RESTART WITH 1");
}
}
return true;
Expand Down Expand Up @@ -426,7 +427,7 @@ protected function _quoteFunctionField($match) {
*/
public function index($model) {
$index = array();
$table = $this->fullTableName($model, false);
$table = $this->fullTableName($model, false, false);
if ($table) {
$indexes = $this->query("SELECT c2.relname, i.indisprimary, i.indisunique, i.indisclustered, i.indisvalid, pg_catalog.pg_get_indexdef(i.indexrelid, 0, true) as statement, c2.reltablespace
FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i
Expand Down Expand Up @@ -887,4 +888,14 @@ public function renderStatement($type, $data) {
break;
}
}

/**
* Gets the schema name
*
* @return string The schema name
*/
function getSchemaName() {
return $this->config['schema'];
}

}
24 changes: 20 additions & 4 deletions lib/Cake/Model/Datasource/Database/Sqlite.php
Expand Up @@ -165,7 +165,7 @@ public function describe($model) {
if ($cache != null) {
return $cache;
}
$table = $this->fullTableName($model);
$table = $this->fullTableName($model, false, false);
$fields = array();
$result = $this->_execute('PRAGMA table_info(' . $table . ')');

Expand Down Expand Up @@ -224,7 +224,7 @@ public function update(Model $model, $fields = array(), $values = null, $conditi
* @return boolean SQL TRUNCATE TABLE statement, false if not applicable.
*/
public function truncate($table) {
$this->_execute('DELETE FROM sqlite_sequence where name=' . $this->fullTableName($table));
$this->_execute('DELETE FROM sqlite_sequence where name=' . $this->startQuote . $this->fullTableName($table, false, false) . $this->endQuote);
return $this->execute('DELETE FROM ' . $this->fullTableName($table));
}

Expand Down Expand Up @@ -431,6 +431,10 @@ public function getEncoding() {
public function buildIndex($indexes, $table = null) {
$join = array();

$table = str_replace('"', '', $table);
list($dbname, $table) = explode('.', $table);
$dbname = $this->name($dbname);

foreach ($indexes as $name => $value) {

if ($name == 'PRIMARY') {
Expand All @@ -447,7 +451,9 @@ public function buildIndex($indexes, $table = null) {
$value['column'] = $this->name($value['column']);
}
$t = trim($table, '"');
$out .= "INDEX {$t}_{$name} ON {$table}({$value['column']});";
$indexname = $this->name($t . '_' .$name);
$table = $this->name($table);
$out .= "INDEX {$dbname}.{$indexname} ON {$table}({$value['column']});";
$join[] = $out;
}
return $join;
Expand All @@ -462,7 +468,7 @@ public function buildIndex($indexes, $table = null) {
*/
public function index($model) {
$index = array();
$table = $this->fullTableName($model);
$table = $this->fullTableName($model, false, false);
if ($table) {
$indexes = $this->query('PRAGMA index_list(' . $table . ')');

Expand Down Expand Up @@ -544,4 +550,14 @@ public function dropSchema(CakeSchema $schema, $table = null) {
}
return $out;
}

/**
* Gets the schema name
*
* @return string The schema name
*/
public function getSchemaName() {
return "main"; // Sqlite Datasource does not support multidb
}

}
9 changes: 9 additions & 0 deletions lib/Cake/Model/Datasource/Database/Sqlserver.php
Expand Up @@ -795,4 +795,13 @@ public function dropSchema(CakeSchema $schema, $table = null) {
return $out;
}

/**
* Gets the schema name
*
* @return string The schema name
*/
public function getSchemaName() {
return $this->config['database'];
}

}
20 changes: 18 additions & 2 deletions lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -913,19 +913,35 @@ public function logQuery($sql) {
*
* @param mixed $model Either a Model object or a string table name.
* @param boolean $quote Whether you want the table name quoted.
* @param boolean $schema Whether you want the schema name included.
* @return string Full quoted table name
*/
public function fullTableName($model, $quote = true) {
public function fullTableName($model, $quote = true, $schema = true) {
if (is_object($model)) {
$schemaName = $model->schemaName;
$table = $model->tablePrefix . $model->table;
} elseif (isset($this->config['prefix'])) {
$table = $this->config['prefix'] . strval($model);
} else {
$table = strval($model);
}
if ($schema && !isset($schemaName)) {
$schemaName = $this->getSchemaName();
}

if ($quote) {
if ($schema && isset($schemaName)) {
if (false == strstr($table, '.')) {
return $this->name($schemaName) . '.' . $this->name($table);
}
}
return $this->name($table);
}
if ($schema && isset($schemaName)) {
if (false == strstr($table, '.')) {
return $schemaName . '.' . $table;
}
}
return $table;
}

Expand Down Expand Up @@ -966,7 +982,7 @@ public function create(Model $model, $fields = null, $values = null) {

if ($this->execute($this->renderStatement('create', $query))) {
if (empty($id)) {
$id = $this->lastInsertId($this->fullTableName($model, false), $model->primaryKey);
$id = $this->lastInsertId($this->fullTableName($model, false, false), $model->primaryKey);
}
$model->setInsertID($id);
$model->id = $id;
Expand Down
10 changes: 10 additions & 0 deletions lib/Cake/Model/Model.php
Expand Up @@ -88,6 +88,14 @@ class Model extends Object {
*/
public $data = array();

/**
* Holds physical schema/database name for this model. Automatically set during Model creation.
*
* @var string
* @access public
*/
public $schemaName = null;

/**
* Table name for this Model.
*
Expand Down Expand Up @@ -3223,6 +3231,8 @@ public function setDataSource($dataSource = null) {
$this->tablePrefix = $db->config['prefix'];
}

$this->schemaName = $db->getSchemaName();

if (empty($db) || !is_object($db)) {
throw new MissingConnectionException(array('class' => $this->name));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/TestSuite/Fixture/CakeTestFixture.php
Expand Up @@ -104,7 +104,7 @@ public function init() {
}
$this->fields = $model->schema(true);
$this->fields[$model->primaryKey]['key'] = 'primary';
$this->table = $db->fullTableName($model, false);
$this->table = $db->fullTableName($model, false, false);
ClassRegistry::config(array('ds' => 'test'));
ClassRegistry::flush();
} elseif (isset($import['table'])) {
Expand Down

0 comments on commit 144b556

Please sign in to comment.