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.11 #46

Merged
merged 10 commits into from
Dec 26, 2024
25 changes: 17 additions & 8 deletions src/Database/PicoDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ private function connectRDMS($withDatabase = true)
throw new InvalidDatabaseConfiguration("Database username may not be empty. Please check your database configuration!");
}

$initialQueries = "";
$initialQueries = array();

// Get charset from the database credentials
$charset = addslashes($this->databaseCredentials->getCharset());
Expand All @@ -406,16 +406,16 @@ private function connectRDMS($withDatabase = true)
if ($this->getDatabaseType() == PicoDatabaseType::DATABASE_TYPE_PGSQL) {

// Set time zone for PostgreSQL
$initialQueries = "SET TIMEZONE TO '$timeZoneOffset';";
$initialQueries[] = "SET TIMEZONE TO '$timeZoneOffset';";

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

// Set schema if provided for PostgreSQL
if ($this->databaseCredentials->getDatabaseSchema() != null && $this->databaseCredentials->getDatabaseSchema() != "") {
$initialQueries .= "SET search_path TO " . $this->databaseCredentials->getDatabaseSchema() . ";";
$initialQueries[] = "SET search_path TO " . $this->databaseCredentials->getDatabaseSchema() . ";";
}

// PostgreSQL connection setup
Expand All @@ -430,18 +430,21 @@ private function connectRDMS($withDatabase = true)

// Execute the initial queries (timezone, charset, schema) in PostgreSQL
if (!empty($initialQueries)) {
$this->databaseConnection->exec($initialQueries);
foreach($initialQueries as $initialQuery)
{
$this->databaseConnection->exec($initialQuery);
}
}

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

// Add charset to the initial queries for MySQL
if ($charset) {
$initialQueries .= "SET NAMES '$charset';"; // Set charset for MySQL
$initialQueries[] = "SET NAMES '$charset';"; // Set charset for MySQL
}

// MySQL connection setup
Expand All @@ -450,11 +453,17 @@ private function connectRDMS($withDatabase = true)
$this->databaseCredentials->getUsername(),
$this->databaseCredentials->getPassword(),
[
PDO::MYSQL_ATTR_INIT_COMMAND => $initialQueries,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_FOUND_ROWS => true
]
);

