Completed
Push — master ( 0df900...0b8529 )
by Ryuichi
02:32
created

DatabaseDriver   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 131
rs 10
c 0
b 0
f 0
wmc 18

12 Methods

Rating   Name   Duplication   Size   Complexity  
A disconnect() 0 6 2
A getStatement() 0 3 2
A rollback() 0 4 2
A inTransaction() 0 3 2
A getTransactionIsolation() 0 3 1
A isConnected() 0 3 1
A commit() 0 4 2
A __destruct() 0 2 1
A beginTransaction() 0 9 2
A __construct() 0 3 1
A setTransactionIsolation() 0 3 1
A getConnection() 0 3 1
1
<?php
2
namespace WebStream\Database\Driver;
3
4
use WebStream\DI\Injector;
0 ignored issues
show
Bug introduced by
The type WebStream\DI\Injector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
use WebStream\Container\Container;
0 ignored issues
show
Bug introduced by
The type WebStream\Container\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Doctrine\DBAL\Connection;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Connection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
/**
9
 * DatabaseDriver
10
 * @author Ryuichi TANAKA.
11
 * @since 2013/12/07
12
 * @version 0.4
13
 */
14
abstract class DatabaseDriver
15
{
16
    use Injector;
17
18
    /**
19
     * @var object DBオブジェクト
20
     */
21
    protected $connection;
22
23
    /**
24
     * @var Container DB接続設定
25
     */
26
    protected $config;
27
28
    /**
29
     * constructor
30
     */
31
    public function __construct(Container $config)
32
    {
33
        $this->config = $config;
34
    }
35
36
    /**
37
     * destructor
38
     */
39
    public function __destruct()
40
    {
41
        // $this->logger->debug("Release driver: " . get_class($this));
42
    }
43
44
    /**
45
     * 接続する
46
     */
47
    abstract public function connect();
48
49
    /**
50
     * 切断する
51
     */
52
    public function disconnect()
53
    {
54
        if ($this->connection !== null) {
55
            $this->logger->debug("Database disconnect.");
56
            $this->connection->close();
57
            $this->connection = null;
58
        }
59
    }
60
61
    /**
62
     * トランザクションを開始する
63
     * @return boolean トランザクション開始結果
64
     */
65
    public function beginTransaction()
66
    {
67
        $inTransaction = false;
68
        if ($this->connection !== null) {
69
            $this->connection->beginTransaction();
70
            $inTransaction = $this->inTransaction();
71
        }
72
73
        return $inTransaction;
74
    }
75
76
    /**
77
     * コミットする
78
     */
79
    public function commit()
80
    {
81
        if ($this->connection !== null) {
82
            $this->connection->commit();
83
        }
84
    }
85
86
    /**
87
     * ロールバックする
88
     */
89
    public function rollback()
90
    {
91
        if ($this->inTransaction()) {
92
            $this->connection->rollback();
93
        }
94
    }
95
96
    /**
97
     * DB接続されているか
98
     * @param boolean 接続有無
99
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment 接続有無 at position 0 could not be parsed: Unknown type name '接続有無' at position 0 in 接続有無.
Loading history...
100
    public function isConnected()
101
    {
102
        return $this->connection !== null;
103
    }
104
105
    /**
106
     * トランザクション内かどうか
107
     * @return boolean トランザクション内かどうか
108
     */
109
    public function inTransaction()
110
    {
111
        return $this->connection !== null ? $this->connection->isTransactionActive() : false;
112
    }
113
114
    /**
115
     * SQLをセットしてステートメントを返却する
116
     * @param string SQL
117
     * @return object ステートメント
118
     */
119
    public function getStatement($sql)
120
    {
121
        return $this->connection !== null ? $this->connection->prepare($sql) : null;
122
    }
123
124
    /**
125
     * トランザクション分離レベルを返却する
126
     * @return int トランザクション分離レベル
127
     */
128
    public function getTransactionIsolation()
129
    {
130
        return $this->connection->getTransactionIsolation();
131
    }
132
133
    /**
134
     * トランザクション分離レベルを設定する
135
     * @param int $isolationLevel トランザクション分離レベル
136
     */
137
    public function setTransactionIsolation(int $isolationLevel)
138
    {
139
        $this->connection->setTransactionIsolation($isolationLevel);
140
    }
141
142
    public function getConnection()
143
    {
144
        return $this->connection;
145
    }
146
}
147