The Data Access class is used to manipulate data stored in the database. It can be used to INSERT, SELECT, UPDATE and DELETE one or more records in a table. It has the ability to use TRANSACTIONS to ensure that the probability of data corruption is kept as low as possible.
Data Access requires access to DBManager to access the database.
The API for the Data Access can be found at Annex D.
The Data Access interface provides the following generic commands.
The getVersion() command returns a string containing the DBMS version. It is called as follows:
$result = $dbmanager->getDataDriver()->getDBVersion();
In this case $result should be a php string containing the version information of the connected database. If the command has failed then $result will be an object of type \g7mzr\db\common\Error.
The startTransaction() command initiates a database transaction.
$result = $dbmanager->getDataDriver()->startTransaction();
$result is true if the transaction is started, false otherwise.
The endTransaction() command will either, depending on the value of the $commit variable, commit (true) or rollback (false) the database commands issued since the last startTransaction() command.
$commit = true; // To commit the changes to the database
$commit = false; // To discard the changes.
$result = $dbmanager->getDataDriver()->endTransaction($commit);
$result is true if the end transaction command runs successfully, false otherwise.
The rowCount() command returns the number rows affected by the last command.
$rows = $dbmanager->getDataDriver()->rowCount();
$rows is an integer containg the number of rows affected by the last command. It is zero if no rows are affected.
The disconnect() function disconnects the script from the database by destroying the PDOStatement object and PDO object.
$dbManager->getDataDriver()->disconnect();
The disconnect() command always returns true.
The Data Access interface provides the following commands when inserting information into the database.
The dbinsert() command is used to add new records to the database. It takes 2 parameters:
$tableName = "table";
$insertData = array (
"field1" => "data",
"field2" => "data",
"field3" => "data",
"field4" => "data"
);
$result = $dbManager->getDataDriver()->dbinsert($tableName, $insertData);
$result is true if the record is inserted into the database. If the command has failed then $result will be an object of type \g7mzr\db\common\Error.
The dbinsertid returns the record number of the last record inserted by searching for information unique to that record. It takes 4 parameters all of which are strings:
$tablename = "tablename";
$idfield = "idfield";
$srchfield = "srchfieldname";
$srchdata = "SearchData";
$result = $dbManager->getDataDriver()->dbinsertid($tableName, $idfield, $srchfield, $srchdata);
If the command is successful then $result contains the record number. If the command has failed then $result will be an object of type \g7mzr\db\common\Error.
The Data Access interface provides the following commands to update information that is stored in the database.
The dbupdate() command is used to update the information stored in existing database records. It takes 3 parameters:
$tableName = "table";
$insertData = array (
"field1" => "data",
"field2" => "data",
"field3" => "data",
"field4" => "data"
);
$searchdata = array("searchfield" => "searchdata");
$result = $dbManager->getDataDriver()->dbupdate($tableName, $insertData, $searchdata);
$result is true if the record is updated. If the command has failed then $result will be an object of type \g7mzr\db\common\Error.
The Data Access interface provides the following commands to retrieve information that is stored in the database.
The dbselectsingle() command is used to select one record from the database. It takes 3 parameters:
$tablename = "table";
$fieldNames = array("field1", "field2", "field3" ...);
$searchdata = array("searchfield" => "searchdata");
$result = $dbManager->getDataDriver()->dbselectsingle($tableName, $fieldNames, $searchdata);
If the search was successful then $result will be an associated array containing the record. If the search has failed then $result will be an object of type \g7mzr\db\common\Error which will either indicate no records were found or an error occurred.
The dbselectmultiple() command is used to select one or more records from the database. It takes 5 parameters:
$tablename = "table1name";
$fieldNames = array("field1", "field2", "field3" ...);
$searchdata = array("searchfield" => "searchdata");
$order = "field1";
$join = array (
'table2' => 'table2name',
'field1' => 'table1name.joinfieldname',
'field2' => 'table2name.joinfieldname'
);
$result = $dbManager->getDataDriver()->dbselectmultiple(
$tableName,
$fieldNames,
searchdata,
$order,
$join
);
If the search was successful then $result will be an multi-dimensional array containing the one or more records. If the search has failed then $result will be an object of type \g7mzr\db\common\Error which will either indicate no records were found or an error occurred.
The Data Access interface provides the following commands to delete information that is stored in the database.
The dbdelete() command is used to delete single records from the database. It takes 2 parameters:
$tablename = "table";
$deletedata = array('fieldname' => 'data');
$result = $dbManager->getDataDriver()->dbdelete($tablename, $deletedata);
If the delete command was successful then $result will be boolean true. If the delete command has failed then $result will be an object of type \g7mzr\db\common\Error
The dbdeletemultiple() command is used to delete single records from the database. It takes 2 parameters:
$tablename = "table";
$deletedata = array();
$deletedata["fieldname"] = array("type" => "=", "data" => 'data');
$result = $dbManager->getDataDriver()->dbdeletemultiple($tablename, $deletedata);
If the delete command was successful then $result will be boolean true. If the delete command has failed then $result will be an object of type \g7mzr\db\common\Error