Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Database\PDO;
4
 
5
use Doctrine\DBAL\Driver\PDO\Exception;
6
use Doctrine\DBAL\Driver\PDO\Result;
7
use Doctrine\DBAL\Driver\PDO\Statement;
8
use Doctrine\DBAL\Driver\Result as ResultInterface;
9
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
10
use Doctrine\DBAL\Driver\Statement as StatementInterface;
11
use Doctrine\DBAL\ParameterType;
12
use PDO;
13
use PDOException;
14
use PDOStatement;
15
 
16
class Connection implements ServerInfoAwareConnection
17
{
18
    /**
19
     * The underlying PDO connection.
20
     *
21
     * @var \PDO
22
     */
23
    protected $connection;
24
 
25
    /**
26
     * Create a new PDO connection instance.
27
     *
28
     * @param  \PDO  $connection
29
     * @return void
30
     */
31
    public function __construct(PDO $connection)
32
    {
33
        $this->connection = $connection;
34
    }
35
 
36
    /**
37
     * Execute an SQL statement.
38
     *
39
     * @param  string  $statement
40
     * @return int
41
     */
42
    public function exec(string $statement): int
43
    {
44
        try {
45
            $result = $this->connection->exec($statement);
46
 
47
            \assert($result !== false);
48
 
49
            return $result;
50
        } catch (PDOException $exception) {
51
            throw Exception::new($exception);
52
        }
53
    }
54
 
55
    /**
56
     * Prepare a new SQL statement.
57
     *
58
     * @param  string  $sql
59
     * @return \Doctrine\DBAL\Driver\Statement
60
     */
61
    public function prepare(string $sql): StatementInterface
62
    {
63
        try {
64
            return $this->createStatement(
65
                $this->connection->prepare($sql)
66
            );
67
        } catch (PDOException $exception) {
68
            throw Exception::new($exception);
69
        }
70
    }
71
 
72
    /**
73
     * Execute a new query against the connection.
74
     *
75
     * @param  string  $sql
76
     * @return \Doctrine\DBAL\Driver\Result
77
     */
78
    public function query(string $sql): ResultInterface
79
    {
80
        try {
81
            $stmt = $this->connection->query($sql);
82
 
83
            \assert($stmt instanceof PDOStatement);
84
 
85
            return new Result($stmt);
86
        } catch (PDOException $exception) {
87
            throw Exception::new($exception);
88
        }
89
    }
90
 
91
    /**
92
     * Get the last insert ID.
93
     *
94
     * @param  string|null  $name
95
     * @return mixed
96
     */
97
    public function lastInsertId($name = null)
98
    {
99
        try {
100
            if ($name === null) {
101
                return $this->connection->lastInsertId();
102
            }
103
 
104
            return $this->connection->lastInsertId($name);
105
        } catch (PDOException $exception) {
106
            throw Exception::new($exception);
107
        }
108
    }
109
 
110
    /**
111
     * Create a new statement instance.
112
     *
113
     * @param  \PDOStatement  $stmt
114
     * @return \Doctrine\DBAL\Driver\PDO\Statement
115
     */
116
    protected function createStatement(PDOStatement $stmt): Statement
117
    {
118
        return new Statement($stmt);
119
    }
120
 
121
    /**
122
     * Begin a new database transaction.
123
     *
124
     * @return void
125
     */
126
    public function beginTransaction()
127
    {
128
        return $this->connection->beginTransaction();
129
    }
130
 
131
    /**
132
     * Commit a database transaction.
133
     *
134
     * @return void
135
     */
136
    public function commit()
137
    {
138
        return $this->connection->commit();
139
    }
140
 
141
    /**
142
     * Rollback a database transaction.
143
     *
144
     * @return void
145
     */
146
    public function rollBack()
147
    {
148
        return $this->connection->rollBack();
149
    }
150
 
151
    /**
152
     * Wrap quotes around the given input.
153
     *
154
     * @param  string  $input
155
     * @param  string  $type
156
     * @return string
157
     */
158
    public function quote($input, $type = ParameterType::STRING)
159
    {
160
        return $this->connection->quote($input, $type);
161
    }
162
 
163
    /**
164
     * Get the server version for the connection.
165
     *
166
     * @return string
167
     */
168
    public function getServerVersion()
169
    {
170
        return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
171
    }
172
 
173
    /**
174
     * Get the wrapped PDO connection.
175
     *
176
     * @return \PDO
177
     */
178
    public function getWrappedConnection(): PDO
179
    {
180
        return $this->connection;
181
    }
182
}