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 = NULL) |
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 (!empty($options)) |
36
|
|
|
{ |
37
|
|
|
if (isset($options['driver'])) |
38
|
|
|
$driver = $options['driver']; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if ($driver == self::DRIVER_AUTO) |
42
|
|
|
{ |
43
|
|
|
if (extension_loaded('handlersocketi')) |
44
|
|
|
{ |
45
|
|
|
$driver = self::DRIVER_HANDLERSOCKETI; |
46
|
|
|
} |
47
|
|
|
else if (class_exists('\\HSPHP\\ReadSocket')) |
48
|
|
|
{ |
49
|
|
|
$driver = self::DRIVER_HSPHP; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($driver == self::DRIVER_HANDLERSOCKETI) |
54
|
|
|
{ |
55
|
|
|
$this->hs = new \HSAL\Driver\Handlersocketi($host, $database, $options); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
else if ($driver == self::DRIVER_HSPHP) |
58
|
|
|
{ |
59
|
|
|
$this->hs = new \HSAL\Driver\HSPHP($host, $database, $options); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
$this->driver = $driver; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
public function fetchArray($table, Array $fields, Array $index_condition, $operator = HSAL::OPERATOR_EQUAL) |
68
|
|
|
{ |
69
|
|
|
list($index, $condition) = self::parse_index_condition($index_condition); |
70
|
|
|
|
71
|
|
|
return $this->hs->fetchArray($table, $fields, $index, $condition, $operator); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function fetchAssoc($table, Array $fields, Array $index_condition, $operator = HSAL::OPERATOR_EQUAL) |
75
|
|
|
{ |
76
|
|
|
list($index, $condition) = self::parse_index_condition($index_condition); |
77
|
|
|
|
78
|
|
|
return $this->hs->fetchAssoc($table, $fields, $index, $condition, $operator); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function fetchColumn($table, $field, Array $index_condition, $operator = HSAL::OPERATOR_EQUAL) |
82
|
|
|
{ |
83
|
|
|
list($index, $condition) = self::parse_index_condition($index_condition); |
84
|
|
|
|
85
|
|
|
return $this->hs->fetchColumn($table, $field, $index, $condition, $operator); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function fetchAll($table, Array $fields, Array $index_condition, $operator = HSAL::OPERATOR_EQUAL, $limit = 1000, $offset = 0) |
89
|
|
|
{ |
90
|
|
|
list($index, $condition) = self::parse_index_condition($index_condition); |
91
|
|
|
|
92
|
|
|
return $this->hs->fetchAll($table, $fields, $index, $condition, $operator, $limit, $offset); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function delete($table, Array $index_condition, $operator = HSAL::OPERATOR_EQUAL) |
96
|
|
|
{ |
97
|
|
|
list($index, $condition) = self::parse_index_condition($index_condition); |
98
|
|
|
|
99
|
|
|
return $this->hs->delete($table, $index, $condition, $operator); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function insert($table, Array $values) |
103
|
|
|
{ |
104
|
|
|
return $this->hs->insert($table, $values); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function update($table, Array $values, Array $index_condition, $operator = HSAL::OPERATOR_EQUAL) |
108
|
|
|
{ |
109
|
|
|
list($index, $condition) = self::parse_index_condition($index_condition); |
110
|
|
|
|
111
|
|
|
return $this->hs->update($table, $values, $index, $condition, $operator); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function increment($table, $field, Array $index_condition, $operator = HSAL::OPERATOR_EQUAL, $increment = 1) |
115
|
|
|
{ |
116
|
|
|
list($index, $condition) = self::parse_index_condition($index_condition); |
117
|
|
|
|
118
|
|
|
return $this->hs->increment($table, $field, $index, $condition, $operator, $increment); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function decrement($table, $field, Array $index_condition, $operator = HSAL::OPERATOR_EQUAL, $decrement = 1) |
122
|
|
|
{ |
123
|
|
|
list($index, $condition) = self::parse_index_condition($index_condition); |
124
|
|
|
|
125
|
|
|
return $this->hs->decrement($table, $field, $index, $condition, $operator, $decrement); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
private static function parse_index_condition($index_condition) |
130
|
|
|
{ |
131
|
|
|
$index = array_keys($index_condition)[0]; |
132
|
|
|
$condition = is_array($index_condition[$index]) ? $index_condition[$index] : [$index_condition[$index]]; |
133
|
|
|
|
134
|
|
|
return [$index, $condition]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.