Completed
Branch feature/pre-split (f8e7b8)
by Anton
04:02
created

PostgresDriver::tableNames()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Database\Drivers\Postgres;
10
11
use Spiral\Database\Builders\InsertQuery;
12
use Spiral\Database\DatabaseInterface;
13
use Spiral\Database\Entities\Driver;
14
use Spiral\Database\Exceptions\DriverException;
15
16
//use Spiral\Database\Drivers\Postgres\Schemas\Commander;
1 ignored issue
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
17
//use Spiral\Database\Drivers\Postgres\Schemas\TableSchema;
18
19
/**
20
 * Talks to postgres databases.
21
 */
22
class PostgresDriver extends Driver
23
{
24
    /**
25
     * Driver type.
26
     */
27
    const TYPE = DatabaseInterface::POSTGRES;
28
29
    /**
30
     * Driver schemas.
31
     */
32
    //const SCHEMA_TABLE = TableSchema::class;
33
34
    /**
35
     * Commander used to execute commands. :).
36
     */
37
    //const COMMANDER = Commander::class;
38
39
    /**
40
     * Query compiler class.
41
     */
42
    const QUERY_COMPILER = QueryCompiler::class;
43
44
    /**
45
     * Default timestamp expression.
46
     */
47
    const TIMESTAMP_NOW = 'now()';
48
49
    /**
50
     * Cached list of primary keys associated with their table names. Used by InsertBuilder to
51
     * emulate last insert id.
52
     *
53
     * @var array
54
     */
55
    private $primaryKeys = [];
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function hasTable(string $name): bool
61
    {
62
        $query = 'SELECT "table_name" FROM "information_schema"."tables" WHERE "table_schema" = \'public\' AND "table_type" = \'BASE TABLE\' AND "table_name" = ?';
63
64
        return (bool)$this->query($query, [$name])->fetchColumn();
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function truncateData(string $table)
71
    {
72
        $this->statement("TRUNCATE TABLE {$this->identifier($table)}");
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function tableNames(): array
79
    {
80
        $query = 'SELECT "table_name" FROM "information_schema"."tables" WHERE "table_schema" = \'public\' AND "table_type" = \'BASE TABLE\'';
81
82
        $tables = [];
83
        foreach ($this->query($query) as $row) {
84
            $tables[] = $row['table_name'];
85
        }
86
87
        return $tables;
88
    }
89
90
    /**
91
     * Get singular primary key associated with desired table. Used to emulate last insert id.
92
     *
93
     * @param string $prefix Database prefix if any.
94
     * @param string $table  Fully specified table name, including postfix.
95
     *
96
     * @return string|null
97
     *
98
     * @throws DriverException
99
     */
100
    public function getPrimary(string $prefix, string $table): string
0 ignored issues
show
Unused Code introduced by
The parameter $prefix is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $table is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
101
    {
102
//        if (!empty($this->cacheStore) && empty($this->primaryKeys)) {
1 ignored issue
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
103
//            $this->primaryKeys = (array)$this->cacheStore->get($this->getSource() . '/keys');
104
//        }
105
//
106
//        if (!empty($this->primaryKeys) && array_key_exists($table, $this->primaryKeys)) {
107
//            return $this->primaryKeys[$table];
108
//        }
109
//
110
//        if (!$this->hasTable($table)) {
111
//            throw new DriverException(
112
//                "Unable to fetch table primary key, no such table '{$table}' exists"
113
//            );
114
//        }
115
//
116
//        $this->primaryKeys[$table] = $this->tableSchema($table)->getPrimaryKeys();
117
//        if (count($this->primaryKeys[$table]) === 1) {
118
//            //We do support only single primary key
119
//            $this->primaryKeys[$table] = $this->primaryKeys[$table][0];
120
//        } else {
121
//            $this->primaryKeys[$table] = null;
122
//        }
123
//
124
//        //Caching
125
//        if (!empty($this->memory)) {
126
//            $this->cacheStore->forever($this->getSource() . '/keys', $this->primaryKeys);
127
//        }
128
//
129
//        return $this->primaryKeys[$table];
130
        return '';
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     *
136
     * Postgres uses custom insert query builder in order to return value of inserted row.
137
     */
138
    public function insertBuilder(string $prefix, array $parameters = []): InsertQuery
139
    {
140
        return $this->factory->make(
141
            PostgresInsertQuery::class,
142
            ['driver' => $this, 'compiler' => $this->queryCompiler($prefix),] + $parameters
143
        );
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    protected function createPDO(): \PDO
150
    {
151
        //Spiral is purely UTF-8
152
        $pdo = parent::createPDO();
153
        $pdo->exec("SET NAMES 'UTF-8'");
154
155
        return $pdo;
156
    }
157
}
158