ja havde lige overset den der med or så det skulle have været or_expr eller orExpr.
Min hovedformål var mest at jeg synes du skulle undgå at man skulle skrive ting som det her: new Criteria\EqualExpr
Efter en lille test tror jeg at det er en dårlig ide at or gav en ny gruppe. I stedet kunne man have or_begin or_end til at starte og slutte blokke af or'ed udtryk.
Eksemplet, er godt klar over du sikkert ville undgå at skriv MySQL, men et eller andet stedet på et eller andet tidspunkt skal der vel gives hvad slags datalager der bruges.
$mysql = new MySQL();
$users = $mysql->get_collection('user');
$users->select()->add('id')->add('user', 'name')->count('count')->sum('earning', 'earnings');
$users->order_by()->desc('id')->asc('name')->random();
$users->group_by()->add('id');
$users->criteria()->equal_expr('name', 'kaj')->or_begin()->less_expr('age', 10)->greater_expr('age', 10)->or_end()->equal_expr('phone',5575757);
$users->page_size(10);
$users->page_number(2);
$users->execute();
Den resulterende MySQL:
SELECT `id`, `user` AS name, COUNT(1) AS count, SUM(`earning`) AS earnings FROM `user` WHERE `name` = kaj AND (`age` < 10 OR `age` > 10) AND `phone` = 5575757 GROUP BY `id` ORDER BY `id` DESC, `name` ASC, RAND() LIMIT 20,10
Og logikken:
<?php
class Select{
private $_fields = array();
public function add($name, $alias = ""){
$this->_fields[] = array(1, $name, $alias);
return $this;
}
public function count($alias){
$this->_fields[] = array(2, $alias);
return $this;
}
public function sum($name, $alias){
$this->_fields[] = array(3,$name, $alias);
return $this;
}
public function fields(){
return $this->_fields;
}
}
class OrderBy{
private $_fields = array();
public function asc($name){
$this->_fields[] = array($name, 1);
return $this;
}
public function desc($name){
$this->_fields[] = array($name, 2);
return $this;
}
public function random(){
$this->_fields[] = array('', 3);
return $this;
}
public function fields(){
return $this->_fields;
}
}
class GroupBy{
private $_fields = array();
public function add($name){
$this->_fields[] = $name;
return $this;
}
public function fields(){
return $this->_fields;
}
}
class Criteria{
private $_fields = array();
public function or_begin(){
$this->_fields[] = array(1);
return $this;
}
public function or_end(){
$this->_fields[] = array(2);
return $this;
}
public function and_begin(){
$this->_fields[] = array(3);
return $this;
}
public function and_end(){
$this->_fields[] = array(4);
return $this;
}
public function equal_expr($field, $value){
$this->_fields[] = array(0, 0, $field, $value);
return $this;
}
public function less_expr($field, $value){
$this->_fields[] = array(0, 1, $field, $value);
return $this;
}
public function greater_expr($field, $value){
$this->_fields[] = array(0, 2, $field, $value);
return $this;
}
public function fields(){
return $this->_fields;
}
}
class Collection{
private $_resource;
private $_handler;
private $_select;
private $_order_by;
private $_group_by;
private $_criteria;
private $_page_number = 0;
private $_page_size = 0;
public function __construct($resource, $handler){
$this->_resource = $resource;
$this->_handler = $handler;
$this->_select = new Select();
$this->_order_by = new OrderBy();
$this->_group_by = new GroupBy();
$this->_criteria = new Criteria();
}
public function resource(){
return $this->_resource;
}
public function select(){
return $this->_select;
}
public function execute(){
$this->_handler->execute($this);
}
public function page_number($number = null){
if($number != null){
$this->_page_number = $number;
}else{
return $this->_page_number;
}
}
public function page_size($size = null){
if($size != null){
$this->_page_size = $size;
}else{
return $this->_page_size;
}
}
public function criteria(){
return $this->_criteria;
}
public function order_by(){
return $this->_order_by;
}
public function group_by(){
return $this->_group_by;
}
}
class ResourceHandler{
public function get_collection($resource){
return new Collection($resource, $this);
}
}
class MySQL extends ResourceHandler{
public function execute(Collection $collection){
$str = 'SELECT ';
if(count($collection->select()->fields()) == 0){
$str .= '*';
}else{
$sep = '';
foreach($collection->select()->fields() as $bindings){
$str .= $sep;
switch($bindings[0]){
case 1:
$str .= '`'. $bindings[1] .'`';
if($bindings[2] != ''){
$str .=' AS '. $bindings[2];
}
break;
case 2:
$str .= 'COUNT(1)';
if($bindings[1] != ''){
$str .=' AS '. $bindings[1];
}
break;
case 3:
$str .= 'SUM(`'. $bindings[1] .'`)';
if($bindings[2] != ''){
$str .=' AS '. $bindings[2];
}
break;
}
$sep =", ";
}
}
$str .= ' FROM `'.$collection->resource().'`';
if(count($collection->criteria()->fields()) > 0){
$str .= ' WHERE ';
$type = array(' AND ');
$sep = '';
foreach($collection->criteria()->fields() as $bindings){
switch($bindings[0]){
case 0:
$str.= $sep.'`'.$bindings[2].'`';
switch($bindings[1]){
case 0: $str.= ' = ';break;
case 1: $str.= ' < ';break;
case 2: $str.= ' > ';break;
}
$str.= $bindings[3];
$sep = $type[count($type) - 1];
break;
case 1:
$str .= $sep . '(';
$sep = '';
$type[] =' OR ';
break;
case 3:
$str .= $sep . '(';
$sep = '';
$type[] =' AND ';
break;
case 2:
case 4:
$str .= ')';
array_pop($type);
if(count($type) >0){
$sep = $type[count($type) - 1];
}
break;
}
}
}
if(count($collection->group_by()->fields()) > 0){
$str .= ' GROUP BY ';
$sep = '';
foreach($collection->group_by()->fields() as $bindings){
$str .= $sep.'`'. $bindings .'`';
$sep =", ";
}
}
if(count($collection->order_by()->fields()) > 0){
$str .= ' ORDER BY ';
$sep = '';
foreach($collection->order_by()->fields() as $bindings){
if($bindings[1] == 3){
$str .= $sep.' RAND()';
}else{
$str .= $sep.'`'. $bindings[0] .'` '.(($bindings[1]==1)?'ASC':'DESC');
}
$sep =", ";
}
}
$size = $collection->page_size();
if($size != 0){
$number = $collection->page_number();
if($number == 0){
$str .= ' LIMIT '.$size;
}else{
$str .= ' LIMIT '.($number*$size).','.$size;
}
}
print $str;
}
}
Indlæg senest redigeret d. 02.06.2011 19:57 af Bruger #5620