Skip to content

Commit

Permalink
add method disableBacktick for driver pgsql & sqlsrv
Browse files Browse the repository at this point in the history
  • Loading branch information
mavinoo committed Jun 30, 2021
1 parent 46300f0 commit d3392a0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
14 changes: 6 additions & 8 deletions src/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ public function update(Model $table, array $values, string $index = null, bool $
$value = (is_null($val[$field]) ? 'NULL' : $finalField);
}

if ($driver == 'pgsql')
if (Common::disableBacktick($driver))
$final[$field][] = 'WHEN ' . $index . ' = \'' . $val[$index] . '\' THEN ' . $value . ' ';
else
$final[$field][] = 'WHEN `' . $index . '` = \'' . $val[$index] . '\' THEN ' . $value . ' ';
}
}
}

if ($driver == 'pgsql') {
if (Common::disableBacktick($driver)) {

$cases = '';
foreach ($final as $k => $v) {
Expand Down Expand Up @@ -189,18 +189,17 @@ public function updateWithTwoIndex(Model $table, array $values, string $index =
$finalField = $raw ? Common::mysql_escape($val[$field]) : "'" . Common::mysql_escape($val[$field]) . "'";
$value = (is_null($val[$field]) ? 'NULL' : $finalField);

if ($driver == 'pgsql') {
if (Common::disableBacktick($driver)) {
$final[$field][] = 'WHEN (' . $index . ' = \'' . Common::mysql_escape($val[$index]) . '\' AND ' . $index2 . ' = \'' . $val[$index2] . '\') THEN ' . $value . ' ';
}
else {
} else {
$final[$field][] = 'WHEN (`' . $index . '` = "' . Common::mysql_escape($val[$index]) . '" AND `' . $index2 . '` = "' . $val[$index2] . '") THEN ' . $value . ' ';
}
}
}
}


if ($driver == 'pgsql') {
if (Common::disableBacktick($driver)) {
$cases = '';
foreach ($final as $k => $v) {
$cases .= '"' . $k . '" = (CASE ' . implode("\n", $v) . "\n"
Expand All @@ -209,8 +208,7 @@ public function updateWithTwoIndex(Model $table, array $values, string $index =

$query = "UPDATE \"" . $this->getFullTableName($table) . '" SET ' . substr($cases, 0, -2) . " WHERE \"$index\" IN('" . implode("','", $ids) . "') AND \"$index2\" IN('" . implode("','", $ids2) . "');";
//$query = "UPDATE \"" . $this->getFullTableName($table) . "\" SET " . substr($cases, 0, -2) . " WHERE \"$index\" IN(" . '"' . implode('","', $ids) . '")' . " AND \"$index2\" IN(" . '"' . implode('","', $ids2) . '"' . " );";
}
else {
} else {
$cases = '';
foreach ($final as $k => $v) {
$cases .= '`' . $k . '` = (CASE ' . implode("\n", $v) . "\n"
Expand Down
35 changes: 21 additions & 14 deletions src/Common/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public static function mysql_escape($fieldValue)
}

if (is_bool($fieldValue)) {
return (int) $fieldValue;
return (int)$fieldValue;
}

if(self::is_json($fieldValue)){
if (self::is_json($fieldValue)) {
return self::safeJson($fieldValue);
}

Expand All @@ -35,7 +35,13 @@ public static function mysql_escape($fieldValue)
return $fieldValue;
}

protected static function safeJsonString($fieldValue){
public static function disableBacktick($drive)
{
return in_array($drive, ['pgsql', 'sqlsrv']);
}

protected static function safeJsonString($fieldValue)
{
return str_replace(
["'"],
["''"],
Expand All @@ -45,26 +51,27 @@ protected static function safeJsonString($fieldValue){

protected static function is_json($str): bool
{
if (!is_string($str)){
if (!is_string($str)) {
return false;
}
return json_decode($str, true) !== null;
}

protected static function safeJson($jsonData,$asArray = false){
$jsonData = json_decode($jsonData,true);
protected static function safeJson($jsonData, $asArray = false)
{
$jsonData = json_decode($jsonData, true);
$safeJsonData = [];
if (!is_array($jsonData)){
if (!is_array($jsonData)) {
return $jsonData;
}
foreach ($jsonData as $key => $value){
if (self::is_json($value)){
$safeJsonData[$key] = self::safeJson($value,true);
}elseif(is_string($value)){
foreach ($jsonData as $key => $value) {
if (self::is_json($value)) {
$safeJsonData[$key] = self::safeJson($value, true);
} elseif (is_string($value)) {
$safeJsonData[$key] = self::safeJsonString($value);
}elseif(is_array($value)){
$safeJsonData[$key] = self::safeJson(json_encode($value),true);
}else{
} elseif (is_array($value)) {
$safeJsonData[$key] = self::safeJson(json_encode($value), true);
} else {
$safeJsonData[$key] = $value;
}
}
Expand Down

0 comments on commit d3392a0

Please sign in to comment.