Passed
Push — master ( 043641...3c8c2b )
by RN
01:48
created

InsertQueryBuilder   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 51
c 1
b 1
f 0
dl 0
loc 96
rs 10
wmc 15

5 Methods

Rating   Name   Duplication   Size   Complexity  
A buildInsertPlaceholder() 0 13 2
A buildInsertPlaceholders() 0 18 3
A buildInsert() 0 11 2
A insert() 0 34 5
A checkMultipleInsert() 0 2 3
1
<?php
2
/**
3
 * The Insert Query builder Class.
4
 *
5
 * @author RN Kushwaha <[email protected]>
6
 *
7
 * @since v0.0.5 <Date: 09th May, 2019>
8
 */
9
10
namespace Dolphin\Builders;
11
12
use Dolphin\Connections\Connection;
13
use Exception;
14
15
/**
16
 * This class provides the mechanism to build the Insert Queries.
17
 */
18
class InsertQueryBuilder extends QueryBuilder
19
{
20
    public function buildInsert($table, $obj){
21
        $qb = new QueryBuilder();
22
        $query = "INSERT INTO ".$table." (";
23
        foreach($obj as $key => $val){
24
            $query.= $qb->quote($key).", ";
25
        }
26
27
        $query = rtrim($query, ", ");
28
        $query.= ") VALUES ";
29
30
        return $query;
31
    }
32
33
    public function buildInsertPlaceholder($rows){
34
        $ar = array();
35
        $query = "(";
36
37
        foreach($rows as $key => $val){
38
            $ar[$key] = $val;
39
            $query.= ":".$key.", ";
40
        }
41
42
        $query = rtrim($query, ", ");
43
        $query.=") ";
44
45
        return ['query' => $query, 'array' => $ar];
46
    }
47
48
    public function buildInsertPlaceholders($rows){
49
        $bindAr = array();
50
        $query = "";
51
52
        foreach($rows as $i => $row){
53
            $query.="(";
54
            foreach($row as $key => $val){
55
                $param = ":" . $key . $i;
56
                $query.= $param.", ";
57
                $bindAr[$param] = $val;
58
            }
59
60
            $query = rtrim($query, ", ");
61
            $query.="), ";
62
        }
63
64
        $query = rtrim($query, ", ");
65
        return ['query' => $query, 'array' => $bindAr];
66
    }
67
68
    private function checkMultipleInsert($rows = array()){
69
      return is_array($rows) && isset($rows[0]) && is_array($rows[0]);
70
    }
71
    /**
72
     * It inserts the new rows
73
     *
74
     * @param array $rows
75
     * @return integer $lastInsertedId
76
     * @throws Exception
77
     * @author RN Kushwaha <[email protected]>
78
     * @since v0.0.5
79
     */
80
    public function insert($rows, $table)
81
    {
82
        $qb = new QueryBuilder();
83
        $db = Connection::get();
84
85
        $dataToBuild = $rows;
86
        $methodToCall = 'buildInsertPlaceholder';
87
88
        if($this->checkMultipleInsert($rows)){
89
            $dataToBuild = $rows[0];
90
            $methodToCall = 'buildInsertPlaceholders';
91
        }
92
93
        $query   = $this->buildInsert($table, $dataToBuild);
94
        $dataRet = $this->$methodToCall($rows);
95
        $query.= $dataRet['query'];
96
        $bindAr = $dataRet['array'];
97
98
        try{
99
            $stmt = $db->prepare($qb->queryPrefix($query));
100
101
            if($this->checkMultipleInsert($rows)){
102
                foreach($bindAr as $param => $val){
103
                    $stmt->bindValue($param, $val);
104
                }
105
106
                $stmt->execute();
107
                return $db->lastInsertId();
108
            }
109
110
            $stmt->execute($bindAr);
111
            return $db->lastInsertId();
112
        } catch(Exception $e){
113
            throw new Exception($e->getMessage());
114
        }
115
    }
116
}
117