-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathkvstorage-mysql.inc
More file actions
83 lines (73 loc) · 2.11 KB
/
kvstorage-mysql.inc
File metadata and controls
83 lines (73 loc) · 2.11 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
<?php
/**
* mod-mysql
* Php 4.0+
*/
class KVStorageMysql
{
public $available = false;
private $conn=null;
private $tableName='storage';
public $engineName='MyISAM';
public function __construct(){
$this->available=true;
if (!function_exists('mysql_query'))
$this->available=false;
else {
$this->open();
}
$this->create();
}
public function open(){
if (!$this->available) return false;
if ($this->conn===null){
include_once('mysql.inc');
$this->conn=get_mysql_conn();
return true;
}
}
public function close(){
if (!$this->available) return false;
mysql_close($this->conn);
$this->conn=null;
return true;
}
public function version(){
if (!$this->available) return '-.-.-';
$v=mysql_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))
mysql_unbuffered_query("UPDATE {$this->tableName} SET val='$value' WHERE key=$key;", $this->conn);
else
mysql_unbuffered_query("INSERT INTO {$this->tableName} (key, val) VALUES ($key, '$value');", $this->conn);
return mysql_affected_rows($this->conn);
}
public function get($key, $default=null){
if (!$this->available) return false;
$res = mysql_query("SELECT * FROM {$this->tableName} WHERE key=$key", $this->conn);
$row=null;
if ($res) $row = mysql_fetch_assoc($res);
if ($row) {
mysql_free_result($res);
return $row['val'];
}
return false;
}
public function del($key){
if (!$this->available) return 0;
mysql_unbuffered_query("DELETE FROM {$this->tableName} WHERE key=$key;", $this->conn);
return mysql_affected_rows($this->conn);
}
public function create(){
if (!$this->available) return 0;
return mysql_unbuffered_query("CREATE TABLE `{$this->tableName}`(key INTEGER, val CHAR(20)) engine {$this->engineName}; CREATE INDEX skey ON {$this->tableName}(key)", $this->conn);
}
public function drop(){
if (!$this->available) return 0;
return mysql_unbuffered_query("DROP TABLE `{$this->tableName}`;", $this->conn);
}
}
//$kvstorage=new KVStorageSqlite3();