00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 class Db extends mysqli {
00038
00039
00040
00041
00042
00043
00044 function __construct() {
00045 global $mysql_host, $mysql_username, $mysql_password, $mysql_dbname;
00046 parent::__construct($mysql_host, $mysql_username, $mysql_password, $mysql_dbname);
00047 if (mysqli_connect_errno()):
00048 throw new Exception(sprintf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error()));
00049 endif;
00050 parent::query("SET NAMES 'utf8'");
00051 }
00052
00053
00054
00055
00056
00057 function query($sql) {
00058 $result = parent::query($sql);
00059 if (!$result):
00060 throw new Exception(sprintf("Error in query %s. Error message: %s\n", $sql, $this->error));
00061 endif;
00062 return $result;
00063 }
00064
00065 }
00066
00067
00068
00069
00070
00071
00072
00073
00074 class DbQuery implements Iterator {
00075
00076 private $sql;
00077 private $result = NULL;
00078 private $row;
00079
00080 function __construct($sql) {
00081 $this->sql = $sql;
00082 }
00083
00084
00085
00086
00087 function rewind() {
00088 if (!is_null($this->result)):
00089 $this->result->close();
00090 $this->result = NULL;
00091 endif;
00092 $db = new Db();
00093 $this->result = $db->query($this->sql);
00094 $this->row = $this->result->fetch_object();
00095 }
00096
00097 function current() {
00098 return $this->row;
00099 }
00100
00101
00102 function key() { return 0; }
00103
00104 function next() {
00105 $this->row = $this->result->fetch_object();
00106 }
00107
00108 function valid() {
00109 return $this->result and $this->row;
00110 }
00111
00112 }
00113
00114
00115
00116
00117 function query($sql) {
00118 $q = new DbQuery($sql);
00119 return $q;
00120 }
00121
00122
00123
00124
00125
00126
00127
00128
00129 function insert_unique($sql, $errormsg) {
00130 $db = new Db();
00131 try {
00132 $db->query($sql);
00133 }
00134 catch (Exception $e) {
00135 if ($db->errno == 1062):
00136 throw new Exception($errormsg);
00137 else:
00138 throw $e;
00139 endif;
00140 }
00141 $db->close();
00142 }
00143
00144
00145
00146
00147
00148
00149
00150 function exists($sql) {
00151 $db = new Db();
00152 $result = $db->query($sql);
00153
00154
00155
00156
00157
00158
00159
00160
00161 $count = $result->fetch_row();
00162 $count = $count[0];
00163
00164 $db->close();
00165 if ($count == 1):
00166 return TRUE;
00167 endif;
00168 return FALSE;
00169 }
00170
00171
00172
00173
00174 class RecordNotFound extends Exception {}
00175
00176
00177
00178
00179
00180
00181
00182 function one_line_query($sql, $errormsg = 'Error.') {
00183 $db = new Db();
00184 $result = $db->query($sql);
00185 $obj = $result->fetch_object();
00186 if (!$obj):
00187 throw new RecordNotFound($errormsg);
00188 endif;
00189 return $obj;
00190 }
00191
00192
00193
00194
00195
00196 function update($sql) {
00197 $db = new Db();
00198 $db->query($sql);
00199 $db->close();
00200 }
00201
00202
00203
00204
00205
00206
00207
00208 function insert($sql) {
00209 $db = new Db();
00210 $db->query($sql);
00211 $id = $db->insert_id;
00212 $db->close();
00213 return $id;
00214 }
00215
00216
00217
00218
00219
00220 function get_identity_value($obj) {
00221 return $obj;
00222 }
00223
00224
00225
00226
00227
00228 function get_id_value($obj) {
00229 return $obj->get_id();
00230 }
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 function build_insert_sql_from_array($array, $pre = '', $post = '', $quote = FALSE, $value_getter = 'get_identity_value') {
00258 if ($pre):
00259 $pre = "$pre,";
00260 endif;
00261 if ($post):
00262 $post = ",$post";
00263 endif;
00264 $sqllines = array();
00265 foreach ($array as $obj):
00266 $value = $value_getter($obj);
00267 if ($quote):
00268 $sqllines[] = "($pre '$value' $post)";
00269 else:
00270 $sqllines[] = "($pre $value $post)";
00271 endif;
00272 endforeach;
00273 return join(',', $sqllines);
00274 }
00275
00276 ?>