con = $db; $this->model = $model; $this->schema = Pluf::factory('Pluf_DB_Schema_'.$db->engine, $db); } /** * Get the schema generator. * * @return object Pluf_DB_Schema_XXXX */ function getGenerator() { return $this->schema; } /** * Create the tables and indexes for the current model. * * @return mixed True if success or database error. */ function createTables() { $sql = $this->schema->getSqlCreate($this->model); foreach ($sql as $k => $query) { if (false === $this->con->execute($query)) { throw new Exception($this->con->getError()); } } $sql = $this->schema->getSqlIndexes($this->model); foreach ($sql as $k => $query) { if (false === $this->con->execute($query)) { throw new Exception($this->con->getError()); } } return true; } /** * Creates the constraints for the current model. * This should be done _after_ all tables of all models have been created. * * @throws Exception */ function createConstraints() { $sql = $this->schema->getSqlCreateConstraints($this->model); foreach ($sql as $k => $query) { if (false === $this->con->execute($query)) { throw new Exception($this->con->getError()); } } } /** * Drop the tables and indexes for the current model. * * @return mixed True if success or database error. */ function dropTables() { $sql = $this->schema->getSqlDelete($this->model); foreach ($sql as $k => $query) { if (false === $this->con->execute($query)) { throw new Exception($this->con->getError()); } } return true; } /** * Drops the constraints for the current model. * This should be done _before_ all tables of all models are dropped. * * @throws Exception * @return boolean */ function dropConstraints() { $sql = $this->schema->getSqlDeleteConstraints($this->model); foreach ($sql as $k => $query) { if (false === $this->con->execute($query)) { throw new Exception($this->con->getError()); } } return true; } /** * Given a column name or a string with column names in the format * "column1, column2, column3", returns the escaped correctly * quoted column names. This is good for index creation. * * @param string Column * @param Pluf_DB DB handler * @return string Quoted for the DB column(s) */ public static function quoteColumn($col, $db) { if (false !== strpos($col, ',')) { $cols = explode(',', $col); } else { $cols = array($col); } $res = array(); foreach ($cols as $col) { $res[] = $db->qn(trim($col)); } return implode(', ', $res); } }