Skip to content

Commit

Permalink
SqlBuilder changes and beginning of comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
KrisJordan committed Nov 10, 2008
1 parent ee638e7 commit 691f68d
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 76 deletions.
41 changes: 0 additions & 41 deletions lib/recess/sources/db/sql/Criterion.class.php

This file was deleted.

32 changes: 0 additions & 32 deletions lib/recess/sources/db/sql/Join.class.php

This file was deleted.

96 changes: 93 additions & 3 deletions lib/recess/sources/db/sql/SqlBuilder.class.php
@@ -1,16 +1,39 @@
<?php

Library::import('recess.sources.db.sql.ISqlConditions');
Library::import('recess.sources.db.sql.ISqlSelectOptions');
Library::import('recess.sources.db.sql.Criterion');
Library::import('recess.sources.db.sql.Join');

/**
* SqlBuilder is used to incrementally compose named-parameter PDO Sql strings
* using a simple, chainable method call API. This is a naive wrapper that does
* not gaurantee valid SQL output (i.e. column names using reserved SQL words).
*
* 4 classes of SQL strings can be built: INSERT, UPDATE, DELETE, SELECT.
* This class is intentionally arranged from the low complexity requirements
* of INSERT to the more complex SELECT.
*
* INSERT: table, column/value assignments
* UPDATE/DELETE: where conditions
* SELECT: order, joins, offset, limit, distinct
*
* Example usage:
*
* $sqlBuilder->into('table_name')->assign('column', 'value')->insert() ..
* returns "INSERT INTO table_name (column) VALUES (:column)"
* $sqlBuilder->getPdoArguments() returns array( ':column' => 'value' )
*
* @author Kris Jordan
*/
class SqlBuilder implements ISqlConditions, ISqlSelectOptions {

/* INSERT */
protected $table;
protected $assignments = array();

/**
*
*
* @return string INSERT string.
*/
public function insert() {
$this->insertSanityCheck();

Expand Down Expand Up @@ -306,4 +329,71 @@ protected function rangeHelper() {
}
}

class Criterion {
public $column;
public $value;
public $operator;

const GREATER_THAN = '>';
const GREATER_THAN_EQUAL_TO = '>=';

const LESS_THAN = '<';
const LESS_THAN_EQUAL_TO = '<=';

const EQUAL_TO = '==';
const NOT_EQUAL_TO = '!=';

const LIKE = 'LIKE';

const COLON = ':';

const ASSIGNMENT = '=';
const ASSIGNMENT_PREFIX = 'assgn_';

const UNDERSCORE = '_';

public function __construct($column, $value, $operator){
$this->column = $column;
$this->value = $value;
$this->operator = $operator;
}

public function getQueryParameter() {
if($this->operator == self::ASSIGNMENT) {
return self::COLON . str_replace(Library::dotSeparator, self::UNDERSCORE, self::ASSIGNMENT_PREFIX . $this->column);
} else {
return self::COLON . str_replace(Library::dotSeparator, self::UNDERSCORE, $this->column);
}
}
}

class Join {
const NATURAL = 'NATURAL';

const LEFT = 'LEFT';
const RIGHT = 'RIGHT';
const FULL = 'FULL';

const INNER = 'INNER';
const OUTER = 'OUTER';
const CROSS = 'CROSS';

public $natural = '';
public $leftRightOrFull = '';
public $innerOuterOrCross = 'OUTER';

public $table;
public $tablePrimaryKey;
public $fromTableForeignKey;

public function __construct($leftRightOrFull, $innerOuterOrCross, $table, $tablePrimaryKey, $fromTableForeignKey, $natural = ''){
$this->natural = $natural;
$this->leftRightOrFull = $leftRightOrFull;
$this->innerOuterOrCross = $innerOuterOrCross;
$this->table = $table;
$this->tablePrimaryKey = $tablePrimaryKey;
$this->fromTableForeignKey = $fromTableForeignKey;
}
}

?>
40 changes: 40 additions & 0 deletions public/StdClass.php
@@ -0,0 +1,40 @@
<?php

class DynamicPropsTake1 {
protected $properties;
function get ($property) { return $this->properties[$property]; }
function set ($property, $value) { $this->properties[$property] = $value; }
}

$take1 = new DynamicPropsTake1();
$take1->set('key','value');
echo 'Take 1: ' . $take1->get('key') . '<br />';
// isset($take1->get('notset')) Fatal Error
// foreach($take1 as $property => value) Returns empty


class DynamicPropsTake2 {
protected $properties;
function __get($property) { return $this->properties[$property]; }
function __set($property, $value) { $this->properties[$property] = $value; }
function __isset($property) { return isset($this->properties[$property]); }
function __unset($property) { unset($this->properties[$property]); }
}
$take2 = new DynamicPropsTake2();
$take2->key = 'value';
echo 'Take 2: ' . $take2->key . '<br />';
if(isset($take2->notset)) echo 'Take 2 isset failed.<br />';
else echo 'Take 2 isset passed.<br />';
unset($take2->key); // Silent Fail
echo 'Take 2: ' . $take2->key . '<br />';
foreach($take2 as $prop => $value) { // Silent fail, returns empty
echo $prop . ':' . $value . '<br />';
}

class DynamicPropsTake3 extends StdClass { }

$dynamicProperties = new DynamicPropsTake3;
$dynamicProperties->name = 'Flexible';
echo $dynamicProperties->name;

?>

0 comments on commit 691f68d

Please sign in to comment.