Completed
Push — master ( 648d22...aaac57 )
by Oleg
05:06
created

PgsqlDriver::listTables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
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
        return $this->conn->query(
84
            'SELECT table_name FROM information_schema.tables WHERE table_schema = \'' . $this->tableSchema . '\''
85
        )->fetchAll(\PDO::FETCH_COLUMN, 0);
86
    }
87
88
    /**
89
     * List database names on this connection
90
     *
91
     * @access public
92
     * @return mixed
93
     */
94
    public function listDatabases()
95
    {
96
        // TODO: Implement listDatabases() method.
97
    }
98
99
    /**
100
     * Create a new table
101
     *
102
     * @param string $name Table name
103
     * @param array $elements Table elements
104
     * @param string $params Table params
105
     *
106
     * @return int
107
     */
108
    public function createTable($name, array $elements = [], $params = '')
109
    {
110
        // TODO: Implement createTable() method.
111
    }
112
113
    /**
114
     * Remove table from database
115
     *
116
     * @access public
117
     *
118
     * @param string $name Table name
119
     *
120
     * @return mixed
121
     */
122
    public function removeTable($name)
123
    {
124
        // TODO: Implement removeTable() method.
125
    }
126
127
    /**
128
     * Clear all data from table
129
     *
130
     * @access public
131
     *
132
     * @param string $name Table name
133
     *
134
     * @return int
135
     */
136
    public function clearTable($name)
137
    {
138
        // TODO: Implement clearTable() method.
139
    }
140
141
    /**
142
     * Get array fields into table
143
     *
144
     * @access public
145
     *
146
     * @param string $table Table name
147
     *
148
     * @return array
149
     */
150
    public function listFields($table)
151
    {
152
        $sth = $this->conn->query('SELECT * FROM information_schema.columns WHERE table_name =\'' . $table . '\'');
153
        $result = [];
154
155
        foreach ($sth->fetchAll(\PDO::FETCH_ASSOC) as $row) {
156
            $result[] = [
157
                'field' => $row['column_name'],
158
                'type' => $row['data_type'] . (($max = $row['character_maximum_length']) ? '(' . $max . ')' : ''),
159
                'null' => $row['is_nullable'],
160
                'default' => $row['column_default']
161
            ];
162
        }
163
164
        return $result;
165
    }
166
167
    /**
168
     * Get info of a field
169
     *
170
     * @access public
171
     *
172
     * @param string $field Field name
173
     * @param string $table Table name
174
     *
175
     * @return array|boolean
176
     */
177
    public function fieldInfo($field, $table)
178
    {
179
        // TODO: Implement fieldInfo() method.
180
    }
181
182
    /**
183
     * Insert row into table
184
     *
185
     * @access public
186
     *
187
     * @param string $table Table name
188
     * @param array $line Line or lines to added
189
     * @param bool $multi Is multi rows
190
     *
191
     * @return bool
192
     */
193 View Code Duplication
    public function insert($table, array $line = [], $multi = false)
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...
194
    {
195
        $fields = '"' . implode('", "', array_keys($multi ? $line[0] : $line)) . '"';
196
        $values = ':' . implode(', :', array_keys($multi ? $line[0] : $line));
197
        $rows = $multi ? $line : [$line];
198
        $id = null;
199
200
        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...
201
            $this->conn->beginTransaction();
202
203
            $dbh = null;
204
            foreach ($rows AS $row) {
205
                $dbh = $this->conn->prepare("INSERT INTO {$table} ({$fields}) VALUES ({$values});")->execute($row);
206
            }
207
208
            $id = $dbh ? $this->conn->lastInsertId() : false;
209
            $this->conn->commit();
210
        }
211
212
        return $id ?: false;
213
    }
214
215
    /**
216
     * Update row in table
217
     *
218
     * @access public
219
     *
220
     * @param string $table Table name
221
     * @param array $elements Elements to update
222
     * @param string $conditions Conditions for search
223
     *
224
     * @return bool
225
     */
226
    public function update($table, array $elements = [], $conditions = '')
227
    {
228
        // TODO: Implement update() method.
229
    }
230
231
    /**
232
     * Delete row from table
233
     *
234
     * @access public
235
     *
236
     * @param string $table Table name
237
     * @param string $conditions Conditions to search
238
     * @param array $params Params array
239
     *
240
     * @return bool
241
     */
242
    public function delete($table, $conditions, array $params = [])
243
    {
244
        // TODO: Implement delete() method.
245
    }
246
247
    /**
248
     * Count element in sub-query
249
     *
250
     * @access public
251
     *
252
     * @param string $query Query
253
     * @param string $table Table name
254
     *
255
     * @return integer|boolean
256
     */
257
    public function count($query = '', $table = '')
258
    {
259
        // TODO: Implement count() method.
260
    }
261
262
    /**
263
     * Exists element in the table by params
264
     *
265
     * @access public
266
     *
267
     * @param string $table Table name
268
     * @param array $params Params array
269
     *
270
     * @return bool
271
     */
272
    public function exists($table, array $params = [])
273
    {
274
        // TODO: Implement exists() method.
275
    }
276
}