aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan-Hendrik Willms <tleilax+studip@gmail.com>2025-01-27 13:03:54 +0000
committerJan-Hendrik Willms <tleilax+studip@gmail.com>2025-01-27 13:03:54 +0000
commite4bf27a6e37efa3d7fd5decabacfd2dc94823aa6 (patch)
treeae5b7708166b82809f39910bd431b37f5270d166
parentd031d9406ebce1b40ca94d137caa199a84e75d24 (diff)
fix sorm default string values for mariadb >= 10.2.7, fixes #4462
Closes #4462 Merge request studip/studip!3250
-rw-r--r--lib/bootstrap.php5
-rw-r--r--lib/classes/SimpleORMap.php30
-rw-r--r--lib/classes/StudipPDO.php16
3 files changed, 49 insertions, 2 deletions
diff --git a/lib/bootstrap.php b/lib/bootstrap.php
index 0ba2599..e03785a 100644
--- a/lib/bootstrap.php
+++ b/lib/bootstrap.php
@@ -164,6 +164,11 @@ if (Studip\ENV === 'production') {
DBManager::get('studip-slave')->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
}
+// Check if we need to enable the fix for column default values for mariadb >= 10.2.7
+if (DBManager::get()->isMariaDB('10.2.7')) {
+ SimpleORMap::setMariadbDefaultColumnFix();
+}
+
// set default exception handler
// command line or http request?
if (isset($_SERVER['REQUEST_METHOD'])) {
diff --git a/lib/classes/SimpleORMap.php b/lib/classes/SimpleORMap.php
index bea9595..689747a 100644
--- a/lib/classes/SimpleORMap.php
+++ b/lib/classes/SimpleORMap.php
@@ -90,6 +90,13 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
protected string $i18n_class = I18NString::class;
/**
+ * Defines whether we need to fix the default values for a column
+ *
+ * @var bool $mariadb_column_default_fix
+ */
+ protected static $mariadb_column_default_fix = false;
+
+ /**
* name of db table
* @return string
*/
@@ -1526,6 +1533,21 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
$default_value = '0';
}
}
+
+ /* @see https://gitlab.studip.de/studip/studip/-/issues/4462 */
+ if (self::$mariadb_column_default_fix) {
+ if ($default_value === 'NULL') {
+ $default_value = null;
+ } elseif (
+ is_string($default_value)
+ && strlen($default_value) >= 2
+ && $default_value[0] === "'"
+ ) {
+ $default_value = str_replace("''", "'", $default_value);
+ $default_value = stripslashes($default_value);
+ $default_value = substr($default_value, 1, -1);
+ }
+ }
}
} else {
$default_value = $this->default_values()[$field];
@@ -2544,4 +2566,12 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
$this->resetRelation($relation);
}
}
+
+ /**
+ * Enable/disable the fix for mariadb >= 10.2.7 default column values.
+ */
+ public static function setMariadbDefaultColumnFix(bool $state = true): void
+ {
+ self::$mariadb_column_default_fix = $state;
+ }
}
diff --git a/lib/classes/StudipPDO.php b/lib/classes/StudipPDO.php
index 77046f7..8a7d149 100644
--- a/lib/classes/StudipPDO.php
+++ b/lib/classes/StudipPDO.php
@@ -376,10 +376,22 @@ class StudipPDO extends PDO
/**
* Determine if the connected database is a MariaDB database.
*
+ * @param string|null $min_version Optional check for a specific minimal
+ * version
* @return bool
*/
- public function isMariaDB(): bool
+ public function isMariaDB(?string $min_version = null): bool
{
- return stripos($this->getAttribute(\PDO::ATTR_SERVER_VERSION), 'MariaDB') !== false;
+ $db_version = $this->getAttribute(\PDO::ATTR_SERVER_VERSION);
+
+ if (stripos($db_version, 'MariaDB') === false) {
+ return false;
+ }
+
+ if ($min_version === null) {
+ return true;
+ }
+
+ return version_compare($db_version, $min_version) >= 0;
}
}