public function createDb() { $this->options = array('host' => $this->config->get('host'), 'user' => $this->config->get('user'), 'password' => $this->config->get('password'), 'database' => $this->config->get('db'), 'prefix' => $this->config->get('dbprefix'), 'select' => false); $dbFactory = new DatabaseFactory(); $this->db = $dbFactory->getDriver($this->config->get('dbtype'), $this->options); // Try to select the database try { $this->db->select($this->options['database']); } catch (RuntimeException $e) { // Get database's UTF support $utfSupport = $this->db->hasUTFSupport(); $createDbConfig = array('db_name' => $this->options['database'], 'db_user' => $this->options['user']); // If the database could not be selected, attempt to create it and then select it. if ($this->createDatabase($this->db, ArrayHelper::toObject($createDbConfig), $utfSupport)) { $this->db->select($this->options['database']); } else { return false; } } $db_driver = $this->config->get('dbtype'); if ($db_driver == 'mysqli') { $db_driver = 'mysql'; } $schema = JPATH_INSTALLATION . "/sql/" . $db_driver . "/joomla.sql"; // Get the contents of the schema file. if (!($buffer = file_get_contents($schema))) { $this->setError($this->db->getErrorMsg()); return false; } // Get an array of queries from the schema and process them. $queries = $this->_splitQueries($buffer); foreach ($queries as $query) { // Trim any whitespace. $query = trim($query); // If the query isn't empty and is not a MySQL or PostgreSQL comment, execute it. if (!empty($query) && $query[0] != '#' && $query[0] != '-') { // Execute the query. $this->db->setQuery($query); try { $this->db->execute(); } catch (RuntimeException $e) { $this->setError($e->getMessage()); $return = false; } } } return true; }
/** * createDbo * * @param array $option * * @return DatabaseDriver */ public static function createDbo(array $option) { $dbFactory = \Joomla\Database\DatabaseFactory::getInstance(); $option['driver'] = !empty($option['driver']) ? $option['driver'] : 'mysql'; return $dbFactory->getDriver($option['driver'], $option); }
/** * Test for the Joomla\Database\DatabaseFactory::getInstance method. * * @return void * * @since 1.0 */ public function testGetInstance() { $this->assertThat(DatabaseFactory::getInstance(), $this->isInstanceOf('\\Joomla\\Database\\DatabaseFactory'), 'Tests that getInstance returns an instance of DatabaseFactory.'); }