Location: PHPKode > scripts > My One Line SQL > my-one-line-sql/DBQuery.class.php
<?php
/**
 * @package myOneLineSQL
 */
/**
 * Class DBQuery
 * 
 * Class provides methods to define a SQL database query
 * This class does not execute a query
 * 
 * Objects of this class can be passed to instances of RunDBQuery class which will execute query
 * 
 * @package myOneLineSQL
 * @version 1.0
 *
 */
class DBQuery implements Parameterable
{
   /**
    * To store keyword for objects of this class
    *
    * @var string
    */
   private $thisKeyword;
   /**
    * Array of parameters to be processed by the class
    *
    * @var array()
    */
   private $parameters;
   /**
    * To store the query once each parameter has been processed
    *
    * @var string
    */
   private $thisQuery;
   /**
    * Constructor for DBQuery objects
    * 
    * Sets the value of thisKeyword to the value of $keyword passed as argument to
    * the constructor.
    * Initiliazes also instance variable parameters to refer to an empty array
    *
    * $keyword values: 
    *  - SELECT, 
    *  - SELECT_ORDER,
    *  - SELECT_WHERE,
    *  - SELECT_WHERE_ORDER
    *  - SELECT_WHERE_ORDER_LIMIT
    *  - UPDATE,
    *  - INSERT,
    *  - CREATE,
    *  - ALTER,
    *  - DROP_TABLE,
    *  - DELETE_WHERE,
    *  - SHOW,
    *  - OPTIMIZE
    * 
    * @param string $keyword
    */
   function __construct($keyword)
   {
      $this->thisKeyword = $keyword;
      $this->parameters = array ( );
   }
   /**
    * Builds parameted query
    * 
    * Depending on the value of instance variable thisKeyword gets the parameted version of the query
    * 
    * Each parameter value for a query can be set via setParameters method before a call to queryBuilder() method is made
    * 
    * You can easily add parameted query in case you need to, here are listed the most common.
    * 
    * RunDBQuery objects will execute the query and these will contain query results always in the form of an associative array.
    * 
    * Refers to RunDBQuery documentation to find out how to retrieve query results and info
    *  
    * Query Parameters currently set in this method:
    *  - WHAT,
    *  - TABLE_NAME,
    *  - WHERE_EXP,
    *  - WHICH_SET,
    *  - WHICH_VALUES,
    *  - ASC_DESC,
    *  - ORDER_BY,
    *  - TABLE_STRUCTURE,
    *  - ADD_WHAT,
    *  - AFTER_WHAT,
    *  - QUERY_LIMITS
    *
    */
   private function switchParametedQuery()
   {
      switch ( $this->getKeyword ())
      {
         case "SELECT" :
            $this->thisQuery = "SELECT {WHAT} FROM {TABLE_NAME}";
            break;
         case "SELECT_ORDER" :
            $this->thisQuery = "SELECT {WHAT} FROM {TABLE_NAME} ORDER BY {ORDER_BY} {ASC_DESC}";
            break;
         case "SELECT_WHERE" :
            $this->thisQuery = "SELECT {WHAT} FROM {TABLE_NAME} WHERE {WHERE_EXP}";
            break;
         case "SELECT_WHERE_ORDER" :
            $this->thisQuery = "SELECT {WHAT} FROM {TABLE_NAME} WHERE {WHERE_EXP} ORDER BY {ORDER_BY} {ASC_DESC}";
            break;
         case "SELECT_WHERE_ORDER_LIMIT" :
            $this->thisQuery = "SELECT {WHAT} FROM {TABLE_NAME} WHERE {WHERE_EXP} ORDER BY {ORDER_BY} {ASC_DESC} LIMIT {QUERY_LIMITS}";
            break;
         case "UPDATE" :
            $this->thisQuery = "UPDATE {TABLE_NAME} SET {WHICH_SET} WHERE {WHERE_EXP}";
            break;
         case "INSERT" :
            $this->thisQuery = "INSERT INTO {TABLE_NAME} VALUES ({WHICH_VALUES})";
            break;
         case "CREATE" :
            $this->thisQuery = "CREATE TABLE {TABLE_STRUCTURE}";
            break;
         case "ALTER" :
            $this->thisQuery = "ALTER TABLE {TABLE_NAME} ADD {ADD_WHAT} AFTER {AFTER_WHAT}";
            break;
         case "DROP_TABLE" :
            $this->thisQuery = "DROP TABLE {TABLE_NAME}";
            break;
         case "DELETE_WHERE" :
            $this->thisQuery = "DELETE FROM {TABLE_NAME} WHERE {WHERE_EXP}";
            break;
         case "SHOW" :
            $this->thisQuery = "SHOW TABLES";
            break;
         case "OPTIMIZE" :
            $this->thisQuery = "OPTIMIZE TABLE {TABLE_NAME}";
            break;
      }
   }
   /**
    * Processes parameters
    * 
    * Processes the parameters for this object ans sets value of thisQuery
    * Replaces the parameters key with given values.
    *
    * @return string (SQL query)
    */
   public function queryBuilder()
   {
      $this->switchParametedQuery ();
      foreach ( $this->parameters as $key => $value )
      {
         $query_block = '{' . $key . '}';
         $this->thisQuery = str_replace ( $query_block, $value, $this->thisQuery );
      }
      return $this->thisQuery;
   }
   /**
    * To set parameters for this object
    *
    * @param string $variable
    * @param mixed $value
    */
   public function SetParameters($variable, $value)
   {
      
      $this->parameters [$variable] = $value;
   }
   /**
    * Getter method
    * 
    * Returns value of query keyword
    *
    * @return string
    */
   public function getKeyword()
   {
      return $this->thisKeyword;
   }
   /**
    * Getter method
    * 
    * Returns a string representation of DBQuery object
    *
    * @return string
    */
   public function getThisQuery()
   {
      return $this->thisQuery;
   }
}
?>
Return current item: My One Line SQL