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

Commit

Permalink
Feature/add more connector tests (#56)
Browse files Browse the repository at this point in the history
* Added support for Proxy connections

* fix some bugs

* testing nasty bugs in php

* breaking build

* fixing build

* finished implementation of Counter

* PSR fixes
  • Loading branch information
romaninsh authored and DarkSide666 committed Apr 13, 2016
1 parent 542a3ec commit 4a7c8fb
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 47 deletions.
35 changes: 24 additions & 11 deletions src/Connection.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php // vim:ts=4:sw=4:et:fdm=marker

namespace atk4\dsql;

Expand All @@ -8,16 +8,17 @@
* @license MIT
* @copyright Agile Toolkit (c) http://agiletoolkit.org/
*/
class Connection {
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);
public static function connect($dsn, $user = null, $password = null)
{
list($driver, $rest) = explode(':', $dsn, 2);

switch(strtolower($driver)){
switch (strtolower($driver)) {
case 'mysql':
return new Connection([
'connection'=>new \PDO($dsn, $user, $password),
Expand All @@ -33,6 +34,17 @@ static function connect($dsn, $user=null, $password=null){
'connection'=>Connection::connect($rest)
]);

case 'counter':
return new Connection_Counter([
'connection'=>Connection::connect($rest)
]);

// let PDO handle the rest
default:
return new Connection([
'connection'=>new \PDO($dsn, $user, $password)
]);

}
}

Expand All @@ -44,8 +56,10 @@ static function connect($dsn, $user=null, $password=null){
*/
public function __construct($attributes = null)
{
if ($attributes) foreach ($attributes as $key => $val) {
$this->$key = $val;
if ($attributes) {
foreach ($attributes as $key => $val) {
$this->$key = $val;
}
}
}

Expand All @@ -67,14 +81,13 @@ public function expr($properties = [], $arguments = null)
return $e;
}

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

function execute(Expression $expr)
public function execute(Expression $expr)
{

// If custom connection is set, execute again using that
if ($this->connection && $this->connection !== $this) {
return $expr->execute($this->connection);
Expand Down
67 changes: 67 additions & 0 deletions src/Connection_Counter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php // vim:ts=4:sw=4:et:fdm=marker

namespace atk4\dsql;

/**
* @license MIT
* @copyright Agile Toolkit (c) http://agiletoolkit.org/
*/
class Connection_Counter extends Connection_Proxy
{
protected $callback = null;

protected $selects = 0;
protected $queries = 0;
protected $expressions = 0;

protected $rows = 0;

public function iterate($ret)
{
foreach ($ret as $key => $row) {
$this->rows++;
yield $key => $row;
}
}

public function execute(Expression $expr)
{
if ($expr instanceof Query) {
$this->queries++;
if ($expr->mode === 'select' || $expr->mode === null) {
$this->selects++;
}
} else {
$this->expressions++;
}

$ret = parent::execute($expr);


return $this->iterate($ret);
}
public function __destruct()
{
if ($this->callback) {
$c = $this->callback;
$c($this->selects, $this->rows, $this->queries, $this->expressions);
} else {
printf(
"Queries: %3d, Selects: %3d, Rows fetched: %4d, Expressions %3d\n",
$this->queries,
$this->selects,
$this->rows,
$this->expressions
);
}



return;

$took = time() + microtime() - $this->start_time;


return $ret;
}
}
26 changes: 5 additions & 21 deletions src/Connection_Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,18 @@
namespace atk4\dsql;

/**
*
* @license MIT
* @copyright Agile Toolkit (c) http://agiletoolkit.org/
*/
class Connection_Dumper extends Connection
class Connection_Dumper extends Connection_Proxy
{
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)
public function execute(Expression $expr)
{
$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);
$ret = parent::execute($expr);
$took = time() + microtime() - $this->start_time;

if ($this->callback) {
Expand Down
33 changes: 33 additions & 0 deletions src/Connection_Proxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php // vim:ts=4:sw=4:et:fdm=marker

namespace atk4\dsql;

/**
* @license MIT
* @copyright Agile Toolkit (c) http://agiletoolkit.org/
*/
class Connection_Proxy extends Connection
{
public 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)
{
return $this->connection->execute($expr);
}
}
21 changes: 17 additions & 4 deletions src/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,10 @@ public function __toString()
}
return $value;
} catch (\Exception $e) {
$previousHandler = set_exception_handler(function (){});
$previousHandler = set_exception_handler(function () {
});
restore_error_handler();
if ($previousHandler !== null ) {
if (is_callable($previousHandler)) {
call_user_func($previousHandler, $e);
}
die($e->getMessage());
Expand Down Expand Up @@ -449,7 +450,13 @@ public function getIterator()
*/
public function get()
{
return $this->execute()->fetchAll();
$stmt = $this->execute();

if ($stmt instanceof \Generator) {
return iterator_to_array($stmt);
}

return $stmt->fetchAll();
}

/**
Expand All @@ -471,7 +478,13 @@ public function getOne()
*/
public function getRow()
{
return $this->execute()->fetch();
$stmt = $this->execute();

if ($stmt instanceof \Generator) {
return $stmt->current();
}

return $stmt->fetch();
}
// }}}
}
7 changes: 6 additions & 1 deletion src/Expressionable.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php
<?php // vim:ts=4:sw=4:et:fdm=marker

namespace atk4\dsql;

/**
* @license MIT
* @copyright Agile Toolkit (c) http://agiletoolkit.org/
*/
interface Expressionable
{
public function getDSQLExpression();
Expand Down
6 changes: 4 additions & 2 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ public function group($group)
}

if (is_array($group)) {
foreach($group as $g){
foreach ($group as $g) {
$this->args['group'][] = $g;
}
return $this;
Expand All @@ -790,7 +790,9 @@ protected function _render_group()
return '';
}

$g = implode(', ', array_map(function($a){return $this->_consume($a, 'escape');}, $this->args['group']));
$g = implode(', ', array_map(function ($a) {
return $this->_consume($a, 'escape');
}, $this->args['group']));

return ' group by '.$g;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Query_MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

/**
* Perform query operation on MySQL server
*
* @license MIT
* @copyright Agile Toolkit (c) http://agiletoolkit.org/
*/
class Query_MySQL extends Query
{
Expand Down
3 changes: 3 additions & 0 deletions src/Query_SQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

/**
* Perform query operation on SQLite server
*
* @license MIT
* @copyright Agile Toolkit (c) http://agiletoolkit.org/
*/
class Query_SQLite extends Query
{
Expand Down
4 changes: 2 additions & 2 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -809,11 +809,11 @@ public function testJoin()
);
$this->assertEquals(
'left join `address` as `a` on `a`.`id` = `u`.`address_id`',
$this->q('[join]')->table('user','u')->join('address a')->render()
$this->q('[join]')->table('user', 'u')->join('address a')->render()
);
$this->assertEquals(
'left join `address` as `a` on `a`.`user_id` = `u`.`id`',
$this->q('[join]')->table('user','u')->join('address.user_id a')->render()
$this->q('[join]')->table('user', 'u')->join('address.user_id a')->render()
);
}

Expand Down
26 changes: 24 additions & 2 deletions tests/db/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,31 @@
*/
class dbConnectionTest extends \PHPUnit_Framework_TestCase
{
function testSQLite() {

public function testSQLite()
{
$c = Connection::connect($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']);
return (string)$c->expr("SELECT date('now')")->getOne();
}

public function testGenerator()
{
$c = new HelloWorldConnection();
$test = 0;
foreach ($c->expr("abrakadabra") as $row) {
$test++;
}
$this->assertEquals(10, $test);
}
}

// @codingStandardsIgnoreStart
class HelloWorldConnection extends Connection
{
public function execute(Expression $e)
{
for ($x=0; $x<10; $x++) {
yield $x => ['greeting'=>'Hello World'];
}
}
// @codingStandardsIgnoreEnd
}
8 changes: 4 additions & 4 deletions tests/db/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function testOtherQueries()
->insert();
$this->assertEquals(
[['id'=>1, 'name'=>'John'], ['id'=>2, 'name'=>'Jane']],
$this->q('employee')->field('id,name')->select()->fetchAll()
$this->q('employee')->field('id,name')->get()
);

// update
Expand All @@ -162,7 +162,7 @@ public function testOtherQueries()
->update();
$this->assertEquals(
[['id'=>1, 'name'=>'Johnny'], ['id'=>2, 'name'=>'Jane']],
$this->q('employee')->field('id,name')->select()->fetchAll()
$this->q('employee')->field('id,name')->get()
);

// replace
Expand All @@ -175,7 +175,7 @@ public function testOtherQueries()
// So order of records after REPLACE in SQLite will be [Jane, Peter] not [Peter, Jane] as in MySQL,
// which in theory does the same thing, but returns [Peter, Jane] - in original order.
// That's why we add usort here.
$data = $this->q('employee')->field('id,name')->select()->fetchAll();
$data = $this->q('employee')->field('id,name')->get();
usort($data, function ($a, $b) {
return $a['id'] - $b['id'];
});
Expand All @@ -190,7 +190,7 @@ public function testOtherQueries()
->delete();
$this->assertEquals(
[['id'=>2, 'name'=>'Jane']],
$this->q('employee')->field('id,name')->select()->fetchAll()
$this->q('employee')->field('id,name')->get()
);
}
}

0 comments on commit 4a7c8fb

Please sign in to comment.