My First Statement

Well I have managed to create a simple SELECT statement. It is cumbersome at best and follows along the process flow introduced in the last post, “Select Statement“. I show this only to demonstrate the foundation of how the statement is ultimately put together. You will notice that each class is initiated using a “create” static method instead of explicitly calling the __construct magic method. The “create” method returns the initiated class that can be further manipulated using a method chaining functionality. The chaining is not used in the following example, however, this is where is will make my next improvements to reduce necessary code. There is very little content validation being done at this point on data being passed to the statement constructors. Of course, because each component is represented by different classes, I can easily add validation once the basic code is completed.

$objSelect = SQL::newQuery('SELECT');
$objTableReference = sqlTableReference::create('table');
$objSelect->addTable($objTableReference);
$objColumnDefinition = sqlColumnDefinition::create('column');
$objColumnDefinition->setTable($objTableReference);
$objSelectExpression = sqlSelectExpression::create($objColumnDefinition);
$objSelect->addSelect($objSelectExpression);
$objConditionGroup = sqlConditionGroup::create('AND');
$objWhereCondition = sqlWhereCondition::create($objColumnDefinition);
$objWhereCondition->setOperator('=');
$objWhereCondition->setValue('value');
$objConditionGroup->addCondition($objWhereCondition);
$objSelect->addWhere($objConditionGroup);

echo $objSelect->__toString();
// SELECT table.column FROM table WHERE (table.column = value)

Leave a Reply