Changeset 319 for trunk/database.php


Ignore:
Timestamp:
Nov 27, 2011, 2:26:59 PM (13 years ago)
Author:
chronos
Message:
  • Upraveno: Knihovna pro práci s databází přepsána z využití třídy mysql na obecnější PDO.
  • Přidáno: Soubory projektu prostředí NetBeans.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/database.php

    r292 r319  
    22
    33// Extended database class
    4 // Date: 2009-02-16
     4// Date: 2011-11-25
    55
    66
    7 class Database extends mysqli
     7class DatabaseResult
     8{
     9  var $PDOStatement;
     10  var $num_rows = 0;
     11 
     12  function fetch_assoc()
     13  {
     14    return($this->PDOStatement->fetch());
     15  }
     16 
     17  function fetch_array()
     18  {
     19    return($this->PDOStatement->fetch());
     20  }
     21
     22  function fetch_row()
     23  {
     24    return($this->PDOStatement->fetch());
     25  }
     26}
     27
     28class Database
    829{
    930  var $Prefix = '';
    1031  var $Functions = array('NOW()', 'CURDATE()', 'CURTIME()', 'UUID()');
     32  var $Type = 'mysql';  // mysql, pgsql
     33  var $PDO;
     34  var $Error = '';
     35 
     36  function __construct($Host, $User, $Password, $Database)
     37  {   
     38    if($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database;
     39      else if($this->Type == 'pgsql') $ConnectionString = 'pgsql:dbname='.$Database.';host='.$Host;
     40      else $ConnectionString = '';
     41    $this->PDO = new PDO($ConnectionString, $User, $Password);
     42  }
     43 
     44  function select_db($Database)
     45  {
     46    $this->query('USE '.$Database);
     47  }
    1148 
    1249  function query($Query)
    1350  {
    14           global $Config;
     51    global $Config;
    1552       
    16           if($Config['Web']['ShowSQLQuery'] == true)
    17             echo('<div style="border-bottom-width: 1px; border-bottom-style: solid; padding-bottom: 3px; padding-top: 3px; font-size: 12px; font-family: Arial;">'.$Query.'</div>'."\n");
    18           $Result = parent::query($Query);
    19     if(($this->error != '') and ($Config['Web']['ShowSQLError'] == true))
    20           echo('<div><strong>SQL Error: </strong>'.$this->error.'<br />'.$Query.'</div>');
     53    if($Config['Web']['ShowSQLQuery'] == true)
     54      echo('<div style="border-bottom-width: 1px; border-bottom-style: solid; padding-bottom: 3px; padding-top: 3px; font-size: 12px; font-family: Arial;">'.$Query.'</div>'."\n");
     55    $Result = new DatabaseResult();
     56    $Result->PDOStatement = $this->PDO->query($Query);
     57    $Result->num_rows = $Result->PDOStatement->rowCount();
     58    if(($this->Error != '') and ($Config['Web']['ShowSQLError'] == true))
     59      echo('<div><strong>SQL Error: </strong>'.$this->Error.'<br />'.$Query.'</div>');
    2160
    2261    return($Result); 
     
    2463
    2564  function select($Table, $What = '*', $Condition = 1)
    26   {
     65  {   
    2766    return($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition)); 
    2867  }
     
    3069  function delete($Table, $Condition)
    3170  {
    32     $this->query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition); 
     71    $this->PDO->query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition); 
    3372  }
    3473 
     
    4079    {
    4180      $Name .= ',`'.$Key.'`';
    42             if(!in_array($Value, $this->Functions)) $Value = '"'.$this->real_escape_string($Value).'"';
     81      if(!in_array($Value, $this->Functions)) $Value = $this->PDO->quote($Value);
    4382      $Values .= ','.$Value;
    4483    }
    4584    $Name = substr($Name, 1);
    4685    $Values = substr($Values, 1);
    47     $this->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
     86    $this->PDO->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
    4887  }
    4988 
     
    5392    foreach($Data as $Key => $Value)
    5493    {
    55             if(!in_array($Value, $this->Functions)) $Value = '"'.$this->real_escape_string($Value).'"';
     94            if(!in_array($Value, $this->Functions)) $Value = $this->PDO->quote($Value);
    5695      $Values .= ', `'.$Key.'`='.$Value;
    5796    }
    5897    $Values = substr($Values, 2); 
    59     $this->query('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');
     98    $this->PDO->query('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');
    6099  }
    61100 
     
    66105    foreach($Data as $Key => $Value)
    67106    {
    68             if(!in_array($Value, $this->Functions)) $Value = '"'.$this->real_escape_string($Value).'"';
     107            if(!in_array($Value, $this->Functions)) $Value = $this->PDO->quote($Value);
    69108      $Name .= ',`'.$Key.'`';
    70109      $Values .= ','.$Value;
     
    73112    $Values = substr($Values, 1);
    74113    //echo('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES ('.$Values.')<br />');
    75     $this->query('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
     114    $this->PDO->query('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
    76115    //echo($this->error().'<br>');
    77116  }
     
    79118  function charset($Charset)
    80119  {
    81     $this->query('SET NAMES "'.$Charset.'"');
     120    $this->PDO->query('SET NAMES "'.$Charset.'"');
    82121  }
     122 
    83123}
    84124
Note: See TracChangeset for help on using the changeset viewer.