Advertisement
Guest User

Untitled

a guest
May 31st, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. <?php
  2.  
  3. namespace gspf\base;
  4.  
  5. use gspf\message\error;
  6.  
  7. class pdo_statement
  8. {
  9. /**
  10. * @var profiler
  11. */
  12. private $profiler;
  13.  
  14. /**
  15. * @var \PDOStatement
  16. */
  17. private $pdo_statement;
  18.  
  19. private $bind_params = array();
  20.  
  21. /**
  22. * @param $pdo_statement \PDOStatement
  23. * @param $profiler
  24. */
  25. public function __construct($pdo_statement, $profiler)
  26. {
  27. $this->profiler = $profiler;
  28. $this->pdo_statement = $pdo_statement;
  29. }
  30.  
  31. public function fetch($fetch_style = null, $cursor_orientation = \PDO::FETCH_ORI_NEXT, $cursor_offset = 0)
  32. {
  33. try
  34. {
  35. /** @noinspection PhpUnusedLocalVariableInspection */
  36. $holder = new guarded_profiler($this->profiler, __METHOD__);
  37.  
  38. $result = $this->pdo_statement->fetch($fetch_style, $cursor_orientation, $cursor_offset);
  39.  
  40. $holder->set_size(1);
  41. }
  42. catch (\PDOException $e)
  43. {
  44. throw new exception("fetch error : " . $this->pdo_statement->queryString . " [error] :" . $e->getMessage(), error::ERROR_SERVER_DB_FAIL);
  45. }
  46.  
  47. return $result;
  48. }
  49.  
  50. public function fetchAll()
  51. {
  52. try
  53. {
  54. /** @noinspection PhpUnusedLocalVariableInspection */
  55. $holder = new guarded_profiler($this->profiler, __METHOD__);
  56.  
  57. $callable = array($this->pdo_statement, __FUNCTION__);
  58. $args = func_get_args();
  59. $result = call_user_func_array($callable, $args);
  60.  
  61. $holder->set_size(count($result));
  62. }
  63. catch (\PDOException $e)
  64. {
  65. throw new exception("fetchAll error : " . $this->pdo_statement->queryString . " [error] :" . $e->getMessage(), error::ERROR_SERVER_DB_FAIL);
  66. }
  67.  
  68. return $result;
  69. }
  70.  
  71. public function bindParam($parameter, &$variable, $data_type, $length, $driver_options)
  72. {
  73. try
  74. {
  75. /** @noinspection PhpUnusedLocalVariableInspection */
  76. $holder = new guarded_profiler($this->profiler, __METHOD__);
  77.  
  78. $this->bind_params[$parameter] = $variable;
  79.  
  80. $callable = array($this->pdo_statement, __FUNCTION__);
  81. $args = func_get_args();
  82. $result = call_user_func_array($callable, $args);
  83. }
  84. catch (\PDOException $e)
  85. {
  86. throw new exception("bindParam error : " . $this->pdo_statement->queryString . " [error] :" . $e->getMessage(), error::ERROR_SERVER_DB_FAIL);
  87. }
  88.  
  89. return $result;
  90. }
  91.  
  92. public function bindValue($parameter, $value, $data_type = \PDO::PARAM_STR)
  93. {
  94. try
  95. {
  96. /** @noinspection PhpUnusedLocalVariableInspection */
  97. $holder = new guarded_profiler($this->profiler, __METHOD__);
  98.  
  99. $this->bind_params[$parameter] = $value;
  100.  
  101. $result = $this->pdo_statement->bindValue($parameter, $value, $data_type);
  102. }
  103. catch (\PDOException $e)
  104. {
  105. throw new exception("bindValue error : " . $this->pdo_statement->queryString . " [error] :" . $e->getMessage(), error::ERROR_SERVER_DB_FAIL);
  106. }
  107.  
  108. return $result;
  109. }
  110.  
  111. public function execute(array $input_parameters = null)
  112. {
  113. try
  114. {
  115. /** @noinspection PhpUnusedLocalVariableInspection */
  116. $holder = new guarded_profiler($this->profiler, __METHOD__);
  117.  
  118. // $query = preg_replace('/\r\n|\r|\n/','',$this->pdo_statement->queryString);
  119. // $query = preg_replace('/ ( *)/', ' ', $query);
  120. // $query = trim($query);
  121. //
  122. // $_SESSION['query'][] = self::interpolateQuery($query, $this->bind_params);
  123.  
  124. $result = $this->pdo_statement->execute($input_parameters);
  125. }
  126. catch (\PDOException $e)
  127. {
  128. throw new exception("execute error : " . $this->pdo_statement->queryString . " [error] :" . $e->getMessage(), error::ERROR_SERVER_DB_FAIL);
  129. }
  130.  
  131. return $result;
  132. }
  133.  
  134. public function rowCount()
  135. {
  136. /** @noinspection PhpUnusedLocalVariableInspection */
  137. $holder = new guarded_profiler($this->profiler, __METHOD__);
  138.  
  139. $result = $this->pdo_statement->rowCount();
  140. return $result;
  141. }
  142.  
  143. /**
  144. * @param $query
  145. * @param null $input_params
  146. * @return mixed
  147. */
  148. public function interpolateQuery($query, $input_params = null)
  149. {
  150. $interpolate_query = $query;
  151.  
  152. if ($this->bind_params) {
  153. ksort($this->bind_params);
  154. foreach ($this->bind_params as $key => $array) {
  155. if (is_numeric($key)) {
  156. $key = '\?';
  157. } else {
  158. $key = (preg_match('/^\:/', $key)) ? $key : ":" . $key;
  159. }
  160. $value = $array;
  161. $match_param = "/" . $key . '(?!\w)/';
  162. $change_value = $this->_prepareValue($value);
  163. $interpolate_query = preg_replace($match_param, $change_value, $interpolate_query, 1);
  164. }
  165. }
  166.  
  167. if (is_array($input_params) && $input_params !== array()) {
  168. ksort($input_params);
  169. foreach ($input_params as $key => $change_value) {
  170. if (is_numeric($key)) {
  171. $key = '\?';
  172. } else {
  173. $key = (preg_match('/^\:/', $key)) ? $key : ":" . $key;
  174. }
  175. $match_param = "/" . $key . '(?!\w)/';
  176. $change_value = $this->_prepareValue($change_value);
  177. $interpolate_query = preg_replace($match_param, $change_value, $interpolate_query, 1);
  178. }
  179. }
  180.  
  181. return $interpolate_query;
  182. }
  183.  
  184. /**
  185. * @param $value
  186. * @return string
  187. */
  188. private function _prepareValue($value)
  189. {
  190. if (!is_numeric($value))
  191. $value = "'" . addslashes($value) . "'";
  192.  
  193. return $value;
  194. }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement