Connection::quote()   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 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\Db\Pdo;
4
5
use Exception;
6
use Lagdo\DbAdmin\Driver\Db\Connection as AbstractConnection;
7
use Lagdo\DbAdmin\Driver\Db\Pdo\Statement;
8
use Lagdo\DbAdmin\Driver\Exception\AuthException;
9
use PDO;
10
11
use function count;
12
13
abstract class Connection extends AbstractConnection
14
{
15
    /**
16
     * Create a PDO connection
17
     *
18
     * @param string $dsn
19
     * @param string $username
20
     * @param string $password
21
     * @param array $options
22
     *
23
     * @return void
24
     */
25
    public function dsn(string $dsn, string $username, string $password, array $options = [])
26
    {
27
        try {
28
            $this->client = new PDO($dsn, $username, $password, $options);
29
        } catch (Exception $ex) {
30
            // auth_error(h($ex->getMessage()));
31
            throw new AuthException($this->utils->str->html($ex->getMessage()));
32
        }
33
        $this->client->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
34
        $this->client->setAttribute(PDO::ATTR_STATEMENT_CLASS, array(Statement::class));
35
    }
36
37
    /**
38
     * @inheritDoc
39
     */
40
    public function serverInfo()
41
    {
42
        return @$this->client->getAttribute(PDO::ATTR_SERVER_VERSION);
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function quote(string $string)
49
    {
50
        return $this->client->quote($string);
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public function query(string $query, bool $unbuffered = false)
57
    {
58
        $statement = $this->client->query($query);
59
        $this->driver->setError();
60
        if (!$statement) {
61
            list(, $errno, $error) = $this->client->errorInfo();
62
            $this->driver->setErrno($errno);
63
            $this->driver->setError(($error) ? $error : $this->utils->trans->lang('Unknown error.'));
64
            return false;
65
        }
66
        // rowCount() is not guaranteed to work with all drivers
67
        if (($statement->numRows = $statement->rowCount()) > 0) {
68
            $this->setAffectedRows($statement->numRows);
69
        }
70
        return $statement;
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76
    public function multiQuery(string $query)
77
    {
78
        $this->statement = $this->driver->execute($query);
79
        return $this->statement !== false;
80
    }
81
82
    /**
83
     * @inheritDoc
84
     */
85
    public function storedResult()
86
    {
87
        if (!$this->statement) {
88
            return null;
89
        }
90
        // rowCount() is not guaranteed to work with all drivers
91
        if ($this->statement->rowCount() > 0) {
92
            $this->setAffectedRows($this->statement->rowCount());
93
        }
94
        return $this->statement;
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100
    public function nextResult()
101
    {
102
        if (!$this->statement) {
103
            return false;
104
        }
105
        $this->statement->offset = 0;
106
        return $this->statement->nextRowset(); // @ - PDO_PgSQL doesn't support it
107
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112
    public function result(string $query, int $field = -1)
113
    {
114
        if ($field < 0) {
115
            $field = $this->defaultField();
116
        }
117
        if (!($statement = $this->driver->execute($query))) {
118
            return null;
119
        }
120
        if (!($row = $statement->fetchRow())) {
121
            return null;
122
        }
123
        return count($row) > $field ? $row[$field] : null;
124
    }
125
126
    /**
127
     * @inheritDoc
128
     */
129
    public function close()
130
    {
131
        $this->client = null;
132
    }
133
}
134