/** * Returns an array of all routes from a table to one of its related tables * * @internal * * @param fSchema $schema The schema object to get the routes for * @param string $table The main table we are searching on behalf of * @param string $related_table The related table we are trying to find the routes for * @param string $relationship_type The relationship type: `NULL`, `'*-to-many'`, `'*-to-one'`, `'!many-to-one'`, `'one-to-one'`, `'one-to-many'`, `'many-to-one'`, `'many-to-many'` * @return array All of the routes from the main table to the related table */ public static function getRoutes($schema, $table, $related_table, $relationship_type = NULL) { $key = $table . '::' . $related_table . '::' . $relationship_type; if (isset(self::$cache['getRoutes'][$key])) { return self::$cache['getRoutes'][$key]; } $valid_relationship_types = array(NULL, '*-to-many', '*-to-one', '!many-to-one', 'many-to-many', 'many-to-one', 'one-to-many', 'one-to-one'); if (!in_array($relationship_type, $valid_relationship_types)) { $valid_relationship_types[0] = '{null}'; throw new fProgrammerException('The relationship type specified, %1$s, is invalid. Must be one of: %2$s.', $relationship_type, join(', ', $valid_relationship_types)); } $all_relationships = $schema->getRelationships($table); if (!in_array($related_table, $schema->getTables())) { throw new fProgrammerException('The related table specified, %1$s, does not exist in the database', $related_table); } $routes = array(); foreach ($all_relationships as $type => $relationships) { // Filter the relationships by the relationship type if ($relationship_type !== NULL) { if ($relationship_type == '!many-to-one') { if ($type == 'many-to-one') { continue; } } else { if (strpos($type, str_replace('*', '', $relationship_type)) === FALSE) { continue; } } } foreach ($relationships as $relationship) { if ($relationship['related_table'] == $related_table) { if ($type == 'many-to-many') { $routes[$relationship['join_table']] = $relationship; } elseif ($type == 'one-to-many') { $routes[$relationship['related_column']] = $relationship; } else { $routes[$relationship['column']] = $relationship; } } } } self::$cache['getRoutes'][$key] = $routes; return $routes; }
public function testRenameTable2() { self::$db->translatedQuery('ALTER TABLE "artists" RENAME TO "bands"'); $this->rollback_statements[] = "ALTER TABLE bands RENAME TO artists"; $schema = new fSchema(self::$db); $tables = array('albums', 'bands', 'blobs', 'categories', 'certification_levels', 'certifications', 'event_details', 'event_slots', 'events', 'events_artists', 'favorite_albums'); if (DB_TYPE == 'oracle' || DB_TYPE == 'db2' || DB_TYPE == 'postgresql' || DB_TYPE == 'mssql') { $tables = array_merge($tables, array_map('fix_schema', array('flourish2.albums', 'flourish2.artists', 'flourish2.groups', 'flourish2.users', 'flourish2.users_groups'))); } $tables = array_merge($tables, array('groups', 'invalid_tables', 'other_user_details', 'owns_on_cd', 'owns_on_tape', 'people', 'record_deals', 'record_labels', 'registrations', 'songs', 'top_albums', 'user_details', 'users', 'users_groups', 'year_favorite_albums')); $this->assertSame($tables, $schema->getTables()); $this->assertSame(array(array('column' => 'artist_id', 'foreign_table' => 'bands', 'foreign_column' => 'artist_id', 'on_delete' => 'cascade', 'on_update' => DB_TYPE == 'oracle' || DB_TYPE == 'db2' ? 'no_action' : 'cascade')), $schema->getKeys('albums', 'foreign')); $this->assertSame(array(array('name')), $schema->getKeys('bands', 'unique')); $this->assertEquals(3, self::$db->query("SELECT count(*) FROM bands")->fetchScalar()); }
public function testRenameTable2() { self::$db->translatedQuery('ALTER TABLE "artists" RENAME TO "bands"'); $this->rollback_statements[] = "ALTER TABLE bands RENAME TO artists"; $schema = new fSchema(self::$db); $this->assertSame(array("albums", "bands", "blobs", "groups", "owns_on_cd", "owns_on_tape", "songs", "users", "users_groups"), $schema->getTables()); $this->assertSame(array(array('column' => 'artist_id', 'foreign_table' => 'bands', 'foreign_column' => 'artist_id', 'on_delete' => 'cascade', 'on_update' => DB_TYPE == 'oracle' || DB_TYPE == 'db2' ? 'no_action' : 'cascade')), $schema->getKeys('albums', 'foreign')); $this->assertSame(array(array('name')), $schema->getKeys('bands', 'unique')); $this->assertEquals(3, self::$db->query("SELECT count(*) FROM bands")->fetchScalar()); }
/** * Takes a table name, cleans off quoting and removes the schema name if unambiguous * * @param fSchema $schema The schema object for the database being inspected * @param string $table The table name to be made cleaned * @return string The cleaned table name */ private static function cleanTableName($schema, $table) { $table = str_replace('"', '', $table); $tables = array_flip($schema->getTables()); if (!isset($tables[$table])) { $short_table = preg_replace('#^\\w\\.#', '', $table); if (isset($tables[$short_table])) { $table = $short_table; } } return $table; }
GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Print Master. If not, see <http://www.gnu.org/licenses/>. */ /* Database patch script for PrintMaster. If this is the first time being run, the patch history table is created. When it exists, it executes the required patch scripts in the ./db folder. */ // Include initialisation file include_once 'inc/core.php'; // Get DB schema so we can find out if the table exists. $schema = new fSchema($db); $tables = $schema->getTables(); // If table does not exist - create it and insert default entry. if (!array_search('patch_history', $tables)) { echo "Initialising patch history table...<br>"; $sql = "CREATE TABLE `patch_history` ( `num` smallint(5) unsigned NOT NULL) ENGINE='MyISAM' COLLATE 'utf8_unicode_ci'"; $db->query($sql); $sql = "ALTER TABLE `patch_history` ADD PRIMARY KEY `num` (`num`)"; $db->query($sql); $sql = "INSERT INTO `patch_history` SET `num` = 0"; $db->query($sql); } // Get the last patch level $sql = "SELECT MAX(num) AS num FROM patch_history"; $row = $db->query($sql)->fetchRow(); $num = (int) $row['num']; // Next patch to install