/** * Process the main SQL file. * * @return $this * * @since 1.0 * @throws \RuntimeException * @throws \UnexpectedValueException */ private function processSql() { // Install. $dbType = $this->getApplication()->get('database.driver'); if ('mysqli' == $dbType) { $dbType = 'mysql'; } $fName = JPATH_ROOT . '/etc/' . $dbType . '.sql'; if (false == file_exists($fName)) { throw new \UnexpectedValueException(sprintf(g11n3t('Install SQL file for %s not found.'), $dbType)); } $sql = file_get_contents($fName); if (false == $sql) { throw new \UnexpectedValueException(g11n3t('SQL file corrupted.')); } $this->out(sprintf(g11n3t('Creating tables from file %s'), realpath($fName)), false); foreach ($this->db->splitSql($sql) as $query) { $q = trim($this->db->replacePrefix($query)); if ('' == trim($q)) { continue; } $this->db->setQuery($q)->execute(); $this->out('.', false); } $this->outOk(); return $this; }
/** * Process the main SQL file. * * @return $this * * @since 1.0 * @throws \RuntimeException * @throws \UnexpectedValueException */ private function processSql() { $fName = JPATH_ROOT . '/etc/schema.sql'; if (!file_exists($fName)) { throw new \UnexpectedValueException('Install SQL file not found.'); } $sql = file_get_contents($fName); if (!$sql) { throw new \UnexpectedValueException('Unable to read SQL file.'); } $this->app->out(sprintf('Creating tables from file %s', realpath($fName)), false); foreach ($this->db->splitSql($sql) as $query) { $q = trim($this->db->replacePrefix($query)); if (trim($q) == '') { continue; } $this->db->setQuery($q)->execute(); $this->app->out('.', false); } $this->app->out("\nFinished!"); return $this; }
/** * Tests the Joomla\Database\DatabaseDriver::splitSql method. * * @param string $sql The SQL string to process * @param array $expected The expected result * * @return void * * @since 1.0 * @dataProvider dataSplitSql */ public function testSplitSql($sql, array $expected) { $this->assertEquals($expected, $this->instance->splitSql($sql), 'splitSql method should split a string of queries into an array.'); }
/** * Tests the Joomla\Database\DatabaseDriver::splitSql method. * * @return void * * @since 1.0 */ public function testSplitSql() { $this->assertThat($this->instance->splitSql('SELECT * FROM #__foo;SELECT * FROM #__bar;'), $this->equalTo(array('SELECT * FROM #__foo;', 'SELECT * FROM #__bar;')), 'splitSql method should split a string of multiple queries into an array.'); }