PgsqlDriver::listFields()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 10
nc 3
nop 1
1
<?php /** PgsqlDriverMicro */
2
3
namespace Micro\Db\Drivers;
4
5
use Micro\Base\Exception;
6
7
/**
8
 * PostgreSQL Driver class file.
9
 *
10
 * @author Oleg Lunegov <[email protected]>
11
 * @link https://github.com/linpax/microphp-framework
12
 * @copyright Copyright (c) 2013 Oleg Lunegov
13
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
14
 * @package Micro
15
 * @subpackage Db\Drivers
16
 * @version 1.0
17
 * @since 1.0
18
 */
19
class PgsqlDriver extends Driver
20
{
21
    /** @var string $tableSchema Table schema for postgres */
22
    protected $tableSchema = 'public';
23
24
25
    /**
26
     * Driver constructor.
27
     *
28
     * @access public
29
     *
30
     * @param string $dsn DSN connection string
31
     * @param array $config Configuration of connection
32
     * @param array $options Other options
33
     *
34
     * @result void
35
     * @throws Exception
36
     */
37
    public function __construct($dsn, array $config = [], array $options = [])
38
    {
39
        parent::__construct($dsn, $config, $options);
40
41
        if (!empty($config['schema'])) {
42
            $this->tableSchema = $config['schema'];
43
        }
44
    }
45
46
    /**
47
     * Set current database
48
     *
49
     * @access public
50
     *
51
     * @param string $dbName Database name
52
     *
53
     * @return boolean
54
     * @throws \InvalidArgumentException
55
     */
56
    public function switchDatabase($dbName)
57
    {
58
        // TODO: Implement switchDatabase() method.
59
    }
60
61
    /**
62
     * Info of database
63
     *
64
     * @access public
65
     *
66
     * @param string $dbName Database name
67
     *
68
     * @return array
69
     */
70
    public function infoDatabase($dbName)
71
    {
72
        // TODO: Implement infoDatabase() method.
73
    }
74
75
    /**
76
     * List tables in db
77
     *
78
     * @access public
79
     * @return array
80
     */
81
    public function listTables()
82
    {
83
        /** @noinspection SqlResolve */
84
        return $this->conn->query(
85
            'SELECT table_name FROM information_schema.tables WHERE table_schema = \''.$this->tableSchema.'\';'
86
        )->fetchAll(\PDO::FETCH_COLUMN, 0);
87
    }
88
89
    /**
90
     * List database names on this connection
91
     *
92
     * @access public
93
     * @return mixed
94
     */
95
    public function listDatabases()
96
    {
97
        /** @noinspection SqlResolve */
98
        return $this->conn->query(
99
            'SELECT datname FROM pg_database WHERE datistemplate = false and datname != \'postgres\';'
100
        )->fetchAll(\PDO::FETCH_COLUMN, 0);
101
    }
102
103
    /**
104
     * Create a new table
105
     *
106
     * @param string $name Table name
107
     * @param array $elements Table elements
108
     * @param string $params Table params
109
     *
110
     * @return int
111
     */
112
    public function createTable($name, array $elements = [], $params = '')
113
    {
114
        return $this->conn->exec(
115
            sprintf('SELECT TABLE IF NOT EXISTS `%s` (%s) %s;', $name, implode(', ', $elements), $params)
116
        );
117
    }
118
119
    /**
120
     * Get array fields into table
121
     *
122
     * @access public
123
     *
124
     * @param string $table Table name
125
     *
126
     * @return array
127
     */
128
    public function listFields($table)
129
    {
130
        /** @noinspection SqlResolve */
131
        $sth = $this->conn->query('SELECT * FROM information_schema.columns WHERE table_name =\''.$table.'\'');
132
        $result = [];
133
134
        foreach ($sth->fetchAll(\PDO::FETCH_ASSOC) as $row) {
135
            $result[] = [
136
                'field' => $row['column_name'],
137
                'type' => $row['data_type'].(($max = $row['character_maximum_length']) ? '('.$max.')' : ''),
138
                'null' => $row['is_nullable'],
139
                'default' => $row['column_default']
140
            ];
141
        }
142
143
        return $result;
144
    }
145
146
    /**
147
     * Insert row into table
148
     *
149
     * @access public
150
     *
151
     * @param string $table Table name
152
     * @param array $line Line or lines to added
153
     * @param bool $multi Is multi rows
154
     *
155
     * @return bool
156
     */
157
    public function insert($table, array $line = [], $multi = false)
158
    {
159
        $fields = '"'.implode('", "', array_keys($multi ? $line[0] : $line)).'"';
160
        $values = ':'.implode(', :', array_keys($multi ? $line[0] : $line));
161
        $rows = $multi ? $line : [$line];
162
        $id = null;
163
164
        if ($rows) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $rows of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
165
            $this->conn->beginTransaction();
166
167
            $dbh = null;
168
            $res = $this->conn->prepare('INSERT INTO '.$table.' ('.$fields.') VALUES ('.$values.');');
169
            foreach ($rows AS $row) {
170
                $dbh = $res->execute($row);
171
            }
172
173
            $id = $dbh ? $this->conn->lastInsertId() : false;
174
            $this->conn->commit();
175
        }
176
177
        return $id ?: false;
178
    }
179
180
    /**
181
     * Update row in table
182
     *
183
     * @access public
184
     *
185
     * @param string $table Table name
186
     * @param array $elements Elements to update
187
     * @param string $conditions Conditions for search
188
     *
189
     * @return bool
190
     */
191 View Code Duplication
    public function update($table, array $elements = [], $conditions = '')
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...
192
    {
193
        $keys = array_keys($elements);
194
195
        if (0 === count($keys)) {
196
            return false;
197
        }
198
199
        $valStr = [];
200
201
        foreach ($keys as $key) {
202
            $valStr[] = '"'.$key.'" = :'.$key;
203
        }
204
205
        $valStr = implode(',', $valStr);
206
207
        if ($conditions) {
208
            $conditions = 'WHERE '.$conditions;
209
        }
210
211
        return $this->conn->prepare("UPDATE {$table} SET {$valStr} {$conditions};")->execute($elements);
212
    }
213
214
    /**
215
     * Exists element in the table by params
216
     *
217
     * @access public
218
     *
219
     * @param string $table Table name
220
     * @param array $params Params array
221
     *
222
     * @return bool
223
     */
224 View Code Duplication
    public function exists($table, array $params = [])
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...
225
    {
226
        $keys = [];
227
228
        foreach ($params AS $key => $val) {
229
            $keys[] = '"'.$key.'"=\''.$val.'\'';
230
        }
231
232
        $sth = $this->conn->prepare('SELECT * FROM '.$table.' WHERE '.implode(' AND ', $keys).' LIMIT 1;');
233
        /** @noinspection PdoApiUsageInspection */
234
        $sth->execute();
235
236
        return (bool)$sth->rowCount();
237
    }
238
}