Skip to content

Commit ae11e56

Browse files
committed
Innitial commit
1 parent e5bbe03 commit ae11e56

File tree

2 files changed

+193
-0
lines changed

2 files changed

+193
-0
lines changed

src/Configuration.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
namespace PhpOrm;
3+
4+
class Configuration
5+
{
6+
private $charset;
7+
8+
private $collation;
9+
10+
private $database;
11+
12+
private $driver;
13+
14+
private $host;
15+
16+
private $password;
17+
18+
private $port;
19+
20+
private $username;
21+
22+
public function __construct(string $username,
23+
string $password,
24+
string $database,
25+
string $host=null,
26+
int $port=null,
27+
string $driver=null,
28+
string $charset=null,
29+
string $collation=null)
30+
{
31+
$this->host = $host ?: 'localhost';
32+
$this->port = $port ?: 3306;
33+
$this->username = $username;
34+
$this->password = $password;
35+
$this->database = $database;
36+
$this->driver = $driver ?: 'mysql';
37+
$this->charset = $charset ?: 'utf8mb4';
38+
$this->collation = $collation ?: 'utf8mb4_general_ci';
39+
}
40+
41+
public function getCharset(): string
42+
{
43+
return $this->charset;
44+
}
45+
46+
public function getCollation(): string
47+
{
48+
return $this->collation;
49+
}
50+
51+
public function getDatabase(): string
52+
{
53+
return $this->database;
54+
}
55+
56+
public function getDriver(): string
57+
{
58+
return $this->driver;
59+
}
60+
61+
public function getHost(): string
62+
{
63+
return $this->host;
64+
}
65+
66+
public function getPassword(): string
67+
{
68+
return $this->password;
69+
}
70+
71+
public function getPort(): int
72+
{
73+
return $this->port;
74+
}
75+
76+
public function getUsername(): string
77+
{
78+
return $this->username;
79+
}
80+
}

src/Connection.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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

Comments
 (0)