Skip to content

Commit

Permalink
Fix: #20171: Support JSON columns for MariaDB 10.4 or higher
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed May 29, 2024
1 parent c413c7b commit 755463f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Yii Framework 2 Change Log
- Bug #20141: Update `ezyang/htmlpurifier` dependency to version `4.17` (@terabytesoftw)
- Bug #19817: Add MySQL Query `addCheck()` and `dropCheck()` (@bobonov)
- Bug #20165: Adjust pretty name of closures for PHP 8.4 compatibility (@staabm)
- Enh: #20171: Support JSON columns for MariaDB 10.4 or higher (@terabytesoftw)

2.0.49.2 October 12, 2023
-------------------------
Expand Down
2 changes: 1 addition & 1 deletion db/mysql/JsonExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public function build(ExpressionInterface $expression, array &$params = [])
$placeholder = static::PARAM_PREFIX . count($params);
$params[$placeholder] = Json::encode($value);

return "CAST($placeholder AS JSON)";
return $placeholder;
}
}
25 changes: 25 additions & 0 deletions db/mysql/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,19 @@ protected function findColumns($table)
}
throw $e;
}


$jsonColumns = $this->getJsonColumns($table);

foreach ($columns as $info) {
if ($this->db->slavePdo->getAttribute(\PDO::ATTR_CASE) !== \PDO::CASE_LOWER) {
$info = array_change_key_case($info, CASE_LOWER);
}

if (\in_array($info['field'], $jsonColumns, true)) {
$info['type'] = static::TYPE_JSON;
}

$column = $this->loadColumnSchema($info);
$table->columns[$column->name] = $column;
if ($column->isPrimaryKey) {
Expand Down Expand Up @@ -641,4 +650,20 @@ private function loadTableConstraints($tableName, $returnType)

return $result[$returnType];
}

private function getJsonColumns(TableSchema $table): array
{
$sql = $this->getCreateTableSql($table);
$result = [];

$regexp = '/json_valid\([\`"](.+)[\`"]\s*\)/mi';

if (\preg_match_all($regexp, $sql, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
$result[] = $match[1];
}
}

return $result;
}
}

0 comments on commit 755463f

Please sign in to comment.