-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests now live in those own direcory, todo added, introduced new Db c…
…lasses
- Loading branch information
Showing
12 changed files
with
206 additions
and
55 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,10 @@ | ||
<? | ||
/** | ||
* @author Kirill "Nemoden" K | ||
* $ Date: Fri 23 Mar 2012 04:36:40 PM VLAT $ | ||
*/ | ||
|
||
class DbCriteria { | ||
public function __construct() { | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
/** | ||
* @author Kirill "Nemoden" K | ||
* $ Date: Fri 23 Mar 2012 10:56:32 AM VLAT $ | ||
*/ | ||
|
||
class DbExpression { | ||
private $_expression; | ||
public function __construct($expression) { | ||
$this->_expression = $expression; | ||
} | ||
public function __toString() { | ||
return (string)$this->_expression; | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
require_once 'DbExpression.php'; | ||
class DbQueryBuilder { | ||
|
||
private $_schema; | ||
private $_table; | ||
private $_bind_params = array(); | ||
|
||
public function setSchema($schema) { | ||
$this->_schema = sprintf('`%s`', (string)$schema); | ||
return $this; | ||
} | ||
|
||
public function setTable($table) { | ||
$this->_table = sprintf('`%s`', (string)$table); | ||
return $this; | ||
} | ||
|
||
public function insert($params) { | ||
$template = 'INSERT INTO %s (%s) VALUES (%s)'; | ||
$dest = implode('.', array($this->_schema, $this->_table)); | ||
$keys = $values = array(); | ||
foreach ($params as $key => $param) { | ||
$keys[] = sprintf('`%s`', $key); | ||
if ($param instanceof DbExpression) { | ||
$values[] = $param; | ||
} | ||
else { | ||
$placeholder = sprintf(':%s', $key); | ||
$values[] = sprintf(':%s', $key); | ||
$this->_bind_params[$placeholder] = $param; | ||
} | ||
} | ||
return sprintf($template, $dest, implode(', ', $keys), implode(', ', $values)); | ||
} | ||
|
||
public function update() { | ||
} | ||
|
||
public function getBindParams() { | ||
return $this->_bind_params; | ||
} | ||
} |
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
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 @@ | ||
Pick out all the Db* classes from here - they should remain as an independant project |
This file was deleted.
Oops, something went wrong.
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 @@ | ||
<? | ||
|
||
require_once 'PHPUnit/Autoload.php'; | ||
require_once '../DbExpression.php'; | ||
require_once '../DbQueryBuilder.php'; | ||
|
||
class DbQueryBuilderTest extends PHPUnit_Framework_TestCase { | ||
|
||
private $queryBuilder; | ||
|
||
/** | ||
* each time create new DbQueryBuilder | ||
*/ | ||
public function setUp() { | ||
$this->queryBuilder = new DbQueryBuilder(); | ||
} | ||
|
||
public function testCreateInsertQuery() { | ||
$params = array( | ||
'name' => 'Kirill', | ||
'date' => new DbExpression('NOW()'), | ||
'value' => 10, | ||
); | ||
$this->queryBuilder = new DbQueryBuilder(); | ||
$query = $this->queryBuilder->setSchema('test')->setTable('test')->insert($params); | ||
$this->assertEquals('INSERT INTO `test`.`test` (`name`, `date`, `value`) VALUES (:name, NOW(), :value)', $query); | ||
$this->assertEquals(array(':name' => 'Kirill', ':value' => '10'), $this->queryBuilder->getBindParams()); | ||
} | ||
|
||
public function testUpdateQuery() { | ||
$criteria = new DbCriteria(); | ||
$criteria->conditions = array('id = :id'); | ||
$query = $this->queryBuilder->update($update_fields, $criteria); | ||
$this->markTestIncomplete(); | ||
} | ||
|
||
public function testSelectQuery() { | ||
$this->markTestIncomplete(); | ||
} | ||
|
||
public function testJoinQuery() { | ||
$this->markTestIncomplete(); | ||
} | ||
|
||
} |
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
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 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> |
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,37 @@ | ||
-- | ||
-- @author Kirill "Nemoden" K | ||
-- schema for tests (MySQL) | ||
-- | ||
|
||
create database if not exists `test` default character set = utf8 default collate = utf8_general_ci; | ||
|
||
use `test`; | ||
|
||
create table if not exists `people` ( | ||
`id` int(11) unsigned not null auto_increment, | ||
`name` varchar(255) not null default '', | ||
`address` text, | ||
`birthday` datetime not null default '0000-00-00 00:00:00', -- test dates < 1970 | ||
`created_at` datetime not null default '0000-00-00 00:00:00', -- test dates | ||
`updated_at` timstamp not null, | ||
`age` tinyint(4) not null, -- test field setter | ||
`gender` set('M', 'F') not null, -- test set | ||
PRIMARY KEY `id` (`id`), | ||
KEY `birthday` (`birthday`), | ||
KEY `created_at` (`created_at`), | ||
KEY `gender` (`gender`), | ||
KEY `gender_age` (`gender`, `age`) | ||
) ENGINE=InnoDB CHARSET=utf8 COMMENT='People table for test cases'; | ||
|
||
create table if not exists `users` ( | ||
`id` int(11) unsigned not null auto_increment, | ||
`name` varchar(64) not null default '', | ||
`email` varchar(64) not null default '', | ||
`password` varchar(64) not null default '', -- test field setter | ||
`created_at` datetime not null default '0000-00-00 00:00:00', | ||
`updated_at` timestamp not null, | ||
`email_confirmation_code` varchar(64) not null default '', -- test field setter | ||
PRIMARY KEY `id` (`id`), | ||
KEY `email_confirmation_code` (`email_confirmation_code`), | ||
KEY `created_at` (`created_at`), | ||
KEY `updated_at` (`updated_at`)) ENGINE=InnoDB CHARSET=utf8 COMMENT='Users'; |