Advertisement
Guest User

Untitled

a guest
May 12th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.02 KB | None | 0 0
  1. function __construct($p_iTableName = "", $p_iTableIndex = "", $p_sDBInfo = "") {
  2.     global $oConfig;       
  3.     parent::__construct(); // Initialise the Base
  4.    
  5.     if (empty($p_sDBInfo)) {
  6.         $p_sDBInfo = "default";        
  7.     }
  8.     $this->sDbConnection = $p_sDBInfo;     
  9.     if (empty($p_iTableName) || empty($p_iTableIndex)) {
  10.         $this->throwException('Table name or index not found', $this->aQueries);
  11.     } else {
  12.         $this->sTableIndex = $p_iTableIndex;
  13.         $this->sTableName  = $p_iTableName;
  14.     }
  15.  
  16.     $db[$p_sDBInfo] = $oConfig->db->toArray(); 
  17.     foreach(array('host', 'username', 'password', 'database', 'enabled') as $field) {
  18.         if(!array_key_exists($field, $db[$p_sDBInfo])) {
  19.             $this->throwException('Database configuration error. Unable to find '.$field.' in '.$p_sDBInfo);
  20.         }
  21.         if($field != 'password' && $db[$p_sDBInfo][$field] == '') {
  22.             $this->throwException('No information found for database configuration option: '.$field);
  23.         }
  24.     }
  25.     if(!$db[$p_sDBInfo]['enabled']) {
  26.         $this->throwException('Trying to use database configuration for '.$p_sDBInfo.' and it is turned off. Check your database configuration file');
  27.     }
  28.     $this->sHostName = $db[$p_sDBInfo]['host'];
  29.     $this->sUserName = $db[$p_sDBInfo]['username'];
  30.     $this->sPassword = $db[$p_sDBInfo]['password'];
  31.     $this->sDataBase = $db[$p_sDBInfo]['database'];
  32.     try {
  33.         $this->rHandler = new PDO('mysql:host='.$this->sHostName.';dbname='.$this->sDataBase, $this->sUserName, $this->sPassword, array(PDO::ATTR_PERSISTENT => true));
  34.         $this->rHandler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  35.     } catch (PDOException $e) {
  36.         $this->throwException('Database Connection Error');
  37.     }      
  38.    
  39.     if(!$this->isTableExist($this->sDataBase, $this->sTableName)) {
  40.         $this->throwException('Table: ' . $p_iTableName . ' doesn\'t exist', $this->aQueries);
  41.     }
  42. }
  43.  
  44.  
  45.  
  46. function putRecord ($p_aRecord) {
  47.     try {
  48.         // If the primary key is found, its an update
  49.         if (isset($p_aRecord[$this->sTableIndex]) && $p_aRecord[$this->sTableIndex] !== null) {
  50.             $sData      = implode(' = ?, ', $this->parse_reserved_keys(array_keys($p_aRecord))) . ' = ?';   // Setup the field = ? template        
  51.             $sQuery     = "UPDATE ".$this->sTableName." SET ".$sData." WHERE ".$this->sTableIndex."=".$p_aRecord[$this->sTableIndex];
  52.             $oResult    = $this->rHandler->prepare($sQuery);
  53.             $oResult    = $oResult->execute(array_values($p_aRecord));
  54.             return $this->rHandler->lastInsertId();            
  55.         // No primary key found its an insert
  56.         } else {
  57.             $sData          = $sValues = $sKeys = ''; $i = 0;
  58.             $sKeys          = implode(',', $this->parse_reserved_keys(array_keys($p_aRecord))); // Make the fields comma separated
  59.             while($i < count($p_aRecord)) { $sValues .= '?,'; $i++; } // Make the question marks
  60.             $sValues        = substr($sValues, 0, -1);   // Remove the last comma
  61.             $sQuery         = "INSERT INTO ".$this->sTableName." (".$sKeys.") VALUES (".$sValues.")";
  62.             $oResult        = $this->rHandler->prepare($sQuery);
  63.             $oQueryResult   = $oResult->execute(array_values($p_aRecord));
  64.             return $this->rHandler->lastInsertId();
  65.         }
  66.     } catch(PDOException $e) {
  67.         $this->throwException($e->getMessage());
  68.     }      
  69. }
  70.  
  71.  
  72. function getList ($p_mFilter = "", $p_sOrder = "", $p_iLimit = "", $p_aExtras = array()) {
  73.     try {
  74.         /* Turn the filters into a string (if not already) */
  75.         if (is_array($p_mFilter) && count($p_mFilter) > 0) {
  76.             $sFilter = $this->_filtersToStr($p_mFilter);
  77.         } elseif(is_string($p_mFilter) && $p_mFilter != '') {
  78.             $sFilter = 'WHERE ' . $p_mFilter;
  79.         } else {
  80.             $sFilter = '';
  81.         }
  82.     //  die($sFilter);
  83.         $sOrder = (is_array($p_sOrder) && count($p_sOrder) > 0) ? 'ORDER BY ' : '';
  84.         if($sOrder == '') {
  85.             $sOrder = ($p_sOrder != '') ? 'ORDER BY ' : '';
  86.         }
  87.         foreach ((array) $p_sOrder as $key => $val) {
  88.             $sOrder .= $val.',';
  89.         }
  90.         $sOrder = substr($sOrder, 0, -1);
  91.         $p_iLimit = ($p_iLimit != '') ? 'LIMIT '.$p_iLimit : '';
  92.         /* Lets get the information from extras array */
  93.         $sTableName = (count($p_aExtras) > 0) ? $this->findTableName($p_aExtras) : $this->sTableName;
  94.         $sQuery     = "SELECT * FROM $sTableName $sFilter $sOrder $p_iLimit";
  95.         $this->_aQueries[] = $sQuery;
  96.         //die($sQuery);
  97.         $oResult    = $this->rHandler->prepare($sQuery);
  98.         $oResult->execute();
  99.         return $oResult->fetchAll($this->sFetchMode);
  100.     } catch(PDOException $e) {
  101.         $this->throwException($e->getMessage());
  102.     } catch(PPI_Exception $e) {
  103.         $this->throwException($e->getMessage());
  104.     }
  105. }
  106.  
  107.  
  108. function find($search) {
  109.     $oResult = $this->rHandler->prepare('SELECT * FROM '.$this->sTableName. ' WHERE ' . $this->sTableIndex . ' = ' . $this->quote($search));
  110.     $oResult->execute();
  111.     return $oResult->fetch($this->sFetchMode);
  112. }
  113.  
  114.  
  115. function getAllDBs() {
  116.     $newdbs = array();
  117.     $dbs = mysql_list_dbs($this->rHandler);
  118.     print_r($db = mysql_fetch_object($dbs)); exit;
  119.     while($db = mysql_fetch_object($dbs)) {
  120.         array_push($newdbs, $db->Database);
  121.     }
  122.     return $newdbs;
  123. }
  124.  
  125. function dropTable($p_sDBName = "", $p_sTableName = "") {
  126.     if(strlen($p_sDBName) > 0 && strlen($p_sDBName) > 0) {
  127.         mysql_select_db($p_sDBName, $this->rHandler);
  128.         /* Drop the table */
  129.         if($this->query("DROP TABLE $p_sTableName IF EXISTS")) {
  130.             return true;
  131.         } else {
  132.             return false;
  133.         }
  134.     }
  135. }
  136.  
  137. function addColumn($p_sDBName = "", $p_sTableName = "", $sFieldProperties = "") {
  138.     if(strlen($p_sDBName) > 0 && strlen($p_sTableName) > 0  && strlen($sFieldProperties) > 0) {
  139.         mysql_select_db($p_sDBName, $this->rHandler);
  140.         if($this->query("ALTER TABLE $p_sTableName ADD COLUMN $sFieldProperties")) {
  141.             return true;
  142.         } else {
  143.             return false;
  144.         }
  145.     }
  146. }
  147.  
  148. function delRecord($p_sTableName = "", $p_sRecordName = "", $p_iRecordID = "") {
  149.     if(strlen($p_sTableName) > 0 && strlen($p_sRecordName) > 0 && strlen($p_iRecordID) > 0) {
  150.         $sTableName = $this->findTableName(array('sTableName' => $p_sTableName));
  151.         $oResult = $this->rHandler->prepare("DELETE FROM $sTableName WHERE $p_sRecordName = ".$this->quote($p_iRecordID));
  152.         $oResult->execute();
  153.         return $oResult->fetch($this->sFetchMode);
  154.     }
  155. }
  156.  
  157. /* return the connection handler generated from mysql_connect() */
  158. function getHandler() { return $this->rHandler; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement