-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtransaction-status-example.pony
More file actions
88 lines (75 loc) · 2.62 KB
/
transaction-status-example.pony
File metadata and controls
88 lines (75 loc) · 2.62 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
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 the pg_transaction_status callback. It sends
// BEGIN and COMMIT to show the status changing from idle to in-transaction
// and back.
actor Client is (SessionStatusNotify & ResultReceiver)
let _session: Session
let _out: OutStream
var _phase: USize = 0
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.")
session.execute(SimpleQuery("BEGIN"), this)
be pg_session_authentication_failed(
s: Session,
reason: AuthenticationFailureReason)
=>
_out.print("Failed to authenticate.")
be pg_transaction_status(session: Session, status: TransactionStatus) =>
match \exhaustive\ status
| TransactionIdle => _out.print("Transaction status: idle")
| TransactionInBlock => _out.print("Transaction status: in transaction")
| TransactionFailed => _out.print("Transaction status: failed")
end
be pg_query_result(session: Session, result: Result) =>
_phase = _phase + 1
match \exhaustive\ _phase
| 1 =>
// BEGIN done. Commit to return to idle.
_session.execute(SimpleQuery("COMMIT"), this)
| 2 =>
// COMMIT done.
_out.print("Done.")
close()
end
be pg_query_failed(session: Session, query: Query,
failure: (ErrorResponseMessage | ClientQueryError))
=>
match \exhaustive\ 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