query-generator-class

Gesponsorde koppelingen

PHP script bestanden

  1. query-generator-class

« Lees de omschrijving en reacties

voorbeeldcode.php

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('class.query.php');
define('EOL', "\n");

/* Examle query's */

// vb1: insert met opgave van de velden en de waarden zijn telkens een array

$query[] = Query::insert('users',array('username','password','email'))
            ->
values( array('Hipska','test','email'), array('Steffan','naffetS','mail2') );

// vb2: insert zonder opgave van de velden en de waarden zitten in 1 grote array
$query[] = Query::insert('users') // geef je geen velden op, dan moet in de waarden alle velden van de tabel zitten
            ->values( array( array('Hipska','test','email') , array('Steffan','naffetS','mail2') ) );

// vb3: select meerdere tabellen door join en een limit
$query[] = Query::select( array('id','page','time'), array('username','email') )->from('log')
            ->
join('users','userid','id')->limit(30,10);

// vb4: select met union en een order by
$query[] = Query::select('id','title','time')->from('posts')->order(array('time' => 'DESC'))
            ->
union('all')->from('comments')->order(array('time' => 'ASC'));

// vb5: select met where statement
$query[] = Query::select(array('id','username'))->from('users')
            ->
where(array('permission' => 'mod','admin','smod'), 'OR');

// vb6: update met where volgens 1e methode
$query[] = Query::update('users')->set(array('permission' => 'admin'))
            ->
where(array('id' => 2, 'permission' => 'mod'));

// vb7: delete met where volgens de 2de methode
$query[] = Query::delete('users')->where('id > 2');

// vb8: volledige tabel legen ( kan ook met from() )
$query[] = Query::delete('log');

// vb9: tabel verwijderen
$query[] = Query::drop('users');

/* Results of the class */
echo '<pre>' . EOL;
foreach ($query as $sql) echo $sql . EOL;    
highlight_file(__FILE__);
highlight_file('class.query.php');
echo '</pre>';

?>

Results:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
INSERT INTO users (username, password, email) VALUES ( 'Hipska', 'test', 'email' ), ( 'Steffan', 'naffetS', 'mail2' )
INSERT INTO users VALUES ( 'Hipska', 'test', 'email' ), ( 'Steffan', 'naffetS', 'mail2' )
SELECT log.id, log.page, log.time, users.username, users.email FROM log JOIN users ON ( log.userid = users.id ) LIMIT 30, 10
SELECT id, title, time FROM posts ORDER BY time DESC UNION ALL SELECT id, title, time FROM comments ORDER BY time ASC
SELECT id, username FROM users WHERE (permission = 'mod' OR permission = 'admin' OR permission = 'smod')
UPDATE users SET permission = 'admin' WHERE (id = '2' AND permission = 'mod')
DELETE FROM users WHERE id > 2
DELETE FROM log
DROP TABLE users

class.query.php
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php

/**
 * class Query
 *
 * Maak snel en overzichtelijk een query
 *
 * @author Hipska
 * @link http://www.hipska.be.tc
 */


class Query {
    
    private $query = '';
    private $tables = array();
    private $fields = array();
    
    
    /**
     * Query::__construct()
     *
     * Constructor: Maak een nieuwe Query instantie.
     *
     * @param string $sql: string om de query mee te beginnen
     * @author Hipska
     */

    public function __construct( $sql = '' ){
        $this->query = $sql;
    }

    
    /**
     * Query::__toString()
     *
     * geef de de query weer.
     *
     * @return string: de volledige query
     */

    public function __toString() {
        return $this->query;
    }

    
    /**
     * Query::select()
     *
     * Maak een SELECT query.
     * Als parameter vul je niets in om alle velden te selecteren.
     * Wil je meerdere velden selecteren, dan kan je een array met de velden opgeven
     * of de velden als aparte parameter invullen.
     * Wil je velden van verschillende tabellen selecteren,
     * dan geef je per tabel een arry met de velden.
     *
     * @param mixed $fields: veld(en) om te selecteren uit de tabel(len)
     * @access public
     * @author Hipska
     */

    public function select( $fields = '*' ) {
        if(is_array($fields) and func_num_args() > 1){
            $fields = array(); $tables = 0;
            foreach (func_get_args() as $field) {
                $table = 't'.++$tables.'.';
                $fields[] = $table . implode(', '.$table, $field);
            }
        }
elseif(!is_array($fields)){
            $fields = func_get_args();
        }

        
        $query = new self('SELECT ' . implode( ', ', $fields ) . ' ');
        $query->fields = $fields;
        return $query;
    }

    
    /**
     * Query::update()
     *
     * Maak een UPDATE query.
     *
     * @param string $table: de tabel welke je wil updaten
     * @access public
     * @author Hipska
     */

    public function update( $table ){
        return new self('UPDATE ' . $table . ' ' );
    }

    
    /**
     * Query::delete()
     *
     * Maak een DELETE query.
     * Laat $table leeg om alle rijen van een tabel te wissen.
     * ( Query::from() is dan wel nog nodig om de tabel te kiezen )
     *
     * @param string $table: tabel waar er rijen uit verwijdert moeten worden
     * @access public
     * @author Hipska
     */

    public function delete( $table = null ){
        return new self('DELETE' . (is_null($table)? ' ' : ' FROM ' . $table . ' ') );
    }

    
    /**
     * Query::insert()
     *
     * Maak een INSERT query.
     * kies in welke tabel en welke velden er in
     * de rijen staan die ingevoegd moeten worden
     *
     * @param string $table: tabel waar er rijen ingevoegd moeten worden
     * @param array $fields: velden waarvan er waarden van zullen ingevoegd worden
     * @access public
     * @author Hipska
     */

    public function insert( $table, $fields = null){
        $sql = 'INSERT INTO ' . $table . ' ';
        if(!is_null($fields)){
            $sql .= '(' . implode(', ', $fields) . ') ';
        }

        return new self($sql);
    }

    
    /**
     * Query::drop()
     *
     * Maak een DROP query.
     *
     * @param string $table: tabel die verwijdert moet worden
     * @access public
     * @author Hipska
     */

    public function drop( $table ){
        return new self('DROP TABLE ' . $table );
    }

    
    /**
     * Query::create()
     *
     * Maak een CREATE query.
     * Niet af! Reden: te complex.
     *
     * @param string $table: naam van de tabel die aangemaakt moet worden
     * @access public
     * @author Hipska
     */

    public function create( $table ){
        return new self('CREATE TABLE ' . $table . ' ' );
    }

        
    
    /**
     * Query::from()
     *
     * Kies met welke tabel(len) er gewerkt moet worden.
     *
     * @param mixed $tables: tabel(en) waarmee gewerkt moet worden
     * @access public
     * @author Hipska
     */

    public function from( $tables ) {
        if(!is_array($tables)) $tables = func_get_args();
        $this->query .= 'FROM ' . implode( ', ', $tables ) . ' ';
        /* niet af
        foreach ($tables as $table) { //overloop de tabellen om een AS te zoeken
            
            $this->table[] = $table;
        }
        */

        $this->tables = $tables;
        foreach ($this->tables as $t => $tname) {
            $this->query = str_ireplace('t'.($t+1).'.', $tname.'.', $this->query);
        }

        return $this;
    }

    
    /**
     * Query::set()
     *
     * Stel de waarden van de velden om te updaten in.
     * Hierbij zijn de keys van de array de naam van het veld
     * en de values van de array zijn de in te stellen waarden.
     *
     * @param array $fields: de velden die je wil updaten
     * @access public
     * @author Hipska
     */

    public function set( Array $fields ){
        $set = array();
        
        foreach ($fields as $field => $value) {
            $set[] = $field . " = '" . $value . "'";
        }

        
        $this->query .= 'SET ' . implode( ', ', $set) . ' ';
        return $this;
    }

    
    /**
     * Query::values()
     *
     * Geef de waarden op om een rij mee in te voegen.
     * Wil je meerdere rijen invoegen, dan kan je een
     * lijst van array's of een array van array's opgeven.
     *
     * @param array $valuelist: array met waarden om in te voegen
     * @access public
     * @author Hipska
     */

    public function values( Array $valuelist ){
        
        $this->query .= 'VALUES ';
        $values = array();
        
        if( func_num_args() > 1 ){
            $valuelist = func_get_args();
        }

        
        foreach ($valuelist as $value) {
            $values[] = "( '" . implode("', '", $value) . "' )";
        }

        
        $this->query .= implode(', ', $values);
        return $this;
    }

    
    /**
     * Query::where()
     *
     * @param mixed $condition: de velden die moeten vergeleken worden
     * @param string $operator: de operator voor deze vergelijkingen ( =, <, >, <=, >=, <>, .. )
     * @param string $logic: de logische operator voor verschillende vergelijkininen (AND, OR)
     * @access public
     * @author Hipska
     */

    public function where( $condition, $operator = '=', $logic = 'AND' ) {
        $conditions = array();
        if($operator == 'AND' or $operator == 'OR'){
            $logic = $operator;
            $operator = '=';
        }

        
        if(is_array($condition)){
            foreach( $condition as $column => $value ) {
                if(is_string($column)){
                    $conditions[] = $column . ' ' . $operator . " '" . $value . "'";
                    $prev_column = $column;
                }
else{
                    $conditions[] = $prev_column . ' ' . $operator . " '" . $value . "'";
                }
            }
        }
else{
            $conditions = func_get_args();
        }

        
        if(count($conditions) <= 1) $this->query .= 'WHERE ' . implode( $logic . ' ', $conditions ) . ' ';
        else $this->query .= 'WHERE (' . implode(' ' . $logic . ' ', $conditions ) . ') ';
        
        return $this;
    }

    
    /**
     * Query::or_condition()
     *
     * @param mixed $condition: de velden die moeten vergeleken worden
     * @param string $operator: de operator voor deze vergelijkingen ( =, <, >, <=, >=, <>, .. )
     * @param string $logic: de logische operator voor verschillende vergelijkininen (AND, OR)
     * @access public
     * @author Hipska
     */

