Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/2.9 #40

Merged
merged 8 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 38 additions & 20 deletions src/Database/PicoDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private static function getDatabaseCredentialsFromPdo($pdo, $driver, $dbType)
$schema = $stmt->fetchColumn(); // Fetch the schema name
$timezone = self::convertOffsetToTimeZone(self::getTimeZoneOffset($pdo));
}
elseif ($dbType == PicoDatabaseType::DATABASE_TYPE_MYSQL || $dbType == PicoDatabaseType::DATABASE_TYPE_MARIADB) {
else if ($dbType == PicoDatabaseType::DATABASE_TYPE_MARIADB || $dbType == PicoDatabaseType::DATABASE_TYPE_MYSQL) {
// For MySQL, the schema is the same as the database name
$schema = $databaseName; // MySQL schema is the database name
$timezone = self::convertOffsetToTimeZone(self::getTimeZoneOffset($pdo));
Expand Down Expand Up @@ -370,14 +370,20 @@ private function connectSqlite()
/**
* Connect to the RDMS (Relational Database Management System).
*
* Establishes a connection to an RDMS database using the provided credentials and optionally selects
* a specific database based on the provided flag. Sets the time zone for the connection and handles
* schema settings for PostgreSQL. Charset is also set based on the provided configuration.
* Establishes a connection to an RDMS database using the provided credentials. Optionally, a specific
* database is selected based on the provided flag. This method also configures the time zone, character set,
* and schema settings (for PostgreSQL) after the connection is established.
*
* @param bool $withDatabase Flag to select the database when connected (default is true).
* @return bool True if the connection is successful, false if it fails.
* @throws InvalidDatabaseConfiguration If the database username is empty.
* @throws PDOException If the connection fails with an error.
* - The time zone is set based on the current offset (`date("P")`), or a configured value.
* - For PostgreSQL, the client encoding (charset) is set using `SET CLIENT_ENCODING`, and the schema is set
* using `SET search_path`.
* - For MySQL, the time zone and charset are set using `SET time_zone` and `SET NAMES`.
*
* @param bool $withDatabase Flag to specify whether to select a database upon connection (default is true).
* If true, the database is selected; otherwise, only the connection is made.
* @return bool True if the connection is successfully established, false otherwise.
* @throws InvalidDatabaseConfiguration If the database username is missing from the configuration.
* @throws PDOException If an error occurs during the connection process.
*/
private function connectRDMS($withDatabase = true)
{
Expand All @@ -386,26 +392,28 @@ private function connectRDMS($withDatabase = true)
try {
$connectionString = $this->constructConnectionString($withDatabase);

// Check for database username configuration
// Check if the database username is provided
if (!$this->databaseCredentials->issetUsername()) {
throw new InvalidDatabaseConfiguration("Database username may not be empty. Please check your database configuration!");
}

// Initialize the query to set the timezone
$initialQueries = "SET time_zone = '$timeZoneOffset';";
$initialQueries = "";

// Get charset from database credentials
// Get charset from the database credentials
$charset = addslashes($this->databaseCredentials->getCharset());

// Handle PostgreSQL-specific connection settings
if ($this->getDatabaseType() == PicoDatabaseType::DATABASE_TYPE_PGSQL) {

// Set charset for PostgreSQL if provided (PostgreSQL does not use `SET NAMES`, but you can set the encoding)
// Set time zone for PostgreSQL
$initialQueries = "SET TIMEZONE TO '$timeZoneOffset';";

// Set the client encoding (charset) for PostgreSQL
if ($charset) {
$initialQueries .= "SET CLIENT_ENCODING TO '$charset';";
}

// Set schema for PostgreSQL if it is provided
// Set schema if provided for PostgreSQL
if ($this->databaseCredentials->getDatabaseSchema() != null && $this->databaseCredentials->getDatabaseSchema() != "") {
$initialQueries .= "SET search_path TO " . $this->databaseCredentials->getDatabaseSchema() . ";";
}
Expand All @@ -427,7 +435,9 @@ private function connectRDMS($withDatabase = true)

}
// Handle MySQL-specific connection settings
elseif ($this->getDatabaseType() == PicoDatabaseType::DATABASE_TYPE_MYSQL) {
else if ($this->getDatabaseType() == PicoDatabaseType::DATABASE_TYPE_MARIADB || $this->getDatabaseType() == PicoDatabaseType::DATABASE_TYPE_MYSQL) {
// Set time zone for MySQL
$initialQueries = "SET time_zone='$timeZoneOffset';";

// Add charset to the initial queries for MySQL
if ($charset) {
Expand Down Expand Up @@ -461,7 +471,6 @@ private function connectRDMS($withDatabase = true)
}
return $connected;
}


/**
* Determine the database type based on the provided database type string.
Expand Down Expand Up @@ -581,8 +590,17 @@ public function disconnect()
*/
public function setTimeZoneOffset($timeZoneOffset)
{
$sql = "SET time_zone='$timeZoneOffset';";
$this->execute($sql);
if($this->getDatabaseType() == PicoDatabaseType::DATABASE_TYPE_PGSQL)
{
$sql = "SET TIMEZONE TO '$timeZoneOffset'";
$this->execute($sql);
}
else if($this->getDatabaseType() == PicoDatabaseType::DATABASE_TYPE_MARIADB || $this->getDatabaseType() == PicoDatabaseType::DATABASE_TYPE_MYSQL)
{
$sql = "SET time_zone='$timeZoneOffset';";
$this->execute($sql);
}

return $this;
}

Expand Down Expand Up @@ -1104,4 +1122,4 @@ public function setCallbackDebugQuery($callbackDebugQuery)

return $this;
}
}
}
44 changes: 44 additions & 0 deletions src/Database/PicoTableInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ class PicoTableInfo // NOSONAR
*/
protected $package;

/**
* List of sorted column names.
*
* This property stores an array of column names in their sorted order.
*
* @var array List of sorted column names.
*/
private $sortedColumnName = array();

/**
* Gets an instance of PicoTableInfo.
*
Expand Down Expand Up @@ -123,6 +132,13 @@ public function __construct($tableName, $columns, $joinColumns, $primaryKeys, $a
$this->notNullColumns = $notNullColumns;
$this->noCache = $noCache;
$this->package = $package;

// Store sorted column list
$res = array();
$res = array_merge($res, array_keys($columns));
$res = array_unique($res);

$this->sortedColumnName = $res;
}

/**
Expand Down Expand Up @@ -384,4 +400,32 @@ public function setPackage($package)

return $this;
}

/**
* Get the sorted column names.
*
* This method retrieves the list of sorted column names.
*
* @return array List of sorted column names.
*/
public function getSortedColumnName()
{
return $this->sortedColumnName;
}

/**
* Set the sorted column names.
*
* This method sets the list of sorted column names.
*
* @param array $sortedColumnName List of sorted column names.
*
* @return self Returns the current instance for method chaining.
*/
public function setSortedColumnName($sortedColumnName)
{
$this->sortedColumnName = $sortedColumnName;

return $this;
}
}
36 changes: 0 additions & 36 deletions src/Database/PicoTableInfoExtended.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ class PicoTableInfoExtended extends PicoTableInfo
const PREV_NAME = "prevColumnName"; // Key for the previous column name
const ELEMENT = "element"; // Key for the element

/**
* List of sorted column names.
*
* This property stores an array of column names in their sorted order.
*
* @var array List of sorted column names.
*/
private $sortedColumnName = array();

/**
* Gets an instance of PicoTableInfoExtended.
*
Expand Down Expand Up @@ -303,32 +294,5 @@ public function mergeNotNullColumns($newList)
return $this;
}

/**
* Get the sorted column names.
*
* This method retrieves the list of sorted column names.
*
* @return array List of sorted column names.
*/
public function getSortedColumnName()
{
return $this->sortedColumnName;
}

/**
* Set the sorted column names.
*
* This method sets the list of sorted column names.
*
* @param array $sortedColumnName List of sorted column names.
*
* @return self Returns the current instance for method chaining.
*/
public function setSortedColumnName($sortedColumnName)
{
$this->sortedColumnName = $sortedColumnName;

return $this;
}

}
2 changes: 1 addition & 1 deletion src/Exceptions/ClassNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ClassNotFoundException extends Exception
* @param int $code Exception code
* @param Throwable|null $previous Previous exception
*/
public function __construct($message, $code = 0, Throwable $previous = null)
public function __construct($message, $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
$this->previous = $previous;
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/CurlException.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CurlException extends Exception
* @param int $code Exception code
* @param Throwable|null $previous Previous exception
*/
public function __construct($message, $code = 0, Throwable $previous = null)
public function __construct($message, $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
$this->previous = $previous;
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/EmptyResultException.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class EmptyResultException extends Exception
* @param int $code Exception code
* @param Throwable|null $previous Previous exception
*/
public function __construct($message, $code = 0, Throwable $previous = null)
public function __construct($message, $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
$this->previous = $previous;
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/ErrorConnectionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ErrorConnectionException extends Exception
* @param int $code Exception code
* @param Throwable|null $previous Previous exception
*/
public function __construct($message, $code = 0, Throwable $previous = null)
public function __construct($message, $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
$this->previous = $previous;
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/FindOptionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FindOptionException extends Exception
* @param int $code Exception code
* @param Throwable|null $previous Previous exception
*/
public function __construct($message, $code = 0, Throwable $previous = null)
public function __construct($message, $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
$this->previous = $previous;
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidAddressException.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class InvalidAddressException extends Exception
* @param int $code Exception code
* @param Throwable|null $previous Previous exception
*/
public function __construct($message, $code = 0, Throwable $previous = null)
public function __construct($message, $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
$this->previous = $previous;
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidAnnotationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class InvalidAnnotationException extends Exception
* @param int $code Exception code
* @param Throwable|null $previous Previous exception
*/
public function __construct($message, $code = 0, Throwable $previous = null)
public function __construct($message, $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
$this->previous = $previous;
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidFileFormatException.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class InvalidFileFormatException extends Exception
* @param int $code Exception code
* @param Throwable|null $previous Previous exception
*/
public function __construct($message, $code = 0, Throwable $previous = null)
public function __construct($message, $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
$this->previous = $previous;
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidFilterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class InvalidFilterException extends Exception
* @param int $code Exception code
* @param Throwable|null $previous Previous exception
*/
public function __construct($message, $code = 0, Throwable $previous = null)
public function __construct($message, $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
$this->previous = $previous;
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidInputFormatException.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class InvalidInputFormatException extends Exception
* @param int $code Exception code
* @param Throwable|null $previous Previous exception
*/
public function __construct($message, $code = 0, Throwable $previous = null)
public function __construct($message, $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
$this->previous = $previous;
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidParameterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class InvalidParameterException extends Exception
* @param int $code Exception code
* @param Throwable|null $previous Previous exception
*/
public function __construct($message, $code = 0, Throwable $previous = null)
public function __construct($message, $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
$this->previous = $previous;
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidPolygonException.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class InvalidPolygonException extends Exception
* @param int $code Exception code
* @param Throwable|null $previous Previous exception
*/
public function __construct($message, $code = 0, Throwable $previous = null)
public function __construct($message, $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
$this->previous = $previous;
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidQueryInputException.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class InvalidQueryInputException extends Exception
* @param int $code Exception code
* @param Throwable|null $previous Previous exception
*/
public function __construct($message, $code = 0, Throwable $previous = null)
public function __construct($message, $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
$this->previous = $previous;
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/MandatoryTableNameException.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class MandatoryTableNameException extends Exception
* @param int $code Exception code
* @param Throwable|null $previous Previous exception
*/
public function __construct($message, $code = 0, Throwable $previous = null)
public function __construct($message, $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
$this->previous = $previous;
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/NoColumnMatchException.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class NoColumnMatchException extends Exception
* @param int $code Exception code
* @param Throwable|null $previous Previous exception
*/
public function __construct($message, $code = 0, Throwable $previous = null)
public function __construct($message, $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
$this->previous = $previous;
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/NoColumnUpdatedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class NoColumnUpdatedException extends Exception
* @param int $code Exception code
* @param Throwable|null $previous Previous exception
*/
public function __construct($message, $code = 0, Throwable $previous = null)
public function __construct($message, $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
$this->previous = $previous;
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/NoDatabaseConnectionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class NoDatabaseConnectionException extends Exception
* @param int $code Exception code
* @param Throwable|null $previous Previous exception
*/
public function __construct($message, $code = 0, Throwable $previous = null)
public function __construct($message, $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
$this->previous = $previous;
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/NoInsertableColumnException.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class NoInsertableColumnException extends Exception
* @param int $code Exception code
* @param Throwable|null $previous Previous exception
*/
public function __construct($message, $code = 0, Throwable $previous = null)
public function __construct($message, $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
$this->previous = $previous;
Expand Down
Loading
Loading