Skip to content

Commit 39e1fb2

Browse files
committed
feat(database): added update part for #1896
1 parent 0757f7e commit 39e1fb2

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

phpmyfaq/setup/update.php

+17-12
Original file line numberDiff line numberDiff line change
@@ -371,22 +371,16 @@
371371
// UPDATES FROM 3.1.0-beta
372372
//
373373
if (version_compare($version, '3.1.0-beta', '<=')) {
374-
switch ($DB['type']) {
375-
case 'mysqli':
376-
$query[] = 'CREATE TABLE ' . $prefix . 'faqcategory_order (
374+
$query[] = match ($DB['type']) {
375+
'mysqli' => 'CREATE TABLE ' . $prefix . 'faqcategory_order (
377376
category_id int(11) NOT NULL,
378377
position int(11) NOT NULL,
379-
PRIMARY KEY (category_id))';
380-
break;
381-
case 'pgsql':
382-
case 'sqlite3':
383-
case 'sqlsrv':
384-
$query[] = 'CREATE TABLE ' . $prefix . 'faqcategory_order (
378+
PRIMARY KEY (category_id))',
379+
'pgsql', 'sqlite3', 'sqlsrv' => 'CREATE TABLE ' . $prefix . 'faqcategory_order (
385380
category_id INTEGER NOT NULL,
386381
position INTEGER NOT NULL,
387-
PRIMARY KEY (category_id))';
388-
break;
389-
}
382+
PRIMARY KEY (category_id))',
383+
};
390384
}
391385

392386
//
@@ -397,6 +391,17 @@
397391
$faqConfig->delete('records.autosaveSecs');
398392
}
399393

394+
//
395+
// UPDATES FROM 3.1.0-alpha
396+
//
397+
if (version_compare($version, '3.1.0-alpha', '<=')) {
398+
if ('sqlserv' === $DB['type']) {
399+
// queries to update VARCHAR -> NVARCHAR on MS SQL Server
400+
// @todo ALTER TABLE [TableName] ALTER COLUMN [ColumnName] nvarchar(N) null
401+
$query[] = 'DBCC CLEANTABLE';
402+
}
403+
}
404+
400405
//
401406
// Always the last step: Update version number
402407
//

phpmyfaq/src/phpMyFAQ/Database.php

+20-23
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace phpMyFAQ;
1919

20+
use phpMyFAQ\Core\Exception;
2021
use phpMyFAQ\Database\DatabaseDriver;
2122
use phpMyFAQ\Database\Mysqli;
2223
use phpMyFAQ\Database\Pgsql;
@@ -33,23 +34,23 @@ class Database
3334
/**
3435
* Instance.
3536
*
36-
* @var DatabaseDriver
37+
* @var DatabaseDriver|null
3738
*/
38-
private static $instance = null;
39+
private static ?DatabaseDriver $instance = null;
3940

4041
/**
4142
* Database type.
4243
*
43-
* @var string
44+
* @var string|null
4445
*/
45-
private static $dbType = null;
46+
private static ?string $dbType = null;
4647

4748
/**
4849
* Table prefix.
4950
*
50-
* @var string
51+
* @var string|null
5152
*/
52-
private static $tablePrefix = null;
53+
private static ?string $tablePrefix = null;
5354

5455
/**
5556
* Constructor.
@@ -62,15 +63,14 @@ private function __construct()
6263
* Database factory.
6364
*
6465
* @param string $type Database management system type
65-
*
66-
* @return Mysqli|Pgsql|Sqlite3|Sqlsrv
66+
* @return Pgsql|Sqlsrv|Mysqli|Sqlite3|DatabaseDriver|null
6767
* @throws Exception
6868
*/
69-
public static function factory($type)
69+
public static function factory(string $type): Pgsql|Sqlsrv|Mysqli|Sqlite3|DatabaseDriver|null
7070
{
7171
self::$dbType = $type;
7272

73-
if (0 === strpos($type, 'pdo_')) {
73+
if (str_starts_with($type, 'pdo_')) {
7474
$class = 'phpMyFAQ\Database\Pdo_' . ucfirst(substr($type, 4));
7575
} else {
7676
$class = 'phpMyFAQ\Database\\' . ucfirst($type);
@@ -88,9 +88,9 @@ public static function factory($type)
8888
/**
8989
* Returns the single instance.
9090
*
91-
* @return DatabaseDriver
91+
* @return DatabaseDriver|null
9292
*/
93-
public static function getInstance()
93+
public static function getInstance(): ?DatabaseDriver
9494
{
9595
if (null == self::$instance) {
9696
$className = __CLASS__;
@@ -110,9 +110,9 @@ private function __clone()
110110
/**
111111
* Returns the database type.
112112
*
113-
* @return string
113+
* @return string|null
114114
*/
115-
public static function getType()
115+
public static function getType(): ?string
116116
{
117117
return self::$dbType;
118118
}
@@ -121,10 +121,9 @@ public static function getType()
121121
* Check if a table is filled with data.
122122
*
123123
* @param string $tableName Table name
124-
*
125124
* @return bool true, if table is empty, otherwise false
126125
*/
127-
public static function checkOnEmptyTable($tableName)
126+
public static function checkOnEmptyTable(string $tableName): bool
128127
{
129128
if (
130129
self::$instance->numRows(
@@ -139,18 +138,16 @@ public static function checkOnEmptyTable($tableName)
139138

140139
/**
141140
* Error page, if the database connection is not possible.
141+
*
142142
* @param string $method
143143
*/
144-
public static function errorPage($method)
144+
public static function errorPage(string $method)
145145
{
146146
echo '<!DOCTYPE html>
147147
<html lang="en" class="no-js">
148148
<head>
149149
<meta charset="utf-8">
150150
<title>Fatal phpMyFAQ Error</title>
151-
<style type="text/css">
152-
@import url("assets/themes/default/css/style.min.css");
153-
</style>
154151
</head>
155152
<body>
156153
<div class="container">
@@ -166,17 +163,17 @@ public static function errorPage($method)
166163
*
167164
* @param string $tablePrefix
168165
*/
169-
public static function setTablePrefix($tablePrefix)
166+
public static function setTablePrefix(string $tablePrefix)
170167
{
171168
self::$tablePrefix = $tablePrefix;
172169
}
173170

174171
/**
175172
* Returns the table prefix.
176173
*
177-
* @return string
174+
* @return string|null
178175
*/
179-
public static function getTablePrefix()
176+
public static function getTablePrefix(): ?string
180177
{
181178
return self::$tablePrefix;
182179
}

0 commit comments

Comments
 (0)