1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
// Copyright (c) Dmitry Kosenkov, All rights reserved. |
4
|
|
|
|
5
|
|
|
namespace HSAL\Driver; |
6
|
|
|
|
7
|
|
|
use \HSAL\HSAL; |
8
|
|
|
use \HSAL\Driver; |
9
|
|
|
use \HSAL\DriverInterface; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class HSPHP extends Driver implements DriverInterface |
13
|
|
|
{ |
14
|
|
|
private $hsr; |
15
|
|
|
private $hsr_connected = FALSE; |
16
|
|
|
|
17
|
|
|
private $hsw; |
18
|
|
|
private $hsw_connected = FALSE; |
19
|
|
|
|
20
|
|
|
protected function getReadSocket() |
21
|
|
|
{ |
22
|
|
|
if (!$this->hsr_connected) |
23
|
|
|
{ |
24
|
|
|
$this->hsr = new \HSPHP\ReadSocket(); |
25
|
|
|
|
26
|
|
|
$this->hsr->connect($this->host, $this->port_read); |
27
|
|
|
|
28
|
|
|
$this->hsr_connected = TRUE; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $this->hsr; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function getWriteSocket() |
35
|
|
|
{ |
36
|
|
|
if (!$this->hsw_connected) |
37
|
|
|
{ |
38
|
|
|
$this->hsw = new \HSPHP\WriteSocket(); |
39
|
|
|
|
40
|
|
|
$this->hsw->connect($this->host, $this->port_write); |
41
|
|
|
|
42
|
|
|
$this->hsw_connected = TRUE; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this->hsw; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
public function fetchArray($table, Array $fields, $index, Array $condition, $operator) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$hs = $this->getReadSocket(); |
51
|
|
|
|
52
|
|
|
list($database, $table) = $this->getTableDatabase($table); |
53
|
|
|
|
54
|
|
|
$idx = $hs->getIndexId($database, $table, $index, implode(',',$fields)); |
55
|
|
|
$hs->select($idx, $operator, $condition); |
56
|
|
|
$result = $hs->readResponse(); |
57
|
|
|
|
58
|
|
|
if (!empty($result)) |
59
|
|
|
return $result[0]; |
60
|
|
|
else |
61
|
|
|
return FALSE; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function fetchAll($table, Array $fields, $index, Array $condition, $operator, $limit, $offset) |
65
|
|
|
{ |
66
|
|
|
$hs = $this->getReadSocket(); |
67
|
|
|
|
68
|
|
|
list($database, $table) = $this->getTableDatabase($table); |
69
|
|
|
|
70
|
|
|
$idx = $hs->getIndexId($database, $table, $index, implode(',', $fields)); |
71
|
|
|
$hs->select($idx, $operator, $condition, $limit, $offset); |
72
|
|
|
|
73
|
|
|
$result = $hs->readResponse(); |
74
|
|
|
|
75
|
|
View Code Duplication |
if (!empty($result)) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
foreach ($result as $key => $arr) |
78
|
|
|
{ |
79
|
|
|
$result[$key] = array_combine($fields, $arr); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $result; |
83
|
|
|
} |
84
|
|
|
else |
85
|
|
|
return FALSE; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
public function delete($table, $index, Array $condition, $operator) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$hs = $this->getWriteSocket(); |
91
|
|
|
|
92
|
|
|
list($database, $table) = $this->getTableDatabase($table); |
93
|
|
|
|
94
|
|
|
$idx = $hs->getIndexId($database, $table, $index, ''); |
95
|
|
|
$hs->delete($idx, $operator, $condition); |
96
|
|
|
$result = $hs->readResponse(); |
97
|
|
|
|
98
|
|
|
if (!empty($result)) |
99
|
|
|
return (bool)$result[0][0]; |
100
|
|
|
else |
101
|
|
|
return FALSE; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function insert($table, Array $values) |
105
|
|
|
{ |
106
|
|
|
$hs = $this->getWriteSocket(); |
107
|
|
|
|
108
|
|
|
list($database, $table) = $this->getTableDatabase($table); |
109
|
|
|
|
110
|
|
|
$fields = array_keys($values); |
111
|
|
|
|
112
|
|
|
$idx = $hs->getIndexId($database, $table, '', implode(',',$fields)); |
113
|
|
|
$hs->insert($idx, array_values($values)); |
114
|
|
|
|
115
|
|
|
$result = $hs->readResponse(); |
116
|
|
|
|
117
|
|
|
return is_array($result) ? TRUE : FALSE; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function update($table, Array $values, $index, Array $condition, $operator) |
121
|
|
|
{ |
122
|
|
|
$hs = $this->getWriteSocket(); |
123
|
|
|
|
124
|
|
|
list($database, $table) = $this->getTableDatabase($table); |
125
|
|
|
|
126
|
|
|
$fields = array_keys($values); |
127
|
|
|
|
128
|
|
|
$idx = $hs->getIndexId($database, $table, $index, implode(',', $fields)); |
129
|
|
|
$hs->update($idx, $operator, $condition, array_values($values)); |
130
|
|
|
|
131
|
|
|
$result = $hs->readResponse(); |
132
|
|
|
|
133
|
|
|
if (!empty($result)) |
134
|
|
|
return (bool)$result[0][0]; |
135
|
|
|
else |
136
|
|
|
return FALSE; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
View Code Duplication |
public function increment($table, $field, $index, Array $condition, $operator, $increment) |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
$hs = $this->getWriteSocket(); |
142
|
|
|
|
143
|
|
|
list($database, $table) = $this->getTableDatabase($table); |
144
|
|
|
|
145
|
|
|
$idx = $hs->getIndexId($database, $table, $index, [$field]); |
146
|
|
|
$hs->increment($idx, $operator, $condition, [$increment]); |
147
|
|
|
$result = $hs->readResponse(); |
148
|
|
|
|
149
|
|
|
if (!empty($result)) |
150
|
|
|
return (bool)$result[0][0]; |
151
|
|
|
else |
152
|
|
|
return FALSE; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
View Code Duplication |
public function decrement($table, $field, $index, Array $condition, $operator, $decrement) |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
$hs = $this->getWriteSocket(); |
158
|
|
|
|
159
|
|
|
list($database, $table) = $this->getTableDatabase($table); |
160
|
|
|
|
161
|
|
|
$idx = $hs->getIndexId($database, $table, $index, [$field]); |
162
|
|
|
$hs->decrement($idx, $operator, $condition, [$decrement]); |
163
|
|
|
$result = $hs->readResponse(); |
164
|
|
|
|
165
|
|
|
if (!empty($result)) |
166
|
|
|
return (bool)$result[0][0]; |
167
|
|
|
else |
168
|
|
|
return FALSE; |
169
|
|
|
} |
170
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.