Completed
Push — master ( d90095...c1e7a0 )
by Patrick
03:27
created

SQLDataSet::getOrderByClause()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 15
rs 9.2
1
<?php
2
namespace Data;
3
4
class SQLDataSet extends DataSet
5
{
6
    protected $pdo;
7
8
    public function __construct($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...
9
    {
10
        if(isset($params['user']))
11
        {
12
            $this->pdo = new \PDO($params['dsn'], $params['user'], $params['pass']);
13
        }
14
        else
15
        {
16
            $this->pdo = new \PDO($params['dsn']);
17
        }
18
    }
19
20
    /**
21
     * Get the number of rows affected by the query
22
     *
23
     * @param string $sql The SQL string
24
     *
25
     * @return integer The number of rows affected by the query
26
     */
27
    private function _get_row_count_for_query($sql)
28
    {
29
        $stmt = $this->pdo->query($sql);
30
        if($stmt === false)
31
        {
32
            return 0;
33
        }
34
        $count = $stmt->rowCount();
35
        if($count === 0)
36
        {
37
            $array = $stmt->fetchAll();
38
            $count = count($array);
39
        }
40
        return $count;
41
    }
42
43 View Code Duplication
    function _tableExistsNoPrefix($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
44
    {
45
        if($this->_get_row_count_for_query('SHOW TABLES LIKE '.$this->pdo->quote($name)) > 0)
46
        {
47
            return true;
48
        }
49
        else if($this->_get_row_count_for_query('SELECT * FROM sqlite_master WHERE name LIKE '.$this->pdo->quote($name)) > 0)
50
        {
51
            return true;
52
        }
53
        return false;
54
    }
55
56 View Code Duplication
    function _tableExists($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
57
    {
58
        if($this->_get_row_count_for_query('SHOW TABLES LIKE '.$this->pdo->quote('tbl'.$name)) > 0)
59
        {
60
            return true;
61
        }
62
        else if($this->_get_row_count_for_query('SELECT * FROM sqlite_master WHERE name LIKE '.$this->pdo->quote('tbl'.$name)) > 0)
63
        {
64
            return true;
65
        }
66
        return false;
67
    }
68
69 View Code Duplication
    function _viewExists($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
70
    {
71
        if($this->_get_row_count_for_query('SHOW TABLES LIKE '.$this->pdo->quote('v'.$name)) > 0)
72
        {
73
            return true;
74
        }
75
        else if($this->_get_row_count_for_query('SELECT * FROM sqlite_master WHERE name LIKE '.$this->pdo->quote('v'.$name)) > 0)
76
        {
77
            return true;
78
        }
79
        return false;
80
    }
81
82
    function tableExists($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
83
    {
84
        if($this->_tableExists($name))
85
        {
86
            return true;
87
        }
88
        if($this->_tableExistsNoPrefix($name))
89
        {
90
            return true;
91
        }
92
        if($this->_viewExists($name))
93
        {
94
            return true;
95
        }
96
        return false;
97
    }
98
99
    function getTable($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
100
    {
101
        if($this->_tableExists($name))
102
        {
103
            return new SQLDataTable($this, 'tbl'.$name);
104
        }
105
        if($this->_viewExists($name))
106
        {
107
            return new SQLDataTable($this, 'v'.$name);
108
        }
109
        if($this->_tableExistsNoPrefix($name))
110
        {
111
            return new SQLDataTable($this, $name);
112
        }
113
        throw new \Exception('No such table '.$name);
114
    }
115
116
    /**
117
     * @param array $sort The array to sort by or false to not sort
118
     */
119
    private function getOrderByClause($sort)
120
    {
121
        if(empty($sort))
122
        {
123
            return false;
124
        }
125
        $sql = ' ORDER BY ';
126
        $tmp = array();
127
        foreach($sort as $sort_col=>$dir)
128
        {
129
            array_push($tmp, $sort_col.' '.($dir === 1 ? 'ASC' : 'DESC'));
130
        }
131
        $sql .= implode($tmp, ',');
132
        return $sql;
133
    }
134
135
    public function read($tablename, $where = false, $select = '*', $count = false, $skip = false, $sort = false)
136
    {
137
        if($select === false)
138
        {
139
            $select = '*';
140
        }
141
        $sql = "SELECT $select FROM $tablename";
142
        if($where !== false)
143
        {
144
            $sql .= ' WHERE '.$where;
145
        }
146
        if($count !== false)
147
        {
148
            if($skip === false)
149
            {
150
                $sql .= ' LIMIT '.(int)$count;
151
            }
152
            else
153
            {
154
                $sql .= " LIMIT $skip, $count";
155
            }
156
        }
157
        $tmp = $this->getOrderByClause($sort);
0 ignored issues
show
Documentation introduced by
$sort is of type boolean, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
158
        if($tmp !== false)
159
        {
160
            $sql .= $tmp;
161
        }
162
        $stmt = $this->pdo->query($sql, \PDO::FETCH_ASSOC);
163
        if($stmt === false)
164
        {
165
            return false;
166
        }
167
        $ret = $stmt->fetchAll();
168
        if($ret === false || empty($ret))
169
        {
170
            return false;
171
        }
172
        return $ret;
173
    }
174
175 View Code Duplication
    function update($tablename, $where, $data)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
176
    {
177
        $set = array();
178
        if(is_object($data))
179
        {
180
            $data = (array)$data;
181
        }
182
        $cols = array_keys($data);
183
        $count = count($cols);
184
        for($i = 0; $i < $count; $i++)
185
        {
186
            array_push($set, $cols[$i].'='.$this->pdo->quote($data[$cols[$i]]));
187
        }
188
        $set = implode(',', $set);
189
        $sql = "UPDATE $tablename SET $set WHERE $where";
190
        if($this->pdo->exec($sql) === false)
191
        {
192
            return false;
193
        }
194
        return true;
195
    }
196
197 View Code Duplication
    function create($tablename, $data)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
198
    {
199
        $set = array();
200
        if(is_object($data))
201
        {
202
            $data = (array)$data;
203
        }
204
        $cols = array_keys($data);
205
        $count = count($cols);
206
        for($i = 0; $i < $count; $i++)
207
        {
208
            array_push($set, $this->pdo->quote($data[$cols[$i]]));
209
        }
210
        $cols = implode(',', $cols);
211
        $set = implode(',', $set);
212
        $sql = "INSERT INTO $tablename ($cols) VALUES ($set);";
213
        if($this->pdo->exec($sql) === false)
214
        {
215
            return false;
216
        }
217
        return true;
218
    }
219
220
    function delete($tablename, $where)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
221
    {
222
        $sql = "DELETE FROM $tablename WHERE $where";
223
        if($this->pdo->exec($sql) === false)
224
        {
225
            return false;
226
        }
227
        return true;
228
    }
229
230
    function raw_query($sql)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
231
    {
232
        $stmt = $this->pdo->query($sql, \PDO::FETCH_ASSOC);
233
        if($stmt === false)
234
        {
235
            return false;
236
        }
237
        $ret = $stmt->fetchAll();
238
        return $ret;
239
    }
240
}
241
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
242