本文整理汇总了PHP中JDatabaseDriver::splitSql方法的典型用法代码示例。如果您正苦于以下问题:PHP JDatabaseDriver::splitSql方法的具体用法?PHP JDatabaseDriver::splitSql怎么用?PHP JDatabaseDriver::splitSql使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JDatabaseDriver
的用法示例。
在下文中一共展示了JDatabaseDriver::splitSql方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: execute
/**
* Method to execute the controller.
*
* @throws RuntimeException
*
* @return bool|void
*/
public function execute()
{
// Get the application database object.
$db = JFactory::getDBO();
// Get the installation database schema split into individual queries.
switch ($db->name) {
case 'sqlite':
$queries = JDatabaseDriver::splitSql(file_get_contents(dirname(JPATH_BASE) . '/database/schema/sqlite/install.sql'));
break;
case 'mysql':
case 'mysqli':
$queries = JDatabaseDriver::splitSql(file_get_contents(dirname(JPATH_BASE) . '/database/schema/mysql/install.sql'));
break;
default:
throw new RuntimeException(sprintf('Database engine %s is not supported.', $db->name));
break;
}
// Execute the installation schema queries.
foreach ($queries as $query) {
if ('' == trim($query)) {
continue;
}
$db->setQuery($query)->execute();
}
/* @var ECR_CLASS_PREFIXApplicationWeb $application */
$application = JFactory::getApplication();
$application->addMessage('Your database has been created', 'success');
JFactory::getApplication()->input->set('view', 'list');
JLog::add('The database has been created');
}
示例2: testGetInstanceReturnsTheCorrectObject
/**
* @testdox getInstance() returns the correct object
*
* @param string $dbDriver The database driver to be mocked
* @param string $itemSubclass The subclass of JSchemaChangeitem that is expected
* @param string $dbFolder The name of the folder where the stubs are located
*
* @covers JSchemaChangeitem::__construct
* @covers JSchemaChangeitem::getInstance
*
* @dataProvider dataGetInstance
*/
public function testGetInstanceReturnsTheCorrectObject($dbDriver, $itemSubclass, $dbFolder)
{
$file = __DIR__ . '/stubs/' . $dbFolder . '/3.5.0-2016-03-01.sql';
$dbo = $this->getMockDatabase($dbDriver);
$queries = JDatabaseDriver::splitSql(file_get_contents($file));
$item = JSchemaChangeitem::getInstance($dbo, $file, $queries[0]);
$this->assertInstanceOf('JSchemaChangeitem' . $itemSubclass, $item, 'The correct JSchemaChangeitem subclass was not instantiated');
}
示例3: testSplitSql
/**
* Tests the JDatabaseDriver::splitSql method.
*
* @return void
*
* @since 12.1
*/
public function testSplitSql()
{
$this->assertThat(
$this->db->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.'
);
}
示例4: restore
public function restore()
{
if (!JSession::checkToken()) {
$this->response->errors[] = JText::_('JINVALID_TOKEN');
$this->response->failed = 1;
} else {
// database
$db = JFactory::getDbo();
// Delete v3 tables
$sql = JPATH_SITE . '/administrator/components/com_k2/uninstall.sql';
$queries = JDatabaseDriver::splitSql(file_get_contents($sql));
foreach ($queries as $query) {
$query = trim($query);
if ($query != '' && $query[0] != '#') {
$db->setQuery($query);
$db->execute();
}
}
// Restore v2 tables
$tables = array('#__k2_v2_attachments', '#__k2_v2_categories', '#__k2_v2_comments', '#__k2_v2_extra_fields', '#__k2_v2_extra_fields_groups', '#__k2_v2_items', '#__k2_v2_rating', '#__k2_v2_tags', '#__k2_v2_tags_xref', '#__k2_v2_users', '#__k2_v2_user_groups');
foreach ($tables as $table) {
$name = str_replace('#__k2_v2_', '#__k2_', $table);
$db->setQuery('DROP TABLE IF EXISTS ' . $db->quoteName($name));
$db->execute();
$db->setQuery('RENAME TABLE ' . $db->quoteName($table) . ' TO ' . $db->quoteName($name));
$db->execute();
}
// Restore component files manually to keep any custom templates
if (JFolder::exists(JPATH_SITE . '/components/com_k2')) {
JFolder::delete(JPATH_SITE . '/components/com_k2');
JFolder::move(JPATH_SITE . '/components/com_k2_v2', JPATH_SITE . '/components/com_k2');
}
if (JFolder::exists(JPATH_ADMINISTRATOR . '/components/com_k2')) {
JFolder::delete(JPATH_ADMINISTRATOR . '/components/com_k2');
JFolder::move(JPATH_ADMINISTRATOR . '/components/com_k2_v2', JPATH_ADMINISTRATOR . '/components/com_k2');
}
// Install K2 v2 package to restore rest extension files
$installer = JInstaller::getInstance();
$file = JInstallerHelper::downloadPackage('http://getk2.org/downloads/?f=K2_v2.6.9.zip');
$config = JFactory::getConfig();
$package = JInstallerHelper::unpack($config->get('tmp_path') . '/' . $file, true);
$installer->install($package['dir']);
}
echo json_encode($this->response);
return $this;
}
示例5: getUpdateQueries
/**
* Get array of SQL queries
*
* @param array $sqlfiles Array of .sql update filenames.
*
* @return array Array of stdClass objects where:
* file=filename,
* update_query = text of SQL update query
*
* @since 2.5
*/
private function getUpdateQueries(array $sqlfiles)
{
// Hold results as array of objects
$result = array();
foreach ($sqlfiles as $file) {
$buffer = file_get_contents($file);
// Create an array of queries from the sql file
$queries = $this->db->splitSql($buffer);
foreach ($queries as $query) {
if (trim($query)) {
$fileQueries = new stdClass();
$fileQueries->file = $file;
$fileQueries->updateQuery = $query;
$result[] = $fileQueries;
}
}
}
return $result;
}
示例6: getInserted
/**
* Method to insert the values from the .sql file
* @param unknown_type $tablename
* @return boolean
*/
public function getInserted($tablename)
{
$db = JFactory::getDbo();
$status = true;
//Force parsing of SQL file since Joomla! does that only in install mode, not in upgrades
$sql = JPATH_ADMINISTRATOR . '/components/com_j2store/sql/install/mysql/' . $tablename . '.sql';
$queries = JDatabaseDriver::splitSql(file_get_contents($sql));
foreach ($queries as $query) {
$query = trim($query);
if ($query != '' && $query[0] != '#') {
$db->setQuery($query);
if (!$db->execute()) {
$application->enqueueMessage(JText::sprintf('JLIB_INSTALLER_ERROR_SQL_ERROR', $db->stderr(true)), 'error');
$status = false;
}
}
}
return $status;
}
示例7: parseSchemaUpdates
/**
* Method to process the updates for an item
*
* @param SimpleXMLElement $schema The XML node to process
* @param integer $eid Extension Identifier
*
* @return boolean Result of the operations
*
* @since 3.1
*/
public function parseSchemaUpdates(SimpleXMLElement $schema, $eid)
{
$update_count = 0;
// Ensure we have an XML element and a valid extension id
if ($eid && $schema) {
$db = JFactory::getDbo();
$schemapaths = $schema->children();
if (count($schemapaths)) {
$dbDriver = strtolower($db->name);
if ($dbDriver == 'mysqli' || $dbDriver == 'pdomysql') {
$dbDriver = 'mysql';
}
$schemapath = '';
foreach ($schemapaths as $entry) {
$attrs = $entry->attributes();
// Assuming that the type is a mandatory attribute but if it is not mandatory then there should be a discussion for it.
$uDriver = strtolower($attrs['type']);
if ($uDriver == 'mysqli' || $uDriver == 'pdomysql') {
$uDriver = 'mysql';
}
if ($uDriver == $dbDriver) {
$schemapath = $entry;
break;
}
}
if (strlen($schemapath)) {
$files = str_replace('.sql', '', JFolder::files($this->getPath('extension_root') . '/' . $schemapath, '\\.sql$'));
usort($files, 'version_compare');
if (!count($files)) {
return false;
}
$query = $db->getQuery(true)->select('version_id')->from('#__schemas')->where('extension_id = ' . $eid);
$db->setQuery($query);
$version = $db->loadResult();
// No version - use initial version.
if (!$version) {
$version = '0.0.0';
}
foreach ($files as $file) {
if (version_compare($file, $version) > 0) {
$buffer = file_get_contents($this->getPath('extension_root') . '/' . $schemapath . '/' . $file . '.sql');
// Graceful exit and rollback if read not successful
if ($buffer === false) {
JLog::add(JText::sprintf('JLIB_INSTALLER_ERROR_SQL_READBUFFER'), JLog::WARNING, 'jerror');
return false;
}
// Create an array of queries from the sql file
$queries = JDatabaseDriver::splitSql($buffer);
if (count($queries) == 0) {
// No queries to process
continue;
}
// Process each query in the $queries array (split out of sql file).
foreach ($queries as $query) {
$query = trim($query);
if ($query != '' && $query[0] != '#') {
$db->setQuery($query);
if (!$db->execute()) {
JLog::add(JText::sprintf('JLIB_INSTALLER_ERROR_SQL_ERROR', $db->stderr(true)), JLog::WARNING, 'jerror');
return false;
} else {
$queryString = (string) $query;
$queryString = str_replace(array("\r", "\n"), array('', ' '), substr($queryString, 0, 80));
JLog::add(JText::sprintf('JLIB_INSTALLER_UPDATE_LOG_QUERY', $file, $queryString), JLog::INFO, 'Update');
}
$update_count++;
}
}
}
}
// Update the database
$query = $db->getQuery(true)->delete('#__schemas')->where('extension_id = ' . $eid);
$db->setQuery($query);
if ($db->execute()) {
$query->clear()->insert($db->quoteName('#__schemas'))->columns(array($db->quoteName('extension_id'), $db->quoteName('version_id')))->values($eid . ', ' . $db->quote(end($files)));
$db->setQuery($query);
$db->execute();
}
}
}
}
return $update_count;
}
示例8: parseSchemaUpdates
/**
* Method to process the updates for an item
*
* @param SimpleXMLElement $schema The XML node to process
* @param integer $eid Extension Identifier
*
* @return boolean Result of the operations
*
* @since 11.1
*/
public function parseSchemaUpdates($schema, $eid)
{
$files = array();
$update_count = 0;
// Ensure we have an XML element and a valid extension id
if ($eid && $schema) {
$db = JFactory::getDBO();
$schemapaths = $schema->children();
if (count($schemapaths)) {
$dbDriver = strtolower($db->name);
if ($dbDriver == 'mysqli') {
$dbDriver = 'mysql';
}
$schemapath = '';
foreach ($schemapaths as $entry) {
$attrs = $entry->attributes();
if ($attrs['type'] == $dbDriver) {
$schemapath = $entry;
break;
}
}
if (strlen($schemapath)) {
$files = str_replace('.sql', '', JFolder::files($this->getPath('extension_root') . '/' . $schemapath, '\\.sql$'));
usort($files, 'version_compare');
if (!count($files)) {
return false;
}
$query = $db->getQuery(true);
$query->select('version_id')->from('#__schemas')->where('extension_id = ' . $eid);
$db->setQuery($query);
$version = $db->loadResult();
if ($version) {
// We have a version!
foreach ($files as $file) {
if (version_compare($file, $version) > 0) {
$buffer = file_get_contents($this->getPath('extension_root') . '/' . $schemapath . '/' . $file . '.sql');
// Graceful exit and rollback if read not successful
if ($buffer === false) {
JLog::add(JText::sprintf('JLIB_INSTALLER_ERROR_SQL_READBUFFER'), JLog::WARNING, 'jerror');
return false;
}
// Create an array of queries from the sql file
$queries = JDatabaseDriver::splitSql($buffer);
if (count($queries) == 0) {
// No queries to process
continue;
}
// Process each query in the $queries array (split out of sql file).
foreach ($queries as $query) {
$query = trim($query);
if ($query != '' && $query[0] != '#') {
$db->setQuery($query);
if (!$db->execute()) {
JLog::add(JText::sprintf('JLIB_INSTALLER_ERROR_SQL_ERROR', $db->stderr(true)), JLog::WARNING, 'jerror');
return false;
}
$update_count++;
}
}
}
}
}
// Update the database
$query = $db->getQuery(true);
$query->delete()->from('#__schemas')->where('extension_id = ' . $eid);
$db->setQuery($query);
if ($db->execute()) {
$query->clear();
$query->insert($db->quoteName('#__schemas'));
$query->columns(array($db->quoteName('extension_id'), $db->quoteName('version_id')));
$query->values($eid . ', ' . $db->quote(end($files)));
$db->setQuery($query);
$db->execute();
}
}
}
}
return $update_count;
}
示例9: splitSql
/**
* Splits a string of multiple queries into an array of individual queries.
*
* @param string $query Input SQL string with which to split into individual queries.
*
* @return array The queries from the input string separated into an array.
*
* @since 11.1
* @deprecated 13.1
*/
public static function splitSql($query)
{
JLog::add('JDatabase::splitSql() is deprecated, use JDatabaseDriver::splitSql() instead.', JLog::WARNING, 'deprecated');
return JDatabaseDriver::splitSql($query);
}
示例10: executeSQL
private function executeSQL($filename = '')
{
if (is_file($filename)) {
$buffer = file_get_contents($filename);
if ($buffer === false) {
return false;
}
if (version_compare(JVERSION, '3.0', 'ge')) {
$queries = JDatabaseDriver::splitSql($buffer);
} else {
$queries = JFactory::getDbo()->splitSql($buffer);
}
if (count($queries)) {
$db = JFactory::getDBO();
foreach ($queries as $query) {
$query = trim($query);
if ($query != '' && $query[0] != '#') {
try {
$db->setQuery($query);
$db->execute();
} catch (RuntimeException $e) {
}
}
}
}
}
return true;
}
示例11: _executeSQLFiles
private function _executeSQLFiles($sql)
{
if (JFile::exists($sql)) {
$db = JFactory::getDbo();
$queries = JDatabaseDriver::splitSql(file_get_contents($sql));
foreach ($queries as $query) {
$query = trim($query);
if ($query != '' && $query[0] != '#') {
$db->setQuery($query);
try {
$db->execute();
} catch (Exception $e) {
//do nothing as customer can do this very well by going to the tools menu
}
}
}
}
}
示例12: preflight
public function preflight($type, $parent)
{
$application = JFactory::getApplication();
$configuration = JFactory::getConfig();
$installer = $parent->getParent();
$db = JFactory::getDbo();
// Init the upgrade flag
$this->upgrade = false;
// Proceed only if we are updating
if ($type != 'install') {
// Ensure that we are under Joomla! 3.2 or later
if (version_compare(JVERSION, '3.3.6', 'lt')) {
$parent->getParent()->abort('K2 requires Joomla! 3.3.6 or later.');
return false;
}
// Get installled version
$query = $db->getQuery(true);
$query->select($db->quoteName('manifest_cache'))->from($db->quoteName('#__extensions'))->where($db->quoteName('name') . ' = ' . $db->quote('com_k2'));
$db->setQuery($query);
$manifest = json_decode($db->loadResult());
$installedVersion = $manifest->version;
// Detect if we need to perform an upgrade
if (version_compare($installedVersion, '3.0.0', 'lt')) {
// Ensure that the installed K2 version is not very old. Otherwise the update will fail.
if (version_compare($installedVersion, '2.6.9', 'lt')) {
$parent->getParent()->abort('You cannot update from this version of K2. Please update first your current K2 installation to the latest 2.x series and try again.');
return false;
}
// User is required to put the site offline while upgrading
if (!$configuration->get('offline')) {
$parent->getParent()->abort('Site is not offline. Please put your site offline and try again.');
return false;
}
// Since this is an upgrade rename all K2 2.x tables so the new ones will be created.
$oldTables = array('#__k2_attachments', '#__k2_categories', '#__k2_comments', '#__k2_extra_fields', '#__k2_extra_fields_groups', '#__k2_items', '#__k2_rating', '#__k2_tags', '#__k2_tags_xref', '#__k2_users', '#__k2_user_groups');
$existingTables = $db->getTableList();
foreach ($oldTables as $oldTable) {
$newTable = str_replace('#__k2_', '#__k2_v2_', $oldTable);
$needle = str_replace('#__', $db->getPrefix(), $newTable, 1);
if (!in_array($needle, $existingTables)) {
$db->setQuery('RENAME TABLE ' . $db->quoteName($oldTable) . ' TO ' . $db->quoteName($newTable));
if (!$db->execute()) {
$parent->getParent()->abort(JText::sprintf('JLIB_INSTALLER_ABORT_COMP_INSTALL_SQL_ERROR', $db->stderr(true)));
return false;
}
}
}
// Force parsing of SQL file since Joomla! does that only in install mode, not in updates
$sql = $installer->getPath('source') . '/administrator/components/com_k2/install.sql';
$queries = JDatabaseDriver::splitSql(file_get_contents($sql));
foreach ($queries as $query) {
$query = trim($query);
if ($query != '' && $query[0] != '#') {
$db->setQuery($query);
if (!$db->execute()) {
$parent->getParent()->abort(JText::sprintf('JLIB_INSTALLER_ERROR_SQL_ERROR', $db->stderr(true)));
return false;
}
}
}
// Rename component files to get rid of files we don't need
if (JFolder::exists(JPATH_SITE . '/components/com_k2')) {
if (JFolder::exists(JPATH_SITE . '/components/com_k2_v2')) {
if (!JFolder::delete(JPATH_SITE . '/components/com_k2_v2')) {
$parent->getParent()->abort('Could not delete folder ' . JPATH_SITE . '/components/com_k2_v2. Check permissions.');
return false;
}
}
if (!JFolder::move(JPATH_SITE . '/components/com_k2', JPATH_SITE . '/components/com_k2_v2')) {
$parent->getParent()->abort('Could not move folder ' . JPATH_SITE . '/components/com_k2. Check permissions.');
return false;
}
if (!JFolder::create(JPATH_SITE . '/components/com_k2')) {
$parent->getParent()->abort('Could not create folder ' . JPATH_SITE . '/components/com_k2. Check permissions.');
return false;
}
if (!JFolder::copy(JPATH_SITE . '/components/com_k2_v2/templates', JPATH_SITE . '/components/com_k2/templates')) {
$parent->getParent()->abort('Could not copy folder ' . JPATH_SITE . '/components/com_k2_v2/templates. Check permissions.');
return false;
}
}
if (JFolder::exists(JPATH_ADMINISTRATOR . '/components/com_k2')) {
if (JFolder::exists(JPATH_ADMINISTRATOR . '/components/com_k2_v2')) {
if (!JFolder::delete(JPATH_ADMINISTRATOR . '/components/com_k2_v2')) {
$parent->getParent()->abort('Could not delete folder ' . JPATH_ADMINISTRATOR . '/components/com_k2_v2. Check permissions.');
return false;
}
}
if (!JFolder::move(JPATH_ADMINISTRATOR . '/components/com_k2', JPATH_ADMINISTRATOR . '/components/com_k2_v2')) {
$parent->getParent()->abort('Could not move folder ' . JPATH_ADMINISTRATOR . '/components/com_k2. Check permissions.');
return false;
}
}
// Set a flag that this is an upgrade
$this->upgrade = true;
}
}
}
示例13: splitSql
/**
* Splits a string of multiple queries into an array of individual queries.
*
* @param string $sql Input SQL string with which to split into individual queries.
*
* @return array The queries from the input string separated into an array.
*
* @since 11.1
* @deprecated 13.1
*/
public static function splitSql($sql)
{
JLog::add('JDatabase::splitSql() is deprecated, use JDatabaseDriver::splitSql() instead.', JLog::NOTICE, 'deprecated');
return JDatabaseDriver::splitSql($sql);
}