Source for file db.inc.php
Documentation is available at db.inc.php
* This file is part of the VCL for PHP project
* Copyright (c) 2004-2007 qadram software <support@qadram.com>
* Checkout AUTHORS file for more information on the developers
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* Encapsulates a table field
function getFieldName() { return $this->_fieldname; }
function setFieldName($value) { $this->_fieldname= $value; }
function getDisplayLabel() { return $this->_displaylabel; }
function setDisplayLabel($value) { $this->_displaylabel= $value; }
* CustomConnection, a common ancestor for all Connection objects
* List of attached DataSets
function readDataSets() { return $this->_datasets; }
function writeDataSets($value) { $this->_datasets= $value; }
function defaultDataSets() { return null; }
* Returns the fieldnames for the table
* @param string $tablename Table to get the fields for
* Completes a transaction
* @param bool $autocomplete If true, the transaction will be commited, if false, will be rolled back
* Send a connect event to all the datasets, both for connecting and disconnecting
* @param $connecting boolean specifies the status of the connection
for($i= 0;$i<= $this->_clients->count()- 1;$i++ )
if ($client->inheritsFrom('DataSet'))
* Returns a date formatted to be used on this database, depending on the type
* @param string $input Date to convert
* Prepares the query, to optimize it
* @param string $query SQL sentence to be prepared
* Returns a parameter formatted depending on the database type
* @param string $input Parameter name
* Quote a string depending on the database type
* Returns the clients of this database
function readClients() { return $this->_clients; }
function writeClients($value) { $this->_clients= $value; }
function defaultClients() { return null; }
* Fired after the database is connected
function defaultOnAfterConnect() { return null; }
* Fired before the database is connected
function defaultOnBeforeConnect() { return null; }
* Fired after the database is disconnected
function defaultOnAfterDisconnect() { return null; }
* Fired before the database is disconnected
function defaultOnBeforeDisconnect() { return null; }
* Fired when requesting login information for this database
function readOnLogin() { return $this->_onlogin; }
function writeOnLogin($value) { $this->_onlogin= $value; }
function defaultOnLogin() { return null; }
//Calls inherited constructor
* Determines if the database is connected or not
function readConnected() { return "0"; }
function writeConnected($value)
if ($value == $this->readConnected())
$this->callEvent("onbeforedisconnect",array());
$this->callEvent("onafterdisconnect",array());
function defaultConnected() { return "0"; }
* Place here the connection code
* Place here the disconnection code
* Base class to encapsulate a data set
class DataSet extends Component
private $_limitstart='0';
private $_limitcount='10';
function getLimitStart() { return $this->_limitstart; }
function setLimitStart($value) { $this->_limitstart=$value; }
function defaultLimitStart() { return "0"; }
function getLimitCount() { return $this->_limitcount; }
function setLimitCount($value) { $this->_limitcount=$value; }
function defaultLimitCount() { return "10"; }
function __construct($aowner=null)
//Calls inherited constructor
parent::__construct($aowner);
$this->fields=new Collection();
function readFieldValues()
function __set($nm, $val)
define('deCheckBrowseMode',8);
define('deFieldListChange',10);
define('deReconcileError',14);
define('deDisabledStateChange',15);
* Exception for a DatabaseError
* Function to raise a Database Error
* @param string $message Message of the exception to show
* @param Component $component Component is raising the exception
if ((assigned($component)) && ($component->Name != ''))
* DataSet component, base class to inherit and create dataset components
* Defines the starting record to filter the dataset with
function getLimitStart() { return $this->_limitstart; }
function setLimitStart($value) { $this->_limitstart= $value; }
function defaultLimitStart() { return "0"; }
* Defines how many records will be shown
function getLimitCount() { return $this->_limitcount; }
function setLimitCount($value) { $this->_limitcount= $value; }
function defaultLimitCount() { return "10"; }
* Override this method to perform the closing of the dataset
* Override this method to handle exceptions
* Override this method to init field definitions
* Override this method to perform the opening of the dataset
* Override this method to return if the cursor is open or not
* This property returns an array with the field names and values
function readFields() { return array(); }
* Specifies how many fields has the dataset
function readFieldCount() { return 0; }
* Buffer to hold values for searching/filtering
function writeDataSetField($value) { $this->_datasetfield= $value; }
function defaultDataSetField() { return null; }
* Specifies the state of the dataset, i.e. dsNone, dsEdit, etc
function readState() { return $this->_state; }
function writeState($value) { $this->_state= $value; }
* Specifies the associations with fields on this dataset and fields on the dataset attached to the MasterSource property
function writeMasterFields($value) { $this->_masterfields= $value; }
function defaultMasterFields() { return array(); }
* Specifies the dataset is going to act as master for this datasource
function writeMasterSource($value)
* Specifies the current record number
function readRecNo() { return $this->_recno; }
function writeRecNo($value)
function defaultRecNo() { return 0; }
* Specifies the record key for thid dataset
function readRecKey() { return $this->_reckey; }
function writeRecKey($value) { $this->_reckey= $value; }
function defaultRecKey() { return ""; }
$owner = $this->readOwner();
$prefix = $owner->readNamePath(). ".". $this->_name. ".";
$_SESSION[$prefix. "State"] = $this->_state;
// $_SESSION[$prefix."FieldBuffer"] = serialize($this->fieldbuffer);
// if (!empty($this->_regkey))
// $_SESSION[$prefix."RegKey"] = serialize($this->_regkey);
// $_SESSION[$prefix."RegKey"] = serialize($this->_regkey);
// $_SESSION[$prefix."RecNo"] = $this->_recno;
$owner = $this->readOwner();
$prefix = $owner->readNamePath(). ".". $this->_name. ".";
if (isset ($_SESSION[$prefix. "State"])) $this->_state= $_SESSION[$prefix. "State"];
// if (isset($_SESSION[$prefix."FieldBuffer"])) $this->fieldbuffer= unserialize($_SESSION[$prefix."FieldBuffer"]);
// if (isset($_SESSION[$prefix."RegKey"])) $this->_regkey= unserialize($_SESSION[$prefix."RegKey"]);
// if (isset($_SESSION[$prefix."RecNo"]))
// $this->RecNo= $_SESSION[$prefix."RecNo"];
* Specifies if the dataset has been modified or not
function readModified() { return $this->_modified; }
function writeModified($value) { $this->_modified= $value; }
function defaultModified() { return false; }
* Specifies if the dataset can be modified or not
function readCanModify() { return $this->_canmodify; }
function writeCanModify($value) { $this->_canmodify= $value; }
function defaultCanModify() { return true; }
* Fired before the dataset is open
function writeOnBeforeOpen($value) { $this->_onbeforeopen= $value; }
function defaultOnBeforeOpen() { return null; }
* Fired after the dataset is open
function writeOnAfterOpen($value) { $this->_onafteropen= $value; }
function defaultOnAfterOpen() { return null; }
* Fired before the dataset is closed
function defaultOnBeforeClose() { return null; }
* Fired after the dataset is closed
function writeOnAfterClose($value) { $this->_onafterclose= $value; }
function defaultOnAfterClose() { return null; }
* Fired before a record is inserted
function defaultOnBeforeInsert() { return null; }
* Fired after a record is inserted
function defaultOnAfterInsert() { return null; }
* Fired before the dataset enters in edit mode
function writeOnBeforeEdit($value) { $this->_onbeforeedit= $value; }
function defaultOnBeforeEdit() { return null; }
* Fired after the dataset enters in edito mode
function writeOnAfterEdit($value) { $this->_onafteredit= $value; }
function defaultOnAfterEdit() { return null; }
* Fired before a record is posted
function writeOnBeforePost($value) { $this->_onbeforepost= $value; }
function defaultOnBeforePost() { return null; }
* Fired after a record is posted
function writeOnAfterPost($value) { $this->_onafterpost= $value; }
function defaultOnAfterPost() { return null; }
* Fired before changes are cancelled
function defaultOnBeforeCancel() { return null; }
* Fired after changes are cancelled
function defaultOnAfterCancel() { return null; }
* Fired before a record is deleted
function defaultOnBeforeDelete() { return null; }
* Fired after a record is deleted
function defaultOnAfterDelete() { return null; }
function writeOnCalcFields($value) { $this->_oncalcfields= $value; }
function defaultOnCalcFields() { return null; }
function defaultOnDeleteError() { return null; }
* Specifies the filter to apply to this dataset
function readFilter() { return $this->_filter; }
function writeFilter($value)
function defaultFilter() { return ""; }
function defaultOnFilterRecord() { return null; }
function writeOnNewRecord($value) { $this->_onnewrecord= $value; }
function defaultOnNewRecord() { return null; }
function writeOnPostError($value) { $this->_onposterror= $value; }
function defaultOnPostError() { return null; }
* Checks if an specific operation can be made, if not, calls $ErrorEvent
* @param string $Operation Operation to perform on the dataset
* @param string $ErrorEvent Event to call if there is any error
// $this->UpdateCursorPos();
$Action= $this->callEvent($ErrorEvent, array('Exception'=> $e, 'Action'=> $Action));
if ($Action== daFail) throw $e;
if ($Action== daAbort) Abort();
//***********************
* Used to notify attached datasets about an specific event
* @param integer $event Event to notify
* @param array $info Info for the event
if TField(Info).FieldKind in [fkData, fkInternalCalc] then
FieldList.Updated := False;
FieldDefs.Updated := False;
deDataSetChange, deDataSetScroll:
FieldList.Updated := False;
FEnableEvent := deLayoutChange;
Event := deDisabledStateChange;
Info := Integer(State <> dsInactive);
NotifyDataSources := True;
FEnableEvent := deLayoutChange;
for I := 0 to FDataSources.Count - 1 do
TDataSource(FDataSources[I]).DataEvent(Event, Info);
* Checks if the dataset is active, if not, raises an exception
* Checks if the dataset can be modified, if not, raises an exception
if (!$this->CanModify) DatabaseError(_("Cannot modify a read-only dataset", Self));
* Returns if attached controls are disabled
* Disables controls attached to the datasource
$this->_DisableState= $this->State;
* Enable controls attached to the datasource
* Clear buffers associated with the dataset
// $this->_activerecord = 0;
// $this->_currentrecord = -1;
* Begins an insert/append operation
* Finishes an insert/append operation
$this->UpdateCursorPos();
$this->FreeFieldBuffers();
//Calls inherited constructor
* Inserts a record on the dataset
//OldCurrent := Bookmark;
//MoveBuffer(FRecordCount, FActiveRecord);
//Buffer := ActiveBuffer;
//if FRecordCount = 0 then
//SetBookmarkFlag(Buffer, bfBOF) else
//SetBookmarkData(Buffer, Pointer(OldCurrent));
//if FRecordCount < FBufferCount then Inc(FRecordCount);
* Appends a record to the dataset
// $this->GetPriorRecords();
* Cancel changes performed on the current record
// $DoScrollEvents = ($this->State == dsInsert);
// if ($DoScrollEvents) $this->DoBeforeScroll();
// $this->UpdateCursorPos();
// if ($DoScrollEvents) $this->DoAfterScroll();
* Updates the current record
* Checks if the dataset is in browse mode or not
if ($this->Modified) $this->post();
* Deletes a record on the dataset, depending on the values set in the buffer
if ($this->Recordcount== 0) DatabaseError(_("Cannot perform this operation on an empty dataset"), Self);
$this->callevent("onbeforedelete",array());
$this->callevent("onafterdelete",array());
* Sets the parent dataset in edit state
if ($this->DataSetField != null) $this->DataSetField->DataSet->Edit();
* Sets the dataset in edit mode, modifications will go to a buffer waiting for post()/cancel()
// if ($this->_recordcount==0) $this->Insert();
$this->callevent("onbeforeedit",array());
// $this->GetCalcFields(ActiveBuffer);
$this->callevent("onafteredit",array());
* Specifies how many records holds this dataset
function readRecordCount()
function defaultRecordCount() { return 0; }
* Moves the internal pointer of the dataset to the first record
if IsUniDirectional then begin
if not BOF then begin // Need to Close and Reopen dataset: (Midas)
//if not FReopened then begin
//$this->GetNextRecord();
//$this->GetNextRecords();
* Performs an internal open of the dataset
//$this->UpdateBufferCount();
* Opens the cursor for the dataset
* @param boolean $InfoQuery If true, initialize internal field defs
* Opens the cursor for this dataset
* Close the cursor for this dataset
// FDefaultFields := False;
* To be overriden to perform a first() operation
* To be overriden to perform a last() operation
* To initialize the current record
* @param array $Buffer Initial values
// $this->ClearCalcFields($Buffer);
// $this->SetBookmarkFlag($Buffer, bfInserted);
* To be overriden to perform a delete() operation
* To be overriden to perform a post() operation
// $this->CheckRequiredFields();
* To be overriden to perform a cancel() operation
* To be overriden to perform a edit() operation
* To be overriden to perform a insert() operation
* To be overriden to perform a refresh() operation
* Moves the internal pointer to the last record on the dataset
//$this->CheckBiDirectional();
// $this->GetPriorRecord();
// $this->GetPriorRecords();
* Refresh the dataset by opening and closing
* Moves the internal pointer to the next record
// if ($this->BlockReadSize > 0) $this->BlockReadNext();
if (!$this->EOF) $this->fieldbuffer= $this->_rs->fields;
* Updates or Inserts a record, depending on the current state of the dataset
$this->callevent("onbeforepost",array());
//$this->FreeFieldBuffers();
$this->callevent("onafterpost",array());
* Move the internal pointer by an specific distance
* @param integer $distance Records to move the pointer
* Moves the pointer one record before
* Specifies if the dataset is active or not
function writeActive($value)
if ($this->Active != $value)
$this->callevent("onbeforeopen",array());
$this->callevent("onbeforeclose",array());
$this->callevent("onafterclose",array());
function defaultActive() { return "0"; }
* If true, the pointer is at the beginning of the buffer
function readBOF() { return $this->_bof; }
function defaultBOF() { return false; }
* If true, the pointer is at the last record of the buffer
function readEOF() { return $this->_eof; }
function defaultEOF() { return false; }
* A class to link controls with datasets
//Calls inherited constructor
* Dataset this Datasource is attached to
function getDataSet() { return $this->_dataset; }
function setDataSet($value)
|