-
Notifications
You must be signed in to change notification settings - Fork 1
/
db-api.php
125 lines (107 loc) · 3.42 KB
/
db-api.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
class DbAPI
{
private static $conn;
public static function init($dbPath)
{
$needCreate = !file_exists($dbPath);
self::$conn = new PDO('sqlite:'.$dbPath);
if($needCreate) {
self::_create();
}
}
private static function _create()
{
self::exec('CREATE TABLE nodes (id INTEGER PRIMARY KEY, ip_address TEXT, need_sync NUMERIC)');
self::exec('CREATE TABLE nodes_ports (id INTEGER PRIMARY KEY AUTOINCREMENT, node_id NUMERIC, port_from NUMERIC, port_to NUMERIC, proto TEXT)');
}
public static function hasNode($id)
{
$res = self::fetch(
sprintf("SELECT COUNT(*) as cnt FROM nodes WHERE id = %s", (int)$id)
);
return (int)$res['cnt'] > 0;
}
public static function needSync()
{
$res = self::fetch("SELECT COUNT(*) as cnt FROM nodes WHERE need_sync = 1");
return (int)$res['cnt'] > 0;
}
public static function successSync()
{
self::exec("UPDATE nodes SET need_sync = 0");
}
public static function addNode($nodeArr)
{
self::exec(
sprintf("INSERT INTO nodes (id, ip_address, need_sync) VALUES(%s, '%s', 1)", (int)$nodeArr['identity'], $nodeArr['ip_address'])
);
}
public static function forwardNodePort($nodeId, $from, $to, $proto = 'tcp')
{
$res = self::exec(
sprintf("INSERT INTO nodes_ports (node_id, port_from, port_to, proto) VALUES(%s, %s, %s, '%s')",
(int)$nodeId, $from, $to, $proto)
);
if(!$res) {
echo "\nPDO::errorInfo():\n";
print_r(self::$conn->errorInfo());
exit;
}
self::exec(
sprintf("UPDATE nodes SET need_sync = 1 WHERE id = %s", (int)$nodeId)
);
$id = self::$conn->lastInsertId();
//echo $id;exit;
return self::fetch(
sprintf("SELECT * FROM nodes_ports WHERE id = %s", (int)$id)
);
}
public static function getPorts($nodeId = null)
{
return self::fetchAll("SELECT np.*,n.ip_address FROM nodes_ports np INNER JOIN nodes n ON n.id = np.node_id ".
($nodeId ? 'WHERE n.id = '.$nodeId : '').
" ORDER BY np.node_id");
}
public static function updatePortsFwd($id, $from, $to, $proto = 'tcp')
{
self::exec(
sprintf("UPDATE nodes_ports SET port_from = %s, port_to = %s, proto = '%s' WHERE id = %s", $from, $to, $proto, (int)$id)
);
self::exec(
sprintf("UPDATE nodes SET need_sync = 1 WHERE id = (SELECT node_id FROM nodes_ports WHERE id=%s)", (int)$id)
);
}
public static function deletePortsFwd($id)
{
self::exec(
sprintf("UPDATE nodes SET need_sync = 1 WHERE id = (SELECT node_id FROM nodes_ports WHERE id=%s)", (int)$id)
);
self::exec(
sprintf("DELETE FROM nodes_ports WHERE id = %s", (int)$id)
);
}
public static function getNodes()
{
return self::fetchAll("SELECT * FROM nodes");
}
public static function exec($q)
{
return self::$conn->exec($q);
}
public static function fetchAll($q)
{
//echo $q."\n";
$sth = self::$conn->prepare($q);
$sth->setFetchMode(PDO::FETCH_ASSOC);
$sth->execute();
return $sth->fetchAll();
}
public static function fetch($q)
{
$sth = self::$conn->prepare($q);
$sth->setFetchMode(PDO::FETCH_ASSOC);
$sth->execute();
return $sth->fetch();
}
}