frosted-mysql-library /classes/mysql.query.update.class.php

Language PHP Lines 401
MD5 Hash fc12e4077f90a83c11c5b8914f3c090b Estimated Cost $5,932 (why?)
Repository https://github.com/magedeveloper/frosted-mysql-library.git View Raw File
  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
<?php
/**
 * Frosted MySQL Library Update Query Class
 * - - - - - - - - - -
 * Add "UPDATE" functionality to Frosted MySQL Library and will not work without them.
 * - - - - - - - - - -
 * Licensed under MIT license
 * - - - - - - - - - -
 * @Creator  Daniel 'Eisbehr' Kern
 * @Require  PHP5
 * @Version  3.0
 * @Date     01.08.2013
 * @Update   01.08.2013
 * - - - - - - - - - -
 */
class mysqlClass_Update extends mysqlClass_Abstract implements mysqlClass_Queries
{
	/**
	 * update error messages
	 * @var string
	 */
	const MESSAGE_ORDER = "you cannot use 'order' if you update more than one table";
	const MESSAGE_LIMIT = "you cannot use 'limit' if you update more than one table";



	/*
	** public 
	*/



	/**
	 * reset update class
	 * @param boolean $format
	 * @return mysqlClass_Update
	 */
	public function resetQuery($format)
	{
		parent::resetQuery($format);

		$this->query["tables"] = array();
		$this->query["low"]    = false;
		$this->query["ignore"] = false;
		$this->query["set"]    = array();
		$this->query["where"]  = array();
		$this->query["order"]  = array();
		$this->query["limit"]  = NULL;
		
		return $this;
	}



	/*
	** query related
	*/



	/**
	 * add 'low priority' to query
	 * @param boolean $low
	 * @return mysqlClass_Update
	 */
	public function lowPriority($low = true)
	{
		$this->query["low"] = (bool)$low;
		return $this;
	}

	/**
	 * add 'ignore' to query
	 * @param boolean $ignore
	 * @return mysqlClass_Update
	 */
	public function ignore($ignore = true)
	{
		$this->query["ignore"] = (bool)$ignore;
		return $this;
	}

	/**
	 * add tables to query
	 * @param string|array $table
	 * @return mysqlClass_Update
	 */
	public function table($table)
	{
		// only one string is set
		if( func_num_args() == 1 && is_string($table) )
		{
			$this->query["tables"][] = $table;
			return $this;
		}

		// add all tables to query
		foreach( func_get_args() as $param )
		{
			if( !is_array($param) )
				$this->query["tables"][] = $param;
			else
				foreach( $param as $database => $name )
				{
					if( !is_numeric($database) )
						$this->query["tables"][] = $database . " AS " . $name;
					else
						$this->query["tables"][] = $name;
				}
		}
		
		return $this;
	}
	
	/**
	 * add 'set' to query
	 * @param string|array $column
	 * @param string $replace
	 * @return mysqlClass_Update
	 */
	public function set($column, $replace = NULL)
	{
		if( is_null($replace) )
		{
			if( is_array($column) )
			{
				foreach( $column as $_column => $_replace )
				{
					if( !is_numeric($_column) )
						$this->query["set"][] = str_replace("?", $this->parent->escape($_replace), $_column);
					else
						$this->query["set"][] = $_replace;
				}
			}
			else
			{
				$this->query["set"][] = $column;
			}
		}
		else
		{
			$this->query["set"][] = str_replace("?", $this->parent->escape($replace), $column);
		}
		
		return $this;
	}

	/**
	 * add 'where' to query
	 * @param string $condition
	 * @param string $replace
	 * @param string $nextRelation
	 * @return mysqlClass_Update
	 */
	public function where($condition, $replace = NULL, $nextRelation = mysqlClass::WHERE_AND)
	{
		// add condition
		if( !is_null($replace) )
		{
			if( is_array($replace) )
			{
				// escape all values
				foreach( $replace as &$value ) $value = $this->parent->escape($value);
				
				// format sub-query
				if( $this->format )
				{
					$condition = str_replace("ANY(?)", "\nANY\n(\n    ?\n)", $condition);
					$condition = str_replace("IN(?)", "\nIN\n(\n    ?\n)", $condition);
					$condition = str_replace("SOME(?)", "\nSOME\n(\n    ?\n)", $condition);
				}

				$glue = $this->format ? ",\n    " : ",";
				$this->query["where"][] = str_replace("?", join($glue, $replace), $condition);
			}
			else if( $replace instanceof mysqlClass_Select )
				$this->query["where"][] = array($condition, $replace);
			else
				$this->query["where"][] = str_replace("?", $this->parent->escape($replace), $condition);
		}
		else
			$this->query["where"][] = $condition;

		// add next relation
		if( strtoupper($nextRelation) == mysqlClass::WHERE_OR )
			$this->query["where"][] = mysqlClass::WHERE_OR;
		else
			$this->query["where"][] = mysqlClass::WHERE_AND;

		return $this;
	}

	/**
	 * add 'or' related 'where' to query
	 * @param string $condition
	 * @param string $replace
	 * @param string $nextRelation
	 * @return mysqlClass_Update
	 */
	public function orWhere($condition, $replace = NULL, $nextRelation = mysqlClass::WHERE_AND)
	{
		if( !empty($this->query["where"]) )
			$this->query["where"][(count($this->query["where"]) - 1)] = mysqlClass::WHERE_OR;
		
		return $this->where($condition, $replace, $nextRelation);
	}
	
	/**
	 * add 'order' to query
	 * @param string $field
	 * @param string $order
	 * @return mysqlClass_Update
	 */
	public function orderBy($field, $order = mysqlClass::ORDER_ASC)
	{
		if( count($this->query["tables"]) >= 2 )
		{
			$this->parent->createError(self::MESSAGE_ORDER);
			return $this;
		}

		if( strtoupper($order) == mysqlClass::ORDER_DESC )
			$this->query["order"][] = $field . " " . mysqlClass::ORDER_DESC;
		else
			$this->query["order"][] = $field . " " . mysqlClass::ORDER_ASC;

		return $this;
	}

	/**
	 * alias of 'orderBy'
	 * @param string $field
	 * @param string $order
	 * @return mysqlClass_Update
	 */
	public function order($field, $order = mysqlClass::ORDER_ASC)
	{
		return $this->orderBy($field, $order);
	}

	/**
	 * add 'limit' to query
	 * @param integer $limit
	 * @return mysqlClass_Update
	 */
	public function limit($limit)
	{
		if( count($this->query["tables"]) >= 2 )
		{
			$this->parent->createError(self::MESSAGE_LIMIT);
			return $this;
		}

		$this->query["limit"] = $limit;

		return $this;
	}



	/*
	** build
	*/



	/**
	 * build mysql update query string
	 * @param integer $formatOffset
	 * @return string
	 */
	public function build($formatOffset = 0)
	{
		$this->formatOffset += $formatOffset;
		$offset = str_pad("", $this->formatOffset, " ");

		// end if no table is set
		if( empty($this->query["tables"]) ) return NULL;

		$query = $this->format ? $offset . "UPDATE " : "UPDATE ";

		// low priority
		if( $this->query["low"] ) $query .= "LOW_PRIORITY ";

		// ignore
		if( $this->query["ignore"] ) $query .= "IGNORE ";

		$query .= $this->format ? $offset . "\n" : NULL;

		// tables
		if( count($this->query["tables"]) == 1 )
			$query .= $this->format ? $offset . "    " . $this->query["tables"][0] . "\n" : $this->query["tables"][0] . " ";
		else if( $this->format )
			for( $i = 0; $i < count($this->query["tables"]); $i++ )
			{
				$query .= $offset . "    " . $this->query["tables"][$i] . "";
				$query .= $i < count($this->query["tables"]) - 1 ? "," : NULL;
				$query .= "\n";
			}
		else
			$query .= join(",", $this->query["tables"]) . " ";

		// set
		if( !empty($this->query["set"]) )
		{
			$query .= $this->format ? "SET\n" : "SET ";

			if( $this->format )
				for( $i = 0; $i < count($this->query["set"]); $i++ )
				{
					$query .= $offset . "    " . $this->query["set"][$i] . "";
					$query .= $i < count($this->query["set"]) - 1 ? ", \n" : NULL;
				}
			else
				$query .= join(",", $this->query["set"]);

			$query .= $this->format ? "\n" : " ";
		}

		// where
		if( !empty($this->query["where"]) )
		{
			if( $this->format )
			{
				$query .= $offset . "WHERE \n";

				for( $i = 0; $i < count($this->query["where"]); $i = $i + 2 )
				{
					if( is_array($this->query["where"][$i]) )
					{
						$select = $this->query["where"][$i][1];

						if( $select instanceof mysqlClass_Select )
						{
							$select = $select->build($this->formatOffset + 4);
							$select = trim($select);
						}

						$query .= $offset . "    " . str_replace("?", "\n" . $offset . "    (" . $select . ")", $this->query["where"][$i][0]);
						$query .= $i < count($this->query["where"]) - 2 ? " \n" . $offset . $this->query["where"][$i + 1] . " " : NULL;
						$query .= " \n";
					}
					else
					{
						$query .= $offset . "    " . $this->query["where"][$i];
						$query .= $i < count($this->query["where"]) - 2 ? " \n" . $offset . $this->query["where"][$i + 1] . " " : NULL;
						$query .= " \n";
					}
				}
			}
			else
			{
				for( $i = 0; $i < count($this->query["where"]); $i = $i + 2 )
				{
					if( is_array($this->query["where"][$i]) )
					{
						$select = $this->query["where"][$i][1];

						if( $select instanceof mysqlClass_Select )
							$select = $select->build();

						$this->query["where"][$i]  = str_replace("?", "(" . $select . ")", $this->query["where"][$i][0]);
					}
				}

				$where  = array_slice($this->query["where"], 0, -1);
				$query .= "WHERE " . join(" ", $where) . " ";
			}
		}

		// add order
		if( !empty($this->query["order"]) && count($this->query["tables"]) == 1 )
		{
			if( $this->format )
			{
				$query .= $offset . "ORDER BY \n";

				for( $i = 0; $i < count($this->query["order"]); $i++ )
				{
					$query .= $offset . "    " . $this->query["order"][$i];
					$query .= $i < count($this->query["order"]) - 1 ? "," : NULL;
					$query .= " \n";
				}
			}
			else
			{
				$query .= "ORDER BY " . join(",", $this->query["order"]) . " ";
			}
		}

		// add limit
		if( !empty($this->query["limit"]) && count($this->query["tables"]) == 1 )
		{
			$query .= $this->format ? $offset . "LIMIT \n" . $offset  . "    " : "LIMIT ";
			$query .= $this->query["limit"];
		}

		return $query;
	}
}
Back to Top