Completed
Push — master ( 8155ff...9fff48 )
by Changwan
03:00
created

MysqlConnection::bindValues()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.3541

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 5
nop 2
dl 0
loc 19
ccs 11
cts 14
cp 0.7856
crap 6.3541
rs 8.8571
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Database\Connection;
3
4
use Exception;
5
use PDO;
6
use PDOStatement;
7
use Throwable;
8
use Wandu\Collection\ArrayList;
9
use Wandu\Database\Configuration;
10
use Wandu\Database\Contracts\ConnectionInterface;
11
use Wandu\Database\Contracts\QueryInterface;
12
use Wandu\Database\Events\Connect;
13
use Wandu\Database\Events\ExecuteQuery;
14
use Wandu\Database\QueryBuilder;
15
use Wandu\Event\DispatcherInterface;
16
17
class MysqlConnection implements ConnectionInterface
18
{
19
    /** @var \PDO */
20
    protected $pdo;
21
22
    /** @var \Wandu\Database\Configuration */
23
    protected $config;
24
25
    /** @var \Wandu\Event\DispatcherInterface */
26
    protected $dispatcher;
27
    
28 19
    public function __construct(
29
        Configuration $config,
30
        DispatcherInterface $dispatcher = null
31
    ) {
32 19
        $this->config = $config;
33 19
        $this->dispatcher = $dispatcher;
34 19
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 16
    public function createQueryBuilder(string $tableName): QueryBuilder
40
    {
41 16
        return new QueryBuilder($this->config->getPrefix() . $tableName);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 19
    public function connect()
48
    {
49 19
        if (!$this->pdo) {
50 19
            $this->pdo = $this->config->createPdo();
51 19
            if ($this->dispatcher) {
52 18
                $this->dispatcher->trigger(new Connect());
53
            }
54
        }
55 19
        return $this;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 8
    public function fetch($query, array $bindings = [])
62
    {
63 8
        $statement = $this->execute($query, $bindings);
64 8
        while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
65 8
            yield $row;
66
        }
67 8
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function all($query, array $bindings = [])
73
    {
74
        return new ArrayList($this->fetch($query, $bindings));
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 12
    public function first($query, array $bindings = [])
81
    {
82 12
        $attributes = $this->execute($query, $bindings)->fetch(PDO::FETCH_ASSOC);
83 12
        return $attributes ?: null;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 2
    public function query($query, array $bindings = [])
90
    {
91 2
        return $this->execute($query, $bindings)->rowCount();
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 1
    public function getLastInsertId()
98
    {
99 1
        return $this->pdo->lastInsertId();
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function transaction(callable $handler)
106
    {
107
        $this->pdo->beginTransaction();
108
        try {
109
            call_user_func($handler, $this);
110
        } catch (Exception $e) {
111
            $this->pdo->rollBack();
112
            throw $e;
113
        } catch (Throwable $e) {
114
            $this->pdo->rollBack();
115
            throw $e;
116
        }
117
        $this->pdo->commit();
118
    }
119
120
    /**
121
     * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query
122
     * @param array $bindings
123
     * @return \PDOStatement
124
     */
125 18
    protected function execute($query, array $bindings = [])
126
    {
127 18
        while (is_callable($query)) {
128 2
            $query = call_user_func($query);
129
        }
130 18
        if ($query instanceof QueryInterface) {
131 12
            $bindings = $query->getBindings();
132 12
            $query = $query->toSql();
133
        }
134 18
        $statement = $this->pdo->prepare($query);
135 18
        $this->bindValues($statement, $bindings);
136 18
        $statement->execute();
137 18
        if ($this->dispatcher) {
138 18
            $this->dispatcher->trigger(new ExecuteQuery($statement->queryString, $bindings));
139
        }
140 18
        return $statement;
141
    }
142
    
143
    /**
144
     * @param \PDOStatement $statement
145
     * @param array $bindings
146
     */
147 18
    protected function bindValues(PDOStatement $statement, array $bindings = [])
148
    {
149 18
        foreach ($bindings as $key => $value) {
150 18
            if (is_int($value)) {
151 11
                $dataType = PDO::PARAM_INT;
152 12
            } elseif (is_bool($value)) {
153
                $dataType = PDO::PARAM_BOOL;
154 12
            } elseif (is_null($value)) {
155
                $dataType = PDO::PARAM_NULL;
156
            } else {
157 12
                $dataType = PDO::PARAM_STR;
158
            }
159 18
            $statement->bindValue(
160 18
                is_int($key) ? $key + 1 : $key,
161
                $value,
162 18
                $dataType
163
            );
164
        }
165 18
    }
166
}
167