Browse Source

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.
Rachman Chavik 14 years ago
parent
commit
144b556d1f

+ 3 - 3
lib/Cake/Model/CakeSchema.php

@@ -250,7 +250,7 @@ class CakeSchema extends Object {
 				$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 @@ class CakeSchema extends Object {
 									$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 @@ class CakeSchema extends Object {
 					'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);

+ 10 - 0
lib/Cake/Model/Datasource/DataSource.php

@@ -407,6 +407,16 @@ class DataSource extends Object {
 	}
 
 /**
+ * Returns the schema name. Override this in subclasses.
+ *
+ * @return string schema name
+ * @access public
+ */
+	public function getSchemaName() {
+		return false;
+	}
+
+/**
  * Closes the current datasource.
  *
  */

+ 10 - 0
lib/Cake/Model/Datasource/Database/Mysql.php

@@ -685,4 +685,14 @@ class Mysql extends DboSource {
 		}
 		return 'text';
 	}
+
+/**
+ * Gets the schema name
+ *
+ * @return string The schema name
+ */
+	public function getSchemaName() {
+		return $this->config['database'];
+	}
+
 }

+ 17 - 6
lib/Cake/Model/Datasource/Database/Postgres.php

@@ -189,7 +189,7 @@ class Postgres extends DboSource {
  */
 	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 @@ class Postgres extends DboSource {
  */
 	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,7 +301,7 @@ class Postgres extends DboSource {
  * @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;
@@ -309,10 +309,11 @@ class Postgres extends DboSource {
 			$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 @@ class Postgres extends DboSource {
  */
 	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 @@ class Postgres extends DboSource {
 			break;
 		}
 	}
+
+/**
+ * Gets the schema name
+ *
+ * @return string The schema name
+ */
+	function getSchemaName() {
+		return $this->config['schema'];
+	}
+
 }

+ 20 - 4
lib/Cake/Model/Datasource/Database/Sqlite.php

@@ -165,7 +165,7 @@ class Sqlite extends DboSource {
 		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 @@ class Sqlite extends DboSource {
  * @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 @@ class Sqlite extends DboSource {
 	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 @@ class Sqlite extends DboSource {
 				$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 @@ class Sqlite extends DboSource {
  */
 	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 @@ class Sqlite extends DboSource {
 		}
 		return $out;
 	}
+
+/**
+ * Gets the schema name
+ *
+ * @return string The schema name
+ */
+	public function getSchemaName() {
+		return "main"; // Sqlite Datasource does not support multidb
+	}
+
 }

+ 9 - 0
lib/Cake/Model/Datasource/Database/Sqlserver.php

@@ -795,4 +795,13 @@ class Sqlserver extends DboSource {
 		return $out;
 	}
 
+/**
+ * Gets the schema name
+ *
+ * @return string The schema name
+ */
+	public function getSchemaName() {
+		return $this->config['database'];
+	}
+
 }

+ 18 - 2
lib/Cake/Model/Datasource/DboSource.php

@@ -913,19 +913,35 @@ class DboSource extends DataSource {
  *
  * @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 @@ class DboSource extends DataSource {
 
 		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;

+ 10 - 0
lib/Cake/Model/Model.php

@@ -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 @@ class Model extends Object {
 			$this->tablePrefix = $db->config['prefix'];
 		}
 
+		$this->schemaName = $db->getSchemaName();
+
 		if (empty($db) || !is_object($db)) {
 			throw new MissingConnectionException(array('class' => $this->name));
 		}

+ 1 - 1
lib/Cake/TestSuite/Fixture/CakeTestFixture.php

@@ -104,7 +104,7 @@ class CakeTestFixture {
 				}
 				$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'])) {