-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathkvstorage-mysqli.inc
More file actions
84 lines (74 loc) · 2.16 KB
/
kvstorage-mysqli.inc
File metadata and controls
84 lines (74 loc) · 2.16 KB
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
<?php
/**
* mod-mysqli
* Php 5.0+
*/
class KVStorageMysqli
{
public $available = false;
private $conn=null;
private $tableName='storage';
public $engineName='MyISAM';
public function __construct(){
$this->available=true;
if (!function_exists('mysqli_query'))
$this->available=false;
else {
$this->open();
}
$this->create();
}
public function open(){
if (!$this->available) return false;
if ($this->conn===null){
include_once('mysqli.inc');
$this->conn=get_mysqli_conn();
if (!$this->conn) { $this->available=false; return false;}
return true;
}
}
public function close(){
if (!$this->available) return false;
mysqli_close($this->conn);
$this->conn=null;
return true;
}
public function version(){
if (!$this->available) return '-.-.-';
$v=mysqli_get_server_info($this->conn);
if($v) return $v;
}
public function set($key, $value, $timeout=60){
if (!$this->available) return 0;
if ($this->get($key))
$res = mysqli_query($this->conn, "UPDATE {$this->tableName} SET v='$value' WHERE k=$key;");
else
$res = mysqli_query($this->conn, "INSERT INTO {$this->tableName} (k, v) VALUES ($key, '$value');");
return mysqli_affected_rows($this->conn);
}
public function get($key, $default=null){
if (!$this->available) return false;
$res = mysqli_query($this->conn, "SELECT * FROM {$this->tableName} WHERE k=$key");
$row=null;
if ($res) $row = mysqli_fetch_assoc($res);
if ($row) {
mysqli_free_result($res);
return $row['v'];
}
return false;
}
public function del($key){
if (!$this->available) return 0;
mysqli_query($this->conn, "DELETE FROM {$this->tableName} WHERE k=$key;");
return mysqli_affected_rows($this->conn);
}
public function create(){
if (!$this->available) return 0;
mysqli_real_query($this->conn, "CREATE TABLE IF NOT EXISTS `{$this->tableName}`(k INTEGER, v CHAR(20)) engine {$this->engineName};");
mysqli_real_query($this->conn, " CREATE INDEX IF NOT EXISTS skey ON {$this->tableName}(k)");
}
public function drop(){
if (!$this->available) return 0;
return mysqli_real_query($this->conn, "DROP TABLE IF EXISTS `{$this->tableName}`;");
}
}