[Obliquid-cvs] obliquid/common/classes/general db_form.php, 1.125, 1.126
Status: Beta
Brought to you by:
slocati
|
From: <sl...@sc...> - 2007-03-03 11:09:32
|
Update of /cvsroot/obliquid/obliquid/common/classes/general In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv4961/classes/general Modified Files: db_form.php Log Message: Added method fetchWithWhere, getSqlReplace, executeSqlReplace. Replace is now implemented with MySQL REPLACE syntax and should be rewritten with DELETE + INSERT Index: db_form.php =================================================================== RCS file: /cvsroot/obliquid/obliquid/common/classes/general/db_form.php,v retrieving revision 1.125 retrieving revision 1.126 diff -b -u -d -r1.125 -r1.126 --- db_form.php 24 Oct 2006 11:47:43 -0000 1.125 +++ db_form.php 3 Mar 2007 11:09:26 -0000 1.126 @@ -199,16 +199,14 @@ } /** Fetches a record from db and fills internal status with it - * @param $id primary key of the record to fetch, supposed on a single field + * @param $where the where part of a sql instruction to fetch the + * record. Example $where="mtime<>'0000-00-00'"; * @return true in case of success, false otherwise */ - function fetch($id, $debug=false) + function fetchWithWhere($where, $debug=false) { - $prikey=$this->getPrimaryKeys(); - $this->_field[$prikey[0]]["DB_VALUE"]=$id; $sql="SELECT ".$this->_getFieldList()." FROM ".$this->_tprefix2 - .$this->_table_name." WHERE `".$prikey[0]."`=" - .$this->getValueForSql($prikey[0]); + .$this->_table_name." WHERE ".$where." LIMIT 1"; $types=array(); foreach ($this->_field as $key => $value) { if ($this->_field[$key]["IS_DB"]) { @@ -216,7 +214,10 @@ } } $ok=$this->_mb->QueryRow($sql, $row, $types); - if (!$ok) echo "ERROR: ".$this->_mb->Error()." "; + if (!$ok) { + $err = $this->_mb->Error(); + if ($err!="result set is empty") echo "ERROR: ".$err." "; + } if ($debug) echo $sql." "; $i=0; foreach ($this->_field as $fname => $value) { @@ -228,6 +229,18 @@ return $ok; } + /** Fetches a record from db and fills internal status with it + * @param $id primary key of the record to fetch, supposed on a single field + * @return true in case of success, false otherwise + */ + function fetch($id, $debug=false) + { + $prikey=$this->getPrimaryKeys(); + $this->_field[$prikey[0]]["DB_VALUE"]=$id; + $where = "`".$prikey[0]."`=".$this->getValueForSql($prikey[0]); + return $this->fetchWithWhere($where, $debug); + } + /** Fetches an array with all the keys from the current table * NOTE: uses only the first primary key field * @access private @@ -474,14 +487,14 @@ * @return String * @access private */ - function _getWhere() + function _getWhere($sep=" AND ") { $res = ""; $first = true; foreach ($this->_field as $field_name => $value) { if ($this->_field[$field_name]["IS_PRIMARY"]) { if (!$first) { - $res.=" AND "; + $res.=$sep; } else { $first=false; } @@ -699,8 +712,19 @@ return $res; } + /** returns a SQL statement to replace data according to the primary key value + * @return text + */ + function getSqlReplace() + { + $sql="REPLACE INTO ".$this->_tprefix2.$this->_table_name." SET " + .$this->_getWhere(", ").", ".$this->_getUpdateCore(); + return $sql; + } + /** executes a replace statement -- insert or update */ - function executeSqlReplace($debug=false, $logentry="", $module="", $sameop=true, $operation="") { + function executeSqlReplace($debug=false, $logentry="", $module="", $sameop=true, $operation="") + { $prikeys=$this->getPrimaryKeys(); $prikey_val=$this->getValue($prikeys[0]); $sql="SELECT COUNT(*) FROM ".$this->_tprefix2 |