Guest User

Untitled

a guest
May 5th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Phorte;
  4.  
  5. use Phorte::Filter;
  6.  
  7. abstract class Db extends ::PDO
  8. {
  9. /**
  10. * Ansi SQL standard identifier quote
  11. * @var string
  12. */
  13. protected $identQuote = '"';
  14.  
  15. /**
  16. * Ansi SQL standard value quote
  17. * @var string
  18. */
  19. protected $quote = '\'';
  20.  
  21. /**
  22. * Creates a new Db
  23. *
  24. * Generic $opts accepted:
  25. * dsn => required
  26. * user => optional
  27. * pass => optional
  28. *
  29. * @param array $opts
  30. */
  31. public function __construct (array $opts)
  32. {
  33. if (isset($opts['dsn']) && strpos($opts['dsn'], 'sqlite')===0)
  34. {
  35. Filter::expect($opts, array('dsn'));
  36. Filter::want($opts, array('user'=>NULL, 'pass'=>NULL));
  37. }
  38. else
  39. {
  40. Filter::expect($opts, array('dsn', 'user', 'pass'));
  41. }
  42. Filter::want($opts, array('errmode'=>PDO::ERRMODE_EXCEPTION, 'fetchmode'=>PDO::FETCH_OBJ));
  43.  
  44. parent::__construct($opts['dsn'], $opts['user'], $opts['pass']);
  45.  
  46. if (isset($opts['errmode']))
  47. {
  48. $errmode = is_int($opts['errmode']) ? $opts['errmode'] : constant($opts['errmode']);
  49. $fetchmode = is_int($opts['fetchmode']) ? $opts['fetchmode'] : constant($opts['fetchmode']);
  50. $this->setAttribute(PDO::ATTR_ERRMODE, $errmode);
  51. $this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, $fetchmode);
  52. }
  53. }
  54.  
  55. /**
  56. * Delete values from a table
  57. *
  58. * @param string $table
  59. * @param array $values
  60. * @param string $where
  61. * @param array $quoteInto
  62. * @param boolean $retStatement
  63. * @return PDOStatement|string
  64. */
  65. public function delete ($table, array $values, $where, array $quoteInto=NULL, $retStatement=false)
  66. {
  67. $q = $this->identQuote;
  68. $query = "DELETE FROM $q$table$q";
  69.  
  70. if ($where && $quoteInto)
  71. {
  72. $query .= " WHERE ".$where;
  73. }
  74. elseif ($where)
  75. {
  76. $query .= " WHERE ".$this->quoteInto($where, $quoteInto);
  77. }
  78.  
  79. if ($retStatement)
  80. {
  81. return $query;
  82. }
  83. else
  84. {
  85. return $this->exec($query);
  86. }
  87. }
  88.  
  89. /**
  90. * Insert values into a table
  91. *
  92. * @param string $table
  93. * @param array $values
  94. * @param string $where
  95. * @param array $quoteInto
  96. * @param boolean $retStatement
  97. * @return PDOStatement|string
  98. */
  99. public function insert ($table, array $values, $where='', array $quoteInto=NULL, $retStatement=false)
  100. {
  101. $q = $this->identQuote;
  102. $query = "INSERT INTO $q$table$q($q".implode($q.','.$q, array_keys($values))
  103. . "$q) VALUES".$this->quote($values);
  104.  
  105. if ($where && $quoteInto)
  106. {
  107. $query .= ' WHERE '.$this->quoteInto($where, $quoteInto);
  108. }
  109. elseif ($where)
  110. {
  111. $query .= ' WHERE '.$where;
  112. }
  113.  
  114. $query .= ';';
  115.  
  116. if ($retStatement)
  117. {
  118. return $query;
  119. }
  120. elseif ($this->exec($query))
  121. {
  122. return $this->lastInsertId();
  123. }
  124. else
  125. {
  126. return false;
  127. }
  128. }
  129.  
  130. /**
  131. * Quotes native datatypes, or quotes as the type given
  132. *
  133. * @param mixed $data
  134. * @param integer|string $type
  135. * @return string|integer|float
  136. */
  137. public function quote ($data, $type=NULL)
  138. {
  139. if (isset($type))
  140. {
  141. return parent::quote($data, $type);
  142. }
  143. else
  144. {
  145. switch (gettype($data))
  146. {
  147. case 'array':
  148. return '('.implode(', ', array_map(array($this, __FUNCTION__), $data)).')';
  149. case 'object':
  150. if (method_exists($data, '__tostring'))
  151. {
  152. return parent::quote((string) $data, PDO::PARAM_STR);
  153. }
  154. else
  155. {
  156. return $this->quote((array) $data);
  157. }
  158. case 'boolean':
  159. return parent::quote($data, PDO::PARAM_BOOL);
  160. case 'int':
  161. case 'double':
  162. return parent::quote($data, PDO::PARAM_INT);
  163. case 'string':
  164. return parent::quote($data);
  165. case 'NULL':
  166. return 'NULL';
  167. default:
  168. throw new Exception('Illegal parameter type '.gettype($data));
  169. }
  170. }
  171. }
  172.  
  173. /**
  174. * Format a query, using native escaping mechanisms on the data
  175. *
  176. * @param string $stmt The query to format
  177. * @param array $values A field=>value pair of data to quote into the statement
  178. * @return PDOStatement
  179. */
  180. public function fquery ($stmt, array $values=array())
  181. {
  182. return $this->query($this->quoteInto($stmt, $values));
  183. }
  184.  
  185. /**
  186. * Format a string for use in a query
  187. * @param string $stmt The query to format
  188. * @param array $values A field=>value pair of data to quote into the statement
  189. * @return PDOStatement
  190. */
  191. public function quoteInto ($stmt, array $values=array())
  192. {
  193. return preg_replace_callback('/\{([!<>]?[=]?)([^}]+)\}/', function ($match) use ($values)
  194. {
  195. if (!isset($values[$match[2]]))
  196. {
  197. throw new Exception('Field '.$match[2].' not found in supplied data.');
  198. }
  199. if (empty($match[1]))
  200. {
  201. return $this->quote($values[$match[2]]);
  202. }
  203. elseif ($match[1]=='!' || $match[1]=='!=')
  204. {
  205. $q = $this->identQuote;
  206. return $q.$match[2].$q.'<>'.$this->quote($values[$match[2]]);
  207. }
  208. else
  209. {
  210. $q = $this->identQuote;
  211. return $q.$match[2].$q.$match[1].$this->quote($values[$match[2]]);
  212. }
  213. }, $stmt);
  214. }
  215.  
  216. /**
  217. * Update rows in a table
  218. *
  219. * @param string $table
  220. * @param array $values
  221. * @param string $where
  222. * @param array $quoteInto
  223. * @param boolean $retStatement
  224. * @return PDOStatement|string
  225. */
  226. public function update ($table, array $values, $where, array $quoteInto=NULL, $retStatement=false)
  227. {
  228. $q = $this->identQuote;
  229. if (empty($values))
  230. {
  231. throw Exception('At least 1 field is required.');
  232. }
  233. $query = "UPDATE $q$table$q SET";
  234. foreach ($values as $field=>$value)
  235. {
  236. $query .= ' '.$q.$field.$q.'='.$this->quote($value);
  237. }
  238.  
  239. if ($where && $quoteInto)
  240. {
  241. $query .= ' WHERE '.$this->quoteInto($where, $quoteInto);
  242. }
  243. elseif ($where)
  244. {
  245. $query .= ' WHERE '.$where;
  246. }
  247.  
  248. if ($retStatement)
  249. {
  250. return $query.';';
  251. }
  252. else
  253. {
  254. return $this->exec($query);
  255. }
  256. }
  257.  
  258. }
  259.  
  260. ?>
Add Comment
Please, Sign In to add comment