From bb98e38522781c93dc8a21851a966ddf4f79b07f Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Sun, 13 Feb 2022 13:18:27 +0100 Subject: [PATCH] Made Sqlite3ooDatabase extending from Sqlite3Database and removed redundand methods. Added a few type hints. Fixed a few typos in comments. --- lib/Serendipity/Database/DbAbstract.php | 6 +- lib/Serendipity/Database/DbFactory.php | 2 +- lib/Serendipity/Database/MysqliDatabase.php | 12 +- .../Database/PdoPostgresDatabase.php | 10 +- .../Database/PdoSqliteDatabase.php | 6 +- lib/Serendipity/Database/PostgresDatabase.php | 10 +- lib/Serendipity/Database/SqlRelayDatabase.php | 10 +- lib/Serendipity/Database/Sqlite3Database.php | 16 +- .../Database/Sqlite3ooDatabase.php | 162 +----------------- lib/Serendipity/Database/SqliteDatabase.php | 8 +- 10 files changed, 27 insertions(+), 215 deletions(-) diff --git a/lib/Serendipity/Database/DbAbstract.php b/lib/Serendipity/Database/DbAbstract.php index 736cbf83..0af89ae8 100644 --- a/lib/Serendipity/Database/DbAbstract.php +++ b/lib/Serendipity/Database/DbAbstract.php @@ -45,12 +45,8 @@ abstract class DbAbstract /** * Returns an escaped string, so that it can be safely included in a SQL string encapsulated within quotes, without allowing SQL injection. - * - * @access public - * @param string input string - * @return string output string */ - public function escapeString($string) + public function escapeString(string $string): string { return $string; } diff --git a/lib/Serendipity/Database/DbFactory.php b/lib/Serendipity/Database/DbFactory.php index f96566d1..344c7f93 100644 --- a/lib/Serendipity/Database/DbFactory.php +++ b/lib/Serendipity/Database/DbFactory.php @@ -11,7 +11,7 @@ class DbFactory { private static $db_instance = null; - public static function createFromConfig(&$serendipity) + public static function createFromConfig(&$serendipity): \Serendipity\Database\DbAbstract { if (self::$db_instance !== null) { // Already instantiated - return it diff --git a/lib/Serendipity/Database/MysqliDatabase.php b/lib/Serendipity/Database/MysqliDatabase.php index 8bc6049f..29a988fb 100644 --- a/lib/Serendipity/Database/MysqliDatabase.php +++ b/lib/Serendipity/Database/MysqliDatabase.php @@ -26,11 +26,11 @@ class MysqliDatabase extends DbAbstract * @access public * @param boolean If true, perform the query. If false, rollback. */ - public function endTransaction($commit) + public function endTransaction(bool $commit) { if ($commit) { $this->query('commit'); - }else{ + } else { $this->query('rollback'); } } @@ -198,13 +198,9 @@ class MysqliDatabase extends DbAbstract } /** - * Returns a escaped string, so that it can be safely included in a SQL string encapsulated within quotes, without allowing SQL injection. - * - * @access public - * @param string input string - * @return string output string + * Returns an escaped string, so that it can be safely included in a SQL string encapsulated within quotes, without allowing SQL injection. */ - public function escapeString($string) + public function escapeString(string $string): string { return mysqli_escape_string($this->db_conn, $string); } diff --git a/lib/Serendipity/Database/PdoPostgresDatabase.php b/lib/Serendipity/Database/PdoPostgresDatabase.php index d3ed9ca0..dce3096d 100644 --- a/lib/Serendipity/Database/PdoPostgresDatabase.php +++ b/lib/Serendipity/Database/PdoPostgresDatabase.php @@ -26,7 +26,7 @@ class PdoPostgresDatabase extends DbAbstract * @access public * @param boolean If true, perform the query. If false, rollback. */ - public function endTransaction($commit) + public function endTransaction(bool $commit) { if ($commit) { $this->db_conn->commit(); @@ -82,13 +82,9 @@ class PdoPostgresDatabase extends DbAbstract } /** - * Returns a escaped string, so that it can be safely included in a SQL string encapsulated within quotes, without allowing SQL injection. - * - * @access public - * @param string input string - * @return string output string + * Returns an escaped string, so that it can be safely included in a SQL string encapsulated within quotes, without allowing SQL injection. */ - public function escapeString($string) + public function escapeString(string $string): string { return substr($this->db_conn->quote($string), 1, -1); } diff --git a/lib/Serendipity/Database/PdoSqliteDatabase.php b/lib/Serendipity/Database/PdoSqliteDatabase.php index 1558b374..c22949f5 100644 --- a/lib/Serendipity/Database/PdoSqliteDatabase.php +++ b/lib/Serendipity/Database/PdoSqliteDatabase.php @@ -119,12 +119,8 @@ class PdoSqliteDatabase extends DbAbstract /** * Returns an escaped string, so that it can be safely included in a SQL string encapsulated within quotes, without allowing SQL injection. - * - * @access public - * @param string input string - * @return string output string */ - public function escapeString($string) + public function escapeString(string $string): string { return substr($this->db_conn->quote($string), 1, -1); } diff --git a/lib/Serendipity/Database/PostgresDatabase.php b/lib/Serendipity/Database/PostgresDatabase.php index 50e2720b..e2c9334f 100644 --- a/lib/Serendipity/Database/PostgresDatabase.php +++ b/lib/Serendipity/Database/PostgresDatabase.php @@ -25,7 +25,7 @@ class PostgresDatabase extends DbAbstract * @access public * @param boolean If true, perform the query. If false, rollback. */ - public function endTransaction($commit) + public function endTransaction(bool $commit) { if ($commit) { $this->query('commit'); @@ -88,12 +88,8 @@ class PostgresDatabase extends DbAbstract /** * Returns an escaped string, so that it can be safely included in a SQL string encapsulated within quotes, without allowing SQL injection. - * - * @access public - * @param string input string - * @return string output string */ - public function escapeString($string) + public function escapeString(string $string): string { return pg_escape_string($string); } @@ -283,7 +279,7 @@ class PostgresDatabase extends DbAbstract } /** - * Prepares a Serendipty query input to fully valid SQL. Replaces certain "template" variables. + * Prepares a Serendipity query input to fully valid SQL. Replaces certain "template" variables. * * @access public * @param string SQL query with template variables to convert diff --git a/lib/Serendipity/Database/SqlRelayDatabase.php b/lib/Serendipity/Database/SqlRelayDatabase.php index cabe5a00..31c55fb4 100644 --- a/lib/Serendipity/Database/SqlRelayDatabase.php +++ b/lib/Serendipity/Database/SqlRelayDatabase.php @@ -43,9 +43,9 @@ class SqlRelayDatabase extends DbAbstract * Tells the DB Layer to end a DB transaction. * * @access public - * @param boolean If true, perform the query. If false, rollback. + * @param boolean $commit If true, perform the query. If false, rollback. */ - public function endTransaction($commit) + public function endTransaction(bool $commit) { if ($commit) { sqlrcon_commit($this->db_conn); @@ -321,12 +321,8 @@ class SqlRelayDatabase extends DbAbstract /** * Returns an escaped string, so that it can be safely included in a SQL string encapsulated within quotes, without allowing SQL injection. - * - * @access public - * @param string input string - * @return string output string */ - public function escapeString($str) + public function escapeString(string $string): string { static $search = array("\x00", '%', "'", '\"'); static $replace = array('%00', '%25', "''", '\\\"'); diff --git a/lib/Serendipity/Database/Sqlite3Database.php b/lib/Serendipity/Database/Sqlite3Database.php index f9542404..fb785a3a 100644 --- a/lib/Serendipity/Database/Sqlite3Database.php +++ b/lib/Serendipity/Database/Sqlite3Database.php @@ -30,7 +30,7 @@ class Sqlite3Database extends DbAbstract * @access public * @param boolean If true, perform the query. If false, rollback. */ - public function endTransaction($commit) + public function endTransaction(bool $commit) { if ($commit) { $this->query('commit transaction'); @@ -58,13 +58,9 @@ class Sqlite3Database extends DbAbstract } /** - * Returns a escaped string, so that it can be safely included in a SQL string encapsulated within quotes, without allowing SQL injection. - * - * @access public - * @param string input string - * @return string output string + * Returns an escaped string, so that it can be safely included in a SQL string encapsulated within quotes, without allowing SQL injection. */ - public function escapeString($string) + public function escapeString(string $string): string { static $search = array("\x00", '%', "'", '\"'); static $replace = array('%00', '%25', "''", '\\\"'); @@ -92,7 +88,7 @@ class Sqlite3Database extends DbAbstract public function updatedRows() { // It is unknown whether sqllite returns rows MATCHED or rows UPDATED - return sqlite3_changes($this->db_conn); + return $this->affectedRows(); } /** @@ -104,7 +100,7 @@ class Sqlite3Database extends DbAbstract public function matchedRows() { // It is unknown whether sqllite returns rows MATCHED or rows UPDATED - return sqlite3_changes($this->db_conn); + return $this->affectedRows; } /** @@ -326,7 +322,7 @@ class Sqlite3Database extends DbAbstract } /** - * Prepares a Serendipty query input to fully valid SQL. Replaces certain "template" variables. + * Prepares a Serendipity query input to fully valid SQL. Replaces certain "template" variables. * * @access public * @param string SQL query with template variables to convert diff --git a/lib/Serendipity/Database/Sqlite3ooDatabase.php b/lib/Serendipity/Database/Sqlite3ooDatabase.php index a51762e9..3c7f447e 100644 --- a/lib/Serendipity/Database/Sqlite3ooDatabase.php +++ b/lib/Serendipity/Database/Sqlite3ooDatabase.php @@ -5,40 +5,10 @@ namespace Serendipity\Database; -use Serendipity\Database\DbAbstract; +use Serendipity\Database\Sqlite3Database; -class Sqlite3ooDatabase extends DbAbstract +class Sqlite3ooDatabase extends Sqlite3Database { - // SQLite3 only fetches by assoc, we will emulate the other result types - public const SQLITE3_ASSOC = 0; - public const SQLITE3_NUM = 1; - public const SQLITE3_BOTH = 2; - - /** - * Tells the DB Layer to start a DB transaction. - * - * @access public - */ - public function beginTransaction() - { - $this->query('begin transaction'); - } - - /** - * Tells the DB Layer to end a DB transaction. - * - * @access public - * @param boolean If true, perform the query. If false, rollback. - */ - public function endTransaction($commit) - { - if ($commit) { - $this->query('commit transaction'); - } else { - $this->query('rollback transaction'); - } - } - /** * Connect to the configured Database * @@ -57,21 +27,6 @@ class Sqlite3ooDatabase extends DbAbstract return $this->db_conn; } - /** - * Returns an escaped string, so that it can be safely included in a SQL string encapsulated within quotes, without allowing SQL injection. - * - * @access public - * @param string input string - * @return string output string - */ - public function escapeString($string) - { - static $search = array("\x00", '%', "'", '\"'); - static $replace = array('%00', '%25', "''", '\\\"'); - - return str_replace($search, $replace, $string); - } - /** * Returns the number of affected rows of a SQL query * @@ -83,30 +38,6 @@ class Sqlite3ooDatabase extends DbAbstract return $this->db_conn->changes(); } - /** - * Returns the number of updated rows in a SQL query - * - * @access public - * @return int Number of updated rows - */ - public function updatedRows() - { - // It is unknown whether sqllite returns rows MATCHED or rows UPDATED - return $this->db_conn->changes(); - } - - /** - * Returns the number of matched rows in a SQL query - * - * @access public - * @return int Number of matched rows - */ - public function matchedRows() - { - // It is unknown whether sqllite returns rows MATCHED or rows UPDATED - return $this->db_conn->changes($this->db_conn); - } - /** * Returns the latest INSERT_ID of an SQL INSERT INTO command, for auto-increment columns * @@ -130,7 +61,7 @@ class Sqlite3ooDatabase extends DbAbstract * @param int Bitmask to tell whether to fetch numerical/associative arrays * @return array Propper array containing the resource results */ - public function sqlite_fetch_array($res, $type = self::SQLITE3_BOTH) + public function fetchArray($res, $type = self::SQLITE3_BOTH) { static $search = array('%00', '%25'); static $replace = array("\x00", '%'); @@ -174,30 +105,6 @@ class Sqlite3ooDatabase extends DbAbstract return $frow; } - /** - * Assemble and return SQL condition for a "IN (...)" clause - * - * @access public - * @param string table column name - * @param array referenced array of values to search for in the "IN (...)" clause - * @param string condition of how to associate the different input values of the $search_ids parameter - * @return string resulting SQL string - */ - public function inSql($col, &$search_ids, $type = ' OR ') - { - $sql = array(); - if (!is_array($search_ids)) { - return false; - } - - foreach ($search_ids as $id) { - $sql[] = $col . ' = ' . $id; - } - - $cond = '(' . implode($type, $sql) . ')'; - return $cond; - } - /** * Perform a DB Layer SQL query. * @@ -338,67 +245,4 @@ class Sqlite3ooDatabase extends DbAbstract $errs[] = "Unable to open \"$dbfile\" - check permissions (directory needs to be writeable for webserver)!"; return false; } - - /** - * Prepares a Serendipty query input to fully valid SQL. Replaces certain "template" variables. - * - * @access public - * @param string SQL query with template variables to convert - * @return resource SQL resource handle of the executed query - */ - public function schemaImport($query) - { - static $search = array('{AUTOINCREMENT}', '{PRIMARY}', '{UNSIGNED}', '{FULLTEXT}', '{BOOLEAN}', '{UTF_8}', '{TEXT}'); - static $replace = array('INTEGER AUTOINCREMENT', 'PRIMARY KEY', '', '', 'BOOLEAN NOT NULL', '', 'LONGTEXT'); - - if (stristr($query, '{FULLTEXT_MYSQL}')) { - return true; - } - - $query = trim(str_replace($search, $replace, $query)); - $query = str_replace('INTEGER AUTOINCREMENT PRIMARY KEY', 'INTEGER PRIMARY KEY AUTOINCREMENT', $query); - if ($query[0] == '@') { - // Errors are expected to happen (like duplicate index creation) - return $this->query(substr($query, 1), false, 'both', false, false, false, true); - } else { - return $this->query($query); - } - } - - /** - * Returns the option to a LIMIT SQL statement, because it varies across DB systems - * - * @access public - * @param int Number of the first row to return data from - * @param int Number of rows to return - * @return string SQL string to pass to a LIMIT statement - */ - public function limit($start, $offset) - { - return $start . ', ' . $offset; - } - - /** - * Return a LIMIT SQL option to the DB Layer as a full LIMIT statement - * - * @access public - * @param SQL string of a LIMIT option - * @return SQL string containing a full LIMIT statement - */ - public function limitSql($limitstring) - { - return ' LIMIT ' . $limitstring; - } - - /** - * Returns the SQL code used for concatenating strings - * - * @access public - * @param string Input string/column to concatenate - * @return string SQL parameter - */ - public function concat($string) - { - return 'concat(' . $string . ')'; - } } diff --git a/lib/Serendipity/Database/SqliteDatabase.php b/lib/Serendipity/Database/SqliteDatabase.php index 213495e7..190cf18c 100644 --- a/lib/Serendipity/Database/SqliteDatabase.php +++ b/lib/Serendipity/Database/SqliteDatabase.php @@ -25,7 +25,7 @@ class SqliteDatabase extends DbAbstract * @access public * @param boolean If true, perform the query. If false, rollback. */ - public function endTransaction($commit) + public function endTransaction(bool $commit) { if ($commit) { $this->query('commit transaction'); @@ -63,12 +63,8 @@ class SqliteDatabase extends DbAbstract /** * Returns an escaped string, so that it can be safely included in a SQL string encapsulated within quotes, without allowing SQL injection. - * - * @access public - * @param string input string - * @return string output string */ - public function escapeString($string) + public function escapeString(string $string): string { static $search = array("\x00", '%', "'", '\"'); static $replace = array('%00', '%25', "''", '\\\"');