Location: PHPKode > scripts > IB Class > ib-class/ibclass.php
<?
// Classes for connecting and interfacing with interbase 6.0
// hide@address.com
// version 0.79
// For the latest version visit: http://www.phpclasses.org/browse.html/package/1002.html 
//
// Please write to me and tell me what you think of my class.
//
// 2003-01-29 14:15 Base class was created
// 2003-02-10 09:30 Better support for parameters
// 2003-02-12 22:45 Added GEN_ID function in query
//                  The script will now end with an error  if the connection is not set in IBQuery
// 2003-02-14 14:00 Count was renamed FldCount
//                  It is now safe to close a closed connection / query
// 2003-02-23 09:30 "Code cleaning"in open and execute
// 2003-02-26 09:30 closes old query before a new one is opened in open and execute
//                  bugfix in FldGetTextLength
//                  Moved GEN_ID and ErrorMsg to TIBConnection
// 2003-02-27 00:00 added FldWD metod to query. if query is not open the default value is returned.
// 2003-02-28 09:45 Bugfix in Query IsOpen
// 2003-03-01 09:45 Comments translated to english
// 2003-03-02 20:00 Added support for / use off "swedish" date and time format
// 2003-03-03 00:00 Bugfix: FldIsEmpty reported true for values of 0
// 2003-03-03 00:10 Bugfix: typeO in Open wrong CharSet parameter used
// 2003-03-03 16:30 Added FldHTML and FldWDHTML methods to IBQuery. Outputs htmlsafe code.
// 2003-03-06 10:00 Better FldIsEmpty function ( I hope :-) )
// 2003-03-16 12:00 Check against multiple include.
//                  Check DB params before connecting
// 2003-03-17 11:00 Serious bugfix: BlobFromFile did not handle binary files... 
// 2003-03-19 08:00 Change so that TIBQuery no longer makes a copy of the TIBCon object
//================================================================================

