|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace vakata\database\driver\oracle; |
|
4
|
|
|
|
|
5
|
|
|
use \vakata\database\DBException; |
|
6
|
|
|
use \vakata\database\DriverInterface; |
|
7
|
|
|
use \vakata\database\DriverAbstract; |
|
8
|
|
|
use \vakata\database\StatementInterface; |
|
9
|
|
|
use \vakata\database\schema\Table; |
|
10
|
|
|
use \vakata\database\schema\TableRelation; |
|
11
|
|
|
use \vakata\collection\Collection; |
|
12
|
|
|
|
|
13
|
|
|
class Driver extends DriverAbstract implements DriverInterface |
|
14
|
|
|
{ |
|
15
|
|
|
use Schema; |
|
16
|
|
|
|
|
17
|
|
|
protected $lnk = null; |
|
18
|
|
|
protected $transaction = false; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(array $connection) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->connection = $connection; |
|
23
|
|
|
} |
|
24
|
|
|
public function __destruct() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->disconnect(); |
|
27
|
|
|
} |
|
28
|
|
|
protected function connect() |
|
29
|
|
|
{ |
|
30
|
|
|
if ($this->lnk === null) { |
|
31
|
|
|
$this->lnk = @call_user_func( |
|
32
|
|
|
$this->option('persist') ? '\oci_pconnect' : '\oci_connect', |
|
33
|
|
|
$this->connection['user'], |
|
34
|
|
|
$this->connection['pass'], |
|
35
|
|
|
$this->connection['host'], |
|
36
|
|
|
$this->option('charset', 'utf8') |
|
37
|
|
|
); |
|
38
|
|
|
if ($this->lnk === false) { |
|
39
|
|
|
throw new DBException('Connect error'); |
|
40
|
|
|
} |
|
41
|
|
|
$this->query("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"); |
|
42
|
|
|
if ($timezone = $this->option('timezone')) { |
|
43
|
|
|
$this->query("ALTER session SET time_zone = '".addslashes($timezone)."'"); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
public function test() : bool |
|
48
|
|
|
{ |
|
49
|
|
|
if ($this->lnk) { |
|
50
|
|
|
return true; |
|
51
|
|
|
} |
|
52
|
|
|
try { |
|
53
|
|
|
@$this->connect(); |
|
54
|
|
|
return true; |
|
55
|
|
|
} catch (\Exception $e) { |
|
56
|
|
|
$this->lnk = null; |
|
57
|
|
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
protected function disconnect() |
|
61
|
|
|
{ |
|
62
|
|
|
if ($this->lnk !== null && $this->lnk !== false) { |
|
63
|
|
|
\oci_close($this->lnk); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
public function prepare(string $sql) : StatementInterface |
|
67
|
|
|
{ |
|
68
|
|
|
$this->connect(); |
|
69
|
|
|
$binder = '?'; |
|
70
|
|
|
if (strpos($sql, $binder) !== false) { |
|
71
|
|
|
$tmp = explode($binder, $sql); |
|
72
|
|
|
$sql = ''; |
|
73
|
|
|
foreach ($tmp as $i => $v) { |
|
74
|
|
|
$sql .= $v; |
|
75
|
|
|
if (isset($tmp[($i + 1)])) { |
|
76
|
|
|
$sql .= ':f'.$i; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
$temp = \oci_parse($this->lnk, $sql); |
|
81
|
|
|
if (!$temp) { |
|
82
|
|
|
$err = \oci_error(); |
|
83
|
|
|
if (!is_array($err)) { |
|
84
|
|
|
$err = []; |
|
85
|
|
|
} |
|
86
|
|
|
throw new DBException('Could not prepare : '.implode(', ', $err).' <'.$sql.'>'); |
|
87
|
|
|
} |
|
88
|
|
|
return new Statement($temp, $this); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function begin() : bool |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->transaction = true; |
|
94
|
|
|
} |
|
95
|
|
|
public function commit() : bool |
|
96
|
|
|
{ |
|
97
|
|
|
$this->connect(); |
|
98
|
|
|
if (!$this->transaction) { |
|
99
|
|
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
if (!\oci_commit($this->lnk)) { |
|
102
|
|
|
return false; |
|
103
|
|
|
} |
|
104
|
|
|
$this->transaction = false; |
|
105
|
|
|
return true; |
|
106
|
|
|
} |
|
107
|
|
|
public function rollback() : bool |
|
108
|
|
|
{ |
|
109
|
|
|
$this->connect(); |
|
110
|
|
|
if (!$this->transaction) { |
|
111
|
|
|
return false; |
|
112
|
|
|
} |
|
113
|
|
|
if (!\oci_rollback($this->lnk)) { |
|
114
|
|
|
return false; |
|
115
|
|
|
} |
|
116
|
|
|
$this->transaction = false; |
|
117
|
|
|
return true; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function isTransaction() |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->transaction; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function lob() |
|
126
|
|
|
{ |
|
127
|
|
|
return \oci_new_descriptor($this->lnk, \OCI_D_LOB); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|