PageRenderTime 121ms CodeModel.GetById 60ms app.highlight 10ms RepoModel.GetById 31ms app.codeStats 0ms

/code/classes/DAO/MySQL.class.php

https://github.com/blekkzor/pinetd2
PHP | 138 lines | 125 code | 13 blank | 0 comment | 29 complexity | accf7256b99543f7198e40a9c8dc152f MD5 | raw file
  1<?php
  2
  3namespace DAO;
  4
  5class MySQL extends \DAO\Base {
  6	public function __construct($SQL, $table, $key) {
  7		$this->SQL = $SQL;
  8		parent::__construct($table, $key);
  9	}
 10
 11	public function buildWhere($key, $key_val) {
 12		if (!is_array($key)) {
 13			return $this->buildWhere(array($key), array($key_val));
 14		}
 15		$res = '';
 16		foreach($key as $idx => $k) {
 17			$res.=($res == ''?'':' AND ').'`'.$k.'` = \''.$this->SQL->escape_string($key_val[$idx]).'\'';
 18		}
 19		return $res;
 20	}
 21
 22	public function deleteBean($bean) {
 23		$key = $this->key;
 24		$query = 'DELETE FROM '.$this->formatTable($this->table).' WHERE '.$this->buildWhere($key, $bean->__get($key));
 25		return $this->SQL->query($query);
 26	}
 27
 28	public function delete(array $where) {
 29		$query = 'DELETE FROM '.$this->formatTable($this->table).' WHERE '.$this->buildQuickWhere($where);
 30		return $this->SQL->query($query);
 31	}
 32
 33	public function formatTable($table) {
 34		if (is_array($table)) {
 35			return '`'.implode('`.`',$table).'`';
 36		}
 37		return '`'.$table.'`';
 38	}
 39
 40	public function createUpdateQuery($data, $table, $qwhere) {
 41		$query = '';
 42		foreach($data as $var=>$val) {
 43			$query .= ($query == ''?'':', ').'`'.$var.'` = '.(is_null($val)?'NULL':'\''.$this->SQL->escape_string($val).'\'');
 44		}
 45		$query = 'UPDATE '.$this->formatTable($table).' SET '.$query.' ';
 46		if (!is_null($qwhere)) {
 47			if (is_array($qwhere)) {
 48				$query.='WHERE ';
 49				$first = true;
 50				foreach($qwhere as $var=>$val) {
 51					if (is_int($var)) {
 52						$query.=($first?'':' AND ').$this->buildWhere($val[0], $val[1]);
 53					} else {
 54						$query.=($first?'':' AND ').'`'.$var.'` = \''.$this->SQL->escape_string($val).'\'';
 55					}
 56					$first = false;
 57				}
 58			} else {
 59				$query.='WHERE '.$qwhere;
 60			}
 61		}
 62		return $this->SQL->query($query);
 63	}
 64
 65	public function insertValues($data) {
 66		$query = '';
 67		foreach($data as $var=>$val) {
 68			$query .= ($query == ''?'':', ').'`'.$var.'` = '.(is_null($val)?'NULL':'\''.$this->SQL->escape_string($val).'\'');
 69		}
 70		$query = 'INSERT INTO '.$this->formatTable($this->table).' SET '.$query;
 71		return $this->SQL->query($query);
 72	}
 73
 74	protected function buildQuickWhere(array $qwhere) {
 75		$first = true;
 76		$query = '';
 77		foreach($qwhere as $var=>$val) {
 78			if ((is_int($var)) && (is_object($val))) {
 79				if (!($val instanceof \pinetd\SQL\Expr))
 80					throw new Exception('Expression of wrong type');
 81				$query.=($first?'':' AND ').$val;
 82			} else if (is_int($var)) {
 83				$query.=($first?'':' AND ').$this->buildWhere($val[0], $val[1]);
 84			} else {
 85				$query.=($first?'':' AND ').'`'.$var.'` '.(is_null($val)?'IS NULL':'= \''.$this->SQL->escape_string($val).'\'');
 86			}
 87			$first = false;
 88		}
 89		return $query;
 90	}
 91
 92	public function createSelectQuery($qtype = 'SELECT', $qfields = '*', $qtable = null, $qwhere = null, $order_by = null, $limit = null) {
 93		$query = $qtype.' ';
 94		if (is_array($qfields)) {
 95			$first = true;
 96			foreach($qfields as $var) {
 97				$query.=($first?'':', ').'`'.$var.'`';
 98				$first = false;
 99			}
100			$query.=' ';
101		} else {
102			$query.=$qfields.' ';
103		}
104		if (!is_null($qtable)) $query.='FROM '.$this->formatTable($qtable).' ';
105		if (!is_null($qwhere)) {
106			if (is_array($qwhere)) {
107				$query.='WHERE ' . $this->buildQuickWhere($qwhere);
108			} else {
109				$query.='WHERE '.$qwhere;
110			}
111		}
112		if (!is_null($order_by)) {
113			if (is_array($order_by)) {
114				$order = '';
115				foreach($order_by as $field => $_order) {
116					if (is_int($field)) {
117						$field = $order;
118						$_order = 'ASC';
119					}
120					$order .= ($order==''?'':', ').'`'.$field.'` '.$_order;
121				}
122			} else {
123				$order = $order_by;
124			}
125			$query.=' ORDER BY '.$order;
126		}
127		if (!is_null($limit)) {
128			$query.=' LIMIT '.$limit[0];
129			if (isset($limit[1])) $query.=', '.$limit[1];
130		}
131		$res = $this->SQL->query($query);
132		if (!$res) throw new \Exception($this->SQL->error);
133		return $res;
134	}
135
136}
137
138