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