<?php
/*
*(c) 2003 christoph althammer hide@address.com
* http://www.milpa.de
*
* GNU GENERAL PUBLIC LICENSE
* die deutsche Übersetzung der GPL ist zu finden auf www.suse.org.
*
* auth: c. althammer
* date: 2002-10-25
* lc : 2002-10-25
* comm: Data Structures
*
*
*/
class DbOperation {
var $PrimaryKey = "";
var $DatabaseTable = "";
var $Struct = array();
var $StructCount = 0;
var $Select = "";
var $StructLoaded = false;
var $DatabaseClass = "Database";
/* additional debug */
var $ObjTrace = false;
/* loads post data for given data structure */
function DbOperation($Struct="",$NoPostData=0){
if (!is_object($Struct)) {
new Debug("No Object given","DbOperation");
return false;
}
$this->Struct = $Struct->Data;
$this->DatabaseTable = $Struct->DatabaseTable;
$this->StructCount = count($this->Struct);
$this->PrimaryKey = $Struct->PrimaryKey;
if ($NoPostData == 0) {
$Vars = new Post;
new Debug("getting ".$this->StructCount." PostVars ","DbOperation");
for (reset($this->Struct); $Key = key($this->Struct); next($this->Struct) ){
$this->{$Key} = $Vars->Post("$Key");
$DebugStr = "Struct->$Key=".$this->{$Key};
$this->Trace($DebugStr);
}
}
$this->StructLoaded = true;
new Debug("Struct Loaded =");
new Debug($this->StructLoaded );
return true;
}
function Insert(){
if (($this->StructLoaded != true)||(empty($this->DatabaseTable))){
new Error("Insert Canceled: Structure Syntax Error","DbOperation");
return false;
}
new Debug("Insert","DbOperation");
$Query = "INSERT INTO ".$this->DatabaseTable;
$Fields = "";
$Values = "";
for (reset($this->Struct); $Key = key($this->Struct); next($this->Struct) ){
$Fields .= " $Key,";
$Values .= "'".$this->{$Key}."',";
}
$Fields = substr($Fields,0,strlen($Fields)-1);
$Values = substr($Values,0,strlen($Values)-1);
$Query .= " ( ".$Fields." ) VALUES ( ".$Values." )";
$this->Trace($Query);
$Result = new $this->DatabaseClass($Query);
$Result = mysql_insert_id();
new Debug("Insert ID:".$Result);
return $Result;
}
function UpdateOneField($PKey=0,$Field=""){
if (($Field == "")||(empty($PKey))){
new Error("UpdateOneField Canceled: Key or Field empty","DbOperation");
return false;
}
new Debug("Update Record $PKey,$Field","DbOperation");
$Query = "UPDATE ".$this->DatabaseTable;
$Values = " $Field = '".$this->{$Field}."'";
$Query .= " SET ".$Values." WHERE ".$this->PrimaryKey." = '".$PKey."'";
$this->Trace($Query);
$Result = new $this->DatabaseClass($Query);
return $Result;
}
function Update($PKey=0){
if (($this->StructLoaded != true)||(empty($PKey))){
new Error("Update Canceled: Key empty","DbOperation");
return false;
}
new Debug("Update Record $PKey","DbOperation");
$Query = "UPDATE ".$this->DatabaseTable;
$Values = "";
for (reset($this->Struct); $Key = key($this->Struct); next($this->Struct) ){
$Values .= $Key." = '".$this->{$Key}."' ,";
}
$Values = substr($Values,0,strlen($Values)-1);
$Query .= " SET ".$Values." WHERE ".$this->PrimaryKey." = '".$PKey."'";
$this->Trace($Query);
$Result = new $this->DatabaseClass($Query);
return $Result;
}
function Count($Clause=""){
if ($this->StructLoaded != true) {
new Debug("Count Canceled: Query or ObjStructure empty","DbOperation");
return false;
}
$Query = "SELECT * FROM ".$this->DatabaseTable." ".$Clause;
$this->Select = new $this->DatabaseClass($Query);
$Count = $this->Select->num_rows();
if (empty($Count)) $Count= 0;
new Debug("Count $Query: found $Count Records","DbOperation");
return $Count;
}
function getMinimum($Field="",$Where=""){
if ($this->StructLoaded != true) {
new Debug("Count Canceled: Query or ObjStructure empty","DbOperation");
return false;
}
$Query = "SELECT MIN($Field) FROM ".$this->DatabaseTable." ".$Where;
$this->RS = new $this->DatabaseClass($Query);
$Result = mysql_fetch_array($this->RS->Query_ID);
$Result= $Result[0];
new Debug("Min Value for ".$this->DatabaseTable.".$Field = ".$Result);
return $Result;
}
function getMaximum($Field="",$Where=""){
if ($this->StructLoaded != true) {
new Debug("Count Canceled: Query or ObjStructure empty","DbOperation");
return false;
}
$Query = "SELECT MAX($Field) FROM ".$this->DatabaseTable." ".$Where;
$this->RS = new $this->DatabaseClass($Query);
$Result = mysql_fetch_array($this->RS->Query_ID);
$Result= $Result[0];
new Debug("Max Value for ".$this->DatabaseTable.".$Field = ".$Result);
return $Result;
}
function Select($Clause="",$Fields=" * "){
if ($this->StructLoaded != true) {
new Debug("Select Canceled: Query or ObjStructure empty","DbOperation");
return false;
}
$Query = "SELECT $Fields FROM ".$this->DatabaseTable." ".$Clause;
$this->Trace($Query);
$this->Select = new $this->DatabaseClass($Query);
return $this->Select;
}
function FreeQuery($Q=""){
new Debug($Q);
if ($this->StructLoaded != true) {
new Debug("FreeQuery Canceled: Query or ObjStructure empty","DbOperation");
return false;
}
new Debug($Q);
$this->Trace($Q);
$this->Select = new $this->DatabaseClass($Q);
return $this->Select;
}
function NextRecord(){
if (!is_object($this->Select)) {
new Debug("Select with no Query pending","DbOperation");
return false;
}
$Flag = $this->Select->next_record();
$this->{$this->PrimaryKey} = $this->Select->f($this->PrimaryKey);
$Str = "<br>Tracing Next Record PrimaryKey=".$this->{$this->PrimaryKey};
for (reset($this->Struct); $Key = key($this->Struct); next($this->Struct) ){
$this->{$Key} = $this->Select->f("$Key");
$Str .= "<br>Var Struct->$Key = '".$this->{$Key}."'";
}
$this->Trace("NextRecord ".$Str);
return $Flag;
}
function NumRows(){
if (!is_object($this->Select)) {
new Debug("NumRows with no Query pending","DbOperation");
return 0;
}
return $this->Select->num_rows();
}
function Delete($PKey=0){
if (empty($PKey)){
new Error("Delete Canceled: Key empty","DbOperation");
return false;
}
new Debug("Delete Record $PKey","DbOperation");
$Query = "DELETE FROM ".$this->DatabaseTable;
$Query .=" WHERE ".$this->PrimaryKey." = '".$PKey."'";
$this->Trace($Query);
$Result = new $this->DatabaseClass($Query);
return $Result;
}
function DeleteWhere($Clause=""){
if (empty($Clause)){
new Error("Delete Canceled: Where Clause empty","DbOperation");
return false;
}
new Debug("Delete MultipleRecords $Clause","DbOperation");
$Query = "DELETE FROM ".$this->DatabaseTable;
$Query .=" ".$Clause;
$this->Trace($Query);
$Result = new $this->DatabaseClass($Query);
return $Result;
}
function Trace($Str=""){
if($this->ObjTrace==false)return false;
new Debug($Str,"DbOperations");
return true;
}
}/* end of class */
?>