connection = $connection; $this->tableName = $configResolver ? $configResolver->getParameter($tableNameParameter) : $tableNameParameter; } abstract function createTable(); /** * @return \Doctrine\DBAL\Connection */ protected function getConnection() { return $this->connection; } /** * Check if a table exists in the database * * @param string $tableName * @return bool */ protected function tableExist($tableName) { /** @var \Doctrine\DBAL\Schema\AbstractSchemaManager $sm */ $sm = $this->getConnection()->getSchemaManager(); foreach ($sm->listTables() as $table) { if ($table->getName() == $tableName) { return true; } } return false; } /** * Check if the version db table exists and create it if not. * * @return bool true if table has been created, false if it was already there * * @todo add a 'force' flag to force table re-creation * @todo manage changes to table definition */ protected function createTableIfNeeded() { if ($this->tableExists) { return false; } if ($this->tableExist($this->tableName)) { $this->tableExists = true; return false; } $this->createTable(); $this->tableExists = true; return true; } /** * Removes all data from storage as well as removing the tables itself */ protected function drop() { if ($this->tableExist($this->tableName)) { $this->connection->exec('DROP TABLE ' . $this->tableName); } } /** * Removes all data from storage */ public function truncate() { if ($this->tableExist($this->tableName)) { $this->connection->exec('TRUNCATE ' . $this->tableName); } } }