|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace vakata\database\driver\odbc; |
|
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
|
|
|
|
|
12
|
|
|
class Driver extends DriverAbstract implements DriverInterface |
|
13
|
|
|
{ |
|
14
|
|
|
protected $lnk = null; |
|
15
|
|
|
protected $transaction = false; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(array $connection) |
|
18
|
|
|
{ |
|
19
|
|
|
$temp = explode('://', $connection['orig'], 2)[1]; |
|
20
|
|
|
$temp = array_pad(explode('?', $temp, 2), 2, ''); |
|
21
|
|
|
$connection = []; |
|
22
|
|
|
parse_str($temp[1], $connection['opts']); |
|
23
|
|
|
$temp = $temp[0]; |
|
24
|
|
|
if (strpos($temp, '@') !== false) { |
|
25
|
|
|
$temp = array_pad(explode('@', $temp, 2), 2, ''); |
|
26
|
|
|
list($connection['user'], $connection['pass']) = array_pad(explode(':', $temp[0], 2), 2, ''); |
|
27
|
|
|
$temp = $temp[1]; |
|
28
|
|
|
} |
|
29
|
|
|
$connection['dsn'] = $temp; |
|
30
|
|
|
$this->connection = $connection; |
|
31
|
|
|
} |
|
32
|
|
|
public function __destruct() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->disconnect(); |
|
35
|
|
|
} |
|
36
|
|
|
protected function connect() |
|
37
|
|
|
{ |
|
38
|
|
|
if ($this->lnk === null) { |
|
39
|
|
|
$this->lnk = call_user_func( |
|
40
|
|
|
$this->option('persist') ? '\odbc_pconnect' : '\odbc_connect', |
|
41
|
|
|
$this->connection['dsn'], |
|
42
|
|
|
isset($this->connection['user']) ? $this->connection['user'] : '', |
|
43
|
|
|
isset($this->connection['pass']) ? $this->connection['pass'] : '' |
|
44
|
|
|
); |
|
45
|
|
|
if ($this->lnk === false) { |
|
46
|
|
|
throw new DBException('Connect error'); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
protected function disconnect() |
|
51
|
|
|
{ |
|
52
|
|
|
if (is_resource($this->lnk)) { |
|
53
|
|
|
\odbc_close($this->lnk); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
public function prepare(string $sql) : StatementInterface |
|
57
|
|
|
{ |
|
58
|
|
|
$this->connect(); |
|
59
|
|
|
return new Statement($sql, $this->lnk); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function begin() : bool |
|
63
|
|
|
{ |
|
64
|
|
|
$this->connect(); |
|
65
|
|
|
$this->transaction = true; |
|
66
|
|
|
\odbc_autocommit($this->lnk, false); |
|
67
|
|
|
return true; |
|
68
|
|
|
} |
|
69
|
|
|
public function commit() : bool |
|
70
|
|
|
{ |
|
71
|
|
|
$this->connect(); |
|
72
|
|
|
$this->transaction = false; |
|
73
|
|
|
$res = \odbc_commit($this->lnk); |
|
74
|
|
|
\odbc_autocommit($this->lnk, false); |
|
75
|
|
|
return $res; |
|
76
|
|
|
} |
|
77
|
|
|
public function rollback() : bool |
|
78
|
|
|
{ |
|
79
|
|
|
$this->connect(); |
|
80
|
|
|
$this->transaction = false; |
|
81
|
|
|
$res = \odbc_rollback($this->lnk); |
|
82
|
|
|
\odbc_autocommit($this->lnk, false); |
|
83
|
|
|
return $res; |
|
84
|
|
|
} |
|
85
|
|
|
public function isTransaction() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->transaction; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|