Source for file TSqlUpdate.php
Documentation is available at TSqlUpdate.php
namespace
Adianti \
Database ;
* Provides an Interface to create UPDATE statements
* @author Pablo Dall'Oglio
* @copyright Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
* @license http://www.adianti.com.br/framework-license
protected $sql ; // stores the SQL statement
* 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, ...)
$value =
str_replace ( [ '#' , '--' , '/*' ] , [ '' , '' , '' ] , $value ) ;
// if the value must not be escaped (NOESC in front)
else if ( substr ( $value , 0 , 6 ) ==
'NOESC:' )
$value =
str_replace ( [ '#' , '--' , '/*' ] , [ '' , '' , '' ] , $value ) ;
else if ( is_string ( $value ) and ( ! empty ( $value )))
$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 ;
* Return the prepared vars
// "column values" prepared vars + "where" prepared vars
return $this -> preparedVars ;
* Returns the UPDATE plain statement
* @param $prepared Return a prepared Statement
$this -> preparedVars =
array ( ) ;
// creates the UPDATE statement
// concatenate the column pairs COLUMN=VALUE
foreach ( $this -> columnValues as $column =>
$value )
$value = $this -> transform ( $value , $prepared ) ;
$set [ ] =
" {
$column } = {
$value } " ;
$this -> sql .=
' SET ' .
implode ( ', ' , $set ) ;
// concatenates the criteria (WHERE)
$dbInfo = TTransaction :: getDatabaseInfo ( ) ;
if ( isset( $dbInfo [ 'case' ] ) AND $dbInfo [ 'case' ] == 'insensitive' )
$this -> criteria -> setCaseInsensitive ( TRUE ) ;
$this -> sql .=
' WHERE ' .
$this -> criteria -> dump ( $prepared ) ;
// returns the SQL statement