Permalink
Browse files

Adding new property Model::schemaName

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...
1 parent 7e790aa commit 144b556d1f535732adc0f73df7f06f8f190d2e2b @rchavik rchavik committed Nov 5, 2011
@@ -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;
}
@@ -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;
}
@@ -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);
@@ -407,6 +407,16 @@ public function resolveKey(Model $model, $key) {
}
/**
+ * Returns the schema name. Override this in subclasses.
+ *
+ * @return string schema name
+ * @access public
+ */
+ public function getSchemaName() {
+ return false;
+ }
+
+/**
* Closes the current datasource.
*
*/
@@ -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'];
+ }
+
}
@@ -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;
@@ -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];
@@ -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;
@@ -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
@@ -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'];
+ }
+
}
@@ -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 . ')');
@@ -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));
}
@@ -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') {
@@ -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;
@@ -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 . ')');
@@ -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
+ }
+
}
@@ -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'];
+ }
+
}
@@ -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;
}
@@ -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;
@@ -89,6 +89,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.
*
* @var string
@@ -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));
}
@@ -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'])) {

0 comments on commit 144b556

Please sign in to comment.