-
Notifications
You must be signed in to change notification settings - Fork 0
/
PDOAdapter.php
55 lines (50 loc) · 1.47 KB
/
PDOAdapter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php namespace Devtools;
class PDOAdapter
{
public static function getConnectionString($params)
{
switch($params['type']) {
case 'mysql':
return "{$params['type']}:host={$params['host']};dbname={$params['db']}";
case 'firebird':
return "{$params['type']}:dbname={$params['host']}:{$params['db']}";
case 'dblib':
return "{$params['type']}:host={$params['host']};{$params['db']}";
};
}
public static function connect($parameters)
{
self::validateParameters($parameters);
return new \PDO(
self::getConnectionString($parameters),
$parameters['username'],
$parameters['password']
);
}
private static function validateParameters($parameters)
{
$keys = array_keys($parameters);
if (!in_array('type', $keys)
|| !in_array('host', $keys)
|| !in_array('db', $keys)
|| !in_array('username', $keys)
|| !in_array('password', $keys)
) {
throw new \Exception('Invalid connection options.');
}
}
private static function getConnectionStringByType($type)
{
switch($type) {
case 'mysql':
return '%s:host=%s;dbname=%s';
break;
case 'firebird':
return '%s:dbname=%s:%s';
break;
case 'dblib':
return '%s:host=%s;%s';
break;
}
}
}