Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once('functions.php');
- function quote($value) {
- switch (gettype($value)) {
- case 'string':
- $ret = str_replace("\\", "\\\\", $value);
- $ret = str_replace("'", "\'", $ret);
- return "'" . $ret . "'";
- case 'integer':
- return $value;
- case 'NULL':
- return $value;
- //return 0;
- }
- }
- class CSQL2 {
- private $_db;
- #public static $db;
- private $_table;
- private $_where;
- private $_options;
- private $_is_dirty = false;
- private $_exists = false;
- private $_data = array();
- private $_changed_keys = array();
- function __construct($db, $table, $where = null, $options = null) {
- $this->_db = $db;
- $this->_table = $table;
- $this->_where = $where;
- $this->_options = $options;
- if (is_numeric($this->_where) && isset($this->_options['sequence'])) {
- $this->_where = array($this->_options['sequence'] => $this->_where);
- }
- if ($this->_where != null) {
- $this->_data = array_merge($this->_data, $this->_where);
- }
- if (isset($this->_options['autoload'])
- && $this->_options['autoload'] && $this->_where != null) {
- $this->load();
- }
- }
- function __destruct() {
- if (isset($this->_options['autosave'])
- && $this->_options['autosave'] && $this->_is_dirty) {
- $this->save();
- }
- }
- function load() {
- $query = 'SELECT * FROM ' . $this->_table .
- ' WHERE ' . $this->build_where();
- #$data = $this->_db->getRow($query);
- $data = query_first($query);
- if ($this->_db->error != 0 || !$data) {
- $this->_is_dirty = true;
- return $data;
- }
- if ($data) {
- $this->_data = $data;
- $this->_exists = true;
- $this->_is_dirty = false;
- }
- return true;
- }
- function load_multi() {
- $query = 'SELECT * FROM ' . $this->_table .
- ' WHERE ' . $this->build_where();
- $data = query_all($query);
- echo '<pre>';
- var_dump($query, $data, $this->_where);
- if ($this->_db->error != 0 || !$data) {
- $this->_is_dirty = true;
- return $data;
- }
- if ($data) {
- $this->_data = $data;
- $this->_exists = true;
- $this->_is_dirty = false;
- }
- return true;
- }
- function del() {
- $query = 'DELETE FROM ' . $this->_table .
- ' WHERE ' . $this->build_where();
- $this->_db->query($query);
- }
- function save() {
- if (!$this->_is_dirty) {
- return true;
- }
- $query = array();
- if (!$this->_exists) {
- if (isset($this->_options['sequence'])) {
- $id = $this->_options['sequence'];
- $this->$id = null;
- }
- $keys = array_keys($this->_data);
- $values = array_map(quote,
- array_values($this->_data), array('text'));
- $query[] = 'INSERT INTO ' . $this->_table;
- $query[] = '(' . implode(',', $keys) . ')';
- $query[] = 'VALUES (' . implode(',', $values) . ')';
- } else {
- $query[] = 'UPDATE ' . $this->_table . ' SET';
- $sets = array();
- foreach ($this->_changed_keys as $attr => $unneeded) {
- $value = $this->_data[$attr];
- if (!isset($value)) {
- continue;
- }
- $sets[] = $attr . '=' . quote($value);
- }
- $query[] = implode(',', $sets);
- $query[] = 'WHERE ' . $this->build_where();
- }
- $ret = false;
- $query = implode(' ', $query);
- if ($this->_db->query($query) && $this->_db->errno == 0) {
- $ret = true;
- if (!$this->_exists && isset($this->_options['sequence'])) {
- $id = $this->_options['sequence'];
- $this->$id = $this->_db->insert_id;
- $this->_options['sequence'];
- }
- $this->_exists = true;
- $this->_is_dirty = false;
- } else {
- _query_error($this->_db);
- }
- return $ret;
- }
- private function build_where() {
- return self::get_where_cond($this->_db, $this->_where);
- }
- public static function get_where_cond($db, $arr) {
- $cond = array();
- var_dump($arr);
- foreach ($arr as $attr => $value) {
- $cond[] = $attr . '="' . quote($value) . '"';
- }
- return implode(' AND ', $cond);
- }
- function __get($attr) {
- if (isset($this->_data[$attr])) {
- return $this->_data[$attr];
- }
- return '';
- }
- function __set($attr, $value) {
- if (isset($this->_data[$attr]) && $this->_data[$attr] === $value) {
- return;
- }
- $this->_is_dirty = true;
- $this->_data[$attr] = $value;
- $this->_changed_keys[$attr] = 1;
- }
- function multi_set($arr, $keys = null) {
- foreach ($arr as $key => $value) {
- if ($keys !== null && !isset($keys[$key])) {
- continue;
- }
- $this->$key = $value;
- }
- }
- function get_data() {
- return $this->_data;
- }
- function exists() {
- return $this->_exists;
- }
- function __toString() {
- return var_export($this->_data, true);
- }
- }
Add Comment
Please, Sign In to add comment