00001 <?php
00010 require_once(DIR_FS_PRONTO.DS.'extlib'.DS.'safesql.php');
00011
00012 class DB_Base
00013 {
00014 var $conn;
00015 var $query;
00016 var $result;
00017 var $error;
00018 var $insert_id;
00019 var $echo = false;
00020 var $debug = false;
00021 var $profile = false;
00022 var $profile_data = array();
00023
00029 function _catch($msg="") {
00030 $this->error($msg);
00031 }
00032
00038 function error($msg) {
00039 throw new Exception($msg);
00040 }
00041
00054 function build_sql($select, $from, $where='', $group='', $having='', $order='', $limit='')
00055 {
00056 $sql = "SELECT $select FROM $from";
00057 if($where) $sql .= " WHERE $where";
00058 if($group) $sql .= " GROUP BY $group";
00059 if($having) $sql .= " HAVING $having";
00060 if($order) $sql .= " ORDER BY $order";
00061 if($limit) $sql .= " LIMIT $limit";
00062 return $sql;
00063 }
00064
00074 function &query($query_str, $query_arg="", $bypass=false) {
00075 if($bypass == true) return($query_str);
00076 return $this->safesql->query($query_str, $query_arg);
00077 }
00078
00087 function &execute($query_str, $query_arg="", $bypass=false) {
00088 $this->query = $bypass ? $query_str : $this->query($query_str, $query_arg);
00089
00090 if($this->profile !== false) {
00091 $bt = debug_backtrace();
00092 $loc = '';
00093 if(isset($bt[1])) {
00094 $loc = $bt[1]['file'].':'.$bt[1]['line'].' ';
00095 }
00096 $start = array_sum(explode(" ",microtime()));
00097 if($this->profile !== true) {
00098 $fp = fopen($this->profile, "a+");
00099 fwrite($fp, $loc.$this->query."\n");
00100 fclose($fp);
00101 }
00102 }
00103
00104 $this->result = $this->run_query($this->query) or $this->_catch();
00105
00106 if($this->profile !== false) {
00107 $delta = round((array_sum(explode(" ",microtime()))-$start)*1000, 4);
00108 if($this->profile === true) {
00109 $this->profile_data[] = array(
00110 'query' => $this->query,
00111 'time' => $delta
00112 );
00113 } else {
00114 $fp = fopen($this->profile, "a+");
00115 fwrite($fp, "\t\tD=$delta ms\n\n");
00116 fclose($fp);
00117 }
00118 }
00119
00120 if($this->echo == true) echo $this->query."\n";
00121 $this->insert_id = $this->get_insert_id();
00122 return $this->result;
00123 }
00124
00132 function &get_item($query_str, $query_arg="") {
00133 $q = $this->execute($query_str, $query_arg);
00134 if(($item = $this->fetch_array($q, true))) {
00135 return $item;
00136 } else {
00137 $ret = false;
00138 return $ret;
00139 }
00140 }
00141
00150 function get_item_by_pk($table, $pk, $pk_col='id') {
00151 return $this->get_item("SELECT * FROM $table WHERE \"$pk_col\"=%i LIMIT 1", array($pk));
00152 }
00153
00162 function &get_all($query_str, $query_arg="", $key="") {
00163 $result = $this->execute($query_str, $query_arg);
00164 $list = array();
00165 while($row = @$this->fetch_array($result)) {
00166 if(empty($key)) {
00167 $list[] = $row;
00168 } else {
00169 $list[$row[$key]] = $row;
00170 }
00171 }
00172 $this->free_result($result);
00173 return $list;
00174 }
00175
00185 function &get_value($query_str, $query_arg="", $value="") {
00186 if(empty($value)) {
00187 if($item = $this->fetch_row($this->execute($query_str, $query_arg), true)) {
00188 return $item[0];
00189 } else {
00190 $ret = false;
00191 return $ret;
00192 }
00193 } else {
00194 if($item = $this->fetch_array($this->execute($query_str, $query_arg), true)) {
00195 return $item[$value];
00196 } else {
00197 $ret = false;
00198 return $ret;
00199 }
00200 }
00201 }
00202
00212 function &get_values($query_str, $query_arg="", $value="") {
00213 $result = $this->execute($query_str, $query_arg);
00214 $values = array();
00215 if(empty($value)) {
00216 while($item = $this->fetch_row($result)) {
00217 $values[] = $item[0];
00218 }
00219 } else {
00220 while($item = $this->fetch_array($result)) {
00221 $values[] = $item[$value];
00222 }
00223 }
00224 $this->free_result($result);
00225 return $values;
00226 }
00227
00238 function &get_item_pair($query_str, $query_arg="") {
00239 if($item = $this->fetch_array($this->execute($query_str, $query_arg), true)) {
00240 if(count($item) > 2) {
00241 $key = array_shift($item);
00242 $sub = array();
00243 foreach($item as $k=>$v) {
00244 $sub[$k] = $v;
00245 }
00246 return array($key => $sub);
00247 } else {
00248 $key = array_shift($item);
00249 $val = array_shift($item);
00250 return array($key => $val);
00251 }
00252 } else {
00253 $ret = false;
00254 return $ret;
00255 }
00256 }
00257
00268 function &get_all_pair($query_str, $query_arg="") {
00269 $result = $this->execute($query_str, $query_arg);
00270 $list = array();
00271
00272 while($item = @$this->fetch_array($result)) {
00273 if(count($item) > 2) {
00274 $key = array_shift($item);
00275 $sub = array();
00276 foreach($item as $k=>$v) {
00277 $sub[$k] = $v;
00278 }
00279 $list[$key] = $sub;
00280 } else {
00281 $key = array_shift($item);
00282 $val = array_shift($item);
00283 $list[$key] = $val;
00284 }
00285 }
00286 $this->free_result($result);
00287 return $list;
00288 }
00289
00297 function insert_all($table, $data, $aFields="", $mode='insert') {
00298 if(!$data) return;
00299 $fields = array();
00300 $values = array();
00301 $arg = array();
00302 foreach($data as $k=>$row) {
00303 $val = array();
00304 if(is_array($aFields)) {
00305 $new_row = array();
00306 foreach($aFields as $f=>$v) {
00307 $new_row[$f] = isset($row[$f]) ? $row[$f] : $v['value'];
00308 }
00309 $row = $new_row;
00310 }
00311 foreach($row as $f=>$v) {
00312 if(!$k) $fields[] = "\"$f\"";
00313 if(is_array($v)) {
00314 $val[] = $v[0];
00315 } else {
00316 $val[] = "'%s'";
00317 $arg[] = $v;
00318 }
00319 }
00320 $values[] = "(".join(",", $val).")";
00321 }
00322 $this->execute("$mode INTO ".$table." (".join(",",$fields).") VALUES ".join(",",$values), $arg);
00323 return $this->insert_id;
00324 }
00325
00333 function replace_all($table, $data, $aFields="") {
00334 return $this->insert_all($table, $data, $aFields, 'replace');
00335 }
00336
00346 function update_all($table, $data, $where="", $where_arg="", $aUpdateFields="") {
00347 if(!$data) return;
00348 if(!$where_arg) $where_arg = array();
00349 if($where) $aWhere[] = $where;
00350 if($aUpdateFields) {
00351 foreach($aUpdateFields as $f) $aWhere[] = $f."='%s'";
00352 }
00353 if($aWhere) $sWhere = " WHERE (".join(") AND (", $aWhere).")";
00354 foreach($data as $k=>$row) {
00355 $aFields = array();
00356 $aArg = array();
00357 foreach($row as $f=>$v) {
00358 if(!is_numeric($f)) {
00359 if(is_array($v)) {
00360 $val = $v[0];
00361 } else {
00362 $val = "'%s'";
00363 $aArg[] = $v;
00364 }
00365 }
00366 $aFields[] = "\"$f\"=$val";
00367 }
00368 $aArg = array_merge($aArg, $where_arg);
00369 if($aUpdateFields) {
00370 foreach($aUpdateFields as $f) {
00371 $aArg[] = $row[$f];
00372 }
00373 }
00374 $this->execute("UPDATE ".$table." SET ".join(",",$aFields).$sWhere, $aArg);
00375 }
00376 }
00377
00386 function insert_row($sTable, $aRow, $mode='insert') {
00387 $mode = $mode == 'replace' ? 'REPLACE' : 'INSERT';
00388 $fs = $this->get_table_defn($sTable);
00389
00390 $aFields = array();
00391 $aValues = array();
00392 $aArgs = array();
00393 foreach($aRow as $k=>$v) {
00394
00395
00396
00397 if(!$fs || isset($fs[$k])) {
00398 $aFields[] = "\"$k\"";
00399 $aValues[] = "'%s'";
00400 $aArgs[] = $v;
00401 }
00402 }
00403 $this->execute("$mode INTO ".$sTable." (".join(",",$aFields).") VALUES (".join(",",$aValues).")", $aArgs);
00404
00405 return $this->insert_id;
00406 }
00407
00415 function replace_row($sTable, $aRow) {
00416 return $this->insert_row($sTable, $aRow, 'replace');
00417 }
00418
00427 function update_row($sTable, $aRow, $sWhere, $aWhereArgs=array()) {
00428 $fs = $this->get_table_defn($sTable);
00429
00430 $aFields = array();
00431 $aArgs = array();
00432 foreach($aRow as $k=>$v) {
00433
00434
00435
00436 if(!$fs || isset($fs[$k])) {
00437 $aFields[] = "\"$k\"='%s'";
00438 $aArgs[] = $v;
00439 }
00440 }
00441 if(is_array($aWhereArgs) && !empty($aWhereArgs)) {
00442 foreach ($aWhereArgs as $v) $aArgs[] = $v;
00443 }
00444 $this->execute("UPDATE \"".$sTable."\" SET ".join(",", $aFields)." WHERE ".$sWhere, $aArgs);
00445 }
00446
00456 function insert_update_row($sTable, $aRow, $aKey) {
00457 $aKeyWhere = array();
00458 $aKeyArgs = array();
00459 foreach($aKey as $k=>$v) {
00460 $aKeyWhere[] = "\"$k\"='%s'";
00461 $aKeyArgs[] = $v;
00462 }
00463 $sKeyWhere = join(' AND ', $aKeyWhere);
00464
00465 if($this->get_item('SELECT * FROM '.$sTable.' WHERE ('.$sKeyWhere.')', $aKeyArgs)) {
00466 $this->update_row($sTable, $aRow, $sKeyWhere, $aKeyArgs);
00467 } else {
00468 $this->insert_row($sTable, array_merge($aKey, $aRow));
00469 }
00470 }
00471
00483 function increment_row($sTable, $sField, $aKey) {
00484 $aKeyWhere = array();
00485 $aKeyArgs = array();
00486 foreach($aKey as $k=>$v) {
00487 $aKeyWhere[] = "\"$k\"='%s'";
00488 $aKeyArgs[] = $v;
00489 }
00490 $sKeyWhere = join(' AND ', $aKeyWhere);
00491
00492 if($this->get_item('SELECT * FROM '.$sTable.' WHERE ('.$sKeyWhere.')', $aKeyArgs)) {
00493 $this->execute("UPDATE $sTable SET $sField=$sField+1 WHERE ($sKeyWhere)", $aKeyArgs);
00494 } else {
00495 $this->insert_row($sTable, array_merge($aKey, array($sField=>'1')));
00496 }
00497 }
00498
00499 }
00500
00501 ?>