1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
// Copyright (c) Dmitry Kosenkov, All rights reserved. |
4
|
|
|
|
5
|
|
|
namespace HSAL; |
6
|
|
|
|
7
|
|
|
class HSAL |
8
|
|
|
{ |
9
|
|
|
public $driver; |
10
|
|
|
|
11
|
|
|
private $hs; |
12
|
|
|
|
13
|
|
|
const DRIVER_AUTO = 1; |
14
|
|
|
const DRIVER_HANDLERSOCKETI = 2; |
15
|
|
|
const DRIVER_HSPHP = 3; |
16
|
|
|
|
17
|
|
|
const OPERATOR_EQUAL = '='; |
18
|
|
|
const OPERATOR_LESS = '<'; |
19
|
|
|
const OPERATOR_LESS_EQUAL = '<='; |
20
|
|
|
const OPERATOR_GREATER = '>'; |
21
|
|
|
const OPERATOR_GREATER_EQUAL = '>='; |
22
|
|
|
|
23
|
|
|
const INDEX_PRIMARY = ''; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
public function __construct($host, $database, Array $options = []) |
27
|
|
|
{ |
28
|
|
|
if (!extension_loaded('handlersocketi') && (!class_exists('\\HSPHP\\ReadSocket'))) |
29
|
|
|
{ |
30
|
|
|
throw new \Exception('Error: cannot detect HandlerSocket library. please, install handlersocketi module or HSPHP library'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$driver = self::DRIVER_AUTO; |
34
|
|
|
|
35
|
|
|
if (isset($options['driver'])) |
36
|
|
|
$driver = $options['driver']; |
37
|
|
|
|
38
|
|
|
if ($driver == self::DRIVER_AUTO) |
39
|
|
|
{ |
40
|
|
|
if (extension_loaded('handlersocketi')) |
41
|
|
|
{ |
42
|
|
|
$driver = self::DRIVER_HANDLERSOCKETI; |
43
|
|
|
} |
44
|
|
|
else if (class_exists('\\HSPHP\\ReadSocket')) |
45
|
|
|
{ |
46
|
|
|
$driver = self::DRIVER_HSPHP; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($driver == self::DRIVER_HANDLERSOCKETI) |
51
|
|
|
{ |
52
|
|
|
$this->hs = new \HSAL\Driver\Handlersocketi($host, $database, $options); |
53
|
|
|
} |
54
|
|
|
else if ($driver == self::DRIVER_HSPHP) |
55
|
|
|
{ |
56
|
|
|
$this->hs = new \HSAL\Driver\HSPHP($host, $database, $options); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
$this->driver = $driver; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
public function fetchArray($table, Array $fields, Array $index_condition, $operator = HSAL::OPERATOR_EQUAL) |
65
|
|
|
{ |
66
|
|
|
list($index, $condition) = self::parse_index_condition($index_condition); |
67
|
|
|
|
68
|
|
|
return $this->hs->fetchArray($table, $fields, $index, $condition, $operator); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function fetchAssoc($table, Array $fields, Array $index_condition, $operator = HSAL::OPERATOR_EQUAL) |
72
|
|
|
{ |
73
|
|
|
list($index, $condition) = self::parse_index_condition($index_condition); |
74
|
|
|
|
75
|
|
|
return $this->hs->fetchAssoc($table, $fields, $index, $condition, $operator); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function fetchColumn($table, $field, Array $index_condition, $operator = HSAL::OPERATOR_EQUAL) |
79
|
|
|
{ |
80
|
|
|
list($index, $condition) = self::parse_index_condition($index_condition); |
81
|
|
|
|
82
|
|
|
return $this->hs->fetchColumn($table, $field, $index, $condition, $operator); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function fetchAll($table, Array $fields, Array $index_condition, $operator = HSAL::OPERATOR_EQUAL, $limit = 1000, $offset = 0) |
86
|
|
|
{ |
87
|
|
|
list($index, $condition) = self::parse_index_condition($index_condition); |
88
|
|
|
|
89
|
|
|
return $this->hs->fetchAll($table, $fields, $index, $condition, $operator, $limit, $offset); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function delete($table, Array $index_condition, $operator = HSAL::OPERATOR_EQUAL) |
93
|
|
|
{ |
94
|
|
|
list($index, $condition) = self::parse_index_condition($index_condition); |
95
|
|
|
|
96
|
|
|
return $this->hs->delete($table, $index, $condition, $operator); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function insert($table, Array $values) |
100
|
|
|
{ |
101
|
|
|
return $this->hs->insert($table, $values); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function update($table, Array $values, Array $index_condition, $operator = HSAL::OPERATOR_EQUAL) |
105
|
|
|
{ |
106
|
|
|
list($index, $condition) = self::parse_index_condition($index_condition); |
107
|
|
|
|
108
|
|
|
return $this->hs->update($table, $values, $index, $condition, $operator); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function increment($table, $field, Array $index_condition, $operator = HSAL::OPERATOR_EQUAL, $increment = 1) |
112
|
|
|
{ |
113
|
|
|
list($index, $condition) = self::parse_index_condition($index_condition); |
114
|
|
|
|
115
|
|
|
return $this->hs->increment($table, $field, $index, $condition, $operator, $increment); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function decrement($table, $field, Array $index_condition, $operator = HSAL::OPERATOR_EQUAL, $decrement = 1) |
119
|
|
|
{ |
120
|
|
|
list($index, $condition) = self::parse_index_condition($index_condition); |
121
|
|
|
|
122
|
|
|
return $this->hs->decrement($table, $field, $index, $condition, $operator, $decrement); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
private static function parse_index_condition($index_condition) |
127
|
|
|
{ |
128
|
|
|
$index = array_keys($index_condition)[0]; |
129
|
|
|
$condition = is_array($index_condition[$index]) ? $index_condition[$index] : [$index_condition[$index]]; |
130
|
|
|
|
131
|
|
|
return [$index, $condition]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |