📦 directus / v6-archive

📄 Connection.php · 95 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95<?php

/**
 * Directus – <http://getdirectus.com>
 *
 * @link      The canonical repository – <https://github.com/directus/directus>
 * @copyright Copyright 2006-2017 RANGER Studio, LLC – <http://rangerstudio.com>
 * @license   GNU General Public License (v3) – <http://www.gnu.org/copyleft/gpl.html>
 */

namespace Directus\Database;

use Zend\Db\Adapter\Adapter;

/**
 * Connection Adapter
 *
 * @author Welling Guzmán <welling@rngr.org>
 */
class Connection extends Adapter
{
    /**
     * Database configuration
     *
     * @var array
     */
    protected $config = [];

    /**
     * Check if this connection has strict mode enabled.
     *
     * @return bool
     *
     * @throws \BadMethodCallException
     */
    public function isStrictModeEnabled()
    {
        $enabled = false;
        $driverName = $this->getDriver()->getDatabasePlatformName();

        switch (strtolower($driverName)) {
            case 'mysql':
                $enabled = $this->isMySQLStrictModeEnabled();
                break;
        }

        return $enabled;
    }

    /**
     * Check if MySQL has Strict mode enabled
     *
     * @return bool
     */
    protected function isMySQLStrictModeEnabled()
    {
        $strictModes = ['STRICT_ALL_TABLES', 'STRICT_TRANS_TABLES'];
        $statement = $this->query('SELECT @@sql_mode as modes');
        $result = $statement->execute();
        $modesEnabled = $result->current();

        $modes = explode(',', $modesEnabled['modes']);
        foreach ($modes as $name) {
            $modeName = strtoupper(trim($name));
            if (in_array($modeName, $strictModes)) {
                return true;
            }
        }

        return false;
    }

    /**
     * Connect to the database
     *
     * @return \Zend\Db\Adapter\Driver\ConnectionInterface
     */
    public function connect()
    {
        return call_user_func_array([$this->getDriver()->getConnection(), 'connect'], func_get_args());
    }

    /**
     * Execute an query string
     *
     * @param $sql
     *
     * @return \Zend\Db\Adapter\Driver\StatementInterface|\Zend\Db\ResultSet\ResultSet
     */
    public function execute($sql)
    {
        return $this->query($sql, static::QUERY_MODE_EXECUTE);
    }
}