Advertisement
Guest User

En's Assignment

a guest
Jul 17th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.63 KB | None | 0 0
  1. <?php
  2. class Db_DataBase
  3. {
  4.     public $handle = null;
  5.     private static $instance = NULL;
  6.     protected $_bebug;
  7.     private $count_query = 0;
  8.    
  9.     static public function instance()
  10.     {
  11.         if (self::$instance == NULL)
  12.         {
  13.             self::$instance = new self();
  14.         }
  15.         return self::$instance;
  16.     }
  17.    
  18.     private function __construct() {}
  19.    
  20.     protected function connect()
  21.     {
  22.         if ($this->handle) return $this->handle;
  23.        
  24.         $config = Registry::get('db_config');
  25.         $this->_bebug = $config['debug'];
  26.         if ($this->_bebug == 2)
  27.             self::DebugLog('[Start connect ' . date('d.m.Y H:i:s') . ' ]');
  28.            
  29.         $this->handle = mysql_pconnect($config['host'], $config['user'], $config['password']);
  30.         $this->sql('SET NAMES utf8');
  31.         mysql_select_db($config['db_name'], $this->handle);
  32.        
  33.         return $this->handle;
  34.     }
  35.    
  36.     public function close()
  37.     {
  38.         if ($this->_bebug == 2)
  39.             self::DebugLog('[end connect ' . date('d.m.Y H:i:s') . ' ]' . "\n\n");
  40.         //mysql_close($this->handle);
  41.     }
  42.    
  43.     public function __destruct()
  44.     {
  45.         if ($this->_bebug == 1) {
  46.             echo "<br/><b>Всего {$this->count_query} запросов.</b><br/>";
  47.             $this->count_query = 0;
  48.         }
  49.        
  50. //      $this->close();
  51.     }
  52.    
  53.     public function getLastId()
  54.     {
  55.         return mysql_insert_id($this->connect()) ? mysql_insert_id($this->connect()) : - 1;
  56.     }
  57.    
  58.     public function quot( $value )
  59.     {
  60.         /*if (is_numeric($value) && !is_float($value)) {
  61.             $value = (int)$value;
  62.         } else {*/
  63.             if (is_array($value))
  64.                 foreach (array_keys($value) as $k)
  65.                     $value[$k] = "'" . mysql_real_escape_string($value[$k], $this->connect()) . "'";
  66.             else
  67.                 $value = "'" . mysql_real_escape_string($value, $this->connect()) . "'";
  68.         /*}*/
  69.         return $value;
  70.     }
  71.    
  72.     public function getErrorNumber()
  73.     {
  74.         return mysql_errno($this->connect());
  75.     }
  76.    
  77.     public function sql( $query )
  78.     {
  79.         if ($this->_bebug == 1) {
  80.             $this->count_query++;
  81.             echo "<b>{$this->count_query}:</b> {$query}<br />";
  82.         }
  83.         if ($this->_bebug == 2)
  84.             self::DebugLog($query);
  85.         return mysql_query($query, $this->connect());
  86.     }
  87.    
  88.     public function sqlParse( $query )
  89.     {
  90.         $res = $this->sql($query);
  91.         if ($res)
  92.         {
  93.             while($row = mysql_fetch_assoc($res))
  94.                 $qresult[] = $row;
  95.             mysql_free_result($res);
  96.         }
  97.        
  98.         return isset($qresult) ? $qresult : null;
  99.     }
  100.    
  101.     public function insert( $table, $items )
  102.     {
  103.         $f_sql = $v_sql = array ();
  104.        
  105.         foreach ($items as $f => $v) {
  106.             $f_sql[] = '`' . $f . '`';
  107.             $v_sql[] = $v;
  108.         }
  109.         $query = 'INSERT INTO `' . $table . '` (' . implode(',', $f_sql) . ') VALUES (' . implode(',', $v_sql) . ')';      
  110.         $this->sql($query);
  111.         return $this->getLastId();
  112.     }
  113.    
  114.     public function replace( $table, $items )
  115.     {
  116.         $f_sql = $v_sql = array ();
  117.         foreach ($items as $f => $v)
  118.         {
  119.             $f_sql[] = '`' . $f . '`';
  120.             $v_sql[] = $v;
  121.         }
  122.         $query = 'REPLACE INTO `' . $table . '` (' . implode(',', $f_sql) . ') VALUES (' . implode(',', $v_sql) . ')';
  123.         $this->sql($query);
  124.         return $this->getLastId();
  125.     }
  126.    
  127.     public function update( $table, $items, $where )
  128.     {
  129.         $sql = array ();
  130.         foreach ($items as $f => $v)
  131.             $sql[] = '`' . $f . '`=' . $v;
  132.         $query = 'UPDATE `' . $table . '` SET ' . implode(',', $sql);
  133.         if (! empty($where))
  134.             $query .= ' WHERE ' . $where;
  135.         return $this->sql($query);
  136.     }
  137.    
  138.     public function delete( $table, $where )
  139.     {
  140.         $query = 'DELETE FROM `' . $table . '` WHERE ' . $where;
  141.         return $this->sql($query);
  142.     }
  143.    
  144.     private static function DebugLog( $str )
  145.     {
  146.         $file = ROOT_DIR . 'sql_tmp_' . date('d_m_Y') . '.txt';
  147.         $handle = @fopen($file, 'a+');
  148.         @fwrite($handle, $str . "\n");
  149.         @fclose($handle);
  150.     }
  151. }
  152. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement