MySQLi::languageDialect()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace kalanis\kw_mapper\Storage\Database\Raw;
4
5
6
use kalanis\kw_mapper\MapperException;
7
use kalanis\kw_mapper\Storage\Database\ASQL;
8
use kalanis\kw_mapper\Storage\Database\Dialects;
9
use kalanis\kw_mapper\Storage\Database\TBindNames;
10
11
12
/**
13
 * Class MySQLi
14
 * @package kalanis\kw_mapper\Storage\Database\Raw
15
 * Problematic connector to MySQL just for compatibility - USE PDO instead!!!
16
 * @codeCoverageIgnore remote connection
17
 */
18
class MySQLi extends ASQL
19
{
20
    use TBindNames;
21
22
    protected string $extension = 'mysqli';
23
    /** @var \mysqli|null */
24
    protected $connection = null;
25
    protected ?\mysqli_stmt $lastStatement = null;
26
27
    public function disconnect(): void
28
    {
29
        if ($this->isConnected()) {
30
            $this->connection->close();
31
        }
32
        $this->connection = null;
33
    }
34
35
    public function languageDialect(): string
36
    {
37
        return Dialects\MySQL::class;
38
    }
39
40
    public function query(string $query, array $params, int $fetchType = MYSQLI_ASSOC): array
41
    {
42
        if (empty($query)) {
43
            return [];
44
        }
45
46
        $this->connect();
47
48
        $statement = $this->connection->stmt_init();
49
        list($updQuery, $binds, $types) = $this->bindFromNamedToQuestions($query, $params);
50
        $statement->prepare(strval($updQuery));
51
        if (!empty($binds)) {
52
            $statement->bind_param(implode('', $types), ...$binds); // @phpstan-ignore-line
53
        }
54
        if (!$statement->execute()) {
55
            throw new MapperException('mysqli execute error: ' . $statement->error);
56
        }
57
        $result = $statement->get_result();
58
59
        $this->lastStatement = $statement;
60
61
        return $result ? $result->fetch_all($fetchType) : [];
0 ignored issues
show
introduced by
$result is of type mysqli_result, thus it always evaluated to true.
Loading history...
62
    }
63
64
    public function exec(string $query, array $params): bool
65
    {
66
        if (empty($query)) {
67
            return false;
68
        }
69
70
        $this->connect();
71
72
        $statement = $this->connection->stmt_init();
73
        list($updQuery, $binds, $types) = $this->bindFromNamedToQuestions($query, $params);
74
        $statement->prepare(strval($updQuery));
75
        if (!empty($binds)) {
76
            $statement->bind_param(implode('', $types), ...$binds); // @phpstan-ignore-line
77
        }
78
        $this->lastStatement = $statement;
79
80
        if (!$statement->execute()) {
81
            throw new MapperException('mysqli execute error: ' . $statement->error);
82
        }
83
        return true;
84
    }
85
86
    /**
87
     * @throws MapperException
88
     */
89
    public function connect(): void
90
    {
91
        if (!$this->isConnected()) {
92
            $this->connection = $this->connectToServer();
93
        }
94
    }
95
96
    /**
97
     * @throws MapperException
98
     * @return \mysqli
99
     */
100
    protected function connectToServer(): \mysqli
101
    {
102
        $connection = new \mysqli(
103
            $this->config->getLocation(),
104
            $this->config->getUser(),
105
            $this->config->getPassword(),
106
            $this->config->getDatabase(),
107
            $this->config->getPort()
108
        );
109
        if ($connection->connect_errno) {
110
            throw new MapperException('mysqli connection error: ' . $connection->connect_error);
111
        }
112
113
//        foreach ($this->attributes as $key => $value){
114
//            $connection->setAttribute($key, $value);
115
//        }
116
117
        $connection->set_charset('utf8');
118
        if ($connection->errno) {
119
            throw new MapperException('mysqli error: ' . $connection->error);
120
        }
121
        $connection->query('SET NAMES utf8;');
122
123
        return $connection;
124
    }
125
126
    public function lastInsertId(): ?string
127
    {
128
        return $this->lastStatement ? strval($this->lastStatement->insert_id) : null ;
129
    }
130
131
    public function rowCount(): ?int
132
    {
133
        return $this->lastStatement ? intval($this->lastStatement->num_rows) : null ;
134
    }
135
136
    public function beginTransaction(): bool
137
    {
138
        if (!$this->isConnected()) {
139
            $this->connection = $this->connectToServer();
140
        }
141
142
        return $this->connection->begin_transaction();
143
    }
144
145
    public function commit(): bool
146
    {
147
        return $this->connection->commit();
0 ignored issues
show
Bug introduced by
The method commit() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

147
        return $this->connection->/** @scrutinizer ignore-call */ commit();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
    }
149
150
    public function rollBack(): bool
151
    {
152
        return $this->connection->rollBack();
153
    }
154
}
155