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