Skip to content

Commit

Permalink
correcao
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno Morais authored and Bruno Morais committed Nov 21, 2023
1 parent 99a7ee1 commit 701a870
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 37 deletions.
12 changes: 0 additions & 12 deletions src/CrudBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 41 additions & 25 deletions src/DatalayerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 701a870

Please sign in to comment.