Completed
Push — 2.0 ( c11079...9c6898 )
by Vermeulen
02:10
created

SqlInsert::assembleRequest()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 6
nop 0
1
<?php
2
3
namespace BfwSql;
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
class SqlInsert extends SqlActions
13
{
14
    /**
15
     * Constructor
16
     * 
17
     * @param \BfwSql\SqlConnect $sqlConnect Instance of SGBD connexion
18
     * @param string             $tableName  The table name used for query
19
     * @param array              $columns    (default: null) Datas to add
20
     *  Format is array('columnName' => 'value', ...);
21
     */
22 View Code Duplication
    public function __construct(
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...
23
        SqlConnect $sqlConnect,
24
        $tableName,
25
        $columns = null
26
    ) {
27
        parent::__construct($sqlConnect);
28
        
29
        $prefix          = $sqlConnect->getConnectionInfos()->tablePrefix;
30
        $this->tableName = $prefix.$tableName;
31
        
32
        if (is_array($columns)) {
33
            $this->columns = $columns;
34
        }
35
    }
36
    
37
    /**
38
     * {@inheritdoc}
39
     */
40
    protected function assembleRequest()
41
    {
42
        $lstColumns    = '';
43
        $lstValues     = '';
44
        $indexReadList = -1;
45
        
46
        foreach ($this->columns as $columnName => $columnValue) {
47
            $indexReadList++;
48
            if ($indexReadList > 0) {
49
                $lstColumns .= ',';
50
                $lstValues  .= ',';
51
            }
52
            
53
            $lstColumns .= '`'.$columnName.'`';
54
            $lstValues  .= $columnValue;
55
        }
56
        
57
        $this->assembledRequest = 'INSERT INTO '.$this->tableName;
58
        
59
        if ($this->columns !== []) {
60
            $this->assembledRequest .= ' ('.$lstColumns.')'
61
                .' VALUES ('.$lstValues.')';
62
        }
63
        
64
        $this->callObserver();
65
    }
66
} 
67