Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
12 / 12
CRAP
100.00% covered (success)
100.00%
31 / 31
Database
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
12 / 12
17
100.00% covered (success)
100.00%
31 / 31
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 get
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
7 / 7
 connectionDriverName
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 driver
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
 buildDriver
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 query
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 values
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 sqlExpression
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 sql
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 configDriverName
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
 buildValues
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 buildSql
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
<?php
namespace PHPixie;
class Database
{
    protected $config;
    protected $values;
    protected $sql;
    protected $document;
    protected $driverClasses = array(
        'pdo'   => '\PHPixie\Database\Driver\PDO',
        'mongo' => '\PHPixie\Database\Driver\Mongo',
    );
    protected $drivers = array();
    protected $connections =  array();
    public function __construct($config)
    {
        $this->config = $config;
    }
    public function get($connectionName = 'default')
    {
        if (!isset($this->connections[$connectionName])) {
            $config = $this->config->slice($connectionName);
            $driverName = $this->configDriverName($config);
            $driver = $this->driver($driverName);
            $this->connections[$connectionName] = $driver->buildConnection($connectionName, $config);
        }
        return $this->connections[$connectionName];
    }
    
    public function connectionDriverName($connectionName)
    {
        $config = $this->config->slice($connectionName);
        return $this->configDriverName($config);
    }
    
    public function driver($name)
    {
        if (!isset($this->drivers[$name])) {
            $this->drivers[$name] = $this->buildDriver($name);
        }
        return $this->drivers[$name];
    }
    public function buildDriver($name)
    {
        $class = $this->driverClasses[$name];
        return new $class($this);
    }
    public function query($type = 'select', $connectionName = 'default')
    {
        return $this->get($connectionName)->query($type);
    }
    public function values()
    {
        if ($this->values === null)
            $this->values = $this->buildValues();
        return $this->values;
    }
    
    public function sqlExpression($sql = '', $params = array())
    {
        return $this->sql()->expression($sql, $params);
    }
    
    public function sql()
    {
        if ($this->sql === null)
            $this->sql = $this->buildSql();
        return $this->sql;
    }
    
    protected function configDriverName($config)
    {
        $driverName = $config->get('driver');
        if(!array_key_exists($driverName, $this->driverClasses)) {
            throw new \PHPixie\Database\Exception\Driver("Driver '$driverName' does not exist");
        }
        
        return $driverName;
    }
    
    protected function buildValues()
    {
        return new \PHPixie\Database\Values();
    }
    protected function buildSql()
    {
        return new \PHPixie\Database\Type\SQL();
    }
    
}