-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparameter-status-example.pony
More file actions
77 lines (65 loc) · 2.35 KB
/
parameter-status-example.pony
File metadata and controls
77 lines (65 loc) · 2.35 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
use "cli"
use "collections"
use lori = "lori"
// in your code this `use` statement would be:
// use "postgres"
use "../../postgres"
actor Main
new create(env: Env) =>
let server_info = ServerInfo(env.vars)
let auth = lori.TCPConnectAuth(env.root)
let client = Client(auth, server_info, env.out)
// This example demonstrates ParameterStatus tracking. It prints server
// parameters as they arrive during startup, then executes a SET command
// to change application_name and prints the updated value.
actor Client is (SessionStatusNotify & ResultReceiver)
let _session: Session
let _out: OutStream
new create(auth: lori.TCPConnectAuth, info: ServerInfo, out: OutStream) =>
_out = out
_session = Session(
ServerConnectInfo(auth, info.host, info.port),
DatabaseConnectInfo(info.username, info.password, info.database),
this)
be close() =>
_session.close()
be pg_session_authenticated(session: Session) =>
_out.print("Authenticated.")
_out.print("Setting application_name...")
session.execute(
SimpleQuery("SET application_name = 'pony_example'"), this)
be pg_session_authentication_failed(
s: Session,
reason: AuthenticationFailureReason)
=>
_out.print("Failed to authenticate.")
be pg_parameter_status(session: Session, status: ParameterStatus) =>
_out.print("Parameter: " + status.name + " = " + status.value)
be pg_query_result(session: Session, result: Result) =>
_out.print("SET completed.")
_out.print("Done.")
close()
be pg_query_failed(session: Session, query: Query,
failure: (ErrorResponseMessage | ClientQueryError))
=>
match failure
| let e: ErrorResponseMessage =>
_out.print("Query failed: [" + e.severity + "] " + e.code + ": "
+ e.message)
| let e: ClientQueryError =>
_out.print("Query failed: client error")
end
close()
class val ServerInfo
let host: String
let port: String
let username: String
let password: String
let database: String
new val create(vars: (Array[String] val | None)) =>
let e = EnvVars(vars)
host = try e("POSTGRES_HOST")? else "127.0.0.1" end
port = try e("POSTGRES_PORT")? else "5432" end
username = try e("POSTGRES_USERNAME")? else "postgres" end
password = try e("POSTGRES_PASSWORD")? else "postgres" end
database = try e("POSTGRES_DATABASE")? else "postgres" end