Completed
Push — master ( 83cea8...e47c19 )
by Anton
17s queued 12s
created

Insert   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 58
ccs 11
cts 12
cp 0.9167
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSql() 0 5 1
A insert() 0 4 1
A execute() 0 7 2
1
<?php
2
3
/**
4
 * Bluz Framework Component
5
 *
6
 * @copyright Bluz PHP Team
7
 * @link      https://github.com/bluzphp/framework
8
 */
9
10
declare(strict_types=1);
11
12
namespace Bluz\Db\Query;
13
14
use Bluz\Proxy\Db;
15
16
/**
17
 * Builder of INSERT queries
18
 *
19
 * @package Bluz\Db\Query
20
 */
21
class Insert extends AbstractBuilder
22
{
23 1
    use Traits\Set;
24
25
    /**
26
     * @var string Table name
27
     */
28
    protected $table;
29
30
    /**
31
     * {@inheritdoc}
32
     *
33
     * @param  null $sequence
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $sequence is correct as it would always require null to be passed?
Loading history...
34
     *
35
     * @return integer|string|array
36
     */
37 1
    public function execute($sequence = null)
38
    {
39 1
        $result = Db::query($this->getSql(), $this->params, $this->types);
40 1
        if ($result) {
41 1
            return Db::handler()->lastInsertId($sequence);
42
        }
43
        return $result;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     *
49
     * @return string
50
     */
51 1
    public function getSql(): string
52
    {
53
        return 'INSERT INTO '
54 1
            . Db::quoteIdentifier($this->table)
55 1
            . $this->prepareSet();
56
    }
57
58
    /**
59
     * Turns the query being built into an insert query that inserts into
60
     * a certain table
61
     *
62
     * Example
63
     * <code>
64
     *     $ib = new InsertBuilder();
65
     *     $ib
66
     *         ->insert('users')
67
     *         ->set('name', 'username')
68
     *         ->set('password', md5('password'));
69
     * </code>
70
     *
71
     * @param  string $table The table into which the rows should be inserted
72
     *
73
     * @return Insert instance
74
     */
75 2
    public function insert($table): Insert
76
    {
77 2
        $this->table = $table;
78 2
        return $this;
79
    }
80
}
81