|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace vakata\database\driver\odbc; |
|
4
|
|
|
|
|
5
|
|
|
use \vakata\database\DBException; |
|
6
|
|
|
use \vakata\database\DriverInterface; |
|
7
|
|
|
use \vakata\database\StatementInterface; |
|
8
|
|
|
use \vakata\database\ResultInterface; |
|
9
|
|
|
|
|
10
|
|
|
class Statement implements StatementInterface |
|
11
|
|
|
{ |
|
12
|
|
|
protected $statement; |
|
13
|
|
|
protected $sql; |
|
14
|
|
|
protected $driver; |
|
15
|
|
|
protected $charIn; |
|
16
|
|
|
protected $charOut; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(string $statement, $driver, $charIn = null, $charOut = null) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->sql = $statement; |
|
21
|
|
|
$this->driver = $driver; |
|
22
|
|
|
$this->charIn = $charIn; |
|
23
|
|
|
$this->charOut = $charOut; |
|
24
|
|
|
$this->statement = \odbc_prepare($this->driver, $statement); |
|
25
|
|
|
} |
|
26
|
|
|
public function execute(array $data = []) : ResultInterface |
|
27
|
|
|
{ |
|
28
|
|
|
if (!is_array($data)) { |
|
29
|
|
|
$data = array(); |
|
30
|
|
|
} |
|
31
|
|
|
$data = $this->convert($data); |
|
32
|
|
|
$temp = \odbc_execute($this->statement, $data); |
|
33
|
|
|
if (!$temp) { |
|
34
|
|
|
throw new DBException('Could not execute query : '.\odbc_errormsg($this->driver)); |
|
35
|
|
|
} |
|
36
|
|
|
$iid = null; |
|
37
|
|
|
if (preg_match('@^\s*(INSERT|REPLACE)\s+INTO@i', $this->sql)) { |
|
38
|
|
|
$iid = \odbc_exec($this->driver, 'SELECT @@IDENTITY'); |
|
39
|
|
|
if ($iid && \odbc_fetch_row($iid)) { |
|
40
|
|
|
$iid = odbc_result($result, 1); |
|
|
|
|
|
|
41
|
|
|
} else { |
|
42
|
|
|
$iid = null; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
return new Result($this->statement, $data, $iid, $this->charIn, $this->charOut); |
|
46
|
|
|
} |
|
47
|
|
|
protected function convert($data) |
|
48
|
|
|
{ |
|
49
|
|
|
if (!is_callable("\iconv") || !isset($this->charIn) || !isset($this->charOut)) { |
|
50
|
|
|
return $data; |
|
51
|
|
|
} |
|
52
|
|
|
if (is_array($data)) { |
|
53
|
|
|
foreach ($data as $k => $v) { |
|
54
|
|
|
$data[$k] = $this->convert($v); |
|
55
|
|
|
} |
|
56
|
|
|
return $data; |
|
57
|
|
|
} |
|
58
|
|
|
if (is_string($data)) { |
|
59
|
|
|
return \iconv($this->charOut, $this->charIn, $data); |
|
60
|
|
|
} |
|
61
|
|
|
return $data; |
|
62
|
|
|
} |
|
63
|
|
|
} |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.