From 701a870a64c527f47b4dc95e0689dd7094a96e24 Mon Sep 17 00:00:00 2001 From: Bruno Morais Date: Tue, 21 Nov 2023 09:20:04 -0300 Subject: [PATCH] correcao --- src/CrudBuilder.php | 12 -------- src/DatalayerTrait.php | 66 ++++++++++++++++++++++++++---------------- 2 files changed, 41 insertions(+), 37 deletions(-) diff --git a/src/CrudBuilder.php b/src/CrudBuilder.php index d742d2c..0dff59a 100644 --- a/src/CrudBuilder.php +++ b/src/CrudBuilder.php @@ -393,18 +393,6 @@ protected function lastInsertId(): ?string } } - /** - * @return string|null - */ - protected function getSQL(): ?string - { - try { - return $this->logSQL ?? ""; - } catch (\PDOException $e) { - $this->setError($e); - } - } - /** * @param $params * @return self diff --git a/src/DatalayerTrait.php b/src/DatalayerTrait.php index 459a3d3..e90ead0 100755 --- a/src/DatalayerTrait.php +++ b/src/DatalayerTrait.php @@ -230,7 +230,7 @@ protected function executeSQL(string $query, ?array $params = null) { try { $this->setPrepare($this->getInstance()->prepare($query)); - $this->setLogSQL($query, $params); + $this->setSQL($query, $params); $this->getPrepare()->execute($params); return $this->getPrepare(); } catch (PDOException $e) { @@ -404,36 +404,52 @@ private function lastId(): ?string * @param array|null $params * @return void */ - private function setLogSQL($sql_string, ?array $params = null) + private function setSQL($sql_string, ?array $params = null) { - if (!empty($params)) { - $indexed = $params == array_values($params); - foreach ($params as $k => $v) { - if (is_object($v)) { - if ($v instanceof \DateTime) { - $v = $v->format('Y-m-d H:i:s'); - } else { - continue; + try { + if (!empty($params)) { + $indexed = $params == array_values($params); + foreach ($params as $k => $v) { + if (is_object($v)) { + if ($v instanceof \DateTime) { + $v = $v->format('Y-m-d H:i:s'); + } else { + continue; + } + } elseif (is_string($v)) { + $v = "'$v'"; + } elseif ($v === null) { + $v = 'NULL'; + } elseif (is_array($v)) { + $v = implode(',', $v); } - } elseif (is_string($v)) { - $v = "'$v'"; - } elseif ($v === null) { - $v = 'NULL'; - } elseif (is_array($v)) { - $v = implode(',', $v); - } - if ($indexed) { - $sql_string = preg_replace('/\?/', $v, $sql_string, 1); - } else { - if ($k[0] != ':') { - $k = ':' . $k; - } //add leading colon if it was left out - $sql_string = str_replace($k, $v, $sql_string); + if ($indexed) { + $sql_string = preg_replace('/\?/', $v, $sql_string, 1); + } else { + if ($k[0] != ':') { + $k = ':' . $k; + } //add leading colon if it was left out + $sql_string = str_replace($k, $v, $sql_string); + } } } + $this->logSQL = $sql_string; + } catch (PDOException $e) { + $this->setError($e); + } + } + + /** + * @return string|null + */ + protected function getSQL(): ?string + { + try { + return $this->logSQL ?? ""; + } catch (\PDOException $e) { + $this->setError($e); } - $this->logSQL = $sql_string; } /**