Location: PHPKode > scripts > autoformBQ > autoformbq/class_field.php
<?php

define("NOT_NULL" ,1);
define("PRIMARY_KEY", 2);
define("UNSIGNED", 4);
define("ZEROFILL", 8);
define("AUTO_INCREMENT", 16);
define("MULTIPLE_KEY", 32);
define("ENUM", 64);
define("SET", 128);
define("TEXT", 256); // mysql returns blob at text
define("BLOB", 256); // so both are defined equal
define("BINARY", 512);


//  name-conventions:
// $aXx = array, $strXx = string, $bXx = bool, $iXx = int
class field
{
  var $strName			= "";
  var $strContent		= ""; // DB-Content if not empty
  var $strType			= ""; // int string date etc...
  var $iMaxLen			= 0;  // int max len of column
  var $aPossibleValues  = array(); // array with possible Values of enum|set
  var $bitFlags			= 0;       // bitwise settings
  var $iConnID			= 0;       // mySql-connection
  var $strTableName		= ""; // the table that keeps this field
  var $aPrecision		= array(); // Number of precision and decimals
  var $strFormatPattern	= "";
  var $bDebug           = FALSE;

function field($iConnectionID,$result, $field_ofset)
{
   // get the current field-content
   if(mysql_num_rows($result)>0)
   {
     mysql_data_seek ($result, 0);
	 $row=mysql_fetch_row($result);
     $this->strContent = $row[$field_ofset];
   }


   // remember if there is a enum or set field
   $enum_set_counter = 0;

  // store ConnID
  $this->iConnID = $iConnectionID;

  // retrieve field-data
  $this->strType  = mysql_field_type($result,$field_ofset);
  $this->strName  = mysql_field_name($result,$field_ofset);
  $this->iMaxLen  = mysql_field_len($result, $field_ofset);
  $this->strTableName = mysql_field_table($result, $field_ofset);

  // check the field-flags
  $flags =  mysql_field_flags($result,$field_ofset);
  // set matching bits
  if (eregi("not_null",$flags))
     $this->bitFlags += NOT_NULL;
  if (eregi("primary_key",$flags))
     $this->bitFlags += PRIMARY_KEY;
  if (eregi("auto_increment",$flags))
     $this->bitFlags += AUTO_INCREMENT;
  if (eregi("unsigned",$flags))
     $this->bitFlags += UNSIGNED;
  if (eregi("zerofill",$flags))
     $this->bitFlags += ZEROFILL;
  if (eregi("enum",$flags))
  {
     $this->bitFlags += ENUM;
	 $enum_set_counter++;
  }
  if (eregi("set",$flags))
  {
     $this->bitFlags += SET;
	 $enum_set_counter++;
  }
  if (eregi("text",$flags))
     $this->bitFlags += TEXT;
  if (eregi("blob",$flags))
     $this->bitFlags += BLOB;
  if (eregi("binary",$flags))
     $this->bitFlags += BINARY;

  // if there is a set or enum column get the values
  if ($enum_set_counter)
     $this->check_enumset($result, $field_ofset);

  // get precision and number of decimals ( if defined )
  $this->check_precision();
} // End function field


function check_precision()
{
	// get the column-data
	$query ="SHOW COLUMNS FROM "
			.$this->strTableName
			." LIKE '"
			.$this->strName
			."'";
   $result2 = @mysql_query($query,$this->iConnID);
   if(mysql_num_rows($result2)>0)
   {
      $row=mysql_fetch_row($result2);
	  $this->strFormatPattern = $row[4];

      if ( $this->strType == "real")
	     $this->aPrecision = $prec = explode(",",preg_replace("|.*\((.*)\).*|", "$1", $row[1]));
   }
}

function check_enumset($result, $field_ofset)
{   // build the query
   $query ="SHOW COLUMNS FROM ".$this->strTableName." LIKE '".$this->strName."'";
   $result2=mysql_query($query,$this->iConnID);
   if(mysql_num_rows($result2)>0)
   {
     $row=mysql_fetch_row($result2);
     $this->aPossibleValues = explode("','",preg_replace("/(enum|set)\('(.+?)'\)/","\\2",$row[1]));
   }



} // end function get_set_or_enum_vaulues($strFieldName)




// used only during development
// prints all field-values
function print_field_values()
{

  echo "<br><b>Values:</b><br> ";
  echo "Name: $this->strName <br>";
  echo "Content: $this->strContent<br>";
  echo "Type: $this->strType <br>";
  echo "Maxlen: $this->iMaxLen <br>";
  echo "Table: $this->strTableName<br>";

  // print flags
  echo "Flags: ";
  if ($this->bitFlags & NOT_NULL)
	      echo "not_null ";
  if ($this->bitFlags & PRIMARY_KEY)
	      echo "primary_key ";
  if ($this->bitFlags & AUTO_INCREMENT)
	      echo "auto-increment ";
  if ($this->bitFlags & UNSIGNED)
	      echo "unsigned ";
  if ($this->bitFlags & ZEROFILL)
	      echo "zerofill ";
  if ($this->bitFlags & MULTIPLE_KEY)
	      echo "multiple_key ";
  if ($this->bitFlags & ENUM)
	      echo "enum ";
  if ($this->bitFlags & SET)
	      echo "set ";
  if ($this->bitFlags & BLOB)
	      echo "blob/text ";
  if ($this->bitFlags & BINARY)
	      echo "binary ";

  if ($this->strType == "real")
  {
      echo "<br>Precision: ";
	  print_r($this->aPrecision);
  }

  $c = count($this->aPossibleValues);
  if ($c)
  {
     echo "<br>Possible values: ";
	 echo implode(" ",$this->aPossibleValues);
  }
  echo "<br>Format-Patttern: ".$this->strFormatPattern;
  echo "<br>";
}  // End function print_field_values

}  // End class column







?>
Return current item: autoformBQ