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
|
|
|
class Handlersocketi extends Driver implements DriverInterface |
12
|
|
|
{ |
13
|
|
|
private $hsr; |
14
|
|
|
private $hsr_connected = FALSE; |
15
|
|
|
|
16
|
|
|
private $hsw; |
17
|
|
|
private $hsw_connected = FALSE; |
18
|
|
|
|
19
|
|
View Code Duplication |
protected function getReadSocket() |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
if (!$this->hsr_connected) |
22
|
|
|
{ |
23
|
|
|
$options = ['timeout' => (isset($this->options['timeout']) ? $this->options['timeout'] : 5)]; |
24
|
|
|
|
25
|
|
|
$this->hsr = new \HandlerSocketi($this->host, $this->port_read, $options); |
26
|
|
|
|
27
|
|
|
$this->hsr_connected = TRUE; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return $this->hsr; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
View Code Duplication |
protected function getWriteSocket() |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
if (!$this->hsw_connected) |
36
|
|
|
{ |
37
|
|
|
$options = ['timeout' => (isset($this->options['timeout']) ? $this->options['timeout'] : 5)]; |
38
|
|
|
|
39
|
|
|
$this->hsw = new \HandlerSocketi($this->host, $this->port_write, $options); |
40
|
|
|
|
41
|
|
|
$this->hsw_connected = TRUE; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this->hsw; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
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
|
|
|
if ($index == HSAL::INDEX_PRIMARY) $index = 'PRIMARY'; |
55
|
|
|
|
56
|
|
|
$idx = $hs->openIndex($database, $table, $fields, ['index' => $index]); |
57
|
|
|
|
58
|
|
|
$result = $idx->find([$operator => $condition]); |
59
|
|
|
|
60
|
|
|
if (!empty($result) && is_array($result)) |
61
|
|
|
return $result[0]; |
62
|
|
|
else |
63
|
|
|
return FALSE; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function fetchAll($table, Array $fields, $index, Array $condition, $operator, $limit, $offset) |
67
|
|
|
{ |
68
|
|
|
$hs = $this->getReadSocket(); |
69
|
|
|
|
70
|
|
|
list($database, $table) = $this->getTableDatabase($table); |
71
|
|
|
|
72
|
|
|
if ($index == HSAL::INDEX_PRIMARY) $index = 'PRIMARY'; |
73
|
|
|
|
74
|
|
|
$idx = $hs->openIndex($database, $table, $fields, ['index' => $index]); |
75
|
|
|
|
76
|
|
|
$result = $idx->find([$operator => $condition], ['limit' => $limit, 'offset' => $offset]); |
77
|
|
|
|
78
|
|
View Code Duplication |
if (!empty($result) && is_array($result)) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
foreach ($result as $key => $arr) |
81
|
|
|
{ |
82
|
|
|
$result[$key] = array_combine($fields, $arr); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $result; |
86
|
|
|
} |
87
|
|
|
else |
88
|
|
|
return FALSE; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function delete($table, $index, Array $condition, $operator) |
92
|
|
|
{ |
93
|
|
|
$hs = $this->getWriteSocket(); |
94
|
|
|
|
95
|
|
|
list($database, $table) = $this->getTableDatabase($table); |
96
|
|
|
|
97
|
|
|
if ($index == HSAL::INDEX_PRIMARY) $index = 'PRIMARY'; |
98
|
|
|
|
99
|
|
|
$idx = $hs->openIndex($database, $table, [], ['index' => $index]); |
100
|
|
|
|
101
|
|
|
$result = $idx->remove([$operator => $condition]); |
102
|
|
|
|
103
|
|
|
return (bool)$result; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function insert($table, Array $values) |
107
|
|
|
{ |
108
|
|
|
$hs = $this->getWriteSocket(); |
109
|
|
|
|
110
|
|
|
list($database, $table) = $this->getTableDatabase($table); |
111
|
|
|
|
112
|
|
|
$idx = $hs->openIndex($database, $table, array_keys($values)); |
113
|
|
|
|
114
|
|
|
$result = $idx->insert(array_values($values)); |
115
|
|
|
|
116
|
|
|
return (bool)$result; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function update($table, Array $values, $index, Array $condition, $operator) |
120
|
|
|
{ |
121
|
|
|
$hs = $this->getWriteSocket(); |
122
|
|
|
|
123
|
|
|
list($database, $table) = $this->getTableDatabase($table); |
124
|
|
|
|
125
|
|
|
if ($index == HSAL::INDEX_PRIMARY) $index = 'PRIMARY'; |
126
|
|
|
|
127
|
|
|
$idx = $hs->openIndex($database, $table, array_keys($values), ['index' => $index]); |
128
|
|
|
|
129
|
|
|
$result = $idx->update([$operator => $condition], array_values($values)); |
130
|
|
|
|
131
|
|
|
return (bool)$result; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
View Code Duplication |
public function increment($table, $field, $index, Array $condition, $operator, $increment) |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$hs = $this->getWriteSocket(); |
137
|
|
|
|
138
|
|
|
list($database, $table) = $this->getTableDatabase($table); |
139
|
|
|
|
140
|
|
|
if ($index == HSAL::INDEX_PRIMARY) $index = 'PRIMARY'; |
141
|
|
|
|
142
|
|
|
$idx = $hs->openIndex($database, $table, [$field], ['index' => $index]); |
143
|
|
|
|
144
|
|
|
$result = $idx->update([$operator => $condition], ['+' => $increment]); |
145
|
|
|
|
146
|
|
|
return (bool)$result; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
View Code Duplication |
public function decrement($table, $field, $index, Array $condition, $operator, $decrement) |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
|
152
|
|
|
$hs = $this->getWriteSocket(); |
153
|
|
|
|
154
|
|
|
list($database, $table) = $this->getTableDatabase($table); |
155
|
|
|
|
156
|
|
|
if ($index == HSAL::INDEX_PRIMARY) $index = 'PRIMARY'; |
157
|
|
|
|
158
|
|
|
$idx = $hs->openIndex($database, $table, [$field], ['index' => $index]); |
159
|
|
|
|
160
|
|
|
$result = $idx->update([$operator => $condition], ['-' => $decrement]); |
161
|
|
|
|
162
|
|
|
return (bool)$result; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
} |
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.