/** * Clone the table structure */ public function cloneTableStructure() { global $wgSharedTables, $wgSharedDB; foreach ($this->tablesToClone as $tbl) { if ($wgSharedDB && in_array($tbl, $wgSharedTables, true)) { // Shared tables don't work properly when cloning due to // how prefixes are handled (bug 65654) throw new RuntimeException("Cannot clone shared table {$tbl}."); } # Clean up from previous aborted run. So that table escaping # works correctly across DB engines, we need to change the pre- # fix back and forth so tableName() works right. self::changePrefix($this->oldTablePrefix); $oldTableName = $this->db->tableName($tbl, 'raw'); self::changePrefix($this->newTablePrefix); $newTableName = $this->db->tableName($tbl, 'raw'); if ($this->dropCurrentTables && !in_array($this->db->getType(), ['postgres', 'oracle'])) { if ($oldTableName === $newTableName) { // Last ditch check to avoid data loss throw new LogicException("Not dropping new table, as '{$newTableName}'" . " is name of both the old and the new table."); } $this->db->dropTable($tbl, __METHOD__); wfDebug(__METHOD__ . " dropping {$newTableName}\n"); // Dropping the oldTable because the prefix was changed } # Create new table wfDebug(__METHOD__ . " duplicating {$oldTableName} to {$newTableName}\n"); $this->db->duplicateTableStructure($oldTableName, $newTableName, $this->useTemporaryTables); } }
public function execute() { // Shouldn't be needed for Postgres $this->db = $this->getDB(DB_MASTER); if ($this->db->getType() == 'postgres') { $this->error("This script is not needed when using Postgres.\n", true); } if ($this->db->getType() == 'sqlite') { if (!DatabaseSqlite::getFulltextSearchModule()) { $this->error("Your version of SQLite module for PHP doesn't " . "support full-text search (FTS3).\n", true); } if (!$this->db->checkForEnabledSearch()) { $this->error("Your database schema is not configured for " . "full-text search support. Run update.php.\n", true); } } if ($this->db->getType() == 'mysql') { $this->dropMysqlTextIndex(); $this->clearSearchIndex(); $this->populateSearchIndex(); $this->createMysqlTextIndex(); } else { $this->clearSearchIndex(); $this->populateSearchIndex(); } $this->output("Done.\n"); }
/** * Helper for database teardown, called from the teardown closure. Destroy * the database clone and fix up some things that CloneDatabase doesn't fix. * * @todo Move most things here to CloneDatabase */ private function teardownDatabase() { $this->checkSetupDone('setupDatabase'); $this->dbClone->destroy(); $this->databaseSetupDone = false; if ($this->useTemporaryTables) { if ($this->db->getType() == 'sqlite') { # Under SQLite the searchindex table is virtual and need # to be explicitly destroyed. See bug 29912 # See also MediaWikiTestCase::destroyDB() wfDebug(__METHOD__ . " explicitly destroying sqlite virtual table parsertest_searchindex\n"); $this->db->query("DROP TABLE `parsertest_searchindex`"); } # Don't need to do anything return; } $tables = $this->listTables(); foreach ($tables as $table) { if ($this->db->getType() == 'oracle') { $this->db->query("DROP TABLE pt_{$table} DROP CONSTRAINTS"); } else { $this->db->query("DROP TABLE `parsertest_{$table}`"); } } if ($this->db->getType() == 'oracle') { $this->db->query('BEGIN FILL_WIKI_INFO; END;'); } }
$name = $_POST['tablename']; $primary_keys = array(); for ($i = 0; $i < $num; $i++) { if ($_POST[$i . '_field'] != "" && isset($_POST[$i . '_primarykey'])) { $primary_keys[] = $_POST[$i . '_field']; } } $query = "CREATE TABLE " . $db->quote($name) . " ("; for ($i = 0; $i < $num; $i++) { if ($_POST[$i . '_field'] != "") { $query .= $db->quote($_POST[$i . '_field']) . " "; $query .= $_POST[$i . '_type'] . " "; if (isset($_POST[$i . '_primarykey'])) { if (count($primary_keys) == 1) { $query .= "PRIMARY KEY "; if (isset($_POST[$i . '_autoincrement']) && $db->getType() != "SQLiteDatabase") { $query .= "AUTOINCREMENT "; } } $query .= "NOT NULL "; } if (!isset($_POST[$i . '_primarykey']) && isset($_POST[$i . '_notnull'])) { $query .= "NOT NULL "; } if ($_POST[$i . '_defaultoption'] != 'defined' && $_POST[$i . '_defaultoption'] != 'none' && $_POST[$i . '_defaultoption'] != 'expr') { $query .= "DEFAULT " . $_POST[$i . '_defaultoption'] . " "; } elseif ($_POST[$i . '_defaultoption'] == 'expr') { $query .= "DEFAULT (" . $_POST[$i . '_defaultvalue'] . ") "; } elseif (isset($_POST[$i . '_defaultvalue']) && $_POST[$i . '_defaultoption'] == 'defined') { $typeAffinity = get_type_affinity($_POST[$i . '_type']); if (($typeAffinity == "INTEGER" || $typeAffinity == "REAL" || $typeAffinity == "NUMERIC") && is_numeric($_POST[$i . '_defaultvalue'])) {
} else { echo "class='tab'"; } echo ">Vacuum</a>"; echo "<div style='clear:both;'></div>"; echo "<div id='main'>"; if ($view == "structure") { $query = "SELECT sqlite_version() AS sqlite_version"; $queryVersion = $db->select($query); $realVersion = $queryVersion['sqlite_version']; echo "<b>Database name</b>: " . $db->getName() . "<br/>"; echo "<b>Path to database</b>: " . $db->getPath() . "<br/>"; echo "<b>Size of database</b>: " . $db->getSize() . "<br/>"; echo "<b>Database last modified</b>: " . $db->getDate() . "<br/>"; echo "<b>SQLite version</b>: " . $realVersion . "<br/>"; echo "<b>SQLite extension</b>: " . $db->getType() . "<br/>"; echo "<b>PHP version</b>: " . phpversion() . "<br/><br/>"; $query = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name"; $result = $db->selectArray($query); $j = 0; for ($i = 0; $i < sizeof($result); $i++) { if (substr($result[$i]['name'], 0, 7) != "sqlite_" && $result[$i]['name'] != "") { $j++; } } if ($j == 0) { echo "No tables in database.<br/><br/>"; } else { echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>"; echo "<tr>"; echo "<td class='tdheader'>Table</td>";
echo "<a href='" . PAGE . "?view=sql' "; if ($view == "sql") { echo "class='tab_pressed'"; } else { echo "class='tab'"; } echo ">SQL</a>"; echo "<div style='clear:both;'></div>"; echo "<div id='main'>"; if ($view == "structure") { echo "<b>Database name</b>: " . $db->getName() . "<br/>"; echo "<b>Path to database</b>: " . $db->getPath() . "<br/>"; echo "<b>Size of database</b>: " . $db->getSize() . "<br/>"; echo "<b>Database last modified</b>: " . $db->getDate() . "<br/>"; echo "<b>SQLite version of database</b>: " . $db->getVersion() . "<br/>"; echo "<b>PHP extension used</b>: " . $db->getType() . "<br/><br/>"; $query = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name"; $result = $db->selectArray($query); $j = 0; for ($i = 0; $i < sizeof($result); $i++) { if (substr($result[$i]['name'], 0, 7) != "sqlite_" && $result[$i]['name'] != "") { $j++; } } if ($j == 0) { echo "No tables in database.<br/><br/>"; } else { echo "<table border='0' cellpadding='2' cellspacing='1'>"; echo "<tr>"; echo "<td class='tdheader'>Table</td>"; echo "<td class='tdheader' colspan='7'>Action</td>";
echo ">Import</a>"; echo "<a href='" . PAGE . "?view=vacuum' "; if ($view == "vacuum") { echo "class='tab_pressed'"; } else { echo "class='tab'"; } echo ">Vacuum</a>"; echo "<div style='clear:both;'></div>"; echo "<div id='main'>"; if ($view == "structure") { echo "<b>Database name</b>: " . $db->getName() . "<br/>"; echo "<b>Path to database</b>: " . $db->getPath() . "<br/>"; echo "<b>Size of database</b>: " . $db->getSize() . "<br/>"; echo "<b>Database last modified</b>: " . $db->getDate() . "<br/>"; if ($db->getType() == "SQLiteDatabase") { echo "<b>SQLite version</b>: " . sqlite_libversion() . "<br/>"; echo "<b>SQLite encoding</b>: " . sqlite_libencoding() . "<br/>"; } if ($db->getType() == "SQLite3") { echo "<b>SQLite version</b>: " . SQLite3::version() . "<br/>"; } else { echo "<b>SQLite version</b>: " . $db->getVersion() . "<br/>"; } echo "<b>SQLite extension</b>: " . $db->getType() . "<br/>"; echo "<b>PHP version</b>: " . phpversion() . "<br/><br/>"; $query = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name"; $result = $db->selectArray($query); $j = 0; for ($i = 0; $i < sizeof($result); $i++) { if (substr($result[$i]['name'], 0, 7) != "sqlite_" && $result[$i]['name'] != "") {
echo "class='tab'"; } echo ">Delete Database</a>"; } echo "<div style='clear:both;'></div>"; echo "<div id='main'>"; if ($view == "structure") { $query = "SELECT sqlite_version() AS sqlite_version"; $queryVersion = $db->select($query); $realVersion = $queryVersion['sqlite_version']; echo "<b>Database name</b>: " . $db->getName() . "<br/>"; echo "<b>Path to database</b>: " . $db->getPath() . "<br/>"; echo "<b>Size of database</b>: " . $db->getSize() . "<br/>"; echo "<b>Database last modified</b>: " . $db->getDate() . "<br/>"; echo "<b>SQLite version</b>: " . $realVersion . "<br/>"; echo "<b>SQLite extension</b> " . helpLink("SQLite Library Extensions") . ": " . $db->getType() . "<br/>"; echo "<b>PHP version</b>: " . phpversion() . "<br/><br/>"; if (isset($_GET['sort'])) { $_SESSION[COOKIENAME . 'sort'] = $_GET['sort']; } else { unset($_SESSION[COOKIENAME . 'sort']); } if (isset($_GET['order'])) { $_SESSION[COOKIENAME . 'order'] = $_GET['order']; } else { unset($_SESSION[COOKIENAME . 'order']); } $query = "SELECT type, name FROM sqlite_master WHERE type='table' OR type='view'"; $queryAdd = ""; if (isset($_SESSION[COOKIENAME . 'sort'])) { $queryAdd .= " ORDER BY " . $_SESSION[COOKIENAME . 'sort'];
} echo ">Structure</a>"; echo "<a href='" . PAGE . "?view=sql' "; if ($view == "sql") { echo "class='tab_pressed'"; } else { echo "class='tab'"; } echo ">SQL</a>"; echo "<div style='clear:both;'></div>"; echo "<div id='main'>"; if ($view == "structure") { echo "<b>Database Name</b>: " . $db->getName() . "<br/>"; echo "<b>Path to Database</b>: " . $db->getPath() . "<br/>"; echo "<b>Database Version</b>: " . $db->getVersion() . "<br/>"; echo "<b>PHP Extension Used</b>: " . $db->getType() . "<br/><br/>"; $dbView->generateTableList(); } else { if ($view == "sql") { $dbView->generateSQL(); } } echo "</div>"; } } } } echo "</div>"; echo "<br/>"; $endTimeTot = microtime(true); $timeTot = round($endTimeTot - $startTimeTot, 5);
private function dropFunctions() { $this->db->query('DROP FUNCTION IF EXISTS mw_test_function' . ($this->db->getType() == 'postgres' ? '()' : '')); }
/** * @param Database $db * @param bool $shared * @param Maintenance $maintenance * * @throws MWException * @return DatabaseUpdater */ public static function newForDB(Database $db, $shared = false, $maintenance = null) { $type = $db->getType(); if (in_array($type, Installer::getDBTypes())) { $class = ucfirst($type) . 'Updater'; return new $class($db, $shared, $maintenance); } else { throw new MWException(__METHOD__ . ' called for unsupported $wgDBtype'); } }
/** * @throws MWException * @since 1.18 */ protected function checkDbIsSupported() { if (!in_array($this->db->getType(), $this->supportedDBs)) { throw new MWException($this->db->getType() . " is not currently supported for unit testing."); } }
/** * Return an SQL expression selecting rows which sort above the given row, * assuming an ordering of cl_collation, cl_to, cl_type, cl_from * @param stdClass $row * @param Database $dbw * @return string */ function getBatchCondition($row, $dbw) { if ($this->hasOption('previous-collation')) { $fields = ['cl_to', 'cl_type', 'cl_from']; } else { $fields = ['cl_collation', 'cl_to', 'cl_type', 'cl_from']; } $first = true; $cond = false; $prefix = false; foreach ($fields as $field) { if ($dbw->getType() === 'mysql' && $field === 'cl_type') { // Range conditions with enums are weird in mysql // This must be a numeric literal, or it won't work. $encValue = intval($row->cl_type_numeric); } else { $encValue = $dbw->addQuotes($row->{$field}); } $inequality = "{$field} > {$encValue}"; $equality = "{$field} = {$encValue}"; if ($first) { $cond = $inequality; $prefix = $equality; $first = false; } else { $cond .= " OR ({$prefix} AND {$inequality})"; $prefix .= " AND {$equality}"; } } return $cond; }
private function quoteTable(Database $db, $table) { if ($db->getType() === 'sqlite') { return $table; } else { return $db->addIdentifierQuotes($table); } }