Completed
Push — master ( b3095d...070008 )
by Edgard
09:23
created

Connection::initConnection()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.3332

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 6
cts 9
cp 0.6667
rs 9.4285
cc 3
eloc 7
nc 3
nop 0
crap 3.3332
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
     * @inheritdoc
24
     */
25
    public $schemaMap = [
26
        'firebird' => 'edgardmessias\db\firebird\Schema', // Firebird
27
    ];
28
29
    /**
30
     * @inheritdoc
31
     */
32
    public $pdoClass = 'edgardmessias\db\firebird\PdoAdapter';
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public $commandClass = 'edgardmessias\db\firebird\Command';
38
    /**
39
     * @var Transaction the currently active transaction
40
     */
41
    private $_transaction;
42
43
    /**
44
     * Returns the currently active transaction.
45
     * @return Transaction the currently active transaction. Null if no active transaction.
46
     */
47 126
    public function getTransaction()
48
    {
49 126
        return $this->_transaction && $this->_transaction->getIsActive() ? $this->_transaction : null;
50
    }
51
52
    /**
53
     * Starts a transaction.
54
     * @param string|null $isolationLevel The isolation level to use for this transaction.
55
     * See [[Transaction::begin()]] for details.
56
     * @return Transaction the transaction initiated
57
     */
58 6
    public function beginTransaction($isolationLevel = null)
59
    {
60 6
        $this->open();
61
62 6
        if (($transaction = $this->getTransaction()) === null) {
63 6
            $transaction = $this->_transaction = new Transaction(['db' => $this]);
64 6
        }
65 6
        $transaction->begin($isolationLevel);
66
67 6
        return $transaction;
68
    }
69
70 250
    public function close()
71
    {
72 250
        if ($this->pdo !== null) {
73 148
            $this->_transaction = null;
74 148
        }
75 250
        parent::close();
76 250
    }
77
    
78 148
    protected function initConnection()
79
    {
80 148
        parent::initConnection();
81
        
82 148
        if ($this->firebird_version) {
83
            return;
84
        }
85
86 148
        $server_version = $this->pdo->getAttribute(\PDO::ATTR_SERVER_VERSION);
87
        
88 148
        if (preg_match('/WI-V(\d+\.\d+\.\d+).*remote server/', $server_version, $matches)) {
89
            $this->firebird_version = $matches[1];
90
        }
91 148
    }
92
}
93