|
| 1 | +use "cli" |
| 2 | +use "collections" |
| 3 | +use lori = "lori" |
| 4 | +// in your code this `use` statement would be: |
| 5 | +// use "postgres" |
| 6 | +use "../../postgres" |
| 7 | + |
| 8 | +actor Main |
| 9 | + new create(env: Env) => |
| 10 | + let server_info = ServerInfo(env.vars) |
| 11 | + let auth = lori.TCPConnectAuth(env.root) |
| 12 | + |
| 13 | + let client = Client(auth, server_info, env.out) |
| 14 | + |
| 15 | +// This example demonstrates COPY OUT for bulk data export. It creates a table, |
| 16 | +// inserts three rows, uses COPY TO STDOUT to export them, prints the received |
| 17 | +// data, verifies the row count, then drops the table. |
| 18 | +// |
| 19 | +// The COPY OUT protocol is server-driven: after the session sends the COPY |
| 20 | +// query, the server pushes data via pg_copy_data callbacks. When all data is |
| 21 | +// sent, pg_copy_complete fires with the row count. |
| 22 | +actor Client is (SessionStatusNotify & ResultReceiver & CopyOutReceiver) |
| 23 | + let _session: Session |
| 24 | + let _out: OutStream |
| 25 | + var _phase: USize = 0 |
| 26 | + var _copy_data: Array[U8] iso = recover iso Array[U8] end |
| 27 | + |
| 28 | + new create(auth: lori.TCPConnectAuth, info: ServerInfo, out: OutStream) => |
| 29 | + _out = out |
| 30 | + _session = Session( |
| 31 | + ServerConnectInfo(auth, info.host, info.port), |
| 32 | + DatabaseConnectInfo(info.username, info.password, info.database), |
| 33 | + this) |
| 34 | + |
| 35 | + be close() => |
| 36 | + _session.close() |
| 37 | + |
| 38 | + be pg_session_authenticated(session: Session) => |
| 39 | + _out.print("Authenticated.") |
| 40 | + _phase = 0 |
| 41 | + session.execute( |
| 42 | + SimpleQuery("DROP TABLE IF EXISTS copy_out_example"), this) |
| 43 | + |
| 44 | + be pg_session_authentication_failed( |
| 45 | + s: Session, |
| 46 | + reason: AuthenticationFailureReason) |
| 47 | + => |
| 48 | + _out.print("Failed to authenticate.") |
| 49 | + |
| 50 | + be pg_copy_data(session: Session, data: Array[U8] val) => |
| 51 | + _copy_data.append(data) |
| 52 | + |
| 53 | + be pg_copy_complete(session: Session, count: USize) => |
| 54 | + let received: String val = String.from_iso_array( |
| 55 | + _copy_data = recover iso Array[U8] end) |
| 56 | + _out.print("COPY complete: " + count.string() + " rows exported.") |
| 57 | + _out.print("Received data:") |
| 58 | + _out.print(received) |
| 59 | + // Drop the table |
| 60 | + _out.print("Dropping table...") |
| 61 | + _session.execute( |
| 62 | + SimpleQuery("DROP TABLE copy_out_example"), this) |
| 63 | + |
| 64 | + be pg_copy_failed(session: Session, |
| 65 | + failure: (ErrorResponseMessage | ClientQueryError)) |
| 66 | + => |
| 67 | + match failure |
| 68 | + | let e: ErrorResponseMessage => |
| 69 | + _out.print("COPY failed: [" + e.severity + "] " + e.code + ": " |
| 70 | + + e.message) |
| 71 | + | let e: ClientQueryError => |
| 72 | + _out.print("COPY failed: client error") |
| 73 | + end |
| 74 | + close() |
| 75 | + |
| 76 | + be pg_query_result(session: Session, result: Result) => |
| 77 | + _phase = _phase + 1 |
| 78 | + |
| 79 | + match _phase |
| 80 | + | 1 => |
| 81 | + // Table dropped (or didn't exist). Create it. |
| 82 | + _out.print("Creating table...") |
| 83 | + _session.execute( |
| 84 | + SimpleQuery( |
| 85 | + """ |
| 86 | + CREATE TABLE copy_out_example ( |
| 87 | + name VARCHAR(50) NOT NULL, |
| 88 | + value INT NOT NULL |
| 89 | + ) |
| 90 | + """), |
| 91 | + this) |
| 92 | + | 2 => |
| 93 | + // Table created. Insert rows. |
| 94 | + _out.print("Inserting rows...") |
| 95 | + _session.execute( |
| 96 | + SimpleQuery( |
| 97 | + "INSERT INTO copy_out_example VALUES " + |
| 98 | + "('alice', 10), ('bob', 20), ('charlie', 30)"), |
| 99 | + this) |
| 100 | + | 3 => |
| 101 | + // Rows inserted. Start COPY OUT. |
| 102 | + _out.print("Starting COPY OUT...") |
| 103 | + _session.copy_out( |
| 104 | + "COPY copy_out_example TO STDOUT", this) |
| 105 | + | 4 => |
| 106 | + // Table dropped. Done. |
| 107 | + _out.print("Done.") |
| 108 | + close() |
| 109 | + end |
| 110 | + |
| 111 | + be pg_query_failed(session: Session, query: Query, |
| 112 | + failure: (ErrorResponseMessage | ClientQueryError)) |
| 113 | + => |
| 114 | + match failure |
| 115 | + | let e: ErrorResponseMessage => |
| 116 | + _out.print("Query failed: [" + e.severity + "] " + e.code + ": " |
| 117 | + + e.message) |
| 118 | + | let e: ClientQueryError => |
| 119 | + _out.print("Query failed: client error") |
| 120 | + end |
| 121 | + close() |
| 122 | + |
| 123 | +class val ServerInfo |
| 124 | + let host: String |
| 125 | + let port: String |
| 126 | + let username: String |
| 127 | + let password: String |
| 128 | + let database: String |
| 129 | + |
| 130 | + new val create(vars: (Array[String] val | None)) => |
| 131 | + let e = EnvVars(vars) |
| 132 | + host = try e("POSTGRES_HOST")? else "127.0.0.1" end |
| 133 | + port = try e("POSTGRES_PORT")? else "5432" end |
| 134 | + username = try e("POSTGRES_USERNAME")? else "postgres" end |
| 135 | + password = try e("POSTGRES_PASSWORD")? else "postgres" end |
| 136 | + database = try e("POSTGRES_DATABASE")? else "postgres" end |
0 commit comments