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

Documentation is available at TSqlUpdate.php

  1. <?php
  2. namespace Adianti\Database;
  3.  
  4. use Adianti\Database\TSqlStatement;
  5. use Adianti\Database\TTransaction;
  6.  
  7. /**
  8.  * Provides an Interface to create UPDATE statements
  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 TSqlUpdate extends TSqlStatement
  17. {
  18.     protected $sql;         // stores the SQL statement
  19.     private $columnValues;
  20.     private $preparedVars;
  21.     
  22.     /**
  23.      * Assign values to the database columns
  24.      * @param $column   Name of the database column
  25.      * @param $value    Value for the database column
  26.      */
  27.     public function setRowData($column$value)
  28.     {
  29.         if (is_scalar($valueOR is_null($value))
  30.         {
  31.             $this->columnValues[$column$value;
  32.         }
  33.     }
  34.     
  35.     /**
  36.      * Unset row data
  37.      * @param $column   Name of the database column
  38.      */
  39.     public function unsetRowData($column)
  40.     {
  41.         if (isset($this->columnValues[$column]))
  42.         {
  43.             unset($this->columnValues[$column]);
  44.         }
  45.     }
  46.     
  47.     /**
  48.      * Transform the value according to its PHP type
  49.      * before send it to the database
  50.      * @param $value    Value to be transformed
  51.      * @param $prepared If the value will be prepared
  52.      * @return       Transformed Value
  53.      */
  54.     private function transform($value$prepared FALSE)
  55.     {
  56.         // store just scalar values (string, integer, ...)
  57.         if (is_scalar($value))
  58.         {
  59.             if (substr(strtoupper($value),0,7== '(SELECT')
  60.             {
  61.                 $value  str_replace(['#''--''/*']['''''']$value);
  62.                 $result $value;
  63.             }
  64.             // if the value must not be escaped (NOESC in front)
  65.             else if (substr($value,0,6== 'NOESC:')
  66.             {
  67.                 $value  str_replace(['#''--''/*']['''''']$value);
  68.                 $result substr($value,6);
  69.             }
  70.             // if is a string
  71.             else if (is_string($valueand (!empty($value)))
  72.             {
  73.                 if ($prepared)
  74.                 {
  75.                     $preparedVar ':par_'.self::getRandomParameter();
  76.                     $this->preparedVars$preparedVar $value;
  77.                     $result $preparedVar;
  78.                 }
  79.                 else
  80.                 {
  81.                     $conn TTransaction::get();
  82.                     $result $conn->quote($value);
  83.                 }
  84.             }
  85.             else if (is_bool($value)) // if is a boolean
  86.             {
  87.                 $info TTransaction::getDatabaseInfo();
  88.                 
  89.                 if (in_array($info['type']['sqlsrv''dblib''mssql']))
  90.                 {
  91.                     $result $value '1''0';
  92.                 }
  93.                 else
  94.                 {
  95.                     $result $value 'TRUE''FALSE';
  96.                 }
  97.             }
  98.             else if ($value !== ''// if its another data type
  99.             {
  100.                 if ($prepared)
  101.                 {
  102.                     $preparedVar ':par_'.self::getRandomParameter();
  103.                     $this->preparedVars$preparedVar $value;
  104.                     $result $preparedVar;
  105.                 }
  106.                 else
  107.                 {
  108.                     $result $value;
  109.                 }
  110.             }
  111.             else
  112.             {
  113.                 $result "NULL";
  114.             }
  115.         }
  116.         else if (is_null($value))
  117.         {
  118.             $result "NULL";
  119.         }
  120.         
  121.         return $result;
  122.     }
  123.     
  124.     /**
  125.      * Return the prepared vars
  126.      */
  127.     public function getPreparedVars()
  128.     {
  129.         if ($this->criteria)
  130.         {
  131.             // "column values" prepared vars + "where" prepared vars
  132.             return array_merge($this->preparedVars$this->criteria->getPreparedVars());
  133.         }
  134.         else
  135.         {
  136.             return $this->preparedVars;
  137.         }
  138.     }
  139.     
  140.     /**
  141.      * Returns the UPDATE plain statement
  142.      * @param $prepared Return a prepared Statement
  143.      */
  144.     public function getInstruction$prepared FALSE)
  145.     {
  146.         $this->preparedVars array();
  147.         // creates the UPDATE statement
  148.         $this->sql = "UPDATE {$this->entity}";
  149.         
  150.         // concatenate the column pairs COLUMN=VALUE
  151.         if ($this->columnValues)
  152.         {
  153.             foreach ($this->columnValues as $column => $value)
  154.             {
  155.                 $value = $this->transform($value$prepared);
  156.                 $set["{$column} = {$value}";
  157.             }
  158.         }
  159.         $this->sql .= ' SET ' implode(', '$set);
  160.         
  161.         // concatenates the criteria (WHERE)
  162.         if ($this->criteria)
  163.         {
  164.             $dbInfo = TTransaction::getDatabaseInfo();
  165.             if (isset($dbInfo['case']) AND $dbInfo['case'] == 'insensitive')
  166.             {
  167.                 $this->criteria->setCaseInsensitive(TRUE);
  168.             }
  169.  
  170.             $this->sql .= ' WHERE ' $this->criteria->dump$prepared );
  171.         }
  172.         
  173.         // returns the SQL statement
  174.         return $this->sql;
  175.     }