Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
Merge pull request #55 from atk4/feature/test-custom-connection
Browse files Browse the repository at this point in the history
Add implementation for Connection class
  • Loading branch information
DarkSide666 committed Apr 13, 2016
2 parents 2d3dca4 + f69346c commit 2604629
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 25 deletions.
79 changes: 79 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace atk4\dsql;

class Connection {
protected $query_class = 'atk4\dsql\Query';
protected $expression_class = 'atk4\dsql\Expression';
protected $connection = null;

static function connect($dsn, $user=null, $password=null){

list($driver,$rest)=explode(':',$dsn,2);

switch(strtolower($driver)){
case 'mysql':
return new Connection([
'connection'=>new \PDO($dsn, $user, $password),
'query_class'=>'atk4\dsql\Query_MySQL'
]);
case 'sqlite':
return new Connection([
'connection'=>new \PDO($dsn, $user, $password),
'query_class'=>'atk4\dsql\Query_SQLite'
]);
case 'dumper':
return new Connection_Dumper([
'connection'=>Connection::connect($rest)
]);

}
}

/**
* Specifying $attributes to constructors will override default
* attribute values of this class.
*
* @param array $attributes
*/
public function __construct($attributes = null)
{
if ($attributes) foreach ($attributes as $key => $val) {
$this->$key = $val;
}
}

public function dsql($properties = [])
{
$c = $this->query_class;
$q = new $c($properties);
$q->connection = $this->connection ?: $this;

return $q;
}

public function expr($properties = [], $arguments = null)
{
$c = $this->expression_class;
$e = new $c($properties, $arguments);
$e->connection = $this->connection ?: $this;

return $e;
}

function connection()
{
return $this->connection;
}

function execute(Expression $expr)
{

// If custom connection is set, execute again using that
if ($this->connection && $this->connection !== $this) {
return $expr->execute($this->connection);
}

throw new Exception('Queries cannot be executed through this connection');
}
}
45 changes: 45 additions & 0 deletions src/Connection_Dumper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php // vim:ts=4:sw=4:et:fdm=marker

namespace atk4\dsql;

/**
*
*/
class Connection_Dumper extends Connection
{
protected $callback = null;

function connection()
{
return $this->connection->connection();
}
public function dsql($properties = [])
{
$dsql = $this->connection->dsql($properties);
$dsql->connection = $this;
return $dsql;
}

public function expr($properties = [], $arguments = null)
{
$expr = $this->connection->expr($properties, $arguments);
$expr->connection = $this;
return $expr;
}

public function execute(Expression $expr) {

$this->start_time = time() + microtime();
$ret = $this->connection->execute($expr);
$took = time() + microtime() - $this->start_time;

if ($this->callback) {
$c = $this->callback;
$c($expr, $took);
} else {
printf("[%02.6f] %s\n", $took, strip_tags($expr->getDebugQuery()));
}

return $ret;
}
}
1 change: 1 addition & 0 deletions src/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ public function execute($connection = null)

// If it's a PDO connection, we're cool
if ($connection instanceof \PDO) {
$connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
// We support PDO
$query = $this->render();
$statement = $connection->prepare($query);
Expand Down
6 changes: 2 additions & 4 deletions src/Query/MySQL.php → src/Query_MySQL.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php // vim:ts=4:sw=4:et:fdm=marker

namespace atk4\dsql\Query;

use atk4\dsql\Query;
namespace atk4\dsql;

/**
* Perform query operation on MySQL server
*/
class MySQL extends Query
class Query_MySQL extends Query
{
}
6 changes: 2 additions & 4 deletions src/Query/SQLite.php → src/Query_SQLite.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<?php // vim:ts=4:sw=4:et:fdm=marker

namespace atk4\dsql\Query;

use atk4\dsql\Query;
namespace atk4\dsql;

/**
* Perform query operation on SQLite server
*/
class SQLite extends Query
class Query_SQLite extends Query
{
/**
* SQLite specific template overwrites
Expand Down
18 changes: 18 additions & 0 deletions tests/db/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace atk4\dsql\tests;

use atk4\dsql\Query;
use atk4\dsql\Expression;
use atk4\dsql\Connection;

/**
* @ coversDefaultClass \atk4\dsql\Query
*/
class dbConnectionTest extends \PHPUnit_Framework_TestCase
{
function testSQLite() {

$c = Connection::connect($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']);
return (string)$c->expr("SELECT date('now')")->getOne();
}
}
24 changes: 7 additions & 17 deletions tests/db/SelectTest.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<?php
namespace atk4\dsql\tests;

use atk4\dsql\Expression;
use atk4\dsql\Query;
use atk4\dsql\Query\MySQL as Query_MySQL;
use atk4\dsql\Query\SQLite as Query_SQLite;
use atk4\dsql\Expression;
use atk4\dsql\Connection;

class dbSelectTest extends \PHPUnit_Extensions_Database_TestCase
{
protected $pdo;

public function __construct()
{
$this->pdo = new \PDO($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']);
$this->c = Connection::connect($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']);
$this->pdo = $this->c->connection();

$this->pdo->query('CREATE TEMPORARY TABLE employee (id int not null, name text, surname text, retired bool, PRIMARY KEY (id))');
}
/**
Expand All @@ -33,18 +34,7 @@ protected function getDataSet()

private function q($table = null, $alias = null)
{
// decide which DB engine to use
$engine = strtolower(explode(':', $GLOBALS['DB_DSN'])[0]);
switch ($engine) {
case 'sqlite':
$q = new Query_SQLite(['connection'=>$this->pdo]);
break;
case 'mysql':
$q = new Query_MySQL(['connection'=>$this->pdo]);
break;
default:
$q = new Query(['connection'=>$this->pdo]);
}
$q = $this->c->dsql();

// add table to query if specified
if ($table !== null) {
Expand All @@ -54,7 +44,7 @@ private function q($table = null, $alias = null)
}
private function e($template = null, $args = null)
{
return $this->q()->expr($template, $args);
return $this->c->expr($template, $args);
}


Expand Down

0 comments on commit 2604629

Please sign in to comment.