<?php

require_once 'orm/OrmSql.php';

/**
 * Select SQL class.
 * 
 * This class holds a 'select' Sql query.
 * 
 * @author  $Author: pruna $
 * @package orm/sql
 * @version $Revision: 1.3 $
 * @created 02-Apr-2005
 * @modified $Date: 2005/12/06 02:56:45 $
 */
class OrmSqlSelect extends OrmSql {
    
    /**
     * Constructor that defines basic properties for this Bean.
     * 
     * It follows Collection rules for constructor.
     * Basic elements are: 
     * <ul>
     * <li>select: array of fields (array)</li>
     * <li>join: array of joins with conditions (array)</li>
     * <li>where: array of conditions (array)</li>
     * <li>groupBy: array of group by clauses (array)</li>
     * <li>orderBy: array of order by clauses (array)</li>
     * <li>limit: array of limit values (array)</li>
     * </ul>
     * Default values may serve as guide for preferred data types.
     * For a full description about this elements, see @docs
     * 
     * @access  public
     * @param   ICollection    $collection    Initial elements
     */
    public function __construct($collection = null) {
        parent::__construct(array(
            'select' => array(),
            'join' => array(),
            'where' => array(),
            'groupBy' => array(),
            'orderBy' => array(),
            'limit' => array(),
        ));
        $this->setAll($collection);
    }
    
    /**
     * Returns a SQL query string representation of this object.
     *
     * @access    public
     * @return    string
     */
    public function __toString() {
        return 'SELECT ' . implode(', ', $this->getSelect()) 
           . ' FROM ' . implode(', ', $this->getTable()) 
           . ($this->getJoin() ? ' ' 
           . implode(' ', $this->getJoin()) : '') 
           . ($this->getWhere() ? ' WHERE ' 
           . implode(' AND ', $this->getWhere()) : '')
           . ($this->getGroupBy() ? ' GROUP BY ' 
           . implode(', ', $this->getGroupBy()) : '')
           . ($this->getOrderBy() ? ' ORDER BY ' 
           . implode(', ', $this->getOrderBy()) : '')
           . ($this->getLimit() ? ' LIMIT ' 
           . implode(', ', $this->getLimit()) : '');
    }
    
}

?>