-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDB_Query.php
More file actions
149 lines (128 loc) · 4.48 KB
/
DB_Query.php
File metadata and controls
149 lines (128 loc) · 4.48 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?
//SH management - API - MySQL DB Interface - Query
//A single DB query
class DB_Query {
//MySQLi link
private $link=false;
//DB object
private $dbobj=false;
//MySQLi statement
private $stmt=false;
//returned column names
private $retNames=array();
//returned column data
private $retData=array();
//the query
private $query="";
//number of rows after SELECT query
public $numRows=false;
//number of affected rows after UPDATE/INSERT/DELETE query
public $affectedRows=false;
//Prepare and execute a statement
//Parameter(s): Query and additional parameters for prepared stmt
function __construct($query) {
DB::$querycount++;
DB::$querytime-=microtime(true);
// printf("Querying DB for '%s'",$query);
//get a link
if($this->dbobj===false) {
$this->dbobj=DB::get();
$this->link=$this->dbobj->getLink();
}
//Save the query
$this->query=$query;
//prepare a statement
$this->stmt=$this->link->prepare($query);
if($this->stmt===false)
throw new Exception(sprintf("MySQLi stmt_prepare failed for '%s': %s",$query,$this->dbobj->getError()));
//check if we have to bind parameters
if(func_num_args()>1) {
$args=func_get_args();
// var_dump($args);
array_shift($args); //remove query
$args_str="'".implode("', '",$args)."'";
// printf("Binding parameters %s",$args_str);
$params_ref=array("");
$typestr="";
foreach($args as $k=>$arg) {
//printf("at %d:%d\n",$k,$arg);
$params_ref[$k+1]=&$args[$k];
if(is_int($arg))
$typestr.="i";
else
$typestr.="s";
}
// var_dump($params_ref);
$params_ref[0]=&$typestr;
// var_dump($params_ref);
$ret=call_user_func_array(array($this->stmt,"bind_param"),$params_ref);
if($ret===false)
throw new Exception(sprintf("MySQLi stmt_bind_param failed for '%s': %s",$query,$this->dbobj->getError()));
// printf("Query is now %s %s",$query,$args_str);
} else
$args_str="";
if(!isset(DB::$queries[$query."||".$args_str]))
DB::$queries[$query."||".$args_str]=0;
DB::$queries[$query."||".$args_str]++;
//Execute
$ret=$this->stmt->execute();
if($ret===false)
throw new Exception(sprintf("MySQLi stmt_execute failed for '%s': %s",$query,$this->dbobj->getError()));
$this->stmt->store_result();
//Statistics
$this->numRows=$this->stmt->num_rows;
$this->affectedRows=$this->stmt->affected_rows;
$this->insertId=$this->link->insert_id;
//Check for errors
if($this->numRows===null || $this->numRows===false)
throw new Exception(sprintf("MySQLi numRows indicates error for '%s': %s",$query,$this->dbobj->getError()));
// printf("MySQLi numRows is %d",$this->numRows);
if($this->affectedRows===null || $this->affectedRows===false)
throw new Exception(sprintf("MySQLi affectedRows indicates error for '%s': %s",$query,$this->dbobj->getError()));
// printf("MySQLi affectedRows is %d",$this->affectedRows);
//if we don't have returned datasets, return to caller
if($this->numRows==0) {
DB::$querytime+=microtime(true);
return;
}
//get return field names (if any)
$meta=$this->stmt->result_metadata();
$params_ref=array();
while($field=$meta->fetch_field()) {
$this->retNames[]=$field->name;
$this->retData[$field->name]=$field->name;
$params_ref[]=&$this->retData[$field->name];
}
$meta->close();
$ret=call_user_func_array(array($this->stmt,"bind_result"),$params_ref);
if($ret===false)
throw new Exception(sprintf("MySQLi stmt_bind_result failed for '%s': %s",$query,$this->dbobj->getError()));
DB::$querytime+=microtime(true);
}
function fetch() {
DB::$querytime-=microtime(true);
$ret=$this->stmt->fetch();
if($ret===false) //error
throw new Exception(sprintf("MySQLi stmt_fetch failed for '%s': %s",$this->query,$this->dbobj->getError()));
elseif($ret===null) { //no more data
DB::$querytime+=microtime(true);
return null;
}
//format log-string and resolve the references
$dstr="";
$da=array();
$ret=array();
foreach($this->retData as $name=>$data) {
$da[]="$name=>$data";
$ret[$name]=$data;
}
$dstr=implode(", ",$da);
// printf("Fetched %s from db",$dstr);
DB::$querytime+=microtime(true);
//force array copy
return $ret;
}
function free() {
$this->stmt->close();
}
}