Rev 863 | Rev 1086 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 863 | Rev 1050 | ||
---|---|---|---|
Line 15... | Line 15... | ||
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | * See the License for the specific language governing permissions and |
16 | * See the License for the specific language governing permissions and |
17 | * limitations under the License. |
17 | * limitations under the License. |
18 | */ |
18 | */ |
19 | 19 | ||
20 | if (!defined('INSIDE_OIDPLUS')) die(); |
20 | namespace ViaThinkSoft\OIDplus; |
21 | 21 | ||
22 | class OIDplusDatabaseConnectionPDO extends OIDplusDatabaseConnection { |
22 | class OIDplusDatabaseConnectionPDO extends OIDplusDatabaseConnection { |
23 | private $conn = null; |
23 | private $conn = null; |
24 | private $last_error = null; // we need that because PDO divides prepared statement errors and normal query errors, but we have only one "error()" method |
24 | private $last_error = null; // we need that because PDO divides prepared statement errors and normal query errors, but we have only one "error()" method |
25 | private $transactions_supported = false; |
25 | private $transactions_supported = false; |
Line 72... | Line 72... | ||
72 | public function insert_id(): int { |
72 | public function insert_id(): int { |
73 | try { |
73 | try { |
74 | $out = @($this->conn->lastInsertId()); |
74 | $out = @($this->conn->lastInsertId()); |
75 | if ($out === false) return parent::insert_id(); // fallback method that uses the SQL slang |
75 | if ($out === false) return parent::insert_id(); // fallback method that uses the SQL slang |
76 | return $out; |
76 | return $out; |
77 | } catch (Exception $e) { |
77 | } catch (\Exception $e) { |
78 | return parent::insert_id(); // fallback method that uses the SQL slang |
78 | return parent::insert_id(); // fallback method that uses the SQL slang |
79 | } |
79 | } |
80 | } |
80 | } |
81 | 81 | ||
82 | public function error(): string { |
82 | public function error(): string { |
Line 88... | Line 88... | ||
88 | protected function doConnect()/*: void*/ { |
88 | protected function doConnect()/*: void*/ { |
89 | if (!class_exists('PDO')) throw new OIDplusConfigInitializationException(_L('PHP extension "%1" not installed','PDO')); |
89 | if (!class_exists('PDO')) throw new OIDplusConfigInitializationException(_L('PHP extension "%1" not installed','PDO')); |
90 | 90 | ||
91 | try { |
91 | try { |
92 | $options = [ |
92 | $options = [ |
93 | PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT, |
93 | \PDO::ATTR_ERRMODE => \PDO::ERRMODE_SILENT, |
94 | PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, |
94 | \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, |
95 | PDO::ATTR_EMULATE_PREPARES => true, |
95 | \PDO::ATTR_EMULATE_PREPARES => true, |
96 | ]; |
96 | ]; |
97 | 97 | ||
98 | // Try connecting to the database |
98 | // Try connecting to the database |
99 | $dsn = OIDplus::baseConfig()->getValue('PDO_DSN', 'mysql:host=localhost;dbname=oidplus;charset=UTF8'); |
99 | $dsn = OIDplus::baseConfig()->getValue('PDO_DSN', 'mysql:host=localhost;dbname=oidplus;charset=UTF8'); |
100 | $username = OIDplus::baseConfig()->getValue('PDO_USERNAME', 'root'); |
100 | $username = OIDplus::baseConfig()->getValue('PDO_USERNAME', 'root'); |
101 | $password = OIDplus::baseConfig()->getValue('PDO_PASSWORD', ''); |
101 | $password = OIDplus::baseConfig()->getValue('PDO_PASSWORD', ''); |
102 | 102 | ||
103 | if (stripos($dsn,"charset=") === false) $dsn = "$dsn;charset=UTF8"; |
103 | if (stripos($dsn,"charset=") === false) $dsn = "$dsn;charset=UTF8"; |
104 | 104 | ||
105 | $this->conn = new PDO($dsn, $username, $password, $options); |
105 | $this->conn = new \PDO($dsn, $username, $password, $options); |
106 | } catch (PDOException $e) { |
106 | } catch (\PDOException $e) { |
107 | $message = $e->getMessage(); |
107 | $message = $e->getMessage(); |
108 | throw new OIDplusConfigInitializationException(trim(_L('Connection to the database failed!').' '.$message)); |
108 | throw new OIDplusConfigInitializationException(trim(_L('Connection to the database failed!').' '.$message)); |
109 | } |
109 | } |
110 | 110 | ||
111 | $this->last_error = null; |
111 | $this->last_error = null; |
112 | 112 | ||
113 | try { |
113 | try { |
114 | @$this->conn->exec("SET NAMES 'utf8'"); |
114 | @$this->conn->exec("SET NAMES 'utf8'"); |
115 | } catch (Exception $e) { |
115 | } catch (\Exception $e) { |
116 | } |
116 | } |
117 | 117 | ||
118 | // We check if the DBMS supports autocommit. |
118 | // We check if the DBMS supports autocommit. |
119 | // Attention: Check it after you have sent a query already, because Microsoft Access doesn't seem to allow |
119 | // Attention: Check it after you have sent a query already, because Microsoft Access doesn't seem to allow |
120 | // changing auto commit once a query was executed ("Attribute cannot be set now SQLState: S1011") |
120 | // changing auto commit once a query was executed ("Attribute cannot be set now SQLState: S1011") |
Line 124... | Line 124... | ||
124 | $dummy = $this->conn->query($sql); |
124 | $dummy = $this->conn->query($sql); |
125 | try { |
125 | try { |
126 | $this->conn->beginTransaction(); |
126 | $this->conn->beginTransaction(); |
127 | $this->conn->rollBack(); |
127 | $this->conn->rollBack(); |
128 | $this->transactions_supported = true; |
128 | $this->transactions_supported = true; |
129 | } catch (Exception $e) { |
129 | } catch (\Exception $e) { |
130 | $this->transactions_supported = false; |
130 | $this->transactions_supported = false; |
131 | } |
131 | } |
132 | } |
132 | } |
133 | 133 | ||
134 | protected function doDisconnect()/*: void*/ { |
134 | protected function doDisconnect()/*: void*/ { |