Guest User

Untitled

a guest
Jul 21st, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. <?php
  2. require_once('functions.php');
  3.  
  4. function quote($value) {
  5. switch (gettype($value)) {
  6. case 'string':
  7. $ret = str_replace("\\", "\\\\", $value);
  8. $ret = str_replace("'", "\'", $ret);
  9. return "'" . $ret . "'";
  10. case 'integer':
  11. return $value;
  12. case 'NULL':
  13. return $value;
  14. //return 0;
  15. }
  16. }
  17.  
  18. class CSQL2 {
  19. private $_db;
  20. #public static $db;
  21. private $_table;
  22. private $_where;
  23. private $_options;
  24.  
  25. private $_is_dirty = false;
  26. private $_exists = false;
  27. private $_data = array();
  28.  
  29. private $_changed_keys = array();
  30.  
  31. function __construct($db, $table, $where = null, $options = null) {
  32. $this->_db = $db;
  33. $this->_table = $table;
  34. $this->_where = $where;
  35. $this->_options = $options;
  36.  
  37. if (is_numeric($this->_where) && isset($this->_options['sequence'])) {
  38. $this->_where = array($this->_options['sequence'] => $this->_where);
  39. }
  40.  
  41. if ($this->_where != null) {
  42. $this->_data = array_merge($this->_data, $this->_where);
  43. }
  44.  
  45. if (isset($this->_options['autoload'])
  46. && $this->_options['autoload'] && $this->_where != null) {
  47. $this->load();
  48. }
  49. }
  50.  
  51. function __destruct() {
  52. if (isset($this->_options['autosave'])
  53. && $this->_options['autosave'] && $this->_is_dirty) {
  54. $this->save();
  55. }
  56. }
  57.  
  58. function load() {
  59. $query = 'SELECT * FROM ' . $this->_table .
  60. ' WHERE ' . $this->build_where();
  61. #$data = $this->_db->getRow($query);
  62. $data = query_first($query);
  63. if ($this->_db->error != 0 || !$data) {
  64. $this->_is_dirty = true;
  65. return $data;
  66. }
  67.  
  68. if ($data) {
  69. $this->_data = $data;
  70. $this->_exists = true;
  71. $this->_is_dirty = false;
  72. }
  73.  
  74. return true;
  75. }
  76.  
  77. function load_multi() {
  78. $query = 'SELECT * FROM ' . $this->_table .
  79. ' WHERE ' . $this->build_where();
  80. $data = query_all($query);
  81. echo '<pre>';
  82. var_dump($query, $data, $this->_where);
  83. if ($this->_db->error != 0 || !$data) {
  84. $this->_is_dirty = true;
  85. return $data;
  86. }
  87.  
  88. if ($data) {
  89. $this->_data = $data;
  90. $this->_exists = true;
  91. $this->_is_dirty = false;
  92. }
  93.  
  94. return true;
  95. }
  96.  
  97. function del() {
  98. $query = 'DELETE FROM ' . $this->_table .
  99. ' WHERE ' . $this->build_where();
  100. $this->_db->query($query);
  101. }
  102.  
  103. function save() {
  104. if (!$this->_is_dirty) {
  105. return true;
  106. }
  107.  
  108. $query = array();
  109. if (!$this->_exists) {
  110. if (isset($this->_options['sequence'])) {
  111. $id = $this->_options['sequence'];
  112. $this->$id = null;
  113. }
  114.  
  115. $keys = array_keys($this->_data);
  116. $values = array_map(quote,
  117. array_values($this->_data), array('text'));
  118.  
  119. $query[] = 'INSERT INTO ' . $this->_table;
  120. $query[] = '(' . implode(',', $keys) . ')';
  121. $query[] = 'VALUES (' . implode(',', $values) . ')';
  122. } else {
  123. $query[] = 'UPDATE ' . $this->_table . ' SET';
  124. $sets = array();
  125. foreach ($this->_changed_keys as $attr => $unneeded) {
  126. $value = $this->_data[$attr];
  127. if (!isset($value)) {
  128. continue;
  129. }
  130. $sets[] = $attr . '=' . quote($value);
  131. }
  132. $query[] = implode(',', $sets);
  133. $query[] = 'WHERE ' . $this->build_where();
  134. }
  135.  
  136. $ret = false;
  137. $query = implode(' ', $query);
  138. if ($this->_db->query($query) && $this->_db->errno == 0) {
  139. $ret = true;
  140. if (!$this->_exists && isset($this->_options['sequence'])) {
  141. $id = $this->_options['sequence'];
  142. $this->$id = $this->_db->insert_id;
  143. $this->_options['sequence'];
  144. }
  145. $this->_exists = true;
  146. $this->_is_dirty = false;
  147. } else {
  148. _query_error($this->_db);
  149. }
  150.  
  151. return $ret;
  152. }
  153.  
  154. private function build_where() {
  155. return self::get_where_cond($this->_db, $this->_where);
  156. }
  157.  
  158. public static function get_where_cond($db, $arr) {
  159. $cond = array();
  160. var_dump($arr);
  161. foreach ($arr as $attr => $value) {
  162. $cond[] = $attr . '="' . quote($value) . '"';
  163. }
  164.  
  165. return implode(' AND ', $cond);
  166. }
  167.  
  168. function __get($attr) {
  169. if (isset($this->_data[$attr])) {
  170. return $this->_data[$attr];
  171. }
  172.  
  173. return '';
  174. }
  175.  
  176. function __set($attr, $value) {
  177. if (isset($this->_data[$attr]) && $this->_data[$attr] === $value) {
  178. return;
  179. }
  180. $this->_is_dirty = true;
  181. $this->_data[$attr] = $value;
  182. $this->_changed_keys[$attr] = 1;
  183. }
  184.  
  185. function multi_set($arr, $keys = null) {
  186. foreach ($arr as $key => $value) {
  187. if ($keys !== null && !isset($keys[$key])) {
  188. continue;
  189. }
  190. $this->$key = $value;
  191. }
  192. }
  193.  
  194. function get_data() {
  195. return $this->_data;
  196. }
  197.  
  198. function exists() {
  199. return $this->_exists;
  200. }
  201.  
  202. function __toString() {
  203. return var_export($this->_data, true);
  204. }
  205. }
Add Comment
Please, Sign In to add comment