/** * Runs the drop and create commands on the fixtures if necessary. * * @param \Cake\TestSuite\Fixture\TestFixture $fixture the fixture object to create * @param Connection $db the datasource instance to use * @param array $sources The existing tables in the datasource. * @param bool $drop whether drop the fixture if it is already created or not * @return void */ protected function _setupTable(TestFixture $fixture, Connection $db, array $sources, $drop = true) { if (!empty($fixture->created) && in_array($db->configName(), $fixture->created)) { return; } $table = $fixture->table; $exists = in_array($table, $sources); if ($drop && $exists) { $fixture->drop($db); $fixture->create($db); } elseif (!$exists) { $fixture->create($db); } else { $fixture->created[] = $db->configName(); $fixture->truncate($db); } }
/** * Runs the drop and create commands on the fixtures if necessary. * * @param \Cake\TestSuite\Fixture\TestFixture $fixture the fixture object to create * @param Connection $db the datasource instance to use * @param array $sources The existing tables in the datasource. * @param bool $drop whether drop the fixture if it is already created or not * @return void */ protected function _setupTable($fixture, $db, array $sources, $drop = true) { $configName = $db->configName(); if ($this->isFixtureSetup($configName, $fixture)) { return; } $table = $fixture->sourceName(); $exists = in_array($table, $sources); if ($drop && $exists) { $fixture->drop($db); $fixture->create($db); } elseif (!$exists) { $fixture->create($db); } else { $this->_insertionMap[$configName][] = $fixture; $fixture->truncate($db); } }
/** * Runs the drop and create commands on the fixtures if necessary. * * @param \Cake\TestSuite\Fixture\TestFixture $fixture the fixture object to create * @param \Cake\Database\Connection $db The Connection object instance to use * @param array $sources The existing tables in the datasource. * @param bool $drop whether drop the fixture if it is already created or not * @return void */ protected function _setupTable($fixture, $db, array $sources, $drop = true) { $configName = $db->configName(); $isFixtureSetup = $this->isFixtureSetup($configName, $fixture); if ($isFixtureSetup) { return; } $table = $fixture->sourceName(); $exists = in_array($table, $sources); if ($drop && $exists || $exists && !$isFixtureSetup && $fixture instanceof TableSchemaInterface && $fixture->schema() instanceof Table) { $fixture->drop($db); $fixture->create($db); } elseif (!$exists) { $fixture->create($db); } else { $fixture->truncate($db); } $this->_insertionMap[$configName][] = $fixture; }
/** * Get the column metadata for a table. * * Caching will be applied if `cacheMetadata` key is present in the Connection * configuration options. Defaults to _cake_model_ when true. * * @param string $name The name of the table to describe. * @return \Cake\Database\Schema\Table Object with column metadata. * @throws \Cake\Database\Exception when table cannot be described. */ public function describe($name) { $cacheConfig = $this->cacheMetadata(); if ($cacheConfig) { $cacheKey = $this->_connection->configName() . '_' . $name; $cached = Cache::read($cacheKey, $cacheConfig); if ($cached !== false) { return $cached; } } $config = $this->_connection->config(); list($sql, $params) = $this->_dialect->describeTableSql($name, $config); $statement = $this->_executeSql($sql, $params); if (count($statement) === 0) { throw new Exception(sprintf('Cannot describe %s. It has 0 columns.', $name)); } $table = new Table($name); foreach ($statement->fetchAll('assoc') as $row) { $this->_dialect->convertFieldDescription($table, $row); } list($sql, $params) = $this->_dialect->describeIndexSql($name, $config); $statement = $this->_executeSql($sql, $params); foreach ($statement->fetchAll('assoc') as $row) { $this->_dialect->convertIndexDescription($table, $row); } list($sql, $params) = $this->_dialect->describeForeignKeySql($name, $config); $statement = $this->_executeSql($sql, $params); foreach ($statement->fetchAll('assoc') as $row) { $this->_dialect->convertForeignKeyDescription($table, $row); } $statement->closeCursor(); if (!empty($cacheConfig)) { Cache::write($cacheKey, $table, $cacheConfig); } return $table; }