Made Sqlite3ooDatabase extending from Sqlite3Database and removed redundand methods. Added a few type hints. Fixed a few typos in comments.

This commit is contained in:
Markus Birth 2022-02-13 13:18:27 +01:00
parent 8ecebf8940
commit bb98e38522
Signed by: mbirth
GPG Key ID: A9928D7A098C3A9A
10 changed files with 27 additions and 215 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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

View File

@ -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', "''", '\\\"');

View File

@ -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

View File

@ -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 . ')';
}
}

View File

@ -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', "''", '\\\"');