Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function __construct($p_iTableName = "", $p_iTableIndex = "", $p_sDBInfo = "") {
- global $oConfig;
- parent::__construct(); // Initialise the Base
- if (empty($p_sDBInfo)) {
- $p_sDBInfo = "default";
- }
- $this->sDbConnection = $p_sDBInfo;
- if (empty($p_iTableName) || empty($p_iTableIndex)) {
- $this->throwException('Table name or index not found', $this->aQueries);
- } else {
- $this->sTableIndex = $p_iTableIndex;
- $this->sTableName = $p_iTableName;
- }
- $db[$p_sDBInfo] = $oConfig->db->toArray();
- foreach(array('host', 'username', 'password', 'database', 'enabled') as $field) {
- if(!array_key_exists($field, $db[$p_sDBInfo])) {
- $this->throwException('Database configuration error. Unable to find '.$field.' in '.$p_sDBInfo);
- }
- if($field != 'password' && $db[$p_sDBInfo][$field] == '') {
- $this->throwException('No information found for database configuration option: '.$field);
- }
- }
- if(!$db[$p_sDBInfo]['enabled']) {
- $this->throwException('Trying to use database configuration for '.$p_sDBInfo.' and it is turned off. Check your database configuration file');
- }
- $this->sHostName = $db[$p_sDBInfo]['host'];
- $this->sUserName = $db[$p_sDBInfo]['username'];
- $this->sPassword = $db[$p_sDBInfo]['password'];
- $this->sDataBase = $db[$p_sDBInfo]['database'];
- try {
- $this->rHandler = new PDO('mysql:host='.$this->sHostName.';dbname='.$this->sDataBase, $this->sUserName, $this->sPassword, array(PDO::ATTR_PERSISTENT => true));
- $this->rHandler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- } catch (PDOException $e) {
- $this->throwException('Database Connection Error');
- }
- if(!$this->isTableExist($this->sDataBase, $this->sTableName)) {
- $this->throwException('Table: ' . $p_iTableName . ' doesn\'t exist', $this->aQueries);
- }
- }
- function putRecord ($p_aRecord) {
- try {
- // If the primary key is found, its an update
- if (isset($p_aRecord[$this->sTableIndex]) && $p_aRecord[$this->sTableIndex] !== null) {
- $sData = implode(' = ?, ', $this->parse_reserved_keys(array_keys($p_aRecord))) . ' = ?'; // Setup the field = ? template
- $sQuery = "UPDATE ".$this->sTableName." SET ".$sData." WHERE ".$this->sTableIndex."=".$p_aRecord[$this->sTableIndex];
- $oResult = $this->rHandler->prepare($sQuery);
- $oResult = $oResult->execute(array_values($p_aRecord));
- return $this->rHandler->lastInsertId();
- // No primary key found its an insert
- } else {
- $sData = $sValues = $sKeys = ''; $i = 0;
- $sKeys = implode(',', $this->parse_reserved_keys(array_keys($p_aRecord))); // Make the fields comma separated
- while($i < count($p_aRecord)) { $sValues .= '?,'; $i++; } // Make the question marks
- $sValues = substr($sValues, 0, -1); // Remove the last comma
- $sQuery = "INSERT INTO ".$this->sTableName." (".$sKeys.") VALUES (".$sValues.")";
- $oResult = $this->rHandler->prepare($sQuery);
- $oQueryResult = $oResult->execute(array_values($p_aRecord));
- return $this->rHandler->lastInsertId();
- }
- } catch(PDOException $e) {
- $this->throwException($e->getMessage());
- }
- }
- function getList ($p_mFilter = "", $p_sOrder = "", $p_iLimit = "", $p_aExtras = array()) {
- try {
- /* Turn the filters into a string (if not already) */
- if (is_array($p_mFilter) && count($p_mFilter) > 0) {
- $sFilter = $this->_filtersToStr($p_mFilter);
- } elseif(is_string($p_mFilter) && $p_mFilter != '') {
- $sFilter = 'WHERE ' . $p_mFilter;
- } else {
- $sFilter = '';
- }
- // die($sFilter);
- $sOrder = (is_array($p_sOrder) && count($p_sOrder) > 0) ? 'ORDER BY ' : '';
- if($sOrder == '') {
- $sOrder = ($p_sOrder != '') ? 'ORDER BY ' : '';
- }
- foreach ((array) $p_sOrder as $key => $val) {
- $sOrder .= $val.',';
- }
- $sOrder = substr($sOrder, 0, -1);
- $p_iLimit = ($p_iLimit != '') ? 'LIMIT '.$p_iLimit : '';
- /* Lets get the information from extras array */
- $sTableName = (count($p_aExtras) > 0) ? $this->findTableName($p_aExtras) : $this->sTableName;
- $sQuery = "SELECT * FROM $sTableName $sFilter $sOrder $p_iLimit";
- $this->_aQueries[] = $sQuery;
- //die($sQuery);
- $oResult = $this->rHandler->prepare($sQuery);
- $oResult->execute();
- return $oResult->fetchAll($this->sFetchMode);
- } catch(PDOException $e) {
- $this->throwException($e->getMessage());
- } catch(PPI_Exception $e) {
- $this->throwException($e->getMessage());
- }
- }
- function find($search) {
- $oResult = $this->rHandler->prepare('SELECT * FROM '.$this->sTableName. ' WHERE ' . $this->sTableIndex . ' = ' . $this->quote($search));
- $oResult->execute();
- return $oResult->fetch($this->sFetchMode);
- }
- function getAllDBs() {
- $newdbs = array();
- $dbs = mysql_list_dbs($this->rHandler);
- print_r($db = mysql_fetch_object($dbs)); exit;
- while($db = mysql_fetch_object($dbs)) {
- array_push($newdbs, $db->Database);
- }
- return $newdbs;
- }
- function dropTable($p_sDBName = "", $p_sTableName = "") {
- if(strlen($p_sDBName) > 0 && strlen($p_sDBName) > 0) {
- mysql_select_db($p_sDBName, $this->rHandler);
- /* Drop the table */
- if($this->query("DROP TABLE $p_sTableName IF EXISTS")) {
- return true;
- } else {
- return false;
- }
- }
- }
- function addColumn($p_sDBName = "", $p_sTableName = "", $sFieldProperties = "") {
- if(strlen($p_sDBName) > 0 && strlen($p_sTableName) > 0 && strlen($sFieldProperties) > 0) {
- mysql_select_db($p_sDBName, $this->rHandler);
- if($this->query("ALTER TABLE $p_sTableName ADD COLUMN $sFieldProperties")) {
- return true;
- } else {
- return false;
- }
- }
- }
- function delRecord($p_sTableName = "", $p_sRecordName = "", $p_iRecordID = "") {
- if(strlen($p_sTableName) > 0 && strlen($p_sRecordName) > 0 && strlen($p_iRecordID) > 0) {
- $sTableName = $this->findTableName(array('sTableName' => $p_sTableName));
- $oResult = $this->rHandler->prepare("DELETE FROM $sTableName WHERE $p_sRecordName = ".$this->quote($p_iRecordID));
- $oResult->execute();
- return $oResult->fetch($this->sFetchMode);
- }
- }
- /* return the connection handler generated from mysql_connect() */
- function getHandler() { return $this->rHandler; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement