Code Coverage  | 
      ||||||||||
Lines  | 
       Functions and Methods  | 
       Classes and Traits  | 
      ||||||||
| Total |         | 
       100.00%  | 
       25 / 25  | 
               | 
       100.00%  | 
       5 / 5  | 
       CRAP |         | 
       100.00%  | 
       1 / 1  | 
      
| Insert |         | 
       100.00%  | 
       25 / 25  | 
               | 
       100.00%  | 
       5 / 5  | 
       9 |         | 
       100.00%  | 
       1 / 1  | 
      
| sql |         | 
       100.00%  | 
       8 / 8  | 
               | 
       100.00%  | 
       1 / 1  | 
       2 | |||
| valuesSQL |         | 
       100.00%  | 
       10 / 10  | 
               | 
       100.00%  | 
       1 / 1  | 
       3 | |||
| params |         | 
       100.00%  | 
       1 / 1  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| reset |         | 
       100.00%  | 
       3 / 3  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| allTablesReferenced |         | 
       100.00%  | 
       3 / 3  | 
               | 
       100.00%  | 
       1 / 1  | 
       2 | |||
| 1 | <?php | 
| 2 | |
| 3 | namespace BO\Zmsdb\Query\Builder; | 
| 4 | |
| 5 | /** | 
| 6 | * Insert | 
| 7 | * | 
| 8 | * Generates an SQL query for an INSERT operation. | 
| 9 | * | 
| 10 | * @package BO\Zmsdb\Query\Builder | 
| 11 | * @author Alex Gisby<alex@solution10.com> | 
| 12 | * @license MIT | 
| 13 | */ | 
| 14 | class Insert extends Query | 
| 15 | { | 
| 16 | use TableName; | 
| 17 | use Values; | 
| 18 | |
| 19 | /** | 
| 20 | * @var string The base part of the query | 
| 21 | */ | 
| 22 | protected $queryBase = 'INSERT INTO'; | 
| 23 | |
| 24 | /** | 
| 25 | * Generates the full SQL statement for this query with all the composite parts. | 
| 26 | * | 
| 27 | * @return string | 
| 28 | */ | 
| 29 | public function sql() | 
| 30 | { | 
| 31 | if ($this->table === null) { | 
| 32 | return ''; | 
| 33 | } | 
| 34 | |
| 35 | $candidateParts = [ | 
| 36 | $this->queryBase, | 
| 37 | $this->dialect->quoteTable($this->table), | 
| 38 | $this->valuesSQL(), | 
| 39 | ]; | 
| 40 | |
| 41 | return implode(' ', $candidateParts); | 
| 42 | } | 
| 43 | |
| 44 | /** | 
| 45 | * Returns the values part of a query for an INSERT statement (so using VALUES()) | 
| 46 | * | 
| 47 | * @return string | 
| 48 | */ | 
| 49 | protected function valuesSQL() | 
| 50 | { | 
| 51 | $sql = ''; | 
| 52 | if (!empty($this->values)) { | 
| 53 | $keyParts = []; | 
| 54 | $valueParts = []; | 
| 55 | foreach (array_keys($this->values) as $field) { | 
| 56 | $keyParts[] = $this->dialect->quoteField($field); | 
| 57 | $valueParts[] = '?'; | 
| 58 | } | 
| 59 | |
| 60 | $sql .= '(' . implode(', ', $keyParts) . ') '; | 
| 61 | $sql .= 'VALUES (' . implode(', ', $valueParts) . ')'; | 
| 62 | } | 
| 63 | return $sql; | 
| 64 | } | 
| 65 | |
| 66 | /** | 
| 67 | * Returns all the parameters, in the correct order, to pass into PDO. | 
| 68 | * | 
| 69 | * @return array | 
| 70 | */ | 
| 71 | public function params() | 
| 72 | { | 
| 73 | return array_values($this->values); | 
| 74 | } | 
| 75 | |
| 76 | /** | 
| 77 | * Resets the entire query. | 
| 78 | * | 
| 79 | * @return $this | 
| 80 | */ | 
| 81 | public function reset() | 
| 82 | { | 
| 83 | $this->table = null; | 
| 84 | $this->resetValues(); | 
| 85 | return $this; | 
| 86 | } | 
| 87 | |
| 88 | /* | 
| 89 | * ------------------- All Tables ------------------------ | 
| 90 | */ | 
| 91 | |
| 92 | /** | 
| 93 | * Returns all the tables that this query makes mention of, in FROMs and JOINs | 
| 94 | * | 
| 95 | * @return array | 
| 96 | */ | 
| 97 | public function allTablesReferenced() | 
| 98 | { | 
| 99 | if ($this->table()) { | 
| 100 | return [$this->table()]; | 
| 101 | } | 
| 102 | return []; | 
| 103 | } | 
| 104 | } |