if (!empty($initialQueries)) {
foreach($initialQueries as $initialQuery)
{
$this->databaseConnection->exec($initialQuery);
}
}
}
// If the database type is neither MySQL nor PostgreSQL, throw an exception
else {
Expand Down
102 changes: 0 additions & 102 deletions src/Database/PicoDatabaseCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,106 +112,4 @@ class PicoDatabaseCredentials extends SecretObject
*/
protected $charset;

/**
* Get the database driver.
*
* @return string Returns the database driver.
*/
public function getDriver()
{
return $this->driver;
}

/**
* Get the database host.
*
* @return string Returns the database host.
*/
public function getHost()
{
return $this->host;
}

/**
* Get the database port.
*
* @return int Returns the database port.
*/
public function getPort()
{
return $this->port;
}

/**
* Get the database username.
*
* @return string Returns the database username.
*/
public function getUsername()
{
return $this->username;
}

/**
* Get the database password.
*
* @return string Returns the database password.
*/
public function getPassword()
{
return $this->password;
}

/**
* Get the database name.
*
* @return string Returns the database name.
*/
public function getDatabaseName()
{
return $this->databaseName;
}

/**
* Get the database schema.
*
* @return string Returns the database schema.
*/
public function getDatabaseSchema()
{
return $this->databaseSchema;
}

/**
* Get the application time zone.
*
* @return string Returns the application time zone.
*/
public function getTimeZone()
{
return $this->timeZone;
}

/**
* Get the charset.
*
* @return string The charset currently set.
*/
public function getCharset()
{
return $this->charset;
}

/**
* Set the charset.
*
* @param string $charset The charset to set.
* @return self Returns the current instance for method chaining.
*/
public function setCharset($charset)
{
$this->charset = $charset;
return $this;
}

}
40 changes: 26 additions & 14 deletions src/Database/PicoPageControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,13 @@ public function setNavigation($prev = null, $next = null, $first = null, $last =
*/
public function setMargin($margin)
{
$this->pageData->generatePagination($margin);
if(isset($margin) && $margin > 0)
{
$this->pageData->generatePagination($margin);
}
return $this;
}

/**
* Converts the pagination control to HTML format.
*
* @return string HTML representation of the pagination controls.
*/
public function toHTML()
{
return $this->__toString();
}

/**
* Gets the template for rendering specific page numbers.
*
Expand Down Expand Up @@ -218,7 +211,10 @@ public function getFormatPageNumber()
*/
public function setFormatPageNumber($formatPageNumber)
{
$this->formatPageNumber = $formatPageNumber;
if(isset($formatPageNumber) && !empty($formatPageNumber))
{
$this->formatPageNumber = $formatPageNumber;
}
return $this;
}

Expand Down Expand Up @@ -260,7 +256,10 @@ public function getFormatStepOne()
*/
public function setFormatStepOne($formatStepOne)
{
$this->formatStepOne = $formatStepOne;
if(isset($formatStepOne) && !empty($formatStepOne))
{
$this->formatStepOne = $formatStepOne;
}
return $this;
}

Expand Down Expand Up @@ -303,7 +302,10 @@ public function getFormatStartEnd()
*/
public function setFormatStartEnd($formatStartEnd)
{
$this->formatStartEnd = $formatStartEnd;
if(isset($formatStartEnd) && !empty($formatStartEnd))
{
$this->formatStartEnd = $formatStartEnd;
}
return $this;
}

Expand Down Expand Up @@ -374,6 +376,16 @@ public function setPaginationConfig($paginationConfig)
);
}

/**
* Converts the pagination control to HTML format.
*
* @return string HTML representation of the pagination controls.
*/
public function toHTML()
{
return $this->__toString();
}

/**
* Generates the HTML for pagination controls.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Language/PicoEntityLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class PicoEntityLanguage
*/
private $_entityLanguage = ""; // NOSONAR

private $_defaultLabel = array(); // NOSONAR

