|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace vakata\database\driver\mysql; |
|
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
|
|
|
|
|
14
|
|
|
public function __construct(\mysqli_stmt $statement) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->statement = $statement; |
|
17
|
|
|
} |
|
18
|
|
|
public function __destruct() |
|
19
|
|
|
{ |
|
20
|
|
|
//@$this->statement->close(); |
|
|
|
|
|
|
21
|
|
|
} |
|
22
|
|
|
public function execute(array $data = []) : ResultInterface |
|
23
|
|
|
{ |
|
24
|
|
|
$data = array_values($data); |
|
25
|
|
|
$this->statement->reset(); |
|
26
|
|
|
if ($this->statement->param_count) { |
|
27
|
|
|
if (count($data) < $this->statement->param_count) { |
|
28
|
|
|
throw new DBException('Prepared execute - not enough parameters.'); |
|
29
|
|
|
} |
|
30
|
|
|
$lds = 32 * 1024; |
|
31
|
|
|
$ref = array(''); |
|
32
|
|
|
$lng = array(); |
|
33
|
|
|
$nul = null; |
|
34
|
|
|
foreach ($data as $i => $v) { |
|
35
|
|
|
switch (gettype($v)) { |
|
36
|
|
|
case 'boolean': |
|
37
|
|
|
case 'integer': |
|
38
|
|
|
$data[$i] = (int) $v; |
|
39
|
|
|
$ref[0] .= 'i'; |
|
40
|
|
|
$ref[$i + 1] = &$data[$i]; |
|
41
|
|
|
break; |
|
42
|
|
|
case 'NULL': |
|
43
|
|
|
$ref[0] .= 's'; |
|
44
|
|
|
$ref[$i + 1] = &$data[$i]; |
|
45
|
|
|
break; |
|
46
|
|
|
case 'double': |
|
47
|
|
|
$ref[0] .= 'd'; |
|
48
|
|
|
$ref[$i + 1] = &$data[$i]; |
|
49
|
|
|
break; |
|
50
|
|
|
default: |
|
51
|
|
|
if (is_resource($data[$i]) && get_resource_type($data[$i]) === 'stream') { |
|
52
|
|
|
$ref[0] .= 'b'; |
|
53
|
|
|
$ref[$i + 1] = &$nul; |
|
54
|
|
|
$lng[] = $i; |
|
55
|
|
|
continue; |
|
56
|
|
|
} |
|
57
|
|
|
if (!is_string($data[$i])) { |
|
58
|
|
|
$data[$i] = serialize($data[$i]); |
|
59
|
|
|
} |
|
60
|
|
|
if (strlen($data[$i]) > $lds) { |
|
61
|
|
|
$ref[0] .= 'b'; |
|
62
|
|
|
$ref[$i + 1] = &$nul; |
|
63
|
|
|
$lng[] = $i; |
|
64
|
|
|
} else { |
|
65
|
|
|
$ref[0] .= 's'; |
|
66
|
|
|
$ref[$i + 1] = &$data[$i]; |
|
67
|
|
|
} |
|
68
|
|
|
break; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
call_user_func_array(array($this->statement, 'bind_param'), $ref); |
|
72
|
|
|
foreach ($lng as $index) { |
|
73
|
|
|
if (is_resource($data[$index]) && get_resource_type($data[$index]) === 'stream') { |
|
74
|
|
|
while (!feof($data[$index])) { |
|
75
|
|
|
$this->statement->send_long_data($index, fread($data[$index], $lds)); |
|
76
|
|
|
} |
|
77
|
|
|
} else { |
|
78
|
|
|
$data[$index] = str_split($data[$index], $lds); |
|
79
|
|
|
foreach ($data[$index] as $chunk) { |
|
80
|
|
|
$this->statement->send_long_data($index, $chunk); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
if (!$this->statement->execute()) { |
|
86
|
|
|
throw new DBException('Prepared execute error'); |
|
87
|
|
|
} |
|
88
|
|
|
return new Result($this->statement); |
|
89
|
|
|
} |
|
90
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.