Completed
Pull Request — 1.2 (#46)
by
unknown
03:10
created

Table::toSql()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 20

Duplication

Lines 4
Ratio 20 %

Importance

Changes 0
Metric Value
dl 4
loc 20
rs 8.9777
c 0
b 0
f 0
cc 6
nc 16
nop 4
1
<?php
2
3
/**
4
 * expression-types.php.
5
 *
6
 *
7
 * Copyright (c) 2010-2013, Justin Swanhart
8
 * with contributions by André Rothe <[email protected], [email protected]>
9
 * and David Négrier <[email protected]>
10
 *
11
 * All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms, with or without modification,
14
 * are permitted provided that the following conditions are met:
15
 *
16
 *   * Redistributions of source code must retain the above copyright notice,
17
 *     this list of conditions and the following disclaimer.
18
 *   * Redistributions in binary form must reproduce the above copyright notice,
19
 *     this list of conditions and the following disclaimer in the documentation
20
 *     and/or other materials provided with the distribution.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25
 * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31
 * DAMAGE.
32
 */
33
namespace SQLParser\Node;
34
35
use Doctrine\DBAL\Connection;
36
use Mouf\MoufInstanceDescriptor;
37
use Mouf\MoufManager;
38
use SQLParser\Node\Traverser\NodeTraverser;
39
use SQLParser\Node\Traverser\VisitorInterface;
40
41
/**
42
 * This class represents a table (and optionally a JOIN .. ON expression) in a SQL expression.
43
 *
44
 * @author David Négrier <[email protected]>
45
 */
46
class Table implements NodeInterface
47
{
48
    private $database;
49
50
    /**
51
     * Returns the name of the database.
52
     *
53
     * @return mixed
54
     */
55
    public function getDatabase()
56
    {
57
        return $this->database;
58
    }
59
60
    /**
61
     * Sets the name of the database
62
     *
63
     * @param mixed $database
64
     */
65
    public function setDatabase($database)
66
    {
67
        $this->database = $database;
68
    }
69
70
    private $table;
71
72
    /**
73
     * Returns the table name.
74
     *
75
     * @return string
76
     */
77
    public function getTable()
78
    {
79
        return $this->table;
80
    }
81
82
    /**
83
     * Sets the table name.
84
     *
85
     * @Important
86
     *
87
     * @param string $table
88
     */
89
    public function setTable($table)
90
    {
91
        $this->table = $table;
92
    }
93
94
    private $alias;
95
96
    /**
97
     * Returns the alias.
98
     *
99
     * @return string
100
     */
101
    public function getAlias()
102
    {
103
        return $this->alias;
104
    }
105
106
    /**
107
     * Sets the alias.
108
     *
109
     * @Important
110
     *
111
     * @param string $alias
112
     */
113
    public function setAlias($alias)
114
    {
115
        $this->alias = $alias;
116
    }
117
118
    private $joinType;
119
120
    /**
121
     * Returns the join type.
122
     *
123
     * @return string
124
     */
125
    public function getJoinType()
126
    {
127
        return $this->joinType;
128
    }
129
130
    /**
131
     * Sets the join type (JOIN, LEFT JOIN, RIGHT JOIN, etc...).
132
     *
133
     * @Important
134
     *
135
     * @param string $joinType
136
     */
137
    public function setJoinType($joinType)
138
    {
139
        $this->joinType = $joinType;
140
    }
141
142
    private $refClause;
143
144
    /**
145
     * Returns the list of refClause statements.
146
     *
147
     * @return NodeInterface[]|NodeInterface
148
     */
149
    public function getRefClause()
150
    {
151
        return $this->refClause;
152
    }
153
154
    /**
155
     * Sets the list of refClause statements.
156
     *
157
     * @Important
158
     *
159
     * @param NodeInterface[]|NodeInterface $refClause
160
     */
161
    public function setRefClause($refClause)
162
    {
163
        $this->refClause = $refClause;
164
    }
165
166
    /**
167
     * Returns a Mouf instance descriptor describing this object.
168
     *
169
     * @param MoufManager $moufManager
170
     *
171
     * @return MoufInstanceDescriptor
172
     */
173
    public function toInstanceDescriptor(MoufManager $moufManager)
174
    {
175
        $instanceDescriptor = $moufManager->createInstance(get_called_class());
176
        $instanceDescriptor->getProperty('database')->setValue($this->database);
177
        $instanceDescriptor->getProperty('table')->setValue($this->table);
178
        $instanceDescriptor->getProperty('alias')->setValue($this->alias);
179
        $instanceDescriptor->getProperty('joinType')->setValue($this->joinType);
180
        $instanceDescriptor->getProperty('refClause')->setValue(NodeFactory::nodeToInstanceDescriptor($this->refClause, $moufManager));
181
182
        return $instanceDescriptor;
183
    }
184
185
    /**
186
     * Renders the object as a SQL string.
187
     *
188
     * @param Connection $dbConnection
189
     * @param array      $parameters
190
     * @param number     $indent
191
     * @param int        $conditionsMode
192
     *
193
     * @return string
194
     */
195
    public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY)
196
    {
197
        $sql = '';
198
        if ($this->refClause || $this->joinType === 'CROSS JOIN') {
199
            $sql .= "\n  ".$this->joinType.' ';
200
        }
201
        if ($this->database) {
202
            $sql .= NodeFactory::escapeDBItem($this->database, $dbConnection).'.';
203
        }
204
        $sql .= NodeFactory::escapeDBItem($this->table, $dbConnection);
205
        if ($this->alias) {
206
            $sql .= ' AS '.NodeFactory::escapeDBItem($this->alias, $dbConnection);
207
        }
208 View Code Duplication
        if ($this->refClause) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
209
            $sql .= ' ON ';
210
            $sql .= NodeFactory::toSql($this->refClause, $dbConnection, $parameters, ' ', true, $indent, $conditionsMode);
211
        }
212
213
        return $sql;
214
    }
215
216
    /**
217
     * Walks the tree of nodes, calling the visitor passed in parameter.
218
     *
219
     * @param VisitorInterface $visitor
220
     */
221 View Code Duplication
    public function walk(VisitorInterface $visitor)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
222
    {
223
        $node = $this;
224
        $result = $visitor->enterNode($node);
225
        if ($result instanceof NodeInterface) {
226
            $node = $result;
227
        }
228
        if ($result !== NodeTraverser::DONT_TRAVERSE_CHILDREN) {
229
            if (is_array($this->refClause)) {
230
                foreach ($this->refClause as $key => $operand) {
231
                    $result2 = $operand->walk($visitor);
232
                    if ($result2 === NodeTraverser::REMOVE_NODE) {
233
                        unset($this->refClause[$key]);
234
                    } elseif ($result2 instanceof NodeInterface) {
235
                        $this->refClause[$key] = $result2;
236
                    }
237
                }
238
            } elseif ($this->refClause) {
239
                $result2 = $this->refClause->walk($visitor);
240
                if ($result2 === NodeTraverser::REMOVE_NODE) {
241
                    $this->refClause = null;
242
                } elseif ($result2 instanceof NodeInterface) {
243
                    $this->refClause = $result2;
244
                }
245
            }
246
        }
247
248
        return $visitor->leaveNode($node);
249
    }
250
}
251