Completed
Push — master ( cd626a...a8f7f6 )
by Oleg
05:12
created

PgsqlDriver::createTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

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