Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace gspf\base;
- use gspf\message\error;
- class pdo_statement
- {
- /**
- * @var profiler
- */
- private $profiler;
- /**
- * @var \PDOStatement
- */
- private $pdo_statement;
- private $bind_params = array();
- /**
- * @param $pdo_statement \PDOStatement
- * @param $profiler
- */
- public function __construct($pdo_statement, $profiler)
- {
- $this->profiler = $profiler;
- $this->pdo_statement = $pdo_statement;
- }
- public function fetch($fetch_style = null, $cursor_orientation = \PDO::FETCH_ORI_NEXT, $cursor_offset = 0)
- {
- try
- {
- /** @noinspection PhpUnusedLocalVariableInspection */
- $holder = new guarded_profiler($this->profiler, __METHOD__);
- $result = $this->pdo_statement->fetch($fetch_style, $cursor_orientation, $cursor_offset);
- $holder->set_size(1);
- }
- catch (\PDOException $e)
- {
- throw new exception("fetch error : " . $this->pdo_statement->queryString . " [error] :" . $e->getMessage(), error::ERROR_SERVER_DB_FAIL);
- }
- return $result;
- }
- public function fetchAll()
- {
- try
- {
- /** @noinspection PhpUnusedLocalVariableInspection */
- $holder = new guarded_profiler($this->profiler, __METHOD__);
- $callable = array($this->pdo_statement, __FUNCTION__);
- $args = func_get_args();
- $result = call_user_func_array($callable, $args);
- $holder->set_size(count($result));
- }
- catch (\PDOException $e)
- {
- throw new exception("fetchAll error : " . $this->pdo_statement->queryString . " [error] :" . $e->getMessage(), error::ERROR_SERVER_DB_FAIL);
- }
- return $result;
- }
- public function bindParam($parameter, &$variable, $data_type, $length, $driver_options)
- {
- try
- {
- /** @noinspection PhpUnusedLocalVariableInspection */
- $holder = new guarded_profiler($this->profiler, __METHOD__);
- $this->bind_params[$parameter] = $variable;
- $callable = array($this->pdo_statement, __FUNCTION__);
- $args = func_get_args();
- $result = call_user_func_array($callable, $args);
- }
- catch (\PDOException $e)
- {
- throw new exception("bindParam error : " . $this->pdo_statement->queryString . " [error] :" . $e->getMessage(), error::ERROR_SERVER_DB_FAIL);
- }
- return $result;
- }
- public function bindValue($parameter, $value, $data_type = \PDO::PARAM_STR)
- {
- try
- {
- /** @noinspection PhpUnusedLocalVariableInspection */
- $holder = new guarded_profiler($this->profiler, __METHOD__);
- $this->bind_params[$parameter] = $value;
- $result = $this->pdo_statement->bindValue($parameter, $value, $data_type);
- }
- catch (\PDOException $e)
- {
- throw new exception("bindValue error : " . $this->pdo_statement->queryString . " [error] :" . $e->getMessage(), error::ERROR_SERVER_DB_FAIL);
- }
- return $result;
- }
- public function execute(array $input_parameters = null)
- {
- try
- {
- /** @noinspection PhpUnusedLocalVariableInspection */
- $holder = new guarded_profiler($this->profiler, __METHOD__);
- // $query = preg_replace('/\r\n|\r|\n/','',$this->pdo_statement->queryString);
- // $query = preg_replace('/ ( *)/', ' ', $query);
- // $query = trim($query);
- //
- // $_SESSION['query'][] = self::interpolateQuery($query, $this->bind_params);
- $result = $this->pdo_statement->execute($input_parameters);
- }
- catch (\PDOException $e)
- {
- throw new exception("execute error : " . $this->pdo_statement->queryString . " [error] :" . $e->getMessage(), error::ERROR_SERVER_DB_FAIL);
- }
- return $result;
- }
- public function rowCount()
- {
- /** @noinspection PhpUnusedLocalVariableInspection */
- $holder = new guarded_profiler($this->profiler, __METHOD__);
- $result = $this->pdo_statement->rowCount();
- return $result;
- }
- /**
- * @param $query
- * @param null $input_params
- * @return mixed
- */
- public function interpolateQuery($query, $input_params = null)
- {
- $interpolate_query = $query;
- if ($this->bind_params) {
- ksort($this->bind_params);
- foreach ($this->bind_params as $key => $array) {
- if (is_numeric($key)) {
- $key = '\?';
- } else {
- $key = (preg_match('/^\:/', $key)) ? $key : ":" . $key;
- }
- $value = $array;
- $match_param = "/" . $key . '(?!\w)/';
- $change_value = $this->_prepareValue($value);
- $interpolate_query = preg_replace($match_param, $change_value, $interpolate_query, 1);
- }
- }
- if (is_array($input_params) && $input_params !== array()) {
- ksort($input_params);
- foreach ($input_params as $key => $change_value) {
- if (is_numeric($key)) {
- $key = '\?';
- } else {
- $key = (preg_match('/^\:/', $key)) ? $key : ":" . $key;
- }
- $match_param = "/" . $key . '(?!\w)/';
- $change_value = $this->_prepareValue($change_value);
- $interpolate_query = preg_replace($match_param, $change_value, $interpolate_query, 1);
- }
- }
- return $interpolate_query;
- }
- /**
- * @param $value
- * @return string
- */
- private function _prepareValue($value)
- {
- if (!is_numeric($value))
- $value = "'" . addslashes($value) . "'";
- return $value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement