prepare($sql) )) { $statement->setFetchMode(\PDO::FETCH_ASSOC); $this->execute($statement, $parameters, false); return $statement; } } catch (\Throwable $e) { throw new \PdoException($e->getMessage() . " `$sql` with data:" . json_encode($parameters)); } } public function __destruct() { if ($this->onClose ?? null) { call_user_func($this->onClose, $this); } } public function runQuery(string $sql, array $parameters = []): ? static { static::$dump && call_user_func_array(static::$dump, [ $sql, $parameters ]); try { if (false !== ( $statement = $this->prepare($sql) )) { return $this->execute($statement, $parameters, true); } } catch (\Throwable $e) { throw new \PdoException($e->getMessage() . " `$sql` with data:" . json_encode($parameters)); } return null; } public function runInsertQuery(string $sql, array $parameters = []) : ? static { return $this->runQuery($sql, $parameters); } public function runUpdateQuery(string $sql, array $parameters = []) : ? static { return $this->runQuery($sql, $parameters); } public function runDeleteQuery(string $sql, array $parameters = []) : ? static { return $this->runQuery($sql, $parameters); } public function execute(PDOStatement $statement, array $parameters = [], bool $commit = true) : ? static { $this->executionStatus = false; $this->lastInsertId = null; try { if ( ! $this->inTransaction() ) { $this->beginTransaction(); } $this->bindVariables($statement, $parameters); $this->executionStatus = $statement->execute(); if ( $this->executionStatus ) { $this->lastInsertId = $this->lastInsertId(); $this->rowCount = $statement->rowCount(); if ( $commit ) { $this->commit(); } return $this; } else { throw new \PDOException($statement->errorCode() . " - " . json_encode($statement->errorInfo())); } } catch (\PDOException $e) { $this->rollback(); throw $e; } catch (\Throwable $e) { throw $e; } return null; } protected function bindVariables(PDOStatement $statement, array &$parameters) : void { if ($parameters) { if (array_is_list($parameters)) { $parameters = array_combine(range(1, count($parameters)), array_values($parameters)); } else { foreach ($parameters as $key => $value) { switch (strtolower(gettype($value))) { #$type = Pdo::PARAM_BOOL; #break; case "boolean": case "integer": $type = Pdo::PARAM_INT; break; case "null": $type = Pdo::PARAM_NULL; break; case "string": default: $type = Pdo::PARAM_STR; } $statement->bindValue($key, $value, $type); } } } } }