Commit bfd09dfc8daf4117f37cec872413b69a0b568b9b
1 parent
637e6fff
+ SqlQuery.class.php
Showing
1 changed file
with
252 additions
and
0 deletions
Show diff stats
| 1 | +<?php | |
| 2 | + | |
| 3 | +class SqlQuery | |
| 4 | +{ | |
| 5 | + /** | |
| 6 | + * @var array list of data to build the query | |
| 7 | + */ | |
| 8 | + protected $query = array( | |
| 9 | + 'select' => array(), | |
| 10 | + 'from' => '', | |
| 11 | + 'join' => array(), | |
| 12 | + 'where' => array(), | |
| 13 | + 'group' => array(), | |
| 14 | + 'having' => array(), | |
| 15 | + 'order' => array(), | |
| 16 | + 'limit' => array('offset' => 0, 'limit' => 0), | |
| 17 | + ); | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * Add fields in query selection | |
| 21 | + * | |
| 22 | + * @param string $fields List of fields to concat to other fields | |
| 23 | + * @return DbQuery | |
| 24 | + */ | |
| 25 | + public function select ($fields) | |
| 26 | + { | |
| 27 | + if (! empty ($fields)) | |
| 28 | + { | |
| 29 | + $this->query['select'][] = $fields; | |
| 30 | + } | |
| 31 | + return $this; | |
| 32 | + } | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * Set table for FROM clause | |
| 36 | + * | |
| 37 | + * @param string $table Table name | |
| 38 | + * @return DbQuery | |
| 39 | + */ | |
| 40 | + public function from ($table, $alias = null) | |
| 41 | + { | |
| 42 | + if (! empty ($table)) | |
| 43 | + { | |
| 44 | + $this->query['from'][] = '`'.$table.'`'.($alias ? ' '.$alias : ''); | |
| 45 | + } | |
| 46 | + return $this; | |
| 47 | + } | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * Add JOIN clause | |
| 51 | + * E.g. $this->join('RIGHT JOIN '._DB_PREFIX_.'product p ON ...'); | |
| 52 | + * | |
| 53 | + * @param string $join Complete string | |
| 54 | + * @return DbQuery | |
| 55 | + */ | |
| 56 | + public function join ($join) | |
| 57 | + { | |
| 58 | + if (! empty ($join)) | |
| 59 | + { | |
| 60 | + $this->query['join'][] = $join; | |
| 61 | + } | |
| 62 | + return $this; | |
| 63 | + } | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * Add LEFT JOIN clause | |
| 67 | + * | |
| 68 | + * @param string $table Table name (without prefix) | |
| 69 | + * @param string $alias Table alias | |
| 70 | + * @param string $on ON clause | |
| 71 | + */ | |
| 72 | + public function leftJoin ($table, $alias = null, $on = null) | |
| 73 | + { | |
| 74 | + return $this->join('LEFT JOIN `'.$table.'`'.($alias ? ' `'.$alias.'`' : '').($on ? ' ON '.$on : '')); | |
| 75 | + } | |
| 76 | + | |
| 77 | + /** | |
| 78 | + * Add INNER JOIN clause | |
| 79 | + * E.g. $this->innerJoin('product p ON ...') | |
| 80 | + * | |
| 81 | + * @param string $table Table name (without prefix) | |
| 82 | + * @param string $alias Table alias | |
| 83 | + * @param string $on ON clause | |
| 84 | + */ | |
| 85 | + public function innerJoin ($table, $alias = null, $on = null) | |
| 86 | + { | |
| 87 | + return $this->join('INNER JOIN `'.$table.'`'.($alias ? ' '.$alias : '').($on ? ' ON '.$on : '')); | |
| 88 | + } | |
| 89 | + | |
| 90 | + /** | |
| 91 | + * Add LEFT OUTER JOIN clause | |
| 92 | + * | |
| 93 | + * @param string $table Table name (without prefix) | |
| 94 | + * @param string $alias Table alias | |
| 95 | + * @param string $on ON clause | |
| 96 | + */ | |
| 97 | + public function leftOuterJoin ($table, $alias = null, $on = null) | |
| 98 | + { | |
| 99 | + return $this->join('LEFT OUTER JOIN `'.$table.'`'.($alias ? ' '.$alias : '').($on ? ' ON '.$on : '')); | |
| 100 | + } | |
| 101 | + | |
| 102 | + /** | |
| 103 | + * Add NATURAL JOIN clause | |
| 104 | + * | |
| 105 | + * @param string $table Table name (without prefix) | |
| 106 | + * @param string $alias Table alias | |
| 107 | + */ | |
| 108 | + public function naturalJoin ($table, $alias = null) | |
| 109 | + { | |
| 110 | + return $this->join('NATURAL JOIN `'.$table.'`'.($alias ? ' '.$alias : '')); | |
| 111 | + } | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * Add a restriction in WHERE clause (each restriction will be separated by AND statement) | |
| 115 | + * | |
| 116 | + * @param string $restriction | |
| 117 | + * @return DbQuery | |
| 118 | + */ | |
| 119 | + public function where ($restriction) | |
| 120 | + { | |
| 121 | + if (! empty ($restriction)) | |
| 122 | + { | |
| 123 | + $this->query['where'][] = $restriction; | |
| 124 | + } | |
| 125 | + return $this; | |
| 126 | + } | |
| 127 | + | |
| 128 | + /** | |
| 129 | + * Add a restriction in HAVING clause (each restriction will be separated by AND statement) | |
| 130 | + * | |
| 131 | + * @param string $restriction | |
| 132 | + * @return DbQuery | |
| 133 | + */ | |
| 134 | + public function having ($restriction) | |
| 135 | + { | |
| 136 | + if (! empty ($restriction)) | |
| 137 | + { | |
| 138 | + $this->query['having'][] = $restriction; | |
| 139 | + } | |
| 140 | + return $this; | |
| 141 | + } | |
| 142 | + | |
| 143 | + /** | |
| 144 | + * Add an ORDER B restriction | |
| 145 | + * | |
| 146 | + * @param string $fields List of fields to sort. E.g. $this->order('myField, b.mySecondField DESC') | |
| 147 | + * @return DbQuery | |
| 148 | + */ | |
| 149 | + public function orderBy ($fields) | |
| 150 | + { | |
| 151 | + if (! empty ($fields)) | |
| 152 | + { | |
| 153 | + $this->query['order'][] = $fields; | |
| 154 | + } | |
| 155 | + return $this; | |
| 156 | + } | |
| 157 | + | |
| 158 | + /** | |
| 159 | + * Add a GROUP BY restriction | |
| 160 | + * | |
| 161 | + * @param string $fields List of fields to sort. E.g. $this->group('myField, b.mySecondField DESC') | |
| 162 | + * @return DbQuery | |
| 163 | + */ | |
| 164 | + public function groupBy ($fields) | |
| 165 | + { | |
| 166 | + if (! empty ($fields)) | |
| 167 | + { | |
| 168 | + $this->query['group'][] = $fields; | |
| 169 | + } | |
| 170 | + return $this; | |
| 171 | + } | |
| 172 | + | |
| 173 | + /** | |
| 174 | + * Limit results in query | |
| 175 | + * | |
| 176 | + * @param string $fields List of fields to sort. E.g. $this->order('myField, b.mySecondField DESC') | |
| 177 | + * @return DbQuery | |
| 178 | + */ | |
| 179 | + public function limit ($limit, $offset = 0) | |
| 180 | + { | |
| 181 | + $offset = (int)$offset; | |
| 182 | + if ($offset < 0) | |
| 183 | + { | |
| 184 | + $offset = 0; | |
| 185 | + } | |
| 186 | + | |
| 187 | + $this->query['limit'] = array( | |
| 188 | + 'offset' => $offset, | |
| 189 | + 'limit' => (int)$limit, | |
| 190 | + ); | |
| 191 | + return $this; | |
| 192 | + } | |
| 193 | + | |
| 194 | + /** | |
| 195 | + * Generate and get the query | |
| 196 | + * | |
| 197 | + * @return string | |
| 198 | + */ | |
| 199 | + public function build () | |
| 200 | + { | |
| 201 | + $sql = 'SELECT '.((($this->query['select'])) ? implode (",\n", $this->query['select']) : '*')."\n"; | |
| 202 | + | |
| 203 | + if (! $this->query['from']) | |
| 204 | + { | |
| 205 | + die('DbQuery->build() missing from clause'); | |
| 206 | + } | |
| 207 | + $sql .= 'FROM '.implode (', ', $this->query['from'])."\n"; | |
| 208 | + | |
| 209 | + if ($this->query['join']) | |
| 210 | + { | |
| 211 | + $sql .= implode ("\n", $this->query['join'])."\n"; | |
| 212 | + } | |
| 213 | + | |
| 214 | + if ($this->query['where']) | |
| 215 | + { | |
| 216 | + $sql .= 'WHERE ('.implode (') AND (', $this->query['where']).")\n"; | |
| 217 | + } | |
| 218 | + | |
| 219 | + if ($this->query['group']) | |
| 220 | + { | |
| 221 | + $sql .= 'GROUP BY '.implode(', ', $this->query['group'])."\n"; | |
| 222 | + } | |
| 223 | + | |
| 224 | + if ($this->query['having']) | |
| 225 | + { | |
| 226 | + $sql .= 'HAVING ('.implode (') AND (', $this->query['having']).")\n"; | |
| 227 | + } | |
| 228 | + | |
| 229 | + if ($this->query['order']) | |
| 230 | + { | |
| 231 | + $sql .= 'ORDER BY '.implode (', ', $this->query['order'])."\n"; | |
| 232 | + } | |
| 233 | + | |
| 234 | + if ($this->query['limit']['limit']) | |
| 235 | + { | |
| 236 | + $limit = $this->query['limit']; | |
| 237 | + $sql .= 'LIMIT '.(($limit['offset']) ? $limit['offset'].', '.$limit['limit'] : $limit['limit']); | |
| 238 | + } | |
| 239 | +/* | |
| 240 | + ob_start(); | |
| 241 | + var_dump($sql); | |
| 242 | + echo ob_get_clean(); | |
| 243 | +*/ | |
| 244 | + return $sql; | |
| 245 | + } | |
| 246 | + | |
| 247 | + public function __toString () | |
| 248 | + { | |
| 249 | + return $this->build(); | |
| 250 | + } | |
| 251 | +} | |
| 252 | + | ... | ... |