Source for file TSqlInsert.php
Documentation is available at TSqlInsert.php
namespace
Adianti \
Database ;
* Provides an Interface to create an INSERT statement
* @author Pablo Dall'Oglio
* @copyright Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
* @license http://www.adianti.com.br/framework-license
$this -> columnValues =
[ ] ;
$this -> preparedVars =
[ ] ;
* Assign values to the database columns
* @param $column Name of the database column
* @param $value Value for the database column
$this -> columnValues [ $column ] =
$value ;
* @param $column Name of the database column
if ( isset
( $this -> columnValues [ $column ] ))
unset
( $this -> columnValues [ $column ] ) ;
* Transform the value according to its PHP type
* before send it to the database
* @param $value Value to be transformed
* @param $prepared If the value will be prepared
* @return Transformed Value
private function transform ( $value , $prepared =
FALSE )
// store just scalar values (string, integer, ...)
$preparedVar =
':par_' .
self :: getRandomParameter ( ) ;
$this -> preparedVars [ $preparedVar ] =
$value ;
$result =
$conn -> quote ( $value ) ;
else if ( is_bool ( $value )) // if is a boolean
if ( in_array ( $info [ 'type' ] , [ 'sqlsrv' , 'dblib' , 'mssql' ] ))
$result =
$value ?
'1' :
'0' ;
$result =
$value ?
'TRUE' :
'FALSE' ;
else if ( $value !==
'' ) // if its another data type
$preparedVar =
':par_' .
self :: getRandomParameter ( ) ;
$this -> preparedVars [ $preparedVar ] =
$value ;
* this method doesn't exist in this class context
* @param $criteria A TCriteria object, specifiyng the filters
* @exception Exception in any case
throw
new Exception ( "Cannot call setCriteria from " . __CLASS__
) ;
* Return the prepared vars
return $this -> preparedVars ;
* Returns the INSERT plain statement
* @param $prepared Return a prepared Statement
$driver =
$conn -> getAttribute ( PDO :: ATTR_DRIVER_NAME ) ;
$this -> preparedVars =
array ( ) ;
$columnValues =
$this -> columnValues ;
foreach ( $columnValues as $key =>
$value )
$columnValues [ $key ] =
$this -> transform ( $value , $prepared ) ;
$this -> sql =
" INSERT INTO {$this -> entity } (
" ;
$columns = implode ( ', ' , array_keys ( $columnValues )) ; // concatenates the column names
$values = implode ( ', ' , array_values ( $columnValues )) ; // concatenates the column values
$this -> sql .=
$columns .
')' ;
$this -> sql .=
" VALUES ({
$values } )
" ;
if ( $driver == 'firebird' )
$this -> sql .=
" RETURNING {{primary_key}}" ;