Completed
Push — master ( 1caf4d...448bfe )
by Patrick
03:00
created

Data/class.SQLDataSet.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Data;
3
4
class SQLDataSet extends DataSet
5
{
6
    protected $pdo;
7
8 View Code Duplication
    function __construct($params)
0 ignored issues
show
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
    function _get_row_count_for_query($sql)
21
    {
22
        $stmt = $this->pdo->query($sql);
23
        if($stmt === false)
24
        {
25
            return 0;
26
        }
27
        $count = $stmt->rowCount();
28
        if($count === 0)
29
        {
30
            $array = $stmt->fetchAll();
31
            $count = count($array);
32
        }
33
        return $count;
34
    }
35
36 View Code Duplication
    function _tableExistsNoPrefix($name)
37
    {
38
        if($this->_get_row_count_for_query('SHOW TABLES LIKE '.$this->pdo->quote($name)) > 0)
39
        {
40
            return true;
41
        }
42
        else if($this->_get_row_count_for_query('SELECT * FROM sqlite_master WHERE name LIKE '.$this->pdo->quote($name)) > 0)
43
        {
44
            return true;
45
        }
46
        return false;
47
    }
48
49 View Code Duplication
    function _tableExists($name)
50
    {
51
        if($this->_get_row_count_for_query('SHOW TABLES LIKE '.$this->pdo->quote('tbl'.$name)) > 0)
52
        {
53
            return true;
54
        }
55
        else if($this->_get_row_count_for_query('SELECT * FROM sqlite_master WHERE name LIKE '.$this->pdo->quote('tbl'.$name)) > 0)
56
        {
57
            return true;
58
        }
59
        return false;
60
    }
61
62 View Code Duplication
    function _viewExists($name)
63
    {
64
        if($this->_get_row_count_for_query('SHOW TABLES LIKE '.$this->pdo->quote('v'.$name)) > 0)
65
        {
66
            return true;
67
        }
68
        else if($this->_get_row_count_for_query('SELECT * FROM sqlite_master WHERE name LIKE '.$this->pdo->quote('v'.$name)) > 0)
69
        {
70
            return true;
71
        }
72
        return false;
73
    }
74
75
    function tableExists($name)
76
    {
77
        if($this->_tableExists($name))
78
        {
79
            return true;
80
        }
81
        if($this->_tableExistsNoPrefix($name))
82
        {
83
            return true;
84
        }
85
        if($this->_viewExists($name))
86
        {
87
            return true;
88
        }
89
        return false;
90
    }
91
92
    function getTable($name)
93
    {
94
        if($this->_tableExists($name))
95
        {
96
            return new SQLDataTable($this, 'tbl'.$name);
97
        }
98
        if($this->_viewExists($name))
99
        {
100
            return new SQLDataTable($this, 'v'.$name);
101
        }
102
        if($this->_tableExistsNoPrefix($name))
103
        {
104
            return new SQLDataTable($this, $name);
105
        }
106
        throw new \Exception('No such table '.$name);
107
    }
108
109
    function read($tablename, $where=false, $select='*', $count=false, $skip=false, $sort=false)
110
    {
111
        if($select === false)
112
        {
113
            $select = '*';
114
        }
115
        $sql = "SELECT $select FROM $tablename";
116
        if($where !== false)
117
        {
118
            $sql.=' WHERE '.$where;
119
        }
120
        if($count !== false)
121
        {
122
            if($skip === false)
123
            {
124
                $sql.=' LIMIT '.(int)$count;
125
            }
126
            else
127
            {
128
                $sql.=" LIMIT $skip, $count";
129
            }
130
        }
131
        if($sort !== false)
132
        {
133
            $sql.=' ORDER BY ';
134
            $tmp = array();
135
            foreach($sort as $sort_col=>$dir)
136
            {
137
                array_push($tmp, $sort_col.' '.($dir === 1?'ASC':'DESC'));
138
            }
139
            $sql.=implode($tmp,',');
140
        }
141
        $stmt = $this->pdo->query($sql, \PDO::FETCH_ASSOC);
142
        if($stmt === false)
143
        {
144
            return false;
145
        }
146
        $ret = $stmt->fetchAll();
147
        if($ret === false || empty($ret))
148
        {
149
            return false;
150
        }
151
        return $ret;
152
    }
153
154 View Code Duplication
    function update($tablename, $where, $data)
155
    {
156
        $set = array();
157
        if(is_object($data))
158
        {
159
            $data = (array)$data;
160
        }
161
        $cols = array_keys($data);
162
        $count = count($cols);
163
        for($i = 0; $i < $count; $i++)
164
        {
165
            array_push($set, $cols[$i].'='.$this->pdo->quote($data[$cols[$i]]));
166
        }
167
        $set = implode(',', $set);
168
        $sql = "UPDATE $tablename SET $set WHERE $where";
169
        if($this->pdo->exec($sql) === false)
170
        {
171
            return false;
172
        }
173
        return true;
174
    }
175
176 View Code Duplication
    function create($tablename, $data)
177
    {
178
        $set = array();
179
        if(is_object($data))
180
        {
181
            $data = (array)$data;
182
        }
183
        $cols = array_keys($data);
184
        $count = count($cols);
185
        for($i = 0; $i < $count; $i++)
186
        {
187
            array_push($set, $this->pdo->quote($data[$cols[$i]]));
188
        }
189
        $cols = implode(',', $cols);
190
        $set = implode(',', $set);
191
        $sql = "INSERT INTO $tablename ($cols) VALUES ($set);";
192
        if($this->pdo->exec($sql) === false)
193
        {
194
            return false;
195
        }
196
        return true;
197
    }
198
199
    function delete($tablename, $where)
200
    {
201
        $sql = "DELETE FROM $tablename WHERE $where";
202
        if($this->pdo->exec($sql) === false)
203
        {
204
            return false;
205
        }
206
        return true;
207
    }
208
209
    function raw_query($sql)
210
    {
211
        $stmt = $this->pdo->query($sql, \PDO::FETCH_ASSOC);
212
        if($stmt === false)
213
        {
214
            return false;
215
        }
216
        $ret = $stmt->fetchAll();
217
        return $ret;
218
    }
219
}
220
?>
221