Completed
Push — master ( c6f12f...94e68a )
by Hong
03:14
created

StatementAbstract::getConfigs()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Query
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Query\Traits;
16
17
use Phossa2\Shared\Base\ObjectAbstract;
18
use Phossa2\Query\Traits\Clause\ExtraClauseTrait;
19
use Phossa2\Query\Interfaces\BuilderInterface;
20
use Phossa2\Query\Interfaces\StatementInterface;
21
22
/**
23
 * StatementAbstract
24
 *
25
 * @package Phossa2\Query
26
 * @author  Hong Zhang <[email protected]>
27
 * @see     ObjectAbstract
28
 * @see     StatementInterface
29
 * @version 2.0.0
30
 * @since   2.0.0 added
31
 */
32
abstract class StatementAbstract extends ObjectAbstract implements StatementInterface
33
{
34
    use SettingsAwareTrait, BuilderAwareTrait, ExtraClauseTrait, PreviousTrait;
35
36
    /**
37
     * value bindings
38
     *
39
     * @var    array
40
     * @access protected
41
     */
42
    protected $bindings;
43
44
    /**
45
     * clause configs,[name => prefix]
46
     *
47
     * @var    array
48
     * @access protected
49
     */
50
    protected $configs = [];
51
52
    /**
53
     * Constructor
54
     *
55
     * @param  BuilderInterface $builder
56
     * @access public
57
     */
58
    public function __construct(BuilderInterface $builder)
59
    {
60
        $this->setBuilder($builder);
61
    }
62
63
    /**
64
     * {@inheritDoc}
65
     */
66
    public function getStatement(array $settings = [])/*# : string */
67
    {
68
        // combine settings
69
        $settings = $this->combineSettings($settings);
70
71
        // build current sql
72
        $sql = $this->buildSql($settings);
73
74
        // replace with ?, :name or real values
75
        return $this->bindValues($sql, $settings);
76
    }
77
78
    /**
79
     * Return the sql string (with parameter replaced)
80
     *
81
     * @param  array $settings extra settings if any
82
     * @return string
83
     * @access public
84
     * @api
85
     */
86
    public function getSql(array $settings = [])/*# : string */
87
    {
88
        $settings['positionedParam'] = false;
89
        $settings['namedParam'] = false;
90
        return $this->getStatement($settings);
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     */
96
    public function getBindings()/*# : array */
97
    {
98
        return $this->bindings;
99
    }
100
101
    /**
102
     * {@inheritDoc}
103
     */
104
    public function __toString()/*# : string */
105
    {
106
        return $this->getSql();
107
    }
108
109
    /**
110
     * Combine builder/statement and provided settings
111
     *
112
     * @param  array $settings statement specific settings
113
     * @return array
114
     * @access protected
115
     */
116
    protected function combineSettings(array $settings)/*# : array */
117
    {
118
        return array_replace(
119
            $this->getBuilder()->getSettings(), // builder settings
120
            $this->getBuilder()->getDialect()->dialectSettings(), // dialect
121
            $settings,  // user settings
122
            $this->getSettings() // statement settings
123
        );
124
    }
125
126
    /**
127
     * Build SQL statement clauses
128
     *
129
     * @param  array $settings
130
     * @param  string
131
     * @access protected
132
     */
133
    protected function buildSql(array $settings)/*# : string */
134
    {
135
        $result = $this->getType(); // type
136
        $result .= $this->buildBeforeAfter('AFTER', 'TYPE', $settings); // hint
137
        $settings['join'] = $settings['seperator'] . $settings['indent'];
138
139
        foreach ($this->getConfigs() as $pos => $prefix) {
140
            // before
141
            $result .= $this->buildBeforeAfter('BEFORE', $pos, $settings);
142
143
            $method = 'build' . ucfirst(strtolower($pos));
144
            $result .= $this->{$method}($prefix, $settings);
145
146
            // after
147
            $result .= $this->buildBeforeAfter('AFTER', $pos, $settings);
148
        }
149
150
        $result .= $this->buildBeforeAfter('AFTER', 'STMT', $settings);
151
        return $result;
152
    }
153
154
    /**
155
     * Replace placeholders in the sql with '?', ':named' or real value
156
     *
157
     * @param  string $sql
158
     * @param  array $settings
159
     * @return string
160
     * @access protected
161
     */
162
    protected function bindValues(/*# string */ $sql, array $settings)/*# : string */
163
    {
164
        // init bindings
165
        $this->bindings = [];
166
167
        // bind values
168
        return $this->getBuilder()->getParameter()
169
            ->bindValues($sql, $this->bindings, $settings);
170
    }
171
172
    /**
173
     * Clause configs ['name' => 'prefix']
174
     *
175
     * @return array
176
     * @access protected
177
     */
178
    abstract protected function getConfigs()/*# : array */;
179
180
    /**
181
     * Get current statement type. e.g. 'SELECT' etc.
182
     *
183
     * @return string
184
     * @access protected
185
     */
186
    abstract protected function getType()/*# : string */;
187
}
188