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

Documentation is available at TSqlInsert.php

  1. <?php
  2. namespace Adianti\Database;
  3.  
  4. use Adianti\Database\TSqlStatement;
  5. use Adianti\Database\TTransaction;
  6. use Adianti\Database\TCriteria;
  7. use Exception;
  8. use PDO;
  9.  
  10. /**
  11.  * Provides an Interface to create an INSERT statement
  12.  *
  13.  * @version    7.4
  14.  * @package    database
  15.  * @author     Pablo Dall'Oglio
  16.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  17.  * @license    http://www.adianti.com.br/framework-license
  18.  */
  19. class TSqlInsert extends TSqlStatement
  20. {
  21.     protected $sql;
  22.     private $columnValues;
  23.     private $preparedVars;
  24.     
  25.     /**
  26.      * Constructor method
  27.      */
  28.     public function __construct()
  29.     {
  30.         $this->columnValues [];
  31.         $this->preparedVars [];
  32.     }
  33.     
  34.     /**
  35.      * Assign values to the database columns
  36.      * @param $column   Name of the database column
  37.      * @param $value    Value for the database column
  38.      */
  39.     public function setRowData($column$value)
  40.     {
  41.         if (is_scalar($valueOR is_null($value))
  42.         {
  43.             $this->columnValues[$column$value;
  44.         }
  45.     }
  46.     
  47.     /**
  48.      * Unset row data
  49.      * @param $column   Name of the database column
  50.      */
  51.     public function unsetRowData($column)
  52.     {
  53.         if (isset($this->columnValues[$column]))
  54.         {
  55.             unset($this->columnValues[$column]);
  56.         }
  57.     }
  58.     
  59.     /**
  60.      * Transform the value according to its PHP type
  61.      * before send it to the database
  62.      * @param $value    Value to be transformed
  63.      * @param $prepared If the value will be prepared
  64.      * @return       Transformed Value
  65.      */
  66.     private function transform($value$prepared FALSE)
  67.     {
  68.         // store just scalar values (string, integer, ...)
  69.         if (is_scalar($value))
  70.         {
  71.             // if is a string
  72.             if (is_string($valueand (!empty($value)))
  73.             {
  74.                 if ($prepared)
  75.                 {
  76.                     $preparedVar ':par_'.self::getRandomParameter();
  77.                     $this->preparedVars$preparedVar $value;
  78.                     $result $preparedVar;
  79.                 }
  80.                 else
  81.                 {
  82.                     $conn TTransaction::get();
  83.                     $result $conn->quote($value);
  84.                 }
  85.             }
  86.             else if (is_bool($value)) // if is a boolean
  87.             {
  88.                 $info TTransaction::getDatabaseInfo();
  89.                 
  90.                 if (in_array($info['type']['sqlsrv''dblib''mssql']))
  91.                 {
  92.                     $result $value '1''0';
  93.                 }
  94.                 else
  95.                 {
  96.                     $result $value 'TRUE''FALSE';
  97.                 }
  98.             }
  99.             else if ($value !== ''// if its another data type
  100.             {
  101.                 if ($prepared)
  102.                 {
  103.                     $preparedVar ':par_'.self::getRandomParameter();
  104.                     $this->preparedVars$preparedVar $value;
  105.                     $result $preparedVar;
  106.                 }
  107.                 else
  108.                 {
  109.                     $result $value;
  110.                 }
  111.             }
  112.             else
  113.             {
  114.                 $result "NULL";
  115.             }
  116.         }
  117.         else if (is_null($value))
  118.         {
  119.             $result "NULL";
  120.         }
  121.         
  122.         return $result;
  123.     }
  124.     
  125.     /**
  126.      * this method doesn't exist in this class context
  127.      * @param $criteria A TCriteria object, specifiyng the filters
  128.      * @exception       Exception in any case
  129.      */
  130.     public function setCriteria(TCriteria $criteria)
  131.     {
  132.         throw new Exception("Cannot call setCriteria from " . __CLASS__);
  133.     }
  134.     
  135.     /**
  136.      * Return the prepared vars
  137.      */
  138.     public function getPreparedVars()
  139.     {
  140.         return $this->preparedVars;
  141.     }
  142.     
  143.     /**
  144.      * Returns the INSERT plain statement
  145.      * @param $prepared Return a prepared Statement
  146.      */
  147.     public function getInstruction$prepared FALSE )
  148.     {
  149.         $conn TTransaction::get();
  150.         $driver $conn->getAttribute(PDO::ATTR_DRIVER_NAME);
  151.         
  152.         $this->preparedVars array();
  153.         $columnValues $this->columnValues;
  154.         if ($columnValues)
  155.         {
  156.             foreach ($columnValues as $key => $value)
  157.             {
  158.                 $columnValues[$key$this->transform($value$prepared);
  159.             }
  160.         }
  161.         
  162.         $this->sql = "INSERT INTO {$this->entity} (";
  163.         $columns = implode(', ', array_keys($columnValues));   // concatenates the column names
  164.         $values  = implode(', ', array_values($columnValues)); // concatenates the column values
  165.         $this->sql .= $columns ')';
  166.         $this->sql .= " VALUES ({$values})";
  167.         
  168.         if ($driver == 'firebird')
  169.         {
  170.             $this->sql .= " RETURNING {{primary_key}}";
  171.         }
  172.         
  173.         // returns the string
  174.         return $this->sql;
  175.     }