Completed
Branch feature/pre-split (b025f2)
by Anton
03:30
created

PostgresDriver::hasTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
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
//    /**
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...
91
//     * Get singular primary key associated with desired table. Used to emulate last insert id.
92
//     *
93
//     * @param string $table Fully specified table name, including postfix.
94
//     *
95
//     * @return string|null
96
//     *
97
//     * @throws DriverException
98
//     */
99
//    public function getPrimary(string $table)
100
//    {
101
//        if (!empty($this->cacheStore) && empty($this->primaryKeys)) {
102
//            $this->primaryKeys = (array)$this->cacheStore->get($this->getSource() . '/keys');
103
//        }
104
//
105
//        if (!empty($this->primaryKeys) && array_key_exists($table, $this->primaryKeys)) {
106
//            return $this->primaryKeys[$table];
107
//        }
108
//
109
//        if (!$this->hasTable($table)) {
110
//            throw new DriverException(
111
//                "Unable to fetch table primary key, no such table '{$table}' exists"
112
//            );
113
//        }
114
//
115
//        $this->primaryKeys[$table] = $this->tableSchema($table)->getPrimaryKeys();
116
//        if (count($this->primaryKeys[$table]) === 1) {
117
//            //We do support only single primary key
118
//            $this->primaryKeys[$table] = $this->primaryKeys[$table][0];
119
//        } else {
120
//            $this->primaryKeys[$table] = null;
121
//        }
122
//
123
//        //Caching
124
//        if (!empty($this->memory)) {
125
//            $this->cacheStore->forever($this->getSource() . '/keys', $this->primaryKeys);
126
//        }
127
//
128
//        return $this->primaryKeys[$table];
129
//    }
130
131
    /**
132
     * {@inheritdoc}
133
     *
134
     * Postgres uses custom insert query builder in order to return value of inserted row.
135
     */
136
    public function insertBuilder(string $prefix, array $parameters = []): InsertQuery
137
    {
138
        return $this->factory->make(
139
            PostgresInsertQuery::class,
140
            ['driver' => $this, 'compiler' => $this->queryCompiler($prefix),] + $parameters
141
        );
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    protected function createPDO(): \PDO
148
    {
149
        //Spiral is purely UTF-8
150
        $pdo = parent::createPDO();
151
        $pdo->exec("SET NAMES 'UTF-8'");
152
153
        return $pdo;
154
    }
155
}
156