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     */
69    public function dialect(DialectInterface $dialect = null)
70    {
71        if ($dialect === null) {
72            return $this->dialect;
73        }
74        $this->dialect = $dialect;
75        return $this;
76    }
77
78    /**
79     * Get/Set a flag for the query. Flags are any metadata that you want passed around with
80     * the query, but not necessarily used within the generation of the SQL. For example you
81     * might put a cache TTL in there, or some engine specific flag for your database adapter
82     * to later read and use.
83     *
84     * Any type can be used as the flag value.
85     *
86     * @param   string      $flag   Name of the flag
87     * @param   null|mixed  $value  Null to get the flag, mixed to set it
88     * @return  mixed|$this Flag value on read (null for not set), or $this on set.
89     */
90    public function flag($flag, $value = null)
91    {
92        if ($value === null) {
93            return (array_key_exists($flag, $this->flags)) ? $this->flags[$flag] : null;
94        }
95        $this->flags[$flag] = $value;
96        return $this;
97    }
98
99    /**
100     * Gets/Sets multiple flags for the query. Get returns all flags as a key => value array.
101     * Set accepts a key => value array.
102     *
103     * @param   null|array      $flags  Null for get, array for set
104     * @return  array|$this     array on get, $this on set
105     * @see     Query::flag() for more info on flags.
106     */
107    public function flags(array $flags = null)
108    {
109        if (!is_array($flags)) {
110            return $this->flags;
111        }
112        foreach ($flags as $f => $v) {
113            $this->flags[$f] = $v;
114        }
115        return $this;
116    }
117
118    /**
119     * Deletes a flag from the query.
120     *
121     * @param   string  $flag   Name of the flag
122     * @return  $this
123     */
124    public function deleteFlag($flag)
125    {
126        unset($this->flags[$flag]);
127        return $this;
128    }
129
130    /**
131     * Generates the full SQL statement for this query with all the composite parts.
132     *
133     * @return  string
134     */
135    abstract public function sql();
136
137    /**
138     * Serves as a shortcut for sql()
139     *
140     * @return  string
141     */
142    public function __toString()
143    {
144        return $this->sql();
145    }
146
147    /**
148     * Returns all the parameters, in the correct order, to pass into PDO.
149     *
150     * @return  array
151     */
152    abstract public function params();
153
154    /**
155     * Resets the entire query.
156     *
157     * @return  $this
158     */
159    abstract public function reset();
160
161    /**
162     * Returns all the tables that this query makes mention of, in FROMs and JOINs
163     *
164     * @return  array
165     */
166    abstract public function allTablesReferenced();
167}