1
+ <?php
2
+ namespace PhpOrm ;
3
+
4
+ class Connection
5
+ {
6
+ private $ configuration ;
7
+
8
+ private $ dbh ;
9
+
10
+ public function __construct (Configuration $ configuration )
11
+ {
12
+ $ this ->configuration = $ configuration ;
13
+ }
14
+
15
+ public function getDbh (): \PDO
16
+ {
17
+ $ this ->connect ();
18
+
19
+ return $ this ->dbh ;
20
+ }
21
+
22
+ public function getDsn (): string
23
+ {
24
+ $ dsn = "" ;
25
+ switch ($ this ->configuration ->getDriver ())
26
+ {
27
+ case 'mysql ' :
28
+ $ dsn = sprintf ('mysql:host=%s;port=%u;dbname=%s;charset=%s ' ,
29
+ $ this ->configuration ->getHost (),
30
+ $ this ->configuration ->getPort (),
31
+ $ this ->configuration ->getDatabase (),
32
+ $ this ->configuration ->getCharset ());
33
+ break ;
34
+ case 'oci ' :
35
+ $ dsn = sprintf ('oci:dbname=%s;charset=%s ' ,
36
+ $ this ->configuration ->getDatabase (),
37
+ $ this ->configuration ->getCharset ());
38
+ break ;
39
+ case 'firebird ' :
40
+ $ dsn = sprintf ('firebird:dbname=%s;charset=%s ' ,
41
+ $ this ->configuration ->getDatabase (),
42
+ $ this ->configuration ->getCharset ());
43
+ break ;
44
+ case 'pgsql ' :
45
+ $ dsn = sprintf ('pgsql:host=%s;port=%u;dbname=%s;user=%s;password=%s ' ,
46
+ $ this ->configuration ->getHost (),
47
+ $ this ->configuration ->getPort (),
48
+ $ this ->configuration ->getDatabase (),
49
+ $ this ->configuration ->getUsername (),
50
+ $ this ->configuration ->getPassword ());
51
+ break ;
52
+ case 'sqlite ' :
53
+ $ dsn = sprintf ('sqlite:%s ' ,
54
+ $ this ->configuration ->getDatabase ());
55
+ break ;
56
+ case 'sybase ' :
57
+ case 'mssql ' :
58
+ case 'dblib ' :
59
+ $ dsn = sprintf ("%s:host=%s;dbname=%s;charset=%s " ,
60
+ $ this ->configuration ->getDriver (),
61
+ $ this ->configuration ->getHost (),
62
+ $ this ->configuration ->getDatabase (),
63
+ $ this ->configuration ->getCharset ());
64
+ break ;
65
+ case 'cubrid ' :
66
+ $ dsn = sprintf ("cubrid:host=%s;port=%u;dbname=%s " ,
67
+ $ this ->configuration ->getHost (),
68
+ $ this ->configuration ->getPort (),
69
+ $ this ->configuration ->getDatabase ());
70
+ break ;
71
+ case '4D ' :
72
+ $ dsn = sprintf ('4D:host=%s;port=%u;user=%s;password=%s;dbname=%s;charset=%s ' ,
73
+ $ this ->configuration ->getHost (),
74
+ $ this ->configuration ->getPort (),
75
+ $ this ->configuration ->getUsername (),
76
+ $ this ->configuration ->getPassword (),
77
+ $ this ->configuration ->getDatabase (),
78
+ $ this ->configuration ->getCharset ());
79
+ break ;
80
+ }
81
+
82
+ return $ dsn ;
83
+ }
84
+
85
+ public function connect (): Connection
86
+ {
87
+ try {
88
+ $ this ->dbh = new \PDO ($ this ->getDsn (), $ this ->configuration ->getUsername (), $ this ->configuration ->getPassword ());
89
+ $ this ->dbh ->setAttribute (\PDO ::ATTR_ERRMODE , \PDO ::ERRMODE_EXCEPTION );
90
+ $ this ->dbh ->setAttribute (\PDO ::ATTR_EMULATE_PREPARES ,true );
91
+
92
+ } catch (\PDOException $ e ) {
93
+ echo 'Connection failed: ' . $ e ->getMessage ();
94
+ }
95
+
96
+ return $ this ;
97
+ }
98
+
99
+ public function disconnect (): Connection
100
+ {
101
+ $ this ->dbh = null ;
102
+
103
+ return $ this ;
104
+ }
105
+
106
+ public function reconnect (): Connection
107
+ {
108
+ $ this ->disconnect ();
109
+ $ this ->connect ();
110
+
111
+ return $ this ;
112
+ }
113
+ }
0 commit comments