pdowrapper.php

Gesponsorde koppelingen

PHP script bestanden

  1. pdowrapper.php
  2. dbwrappersample.php

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php

/**
 * PHP PDO Wrapper for multiple databases.
 *
 * PHP version 5.5+
 *
 * LICENSE:
 * Feel free to use this code however you please.
 * I only ask to leave this comment as it is.
 * Also, I'd appreciate a message if you use it
 * and or have suggestions to improve it.
 *
 * @category   Database Class
 * @author     Johan Kruse <johan dot kruse at gmail>
 * @version    0.1
 */

 
  class DatabaseException extends Exception{}
  
  abstract class Database extends PDO{
    public $sth;
    
    abstract protected function sqlTableExists();
    abstract protected function sqlSelectDatabase();
    
    private function getDSN($driver, $multiarg){
      return "$driver:" . implode(';', array_map(function($v, $k){return "$k=$v";}, $multiarg, array_keys($multiarg)));
    }

    
    private function getParamType($type){
      switch($type){
        case
'string':   return parent::PARAM_STR;
        case
'integer':  return parent::PARAM_INT;
        case
'boolean':  return parent::PARAM_BOOL;
        case
'NULL':     return parent::PARAM_NULL;
        case
'double':   return parent::PARAM_STR;
        case
'resource': return parent::PARAM_LOB;
        default:

          throw new DatabaseException("Unsupported variable type: '$type' parsed in query.");
      }
    }

    
    public function prepare($sql, $options = array()){
      $this->sth = parent::prepare($sql, $options);
      return $this;
    }

    public function query($stmt){
      return $this->sth = parent::query($stmt);  
    }

      
    public function execute(){
      for($i = 0; $i < func_num_args(); $i++){
        if(($type = gettype(($var = func_get_arg($i)))) == 'array'){
          foreach($var as $key => $val){
            if($type = (gettype($key) == 'string')){
              $this->sth->bindValue(($key[0] != ':') ? ":$key" : $key, $val, $this->getParamType($type));
            }
else {
              throw new DatabaseException("Unsupported array index, expected named parameter.");
            }            
          }
        }
else {
          $this->sth->bindValue($i+1, $var, $this->getParamType($type));
        }
      }

      
      $this->sth->execute();
      return $this->sth;
    }


    public function fetchAssoc(){
      return $this->sth->fetch(PDO::FETCH_ASSOC);
    }

    public function fetchBoth(){
      return $this->sth->fetch(PDO::FETCH_BOTH);
    }

    public function fetchNum(){
      return $this->sth->fetch(PDO::FETCH_NUM);
    }

    public function fetchObj(){
      return $this->sth->fetch(PDO::FETCH_OBJ);
    }

    public function fetchLazy(){
      return $this->sth->fetch(PDO::FETCH_LAZY);
    }

      
    public function tableExists($name){
      return $this->prepare($this->sqlTableExists())->execute($name)->rowCount() > 0;
    }

    
    public function selectDatabase($name){
      return $this->prepare($this->sqlSelectDatabase())->execute($name);
    }
          
    function
__construct($username, $password, $dsn, $options){
      if(in_array($driver = strtolower(get_class($this)), parent::getAvailableDrivers())){
        $dsn = gettype($dsn) == 'array' ? $this->getDSN($driver, $dsn) : (string) $dsn;
        
        if(gettype($options) == 'array'){
          $options[parent::ATTR_EMULATE_PREPARES] = true;
          $options[parent::ATTR_ERRMODE] = parent::ERRMODE_EXCEPTION;
          
          try{
            parent::__construct($dsn, $username, $password, $options);
          }
catch(PDOException $e){
            throw new DatabaseException('Could not connect to the database server.', $e);
          }          
        }
else {
          throw new DatabaseException('Expected an array of options.', $e);
        }
      }
else {
        throw new DatabaseException("Database driver '$driver' is not supported on this machine.'");
      }
    }
  }

  
  class MySQL extends Database{  
    protected function sqlTableExists(){
      return 'SHOW TABLES LIKE ?';
    }

    protected function sqlSelectDatabase(){
      return "USE ?";
    }
    
    function
__construct($username, $password, $dsn, $options = array()) {
      parent::__construct($username, $password, $dsn, $options);
    }
  }

  
  class PostgreSQL extends Database{
    function
sqlTableExists(){
      return "SELECT count(*) FROM pg_class WHERE relname=? AND relkind='r'";
    }
    function
sqlSelectDatabase(){
      throw new DatabaseException('PostgrSQL does not support database swapping, create a new link or select from "db.table".');
    }      
    
    function
__construct($username, $password, $dsn, $options = array()) {
      parent::__construct($username, $password, $dsn, $options);
    }
  }

  
?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.