Skip to content

Commit

Permalink
adicao de log sql
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobmorais committed Jan 25, 2023
1 parent 80da6c6 commit fca83ba
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/Crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ public function lastInsertId()
}

/**
* @return mixed
* @return string|null
*/
public function getLogSQL(){
public function getLogSQL():?string
{
return $this->logSQL;
}

Expand Down
15 changes: 11 additions & 4 deletions src/DatalayerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function executeSQL(String $query, ?array $params = null)
$this->getInstance();
$this->prepare = $this->instance->prepare($query);
$this->prepare->execute($params);
$this->logSQL = $this->prepare->queryString;
$this->setLogSQL($query, $params);
} catch (PDOException $e) {
Connect::setError($e,$query);
return false;
Expand Down Expand Up @@ -245,7 +245,7 @@ protected function selectDB($sql, $params = null, $class = null)
$this->getInstance();
$this->prepare = $this->instance->prepare($sql);
$this->prepare->execute($params);
$this->logSQL = $this->prepare->queryString;
$this->setLogSQL($sql, $params);

if (!empty($class)) {
$rs = $this->fetchArrayClass($this->prepare,$class);
Expand Down Expand Up @@ -289,7 +289,7 @@ protected function updateDB($sql, $params = null): bool
$this->getInstance();
$query = $this->instance->prepare($sql);
$rs = $query->execute($params);
$this->logSQL = $this->prepare->queryString;
$this->setLogSQL($sql, $params);
} catch (PDOException $e) {
Connect::setError($e,$sql);
return false;
Expand All @@ -308,7 +308,7 @@ protected function deleteDB($sql, $params = null): bool
$this->getInstance();;
$this->prepare = $this->instance->prepare($sql);
$rs = $this->prepare->execute($params);
$this->logSQL = $this->prepare->queryString;
$this->setLogSQL($sql, $params);
} catch (PDOException $e) {
Connect::setError($e,$sql);
return false;
Expand All @@ -334,4 +334,11 @@ protected function printErrorInfo(): array
{
return $this->getInstance($this->database)->errorInfo();
}

private function setLogSQL($sql, $placeholders){
foreach($placeholders as $k => $v){
$sql = preg_replace('/:'.$k.'/',"'".$v."'",$sql);
}
$this->logSQL = $sql;
}
}

0 comments on commit fca83ba

Please sign in to comment.