Lançado Adianti Framework 7.6!
Clique aqui para saber mais
menu

Adianti Solutions

API

Adianti, Framework, PHP, MVC, Active record, Front controller, IDE, RAD, Web, multiplataforma, geração de código, desenvolvimento rápido, relatórios, formulários, listagens, datagrids, gráficos, banco de dados, padrões de projeto, design patterns API do Adianti Framework.
API Docs
code
Selecione a classe

Source for file TSqlSelect.php

Documentation is available at TSqlSelect.php

  1. <?php
  2. namespace Adianti\Database;
  3.  
  4. use Adianti\Database\TSqlStatement;
  5. use Adianti\Database\TTransaction;
  6.  
  7. use PDO;
  8.  
  9. /**
  10.  * Provides an Interface to create SELECT statements
  11.  *
  12.  * @version    7.4
  13.  * @package    database
  14.  * @author     Pablo Dall'Oglio
  15.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  16.  * @license    http://www.adianti.com.br/framework-license
  17.  */
  18. class TSqlSelect extends TSqlStatement
  19. {
  20.     private $columns;   // array with the column names to be returned
  21.     
  22.     /**
  23.      * Add a column name to be returned
  24.      * @param $column   A string containing a column name
  25.      */
  26.     public function addColumn($column)
  27.     {
  28.         // add the column name to the array
  29.         $this->columns[$column;
  30.     }
  31.     
  32.     /**
  33.      * Returns the SELECT statement as an string according to the database driver
  34.      * @param $prepared Return a prepared Statement
  35.      */
  36.     public function getInstruction$prepared FALSE)
  37.     {
  38.         $conn TTransaction::get();
  39.         $driver $conn->getAttribute(PDO::ATTR_DRIVER_NAME);
  40.  
  41.         if ($this->criteria)
  42.         {
  43.             $dbInfo TTransaction::getDatabaseInfo();
  44.             if(isset($dbInfo['case']AND $dbInfo['case'== 'insensitive')
  45.             {
  46.                 $this->criteria->setCaseInsensitive(TRUE);
  47.             }
  48.         }
  49.  
  50.         if (in_array($driverarray('mssql''dblib''sqlsrv')))
  51.         {
  52.             return $this->getSqlServerInstruction$prepared );
  53.         }
  54.         else if (in_array($driverarray('oci''oci8')))
  55.         {
  56.             return $this->getOracleInstruction$prepared );
  57.         }
  58.         else if (in_array($driverarray('firebird')))
  59.         {
  60.             return $this->getInterbaseInstruction$prepared );
  61.         }
  62.         else
  63.         {
  64.             return $this->getStandardInstruction$prepared );
  65.         }
  66.     }
  67.     
  68.     /**
  69.      * Returns the SELECT statement as an string for standard open source drivers
  70.      * @param $prepared Return a prepared Statement
  71.      */
  72.     public function getStandardInstruction$prepared )
  73.     {
  74.         // creates the SELECT instruction
  75.         $this->sql  = 'SELECT ';
  76.         // concatenate the column names
  77.         $this->sql .= implode(','$this->columns);
  78.         // concatenate the entity name
  79.         $this->sql .= ' FROM ' $this->entity;
  80.         
  81.         // concatenate the criteria (WHERE)
  82.         if ($this->criteria)
  83.         {
  84.             $expression $this->criteria->dump$prepared );
  85.             if ($expression)
  86.             {
  87.                 $this->sql .= ' WHERE ' $expression;
  88.             }
  89.             
  90.             // get the criteria properties
  91.             $order     $this->criteria->getProperty('order');
  92.             $group     $this->criteria->getProperty('group');
  93.             $limit     = (int) $this->criteria->getProperty('limit');
  94.             $offset    = (int) $this->criteria->getProperty('offset');
  95.             $direction in_array($this->criteria->getProperty('direction')array('asc''desc')) $this->criteria->getProperty('direction''';
  96.             
  97.             if ($group)
  98.             {
  99.                 $this->sql .= ' GROUP BY ' $group;
  100.             }
  101.             if ($order)
  102.             {
  103.                 $this->sql .= ' ORDER BY ' $order ' ' $direction;
  104.             }
  105.             if ($limit)
  106.             {
  107.                 $this->sql .= ' LIMIT ' $limit;
  108.             }
  109.             if ($offset)
  110.             {
  111.                 $this->sql .= ' OFFSET ' $offset;
  112.             }
  113.         }
  114.         // return the SQL statement
  115.         return $this->sql;
  116.     }
  117.     
  118.     /**
  119.      * Returns the SELECT statement as an string for standard open source drivers
  120.      * @param $prepared Return a prepared Statement
  121.      */
  122.     public function getInterbaseInstruction$prepared )
  123.     {
  124.         // creates the SELECT instruction
  125.         $this->sql  = 'SELECT ';
  126.         
  127.         if ($this->criteria)
  128.         {
  129.             $limit     = (int) $this->criteria->getProperty('limit');
  130.             $offset    = (int) $this->criteria->getProperty('offset');
  131.             
  132.             if ($limit)
  133.             {
  134.                 $this->sql .= ' FIRST ' $limit;
  135.             }
  136.             if ($offset)
  137.             {
  138.                 $this->sql .= ' SKIP ' $offset;
  139.             }
  140.         }
  141.         
  142.         // concatenate the column names
  143.         $this->sql .= implode(','$this->columns);
  144.         
  145.         // concatenate the entity name
  146.         $this->sql .= ' FROM ' $this->entity;
  147.         
  148.         // concatenate the criteria (WHERE)
  149.         if ($this->criteria)
  150.         {
  151.             $expression $this->criteria->dump$prepared );
  152.             if ($expression)
  153.             {
  154.                 $this->sql .= ' WHERE ' $expression;
  155.             }
  156.             
  157.             // get the criteria properties
  158.             $group     $this->criteria->getProperty('group');
  159.             $order     $this->criteria->getProperty('order');
  160.             $direction in_array($this->criteria->getProperty('direction')array('asc''desc')) $this->criteria->getProperty('direction''';
  161.             
  162.             if ($group)
  163.             {
  164.                 $this->sql .= ' GROUP BY ' $group;
  165.             }
  166.             
  167.             if ($order)
  168.             {
  169.                 $this->sql .= ' ORDER BY ' $order ' ' $direction;
  170.             }
  171.         }
  172.         
  173.         // return the SQL statement
  174.         return $this->sql;
  175.     }
  176.     
  177.     /**
  178.      * Returns the SELECT statement as an string for mssql/dblib drivers
  179.      * @param $prepared Return a prepared Statement
  180.      */
  181.     public function getSqlServerInstruction$prepared )
  182.     {
  183.         // obtém a cláusula WHERE do objeto criteria.
  184.         if ($this->criteria)
  185.         {
  186.             $expression $this->criteria->dump$prepared );
  187.             
  188.             // obtém as propriedades do critério
  189.             $group    $this->criteria->getProperty('group');
  190.             $order    $this->criteria->getProperty('order');
  191.             $limit    = (int) $this->criteria->getProperty('limit');
  192.             $offset   = (int) $this->criteria->getProperty('offset');
  193.             $directionin_array($this->criteria->getProperty('direction')array('asc''desc')) $this->criteria->getProperty('direction''';
  194.         }
  195.         $columns implode(','$this->columns);
  196.         
  197.         if ((isset($limitOR isset($offset)) AND ($limit>OR $offset>0))
  198.         {
  199.             if (empty($order))
  200.             {
  201.                 $order '(SELECT NULL)';
  202.             }
  203.             $this->sql = "SELECT {$columns} FROM ( SELECT ROW_NUMBER() OVER (order by {$order} {$direction}) AS __ROWNUMBER__, {$columns} FROM {$this->entity}";
  204.             
  205.             if (!empty($expression))
  206.             {
  207.                 $this->sql.= "    WHERE {$expression} ";
  208.             }
  209.             $this->sql .= " ) AS TAB2";
  210.             if ((isset($limitOR isset($offset)) AND ($limit>OR $offset>0))
  211.             {
  212.                 $this->sql .= " WHERE";
  213.             }
  214.             
  215.             if ($limit >0 )
  216.             {
  217.                 $total = $offset + $limit;
  218.                 $this->sql .= " __ROWNUMBER__ <= {$total} ";
  219.                 
  220.                 if ($offset)
  221.                 {
  222.                     $this->sql .= " AND ";
  223.                 }
  224.             }
  225.             if ($offset > 0)
  226.             {
  227.                 $this->sql .= " __ROWNUMBER__ > {$offset} ";
  228.             }
  229.         }
  230.         else
  231.         {
  232.             $this->sql  = 'SELECT ';
  233.             $this->sql .= $columns;
  234.             $this->sql .= ' FROM ' $this->entity;
  235.             if (!empty($expression))
  236.             {
  237.                 $this->sql .= ' WHERE ' $expression;
  238.             }
  239.             
  240.             if (isset($group) AND !empty($group))
  241.             {
  242.                 $this->sql .= ' GROUP BY ' $group;
  243.             }
  244.             if (isset($order) AND !empty($order))
  245.             {
  246.                 $this->sql .= ' ORDER BY ' $order ' ' $direction;
  247.             }
  248.         }
  249.         return $this->sql;
  250.     }
  251.     
  252.     /**
  253.      * Returns the SELECT statement as an string for oci8 drivers
  254.      * @param $prepared Return a prepared Statement
  255.      */
  256.     public function getOracleInstruction( $prepared )
  257.     {
  258.         // obtém a cláusula WHERE do objeto criteria.
  259.         if ($this->criteria)
  260.         {
  261.             $expression = $this->criteria->dump$prepared );
  262.             
  263.             // obtém as propriedades do critério
  264.             $group    $this->criteria->getProperty('group');
  265.             $order    $this->criteria->getProperty('order');
  266.             $limit    = (int) $this->criteria->getProperty('limit');
  267.             $offset   = (int) $this->criteria->getProperty('offset');
  268.             $directionin_array($this->criteria->getProperty('direction')array('asc''desc')) $this->criteria->getProperty('direction''';
  269.         }
  270.         $columns = implode(',', $this->columns);
  271.         
  272.         $basicsql  'SELECT ';
  273.         $basicsql .= $columns;
  274.         $basicsql .= ' FROM ' $this->entity;
  275.         
  276.         if (!empty($expression))
  277.         {
  278.             $basicsql .= ' WHERE ' . $expression;
  279.         }
  280.         
  281.         if (isset($group) AND !empty($group))
  282.         {
  283.             $basicsql .= ' GROUP BY ' . $group;
  284.         }
  285.         if (isset($order) AND !empty($order))
  286.         {
  287.             $basicsql .= ' ORDER BY ' . $order . ' ' . $direction;
  288.         }
  289.         
  290.         if ((isset($limit) OR isset($offset)) AND ($limit>0 OR $offset>0))
  291.         {
  292.             $this->sql = "SELECT {$columns} ";
  293.             $this->sql.= "  FROM (";
  294.             $this->sql.= "       SELECT rownum \"__ROWNUMBER__\", A.{$columns} FROM ({$basicsql}) A";
  295.             
  296.             if ($limit >0 )
  297.             {
  298.                 $total = $offset + $limit;
  299.                 $this->sql .= " WHERE rownum <= {$total} ";
  300.             }
  301.             $this->sql.= ")";
  302.             if ($offset 0)
  303.             {
  304.                 $this->sql .= " WHERE \"__ROWNUMBER__\" > {$offset} ";
  305.             }
  306.         }
  307.         else
  308.         {
  309.             $this->sql = $basicsql;
  310.         }
  311.         
  312.         return $this->sql;
  313.     }