Completed
Branch master (e3a0da)
by
unknown
15:36
created

PgSqlConnection::getPdo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

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.

Loading history...
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);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \PDO($dsn) of type object<PDO> is incompatible with the declared type object<Opeyemiabiodun\PotatoORM\Connections\PDO> of property $_pdo.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
30
        } catch (PDOException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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