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\Connectors;
4
 
5
use PDO;
6
 
7
class MySqlConnector extends Connector implements ConnectorInterface
8
{
9
    /**
10
     * Establish a database connection.
11
     *
12
     * @param  array  $config
13
     * @return \PDO
14
     */
15
    public function connect(array $config)
16
    {
17
        $dsn = $this->getDsn($config);
18
 
19
        $options = $this->getOptions($config);
20
 
21
        // We need to grab the PDO options that should be used while making the brand
22
        // new connection instance. The PDO options control various aspects of the
23
        // connection's behavior, and some might be specified by the developers.
24
        $connection = $this->createConnection($dsn, $config, $options);
25
 
26
        if (! empty($config['database'])) {
27
            $connection->exec("use `{$config['database']}`;");
28
        }
29
 
30
        $this->configureIsolationLevel($connection, $config);
31
 
32
        $this->configureEncoding($connection, $config);
33
 
34
        // Next, we will check to see if a timezone has been specified in this config
35
        // and if it has we will issue a statement to modify the timezone with the
36
        // database. Setting this DB timezone is an optional configuration item.
37
        $this->configureTimezone($connection, $config);
38
 
39
        $this->setModes($connection, $config);
40
 
41
        return $connection;
42
    }
43
 
44
    /**
45
     * Set the connection transaction isolation level.
46
     *
47
     * @param  \PDO  $connection
48
     * @param  array  $config
49
     * @return void
50
     */
51
    protected function configureIsolationLevel($connection, array $config)
52
    {
53
        if (! isset($config['isolation_level'])) {
54
            return;
55
        }
56
 
57
        $connection->prepare(
58
            "SET SESSION TRANSACTION ISOLATION LEVEL {$config['isolation_level']}"
59
        )->execute();
60
    }
61
 
62
    /**
63
     * Set the connection character set and collation.
64
     *
65
     * @param  \PDO  $connection
66
     * @param  array  $config
67
     * @return void|\PDO
68
     */
69
    protected function configureEncoding($connection, array $config)
70
    {
71
        if (! isset($config['charset'])) {
72
            return $connection;
73
        }
74
 
75
        $connection->prepare(
76
            "set names '{$config['charset']}'".$this->getCollation($config)
77
        )->execute();
78
    }
79
 
80
    /**
81
     * Get the collation for the configuration.
82
     *
83
     * @param  array  $config
84
     * @return string
85
     */
86
    protected function getCollation(array $config)
87
    {
88
        return isset($config['collation']) ? " collate '{$config['collation']}'" : '';
89
    }
90
 
91
    /**
92
     * Set the timezone on the connection.
93
     *
94
     * @param  \PDO  $connection
95
     * @param  array  $config
96
     * @return void
97
     */
98
    protected function configureTimezone($connection, array $config)
99
    {
100
        if (isset($config['timezone'])) {
101
            $connection->prepare('set time_zone="'.$config['timezone'].'"')->execute();
102
        }
103
    }
104
 
105
    /**
106
     * Create a DSN string from a configuration.
107
     *
108
     * Chooses socket or host/port based on the 'unix_socket' config value.
109
     *
110
     * @param  array  $config
111
     * @return string
112
     */
113
    protected function getDsn(array $config)
114
    {
115
        return $this->hasSocket($config)
116
                            ? $this->getSocketDsn($config)
117
                            : $this->getHostDsn($config);
118
    }
119
 
120
    /**
121
     * Determine if the given configuration array has a UNIX socket value.
122
     *
123
     * @param  array  $config
124
     * @return bool
125
     */
126
    protected function hasSocket(array $config)
127
    {
128
        return isset($config['unix_socket']) && ! empty($config['unix_socket']);
129
    }
130
 
131
    /**
132
     * Get the DSN string for a socket configuration.
133
     *
134
     * @param  array  $config
135
     * @return string
136
     */
137
    protected function getSocketDsn(array $config)
138
    {
139
        return "mysql:unix_socket={$config['unix_socket']};dbname={$config['database']}";
140
    }
141
 
142
    /**
143
     * Get the DSN string for a host / port configuration.
144
     *
145
     * @param  array  $config
146
     * @return string
147
     */
148
    protected function getHostDsn(array $config)
149
    {
150
        extract($config, EXTR_SKIP);
151
 
152
        return isset($port)
153
                    ? "mysql:host={$host};port={$port};dbname={$database}"
154
                    : "mysql:host={$host};dbname={$database}";
155
    }
156
 
157
    /**
158
     * Set the modes for the connection.
159
     *
160
     * @param  \PDO  $connection
161
     * @param  array  $config
162
     * @return void
163
     */
164
    protected function setModes(PDO $connection, array $config)
165
    {
166
        if (isset($config['modes'])) {
167
            $this->setCustomModes($connection, $config);
168
        } elseif (isset($config['strict'])) {
169
            if ($config['strict']) {
170
                $connection->prepare($this->strictMode($connection, $config))->execute();
171
            } else {
172
                $connection->prepare("set session sql_mode='NO_ENGINE_SUBSTITUTION'")->execute();
173
            }
174
        }
175
    }
176
 
177
    /**
178
     * Set the custom modes on the connection.
179
     *
180
     * @param  \PDO  $connection
181
     * @param  array  $config
182
     * @return void
183
     */
184
    protected function setCustomModes(PDO $connection, array $config)
185
    {
186
        $modes = implode(',', $config['modes']);
187
 
188
        $connection->prepare("set session sql_mode='{$modes}'")->execute();
189
    }
190
 
191
    /**
192
     * Get the query to enable strict mode.
193
     *
194
     * @param  \PDO  $connection
195
     * @param  array  $config
196
     * @return string
197
     */
198
    protected function strictMode(PDO $connection, $config)
199
    {
200
        $version = $config['version'] ?? $connection->getAttribute(PDO::ATTR_SERVER_VERSION);
201
 
202
        if (version_compare($version, '8.0.11') >= 0) {
203
            return "set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'";
204
        }
205
 
206
        return "set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'";
207
    }
208
}