pinetd2 /code/classes/DAO/SQLite3.class.php

Language PHP Lines 134
MD5 Hash 4be4ae177c233b945f23c59da11ad5a5
Repository https://github.com/blekkzor/pinetd2.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php

namespace DAO;

class SQLite3 extends \DAO\Base {
	public function __construct($SQL, $table, $key) {
		$this->SQL = $SQL;
		parent::__construct($table, $key);
	}

	public function buildWhere($key, $key_val) {
		if (!is_array($key)) {
			return $this->buildWhere(array($key), array($key_val));
		}
		$res = '';
		foreach($key as $idx => $k) {
			$res.=($res == ''?'':' AND ').'`'.$k.'` = \''.$this->SQL->escape_string($key_val[$idx]).'\'';
		}
		return $res;
	}

	public function deleteBean($bean) {
		$key = $this->key;
		$query = 'DELETE FROM '.$this->formatTable($this->table).' WHERE '.$this->buildWhere($key, $bean->__get($key));
		return $this->SQL->query($query);
	}

	public function delete(array $where) {
		$query = 'DELETE FROM '.$this->formatTable($this->table).' WHERE '.$this->buildQuickWhere($where);
		return $this->SQL->query($query);
	}

	public function formatTable($table) {
		if (is_array($table)) {
			return '`'.implode('`.`',$table).'`';
		}
		return '`'.$table.'`';
	}

	public function createUpdateQuery($data, $table, $qwhere) {
		$query = '';
		foreach($data as $var=>$val) {
			$query .= ($query == ''?'':', ').'`'.$var.'` = '.(is_null($val)?'NULL':'\''.$this->SQL->escape_string($val).'\'');
		}
		$query = 'UPDATE '.$this->formatTable($table).' SET '.$query.' ';
		if (!is_null($qwhere)) {
			if (is_array($qwhere)) {
				$query.='WHERE ';
				$first = true;
				foreach($qwhere as $var=>$val) {
					if (is_int($var)) {
						$query.=($first?'':' AND ').$this->buildWhere($val[0], $val[1]);
					} else {
						$query.=($first?'':' AND ').'`'.$var.'` = \''.$this->SQL->escape_string($val).'\'';
					}
					$first = false;
				}
			} else {
				$query.='WHERE '.$qwhere;
			}
		}
		return $this->SQL->query($query);
	}

	public function insertValues($data) {
		return $this->SQL->insert($this->table, $data);
	}

	protected function buildQuickWhere(array $qwhere) {
		$first = true;
		$query = '';
		foreach($qwhere as $var=>$val) {
			if ((is_int($var)) && (is_object($val))) {
				if (!($val instanceof \pinetd\SQL\Expr))
					throw new Exception('Expression of wrong type');
				$query.=($first?'':' AND ').$val;
			} else if (is_int($var)) {
				$query.=($first?'':' AND ').$this->buildWhere($val[0], $val[1]);
			} else {
				$query.=($first?'':' AND ').'`'.$var.'` '.(is_null($val)?'IS NULL':'= \''.$this->SQL->escape_string($val).'\'');
			}
			$first = false;
		}
		return $query;
	}

	public function createSelectQuery($qtype = 'SELECT', $qfields = '*', $qtable = null, $qwhere = null, $order_by = null, $limit = null) {
		$query = $qtype.' ';
		if (is_array($qfields)) {
			$first = true;
			foreach($qfields as $var) {
				$query.=($first?'':', ').'`'.$var.'`';
				$first = false;
			}
			$query.=' ';
		} else {
			$query.=$qfields.' ';
		}
		if (!is_null($qtable)) $query.='FROM '.$this->formatTable($qtable).' ';
		if (!is_null($qwhere)) {
			if (is_array($qwhere)) {
				$query.='WHERE ' . $this->buildQuickWhere($qwhere);
			} else {
				$query.='WHERE '.$qwhere;
			}
		}
		if (!is_null($order_by)) {
			if (is_array($order_by)) {
				$order = '';
				foreach($order_by as $field => $_order) {
					if (is_int($field)) {
						$field = $order;
						$_order = 'ASC';
					}
					$order .= ($order==''?'':', ').'`'.$field.'` '.$_order;
				}
			} else {
				$order = $order_by;
			}
			$query.=' ORDER BY '.$order;
		}
		if (!is_null($limit)) {
			$query.=' LIMIT '.$limit[0];
			if (isset($limit[1])) $query.=', '.$limit[1];
		}
		$res = $this->SQL->query($query);
		if (!$res) throw new \Exception($this->SQL->error);
		return $res;
	}

}
Back to Top