-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlersocket_wrapper.php
110 lines (94 loc) · 3.28 KB
/
handlersocket_wrapper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
class HandlerScoketWrapper {
protected $hs = NULL;
protected $index = 1;
protected $db_name;
protected $db_table;
protected $db_host;
protected $db_port;
protected $db_secret;
protected $debug;
protected $primary_key = NULL;
protected $columns = array();
function __construct($db_host, $db_port, $db_name, $db_table, $debug = FALSE, $db_secret = NULL)
{
$status = TRUE;
$this->db_host = $db_host;
$this->db_port = $db_port;
$this->db_name = $db_name;
$this->db_table = $db_table;
$this->db_secret = (isset($db_secret)) ? $db_secret : NULL;
$this->debug = (isset($debug)) ? $debug : FALSE;
try {
$this->hs = new HandlerSocket($this->db_host, $this->db_port);
} catch (HandlerSocketException $e) {
$this->error_log(__METHOD__, __LINE__, $e->getMessage());
return null;
}
if (is_null($this->db_secret)) {
$status = $this->hs->auth($this->db_secret);
if ($status == FALSE) {
$this->error_log(__METHOD__, __LINE__, $this->hs->getError());
return null;
}
}
}
function init($array = array(), $index = 1, $primary_key = NULL)
{
$this->index = $index;
$this->columns = implode(',', $array);
if (!isset($primary_key)) {
$this->primary_key = HandlerSocket::PRIMARY;
} else {
$this->primary_key = $primary_key;
}
$status = $this->hs->openIndex($this->index, $this->db_name, $this->db_table, $this->primary_key, $this->columns);
if ($status == FALSE) {
$this->error_log(__METHOD__, __LINE__, $this->hs->getError());
}
return $status;
}
function get($key, $op = '=')
{
if (!is_array($key)) {
return $this->hs->executeSingle(1, $op, array($key), 1, 0);
}
$array = array();
foreach ($key as $row) {
$query = array($this->index, $op, array($row), 1, 0);
array_push($array, $query);
}
return $this->hs->executeMulti($array);
}
function add($array = array())
{
$status = $this->hs->executeInsert($this->index, $array);
if ($status == FALSE) {
$this->error_log(__METHOD__, __LINE__, $this->hs->getError());
}
return $status;
}
function update($key, $value = array(), $op = '=')
{
$update_array = array_merge(array($key), $value);
$status = $this->hs->executeUpdate($this->index, $op, array('104'), $update_array, 1, 0);
if ($status == FALSE) {
$this->error_log(__METHOD__, __LINE__, $this->hs->getError());
}
return $status;
}
function del($key, $op = '=')
{
$status = $this->hs->executeDelete($this->index, $op, array($key));
if ($status == FALSE) {
$this->error_log(__METHOD__, __LINE__, $this->hs->getError());
}
return $status;
}
private function error_log($method, $line, $log)
{
if ($this->debug) {
echo "[ERROR]\n$method\nLine: $line\nMsg: $log\n";
}
}
}