Location: PHPKode > scripts > Database access class > database-access-class/databaseaccessclass.php
<?php
if(!defined("PHP_LIBRARY_DATABASE_ACCESS_CLASS"))
{
 define("PHP_LIBRARY_DATABASE_ACCESS_CLASS",1);

/*
 *
 * @(#) $Id: databaseaccessclass.class,v 1.17 2001/05/06 02:24:21 mlemos Exp $
 *
 */

class database_access_class
{
 var $database=0;
 var $error="";
 var $changes=0;
 var $date_year_suffix="_year";
 var $date_month_suffix="_month";
 var $date_day_suffix="_day";
 var $date_hour_suffix="_hour";
 var $date_minute_suffix="_minute";
 var $date_second_suffix="_second";
 var $verify_multiple_selected_rows=1;

 Function convertfieldvalue($fieldvalue,$type)
 {
  $database=$this->database;
  switch($type)
  {
   case "TEXT":
    return MetabaseGetTextFieldValue($database,$fieldvalue);
    break;
   case "INTEGER":
    return ((!strcmp(strval(intval($fieldvalue)),$fieldvalue)) ? intval($fieldvalue) : "NULL");
    break;
   case "DECIMAL":
    return ((ereg("^[0-9]+(\.[0-9]*)?$",$fieldvalue)) ? MetabaseGetDecimalFieldValue($database,$fieldvalue) : "NULL");
    break;
   case "FLOAT":
    return ((!strcmp(strval(doubleval($fieldvalue)),$fieldvalue)) ? MetabaseGetFloatFieldValue($database,(doubleval($fieldvalue)!=0)) : "NULL");
    break;
   case "DATE":
    return MetabaseGetDateFieldValue($database,$fieldvalue);
    break;
   case "TIMESTAMP":
    return MetabaseGetTimeStampFieldValue($database,$fieldvalue);
    break;
   case "BOOLEAN":
    return MetabaseGetBooleanFieldValue($database,(intval($fieldvalue)!=0));
    break;
   default:
    return $fieldvalue;
  }
 }

 Function insertformvalues(&$form,$table,$fields,$inputs,$nullmap,$extrafields)
 {
  $database=$this->database;
  $field_names="";
  $field_values="";
  $value=0;
  Reset($fields);
  for(;$value<Count($fields);)
  {
   $field_name=Key($fields);
   $input_name=((IsSet($inputs[$field_name])) ? $inputs[$field_name] : $field_name);
   $type=$fields[$field_name];
   switch($type)
   {
    case "DATE":
     $field_value=($form->GetInputValue($input_name.$this->date_year_suffix)."-".$form->GetInputValue($input_name.$this->date_month_suffix)."-".$form->GetInputValue($input_name.$this->date_day_suffix));
     break;
    case "TIMESTAMP":
     $field_value=($form->GetInputValue($input_name.$this->date_year_suffix)."-".$form->GetInputValue($input_name.$this->date_month_suffix)."-".$form->GetInputValue($input_name.$this->date_day_suffix)." ".$form->GetInputValue($input_name.$this->date_hour_suffix).":".$form->GetInputValue($input_name.$this->date_minute_suffix).":".$form->GetInputValue($input_name.$this->date_second_suffix));
     break;
    default:
     $field_value=$form->GetInputValue($input_name);
     break;
   }
   switch($type)
   {
    case "TEXT":
    case "INTEGER":
    case "DECIMAL":
    case "FLOAT":
    case "DATE":
    case "TIMESTAMP":
    case "BOOLEAN":
     if((IsSet($nullmap[$field_name]) && !strcmp($nullmap[$field_name],$field_value)))
      $field_value="NULL";
     else
     {
      if((!strcmp($type,"BOOLEAN") && !(!strcmp($field_value,"0") || !strcmp($field_value,"1"))))
      {
       $this->error=("field \"".$field_name."\" does not contain a valid boolean value");
       return 0;
      }
      $field_value=$this->convertfieldvalue($field_value,$fields[$field_name]);
     }
     break;
    default:
     $this->error=("it was not specified a supported a database table column type (".$fields[$field_name].")");
     return 0;
   }
   if((strcmp($field_names,"")))
    $field_names=($field_names.", ");
   $field_names=($field_names.$field_name);
   if((strcmp($field_values,"")))
    $field_values=($field_values.", ");
   $field_values=($field_values.$field_value);
   $value++;
   Next($fields);
  }
  $value=0;
  Reset($extrafields);
  for(;$value<Count($extrafields);)
  {
   if((strcmp($field_names,"")))
    $field_names=($field_names.", ");
   $field_names=($field_names.Key($extrafields));
   if((strcmp($field_values,"")))
    $field_values=($field_values.", ");
   $field_values=($field_values.$extrafields[Key($extrafields)]);
   $value++;
   Next($extrafields);
  }
  $query=("INSERT INTO ".$table." (".$field_names.") VALUES (".$field_values.")");
  if((MetabaseQuery($database,$query)==0))
  {
   $this->error=MetabaseError($database);
   return 0;
  }
  return 1;
 }

 Function selectformvalues(&$form,$table,$fields,$inputs,$nullmap,$condition)
 {
  $database=$this->database;
  $query="SELECT ";
  $field=0;
  Reset($fields);
  for(;$field<Count($fields);)
  {
   if(($field>0))
    $query=($query.", ");
   $query=($query.Key($fields));
   $field++;
   Next($fields);
  }
  if(($field==0))
  {
   $this->error="it were not specified any table fields to select from";
   return 0;
  }
  $query=($query." FROM ".$table.$condition);
  $result=MetabaseQuery($database,$query);
  if(($result==0))
  {
   $this->error=MetabaseError($database);
   return 0;
  }
  if((MetabaseEndOfResult($database,$result)))
  {
   MetabaseFreeResult($database,$result);
   $this->error="selected table result does not contain any rows";
   return 0;
  }
  $field=0;
  Reset($fields);
  for(;$field<Count($fields);)
  {
   $field_name=Key($fields);
   $isnull=MetabaseResultIsNull($database,$result,0,$field_name);
   if((!$isnull || IsSet($nullmap[$field_name])))
   {
    if(($isnull))
     $field_value=$nullmap[$field_name];
    else
    {
     switch($fields[$field_name])
     {
      case "TEXT":
       $field_value=MetabaseFetchResult($database,$result,0,$field_name);
       break;
      case "INTEGER":
       $field_value=strval(intval(MetabaseFetchResult($database,$result,0,$field_name)));
       break;
      case "DECIMAL":
       $field_value=MetabaseFetchDecimalResult($database,$result,0,$field_name);
       break;
      case "FLOAT":
       $field_value=strval(doubleval(MetabaseFetchFloatResult($database,$result,0,$field_name)));
       break;
      case "DATE":
       $field_value=MetabaseFetchDateResult($database,$result,0,$field_name);
       break;
      case "TIMESTAMP":
       $field_value=MetabaseFetchTimestampResult($database,$result,0,$field_name);
       break;
      case "BOOLEAN":
       $field_value=strval(intval(MetabaseFetchBooleanResult($database,$result,0,$field_name)));
       break;
      default:
       MetabaseFreeResult($database,$result);
       $this->error=("it was not specified a supported a database table column type (".$fields[$field_name].")");
       return 0;
     }
    }
    $input_name=((IsSet($inputs[$field_name])) ? $inputs[$field_name] : $field_name);
    switch($fields[$field_name])
    {
     case "TEXT":
     case "INTEGER":
     case "DECIMAL":
     case "FLOAT":
     case "BOOLEAN":
      $form->SetInputValue($input_name,$field_value);
      break;
     case "DATE":
      $form->SetInputValue($input_name.$this->date_year_suffix,substr($field_value,0,4));
      $form->SetInputValue($input_name.$this->date_month_suffix,substr($field_value,5,2));
      $form->SetInputValue($input_name.$this->date_day_suffix,substr($field_value,8,2));
      break;
     case "TIMESTAMP":
      $form->SetInputValue($input_name.$this->date_year_suffix,substr($field_value,0,4));
      $form->SetInputValue($input_name.$this->date_month_suffix,substr($field_value,5,2));
      $form->SetInputValue($input_name.$this->date_day_suffix,substr($field_value,8,2));
      $form->SetInputValue($input_name.$this->date_hour_suffix,substr($field_value,11,2));
      $form->SetInputValue($input_name.$this->date_minute_suffix,substr($field_value,14,2));
      $form->SetInputValue($input_name.$this->date_second_suffix,substr($field_value,17,2));
      break;
    }
   }
   $field++;
   Next($fields);
  }
  if(($this->verify_multiple_selected_rows && !MetabaseEndOfResult($database,$result)))
  {
   MetabaseFreeResult($database,$result);
   $this->error="selected table result contains more than one row";
   return 0;
  }
  MetabaseFreeResult($database,$result);
  return 1;
 }

 Function deleteformvalues(&$form,$table,$fields,$inputs,$nullmap,$condition,$extrafields)
 {
  $database=$this->database;
  $query=("DELETE FROM ".$table." WHERE ");
  $delete_condition=$condition;
  $value=0;
  Reset($fields);
  for(;$value<Count($fields);)
  {
   $field_name=Key($fields);
   $input_name=((IsSet($inputs[$field_name])) ? $inputs[$field_name] : $field_name);
   $type=$fields[$field_name];
   switch($type)
   {
    case "DATE":
     $field_value=($form->GetInputValue($input_name.$this->date_year_suffix)."-".$form->GetInputValue($input_name.$this->date_month_suffix)."-".$form->GetInputValue($input_name.$this->date_day_suffix));
     break;
    case "TIMESTAMP":
     $field_value=($form->GetInputValue($input_name.$this->date_year_suffix)."-".$form->GetInputValue($input_name.$this->date_month_suffix)."-".$form->GetInputValue($input_name.$this->date_day_suffix)." ".$form->GetInputValue($input_name.$this->date_hour_suffix).":".$form->GetInputValue($input_name.$this->date_minute_suffix).":".$form->GetInputValue($input_name.$this->date_second_suffix));
     break;
    default:
     $field_value=$form->GetInputValue($input_name);
     break;
   }
   if((strcmp($delete_condition,"")))
    $delete_condition=($delete_condition." AND ");
   switch($type)
   {
    case "TEXT":
    case "INTEGER":
    case "DECIMAL":
    case "FLOAT":
    case "DATE":
    case "TIMESTAMP":
    case "BOOLEAN":
     $field_value=((IsSet($nullmap[$field_name]) && !strcmp($nullmap[$field_name],$field_value)) ? " IS NULL" : "=".$this->convertfieldvalue($field_value,$fields[$field_name]));
     break;
    default:
     $this->error=("it was not specified a supported a database table column type (".$fields[$field_name].")");
     return 0;
   }
   $delete_condition=($delete_condition.$field_name.$field_value);
   $value++;
   Next($fields);
  }
  $value=0;
  Reset($extrafields);
  for(;$value<Count($extrafields);)
  {
   if((strcmp($delete_condition,"")))
    $delete_condition=($delete_condition." AND ");
   $delete_condition=($delete_condition.Key($extrafields)."=".$extrafields[Key($extrafields)]);
   $value++;
   Next($extrafields);
  }
  if((strcmp($delete_condition,"")))
  {
   if((MetabaseQuery($database,$query.$delete_condition)==0))
   {
    $this->error=MetabaseError($database);
    return 0;
   }
  }
  else
  {
   $this->error="it were not specified any delete search conditions";
   return 0;
  }
  return 1;
 }

 Function updateformchanges(&$form,$table,$fields,$nullmap,$inputs,$condition,$extrafields)
 {
  $database=$this->database;
  $query=("UPDATE ".$table." SET ");
  $value=0;
  $changes=0;
  Reset($fields);
  for(;$value<Count($fields);)
  {
   $field_name=Key($fields);
   $input_name=((IsSet($inputs[$field_name])) ? $inputs[$field_name] : $field_name);
   $type=$fields[$field_name];
   switch($type)
   {
    case "DATE":
     $changed=(IsSet($form->Changes[($input_name.$this->date_year_suffix)]) || IsSet($form->Changes[($input_name.$this->date_month_suffix)]) || IsSet($form->Changes[($input_name.$this->date_day_suffix)]));
     if(($changed))
      $field_value=($form->GetInputValue($input_name.$this->date_year_suffix)."-".$form->GetInputValue($input_name.$this->date_month_suffix)."-".$form->GetInputValue($input_name.$this->date_day_suffix));
     break;
    case "TIMESTAMP":
     $changed=(IsSet($form->Changes[($input_name.$this->date_year_suffix)]) || IsSet($form->Changes[($input_name.$this->date_month_suffix)]) || IsSet($form->Changes[($input_name.$this->date_day_suffix)]) || IsSet($form->Changes[($input_name.$this->date_hour_suffix)]) || IsSet($form->Changes[($input_name.$this->date_minute_suffix)]) || IsSet($form->Changes[($input_name.$this->date_second_suffix)]));
     if(($changed))
      $field_value=($form->GetInputValue($input_name.$this->date_year_suffix)."-".$form->GetInputValue($input_name.$this->date_month_suffix)."-".$form->GetInputValue($input_name.$this->date_day_suffix)." ".$form->GetInputValue($input_name.$this->date_hour_suffix).":".$form->GetInputValue($input_name.$this->date_minute_suffix).":".$form->GetInputValue($input_name.$this->date_second_suffix));
     break;
    default:
     $changed=IsSet($form->Changes[$input_name]);
     if(($changed))
      $field_value=$form->GetInputValue($input_name);
     break;
   }
   if(($changed))
   {
    if(($changes>0))
     $query=($query.", ");
    switch($type)
    {
     case "TEXT":
     case "INTEGER":
     case "DECIMAL":
     case "FLOAT":
     case "DATE":
     case "TIMESTAMP":
     case "BOOLEAN":
      if((IsSet($nullmap[$field_name]) && !strcmp($nullmap[$field_name],$field_value)))
       $field_value="NULL";
      else
      {
       if((!strcmp($type,"BOOLEAN") && !(!strcmp($field_value,"0") || !strcmp($field_value,"1"))))
       {
        $this->error=("field \"".$field_name."\" does not contain a valid boolean value");
        return 0;
       }
       $field_value=$this->convertfieldvalue($field_value,$fields[$field_name]);
      }
      break;
     default:
      $this->error=("it was not specified a supported a database table column type (".$fields[$field_name].")");
      return 0;
    }
    $query=($query.$field_name."=".$field_value);
    $changes++;
   }
   $value++;
   Next($fields);
  }
  if(($changes>0))
  {
   $value=0;
   Reset($extrafields);
   for(;$value<Count($extrafields);)
   {
    $query=($query.", ".Key($extrafields)."=".$extrafields[Key($extrafields)]);
    $value++;
    Next($extrafields);
   }
   if((MetabaseQuery($database,$query.$condition)==0))
   {
    $this->error=MetabaseError($database);
    return 0;
   }
  }
  $this->changes=($this->changes+$changes);
  return 1;
 }

 Function selectlistvalues(&$list,$table,$indexes,$values,$condition,$emptyerror)
 {
  $query=("SELECT ".$indexes.",".$values." FROM ".$table.$condition);
  $database=$this->database;
  $result=MetabaseQuery($database,$query);
  if(($result==0))
  {
   $this->error=MetabaseError($database);
   return 0;
  }
  $row=0;
  for(;!MetabaseEndOfResult($database,$result);)
  {
   $entry=MetabaseFetchResult($database,$result,$row,$indexes);
   if((IsSet($list[$entry])))
   {
    MetabaseFreeResult($database,$result);
    $this->error="selected table result contains duplicated entry";
    return 0;
   }
   $list[$entry]=MetabaseFetchResult($database,$result,$row,$values);
   $row++;
  }
  MetabaseFreeResult($database,$result);
  if(($row==0 && strcmp($emptyerror,"")))
  {
   $this->error=$emptyerror;
   return 0;
  }
  return 1;
 }
};

}
?>
Return current item: Database access class