Created
October 24, 2011 08:48
-
-
Save augustohp/1308618 to your computer and use it in GitHub Desktop.
A silly database interaction implementation
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
<?php | |
function examples() { | |
$say = new Sql(); | |
echo $say->what->happens->when->you->think->out->of->the->box->raw('?'),"\n\n"; | |
// Our database | |
$pdo = new Pdo('sqlite::memory:'); | |
//$pdo = new Pdo('mysql:dbname=test;host=127.0.0.1', 'zend', ''); | |
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
// Faking we are god, and creating our world | |
$lucky_number = rand(14,42); | |
$god = new Sql($pdo); | |
$god->raw(" | |
DROP TABLE IF EXISTS `names`; | |
CREATE TABLE `names` ( | |
`id` int(11) NOT NULL, | |
`name` varchar(255) DEFAULT NULL, | |
`status` char(1) NOT NULL DEFAULT 'A', | |
PRIMARY KEY (`id`) | |
); | |
INSERT INTO `names` (`id`, `name`, `status`) VALUES (1,'Where is he now? {$lucky_number}','A'); | |
INSERT INTO `names` (`id`, `name`, `status`) VALUES (2,'Papa','A'); | |
INSERT INTO `names` (`id`, `name`, `status`) VALUES (3,'Batman','A'); | |
")->execute(); | |
// Lets test the world now ... | |
$sql = new Sql($pdo); | |
$sql->select->all | |
->from->names | |
->where->id->equals->raw('1') | |
->and->status->equals->string('A') | |
->order->by->id->desc->limit->raw('1'); | |
echo $sql,"\n"; | |
var_dump($sql->fetch()); | |
echo "\n"; | |
// Let's test it in another way | |
$sql = new Sql($pdo); | |
echo $sql->raw('select * from names where id = 2'),"\n"; | |
var_dump($sql->fetch()); | |
} | |
// ----------------------------------------------------------------------------- | |
// Mapper to the SQL class pussies that need to become men | |
$map = array( | |
'all' => '*', | |
'equals' => '=' | |
); | |
/** | |
* Class that does the SQL thing .... | |
*/ | |
class Sql { | |
public $name; | |
public $c; | |
public function __construct($db='') { $this->c = $db; } | |
public function __get($name) { $this->name .= $name.' '; return $this; } | |
public function fetch(array $args = array()) { | |
$ps = $this->c->prepare($this->__toString()); | |
$ps->execute($args); | |
return $ps->fetchAll(PDO::FETCH_ASSOC); | |
} | |
public function execute() { return $this->c->exec($this->__toString()); } | |
/** | |
* Redefines the name in a very fun way. | |
* | |
* @param string $d The decorator to use. | |
* @param string $v The value to decorate | |
* @return Map | |
*/ | |
public function __call($d, $v) { | |
switch($d) { | |
case 'raw': | |
$this->name = $this.implode(' ', $v).' '; | |
break; | |
case 'string': | |
$this->name = $this.'"'.implode(' ', $v).'" '; | |
break; | |
} | |
return $this; | |
} | |
public function __toString() { | |
global $map; | |
$nap = array_flip($map); | |
$search = $replace = array(); | |
foreach ($nap as $kill=>$bill) { | |
$search[] = $bill; | |
$replace[] = $kill; | |
} | |
$this->name = str_replace($search, $replace, $this->name); | |
return $this->name; | |
} | |
} | |
examples(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment