Completed
Push — master ( de6839...cc0d83 )
by Edgard
37:12
created

Connection   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 130
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getTransaction() 0 4 3
A beginTransaction() 0 11 2
A close() 0 7 2
C init() 0 32 7
A __clone() 0 6 1
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace edgardmessias\db\firebird;
9
10
/**
11
 *
12
 * @author Edgard Lorraine Messias <[email protected]>
13
 * @since 2.0
14
 */
15
class Connection extends \yii\db\Connection
16
{
17
    /**
18
     * Firebird server version
19
     */
20
    public $firebird_version = null;
21
    
22
    /**
23
     * @see https://www.firebirdsql.org/file/documentation/release_notes/html/en/3_0/rnfb30-ddl-enhance.html#rnfb30-ddl-identity
24
     * @var boolean|null
25
     */
26
    public $supportColumnIdentity = null;
27
    
28
    /**
29
     * @see https://firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25-dml-insert.html#fblangref25-dml-insert-select-unstable
30
     * @var boolean|null
31
     */
32
    public $supportStableCursor = null;
33
    
34
    /**
35
     * @see https://bugs.php.net/bug.php?id=72931
36
     * @var boolean|null
37
     */
38
    public $supportReturningInsert = null;
39
    
40
    /**
41
     * @see https://bugs.php.net/bug.php?id=61183
42
     * @var boolean|null
43
     */
44
    public $supportBlobDataType = null;
45
    
46
    /**
47 138
     * @inheritdoc
48
     */
49 138
    public $schemaMap = [
50
        'firebird' => 'edgardmessias\db\firebird\Schema', // Firebird
51
    ];
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public $pdoClass = 'edgardmessias\db\firebird\PdoAdapter';
57
58 6
    /**
59
     * @inheritdoc
60 6
     */
61
    public $commandClass = 'edgardmessias\db\firebird\Command';
62 6
    /**
63 6
     * @var Transaction the currently active transaction
64
     */
65 6
    private $_transaction;
66
67 6
    /**
68
     * Returns the currently active transaction.
69
     * @return Transaction the currently active transaction. Null if no active transaction.
70 263
     */
71
    public function getTransaction()
72 263
    {
73 160
        return $this->_transaction && $this->_transaction->getIsActive() ? $this->_transaction : null;
74
    }
75 263
76 263
    /**
77
     * Starts a transaction.
78 160
     * @param string|null $isolationLevel The isolation level to use for this transaction.
79
     * See [[Transaction::begin()]] for details.
80 160
     * @return Transaction the transaction initiated
81
     */
82 160
    public function beginTransaction($isolationLevel = null)
83 148
    {
84
        $this->open();
85
86 160
        if (($transaction = $this->getTransaction()) === null) {
87
            $transaction = $this->_transaction = new Transaction(['db' => $this]);
88 160
        }
89 160
        $transaction->begin($isolationLevel);
90
91 160
        return $transaction;
92
    }
93
94
    public function close()
95
    {
96
        if ($this->pdo !== null) {
97
            $this->_transaction = null;
98
        }
99
        parent::close();
100
    }
101
102
    public function init()
103
    {
104
        parent::init();
105
        
106
        if ($this->firebird_version) {
107
            return;
108
        }
109
110
        try {
111
            $pdo = $this->createPdoInstance();
112
113
            $server_version = $pdo->getAttribute(\PDO::ATTR_SERVER_VERSION);
114
115
            if (preg_match('/\w{2}-[TV](\d+\.\d+\.\d+).*remote server/', $server_version, $matches)) {
116
                $this->firebird_version = $matches[1];
117
            }
118
        } catch (\Exception $ex) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
119
        }
120
        
121
        $supports = [
122
            'supportColumnIdentity' => version_compare($this->firebird_version, '3.0.0', '>='),
123
            'supportStableCursor' => version_compare($this->firebird_version, '3.0.0', '>='),
124
            'supportReturningInsert' => version_compare($this->firebird_version, '3.0.0', '<') || version_compare(phpversion('pdo_firebird'), '7.0.15', '>='),
125
            'supportBlobDataType' => version_compare(phpversion('pdo_firebird'), '7.0.13', '>'),
126
        ];
127
        
128
        foreach ($supports as $key => $value) {
129
            if ($this->{$key} === null) {
130
                $this->{$key} = $value;
131
            }
132
        }
133
    }
134
135
    /**
136
     * Reset the connection after cloning.
137
     */
138
    public function __clone()
139
    {
140
        parent::__clone();
141
142
        $this->_transaction = null;
143
    }
144
}
145