0
|
1 <?php |
8
|
2 /* Copyright 2010 Ivo Smits <Ivo@UCIS.nl>. All rights reserved. |
|
3 Redistribution and use in source and binary forms, with or without modification, are |
|
4 permitted provided that the following conditions are met: |
|
5 |
|
6 1. Redistributions of source code must retain the above copyright notice, this list of |
|
7 conditions and the following disclaimer. |
|
8 |
|
9 2. Redistributions in binary form must reproduce the above copyright notice, this list |
|
10 of conditions and the following disclaimer in the documentation and/or other materials |
|
11 provided with the distribution. |
|
12 |
|
13 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
14 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
|
15 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR |
|
16 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
17 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
18 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
|
19 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
20 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|
21 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
22 |
|
23 The views and conclusions contained in the software and documentation are those of the |
|
24 authors and should not be interpreted as representing official policies, either expressed |
|
25 or implied, of Ivo Smits.*/ |
|
26 |
0
|
27 class PDOWrapper { |
|
28 private $numQueries = 0; |
|
29 private $pdo = NULL; |
|
30 |
|
31 public static function connectMysql($host, $database, $user = NULL, $pass = NULL, $initialcommand = NULL) { |
|
32 $options = array(); |
|
33 if ($initialcommand !== NULL) $options[PDO::MYSQL_ATTR_INIT_COMMAND] = $initialcommand; |
|
34 return new self('mysql:dbname='.$database.';host='.$host, $user, $pass, $options); |
|
35 //"SET NAMES utf8,time_zone = '+0:00'" |
|
36 } |
|
37 |
|
38 public function __construct($dsn, $user = NULL, $pass = NULL, $options = NULL) { |
|
39 try { |
|
40 $this->pdo = new PDO($dsn, $user, $pass, $options); |
|
41 } catch (PDOException $e) { |
|
42 throw new Exception('Could not connect to database: '.$e->getMessage()); |
|
43 } |
|
44 $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
45 } |
|
46 public function __call($name, $arguments) { |
|
47 if ($name == 'eval') { |
|
48 $q = array_shift($arguments); |
|
49 return $this->evalField($q, $arguments); |
|
50 } |
|
51 } |
|
52 |
|
53 private function queryi($query, $args = NULL) { |
|
54 $this->numQueries++; |
|
55 if ($args === NULL) { |
|
56 //$stmt = $db_pdo->query($query); |
|
57 $stmt = $this->pdo->prepare($query); |
|
58 $stmt->execute(); |
|
59 } else { |
|
60 if (!is_array($args)) $args = array($args); |
|
61 $stmt = $this->pdo->prepare($query); |
|
62 $stmt->execute($args); |
|
63 } |
|
64 return $stmt; |
|
65 } |
|
66 function insert($query, $args = NULL) { |
|
67 $this->queryi($query, $args)->closeCursor(); |
|
68 return $this->pdo->lastInsertId(); |
|
69 } |
|
70 function update($query, $args = NULL) { |
|
71 global $db_pdo; |
|
72 $stmt = $this->queryi($query, $args); |
|
73 $cnt = $stmt->rowCount(); |
|
74 $stmt->closeCursor(); |
|
75 return $cnt; |
|
76 } |
|
77 function evalField($query, $args = NULL) { |
|
78 $stmt = $this->queryi($query, $args); |
|
79 $ret = $stmt->fetchColumn(); |
|
80 $stmt->closeCursor(); |
|
81 return $ret; |
|
82 } |
|
83 function evalRow($query, $args = NULL) { |
|
84 $stmt = $this->queryi($query, $args); |
|
85 $ret = $stmt->fetch(PDO::FETCH_NUM); |
|
86 $stmt->closeCursor(); |
|
87 return $ret; |
|
88 } |
|
89 function evalRowAssoc($query, $args = NULL) { |
|
90 $stmt = $this->queryi($query, $args); |
|
91 $ret = $stmt->fetch(PDO::FETCH_ASSOC); |
|
92 $stmt->closeCursor(); |
|
93 return $ret; |
|
94 } |
|
95 function evalAllAssoc($query, $args = NULL) { |
|
96 $stmt = $this->queryi($query, $args); |
|
97 $ret = $stmt->fetchAll(PDO::FETCH_ASSOC); |
|
98 $stmt->closeCursor(); |
|
99 return $ret; |
|
100 } |
|
101 function evalAllKVP($query, $args = NULL) { |
|
102 $stmt = $this->queryi($query, $args); |
|
103 $ret = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); |
|
104 $stmt->closeCursor(); |
|
105 return $ret; |
|
106 } |
|
107 function evalColumn($query, $args = NULL) { |
|
108 $stmt = $this->queryi($query, $args); |
|
109 $ret = $stmt->fetchAll(PDO::FETCH_COLUMN); |
|
110 $stmt->closeCursor(); |
|
111 return $ret; |
|
112 } |
|
113 } |