InsertQueryBuilder::insert()   A
last analyzed

Complexity

Conditions 5
Paths 26

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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