1<?php
2
3/*
4	[UCenter] (C)2001-2009 Comsenz Inc.
5	This is NOT a freeware, use is subject to license terms
6
7	$Id: db.class.php 922 2009-02-19 01:30:22Z zhaoxiongfei $
8*/
9
10
11class ucclient_db {
12	var $querynum = 0;
13	var $link;
14	var $histories;
15
16	var $dbhost;
17	var $dbuser;
18	var $dbpw;
19	var $dbcharset;
20	var $pconnect;
21	var $tablepre;
22	var $time;
23
24	var $goneaway = 5;
25
26	function connect($dbhost, $dbuser, $dbpw, $dbname = '', $dbcharset = '', $pconnect = 0, $tablepre='', $time = 0) {
27		$this->dbhost = $dbhost;
28		$this->dbuser = $dbuser;
29		$this->dbpw = $dbpw;
30		$this->dbname = $dbname;
31		$this->dbcharset = $dbcharset;
32		$this->pconnect = $pconnect;
33		$this->tablepre = $tablepre;
34		$this->time = $time;
35
36		if(!$this->link = new mysqli($dbhost, $dbuser, $dbpw, $dbname)) {
37			$this->halt('Can not connect to MySQL server');
38		}
39
40		if($this->version() > '4.1') {
41			if($dbcharset) {
42				$this->link->set_charset($dbcharset);
43			}
44
45			if($this->version() > '5.0.1') {
46				$this->link->query("SET sql_mode=''");
47			}
48		}
49	}
50
51	function fetch_array($query, $result_type = MYSQLI_ASSOC) {
52		return $query ? $query->fetch_array($result_type) : null;
53	}
54
55	function result_first($sql) {
56		$query = $this->query($sql);
57		return $this->result($query, 0);
58	}
59
60	function fetch_first($sql) {
61		$query = $this->query($sql);
62		return $this->fetch_array($query);
63	}
64
65	function fetch_all($sql, $id = '') {
66		$arr = array();
67		$query = $this->query($sql);
68		while($data = $this->fetch_array($query)) {
69			$id ? $arr[$data[$id]] = $data : $arr[] = $data;
70		}
71		return $arr;
72	}
73
74	function cache_gc() {
75		$this->query("DELETE FROM {$this->tablepre}sqlcaches WHERE expiry<$this->time");
76	}
77
78	function query($sql, $type = '', $cachetime = FALSE) {
79		$resultmode = $type == 'UNBUFFERED' ? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT;
80		if(!($query = $this->link->query($sql, $resultmode)) && $type != 'SILENT') {
81			$this->halt('MySQL Query Error', $sql);
82		}
83		$this->querynum++;
84		$this->histories[] = $sql;
85		return $query;
86	}
87
88	function affected_rows() {
89		return $this->link->affected_rows;
90	}
91
92	function error() {
93		return (($this->link) ? $this->link->error : mysqli_error());
94	}
95
96	function errno() {
97		return intval(($this->link) ? $this->link->errno : mysqli_errno());
98	}
99
100	function result($query, $row) {
101		if(!$query || $query->num_rows == 0) {
102			return null;
103		}
104		$query->data_seek($row);
105		$assocs = $query->fetch_row();
106		return $assocs[0];
107	}
108
109	function num_rows($query) {
110		$query = $query ? $query->num_rows : 0;
111		return $query;
112	}
113
114	function num_fields($query) {
115		return $query ? $query->field_count : 0;
116	}
117
118	function free_result($query) {
119		return $query ? $query->free() : false;
120	}
121
122	function insert_id() {
123		return ($id = $this->link->insert_id) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
124	}
125
126	function fetch_row($query) {
127		$query = $query ? $query->fetch_row() : null;
128		return $query;
129	}
130
131	function fetch_fields($query) {
132		return $query ? $query->fetch_field() : null;
133	}
134
135	function version() {
136		return $this->link->server_info;
137	}
138
139	function escape_string($str) {
140		return $this->link->escape_string($str);
141	}
142
143	function close() {
144		return $this->link->close();
145	}
146
147	function halt($message = '', $sql = '') {
148		$error = $this->error();
149		$errorno = $this->errno();
150		if($errorno == 2006 && $this->goneaway-- > 0) {
151			$this->connect($this->dbhost, $this->dbuser, $this->dbpw, $this->dbname, $this->dbcharset, $this->pconnect, $this->tablepre, $this->time);
152			$this->query($sql);
153		} else {
154			$s = '';
155			if($message) {
156				$s = "<b>UCenter info:</b> $message<br />";
157			}
158			if($sql) {
159				$s .= '<b>SQL:</b>'.htmlspecialchars($sql).'<br />';
160			}
161			$s .= '<b>Error:</b>'.$error.'<br />';
162			$s .= '<b>Errno:</b>'.$errorno.'<br />';
163			$s = str_replace(UC_DBTABLEPRE, '[Table]', $s);
164			exit($s);
165		}
166	}
167}
168
169?>