diff --git a/src/config-sample.php b/src/config-sample.php index e96efa5..3441b99 100644 --- a/src/config-sample.php +++ b/src/config-sample.php @@ -3,7 +3,7 @@ /* Uncomment the line with MTT_DB_TYPE if you make clean install only. Leave it commented (with # at start) if you are upgrading from version before 1.7. - Select the database type: sqlite or mysql. + Select the database type: sqlite or mysql or postgres. */ #define("MTT_DB_TYPE", "sqlite"); diff --git a/src/includes/class.db.mysql.php b/src/includes/class.db.mysql.php index 48a6aee..f32b921 100644 --- a/src/includes/class.db.mysql.php +++ b/src/includes/class.db.mysql.php @@ -57,6 +57,8 @@ class DatabaseResult_Mysql extends DatabaseResult_Abstract // ---------------------------------------------------------------------------- // class Database_Mysql extends Database_Abstract { + const DBTYPE = 'mysql'; + /** @var PDO */ protected $dbh; diff --git a/src/includes/class.db.mysqli.php b/src/includes/class.db.mysqli.php index 0565543..93b9d8c 100644 --- a/src/includes/class.db.mysqli.php +++ b/src/includes/class.db.mysqli.php @@ -39,6 +39,8 @@ class DatabaseResult_Mysqli extends DatabaseResult_Abstract // ---------------------------------------------------------------------------- // class Database_Mysqli extends Database_Abstract { + const DBTYPE = 'mysql'; + /** @var mysqli */ protected $dbh; protected $dbname; diff --git a/src/includes/class.db.sqlite3.php b/src/includes/class.db.sqlite3.php index 348244c..34c0e9e 100644 --- a/src/includes/class.db.sqlite3.php +++ b/src/includes/class.db.sqlite3.php @@ -56,6 +56,8 @@ class DatabaseResult_Sqlite3 extends DatabaseResult_Abstract class Database_Sqlite3 extends Database_Abstract { + const DBTYPE = 'sqlite'; + /** @var PDO */ protected $dbh; diff --git a/src/setup.php b/src/setup.php index ea3bccc..919e6b5 100644 --- a/src/setup.php +++ b/src/setup.php @@ -2,7 +2,7 @@ /* This file is a part of myTinyTodo. - (C) Copyright 2009-2011,2020-2022 Max Pozdeev + (C) Copyright 2009-2011,2020-2023 Max Pozdeev Licensed under the GNU GPL version 2 or any later. See file COPYRIGHT for details. */ @@ -166,11 +166,7 @@ if ($ver == '') { check_post_stoken(); # install database - try { - createAllTables($db, $dbtype); - } catch (Exception $e) { - exitMessage("Error: ". htmlarray($e->getMessage())); - } + createAllTables($db, $dbtype); # throws # create default list $db->ex( "INSERT INTO {$db->prefix}lists (uuid,name,d_created,taskview) VALUES (?,?,?,?)", array(generateUUID(), 'Todo', time(), 1) ); @@ -259,8 +255,10 @@ function createAllTables($db, $dbtype) } /* ===== mysql ========================================================= */ +// TODO: use utf8mb4_unicode_520_ci collation function createMysqlTables($db) { + // Mysql does not support transactions in DDL $db->ex( "CREATE TABLE {$db->prefix}lists ( `id` INT UNSIGNED NOT NULL auto_increment, @@ -297,7 +295,7 @@ function createMysqlTables($db) UNIQUE KEY(`uuid`) ) CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci "); - + // Max length of varchar of utf8mb4 with UNIQUE index is 191 until Mysql 5.7 and MariaDB 10.2 $db->ex( "CREATE TABLE {$db->prefix}tags ( `id` INT UNSIGNED NOT NULL auto_increment, @@ -659,27 +657,33 @@ function testConnect(&$error) return $db; } -function debugExceptionHandler($e) +function debugExceptionHandler(Throwable $e) { echo '
Error ('. htmlspecialchars(get_class($e)) .'): \''. htmlspecialchars($e->getMessage()) .'\' in '. htmlspecialchars($e->getFile() .':'. $e->getLine()). ''. "\n
". htmlspecialchars($e->getTraceAsString()) . "
\n"; exit; } -function myExceptionHandler($e) +function myExceptionHandler(Throwable $e) { - echo '
Error: '. htmlspecialchars($e->getMessage()) ; + $called = ''; + foreach ($e->getTrace() as $a) { + if ($a['file'] == __FILE__) { + $called = " in ". htmlspecialchars(basename($a['file']). ':'. $a['line']). ""; + break; + } + } + echo '
Error: \''. htmlspecialchars($e->getMessage()). '\''. $called ; exit; } function databaseTypeName(Database_Abstract $db) { - $s = get_class($db); - switch ($s) { - case "Database_Mysql": return "MySQL"; - case "Database_Postgres": return "PostgreSQL"; - case "Database_Sqlite3": return "SQLite"; - default: return $s; + switch ($db::DBTYPE) { + case DBConnection::DBTYPE_MYSQL: return "MySQL"; + case DBConnection::DBTYPE_POSTGRES: return "PostgreSQL"; + case DBConnection::DBTYPE_SQLITE: return "SQLite"; + default: throw new Exception("Unsupported database type: ". $db::DBTYPE); } }