-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix long string param support for Oracle (#974)
- Loading branch information
Showing
5 changed files
with
105 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atk4\Data\Persistence\Sql\Oracle; | ||
|
||
use Atk4\Data\Persistence\Sql\Expression as BaseExpression; | ||
|
||
class Expression extends BaseExpression | ||
{ | ||
use ExpressionTrait; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atk4\Data\Persistence\Sql\Oracle; | ||
|
||
trait ExpressionTrait | ||
{ | ||
protected function castLongStringToClobExpr(string $value): Expression | ||
{ | ||
$exprArgs = []; | ||
$buildConcatExprFx = function (array $parts) use (&$buildConcatExprFx, &$exprArgs): string { | ||
if (count($parts) > 1) { | ||
$valueLeft = array_slice($parts, 0, intdiv(count($parts), 2)); | ||
$valueRight = array_slice($parts, count($valueLeft)); | ||
|
||
return 'CONCAT(' . $buildConcatExprFx($valueLeft) . ', ' . $buildConcatExprFx($valueRight) . ')'; | ||
} | ||
|
||
$exprArgs[] = count($parts) > 0 ? reset($parts) : ''; | ||
|
||
return 'TO_CLOB([])'; | ||
}; | ||
|
||
// Oracle SQL (multibyte) string literal is limited to 1332 bytes | ||
$parts = []; | ||
foreach (mb_str_split($value, 10_000) as $shorterValue) { | ||
$lengthBytes = strlen($shorterValue); | ||
$startBytes = 0; | ||
do { | ||
$part = mb_strcut($shorterValue, $startBytes, 1000); | ||
$startBytes += strlen($part); | ||
$parts[] = $part; | ||
} while ($startBytes < $lengthBytes); | ||
} | ||
|
||
$expr = $buildConcatExprFx($parts); | ||
|
||
return $this->expr($expr, $exprArgs); // @phpstan-ignore-line | ||
} | ||
|
||
protected function updateRenderBeforeExecute(array $render): array | ||
{ | ||
[$sql, $params] = parent::updateRenderBeforeExecute($render); | ||
|
||
$newParamBase = $this->paramBase; | ||
$newParams = []; | ||
$sql = preg_replace_callback( | ||
'~(?:\'(?:\'\'|\\\\\'|[^\'])*\')?+\K:\w+~s', | ||
function ($matches) use ($params, &$newParams, &$newParamBase) { | ||
$value = $params[$matches[0]]; | ||
if (is_string($value) && strlen($value) > 4000) { | ||
$expr = $this->castLongStringToClobExpr($value); | ||
unset($value); | ||
[$exprSql, $exprParams] = $expr->render(); | ||
$sql = preg_replace_callback( | ||
'~(?:\'(?:\'\'|\\\\\'|[^\'])*\')?+\K:\w+~s', | ||
function ($matches) use ($exprParams, &$newParams, &$newParamBase) { | ||
$name = ':' . $newParamBase; | ||
++$newParamBase; | ||
$newParams[$name] = $exprParams[$matches[0]]; | ||
|
||
return $name; | ||
}, | ||
$exprSql | ||
); | ||
} else { | ||
$sql = ':' . $newParamBase; | ||
++$newParamBase; | ||
|
||
$newParams[$sql] = $value; | ||
} | ||
|
||
return $sql; | ||
}, | ||
$sql | ||
); | ||
|
||
return [$sql, $newParams]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters