Location: PHPKode > scripts > SQLReactor > SQLReactor-0.6/SQLReactor/engines/default/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{
	public $connection;
	public $class;
	public $escapeName = '"';

	final public function __construct( &$connection ){
		$this->connection = $connection;
	}
	
	public function fetchAll( $result ){
		$lines = array();
		while( $line = $this->fetch( $result ) ){
			$lines[] = $line;
		}
		return $lines;
	}
	
	public function beginTransaction(){
		$this->query( "START TRANSACTION" );
	}
	
	public function commit(){
		$this->query( "COMMIT" );
	}
	
	public function rollback(){
		$this->query( "ROLLBACK" );
	}

    public function addLimitAndOffset( $sql = "", $limit = null, $offset = null, &$query ){
        $sql .= "\n\t";
        
        if( !is_null( $limit ) ){
            $sql .= "LIMIT {$limit} ";
        }
        if( !is_null( $offset ) ){
            $sql .= "OFFSET {$offset} ";
        }
        
        return $sql;
    }
	
	public function insert( &$table ){
		try{
			if( $table->class->_primaryKey == array( 'id' ) ){
				$result = $table->connection->query( "SELECT nextval( '{$table->columns->id->sequence}' ) AS \"id\"" );
				$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 );
	}
    
	public function delete( &$table ){
		$sql = $table->getDeleteSQL();
		if( is_null( $sql ) ) return;
        
		$this->connection->query( $sql );
	}
}
?>
Return current item: SQLReactor