Browse Source

Make schema internal methods consistent.

Make methods consistent so that method names can be generated allowing
code to be simpler and more well factored.

Refs #4263
mark_story 11 years ago
parent
commit
2fc00df1d9

+ 13 - 13
src/Database/Schema/BaseSchema.php

@@ -115,7 +115,7 @@ abstract class BaseSchema {
  * @param array $config The connection configuration.
  * @return array An array of (sql, params) to execute.
  */
-	abstract public function describeTableSql($name, $config);
+	abstract public function describeColumnSql($name, $config);
 
 /**
  * Generate the SQL to describe the indexes in a table.
@@ -150,10 +150,10 @@ abstract class BaseSchema {
  * Convert field description results into abstract schema fields.
  *
  * @param \Cake\Database\Schema\Table $table The table object to append fields to.
- * @param array $row The row data from `describeTableSql`.
+ * @param array $row The row data from `describeColumnSql`.
  * @return void
  */
-	abstract public function convertFieldDescription(Table $table, $row);
+	abstract public function convertColumnDescription(Table $table, $row);
 
 /**
  * Convert an index description results into abstract schema indexes or constraints.
@@ -176,6 +176,16 @@ abstract class BaseSchema {
 	abstract public function convertForeignKeyDescription(Table $table, $row);
 
 /**
+ * Convert options data into table options.
+ *
+ * @param \Cake\Database\Schema\Table $table Table instance.
+ * @param array $row The row of data.
+ * @return void
+ */
+	public function convertOptionsDescription(Table $table, $row) {
+	}
+
+/**
  * Generate the SQL to create a table.
  *
  * @param \Cake\Database\Schema\Table $table Table instance.
@@ -221,14 +231,4 @@ abstract class BaseSchema {
  */
 	abstract public function truncateTableSql(Table $table);
 
-/**
- * Convert options data into table options.
- *
- * @param \Cake\Database\Schema\Table $table Table instance.
- * @param array $row The row of data.
- * @return void
- */
-	public function convertOptions(Table $table, $row) {
-	}
-
 }

+ 34 - 47
src/Database/Schema/Collection.php

@@ -107,47 +107,50 @@ class Collection {
 				return $cached;
 			}
 		}
-
+		$table = new Table($name);
 		$config = $this->_connection->config();
