2010-12-02 23:34:41 +00:00
< ? php
2012-10-18 10:54:46 -04:00
2010-12-02 23:34:41 +00:00
/**
* class . dbtable . php
2012-10-18 10:54:46 -04:00
*
2012-07-12 20:39:07 -04:00
* @ package gulliver . system
2010-12-02 23:34:41 +00:00
*
* ProcessMaker Open Source Edition
2011-01-24 20:33:07 +00:00
* Copyright ( C ) 2004 - 2011 Colosa Inc .
2010-12-02 23:34:41 +00:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation , either version 3 of the
* License , or ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
2012-10-18 10:54:46 -04:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
2010-12-02 23:34:41 +00:00
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License
2012-10-18 10:54:46 -04:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2010-12-02 23:34:41 +00:00
*
* For more information , contact Colosa Inc , 2566 Le Jeune Rd . ,
* Coral Gables , FL , 33134 , USA , or email info @ colosa . com .
*
*/
/**
* DBTable class definition
* This class provides acces to a generalized table
* it assumes that the dbconnection object is already initialized , the table name , as well as the primary keys
* for the table should be also provided in order to provide the class a way to generate an UPDATE query properly .
2012-10-18 10:54:46 -04:00
*
2011-01-14 11:51:34 +00:00
* @ package gulliver . system
2010-12-02 23:34:41 +00:00
* @ author Fernando Ontiveros Lira < fernando @ colosa . com >
* @ copyright ( C ) 2002 by Colosa Development Team .
*
*/
class DBTable
{
2013-03-14 11:33:22 -04:00
public $_dbc ;
public $_dbses ;
public $_dset ;
public $table_name ;
public $table_keys ;
public $Fields = null ;
public $is_new ;
public $errorLevel ;
public $debug = false ;
2012-10-18 10:54:46 -04:00
/**
* Initiate a database conecction using default values
*
* @ author Fernando Ontiveros Lira < fernando @ colosa . com >
* @ access public
* @ param object $objConnection conecction string
* @ return void
*/
2013-03-14 11:33:22 -04:00
public function dBTable ( $objConnection = null , $strTable = " " , $arrKeys = array ( 'UID' ))
2010-12-02 23:34:41 +00:00
{
2012-10-18 10:54:46 -04:00
$this -> _dbc = null ;
$this -> _dbses = null ;
2013-03-14 11:33:22 -04:00
$this -> SetTo ( $objConnection , $strTable , $arrKeys );
2010-12-02 23:34:41 +00:00
}
2012-10-18 10:54:46 -04:00
/**
* Initiate a database conecction using default values
*
* @ author Fernando Ontiveros Lira < fernando @ colosa . com >
* @ access public
* @ param object $objDBConnection conecction string
* @ param string $strTable Table name defaultvalue = ''
* @ param array $arrKeys table keys defaultvalue = UID
* @ return void
*/
2013-03-14 11:33:22 -04:00
public function setTo ( $objDBConnection , $strTable = " " , $arrKeys = array ( 'UID' ))
2010-12-02 23:34:41 +00:00
{
2012-10-18 10:54:46 -04:00
$this -> _dbc = $objDBConnection ;
2013-03-14 11:33:22 -04:00
if ( $this -> _dbc != null && strcasecmp ( get_class ( $objDBConnection ), 'dbconnection' ) == 0 ) {
$this -> _dbses = new DBSession ( $this -> _dbc );
$this -> _dbses -> UseDB ( DB_NAME );
2012-10-18 10:54:46 -04:00
} else {
2013-03-14 11:33:22 -04:00
$dberror = PEAR :: raiseError ( null , DB_ERROR_OBJECT_NOT_DEFINED , null , 'null' , " You tried to call to a DBTable function without create an instance of DBConnection " , 'G_Error' , true );
2012-10-18 10:54:46 -04:00
//DBconnection::logError( $dberror );
return $dberror ;
2010-12-02 23:34:41 +00:00
}
2012-10-18 10:54:46 -04:00
$this -> is_new = true ;
$this -> Fields = null ;
$this -> table_name = $strTable ;
2013-03-14 11:33:22 -04:00
if ( is_array ( $arrKeys )) {
2012-10-18 10:54:46 -04:00
$this -> table_keys = $arrKeys ;
} else {
2013-03-14 11:33:22 -04:00
$this -> table_keys = array ( 0 => $arrKeys
2012-10-18 10:54:46 -04:00
);
2010-12-02 23:34:41 +00:00
}
2012-10-18 10:54:46 -04:00
$this -> errorLevel = $this -> _dbc -> errorLevel ;
2010-12-02 23:34:41 +00:00
}
2012-10-18 10:54:46 -04:00
/**
* Loads full description of a referenced table in Fields
*
* @ author Fernando Ontiveros Lira < fernando @ colosa . com >
* @ access public
* @ return void
*/
2013-03-14 11:33:22 -04:00
public function loadEmpty ()
2012-10-18 10:54:46 -04:00
{
$stQry = " DESCRIBE ` " . $this -> table_name . " ` " ;
2013-03-14 11:33:22 -04:00
$dset = $this -> _dbses -> execute ( $stQry );
2012-10-18 10:54:46 -04:00
//$dset = new DBRecordSet( $this->_dbses->Result );
$nlim = $dset -> Count ();
$this -> Fields = null ;
2013-03-14 11:33:22 -04:00
for ( $ncount = 0 ; $ncount < $nlim ; $ncount ++ ) {
2012-10-18 10:54:46 -04:00
$data = $dset -> Read ();
$fname = $data [ 'Field' ];
$fval = " " ;
2013-03-14 11:33:22 -04:00
$ftypearr = explode ( $data [ 'Type' ], '(' );
2012-10-18 10:54:46 -04:00
$ftype = $ftypearr [ 0 ];
if ( $data [ 'Key' ] == 'PRI' ) {
2013-03-14 11:33:22 -04:00
if ( is_array ( $this -> table_keys )) {
$this -> table_keys [ count ( $this -> table_keys ) - 1 ] = $fname ;
2012-10-18 10:54:46 -04:00
} else {
$this -> table_keys [ 0 ] = $fname ;
}
}
2010-12-02 23:34:41 +00:00
2012-10-18 10:54:46 -04:00
switch ( $ftype ) {
case 'int' :
case 'smallint' :
case 'tinyint' :
case 'decimal' :
case 'numeric' :
case 'double' :
case 'float' :
$fval = 0 ;
break ;
}
2010-12-02 23:34:41 +00:00
2012-10-18 10:54:46 -04:00
$this -> Fields [ $fname ] = $fval ;
$this -> Fields [ $ncount ] = & $this -> Fields [ $fname ];
}
2010-12-02 23:34:41 +00:00
}
2012-10-18 10:54:46 -04:00
/**
* Return specified field on the referenced table
*
* @ author Fernando Ontiveros Lira < fernando @ colosa . com >
* @ access public
* @ param string $strWhere string which contains conditions
* @ return strint
*/
2013-03-14 11:33:22 -04:00
public function loadWhere ( $strWhere )
2010-12-02 23:34:41 +00:00
{
2012-10-18 10:54:46 -04:00
$this -> Fields = null ;
2010-12-02 23:34:41 +00:00
2012-10-18 10:54:46 -04:00
$stQry = " SELECT * FROM ` " . $this -> table_name . " ` " ;
if ( $strWhere != " " ) {
$stQry .= " WHERE " . $strWhere ;
}
2013-03-14 11:33:22 -04:00
$this -> _dset = $this -> _dbses -> Execute ( $stQry , $this -> debug , $this -> errorLevel );
if ( DB :: isError ( $this -> _dset )) {
2012-10-18 10:54:46 -04:00
return $this -> _dset ;
}
2010-12-02 23:34:41 +00:00
2012-10-18 10:54:46 -04:00
if ( $this -> _dset -> Count () > 0 ) {
$this -> Fields = $this -> _dset -> Read ();
$this -> is_new = false ;
} else {
$this -> Fields = null ;
$this -> is_new = true ;
2010-12-02 23:34:41 +00:00
}
2012-10-18 10:54:46 -04:00
return $this -> Fields ;
}
/**
* Return all fields on the referenced table
*
* @ author Fernando Ontiveros Lira < fernando @ colosa . com >
* @ access public
* @ param array array of arguments key values
* @ return void
*/
2017-12-04 13:25:35 +00:00
public function load ( $sUID = null )
2012-10-18 10:54:46 -04:00
{
// bug::traceRoute();
$ncount = 0 ;
$stWhere = " " ;
$arrKeys = func_get_args ();
2013-03-14 11:33:22 -04:00
if ( isset ( $arrKeys [ 0 ]) && is_array ( $arrKeys [ 0 ])) {
2012-10-18 10:54:46 -04:00
foreach ( $this -> table_keys as $key => $val ) {
if ( $stWhere == " " ) {
$stWhere .= " $val = ' " . $arrKeys [ 0 ][ $val ] . " ' " ;
} else {
$stWhere .= " AND $val = ' " . $arrKeys [ 0 ][ $val ] . " ' " ;
}
}
} else {
foreach ( $arrKeys as $val ) {
if ( $stWhere == " " ) {
$stWhere .= $this -> table_keys [ $ncount ] . " =' " . $val . " ' " ;
} else {
$stWhere .= " AND " . $this -> table_keys [ $ncount ] . " =' " . $val . " ' " ;
}
2013-03-14 11:33:22 -04:00
$ncount ++ ;
2012-10-18 10:54:46 -04:00
}
2010-12-02 23:34:41 +00:00
}
2013-03-14 11:33:22 -04:00
return $this -> LoadWhere ( $stWhere );
2010-12-02 23:34:41 +00:00
}
2012-10-18 10:54:46 -04:00
/**
* Function nextvalPGSql
*
* @ author David S . Callizaya S . < davidsantos @ colosa . com >
* @ access public
* @ param eter string seq
* @ return string
*/
2013-03-14 11:33:22 -04:00
public function nextvalPGSql ( $seq )
2012-10-18 10:54:46 -04:00
{
$stQry = " Select NEXTVAL( ' $seq ' ) " ;
2013-03-14 11:33:22 -04:00
$dset = $this -> _dbses -> Execute ( $stQry );
2012-10-18 10:54:46 -04:00
$row = $dset -> Read ();
2013-03-14 11:33:22 -04:00
if ( is_array ( $row )) {
2012-10-18 10:54:46 -04:00
return $row [ 'NEXTVAL' ];
}
2013-03-14 11:33:22 -04:00
die ( " Sequence ' $seq ' is not exist!! " );
2012-10-18 10:54:46 -04:00
return - 1 ;
2010-12-02 23:34:41 +00:00
}
2012-10-18 10:54:46 -04:00
/**
* Insert a new row
*
* @ author Fernando Ontiveros Lira < fernando @ colosa . com >
* @ access public
* @ return boolean
*
*/
2013-03-14 11:33:22 -04:00
public function insert ()
2012-10-18 10:54:46 -04:00
{
$strFields = " " ;
$strValues = " " ;
2013-03-14 11:33:22 -04:00
if ( defined ( 'DB_ADAPTER' )) {
2012-10-18 10:54:46 -04:00
$DBEngine = DB_ADAPTER ;
} else {
$DBEngine = 'mysql' ;
}
foreach ( $this -> Fields as $field => $val ) {
$strFields .= $field . " , " ;
$iskey = false ;
2013-03-14 11:33:22 -04:00
if ( isset ( $this -> table_keys ) && is_array ( $this -> table_keys )) {
$iskey = in_array ( $field , $this -> table_keys ) && strtoupper ( substr ( trim ( $val ), 0 , 7 )) == " NEXTVAL " ;
2012-10-18 10:54:46 -04:00
}
2013-03-14 11:33:22 -04:00
$dbcType = isset ( $this -> _dbc -> type ) ? $this -> _dbc -> type : $DBEngine ;
2012-10-18 10:54:46 -04:00
// Commented by new format of textarea in javascript
2013-03-14 11:33:22 -04:00
if ( ! $iskey ) {
2012-10-18 10:54:46 -04:00
$val = " ' " . $val . " ' " ;
}
2013-03-14 11:33:22 -04:00
///-- $val = "'" . G::sqlEscape( $val , $dbcType ) . "'";
2012-10-18 10:54:46 -04:00
$strValues .= $val . " , " ;
}
2013-03-14 11:33:22 -04:00
$strFields = substr ( $strFields , 0 , strlen ( $strFields ) - 1 );
$strValues = substr ( $strValues , 0 , strlen ( $strValues ) - 1 );
2010-12-02 23:34:41 +00:00
2012-10-18 10:54:46 -04:00
$stQry = " INSERT INTO ` " . $this -> table_name . " ` ( " . $strFields . " ) values ( " . $strValues . " ) " ;
2010-12-02 23:34:41 +00:00
2013-03-14 11:33:22 -04:00
$result = $this -> _dbses -> Execute ( $stQry , $this -> debug );
2012-10-18 10:54:46 -04:00
return $result ;
2010-12-02 23:34:41 +00:00
}
2012-10-18 10:54:46 -04:00
/**
* Update an existing row
*
* @ author Fernando Ontiveros Lira < fernando @ colosa . com >
* @ access public
* @ return boolean
*/
2013-03-14 11:33:22 -04:00
public function update ()
2012-10-18 10:54:46 -04:00
{
$stQry = " " ;
2010-12-02 23:34:41 +00:00
2012-10-18 10:54:46 -04:00
$stWhere = '' ;
2013-03-14 11:33:22 -04:00
$remainKeys = array ();
2011-01-04 20:27:02 +00:00
2013-03-14 11:33:22 -04:00
if ( defined ( 'DB_ADAPTER' )) {
2012-10-18 10:54:46 -04:00
$DBEngine = DB_ADAPTER ;
} else {
$DBEngine = 'mysql' ;
}
2011-01-04 20:27:02 +00:00
2012-10-18 10:54:46 -04:00
foreach ( $this -> table_keys as $k => $v ) {
$remainKeys [ $v ] = false ;
2010-12-02 23:34:41 +00:00
}
2012-10-18 10:54:46 -04:00
foreach ( $this -> Fields as $field => $val ) {
$iskey = false ;
2013-03-14 11:33:22 -04:00
$iskey = in_array ( $field , $this -> table_keys );
2012-10-18 10:54:46 -04:00
if ( $iskey == false ) {
$stQry .= $field . " =' " . $val . " ', " ;
// Commented by new format of textarea in javascript
///-- $stQry .= $field . "='" . G::sqlEscape ( $val, isset( $this->_dbc->type) ? $this->_dbc->type : $DBEngine ) . "', ";
} else {
if ( $stWhere == " " ) {
2013-03-14 11:33:22 -04:00
$stWhere .= $field . " =' " . G :: sqlEscape ( $val , isset ( $this -> _dbc -> type ) ? $this -> _dbc -> type : $DBEngine ) . " ' " ;
2012-10-18 10:54:46 -04:00
} else {
2013-03-14 11:33:22 -04:00
$stWhere .= " AND " . $field . " =' " . G :: sqlEscape ( $val , isset ( $this -> _dbc -> type ) ? $this -> _dbc -> type : $DBEngine ) . " ' " ;
2012-10-18 10:54:46 -04:00
}
$remainKeys [ $field ] = true ;
}
2010-12-02 23:34:41 +00:00
}
2013-03-14 11:33:22 -04:00
foreach ( $remainKeys as $field => $bool ) {
2012-10-18 10:54:46 -04:00
if ( $bool == false ) {
if ( $stWhere != " " ) {
$stWhere = " AND " ;
}
$stWhere .= $field . " = '' " ;
$remainKeys [ $field ] = true ;
}
2013-03-14 11:33:22 -04:00
}
2010-12-02 23:34:41 +00:00
2013-03-14 11:33:22 -04:00
$stQry = trim ( $stQry );
$stQry = substr ( $stQry , 0 , strlen ( $stQry ) - 1 ); //to remove the last comma ,
if ( ! $stQry ) {
2012-10-18 10:54:46 -04:00
return ;
}
$stQry = " UPDATE ` " . $this -> table_name . " ` SET " . $stQry ;
2013-03-14 11:33:22 -04:00
$stWhere = trim ( $stWhere );
2012-10-18 10:54:46 -04:00
if ( $stWhere != " " ) {
$stQry .= " WHERE " . $stWhere ;
}
$result = false ;
2010-12-02 23:34:41 +00:00
2013-03-14 11:33:22 -04:00
$result = $this -> _dbses -> execute ( $stQry , $this -> debug , $this -> errorLevel );
2012-10-18 10:54:46 -04:00
$this -> is_new = false ;
return $result ;
}
2010-12-02 23:34:41 +00:00
2012-10-18 10:54:46 -04:00
/**
* Save a register in a table
*
* depending of value of " is_new " inserts or update is do it
*
* @ author Fernando Ontiveros Lira < fernando @ colosa . com >
* @ access public
* @ return boolean
*/
2017-12-04 13:25:35 +00:00
public function save ( $Fields = null , $labels = [], $options = [])
2012-10-18 10:54:46 -04:00
{
if ( $this -> is_new == true ) {
return $this -> Insert ();
} else {
return $this -> Update ();
}
}
2010-12-02 23:34:41 +00:00
2012-10-18 10:54:46 -04:00
/**
* Delete an existing row
*
* @ author Fernando Ontiveros Lira < fernando @ colosa . com >
* @ access public
* @ return boolean
*/
2017-12-04 13:25:35 +00:00
public function delete ( $uid = null )
2012-10-18 10:54:46 -04:00
{
$stQry = " delete from ` " . $this -> table_name . " ` " ;
$stWhere = '' ;
2013-03-14 11:33:22 -04:00
$remainKeys = array ();
if ( defined ( 'DB_ADAPTER' )) {
2012-10-18 10:54:46 -04:00
$DBEngine = DB_ADAPTER ;
} else {
$DBEngine = 'mysql' ;
}
foreach ( $this -> table_keys as $k => $v ) {
$remainKeys [ $v ] = false ;
}
2013-03-14 11:33:22 -04:00
if ( is_array ( $this -> Fields )) {
2012-10-18 10:54:46 -04:00
foreach ( $this -> Fields as $field => $val ) {
$iskey = false ;
2013-03-14 11:33:22 -04:00
$iskey = in_array ( $field , $this -> table_keys );
2012-10-18 10:54:46 -04:00
if ( $iskey == true ) {
if ( $stWhere == " " ) {
2013-03-14 11:33:22 -04:00
$stWhere .= $field . " =' " . G :: sqlEscape ( $val , isset ( $this -> _dbc -> type ) ? $this -> _dbc -> type : $DBEngine ) . " ' " ;
2012-10-18 10:54:46 -04:00
} else {
2013-03-14 11:33:22 -04:00
$stWhere .= " AND " . $field . " =' " . G :: sqlEscape ( $val , isset ( $this -> _dbc -> type ) ? $this -> _dbc -> type : $DBEngine ) . " ' " ;
2012-10-18 10:54:46 -04:00
}
$remainKeys [ $field ] = true ;
}
2010-12-02 23:34:41 +00:00
}
2012-10-18 10:54:46 -04:00
}
2013-03-14 11:33:22 -04:00
foreach ( $remainKeys as $field => $bool ) {
2012-10-18 10:54:46 -04:00
if ( $bool == false ) {
if ( $stWhere != " " ) {
$stWhere .= " AND " ;
}
$stWhere .= $field . " = '' " ;
$remainKeys [ $field ] = true ;
2010-12-02 23:34:41 +00:00
}
2013-03-14 11:33:22 -04:00
}
2010-12-02 23:34:41 +00:00
2013-03-14 11:33:22 -04:00
$stQry = trim ( $stQry );
$stWhere = trim ( $stWhere );
2012-10-18 10:54:46 -04:00
if ( $stWhere == '' ) {
2013-03-14 11:33:22 -04:00
$dberror = PEAR :: raiseError ( null , G_ERROR_WARNING_MESSAGE , null , 'null' , " You tried to call delete method without WHERE clause, if you want to delete all records use dbsession " , 'G_Error' , true );
DBconnection :: logError ( $dberror , $this -> errorLevel );
2012-10-18 10:54:46 -04:00
return $dberror ;
}
$stQry .= " WHERE " . $stWhere ;
2010-12-02 23:34:41 +00:00
2013-03-14 11:33:22 -04:00
$result = $this -> _dbses -> execute ( $stQry , $this -> debug , $this -> errorLevel );
2012-10-18 10:54:46 -04:00
$this -> is_new = false ;
return $result ;
}
2010-12-02 23:34:41 +00:00
2012-10-18 10:54:46 -04:00
/**
* Move to next record in a recordset
*
* Move to next record in a recordset , this is useful where the load method have a recordset with many rows
*
* @ author Fernando Ontiveros Lira < fernando @ colosa . com >
* @ access public
* @ return boolean
*/
2013-03-14 11:33:22 -04:00
public function next ()
2012-10-18 10:54:46 -04:00
{
$this -> Fields = $this -> _dset -> read ();
}
2011-01-04 20:27:02 +00:00
}
2013-03-14 11:33:22 -04:00