Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
21 / 21 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| Query | |
100.00% |
21 / 21 |
|
100.00% |
7 / 7 |
14 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| queryBaseStatement | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| dialect | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| flag | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| flags | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| deleteFlag | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| sql | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| params | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| reset | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| allTablesReferenced | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 1 | <?php |
| 2 | |
| 3 | namespace BO\Zmsdb\Query\Builder; |
| 4 | |
| 5 | use BO\Zmsdb\Query\Builder\Dialect\ANSI; |
| 6 | |
| 7 | /** |
| 8 | * Query |
| 9 | * |
| 10 | * Base Query class that all other query types should inherit from. |
| 11 | * |
| 12 | * @package BO\Zmsdb\Query\Builder |
| 13 | * @author Alex Gisby<alex@solution10.com> |
| 14 | * @license MIT |
| 15 | */ |
| 16 | abstract class Query |
| 17 | { |
| 18 | /** |
| 19 | * @var DialectInterface |
| 20 | */ |
| 21 | protected $dialect; |
| 22 | |
| 23 | /** |
| 24 | * @var string The base part of the query (SELECT, INSERT etc) |
| 25 | */ |
| 26 | protected $queryBase = null; |
| 27 | |
| 28 | /** |
| 29 | * @var array Flags for this query |
| 30 | */ |
| 31 | protected $flags = []; |
| 32 | |
| 33 | /** |
| 34 | * Pass in a dialect, otherwise it'll assume ANSI SQL. |
| 35 | * |
| 36 | * @param DialectInterface|null $dialect |
| 37 | */ |
| 38 | public function __construct(DialectInterface $dialect = null) |
| 39 | { |
| 40 | $this->dialect = ($dialect === null) ? new ANSI() : $dialect; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Gets/sets the query base for this query. |
| 45 | * Note: This will NOT be escaped in any way! Be super careful what you pass. |
| 46 | * |
| 47 | * @param null|string $base Null to get, string to set |
| 48 | * @return string|$this String on get, $this on set |
| 49 | */ |
| 50 | public function queryBaseStatement($base = null) |
| 51 | { |
| 52 | if ($base === null) { |
| 53 | return $this->queryBase; |
| 54 | } |
| 55 | $this->queryBase = $base; |
| 56 | return $this; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get/set the dialect in use for this query. |
| 61 | * |
| 62 | * @param null|DialectInterface $dialect Null to get, DialectInterface to set |
| 63 | * @return DialectInterface|$this DialectInterface on get, $this on set. |
| 64 | */ |
| 65 | public function dialect(DialectInterface $dialect = null) |
| 66 | { |
| 67 | if ($dialect === null) { |
| 68 | return $this->dialect; |
| 69 | } |
| 70 | $this->dialect = $dialect; |
| 71 | return $this; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get/Set a flag for the query. Flags are any metadata that you want passed around with |
| 76 | * the query, but not necessarily used within the generation of the SQL. For example you |
| 77 | * might put a cache TTL in there, or some engine specific flag for your database adapter |
| 78 | * to later read and use. |
| 79 | * |
| 80 | * Any type can be used as the flag value. |
| 81 | * |
| 82 | * @param string $flag Name of the flag |
| 83 | * @param null|mixed $value Null to get the flag, mixed to set it |
| 84 | * @return mixed|$this Flag value on read (null for not set), or $this on set. |
| 85 | */ |
| 86 | public function flag($flag, $value = null) |
| 87 | { |
| 88 | if ($value === null) { |
| 89 | return (array_key_exists($flag, $this->flags)) ? $this->flags[$flag] : null; |
| 90 | } |
| 91 | $this->flags[$flag] = $value; |
| 92 | return $this; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Gets/Sets multiple flags for the query. Get returns all flags as a key => value array. |
| 97 | * Set accepts a key => value array. |
| 98 | * |
| 99 | * @param null|array $flags Null for get, array for set |
| 100 | * @return array|$this array on get, $this on set |
| 101 | * @see Query::flag() for more info on flags. |
| 102 | */ |
| 103 | public function flags(array $flags = null) |
| 104 | { |
| 105 | if (!is_array($flags)) { |
| 106 | return $this->flags; |
| 107 | } |
| 108 | foreach ($flags as $f => $v) { |
| 109 | $this->flags[$f] = $v; |
| 110 | } |
| 111 | return $this; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Deletes a flag from the query. |
| 116 | * |
| 117 | * @param string $flag Name of the flag |
| 118 | * @return $this |
| 119 | */ |
| 120 | public function deleteFlag($flag) |
| 121 | { |
| 122 | unset($this->flags[$flag]); |
| 123 | return $this; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Generates the full SQL statement for this query with all the composite parts. |
| 128 | * |
| 129 | * @return string |
| 130 | */ |
| 131 | abstract public function sql(); |
| 132 | |
| 133 | /** |
| 134 | * Serves as a shortcut for sql() |
| 135 | * |
| 136 | * @return string |
| 137 | */ |
| 138 | public function __toString() |
| 139 | { |
| 140 | return $this->sql(); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Returns all the parameters, in the correct order, to pass into PDO. |
| 145 | * |
| 146 | * @return array |
| 147 | */ |
| 148 | abstract public function params(); |
| 149 | |
| 150 | /** |
| 151 | * Resets the entire query. |
| 152 | * |
| 153 | * @return $this |
| 154 | */ |
| 155 | abstract public function reset(); |
| 156 | |
| 157 | /** |
| 158 | * Returns all the tables that this query makes mention of, in FROMs and JOINs |
| 159 | * |
| 160 | * @return array |
| 161 | */ |
| 162 | abstract public function allTablesReferenced(); |
| 163 | } |