-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathkvstorage-pgsql.inc
More file actions
88 lines (78 loc) · 2.11 KB
/
kvstorage-pgsql.inc
File metadata and controls
88 lines (78 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
84
85
86
87
88
<?php
/**
* mod-pgsql
* Php 5.0+
*/
class KVStoragePgsql
{
public $available = false;
private $conn=null;
private $tableName='storage';
public function __construct(){
$this->available=true;
if (!function_exists('pg_query'))
$this->available=false;
else {
$this->open();
}
$this->create();
}
public function open(){
if (!$this->available) return false;
if ($this->conn===null){
include_once('pgsql.inc');
$this->conn=get_pgsql_conn();
return true;
}
}
public function close(){
if (!$this->available) return false;
pg_close($this->conn);
$this->conn=null;
return true;
}
public function version(){
if (!$this->available) return '-.-.-';
if (PHP_VERSION<'5.0') return '-.-.-';
$v=pg_version($this->conn);
if($v) return $v['server'];
}
public function set($key, $value, $timeout=60){
if (!$this->available) return 0;
if ($this->get($key))
$res = pg_query($this->conn, "UPDATE {$this->tableName} SET v='$value' WHERE k=$key;");
else
$res = pg_query($this->conn, "INSERT INTO {$this->tableName} (k, v) VALUES ($key, '$value');");
$cnt = pg_affected_rows($res);
pg_free_result($res);
return $cnt;
}
public function get($key, $default=null){
if (!$this->available) return false;
$res = pg_query($this->conn, "SELECT * FROM {$this->tableName} WHERE k=$key");
$row=null;
if ($res) $row = pg_fetch_assoc($res);
if ($row) {
pg_free_result($res);
return $row['v'];
}
return false;
}
public function del($key){
if (!$this->available) return 0;
$res = pg_query($this->conn, "DELETE FROM {$this->tableName} WHERE k=$key;");
$cnt = pg_affected_rows($res);
pg_free_result($res);
return $cnt;
}
public function create(){
if (!$this->available) return 0;
pg_query($this->conn, "CREATE TABLE IF NOT EXISTS {$this->tableName}(k INTEGER, v CHAR(20));");
pg_query($this->conn, " CREATE INDEX IF NOT EXISTS skey ON {$this->tableName}(k)");
}
public function drop(){
if (!$this->available) return 0;
return pg_query($this->conn, "DROP TABLE IF EXISTS {$this->tableName};");
}
}
//$kvstorage=new KVStorageSqlite3();