Completed
Push — 2.0 ( bddf1c )
by Vermeulen
02:18
created

Sql   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 23
lcom 2
cbo 5
dl 0
loc 225
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSqlConnect() 0 4 1
A getPrefix() 0 4 1
A getLastInsertedId() 0 4 1
C getLastInsertedIdWithoutAI() 0 35 7
A select() 0 4 1
A insert() 0 4 1
A update() 0 4 1
A delete() 0 4 1
B create_id() 0 28 3
B query() 0 18 5
1
<?php
2
3
namespace BfwSql;
4
5
use \Exception;
6
7
/**
8
 * Class to access to query writer
9
 * 
10
 * @package bfw-sql
11
 * @author Vermeulen Maxime <[email protected]>
12
 * @version 2.0
13
 */
14
class Sql
15
{
16
    /**
17
     * @var \BfwSql\SqlConnect $sqlConnect SqlConnect object
18
     */
19
    protected $sqlConnect;
20
    
21
    /**
22
     * @var string $prefix Tables prefix
23
     */
24
    protected $prefix = '';
25
    
26
    /**
27
     * Constructor
28
     * 
29
     * @param \BfwSql\SqlConnect $sqlConnect SqlConnect instance
30
     * 
31
     * @throws \Exception
32
     */
33
    public function __construct($sqlConnect)
34
    {
35
        $this->sqlConnect = $sqlConnect;
36
        $this->prefix     = $sqlConnect->getConnectionInfos()->tablePrefix;
37
    }
38
    
39
    /**
40
     * Getter to the property sqlConnect
41
     * 
42
     * @return \BFWSql\SqlConnect
43
     */
44
    public function getSqlConnect()
45
    {
46
        return $this->sqlConnect;
47
    }
48
    
49
    /**
50
     * Getter to the property prefix
51
     * 
52
     * @return string
53
     */
54
    public function getPrefix()
55
    {
56
        return $this->prefix;
57
    }
58
    
59
    /**
60
     * Get the id for the last item has been insert in database
61
     * 
62
     * @param string|null $name (default: null) Name of the sequence for the id
63
     *  Used for SGDB like PostgreSQL. Not use it for mysql.
64
     * 
65
     * @return integer
66
     */
67
    public function getLastInsertedId($name=null)
68
    {
69
        return (int) $this->PDO->lastInsertId($name);
0 ignored issues
show
Bug introduced by
The property PDO does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
70
    }
71
    
72
    /**
73
     * Get the id for the last item has been insert in database for a table
74
     * without auto-increment
75
     * 
76
     * @param string       $table The table name
77
     * @param string       $colId The column name for the ID
78
     * @param string|array $order Columns to sort table content
79
     * @param string|array $where All where instruction used for filter content
80
     * 
81
     * @return integer
82
     */
83
    public function getLastInsertedIdWithoutAI(
84
        $table,
85
        $colId,
86
        $order,
87
        $where=''
88
    ) {
89
        $req = $this->select()
90
                    ->from($table, $colId)
91
                    ->limit(1);
92
    
93
        if (is_array($where)) {
94
            foreach ($where as $val) {
95
                $req->where($val);
96
            }
97
        } elseif ($where != '') {
98
            $req->where($where);
99
        }
100
        
101
        if (is_array($order)) {
102
            foreach ($order as $val) {
103
                $req->order($val);
104
            }
105
        } else {
106
            $req->order($order);
107
        }
108
        
109
        $res = $req->fetchRow();
110
        $req->closeCursor();
111
        
112
        if ($res) {
113
            return (int) $res[$colId];
114
        }
115
        
116
        return 0;
117
    }
118
    
119
    /**
120
     * Return a new instance of SqlSelect
121
     * 
122
     * @param string $type (default: "array") Return PHP type
123
     *  Possible value : "array" or "object"
124
     * 
125
     * @return \BfwSql\SqlSelect
126
     */
127
    public function select($type='array')
128
    {
129
        return new SqlSelect($this->sqlConnect, $type);
130
    }
131
    
132
    /**
133
     * Return a new instance of SqlInsert
134
     * 
135
     * @param string $table   The table concerned by the request
136
     * @param array  $columns (default: null) All datas to add
137
     *  Format is array('columnName' => 'value', ...);
138
     * 
139
     * @return \BfwSql\SqlInsert
140
     */
141
    public function insert($table, $columns=null)
142
    {
143
        return new SqlInsert($this->sqlConnect, $table, $columns);
144
    }
145
    
146
    /**
147
     * Return a new instance of SqlUpdate
148
     * 
149
     * @param string $table   The table concerned by the request
150
     * @param array  $columns (default: null) All datas to update
151
     *  Format is array('columnName' => 'newValue', ...);
152
     * 
153
     * @return \BfwSql\SqlUpdate
154
     */
155
    public function update($table, $columns=null)
156
    {
157
        return new SqlUpdate($this->sqlConnect, $table, $columns);
158
    }
159
    
160
    /**
161
     * Return a new instance of SqlDelete
162
     * 
163
     * @param string $table The table concerned by the request
164
     * 
165
     * @return \BfwSql\SqlDelete
166
     */
167
    public function delete($table)
168
    {
169
        return new SqlDelete($this->sqlConnect, $table);
170
    }
171
    
172
    /**
173
     * Find the first vacant id on a table and for a column
174
     * 
175
     * @param string $table  The table concerned by the request
176
     * @param string $column The id column. Must be an integer..
177
     * 
178
     * @throws \Exception If a error has been throw during the search
179
     * 
180
     * @return integer
181
     */
182
    public function create_id($table, $column)
183
    {
184
        $req = $this->select()
185
                    ->from($table, $column)
186
                    ->order($column.' ASC')
187
                    ->limit(1);
188
        
189
        $res = $req->fetchRow();
190
        $req->closeCursor();
191
        
192
        if (!$res) {
193
            return 1;
194
        }
195
        
196
        if ($res[$column] > 1) {
197
            return $res[$column]-1;
198
        }
199
        
200
        $req2 = $this->select()
201
                    ->from($table, $column)
202
                    ->order($column.' DESC')
203
                    ->limit(1);
204
        
205
        $res2 = $req2->fetchRow();
206
        $req2->closeCursor();
207
208
        return $res2[$column]+1;
209
    }
210
    
211
    /**
212
     * Run the query in parameter 
213
     * 
214
     * @param string $request The request to run
215
     * 
216
     * @throws \Exception If the request has failed
217
     * 
218
     * @return \PDOStatement
219
     */
220
    public function query($request)
221
    {
222
        $this->sqlConnect->upNbQuery();
223
        
224
        $req   = $this->sqlConnect->PDO->query($request);
0 ignored issues
show
Bug introduced by
The property PDO cannot be accessed from this context as it is declared protected in class BfwSql\SqlConnect.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
225
        $error = $this->sqlConnect->PDO->errorInfo();
0 ignored issues
show
Bug introduced by
The property PDO cannot be accessed from this context as it is declared protected in class BfwSql\SqlConnect.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
226
        
227
        if(
228
            !$req
229
            && $error[0] != null
230
            && $error[0] != '00000'
231
            && isset($error[2])
232
        ) {
233
            throw new Exception($error[2]);
234
        }
235
        
236
        return $req;
237
    }
238
}
239