This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from atk4/feature/test-custom-connection
Add implementation for Connection class
- Loading branch information
Showing
7 changed files
with
154 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters