queryBuilder.txt

Gesponsorde koppelingen

PHP script bestanden

  1. dao.txt
  2. queryBuilder.txt
  3. db.txt
  4. voorbeeldtabel.txt

« Lees de omschrijving en reacties

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
<?php
/**
 * Class queryBuilder
 * @uses db
 *
 */

class queryBuilder {
    
    /**
     * Builds a simple select query
     * with a WHERE, IN, NOT IN
     * ORDER ASC, DESC and LIMIT
     *
     *
     * @param $table string           
     * @param $filter array           
     * @param $filter_way array           
     * @param $order_field array           
     * @throws Exception
     * @return $string;
     * @return &reference $db
     */

    public static function buildSelect(&$db, $table, $filter = array(), $filter_way = array(), $order_field = array(), $limit = array()) {
        
        $db = db::get ();
        
        // First part of select
        $q = "SELECT * FROM " . $db->real_escape_string ( $table ) . " ";
        
        // Build the where
        
        if(! empty ( $filter )){
            
            $q .= 'WHERE ';
            $c = 1;
            foreach($filter as $field=>$val){
                $q .= $db->real_escape_string ( $field ) . ' ';
                
            
                if(array_key_exists ( $field, $filter_way )){
                    
                    switch ($filter_way [$field]){
                        case
dao::WHERE_IN :
                            
                            if(! is_array ( $filter [$field] ))
                                throw new Exception ( 'Filter for' . $field . 'expects an array as parameter' );
                            
                            $q .= 'IN (';
                            
                            $i = 1;
                            foreach($filter [$field] as $value){
                                $q .= "'" . $db->real_escape_string ( $value ) . "'";
                                $q .= ($i == sizeof ( $filter [$field] ) ? '' : ',');
                                $i ++;
                            }

                            
                            $q .= ') ';
                        
                        break;
                        
                        case
dao::WHERE_NOT :
                            
                            if(! is_array ( $filter [$field] ))
                                throw new Exception ( 'Filter for' . $field . 'expects an array as parameter' );
                            
                            $q .= 'NOT IN (';
                            
                            $i = 1;
                            
                            foreach($filter [$field] as $value){
                                $q .= "'" . $db->real_escape_string ( $value ) . "'";
                                $q .= ($i == sizeof ( $filter [$field] ) ? '' : ',');
                                $i ++;
                            }

                            
                            $q .= ') ';
                        
                        break;
                    }
                
                }
else{
                    $q .= "= '" . $db->real_escape_string ( $val ) . "' ";
                
                }

                
                if($c != sizeof ( $filter )){
                    $q .= 'AND ';
                }

                
                $c ++;
            
            }
            
        
            
        }

        
        if(!empty($order_field)){
            switch($order_field[0]){
                case
dao::ORDER_ASC:
                    $q .= 'ORDER BY '.key($order_field[0]) . ' ASC';
                    break;
                case
dao::ORDER_DESC:
                    $q .= 'ORDER BY '.key($order_field[0]) . ' DESC';
                    break;
        
            }
        }

        
        return $q;
    
    }

        
        public static function buildInsert(&$db, $oTable ){
            $db = db::get();
            $q =  "INSERT INTO ".$oTable->table." ";
            $class = get_class($oTable);
             $q .= '(';        
            $i = 1;
            
            foreach($class::$arTableFields as $key => $value){
                
                if($oTable->primaryKey == $class::$arTableFields[$key])
                    continue;
                if(empty($oTable->arAssoc[$key]))
                    continue;
                if(in_array($key, db::$reservedMysqlWords))
                    $key = "`".$key."`";
                    
                $q .= $key;    
                $q .= ($i == sizeof($oTable->arAssoc) ? '' : ',');
                $i++;
            }

            
            $q.= ')';
            $q.= ' VALUES (';
            
            
            $i = 1;
            foreach($class::$arTableFields as $key => $value){
        
                if($oTable->primaryKey == $class::$arTableFields[$key])
                    continue;
                if(empty($oTable->arAssoc[$key]))
                    continue;            
                $q .= (!empty($oTable->arAssoc[$key]) ? (is_numeric($oTable->arAssoc[$key]) ? $oTable->arAssoc[$key] : "'".$db->real_escape_string($oTable->arAssoc[$key])."'" ) : "' '");
                    
                $q .= ($i == sizeof($oTable->arAssoc) ? '' : ',');
                $i++;
                    
                
            }

            
            $q.= ') ';
            return $q;
        }

        
        public static function buildUpdate(&$db, $oTable ){
            $db = db::get();
            $q =  "UPDATE ".$oTable->table." ";
            $class = get_class($oTable);
            $q .= 'SET ';
            $i = 1;
                
            foreach($class::$arTableFields as $key => $value){
        
                if($oTable->primaryKey == $class::$arTableFields[$key])
                    continue;
                if(empty($oTable->arAssoc[$key]))
                    continue;
                if(in_array($key, db::$reservedMysqlWords))
                    $key = "`".$key."`";
                    
                $q .= $key .' = '. (!empty($oTable->arAssoc[$key]) ? (is_numeric($oTable->arAssoc[$key]) ? $oTable->arAssoc[$key] : "'".$db->real_escape_string($oTable->arAssoc[$key])."'" ) : "' '");
                    $q .= ($i == sizeof($oTable->arAssoc) ? '' : ',');
                $i++;
            }

                
            $q .= ' WHERE ' . $oTable->primaryKey . ' = ' .$oTable->arAssoc[$oTable->primaryKey];

            return $q;
        }

        public static function buildDelete(&$db, $oTable ){
            $db = db::get();
            $q =  "DELETE FROM ".$oTable->table." ";
            $q .= ' WHERE ' . $oTable->primaryKey . ' = ' .$oTable->arAssoc[$oTable->primaryKey];
        
            return $q;
        }

        

    public static function simpleSelect($table, $primkey, $val){
        return 'SELECT * FROM '.$table.' WHERE '.$primkey . ' = ' . $val ;        
    }

}

 
 

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.