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 TFilter.php

Documentation is available at TFilter.php

  1. <?php
  2. namespace Adianti\Database;
  3.  
  4. use Adianti\Database\TExpression;
  5. use Adianti\Database\TSqlStatement;
  6.  
  7. /**
  8.  * Provides an interface to define filters to be used inside a criteria
  9.  *
  10.  * @version    7.4
  11.  * @package    database
  12.  * @author     Pablo Dall'Oglio
  13.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  14.  * @license    http://www.adianti.com.br/framework-license
  15.  */
  16. class TFilter extends TExpression
  17. {
  18.     private $variable;
  19.     private $operator;
  20.     private $value;
  21.     private $value2;
  22.     private $preparedVars;
  23.     private $caseInsensitive;
  24.  
  25.     /**
  26.      * Class Constructor
  27.      * @param  $variable = variable
  28.      * @param  $operator = operator (>, <, =, BETWEEN)
  29.      * @param  $value    = value to be compared
  30.      * @param  $value2   = second value to be compared (between)
  31.      */
  32.     public function __construct($variable$operator$value$value2 NULL)
  33.     {
  34.         // store the properties
  35.         $this->variable $variable;
  36.         $this->operator $operator;
  37.         $this->preparedVars array();
  38.         
  39.         // transform the value according to its type
  40.         $this->value    $value;
  41.         
  42.         if ($value2)
  43.         {
  44.             $this->value2 $value2;
  45.         }
  46.         $this->caseInsensitive FALSE;
  47.     }
  48.     
  49.     /**
  50.      * Transform the value according to its PHP type
  51.      * before send it to the database
  52.      * @param $value    Value to be transformed
  53.      * @param $prepared If the value will be prepared
  54.      * @return       Transformed Value
  55.      */
  56.     private function transform($value$prepared FALSE)
  57.     {
  58.         // if the value is an array
  59.         if (is_array($value))
  60.         {
  61.             $foo array();
  62.             // iterate the array
  63.             foreach ($value as $x)
  64.             {
  65.                 // if the value is an integer
  66.                 if (is_numeric($x))
  67.                 {
  68.                     if ($prepared)
  69.                     {
  70.                         $preparedVar ':par_'.$this->getRandomParameter();
  71.                         $this->preparedVars$preparedVar $x;
  72.                         $foo[$preparedVar;
  73.                     }
  74.                     else
  75.                     {
  76.                         $foo[$x;
  77.                     }
  78.                 }
  79.                 else if (is_string($x))
  80.                 {
  81.                     // if the value is an string, add quotes
  82.                     if ($prepared)
  83.                     {
  84.                         $preparedVar ':par_'.$this->getRandomParameter();
  85.                         $this->preparedVars$preparedVar $x;
  86.                         $foo[$preparedVar;
  87.                     }
  88.                     else
  89.                     {
  90.                         $foo["'$x'";
  91.                     }
  92.                 }
  93.                 else if (is_bool($x))
  94.                 {
  95.                     $foo[($x'TRUE' 'FALSE';
  96.                 }
  97.             }
  98.             // convert the array into a string, splitted by ","
  99.             $result '(' implode(','$foo')';
  100.         }
  101.         // if the value is a subselect (must not be escaped as string)
  102.         else if (substr(strtoupper(string) $value),0,7== '(SELECT')
  103.         {
  104.             $value  str_replace(['#''--''/*']['''''']$value);
  105.             $result $value;
  106.         }
  107.         // if the value must not be escaped (NOESC in front)
  108.         else if (substr(string) $value,0,6== 'NOESC:')
  109.         {
  110.             $value  str_replace(['#''--''/*']['''''']$value);
  111.             $result substr($value,6);
  112.         }
  113.         // if the value is a string
  114.         else if (is_string($value))
  115.         {
  116.             if ($prepared)
  117.             {
  118.                 $preparedVar ':par_'.$this->getRandomParameter();
  119.                 $this->preparedVars$preparedVar $value;
  120.                 $result $preparedVar;
  121.             }
  122.             else
  123.             {
  124.                 // add quotes
  125.                 $result "'$value'";
  126.             }
  127.         }
  128.         // if the value is NULL
  129.         else if (is_null($value))
  130.         {
  131.             // the result is 'NULL'
  132.             $result 'NULL';
  133.         }
  134.         // if the value is a boolean
  135.         else if (is_bool($value))
  136.         {
  137.             // the result is 'TRUE' of 'FALSE'
  138.             $result $value 'TRUE' 'FALSE';
  139.         }
  140.         // if the value is a TSqlStatement object
  141.         else if ($value instanceof TSqlStatement)
  142.         {
  143.             // the result is the return of the getInstruction()
  144.             $result '(' $value->getInstruction(')';
  145.         }
  146.         else
  147.         {
  148.             if ($prepared)
  149.             {
  150.                 $preparedVar ':par_'.$this->getRandomParameter();
  151.                 $this->preparedVars$preparedVar $value;
  152.                 $result $preparedVar;
  153.             }
  154.             else
  155.             {
  156.                 $result $value;
  157.             }
  158.         }
  159.         
  160.         // returns the result
  161.         return $result;
  162.     }
  163.     
  164.     /**
  165.      * Return the prepared vars
  166.      */
  167.     public function getPreparedVars()
  168.     {
  169.         return $this->preparedVars;
  170.     }
  171.     
  172.     /**
  173.      * Return the filter as a string expression
  174.      * @return  string containing the filter
  175.      */
  176.     public function dump$prepared FALSE )
  177.     {
  178.         $this->preparedVars array();
  179.         $value $this->transform($this->value$prepared);
  180.         if ($this->value2)
  181.         {
  182.             $value2 $this->transform($this->value2$prepared);
  183.             // concatenated the expression
  184.             return "{$this->variable{$this->operator{$value} AND {$value2}";
  185.         }
  186.         else
  187.         {
  188.             $variable = $this->variable;
  189.             $operator $this->operator;
  190.  
  191.             if ($this->caseInsensitive && stristr(strtolower($operator),'like'!== FALSE)
  192.             {
  193.                 $variable = "UPPER({$variable})";
  194.                 $value = "UPPER({$value})";
  195.                 $operator = 'like';
  196.             }
  197.  
  198.             // concatenated the expression
  199.             return "{$variable} {$operator} {$value}";
  200.         }
  201.     }
  202.     
  203.     /**
  204.      * Returns a random parameter
  205.      */
  206.     private function getRandomParameter()
  207.     {
  208.         return mt_rand(1000000000, 1999999999);
  209.     }
  210.  
  211.     /**
  212.      * Force case insensitive searches
  213.      */
  214.     public function setCaseInsensitive(bool $value) : void
  215.     {
  216.         $this->caseInsensitive $value;
  217.     }
  218.  
  219.     /**
  220.      * Return if case insensitive is turned ON
  221.      */
  222.     public function getCaseInsensitive() : bool
  223.     {
  224.         return $this->caseInsensitive;
  225.     }