1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Opeyemiabiodun\PotatoORM\Connections; |
4
|
|
|
|
5
|
|
|
use PDO; |
6
|
|
|
use PDOException; |
7
|
|
|
use RuntimeException; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Opeyemiabiodun\PotatoORM\Connections\Connection; |
10
|
|
|
use Opeyemiabiodun\PotatoORM\Connections\LoadEnvVariablesTrait; |
11
|
|
|
use Opeyemiabiodun\PotatoORM\Connections\DatabaseTransactionsTrait; |
12
|
|
|
|
13
|
|
|
final class PgSqlConnection implements Connection |
14
|
|
|
{ |
15
|
|
|
use LoadEnvVariablesTrait, DatabaseTransactionsTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The method called in the constructor. |
19
|
|
|
* |
20
|
|
|
* @return void |
|
|
|
|
21
|
|
|
*/ |
22
|
|
|
private function __construct() |
23
|
|
|
{ |
24
|
|
|
$this->useDbEnv(); |
25
|
|
|
|
26
|
|
|
$dsn = "pgsql:host={$this->_host};port={$this->_port};dbname={$this->_database};user={$this->_username};password={$this->_password}"; |
27
|
|
|
|
28
|
|
|
try { |
29
|
|
|
$this->_pdo = new PDO($dsn); |
|
|
|
|
30
|
|
|
} catch (PDOException $e) { |
|
|
|
|
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns the columns of a table. |
36
|
|
|
* |
37
|
|
|
* @param string $table The table inspected for its columns. |
38
|
|
|
* |
39
|
|
|
* @return array The columns of the table. |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function getColumns($table) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
if (gettype($table) !== 'string') { |
44
|
|
|
throw new InvalidArgumentException("The parameter {$table} is not an string. A string is required instead."); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this->getPdo()->query("SELECT COLUMN_NAME |
48
|
|
|
FROM {$this->_database}.INFORMATION_SCHEMA.COLUMNS |
49
|
|
|
WHERE TABLE_NAME = N'{$table}'")->fetchAll(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns the Connection's PDO. |
54
|
|
|
* |
55
|
|
|
* @return PDO PHP Data Objects |
56
|
|
|
*/ |
57
|
|
|
public function getPdo() |
58
|
|
|
{ |
59
|
|
|
return $this->_pdo; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns the primary key of a table. |
64
|
|
|
* |
65
|
|
|
* @param string $table The table inspected for its primary key. |
66
|
|
|
* |
67
|
|
|
* @return string The primary key of the table. |
68
|
|
|
*/ |
69
|
|
|
public function getPrimaryKey($table) |
70
|
|
|
{ |
71
|
|
|
if (gettype($table) !== 'string') { |
72
|
|
|
throw new InvalidArgumentException("The parameter {$table} is not an string. A string is required instead."); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$array = $this->getPdo()->query("SELECT KU.table_name as tablename,column_name as primarykeycolumn |
76
|
|
|
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC |
77
|
|
|
INNER JOIN |
78
|
|
|
INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KU |
79
|
|
|
ON TC.CONSTRAINT_TYPE = 'PRIMARY KEY' AND |
80
|
|
|
TC.CONSTRAINT_NAME = KU.CONSTRAINT_NAME |
81
|
|
|
and ku.table_name='{$table}' |
82
|
|
|
ORDER BY KU.TABLE_NAME, KU.ORDINAL_POSITION;")->fetchAll(); |
83
|
|
|
if (count($array) === 0) { |
84
|
|
|
throw new RuntimeException("Error Processing Request", 1); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $array[0]['primarykeycolumn']; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public static function load() |
91
|
|
|
{ |
92
|
|
|
return new PgSqlConnection(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.