|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Phossa Project |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.4 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Library |
|
8
|
|
|
* @package Phossa2\Db |
|
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
|
10
|
|
|
* @license http://mit-license.org/ MIT License |
|
11
|
|
|
* @link http://www.phossa.com/ |
|
12
|
|
|
*/ |
|
13
|
|
|
/*# declare(strict_types=1); */ |
|
14
|
|
|
|
|
15
|
|
|
namespace Phossa2\Db\Driver\Mysqli; |
|
16
|
|
|
|
|
17
|
|
|
use Phossa2\Db\Types; |
|
18
|
|
|
use Phossa2\Db\Driver\StatementAbstract; |
|
19
|
|
|
use Phossa2\Db\Interfaces\ResultInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Statement |
|
23
|
|
|
* |
|
24
|
|
|
* Mysqli driver statement |
|
25
|
|
|
* |
|
26
|
|
|
* @package Phossa2\Db |
|
27
|
|
|
* @author Hong Zhang <[email protected]> |
|
28
|
|
|
* @see StatementAbstract |
|
29
|
|
|
* @version 2.0.0 |
|
30
|
|
|
* @since 2.0.0 added |
|
31
|
|
|
*/ |
|
32
|
|
|
class Statement extends StatementAbstract |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* Constructor |
|
36
|
|
|
* |
|
37
|
|
|
* @param ResultInterface $resultPrototype |
|
38
|
|
|
* @access public |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(ResultInterface $resultPrototype = null) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->result_prototype = $resultPrototype ?: new Result(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritDoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function realPrepare($link, /*# string */ $sql) |
|
49
|
|
|
{ |
|
50
|
|
|
/* @var $link \mysqli */ |
|
51
|
|
|
return $link->prepare($sql); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritDoc} |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function realExecute(array $parameters)/*# : bool */ |
|
58
|
|
|
{ |
|
59
|
|
|
/** @var $stmt \mysqli_stmt */ |
|
60
|
|
|
$stmt = $this->prepared; |
|
61
|
|
|
|
|
62
|
|
|
// bind parameters |
|
63
|
|
|
if (!empty($parameters) && |
|
64
|
|
|
!$this->bindParameters($stmt, $parameters) |
|
65
|
|
|
) { |
|
66
|
|
|
// bind failure |
|
67
|
|
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$res = $stmt->execute(); |
|
71
|
|
|
|
|
72
|
|
|
if ($stmt->result_metadata()) { |
|
73
|
|
|
$stmt->store_result(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $res; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritDoc} |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function realClose($stmt) |
|
83
|
|
|
{ |
|
84
|
|
|
/* @var $stmt \mysqli_stmt */ |
|
85
|
|
|
$stmt->close(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* bind parameters |
|
90
|
|
|
* |
|
91
|
|
|
* @param \mysqli_stmt $stmt |
|
92
|
|
|
* @param array $parameters |
|
93
|
|
|
* @return bool |
|
94
|
|
|
* @access protected |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function bindParameters( |
|
97
|
|
|
\mysqli_stmt $stmt, |
|
98
|
|
|
array $parameters |
|
99
|
|
|
)/*# : bool */ { |
|
100
|
|
|
$types = ''; |
|
101
|
|
|
$args = []; |
|
102
|
|
|
foreach ($parameters as $name => &$value) { |
|
103
|
|
|
$this->combineTypes($types, $value); |
|
104
|
|
|
$args[] = &$value; |
|
105
|
|
|
} |
|
106
|
|
|
if (count($args)) { |
|
107
|
|
|
array_unshift($args, $types); |
|
108
|
|
|
return call_user_func_array([$stmt, 'bind_param'], $args); |
|
109
|
|
|
} |
|
110
|
|
|
return true; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Combine types |
|
115
|
|
|
* |
|
116
|
|
|
* @param string &$types |
|
117
|
|
|
* @param mixed $value |
|
118
|
|
|
* @access protected |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function combineTypes(&$types, $value) |
|
121
|
|
|
{ |
|
122
|
|
|
$type = Types::guessType($value); |
|
123
|
|
|
switch ($type) { |
|
124
|
|
|
case Types::PARAM_INT: |
|
125
|
|
|
case Types::PARAM_BOOL: |
|
126
|
|
|
$types .= 'i'; |
|
127
|
|
|
break; |
|
128
|
|
|
default: |
|
129
|
|
|
$types .= 's'; |
|
130
|
|
|
break; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|