Skip to content

Commit

Permalink
feat(database): added update part for #1896
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Apr 9, 2022
1 parent 0757f7e commit 39e1fb2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
29 changes: 17 additions & 12 deletions phpmyfaq/setup/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,22 +371,16 @@
// UPDATES FROM 3.1.0-beta
//
if (version_compare($version, '3.1.0-beta', '<=')) {
switch ($DB['type']) {
case 'mysqli':
$query[] = 'CREATE TABLE ' . $prefix . 'faqcategory_order (
$query[] = match ($DB['type']) {
'mysqli' => 'CREATE TABLE ' . $prefix . 'faqcategory_order (
category_id int(11) NOT NULL,
position int(11) NOT NULL,
PRIMARY KEY (category_id))';
break;
case 'pgsql':
case 'sqlite3':
case 'sqlsrv':
$query[] = 'CREATE TABLE ' . $prefix . 'faqcategory_order (
PRIMARY KEY (category_id))',
'pgsql', 'sqlite3', 'sqlsrv' => 'CREATE TABLE ' . $prefix . 'faqcategory_order (
category_id INTEGER NOT NULL,
position INTEGER NOT NULL,
PRIMARY KEY (category_id))';
break;
}
PRIMARY KEY (category_id))',
};
}

//
Expand All @@ -397,6 +391,17 @@
$faqConfig->delete('records.autosaveSecs');
}

//
// UPDATES FROM 3.1.0-alpha
//
if (version_compare($version, '3.1.0-alpha', '<=')) {
if ('sqlserv' === $DB['type']) {
// queries to update VARCHAR -> NVARCHAR on MS SQL Server
// @todo ALTER TABLE [TableName] ALTER COLUMN [ColumnName] nvarchar(N) null
$query[] = 'DBCC CLEANTABLE';
}
}

//
// Always the last step: Update version number
//
Expand Down
43 changes: 20 additions & 23 deletions phpmyfaq/src/phpMyFAQ/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace phpMyFAQ;

use phpMyFAQ\Core\Exception;
use phpMyFAQ\Database\DatabaseDriver;
use phpMyFAQ\Database\Mysqli;
use phpMyFAQ\Database\Pgsql;
Expand All @@ -33,23 +34,23 @@ class Database
/**
* Instance.
*
* @var DatabaseDriver
* @var DatabaseDriver|null
*/
private static $instance = null;
private static ?DatabaseDriver $instance = null;

/**
* Database type.
*
* @var string
* @var string|null
*/
private static $dbType = null;
private static ?string $dbType = null;

/**
* Table prefix.
*
* @var string
* @var string|null
*/
private static $tablePrefix = null;
private static ?string $tablePrefix = null;

/**
* Constructor.
Expand All @@ -62,15 +63,14 @@ private function __construct()
* Database factory.
*
* @param string $type Database management system type
*
* @return Mysqli|Pgsql|Sqlite3|Sqlsrv
* @return Pgsql|Sqlsrv|Mysqli|Sqlite3|DatabaseDriver|null
* @throws Exception
*/
public static function factory($type)
public static function factory(string $type): Pgsql|Sqlsrv|Mysqli|Sqlite3|DatabaseDriver|null
{
self::$dbType = $type;

if (0 === strpos($type, 'pdo_')) {
if (str_starts_with($type, 'pdo_')) {
$class = 'phpMyFAQ\Database\Pdo_' . ucfirst(substr($type, 4));
} else {
$class = 'phpMyFAQ\Database\\' . ucfirst($type);
Expand All @@ -88,9 +88,9 @@ public static function factory($type)
/**
* Returns the single instance.
*
* @return DatabaseDriver
* @return DatabaseDriver|null
*/
public static function getInstance()
public static function getInstance(): ?DatabaseDriver
{
if (null == self::$instance) {
$className = __CLASS__;
Expand All @@ -110,9 +110,9 @@ private function __clone()
/**
* Returns the database type.
*
* @return string
* @return string|null
*/
public static function getType()
public static function getType(): ?string
{
return self::$dbType;
}
Expand All @@ -121,10 +121,9 @@ public static function getType()
* Check if a table is filled with data.
*
* @param string $tableName Table name
*
* @return bool true, if table is empty, otherwise false
*/
public static function checkOnEmptyTable($tableName)
public static function checkOnEmptyTable(string $tableName): bool
{
if (
self::$instance->numRows(
Expand All @@ -139,18 +138,16 @@ public static function checkOnEmptyTable($tableName)

/**
* Error page, if the database connection is not possible.
*
* @param string $method
*/
public static function errorPage($method)
public static function errorPage(string $method)
{
echo '<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<meta charset="utf-8">
<title>Fatal phpMyFAQ Error</title>
<style type="text/css">
@import url("assets/themes/default/css/style.min.css");
</style>
</head>
<body>
<div class="container">
Expand All @@ -166,17 +163,17 @@ public static function errorPage($method)
*
* @param string $tablePrefix
*/
public static function setTablePrefix($tablePrefix)
public static function setTablePrefix(string $tablePrefix)
{
self::$tablePrefix = $tablePrefix;
}

/**
* Returns the table prefix.
*
* @return string
* @return string|null
*/
public static function getTablePrefix()
public static function getTablePrefix(): ?string
{
return self::$tablePrefix;
}
Expand Down

0 comments on commit 39e1fb2

Please sign in to comment.