Skip to content

Commit

Permalink
Add stub methods for describing foreign keys to all schema classes.
Browse files Browse the repository at this point in the history
Describing foreign keys often requires separate logic from indexes. Add
skeleton methods to do that work.
  • Loading branch information
markstory committed Jun 6, 2013
1 parent 1567a50 commit 59685a7
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 9 deletions.
36 changes: 27 additions & 9 deletions lib/Cake/Database/Schema/Collection.php
Expand Up @@ -79,11 +79,7 @@ public function describe($name) {
$name,
$this->_connection->config()
);
try {
$statement = $this->_connection->execute($sql, $params);
} catch (\PDOException $e) {
throw new Exception($e->getMessage(), 500, $e);
}
$statement = $this->_executeSql($sql, $params);
if (count($statement) === 0) {
throw new Exception(__d('cake_dev', 'Cannot describe %s. It has 0 columns.', $name));
}
Expand All @@ -97,15 +93,37 @@ public function describe($name) {
$name,
$this->_connection->config()
);
$statement = $this->_executeSql($sql, $params);
foreach ($statement->fetchAll('assoc') as $row) {
$this->_dialect->convertIndexDescription($table, $row);
}

list($sql, $params) = $this->_dialect->describeForeignKeySql(
$name,
$this->_connection->config()
);
$statement = $this->_executeSql($sql, $params);
foreach ($statement->fetchAll('assoc') as $row) {
$this->_dialect->convertForeignKeyDescription($table, $row);
}
return $table;
}

/**
* 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\Statement Prepared statement
* @throws Cake\Database\Exception on query failure.
*/
protected function _executeSql($sql, $params) {
try {
$statement = $this->_connection->execute($sql, $params);
return $statement;
} catch (\PDOException $e) {
throw new Exception($e->getMessage(), 500, $e);
}
foreach ($statement->fetchAll('assoc') as $row) {
$this->_dialect->convertIndexDescription($table, $row);
}
return $table;
}

}
18 changes: 18 additions & 0 deletions lib/Cake/Database/Schema/MysqlSchema.php
Expand Up @@ -211,6 +211,24 @@ public function convertIndexDescription(Table $table, $row) {
}
}

/**
* Generate the SQL to describe the foreign keys on a table.
*
* @return array List of sql, params
*/
public function describeForeignKeySql($table) {
return ['', []];
}

/**
* Convert a foreign key description into constraints on the Table object.
*
* @param Cake\Database\Table $table The table instance to populate.
* @param array $row The row of data.
* @return void
*/
public function convertForeignKey(Table $table, $row) {
}
/**
* Generate the SQL to truncate a table.
*
Expand Down
19 changes: 19 additions & 0 deletions lib/Cake/Database/Schema/PostgresSchema.php
Expand Up @@ -239,6 +239,25 @@ public function convertIndexDescription(Table $table, $row) {
]);
}

/**
* Generate the SQL to describe the foreign keys on a table.
*
* @return array List of sql, params
*/
public function describeForeignKeySql($table) {
return ['', []];
}

/**
* Convert a foreign key description into constraints on the Table object.
*
* @param Cake\Database\Table $table The table instance to populate.
* @param array $row The row of data.
* @return void
*/
public function convertForeignKey(Table $table, $row) {
}

/**
* Generate the SQL fragment for a single column.
*
Expand Down
19 changes: 19 additions & 0 deletions lib/Cake/Database/Schema/SqliteSchema.php
Expand Up @@ -189,6 +189,25 @@ public function convertIndexDescription(Table $table, $row) {
}
}

/**
* Generate the SQL to describe the foreign keys on a table.
*
* @return array List of sql, params
*/
public function describeForeignKeySql($table) {
return ['', []];
}

/**
* Convert a foreign key description into constraints on the Table object.
*
* @param Cake\Database\Table $table The table instance to populate.
* @param array $row The row of data.
* @return void
*/
public function convertForeignKey(Table $table, $row) {
}

/**
* Generate the SQL fragment for a single column in Sqlite
*
Expand Down

0 comments on commit 59685a7

Please sign in to comment.