-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.php
195 lines (159 loc) · 4.13 KB
/
proxy.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php namespace phpsql;
class proxy_storage
{
protected $connector;
public function __construct( $connector )
{
$this->connector = $connector;
}
}
class proxy extends proxy_storage
{
private $transactions = [];
private $next_transaction_id = 1;
private $last_insert_id = "NOT_AN_ID";
public function OpenConnection( $user, $pass, $ip, $port, $db, $options )
{
return $this->connector->OpenConnection($user, $pass, $ip, $port, $db, $options);
}
public function AffectedID()
{
$affected_id = $this->connector->affected_id;
if ($affected_id === null)
throw new \Exception("Unable to determine affected ID. This DB support it?");
if ($affected_id === false)
throw new \Exception("Unable to determine affected ID. Maybe you updating multiply rows?");
return $affected_id;
}
public function Query( $query, $params = [], $one_row = false, $reindex_by = null )
{
assert(is_array($params), "phpsql->Query params should be array");
foreach ($params as &$param)
if ($param instanceof \phpa2o\phpa2o)
$param = $param->__2array();
$res = $this->connector->Query($query, $params);
if (!is_array($res))
return $res;
$ret = [];
if (!is_null($reindex_by))
{
if (!$one_row)
foreach ($res as $row)
$ret[$row[$reindex_by]] = $row;
else
foreach ($res as $row)
$ret[] = $row[$reindex_by];
return $ret;
}
else
$ret = $res;
if ($one_row && count($ret) == 1)
return $ret[0];
return $ret;
}
public function Begin()
{
$id = $this->next_transaction_id++;
$this->transactions[] = $id;
if (!$this->InTransaction())
$this->connector->Begin();
else
$this->connector->SaveStep($id);
return new transaction_object($this, $id);
}
public function Rollback( $id )
{
$this->DieInWrongTransactionExitOrder($id);
if ($this->IsHeadTransaction($id))
$res = $this->connector->Rollback();
else
{
$res = $this->connector->StepBack($id);
$this->connector->ForgetStep($id);
}
array_pop($this->transactions);
return is_null($res) ? false : $res;
}
public function Commit( $id )
{
$this->DieInWrongTransactionExitOrder($id);
if ($this->IsHeadTransaction($id))
$res = $this->connector->Commit();
else
$res = $this->connector->ForgetStep($id);
array_pop($this->transactions);
return is_null($res) ? true : $res;
}
private function DieInWrongTransactionExitOrder( $id )
{
$cur_transaction = end($this->transactions);
if ($cur_transaction != $id)
die("Could not exit from transaction {$id}, waiting to finish {$cur_transaction}");
}
public function InTransaction()
{
return $this->connector->InTransaction();
}
private function IsHeadTransaction( $id )
{
if (!count($this->transactions))
return false;
return $this->transactions[0] == $id;
}
public function RawConnection()
{
return $this->connector->RawConnection();
}
// Dig into transaction if required
public function ConditionalQuery( $check, $then, $else )
{
if ($check())
return $then();
$tran = $this->Begin();
if ($check()) // Appeared while we getting lock
return $tran->Rollback();
$ret = $else();
$tran->Commit();
return $ret;
// Execution example:
$this->ConditionalQuery
(
function()
{
return db::Query("SELECT count(*) FROM table WHERE id=1")['count'];
},
function ()
{
return db::Query("UPDATE table SET acc=acc+1 WHERE id=1");
},
function ()
{
return db::Query("INSERT INTO table(id) VALUES (1)");
}
);
}
}
class transaction_object
{
private $proxy;
private $id;
public function __construct( $proxy, $id )
{
$this->proxy = $proxy;
$this->id = $id;
}
public function Commit()
{
return $this->proxy->Commit($this->id);
}
public function Rollback()
{
return $this->proxy->Rollback($this->id);
}
public function Finish( $status )
{
if ($status)
return $this->Commit();
return $this->Rollback();
}
}