Location: PHPKode > scripts > SQLReactor > SQLReactor-0.6/SQLReactor/engines/oracle/SQLReactorEngine.php
<?php
/*******************************************************************************
* Copyright 2008 Rafael Marques Martins
*
* This file is part of SQLReactor.
* 
* SQLReactor is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* 
* SQLReactor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with SQLReactor; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
* 
*******************************************************************************/

class SQLReactorEngine_oracle extends SQLReactorEngine implements iSQLReactorEngine{
	public function open(){
        @$this->connection->resource = oci_connect( $this->connection->user, $this->connection->password, $this->connection->host );
        $error = oci_error();
        
        if( $error ){
            throw new Exception( $error[ 'message' ] );
        }
        
		return $this->connection->resource;
	}
	public function close(){
        return oci_close ( $this->connection->resource );
	}
    
    public function addLimitAndOffset( $sql = "", $limit = null, $offset = null, &$query ){
        $sql2 = "SELECT * FROM ( SELECT \"_A_N_O_N_2_\".*, ROW_NUMBER() OVER (ORDER BY 0) AS \"_SQL_REACTOR_ROW_\" FROM (\r\n";
        $sql2 .= $sql;
        $sql2 .= "\r\n) \"_A_N_O_N_2_\" ) \"_A_N_O_N_3_\"";
        
        if( !is_null( $offset ) ){
            $start = $offset;
        }else{
            $start = 0;
        }
        $sql2 .= " WHERE \"_A_N_O_N_3_\".\"_SQL_REACTOR_ROW_\" > {$start}";
        
        if( !is_null( $limit ) ){
            $end = $start + $limit;
            $sql2 .= " AND \"_A_N_O_N_3_\".\"_SQL_REACTOR_ROW_\" <= {$end}";
        }
        
        return $sql2;
    }
	
	public function query( $query ){
        //var_dump( $query );
        $statement = oci_parse( $this->connection->resource, $query );
        @oci_execute( $statement );
        
        $error = oci_error( $statement );
		if( $error ){
			throw new Exception( $error[ 'message' ] );
		}
        
		return $statement;
	}
	public function fetch( $statement ){
		return @oci_fetch_array( $statement, OCI_ASSOC + OCI_RETURN_NULLS + OCI_RETURN_LOBS );
	}
    
    public function beginTransaction(){
	}
	
	public function commit(){
		oci_commit( $this->connection->resource );
	}
	
	public function rollback(){
		oci_rollback( $this->connection->resource );
	}
    
	public function insert( &$table ){
		try{
			if( $table->class->_primaryKey == array( 'id' ) ){
				$result = $table->connection->query( "SELECT \"{$table->columns->id->sequence}\".nextval AS \"id\" FROM DUAL" );
				$rs = $table->connection->fetch( $result );
				
				
				$table->class->setId( $rs[ 'id' ] );
			}
            
			$table->connection->query( $table->getInsertSQL() );
			
		}catch( Exception $exception ){
			if( $table->class->_primaryKey == array( 'id' ) ){
				$table->class->setId( null );
			}
			throw $exception;
		}
	}
    
	public function update( &$table ){
		$sql = $table->getUpdateSQL();
		if( is_null( $sql ) ) return;
        
		$this->connection->query( $sql );
	}
}
?>
Return current item: SQLReactor