<?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 SQLReactorConnection{
public $type;
public $user;
public $password;
public $host;
public $database;
public $resource;
public $engine;
public $logger;
final public function __construct( $connString ){
$this->parseConnectionString( $connString );
$this->createEngine();
$this->logger = new SQLReactorLogger();
$this->open();
}
protected function createEngine(){
require_once "utils/SQLReactorLogger.php";
require_once "interfaces/iSQLReactorEngine.php";
require_once "engines/default/SQLReactorEngine.php";
require_once "engines/{$this->type}/SQLReactorEngine.php";
require_once "interfaces/iSQLReactorCol.php";
require_once "engines/default/SQLReactorTable.php";
require_once "engines/default/SQLReactorCol.php";
require_once "engines/default/SQLReactorQuery.php";
require_once "engines/default/SQLReactorClause.php";
$columnTypes = array( "IntCol", "FloatCol", "StringCol", "DatetimeCol", "DateCol", "TimeCol", "BoolCol", "Backref", "ForeignKey", "SerialCol" );
foreach( $columnTypes as $columnType ){
require_once "engines/default/columns/{$columnType}.php";
require_once "engines/{$this->type}/columns/{$columnType}.php";
}
$engineName = "SQLReactorEngine_{$this->type}";
$this->engine = new $engineName( $this );
}
protected function parseConnectionString( $connString ){
$connString = preg_replace( "/^sqlite:\/\//", "sqlite://no_url/", $connString );
$uriInfo = parse_url( $connString );
$this->type = $uriInfo[ 'scheme' ];
$this->user = $uriInfo[ 'user' ];
$this->password = $uriInfo[ 'pass' ];
$this->host = $uriInfo[ 'host' ];
$this->database = preg_replace( "/^\//", "", $uriInfo[ 'path' ] );
}
public function __call( $method, $arguments ){
return call_user_func_array( array( $this->engine, $method ), $arguments );
}
public function query( $query ){
$result = $this->engine->query( $query );
$this->logger->debug( $query );
if( $php_errormsg ){
$this->logger->error( $php_errormsg );
}
return $result;
}
}
?>