-		list($sql, $params) = $this->_dialect->describeTableSql($name, $config);
-		$statement = $this->_executeSql($sql, $params);
-		if (count($statement) === 0) {
+
+		$this->_reflect('Column', $name, $config, $table);
+		if (count($table->columns()) === 0) {
 			throw new Exception(sprintf('Cannot describe %s. It has 0 columns.', $name));
 		}
+		$this->_reflect('Index', $name, $config, $table);
+		$this->_reflect('ForeignKey', $name, $config, $table);
+		$this->_reflect('Options', $name, $config, $table);
 
-		$table = new Table($name);
-		foreach ($statement->fetchAll('assoc') as $row) {
-			$this->_dialect->convertFieldDescription($table, $row);
+		if (!empty($cacheConfig)) {
+			Cache::write($cacheKey, $table, $cacheConfig);
 		}
-		$statement->closeCursor();
+		return $table;
+	}
 
-		list($sql, $params) = $this->_dialect->describeIndexSql($name, $config);
-		$statement = $this->_executeSql($sql, $params);
-		foreach ($statement->fetchAll('assoc') as $row) {
-			$this->_dialect->convertIndexDescription($table, $row);
-		}
-		$statement->closeCursor();
+/**
+ * Helper method for running each step of the reflection process.
+ *
+ * @param string $stage The stage name.
+ * @param string $name The talble name.
+ * @param array $config The config data.
+ * @param \Cake\Database\Schema\Table $table The table instance
+ * @return void
+ * @throws \Cake\Database\Exception on query failure.
+ */
+	protected function _reflect($stage, $name, $config, $table) {
+		$describeMethod = "describe{$stage}Sql";
+		$convertMethod = "convert{$stage}Description";
 
-		list($sql, $params) = $this->_dialect->describeForeignKeySql($name, $config);
-		$statement = $this->_executeSql($sql, $params);
-		foreach ($statement->fetchAll('assoc') as $row) {
-			$this->_dialect->convertForeignKeyDescription($table, $row);
+		list($sql, $params) = $this->_dialect->{$describeMethod}($name, $config);
+		if (empty($sql)) {
+			return;
 		}
-		$statement->closeCursor();
-
-		list($sql, $params) = $this->_dialect->describeOptionsSql($name, $config);
-		if ($sql) {
-			$statement = $this->_executeSql($sql, $params);
-			foreach ($statement->fetchAll('assoc') as $row) {
-				$this->_dialect->convertOptions($table, $row);
-			}
-			$statement->closeCursor();
+		try {
+			$statement = $this->_connection->execute($sql, $params);
+		} catch (\PDOException $e) {
+			throw new Exception($e->getMessage(), 500, $e);
 		}
-
-		if (!empty($cacheConfig)) {
-			Cache::write($cacheKey, $table, $cacheConfig);
+		foreach ($statement->fetchAll('assoc') as $row) {
+			$this->_dialect->{$convertMethod}($table, $row);
 		}
-		return $table;
+		$statement->closeCursor();
 	}
 
 /**
@@ -178,20 +181,4 @@ class Collection {
 		return $this->_cache = $enable;
 	}
 
-/**
- * Helper method to run queries and convert Exceptions to the correct types.
- *
- * @param string $sql The sql to run.
- * @param array $params Parameters for the statement.
- * @return \Cake\Database\StatementInterface Prepared statement
- * @throws \Cake\Database\Exception on query failure.
- */
-	protected function _executeSql($sql, $params) {
-		try {
-			return $this->_connection->execute($sql, $params);
-		} catch (\PDOException $e) {
-			throw new Exception($e->getMessage(), 500, $e);
-		}
-	}
-
 }

+ 3 - 3
src/Database/Schema/MysqlSchema.php

@@ -32,7 +32,7 @@ class MysqlSchema extends BaseSchema {
 /**
  * {@inheritDoc}
  */
-	public function describeTableSql($name, $config) {
+	public function describeColumnSql($name, $config) {
 		return ['SHOW FULL COLUMNS FROM ' . $this->_driver->quoteIdentifier($name), []];
 	}
 
@@ -53,7 +53,7 @@ class MysqlSchema extends BaseSchema {
 /**
  * {@inheritDoc}
  */
-	public function convertOptions(Table $table, $row) {
+	public function convertOptionsDescription(Table $table, $row) {
 		$table->options([
 			'engine' => $row['Engine'],
 			'collation' => $row['Collation'],
@@ -137,7 +137,7 @@ class MysqlSchema extends BaseSchema {
 /**
  * {@inheritDoc}
  */
-	public function convertFieldDescription(Table $table, $row) {
+	public function convertColumnDescription(Table $table, $row) {
 		$field = $this->_convertColumn($row['Type']);
 		$field += [
 			'null' => $row['Null'] === 'YES' ? true : false,

+ 2 - 2
src/Database/Schema/PostgresSchema.php

@@ -34,7 +34,7 @@ class PostgresSchema extends BaseSchema {
 /**
  * {@inheritDoc}
  */
-	public function describeTableSql($name, $config) {
+	public function describeColumnSql($name, $config) {
 		$sql =
 		'SELECT DISTINCT table_schema AS schema, column_name AS name, data_type AS type,
 			is_nullable AS null, column_default AS default,
@@ -124,7 +124,7 @@ class PostgresSchema extends BaseSchema {
 /**
  * {@inheritDoc}
  */
-	public function convertFieldDescription(Table $table, $row) {
+	public function convertColumnDescription(Table $table, $row) {
 		$field = $this->_convertColumn($row['type']);
 
 		if ($field['type'] === 'boolean') {

+ 2 - 2
src/Database/Schema/SqliteSchema.php

@@ -100,7 +100,7 @@ class SqliteSchema extends BaseSchema {
 /**
  * {@inheritDoc}
  */
-	public function describeTableSql($name, $config) {
+	public function describeColumnSql($name, $config) {
 		$sql = sprintf(
 			'PRAGMA table_info(%s)',
 			$this->_driver->quoteIdentifier($name)
@@ -111,7 +111,7 @@ class SqliteSchema extends BaseSchema {
 /**
  * {@inheritDoc}
  */
-	public function convertFieldDescription(Table $table, $row) {
+	public function convertColumnDescription(Table $table, $row) {
 		$field = $this->_convertColumn($row['type']);
 		$field += [
 			'null' => !$row['notnull'],

+ 2 - 2
src/Database/Schema/SqlserverSchema.php

@@ -40,7 +40,7 @@ class SqlserverSchema extends BaseSchema {
 /**
  * {@inheritDoc}
  */
-	public function describeTableSql($name, $config) {
+	public function describeColumnSql($name, $config) {
 		$sql =
 		"SELECT DISTINCT TABLE_SCHEMA AS [schema], COLUMN_NAME AS [name], DATA_TYPE AS [type],
 			IS_NULLABLE AS [null], COLUMN_DEFAULT AS [default],
@@ -135,7 +135,7 @@ class SqlserverSchema extends BaseSchema {
 /**
  * {@inheritDoc}
  */
-	public function convertFieldDescription(Table $table, $row) {
+	public function convertColumnDescription(Table $table, $row) {
 		$field = $this->_convertColumn(
 			$row['type'],
 			$row['char_length'],

+ 2 - 1
tests/TestCase/Database/Schema/MysqlSchemaTest.php

@@ -174,7 +174,7 @@ class MysqlSchemaTest extends TestCase {
 		$table = $this->getMock('Cake\Database\Schema\Table', [], ['table']);
 		$table->expects($this->at(0))->method('addColumn')->with('field', $expected);
 
-		$dialect->convertFieldDescription($table, $field);
+		$dialect->convertColumnDescription($table, $field);
 	}
 
 /**
@@ -378,6 +378,7 @@ SQL;
 		$schema = new SchemaCollection($connection);
 		$result = $schema->describe('schema_articles');
 		$this->assertArrayHasKey('engine', $result->options());
+		$this->assertArrayHasKey('collation', $result->options());
 	}
 
 /**

+ 1 - 1
tests/TestCase/Database/Schema/PostgresSchemaTest.php

@@ -221,7 +221,7 @@ SQL;
 		$table = $this->getMock('Cake\Database\Schema\Table', [], ['table']);
 		$table->expects($this->at(0))->method('addColumn')->with('field', $expected);
 
-		$dialect->convertFieldDescription($table, $field);
+		$dialect->convertColumnDescription($table, $field);
 	}
 
 /**

+ 3 - 3
tests/TestCase/Database/Schema/SqliteSchemaTest.php

@@ -151,7 +151,7 @@ class SqliteSchemaTest extends TestCase {
 		$table = $this->getMock('Cake\Database\Schema\Table', [], ['table']);
 		$table->expects($this->at(0))->method('addColumn')->with('field', $expected);
 
-		$dialect->convertFieldDescription($table, $field);
+		$dialect->convertColumnDescription($table, $field);
 	}
 
 /**
@@ -180,8 +180,8 @@ class SqliteSchemaTest extends TestCase {
 		];
 
 		$table = new \Cake\Database\Schema\Table('table');
-		$dialect->convertFieldDescription($table, $field1);
-		$dialect->convertFieldDescription($table, $field2);
+		$dialect->convertColumnDescription($table, $field1);
+		$dialect->convertColumnDescription($table, $field2);
 		$this->assertEquals(['field1', 'field2'], $table->primaryKey());
 	}
 

+ 1 - 1
tests/TestCase/Database/Schema/SqlserverSchemaTest.php

@@ -254,7 +254,7 @@ SQL;
 		$table = $this->getMock('Cake\Database\Schema\Table', [], ['table']);
 		$table->expects($this->at(0))->method('addColumn')->with('field', $expected);
 
-		$dialect->convertFieldDescription($table, $field);
+		$dialect->convertColumnDescription($table, $field);
 	}
 
 /**