    public function or_condition( $condition, $operator = '=', $logic = 'AND' ) {
        $conditions = array();
        if($operator == 'AND' or $operator == 'OR'){
            $logic = $operator;
            $operator = '=';
        }

        
        if(is_array($condition)){
            foreach( $condition as $column => $value ) {
                if(is_string($column)){
                    $conditions[] = $column . ' ' . $operator . " '" . $value . "'";
                    $prev_column = $column;
                }
else{
                    $conditions[] = $prev_column . ' ' . $operator . " '" . $value . "'";
                }
            }
        }
else{
            $conditions = func_get_args();
        }

        
        if(count($conditions) <= 1) $this->query .= 'OR ' . implode( $logic . ' ', $conditions ) . ' ';
        else $this->query .= 'OR (' . implode(' ' . $logic . ' ', $conditions ) . ') ';
        
        return $this;
    }

    
    /**
     * Query::and_condition()
     *
     * @param mixed $condition: de velden die moeten vergeleken worden
     * @param string $operator: de operator voor deze vergelijkingen ( =, <, >, <=, >=, <>, .. )
     * @param string $logic: de logische operator voor verschillende vergelijkininen (AND, OR)
     * @access public
     * @author Hipska
     */

    public function and_condition( $condition, $operator = '=', $logic = 'OR' ) {
        $conditions = array();
        if($operator == 'AND' or $operator == 'OR'){
            $logic = $operator;
            $operator = '=';
        }

        
        if(is_array($condition)){
            foreach( $condition as $column => $value ) {
                if(is_string($column)){
                    $conditions[] = $column . ' ' . $operator . " '" . $value . "'";
                    $prev_column = $column;
                }
else{
                    $conditions[] = $prev_column . ' ' . $operator . " '" . $value . "'";
                }
            }
        }
else{
            $conditions = func_get_args();
        }

        
        if(count($conditions) <= 1) $this->query .= 'AND ' . implode( $logic . ' ', $conditions ) . ' ';
        else $this->query .= 'AND (' . implode(' ' . $logic . ' ', $conditions ) . ') ';
        
        return $this;
    }

    
    /**
     * Query::join()
     *
     * Kies met met welke tabel en welke voorwaarde er gejoind moet worden.
     *
     * @param string $table: tabel waarmee gejoind moet worden
     * @param string $cond1: veld van de eerste tabel om te vergelijken
     * @param string $cond2: veld van de join-tabel om te vergelijken
     * @param string $type: het type join (INNER, LEFT, RIGHT)
     * @access public
     * @author Hipska
     */

    public function join( $table, $cond1, $cond2 = '', $type = '' ){
        $this->tables[] = $table; // add table to tables pool
        $this->query .= ltrim($type . ' JOIN '. $table . ' ');
        
        if( $cond2 == $cond1 or $cond2 == ''){
            $this->query .= 'USING ( ' . $cond1 . ' ) ';
        }
else{
            $cond1 = (strpos($cond1, '.') === false)? $this->tables[0] . '.' . $cond1 : $cond1;
            $cond2 = (strpos($cond2, '.') === false)? $table . '.' . $cond2 : $cond2;
            
            $this->query .= 'ON ( ' . $cond1 . ' = ' . $cond2 . ' ) ';
        }

        
        foreach ($this->tables as $t => $tname) {
            $this->query = str_ireplace('t'.($t+1).'.', $tname.'.', $this->query);
        }

        return $this;
    }

    
    /**
     * Query::union()
     *
     * Een query van de ene tabel met een query van een andere tabel samenvoegen
     *
     * @param mixed $all: true of 'all' om UNION ALL te doen, leeg of false om gewone UNION te doen
     * @access public
     * @author Hipska
     */

    public function union($all = false){
        $this->query .= 'UNION ';
        if($all) $this->query .= 'ALL ';
        
        $this->query .= 'SELECT ' . implode( ', ', $this->fields) . ' ';
        
        return $this;
    }

    
    /**
     * Query::limit()
     *
     * het aantal resultaten limiteren
     * ( als rowcount leeg is, dan is offset het aantal rijen )
     *
     * @param int $offset: vanaf de hoeveelste rij
     * @param int $rowcount: het aantal rijen
     * @access public
     * @author Hipska
     */

    public function limit( $offset, $rowcount = true ){
        $this->query .= 'LIMIT ' . $offset;
        
        if($rowcount !== true){
            $this->query .= ', ' . $rowcount;
        }

        
        return $this;
    }

    
    /**
     * Query::order()
     *
     * Stel in op welke velden gesorteerd moet worden.
     * deze array heeft telkens als key het veld dat
     * gesorteerd moet worden en als value de volgorde
     * (oplopend = ASC, aflopend = DESC)
     *
     * @param array $by: array met sorteervolgorden
     * @access public
     * @author Hipska
     */

    public function order( Array $by ){
        $this->query .= 'ORDER BY ';
        $orders = array();
        foreach ($by as $field => $order) {
            $orders[] = $field . ' ' . $order;
        }

        $this->query .= implode( ', ', $orders) . ' ';
        return $this;
    }
}


?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.