Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
57 / 57
100.00% covered (success)
100.00%
11 / 11
CRAP
100.00% covered (success)
100.00%
1 / 1
Sql
100.00% covered (success)
100.00%
57 / 57
100.00% covered (success)
100.00%
11 / 11
26
100.00% covered (success)
100.00%
1 / 1
 select
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
6
 insert
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 update
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 delete
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 hasSelect
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasInsert
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasUpdate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasDelete
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 reset
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Db;
16
17use Pop\Db\Sql\AbstractSql;
18
19/**
20 * Sql class
21 *
22 * @category   Pop
23 * @package    Pop\Db
24 * @author     Nick Sagona, III <nick@popphp.org>
25 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
26 * @license    https://www.popphp.org/license     New BSD License
27 * @version    7.0.0
28 */
29class Sql extends AbstractSql
30{
31
32    /**
33     * Select object
34     * @var ?Sql\Select
35     */
36    protected ?Sql\Select $select = null;
37
38    /**
39     * Insert object
40     * @var ?Sql\Insert
41     */
42    protected ?Sql\Insert $insert = null;
43
44    /**
45     * Update object
46     * @var ?Sql\Update
47     */
48    protected ?Sql\Update $update = null;
49
50    /**
51     * Delete object
52     * @var ?Sql\Delete
53     */
54    protected ?Sql\Delete $delete = null;
55
56    /**
57     * Access the select object
58     *
59     * @param  mixed $columns
60     * @return ?Sql\Select
61     */
62    public function select(mixed $columns = null): ?Sql\Select
63    {
64        $this->insert = null;
65        $this->update = null;
66        $this->delete = null;
67
68        if ($this->select === null) {
69            $this->select = new Sql\Select($this->db);
70        }
71        if ($columns !== null) {
72            if (!is_array($columns)) {
73                $columns = [$columns];
74            }
75            foreach ($columns as $name => $value) {
76                if (!is_numeric($name)) {
77                    $this->select->addValue($value, $name);
78                } else {
79                    $this->select->addValue($value);
80                }
81            }
82        }
83
84        return $this->select;
85    }
86
87    /**
88     * Access the insert object
89     *
90     * @param  ?string $table
91     * @return ?Sql\Insert
92     */
93    public function insert(?string $table = null): ?Sql\Insert
94    {
95        $this->select = null;
96        $this->update = null;
97        $this->delete = null;
98
99        if ($this->insert === null) {
100            $this->insert = new Sql\Insert($this->db);
101        }
102        if ($table !== null) {
103            $this->insert->setTable($table);
104        }
105
106        return $this->insert;
107    }
108
109    /**
110     * Access the update object
111     *
112     * @param  ?string $table
113     * @return ?Sql\Update
114     */
115    public function update(?string $table = null): ?Sql\Update
116    {
117        $this->insert = null;
118        $this->select = null;
119        $this->delete = null;
120
121        if ($this->update === null) {
122            $this->update = new Sql\Update($this->db);
123        }
124        if ($table !== null) {
125            $this->update->setTable($table);
126        }
127
128        return $this->update;
129    }
130
131    /**
132     * Access the delete object
133     *
134     * @param  ?string $table
135     * @return ?Sql\Delete
136     */
137    public function delete(?string $table = null): ?Sql\Delete
138    {
139        $this->insert = null;
140        $this->update = null;
141        $this->select = null;
142
143        if ($this->delete === null) {
144            $this->delete = new Sql\Delete($this->db);
145        }
146        if ($table !== null) {
147            $this->delete->setTable($table);
148        }
149
150        return $this->delete;
151    }
152
153    /**
154     * Determine if SQL object has a select object
155     *
156     * @return bool
157     */
158    public function hasSelect(): bool
159    {
160        return ($this->select !== null);
161    }
162
163    /**
164     * Determine if SQL object has a insert object
165     *
166     * @return bool
167     */
168    public function hasInsert(): bool
169    {
170        return ($this->insert !== null);
171    }
172
173    /**
174     * Determine if SQL object has a update object
175     *
176     * @return bool
177     */
178    public function hasUpdate(): bool
179    {
180        return ($this->update !== null);
181    }
182
183    /**
184     * Determine if SQL object has a delete object
185     *
186     * @return bool
187     */
188    public function hasDelete(): bool
189    {
190        return ($this->delete !== null);
191    }
192
193    /**
194     * Reset and clear the SQL object
195     *
196     * @return Sql
197     */
198    public function reset(): Sql
199    {
200        $this->select = null;
201        $this->insert = null;
202        $this->update = null;
203        $this->delete = null;
204
205        return $this;
206    }
207
208    /**
209     * Render the SQL statement
210     *
211     * Rendering is side effect free: the clause object that produced the string is left in
212     * place, so the same object can be rendered again (logged and then executed, inspected
213     * while debugging, echoed by a caller it was returned to). Use reset() to explicitly
214     * clear the object before building an unrelated statement with it.
215     *
216     * @return string
217     */
218    public function render(): string
219    {
220        $sql = '';
221
222        if ($this->select !== null) {
223            $sql = $this->select->render();
224        } else if ($this->insert !== null) {
225            $sql = $this->insert->render();
226        } else if ($this->update !== null) {
227            $sql = $this->update->render();
228        } else if ($this->delete !== null) {
229            $sql = $this->delete->render();
230        }
231
232        return $sql;
233    }
234
235    /**
236     * Render the SQL statement
237     *
238     * @return string
239     */
240    public function __toString(): string
241    {
242        return $this->render();
243    }
244
245}