Skip to content

Commit

Permalink
backported new database util
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Nov 14, 2023
1 parent f091958 commit 82d159c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file.

## [2.232.0] - 2023-11-14
- Changed: backported Utils/DatabaseUtil from v3 to v2
- Deprecated: old DatabaseUtil class

## [2.231.0] - 2023-10-24
- Added: Utils/ModelUtil::findParentsRecursively()
- Deprecated: ModelUtil::findParentsRecursively()
Expand Down
4 changes: 4 additions & 0 deletions src/Database/DatabaseUtil.php
Expand Up @@ -323,9 +323,13 @@ function ($val) {
* - inline_values: (bool) Inline the values in the sql part instead of using ? ('REGEXP (':"3"')' instead of 'REGEXP (?)'). Return value not change (still an array with an values index)
*
* @return array
*
* @deprecated Use Utils service instead
*/
public function createWhereForSerializedBlob(string $field, array $values, string $connective = self::SQL_CONDITION_OR, array $options = [])
{
trigger_deprecation('heimrichhannot/contao-utils-bundle', '2.232.0', 'Using %s is deprecated and will no longer work in Utils Bundle 3.0. Use the Utils service instead.', __METHOD__);

$where = null;
$returnValues = [];
$inlineValues = $options['inline_values'] ?? false;
Expand Down
19 changes: 19 additions & 0 deletions src/Util/DatabaseUtil.php
@@ -0,0 +1,19 @@
<?php

namespace HeimrichHannot\UtilsBundle\Util;

use HeimrichHannot\UtilsBundle\Util\DatabaseUtil\CreateWhereForSerializedBlobResult;

class DatabaseUtil
{
/**
* Create a where condition for a field that contains a serialized blob.
*
* @param string $field A field containing a serialized array.
* @param array $values The values that should be searched for in the field.
*/
public function createWhereForSerializedBlob(string $field, array $values): CreateWhereForSerializedBlobResult
{
return new CreateWhereForSerializedBlobResult($field, $values);
}
}
68 changes: 68 additions & 0 deletions src/Util/DatabaseUtil/CreateWhereForSerializedBlobResult.php
@@ -0,0 +1,68 @@
<?php

namespace HeimrichHannot\UtilsBundle\Util\DatabaseUtil;

class CreateWhereForSerializedBlobResult
{
/**
* @var string
*/
public $field;
/**
* @var array
*/
public $values;

public function __construct(
string $field,
array $values
) {
$this->field = $field;
$this->values = $values;
}

/**
* Return the where query with AND operation for each value. Values are inlined in the query ('REGEXP (':"3"')' instead of 'REGEXP (?)').
*/
public function createInlineAndWhere(): string
{
return '('.$this->field.' REGEXP ('.implode(") AND ".$this->field.' REGEXP (', $this->getValueList()).'))';
}

/**
* Return the where query with OR operation for each value. Values are inlined in the query ('REGEXP (':"3"')' instead of 'REGEXP (?)').
*/
public function createInlineOrWhere(): string
{
return '('.$this->field.' REGEXP ('.implode(") OR ".$this->field.' REGEXP (', $this->getValueList()).'))';
}

/**
* Return the where query with AND operation and placeholder for each value ('REGEXP (?)'). Values can be obtained from the values property.
*/
public function createAndWhere(): string
{

return '('.$this->field.' REGEXP ('.implode(") AND ".$this->field.' REGEXP (', array_fill(0, count($this->getValueList()), '?')).'))';
}

/**
* Return the where query with OR operation and placeholder for each value ('REGEXP (?)'). Values can be obtained from the values property.
*/
public function createOrWhere(): string
{
return '('.$this->field.' REGEXP ('.implode(") OR ".$this->field.' REGEXP (', array_fill(0, count($this->getValueList()), '?')).'))';
}

private function getValueList(): array
{
$returnValues = [];

foreach ($this->values as $val) {
$returnValues[] = ":\"$val\"";
}

return $returnValues;
}

}
6 changes: 6 additions & 0 deletions src/Util/Utils.php
Expand Up @@ -53,6 +53,11 @@ public function container(): ContainerUtil
return $this->locator->get(ContainerUtil::class);
}

public function database(): DatabaseUtil
{
return $this->locator->get(DatabaseUtil::class);
}

public function dca(): DcaUtil
{
return $this->locator->get(DcaUtil::class);
Expand Down Expand Up @@ -109,6 +114,7 @@ public static function getSubscribedServices()
AccordionUtil::class,
ArrayUtil::class,
ContainerUtil::class,
DatabaseUtil::class,
DcaUtil::class,
FileUtil::class,
HtmlUtil::class,
Expand Down

0 comments on commit 82d159c

Please sign in to comment.