Rev 250 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 250 | Rev 257 | ||
---|---|---|---|
Line 18... | Line 18... | ||
18 | */ |
18 | */ |
19 | 19 | ||
20 | if (!defined('IN_OIDPLUS')) die(); |
20 | if (!defined('IN_OIDPLUS')) die(); |
21 | 21 | ||
22 | class OIDplusDatabasePluginPDO extends OIDplusDatabasePlugin { |
22 | class OIDplusDatabasePluginPDO extends OIDplusDatabasePlugin { |
23 | private $pdo; |
23 | private $conn; |
24 | private $last_query; |
- | |
25 | 24 | ||
26 | public static function getPluginInformation(): array { |
25 | public static function getPluginInformation(): array { |
27 | $out = array(); |
26 | $out = array(); |
28 | $out['name'] = 'PDO'; |
27 | $out['name'] = 'PDO'; |
29 | $out['author'] = 'ViaThinkSoft'; |
28 | $out['author'] = 'ViaThinkSoft'; |
Line 37... | Line 36... | ||
37 | } |
36 | } |
38 | 37 | ||
39 | private $last_error = null; |
38 | private $last_error = null; |
40 | 39 | ||
41 | public function query(string $sql, /*?array*/ $prepared_args=null): OIDplusQueryResult { |
40 | public function query(string $sql, /*?array*/ $prepared_args=null): OIDplusQueryResult { |
42 | $this->last_query = $sql; |
- | |
43 | $this->last_error = null; |
41 | $this->last_error = null; |
44 | if (is_null($prepared_args)) { |
42 | if (is_null($prepared_args)) { |
45 | $res = $this->pdo->query($sql); |
43 | $res = $this->conn->query($sql); |
46 | 44 | ||
47 | if ($res === false) { |
45 | if ($res === false) { |
48 | $this->last_error = $this->pdo->errorInfo()[2]; |
46 | $this->last_error = $this->conn->errorInfo()[2]; |
49 | throw new OIDplusSQLException($sql, $this->error()); |
47 | throw new OIDplusSQLException($sql, $this->error()); |
50 | } else { |
48 | } else { |
51 | return new OIDplusQueryResultPDO($res); |
49 | return new OIDplusQueryResultPDO($res); |
52 | } |
50 | } |
53 | } else { |
51 | } else { |
Line 59... | Line 57... | ||
59 | $pos = strpos($sql, $needle); |
57 | $pos = strpos($sql, $needle); |
60 | if ($pos !== false) { |
58 | if ($pos !== false) { |
61 | $sql = substr_replace($sql, $replace, $pos, strlen($needle)); |
59 | $sql = substr_replace($sql, $replace, $pos, strlen($needle)); |
62 | } |
60 | } |
63 | } |
61 | } |
64 | return OIDplusQueryResultPDO($this->pdo->query($sql)); |
62 | return OIDplusQueryResultPDO($this->conn->query($sql)); |
65 | */ |
63 | */ |
66 | 64 | ||
67 | if (!is_array($prepared_args)) { |
65 | if (!is_array($prepared_args)) { |
68 | throw new OIDplusException("'prepared_args' must be either NULL or an ARRAY."); |
66 | throw new OIDplusException("'prepared_args' must be either NULL or an ARRAY."); |
69 | } |
67 | } |
Line 74... | Line 72... | ||
74 | // https://bugs.php.net/bug.php?id=57157 |
72 | // https://bugs.php.net/bug.php?id=57157 |
75 | // Note: We are using '1' and '0' instead of 'true' and 'false' because MySQL converts boolean to tinyint(1) |
73 | // Note: We are using '1' and '0' instead of 'true' and 'false' because MySQL converts boolean to tinyint(1) |
76 | if (is_bool($value)) $value = $value ? '1' : '0'; |
74 | if (is_bool($value)) $value = $value ? '1' : '0'; |
77 | } |
75 | } |
78 | 76 | ||
79 | $ps = $this->pdo->prepare($sql); |
77 | $ps = $this->conn->prepare($sql); |
80 | if (!$ps) { |
78 | if (!$ps) { |
81 | throw new OIDplusSQLException($sql, 'Cannot prepare statement'); |
79 | throw new OIDplusSQLException($sql, 'Cannot prepare statement'); |
82 | } |
80 | } |
83 | $this->prepare_cache[$sql] = $ps; |
81 | $this->prepare_cache[$sql] = $ps; |
84 | 82 | ||
Line 89... | Line 87... | ||
89 | return new OIDplusQueryResultPDO($ps); |
87 | return new OIDplusQueryResultPDO($ps); |
90 | } |
88 | } |
91 | } |
89 | } |
92 | 90 | ||
93 | public function insert_id(): int { |
91 | public function insert_id(): int { |
94 | return $this->pdo->lastInsertId(); |
92 | return $this->conn->lastInsertId(); |
95 | } |
93 | } |
96 | 94 | ||
97 | public function error(): string { |
95 | public function error(): string { |
98 | $err = $this->last_error; |
96 | $err = $this->last_error; |
99 | if ($err == null) $err = ''; |
97 | if ($err == null) $err = ''; |
Line 107... | Line 105... | ||
107 | PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, |
105 | PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, |
108 | PDO::ATTR_EMULATE_PREPARES => true, |
106 | PDO::ATTR_EMULATE_PREPARES => true, |
109 | ]; |
107 | ]; |
110 | 108 | ||
111 | // Try connecting to the database |
109 | // Try connecting to the database |
112 | $this->pdo = new PDO(OIDPLUS_PDO_DSN, OIDPLUS_PDO_USERNAME, base64_decode(OIDPLUS_PDO_PASSWORD), $options); |
110 | $this->conn = new PDO(OIDPLUS_PDO_DSN, OIDPLUS_PDO_USERNAME, base64_decode(OIDPLUS_PDO_PASSWORD), $options); |
113 | } catch (PDOException $e) { |
111 | } catch (PDOException $e) { |
114 | $message = $e->getMessage(); |
112 | $message = $e->getMessage(); |
115 | throw new OIDplusConfigInitializationException('Connection to the database failed! '.$message); |
113 | throw new OIDplusConfigInitializationException('Connection to the database failed! '.$message); |
116 | } |
114 | } |
117 | 115 | ||
118 | $this->query("SET NAMES 'utf8'"); |
116 | $this->query("SET NAMES 'utf8'"); |
119 | } |
117 | } |
120 | 118 | ||
121 | protected function doDisconnect(): void { |
119 | protected function doDisconnect(): void { |
122 | $this->pdo = null; // the connection will be closed by removing the reference |
120 | $this->conn = null; // the connection will be closed by removing the reference |
123 | } |
121 | } |
124 | 122 | ||
125 | private $intransaction = false; |
123 | private $intransaction = false; |
126 | 124 | ||
127 | public function transaction_begin(): void { |
125 | public function transaction_begin(): void { |
128 | if ($this->intransaction) throw new OIDplusException("Nested transactions are not supported by this database plugin."); |
126 | if ($this->intransaction) throw new OIDplusException("Nested transactions are not supported by this database plugin."); |
129 | $this->pdo->beginTransaction(); |
127 | $this->conn->beginTransaction(); |
130 | $this->intransaction = true; |
128 | $this->intransaction = true; |
131 | } |
129 | } |
132 | 130 | ||
133 | public function transaction_commit(): void { |
131 | public function transaction_commit(): void { |
134 | $this->pdo->commit(); |
132 | $this->conn->commit(); |
135 | $this->intransaction = false; |
133 | $this->intransaction = false; |
136 | } |
134 | } |
137 | 135 | ||
138 | public function transaction_rollback(): void { |
136 | public function transaction_rollback(): void { |
139 | $this->pdo->rollBack(); |
137 | $this->conn->rollBack(); |
140 | $this->intransaction = false; |
138 | $this->intransaction = false; |
141 | } |
139 | } |
142 | } |
140 | } |
143 | 141 | ||
144 | class OIDplusQueryResultPDO extends OIDplusQueryResult { |
142 | class OIDplusQueryResultPDO extends OIDplusQueryResult { |