if( $___IBClassExists___!=TRUE ) // I do not trust require_once for 1 second
{
  $___IBClassExists___==TRUE;
  
  class TIBConnection
  {
    var $IBConnection;
    var $Database;
    var $UserName;
    var $Password;
    var $CharSet;
    //----------------------------------------------------
    function TIBConnection( $sDb, $sUser, $sPass, $bAutoOpen=true, $sCharSet="ISO8859_1" )
    {
      $this->Database = $sDb;
      $this->UserName = $sUser;
      $this->Password = $sPass;
      $this->CharSet  = $sCharSet;

      if( $bAutoOpen )
        $this->Open();
    }
    //----------------------------------------------------  
    function Open()
    {    
      if( $this->Database && $this->UserName && $this->Password && $this->CharSet )
        $this->IBConnection = ibase_connect($this->Database,$this->UserName,$this->Password,$this->CharSet);
      else
        trigger_error ("Connection parameters not set.", E_USER_ERROR);
      
      
      if($this->CharSet=="ISO8859_1") // I think you want swedish date format...
      {
        ibase_timefmt("%Y-%m-%d", IBASE_DATE);
        ibase_timefmt("%H:%M:%S", IBASE_TIME);
        ibase_timefmt("%Y-%m-%d %H:%M:%S", IBASE_TIMESTAMP);
      }
      return $this->IsOpen();
    }
    //----------------------------------------------------
    function IsOpen()
    {
      return ! is_null( $this->IBConnection );
    }
    //----------------------------------------------------
    function Close()
    {
      if( $this->IsOpen() )
      { 
        // when is it right to commit?! this has to change later... @ maybe?
        ibase_commit( $this->IBConnection );
        ibase_close( $this->IBConnection );
        unset( $this->IBConnection );
      }
    }
    //----------------------------------------------------  
    function GEN_ID( $GeneratorName, $cnt=1 )
    {
      $Result = ibase_query( $this->IBConnection, "SELECT GEN_ID( $GeneratorName, $cnt ) from RDB\$DATABASE" );

      if ( $Result==false )
        return false;

      $QryRow = ibase_fetch_row( $Result );

      if( $QryRow === false )
        $retur = false;
      else
        $retur = $QryRow[0];

      ibase_free_result ( $Result );

      return $retur;    
    }
    //----------------------------------------------------  
    function ErrorMsg()
    {
      return ibase_errmsg ();
    }
  }

  //================================================================================

  class TIBQuery
  { 
    var $IBConnection;
    var $QueryString;
    var $Params;

    var $QryResult;
    var $QryRow;

    var $FieldArr;
    var $TableArr;

    var $FldCount;  // number of fields in the result
    //----------------------------------------------------
    function TIBQuery( &$oIBConn, $sQry="", $aParams=array() )
    {
      $this->IBConnection = &$oIBConn;
      $this->QueryString = $sQry;
      $this->Params = $aParams;

      $this->FieldArr = array();
      $this->TableArr = array();
    }
    //----------------------------------------------------
    function Open( $sQuery=NULL, $aParams=NULL )
    {    
      if( ! is_a ( $this->IBConnection, "TIBConnection" ) )    
        trigger_error ("IBConnection variable not set or is not of the right type.", E_USER_ERROR);

      // if open was called with params don´t use class params
      if( is_null( $aParams ) )
        $aParams = $this->Params;

      if( is_null( $sQuery ) )
        $sQuery = $this->QueryString;

      $sQuery = Trim($sQuery);

      if( strncasecmp ( $sQuery, "select", 6) != 0 )
        return false;

      $this->Close(); // close old query if any

      // makes it posible to add varying number of parameters
      $params = "";

      foreach( $aParams as $k => $val )
        $params .= ', $aParams['. $k .']';

      @eval( "\$this->QryResult = ibase_query( \$this->IBConnection->IBConnection, \$sQuery $params);" );

      if ( $this->QryResult === false )
      {
        return false;
      }
      else
      {
        $this->FldCount = ibase_num_fields( $this->QryResult );

        for ( $i=0 ; $i<$this->FldCount ; $i++ )
        {
          $col_info = ibase_field_info( $this->QryResult, $i );

          $this->FieldArr[$i] = $col_info["alias"];
          $this->TableArr[$i] = $col_info["relation"];
        }

        return true;
      }
    }
    //----------------------------------------------------
    function Execute( $sQuery=NULL, $aParams=NULL )
    {        
      if( ! is_a ( $this->IBConnection, "TIBConnection" ) )    
        trigger_error ("IBConnection variable not set or is not of the right type.", E_USER_ERROR);

      // if open was called with params don´t use class params
      if( is_null( $aParams ) )
        $aParams = $this->Params;

      if( is_null( $sQuery ) )
        $sQuery = $this->QueryString;

      $sQuery = Trim($sQuery);

      if( $sQuery == "" || strncasecmp ( $sQuery, "select", 6) == 0 )
        return false;

      $this->Close(); // Stäng ev föregående fråga å rensa arrayerna

      // makes it posible to add varying number of parameters
      $params = "";

      foreach( $aParams as $k => $val )
        $params .= ', $aParams['. $k .']';

      @eval( "\$Result = ibase_query( \$this->IBConnection->IBConnection, \$sQuery $params);" );

      return $Result;
    }
    //----------------------------------------------------
    function IsOpen()
    {
      return $this->QryResult==true;
    }
    //----------------------------------------------------
    function Close()
    {
      $this->FieldArr = array();
      $this->TableArr = array();
      $this->FldCount    = 0;

      if( $this->IsOpen() )    
        ibase_free_result ( $this->QryResult );

      unset ($this->QryResult);
      unset ($this->QryRow);
    }
    //----------------------------------------------------
    function Next() // returns true / false
    {
      if( $this->QryResult==false )
        return false;

      $this->QryRow = ibase_fetch_row( $this->QryResult );
      return $this->QryRow != false;
    }
    //----------------------------------------------------
    function Fld( $sFldName, $sTable=NULL ) 
    {
      $idx = $this->FldId( $sFldName, $sTable );

      if( is_null( $idx ) )
        return NULL;

      $col_info = ibase_field_info( $this->QryResult, $idx );
      if($col_info['type']=="BLOB")
      {
        $retur = NULL;

        if( ! empty($this->QryRow[$idx]) )
        {
          $blob_data = ibase_blob_info( $this->QryRow[$idx] );
          $blob_hndl = ibase_blob_open( $this->QryRow[$idx] );

          $retur = ibase_blob_get( $blob_hndl, $blob_data[0] );

          ibase_blob_close ( $blob_hndl );  
        }
        return $retur;
      }
      return $this->QryRow[$idx];
    }
    //----------------------------------------------------
    function FldHTML( $sFldName, $sTable=NULL )  // HTML encoded output
    {
      return htmlentities( $this->Fld( $sFldName, $sTable ) );
    }
    //----------------------------------------------------
    // Field with default value. if the query is closed then use default.
    function FldWD( $sFldName, $Default=NULL, $sTable=NULL ) 
    {    
      if( $this->IsOpen() )
        return  $this->Fld( $sFldName, $sTable ) ;

      return $Default;
    }
    //----------------------------------------------------  
    function FldWDHTML( $sFldName, $Default=NULL, $sTable=NULL ) // HTML encoded output
    {
      return htmlentities( $this->FldWD( $sFldName, $sTable ) );
    }
    //----------------------------------------------------  
    function FldRaw( $sFldName, $sTable=NULL ) 
    {
      $idx = $this->FldId( $sFldName, $sTable );

      if( is_null( $idx ) )
        return NULL;

      return $this->QryRow[$idx];
    }
    //----------------------------------------------------
    function FldIsEmpty( $sFldName, $sTable=NULL ) 
    {
      $idx = $this->FldId( $sFldName, $sTable );

      if( is_null( $idx ) )
        return NULL;

      return (is_null($this->QryRow[$idx]) || $this->FldGetTextLength($idx)==0);
    }
    //----------------------------------------------------
    function FldId( $sFldName, $sTable=NULL )
    {
      if( ! $this->IsOpen() )
        trigger_error ("Fieldaccess while query is not open!", E_USER_ERROR);

      if( is_int( $sFldName ) )
      {
        // Parameter is an index, us it!
        return $sFldName;
      }
      else
      {
        for ($i=0; $i < $this->FldCount; $i++)
        {
          if( !strcasecmp( $sFldName, $this->FieldArr[$i] ) )
          {
            if( is_null( $sTable ) || !strcasecmp( $sTable, $this->TableArr[$i] ) )
              return $i;
          }
        }
      }

      return NULL;
    }
    //----------------------------------------------------
    function FldRealName( $sFldName, $sTable=NULL )
    {
      $idx = $this->FldId( $sFldName, $sTable );
      if( is_null( $idx ) )
        return NULL;

      $col_info = ibase_field_info( $this->QryResult, $idx );
      return $col_info["name"];
    }
    //----------------------------------------------------
    function FldAlias( $sFldName, $sTable=NULL ) 
    {
      $idx = $this->FldId( $sFldName, $sTable );
      if( is_null( $idx ) )
        return NULL;

      $col_info = ibase_field_info( $this->QryResult, $idx );
      return $col_info["alias"];
    }
    //----------------------------------------------------
    function FldTable( $sFldName, $sTable=NULL ) 
    {
      $idx = $this->FldId( $sFldName, $sTable );
      if( is_null( $idx ) )
        return NULL;

      $col_info = ibase_field_info( $this->QryResult, $idx );
      return $col_info["relation"];
    }
    //----------------------------------------------------
    function FldType( $sFldName, $sTable=NULL ) 
    {
      $idx = $this->FldId( $sFldName, $sTable );
      if( is_null( $idx ) )
        return NULL;

      $col_info = ibase_field_info( $this->QryResult, $idx );
      return $col_info["type"];
    }
    //----------------------------------------------------
    function FldLength( $sFldName, $sTable=NULL )  
    {
      $idx = $this->FldId( $sFldName, $sTable );
      if( is_null( $idx ) )
        return NULL;

      $col_info = ibase_field_info( $this->QryResult, $idx );
      return $col_info["length"];
    }
    //----------------------------------------------------
    function FldGetTextLength( $sFldName, $sTable=NULL )  
    {
      $idx = $this->FldId( $sFldName, $sTable );
      if( is_null( $idx ) )
        return NULL;

      $col_info = ibase_field_info( $this->QryResult, $idx );
      if($col_info['type']=="BLOB")
      {
        if( ! empty($this->QryRow[$idx]) )
        {
          $blob_data = ibase_blob_info( $this->QryRow[$idx] );
          return $blob_data[0];
        }
        return 0;
      }
      return strlen( $this->QryRow[$idx] );
    }
    //----------------------------------------------------
    function BlobFromString( $String )
    {      
      if( ! is_a ( $this->IBConnection, "TIBConnection" ) )    
        trigger_error ("IBConnection variable not set or is not of the right type.", E_USER_ERROR);

      $blob_id = ibase_blob_create( $this->IBConnection->IBConnection ); 

      ibase_blob_add( $blob_id, $String );

      return (string)ibase_blob_close($blob_id); 
    }
    //----------------------------------------------------  
    function BlobFromFile( $FileName )
    {
      if( ! is_a ( $this->IBConnection, "TIBConnection" ) )    
        trigger_error ("IBConnection variable not set or is not of the right type.", E_USER_ERROR);

      if ( !file_exists( $FileName ) ) 
        return NULL;

      $fid = fopen( $FileName, "rb" );

      if( $fid==false )
        return NULL;

      $retur = ibase_blob_import( $this->IBConnection->IBConnection, $fid );
      fclose( $fid );
      return $retur;
    }
  }

}
//================================================================================
?>
Return current item: IB Class