-
Notifications
You must be signed in to change notification settings - Fork 232
/
ExampleWithPostgreSQL.php
100 lines (87 loc) · 2.21 KB
/
ExampleWithPostgreSQL.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
<?php
class ExampleWithPostgreSQL extends AppInstance {
/**
* @method beginRequest
* @description Creates Request.
* @param object Request.
* @param object Upstream application instance.
* @return object Request.
*/
public function beginRequest($req, $upstream) {
return new ExampleWithPostgreSQLRequest($this, $upstream, $req);
}
}
class ExampleWithPostgreSQLRequest extends Request {
public $stime;
public $queryResult;
public $sql;
public $runstate = 0;
/**
* @method init
* @description Constructor.
* @return void
*/
public function init() {
$this->stime = microtime(TRUE);
$sqlclient = Daemon::$appResolver->getInstanceByAppName('PostgreSQLClient');
if (
$sqlclient
&& ($this->sql = $sqlclient->getConnection())
) {
$this->sql->context = $this;
$this->sql->onConnected(
function($sql, $success) {
if (!$success) {
return;
}
$sql->query(
'SELECT 123 as integer, NULL as nul, \'test\' as string',
function($sql, $success) {
$sql->context->queryResult = $sql->resultRows; // save the result
$sql->context->wakeup(); // wake up the request immediately
}
);
}
);
}
}
/**
* @method run
* @description Called when request iterated.
* @return integer Status.
*/
public function run() {
if (
!$this->queryResult
&& ($this->runstate++ === 0)
) {
// sleep for 5 seconds or until wakeup
$this->sleep(5);
}
try {
$this->header('Content-Type: text/html; charset=utf-8');
} catch (Exception $e) {}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example with PostgreSQL</title>
</head>
<body>
<?php
if ($this->queryResult) {
echo '<h1>It works! Be happy! ;-)</h1>Result of `SELECT 123 as integer, NULL as nul, \'test\' as string`: <pre>';
var_dump($this->queryResult);
echo '</pre>';
} else {
echo '<h1>Something went wrong! We have no result.</h1>';
}
echo '<br />Request (http) took: ' . round(microtime(TRUE) - $this->stime, 6);
?>
</body>
</html>
<?php
return Request::DONE;
}
}