Guest User

Untitled

a guest
Mar 8th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. <?php
  2.  
  3. /* Copyright 2009 Mo McRoberts.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. The names of the author(s) of this software may not be used to endorse
  15. * or promote products derived from this software without specific prior
  16. * written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  19. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  20. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  21. * AUTHORS OF THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  23. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  25. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29.  
  30.  
  31. class DBCore
  32. {
  33. protected $rsClass;
  34.  
  35. public function vquery($query, $params)
  36. {
  37. if(!is_array($params)) $params = array();
  38. $sql = preg_replace('/\?/e', "\$this->quote(array_shift(\$params))", $query);
  39. return $this->execute($sql);
  40. }
  41.  
  42. public function exec($query)
  43. {
  44. $params = func_get_args();
  45. array_shift($params);
  46. if($this->vquery($query, $params))
  47. {
  48. return true;
  49. }
  50. return false;
  51. }
  52.  
  53. /* $rs = $inst->query('SELECT * FROM `sometable` WHERE `field` = ? AND `otherfield` = ?', $something, 27); */
  54. public function query($query)
  55. {
  56. $params = func_get_args();
  57. array_shift($params);
  58. if(($r = $this->vquery($query, $params)))
  59. {
  60. return new $this->rsClass($this, $r);
  61. }
  62. return null;
  63. }
  64.  
  65. public function getRow($query)
  66. {
  67. $row = null;
  68. $params = func_get_args();
  69. array_shift($params);
  70. if(($r = $this->vquery($query, $params)))
  71. {
  72. $rs = $this->rsClass($this, $r);
  73. $row = $rs->next();
  74. $rs = null;
  75. }
  76. return $row;
  77. }
  78.  
  79. protected function reportError($errmsg, $sqlString)
  80. {
  81. ini_set('display_errors', 'On');
  82. trigger_error($errmsg . ' while executing ' . $sqlString, E_USER_ERROR);
  83. }
  84.  
  85. public function insert($table, $kv)
  86. {
  87. $keys = array_keys($kv);
  88. $values = array_values($kv);
  89. array_walk($keys, array($this, 'quoteObjectRef'));
  90. array_walk($values, array($this, 'quoteRef'));
  91. $sql = 'INSERT INTO ' . $this->quoteObject($table) . '(' . implode(',', $keys) . ') VALUES (' . implode(',', $values) . ')';
  92. return $this->execute($sql);
  93. }
  94.  
  95. public function update($table, $kv, $clause)
  96. {
  97. $sql = 'UPDATE ' . $this->quoteObject($table) . ' SET ';
  98. foreach($kv as $key => $value)
  99. {
  100. $sql .= $this->quoteObject($key) . ' = ' . $this->quote($value) . ', ';
  101. }
  102. $sql = substr($sql, 0, -2);
  103. if(is_string($clause) && strlen($clause))
  104. {
  105. $sql .= ' WHERE ' . $clause;
  106. }
  107. else if(is_array($clause) && count($clause))
  108. {
  109. $sql .= ' WHERE ';
  110. foreach($clause as $key => $value)
  111. {
  112. $sql .= $this->quoteObject($key) . ' = ' . $this->quote($value) . ' AND ';
  113. }
  114. $sql = substr($sql, 0, -4);
  115. }
  116. return $this->execute($sql);
  117. }
  118.  
  119. public function quoteObject($name)
  120. {
  121. return '"' . $name . '"';
  122. }
  123.  
  124. public function quoteRef(&$value)
  125. {
  126. $value = $this->quote($value);
  127. }
  128.  
  129. public function quoteObjectRef(&$name)
  130. {
  131. $name = $this->quoteObject($name);
  132. }
  133.  
  134. }
  135.  
  136. /* while(($row = $rs->next())) { ... } */
  137. class DBDataSet
  138. {
  139. public $fields = array();
  140. public $EOF = true;
  141. public $db;
  142. protected $resource;
  143.  
  144. public function __construct($db, $resource)
  145. {
  146. $this->db = $db;
  147. $this->resource = $resource;
  148. $this->EOF = false;
  149. }
  150.  
  151. public function next()
  152. {
  153. if($this->EOF) return false;
  154. if(!$this->getRow())
  155. {
  156. $this->EOF = true;
  157. return null;
  158. }
  159. return $this->fields;
  160. }
  161. }
  162.  
  163. class MySQL extends DBCore
  164. {
  165. protected $rsClass = 'MySQLSet';
  166. protected $mysql;
  167.  
  168. public function __construct($name = null, $host = null, $user = null, $pass = null)
  169. {
  170. if(!($this->mysql = mysql_connect($host, $user, $pass)))
  171. {
  172. $this->raiseError(null);
  173. }
  174. if(!mysql_select_db($name, $this->mysql))
  175. {
  176. $this->raiseError(null);
  177. }
  178. }
  179.  
  180. public function quoteObject($name)
  181. {
  182. return '`' . $name . '`';
  183. }
  184.  
  185. protected function execute($sql)
  186. {
  187. $r = mysql_query($sql, $this->mysql);
  188. if($r === false)
  189. {
  190. $this->raiseError($sql);
  191. }
  192. return $r;
  193. }
  194.  
  195. protected function raiseError($query)
  196. {
  197. return $this->reportError(mysql_error($this->mysql), $query);
  198. }
  199.  
  200. public function quote($string)
  201. {
  202. if(is_null($string))
  203. {
  204. return 'NULL';
  205. }
  206. if(is_bool($string))
  207. {
  208. return ($string ? "'Y'" : '"N"');
  209. }
  210. return "'" . mysql_real_escape_string($string, $this->mysql) . "'";
  211. }
  212.  
  213. public function getRow($query)
  214. {
  215. $row = null;
  216. $params = func_get_args();
  217. array_shift($params);
  218. if(($r = $this->vquery($query . ' LIMIT 1', $params)))
  219. {
  220. $row = mysql_fetch_assoc($r);
  221. }
  222. return $row;
  223. }
  224.  
  225. public function insertId()
  226. {
  227. return mysql_insert_id($this->mysql);
  228. }
  229.  
  230. }
  231.  
  232. class MySQLSet extends DBDataSet
  233. {
  234. protected function getRow()
  235. {
  236. return ($this->fields = mysql_fetch_assoc($this->resource));
  237. }
  238. }
  239.  
  240. global $DB;
  241.  
  242. $DB = new MySQL(DB_NAME, DB_HOST, DB_USER, DB_PASSWORD);
Add Comment
Please, Sign In to add comment