/**
* Constructor
*
Expand Down Expand Up @@ -118,6 +120,8 @@ public function loadEntityLabel($entity)
}
}
}

$this->_defaultLabel = $defaultLanguage;

$this->addLanguage($this->_entityLanguage, $defaultLanguage, true);
return $this;
Expand Down Expand Up @@ -365,4 +369,13 @@ public function __toString()
return "{}";
}
}

/**
* Get the value of defaultLabel
* @return array Get default label of the entity
*/
public function getDefaultLabel()
{
return $this->_defaultLabel;
}
}
2 changes: 1 addition & 1 deletion src/Util/Database/PicoDatabaseUtilMySql.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function fixDefaultValue($defaultValue, $type)
$result = $defaultValue;
if(stripos($type, 'tinyint(1)') !== false || self::isTypeBoolean($type))
{
$result = $defaultValue != 0 ? 'true' : 'false';
$result = ($defaultValue != 0 || strtolower($defaultValue) == 'true') ? 'true' : 'false';
}
else if(self::isNativeValue($defaultValue))
{
Expand Down
2 changes: 1 addition & 1 deletion src/Util/Database/PicoDatabaseUtilPostgreSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public function fixDefaultValue($defaultValue, $type)
$result = $defaultValue;
if(self::isTypeBoolean($type))
{
$result = $defaultValue != 0 ? 'true' : 'false';
$result = ($defaultValue != 0 || strtolower($defaultValue) == 'true') ? 'true' : 'false';
}
else if(self::isNativeValue($defaultValue)) {
$result = $defaultValue;
Expand Down
25 changes: 16 additions & 9 deletions src/Util/Database/PicoDatabaseUtilSqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,10 @@ private function determineSqlType($column, $autoIncrementKeys = null, $length =
'float' => 'REAL',
'text' => 'TEXT',
'longtext' => 'TEXT',
'datetime' => 'DATETIME',
'date' => 'DATE',
'timestamp' => 'TIMESTAMP',
'time' => 'TIME',
'blob' => 'BLOB',
);

Expand Down Expand Up @@ -228,13 +230,14 @@ public function getColumnList($database, $tableName)
// Fetch and display the column details
$rows = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
error_log(json_encode($row, JSON_PRETTY_PRINT));
$rows[] = array(
"Field" => $row['name'],
"Type" => $row['type'],
"Null" => $row['notnull'] ? 'YES' : 'NO',
"Key" => $row['pk'] ? 'PRI' : null,
"Field" => $row['name'],
"Type" => $row['type'],
"Null" => $row['notnull'] == 1 ? 'NO' : 'YES',
"Key" => $row['pk'] ? 'PRI' : null,
"Default" => $row['dflt_value'] ? $row['dflt_value'] : null,
"Extra" => ($row['pk'] == 1 && strtoupper($row['type']) === 'INTEGER') ? 'auto_increment' : null
"Extra" => ($row['pk'] == 1 && strtoupper($row['type']) === 'INTEGER') ? 'auto_increment' : null
);
}
return $rows;
Expand Down Expand Up @@ -333,7 +336,10 @@ private function mysqlToSqliteType($type) // NOSONAR
// Define a mapping of common MySQL types to SQLite types
$map = array(
'tinyint(1)' => 'BOOLEAN', // MySQL 'tinyint(1)' maps to SQLite 'BOOLEAN'
'smallint' => 'INT', // MySQL 'smallint' maps to SQLite 'INT'
'mediumint' => 'INT', // MySQL 'mediumint' maps to SQLite 'INT'
'integer' => 'INTEGER', // MySQL 'integer' maps to SQLite 'INTEGER'
'int' => 'INT', // MySQL 'int' maps to SQLite 'INT'
'float' => 'REAL', // MySQL 'float' maps to SQLite 'REAL'
'double' => 'REAL', // MySQL 'double' maps to SQLite 'REAL'
'decimal' => 'NUMERIC', // MySQL 'decimal' maps to SQLite 'NUMERIC'
Expand All @@ -342,9 +348,10 @@ private function mysqlToSqliteType($type) // NOSONAR
'mediumtext' => 'TEXT', // MySQL 'mediumtext' maps to SQLite 'TEXT'
'longtext' => 'TEXT', // MySQL 'longtext' maps to SQLite 'TEXT'
'text' => 'TEXT', // MySQL 'text' maps to SQLite 'TEXT'
'date' => 'TEXT', // MySQL 'date' maps to SQLite 'TEXT'
'datetime' => 'TEXT', // MySQL 'datetime' maps to SQLite 'TEXT'
'timestamp' => 'TEXT' // MySQL 'timestamp' maps to SQLite 'TEXT'
'datetime' => 'DATETIME', // MySQL 'datetime' maps to SQLite 'DATETIME'
'date' => 'DATE', // MySQL 'date' maps to SQLite 'DATE'
'timestamp' => 'TIMESTAMP', // MySQL 'timestamp' maps to SQLite 'TIMESTAMP'
'time' => 'TIME', // MySQL 'time' maps to SQLite 'TIME'
);

// Handle 'enum' types and convert them to 'NVARCHAR' with length based on max enum value length + 2
Expand Down Expand Up @@ -471,7 +478,7 @@ public function fixDefaultValue($defaultValue, $type)
$result = $defaultValue;
if(self::isTypeBoolean($type))
{
$result = $defaultValue != 0 ? 'true' : 'false';
$result = ($defaultValue != 0 || strtolower($defaultValue) == 'true') ? 'true' : 'false';
}
else if(self::isNativeValue($defaultValue))
{
Expand Down
Loading