Insert::generateSelect()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BfwSql\Queries;
4
5
/**
6
 * Class to write INSERT INTO queries
7
 * 
8
 * @package bfw-sql
9
 * @author Vermeulen Maxime <[email protected]>
10
 * @version 2.0
11
 * 
12
 * @method \BfwSql\Queries\Insert into(string|array $nameInfos, string|array|null $columns=null)
13
 * @method \BfwSql\Queries\Insert values(array $columns)
14
 * @method \BfwSql\Queries\Select select()
15
 * @method \BfwSql\Queries\Insert onDuplicate(array $columns)
16
 */
17
class Insert extends AbstractQuery
18
{
19
    /**
20
     * @var \BfwSql\Helpers\Quoting $quoting The quoting system
21
     */
22
    protected $quoting;
23
    
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected $requestType = 'insert';
28
    
29
    /**
30
     * Constructor
31
     * 
32
     * @param \BfwSql\SqlConnect $sqlConnect Instance of SGBD connexion
33
     * @param string $quoteStatus (default: QUOTE_ALL) Status to automatic
34
     *  quoted string value system.
35
     */
36
    public function __construct(
37
        \BfwSql\SqlConnect $sqlConnect,
38
        $quoteStatus = \BfwSql\Helpers\Quoting::QUOTE_ALL
39
    ) {
40
        parent::__construct($sqlConnect);
41
        
42
        $this->quoting = new \BfwSql\Helpers\Quoting(
43
            $quoteStatus,
44
            $this->sqlConnect
45
        );
46
    }
47
    
48
    /**
49
     * Getter accessor to property quoting
50
     * 
51
     * @return \BfwSql\Helpers\Quoting
52
     */
53
    public function getQuoting(): \BfwSql\Helpers\Quoting
54
    {
55
        return $this->quoting;
56
    }
57
    
58
    /**
59
     * {@inheritdoc}
60
     */
61
    protected function defineQueriesParts()
62
    {
63
        parent::defineQueriesParts();
64
        unset($this->queriesParts['where']);
65
        
66
        $parts     = &$this->queriesParts; //Yes, it's just to have < 80 chars
67
        $partTable = $parts['table'];
68
        $partTable->setColumnsWithValue(true);
69
        $partTable->createColumnInstance();
70
        
71
        $usedClass       = \BfwSql\UsedClass::getInstance();
72
        $selectClass     = $usedClass->obtainClassNameToUse('QueriesSelect');
73
        $colValListClass = $usedClass->obtainClassNameToUse('QueriesPartsColumnValueList');
74
        
75
        $parts['into']        = $partTable;
76
        $parts['values']      = &$partTable->getColumns();
77
        $parts['select']      = new $selectClass($this->sqlConnect, 'object');
78
        $parts['onDuplicate'] = new $colValListClass($this, $partTable);
79
        
80
        $this->querySgbd->disableQueriesParts($this->queriesParts);
81
    }
82
    
83
    /**
84
     * {@inheritdoc}
85
     */
86
    protected function obtainGenerateOrder(): array
87
    {
88
        return [
89
            'table'           => [
90
                'prefix'     => 'INSERT INTO',
91
                'canBeEmpty' => false
92
            ],
93
            'values'          => [
94
                'callback'      => [$this, 'generateValues'],
95
                'usePartPrefix' => false
96
            ],
97
            'select'          => [
98
                'callback'      => [$this, 'generateSelect'],
99
                'usePartPrefix' => false
100
            ],
101
            'onDuplicate' => [
102
                'prefix'   => 'ON DUPLICATE KEY UPDATE',
103
                'callback' => [$this, 'generateOnDuplicate']
104
            ]
105
        ];
106
    }
107
    
108
    /**
109
     * Callback used by assembleRequestPart method
110
     * Generate the sql query part who contains columns list and their values
111
     * 
112
     * @return string
113
     */
114
    protected function generateValues(): string
115
    {
116
        $listNames   = '';
117
        $listValues  = '';
118
        
119
        foreach ($this->queriesParts['values'] as $colIndex => $column) {
120
            
121
            if ($colIndex > 0) {
122
                $listNames  .= ',';
123
                $listValues .= ',';
124
            }
125
            
126
            $listNames  .= '`'.$column->getName().'`';
127
            $listValues .= $column->obtainValue();
128
        }
129
        
130
        if ($listNames === '') {
131
            return '';
132
        }
133
        
134
        $select = $this->queriesParts['select'];
135
        if ($select->getQueriesParts()['table']->getName() === '') {
136
            return '('.$listNames.') VALUES ('.$listValues.')';
137
        } else {
138
            return '('.$listNames.')';
139
        }
140
    }
141
    
142
    /**
143
     * Callback used by assembleRequestPart method
144
     * Generate the sql query for the SELECT part
145
     * 
146
     * @return string
147
     */
148
    protected function generateSelect(): string
149
    {
150
        $select = $this->queriesParts['select'];
151
        if ($select->getQueriesParts()['table']->getName() === '') {
152
            return '';
153
        }
154
        
155
        return $select->assemble();
156
    }
157
    
158
    /**
159
     * Callback used by assembleRequestPart method
160
     * Generate the sql query for the ON DUPLICATE KEY UPDATE part
161
     * 
162
     * @return string
163
     */
164
    protected function generateOnDuplicate(): string
165
    {
166
        $listColumns = $this->queriesParts['onDuplicate'];
167
        $sqlPart     = '';
168
        
169
        foreach ($listColumns as $colIndex => $column) {
170
            if ($colIndex > 0) {
171
                $sqlPart .= ',';
172
            }
173
            
174
            $sqlPart .= '`'.$column->getName().'`='.$column->obtainValue();
175
        }
176
        
177
        return $sqlPart;
178
    }
179
} 
180