<?php
/**
 * YANA library
 *
 * @package yana
 * @licence http://creativecommons.org/licenses/by-nc-sa/2.5/
 *
 * @ignore
 */

/**
 * @ignore
 */
@include_once("DB/QueryTool.php");

/**
 * QueryBuilder
 *
 * not used yet
 *
 * @access public
 * @package yana
 *
 * @ignore
 */
class QueryBuilder extends DB_QueryTool
{

    /**#@+
     * @ignore
     * @access  private
     */
    var $db = NULL;
    var $_column = '';
    var $_key = '';
    /**#@-*/

    function QueryBuilder(/* dbServer */ $dbServer = NULL)
    {
        if (is_null($dbServer)) {
            $dbServer = new dbServer();
        }
        $this->db = $dbServer->get();
    }

    function insert($data)
    {
        $this->add($data);
    }

    function setKey($key)
    {
        assert('is_string($key) && !empty($key);');
        settype($key, "string");
        $key = strtolower($key);
        $keyArray = explode(".", $key);
        if (count($keyArray)>0) {
            $this->setTable($keyArray[0]);
        }
        if (count($keyArray)>1 && $keyArray[1] != '*' && !empty($keyArray[1])) {
            $this->setIndex($keyArray[1]);
            $this->_getComlumn = false;
        } else {
            $this->_getComlumn = true;
        }
        if (count($keyArray)>2) {
            $this->_column = $keyArray[2];
        }
    }
    
    function setWhere($where = NULL)
    {
        if (is_array($where)){
            $out = "";
            foreach ($where as $col => $value) {
                if (!empty($out)) {
                    $out .= ' AND ';
                }
                if (!is_string($value) || strpos($value,'*') === false) {
                    $out .= $this->db->quoteIdentifier($col).'='.$this->db->quoteSmart($value);
                } else {
                    $out .= $this->db->quoteIdentifier($col).' LIKE '.$this->db->quoteSmart(str_replace('*', '%', $value));
                }
            }
            parent::setWhere($out);
        } else {
            parent::setWhere($where);
        }
    }

    function transaction($selector)
    {
        $selector = strtolower($selector);
        
        $sqlStmt = "";
        switch ($selector) {
            case 'start':
              switch (YANA_DATABASE_DBMS) {
                  case 'mssql':
                      $sqlStmt = 'BEGIN TRANSACTION;';
                  break;
                  default:
                      $sqlStmt = 'START TRANSACTION;';
                  break;
              }
            break;
            case 'rollback':
                $sqlStmt = 'ROLLBACK;';
            break;
            case 'commit':
                $sqlStmt = 'COMMIT;';
            break;
            default:
            break;
        }
        return $sqlStmt;
    }

}

?>