Advertisement
terorama

WP / inc / wpdb / dbmodel.inc.php

May 29th, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.15 KB | None | 0 0
  1. <?php
  2.  
  3. //----------------------------------------------
  4. //                  DBModel
  5. //----------------------------------------------
  6. class DBModel implements IDBModel, IInjectable {
  7.  
  8.    private $_debug;
  9.    private $_db;
  10.  
  11.    //---------------------------- debug information
  12.    private function _w($info, $method) {
  13.    
  14.       $className = get_class($this);
  15.       $this->_debug->writeInfo($info, $className, $method);
  16.    }
  17.    //------------------------------ constructor
  18.    
  19.    public function __construct($injector) {
  20.  
  21.       $this->_db = $injector->get('IDBConn')->get();
  22.       $this->_debug = $injector->get('IDebug');
  23.    }
  24.  
  25.    //------------------------------ check errors
  26.    
  27.    private function checkResult($result,  $sql='') {
  28.    
  29.       if ($result===false)
  30.          throw new Exception ('db error <br/>sql: '.$sql, 30044);
  31.    }
  32.    
  33.    //------------------------------ get full table name
  34.    
  35.    public function getTableName($tabname) {
  36.       return $this->_db->prefix.'dkrss_'.$tabname;
  37.    }
  38.    
  39.    //------------------------------ get single record from model table
  40.    
  41.    public function getRecordById($model, $id) {
  42.    
  43.            
  44.        $sql =  'select * from '.$model->_table.
  45.        ' where '.$model->_idfield.'=%d';
  46.                                          
  47.        $result = $this->_db->get_row($this->_db->prepare($sql,$id), ARRAY_A);
  48.        $this->checkResult($result,  $sql);
  49.    
  50.        return array_map('stripslashes',$result);
  51.     }
  52.  
  53.    //------------------------------ get N records from model table
  54.    
  55.    public function getNRecords($model, $start, $num) {
  56.      
  57.      
  58.       $sql =  'select * from '.$model->_table.
  59.                ($model->_filterstr ? ' where ':'').$model->_filterstr.
  60.               ' order by '.$model->_orderstr.' LIMIT '.$start.','.$num;
  61.            
  62.       $result = $this->_db->get_results($sql, ARRAY_A);
  63.      
  64.       $this->checkResult($result, $sql);
  65.       foreach ($result as $key=>$value) {
  66.          $result[$key] = array_map('stripslashes', $result[$key]);
  67.       }
  68.    
  69.       return $result;
  70.    }
  71.  
  72.    //------------------------------ get number of records in model table
  73.    
  74.   public function getNumRecs($model) {
  75.    
  76.       $sql = 'select count(*) from '.$model->_table;
  77.       $result = $this->_db->get_var($sql);
  78.      
  79.       $this->checkResult($result,  $sql);
  80.    
  81.       return $result;
  82.      
  83.    }
  84.    
  85.    //------------------------------ inserting and updating records
  86.    
  87.    private function processRecord($model, $type='update',$id,$values) {
  88.      
  89.      
  90.       $pairs = array();
  91.       $types = array();
  92.      
  93.       $func = create_function ('$a', 'return $a["fn"];');
  94.      
  95.       $sarr = array_map($func, $model->fields);
  96.      
  97.      
  98.       foreach ($values as $key=>$value) {
  99.      
  100.          $pairs[$key] = $value;
  101.          $inf = $model->fields[array_search($key, $sarr)]['ft'];
  102.          $types[] = '%'.($inf=='i' ? 'd' : $inf);
  103.       }
  104.      
  105.       if ($type=='insert') {
  106.      
  107.          $rez = $this->_db->insert( $model->_table, $pairs, $types);
  108.          $this->checkResult($rez, '');
  109.        
  110.          }
  111.       else {
  112.          
  113.          $rez = $this->_db->update(
  114.          
  115.             $model->_table,
  116.             $pairs,   array($model->_idfield=>$id),
  117.             $types, array('%d'));
  118.            
  119.             $this->checkResult($rez, '');
  120.  
  121.       }
  122.      
  123.       $rid = ($type=='insert') ? $this->_db->insert_id : $rez ;
  124.      
  125.      
  126.       return($rid);
  127.  
  128.    }
  129.    //-------------------------------------- insert record  
  130.    public function insertRecord($model, $values) {
  131.    
  132.       $rid = $this->processRecord ($model, 'insert', 0, $values);    
  133.       return array('message'=>'record successfully inserted', 'rid'=>$rid);
  134.    }
  135.    
  136.    //-------------------------------------- update record
  137.    
  138.    public function updateRecord($model, $id, $values) {
  139.    
  140.       $affected_rows = $this->processRecord($model, 'update', $id, $values);
  141.       return 'record successfully updated';
  142.    }
  143.    
  144.    //--------------------------------------- delete record
  145.    public function deleteRecord($model, $id) {
  146.    
  147.       $sql = 'delete from '.$model->_table.
  148.       ' where '.$model->_idfield.'=%d';
  149.      
  150.       $rez = $this->_db->query($this->_db->prepare($sql, $id));
  151.  
  152.       $this->checkResult($rez, $sql);
  153.      
  154.      
  155.       return ('record successfully deleted. affected rows: '.$rez);
  156.    }
  157. }
  158.  
  159. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement