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

Language PHP Lines 561
MD5 Hash 5c1b22c2d323c6b92921430bb9d0a687
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<?php
/**
 * Frosted MySQL Library Insert Query Class
 * - - - - - - - - - -
 * Add "INSERT" 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_Insert extends mysqlClass_Abstract implements mysqlClass_Queries
{
	/**
	 * insert error messages
	 * @var string
	 */
	const MESSAGE_AFTER_FIELDS = "you cannot add columns after using 'set' or 'select'";
	const MESSAGE_AFTER_SET = "you cannot use 'set' after adding columns or a select";
	const MESSAGE_AFTER_SELECT = "you cannot use 'select' after adding values or 'set'";
	const MESSAGE_BEFORE_VALUES = "you have to specify a column list before adding values";
	const MESSAGE_VALUES_COUNT = "value count doesn't match columns";
	const MESSAGE_VALUES_MISSING = "columns not found in values";



	/*
	** public 
	*/



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

		$this->query["table"]     = NULL;
		$this->query["low"]       = false;
		$this->query["delayed"]   = false;
		$this->query["high"]      = false;
		$this->query["ignore"]    = false;
		$this->query["columns"]   = array();
		$this->query["values"]    = array();
		$this->query["set"]       = array();
		$this->query["select"]    = NULL;
		$this->query["duplicate"] = array();

		return $this;
	}



	/*
	** query related
	*/


	
	/**
	 * add table to insert query
	 * @param string $table
	 * @return mysqlClass_Insert
	 */
	public function table($table)
	{
		if( is_string($table) )
			$this->query["table"] = $table;
		else if( is_array($table) )
			foreach( $table as $_table => $_name )
				if( !is_numeric($_table) )
					$this->query["table"] = $_table;
				else
					$this->query["table"] = $_name;
		
		return $this;
	}

	/**
	 * alias of 'table'
	 * @param string $table
	 * @return mysqlClass_Insert
	 */
	public function into($table)
	{
		return $this->table($table);
	}

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

	/**
	 * add 'high priority' to query
	 * @param boolean $high
	 * @return mysqlClass_Insert
	 */
	public function highPriority($high = true)
	{
		if( (bool)$high )
		{
			$this->query["low"] = false;
			$this->query["delayed"] = false;
		}
		
		$this->query["high"] = (bool)$high;
		return $this;
	}

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

	/**
	 * add columns to insert query
	 * @param string|array $columns
	 * @return mysqlClass_Insert
	 */
	public function columns($columns)
	{
		if( count($this->query["set"]) > 0 || !is_null($this->query["select"]) )
		{
			$this->parent->createError(self::MESSAGE_AFTER_FIELDS);
			return $this;
		}

		if( func_num_args() == 1 && is_string($columns) && !in_array($columns, $this->query["columns"]) )
		{
			$this->query["columns"][] = $columns;
			return $this;
		}

		foreach( func_get_args() as $column )
		{
			if( !is_array($column) && !in_array($column, $this->query["columns"]) )
			{
				$this->query["columns"][] = $column;
			}
			else
			{
				foreach( $column as $_column )
					if( !in_array($_column, $this->query["columns"]) )
						$this->query["columns"][] = $_column;
			}
		}

		return $this;
	}

	/**
	 * alias of "columns"
	 * @param string|array $fields
	 * @return mysqlClass_Insert
	 */
	public function fields($fields)
	{
		if( count($this->query["set"]) > 0 || !is_null($this->query["select"]) )
		{
			$this->parent->createError(self::MESSAGE_AFTER_FIELDS);
			return $this;
		}

		if( func_num_args() == 1 && is_string($fields) && !in_array($fields, $this->query["columns"]) )
		{
			$this->query["columns"][] = $fields;
			return $this;
		}

		return call_user_func_array(array($this, "columns"), func_get_args());
	}

	/**
	 * add 'values' to insert query
	 * @param string|array $values
	 * @return mysqlClass_Insert
	 */
	public function values($values)
	{
		$columnCount = count($this->query["columns"]);

		if( $columnCount == 0 )
		{
			$this->parent->createError(self::MESSAGE_BEFORE_VALUES);
			return $this;
		}

		if( count($this->query["set"]) > 0 || !is_null($this->query["select"]) )
		{
			$this->parent->createError(self::MESSAGE_AFTER_FIELDS);
			return $this;
		}

		if( func_num_args() == 1 && $columnCount == 1 && !is_array($values) )
		{
			$this->query["values"][] = array($values);
			return $this;
		}

		// count params
		$count = 0;
		$values = array();

		foreach( func_get_args() as $value )
		{
			if( !is_array($value) )
			{
				$count++;
				$values[] = $this->parent->escape($value);
			}
			else
			{
				foreach( $value as $_key => $_value )
				{
					$count++;

					if( is_numeric($_key) )
						$values[] = $this->parent->escape($_value);
					else
						$values[$_key] = $this->parent->escape($_value);
				}
			}
		}

		// check if params count match fields
		if( $count == $columnCount )
		{
			$this->query["values"][] = $values;
		}

		// check if fields names are in values
		else if( $count > $columnCount )
		{
			$_values = array();
			
			foreach( $this->query["columns"] as $column )
			{
				if( !isset($values[$column]) ) break;
				$_values[] = $values[$column];
			}
			
			if( count($_values) == $columnCount )
				$this->query["values"][] = $_values;
			else
				$this->parent->createError(self::MESSAGE_VALUES_MISSING);
		}

		// values doesn't match
		else
		{
			$this->parent->createError(self::MESSAGE_VALUES_COUNT);
		}

		return $this;
	}

	/**
	 * add 'set' to insert query
	 * @param string|array $column
	 * @param string $replace
	 * @return mysqlClass_Insert
	 */
	public function set($column, $replace = NULL)
	{
		if( count($this->query["columns"]) > 0 || !is_null($this->query["select"]) )
		{
			$this->parent->createError(self::MESSAGE_AFTER_SET);
			return $this;
		}

		if( is_null($replace) )
		{
			if( is_array($column) )
			{
				foreach( $column as $_column => $_replace )
				{
					if( !is_numeric($_column) )
						if( strpos($_column, "?") === false && strpos($column, "=") === false )
							$this->query["set"][] = $_column . " = " . $this->parent->escape($_replace);
						else
							$this->query["set"][] = str_replace("?", $this->parent->escape($_replace), $_column);
					else
						$this->query["set"][] = $_replace;
				}
			}
			else
			{
				$this->query["set"][] = $column;
			}
		}
		else
		{
			if( strpos($column, "?") === false && strpos($column, "=") === false )
				$this->query["set"][] = $column . " = " . $this->parent->escape($replace);
			else
				$this->query["set"][] = str_replace("?", $this->parent->escape($replace), $column);
		}

		return $this;
	}

	/**
	 * add a select statement to insert query
	 * @param string|mysqlClass_Select $subSelect
	 * @return mysqlClass_Insert
	 */
	public function select($subSelect)
	{
		if( count($this->query["values"]) > 0 || count($this->query["set"]) > 0 )
		{
			$this->parent->createError(self::MESSAGE_AFTER_SELECT);
			return $this;
		}

		if( is_string($subSelect) || $subSelect instanceof mysqlClass_Select )
		{
			$this->query["select"] = $subSelect;
		}

		return $this;
	}
	
	/**
	 * add 'onDuplicate' to query
	 * @param array|string $update
	 * @param string $replace
	 * @return mysqlClass_Insert
	 */
	public function onDuplicate($update, $replace = NULL)
	{
		if( is_null($replace) )
		{
			if( is_array($update) )
			{
				foreach( $update as $_update => $_replace )
				{
					if( !is_numeric($_update) )
						$this->query["duplicate"][] = str_replace("?", $this->parent->escape($_replace), $_update);
					else
						$this->query["duplicate"][] = $_replace;
				}
			}
			else
			{
				$this->query["duplicate"][] = $update;
			}
		}
		else
		{
			$this->query["duplicate"][] = str_replace("?", $this->parent->escape($replace), $update);
		}

		return $this;
	}

	/**
	 * alias of 'onDuplicate'
	 * @param array|string $update
	 * @param string $replace
	 * @return mysqlClass_Insert
	 */
	public function duplicate($update, $replace = NULL)
	{
		return $this->onDuplicate($update, $replace);
	}



	/*
	** build
	*/



	/**
	 * build mysql insert 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( is_null($this->query["table"]) ) return NULL;

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

		// low priority
		if( $this->query["low"] ) $query .= $this->format ? "\n" . $offset . "    LOW_PRIORITY " : "LOW_PRIORITY ";

		// delayed
		if( $this->query["delayed"] ) $query .= $this->format ? "\n" . $offset . "    DELAYED " : "DELAYED ";

		// high priority
		if( $this->query["high"] ) $query .= $this->format ? "\n" . $offset . "    HIGH_PRIORITY " : "HIGH_PRIORITY ";

		// ignore
		if( $this->query["ignore"] ) $query .= $this->format ? "\n" . $offset . "    IGNORE \n" : "IGNORE ";

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

		// table
		$query .= $this->format ? "\n" . $offset . "    " . $this->query["table"] . "\n" : $this->query["table"] . " ";

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

				for( $i = 0; $i < count($this->query["columns"]); $i++ )
				{
					$query .= " " . $this->query["columns"][$i];
					$query .= $i < count($this->query["columns"]) - 1 ? "," : NULL;
				}

				$query .= " ) \n";
			}
			else
				$query .= "(" . join(",", $this->query["columns"]) . ") ";

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

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

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

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

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

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

		// select		
		if( !is_null($this->query["select"]) )
		{
			if( is_string($this->query["select"]) )
			{
				if( $this->format )
					$query .= $offset . "    ( " . $this->query["select"] . " ) \n";
				else
					$query .= "(" . $this->query["select"] . ") ";
			}
			else
			{
				$select = $this->query["select"];

				if( $select instanceof mysqlClass_Select )
				{
					if( $this->format )
						$query .= $offset . "    (" . trim($select->build($this->formatOffset + 4)) . ") \n";
					else
						$query .= "(" . $select->build() . ") ";
				}
			}
		}

		// on duplicate 
		if( !empty($this->query["duplicate"]) )
		{
			if( $this->format )
			{
				$query .= "ON DUPLICATE KEY UPDATE \n";

				for( $i = 0; $i < count($this->query["duplicate"]); $i++ )
				{
					$query .= $offset . "    " . $this->query["duplicate"][$i];
					$query .= $i < count($this->query["duplicate"]) - 1 ? "," : NULL;
					$query .= " \n";
				}
			}
			else
			{
				$query .= "ON DUPLICATE KEY UPDATE " . join(",", $this->query["duplicate"]) . " ";
			}
		}

		return $query;
	}
}
Back to Top