|
| 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 IN for bulk data loading. It creates a table, |
| 16 | +// uses COPY FROM STDIN to load three rows of tab-delimited text data, verifies |
| 17 | +// the data with a SELECT query, then drops the table. |
| 18 | +// |
| 19 | +// The COPY IN protocol uses a pull-based flow: the session calls pg_copy_ready |
| 20 | +// after each send_copy_data, letting the client send the next chunk. When all |
| 21 | +// data is sent, call finish_copy to complete the operation. |
| 22 | +actor Client is (SessionStatusNotify & ResultReceiver & CopyInReceiver) |
| 23 | + let _session: Session |
| 24 | + let _out: OutStream |
| 25 | + var _phase: USize = 0 |
| 26 | + var _rows_sent: USize = 0 |
| 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_in_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_ready(session: Session) => |
| 51 | + _rows_sent = _rows_sent + 1 |
| 52 | + if _rows_sent <= 3 then |
| 53 | + // Send one row per callback. Tab-delimited, newline-terminated. |
| 54 | + let row: Array[U8] val = recover val |
| 55 | + ("row" + _rows_sent.string() + "\t" + (_rows_sent * 10).string() |
| 56 | + + "\n").array() |
| 57 | + end |
| 58 | + _out.print(" Sending row " + _rows_sent.string() + "...") |
| 59 | + session.send_copy_data(row) |
| 60 | + else |
| 61 | + _out.print(" All rows sent. Finishing COPY...") |
| 62 | + session.finish_copy() |
| 63 | + end |
| 64 | + |
| 65 | + be pg_copy_complete(session: Session, count: USize) => |
| 66 | + _out.print("COPY complete: " + count.string() + " rows copied.") |
| 67 | + // Verify with a SELECT |
| 68 | + _out.print("Verifying with SELECT...") |
| 69 | + _session.execute( |
| 70 | + SimpleQuery("SELECT name, value FROM copy_in_example ORDER BY name"), |
| 71 | + this) |
| 72 | + |
| 73 | + be pg_copy_failed(session: Session, |
| 74 | + failure: (ErrorResponseMessage | ClientQueryError)) |
| 75 | + => |
| 76 | + match failure |
| 77 | + | let e: ErrorResponseMessage => |
| 78 | + _out.print("COPY failed: [" + e.severity + "] " + e.code + ": " |
| 79 | + + e.message) |
| 80 | + | let e: ClientQueryError => |
| 81 | + _out.print("COPY failed: client error") |
| 82 | + end |
| 83 | + close() |
| 84 | + |
| 85 | + be pg_query_result(session: Session, result: Result) => |
| 86 | + _phase = _phase + 1 |
| 87 | + |
| 88 | + match _phase |
| 89 | + | 1 => |
| 90 | + // Table dropped (or didn't exist). Create it. |
| 91 | + _out.print("Creating table...") |
| 92 | + _session.execute( |
| 93 | + SimpleQuery( |
| 94 | + """ |
| 95 | + CREATE TABLE copy_in_example ( |
| 96 | + name VARCHAR(50) NOT NULL, |
| 97 | + value INT NOT NULL |
| 98 | + ) |
| 99 | + """), |
| 100 | + this) |
| 101 | + | 2 => |
| 102 | + // Table created. Start COPY IN. |
| 103 | + _out.print("Table created. Starting COPY IN...") |
| 104 | + _session.copy_in( |
| 105 | + "COPY copy_in_example (name, value) FROM STDIN", this) |
| 106 | + | 3 => |
| 107 | + // SELECT done. Print results and drop table. |
| 108 | + match result |
| 109 | + | let r: ResultSet => |
| 110 | + _out.print("ResultSet (" + r.rows().size().string() + " rows):") |
| 111 | + for row in r.rows().values() do |
| 112 | + _out.write(" ") |
| 113 | + for field in row.fields.values() do |
| 114 | + _out.write(" " + field.name + "=") |
| 115 | + match field.value |
| 116 | + | let v: String => _out.write(v) |
| 117 | + | let v: I32 => _out.write(v.string()) |
| 118 | + | None => _out.write("NULL") |
| 119 | + end |
| 120 | + end |
| 121 | + _out.print("") |
| 122 | + end |
| 123 | + end |
| 124 | + _out.print("Dropping table...") |
| 125 | + _session.execute( |
| 126 | + SimpleQuery("DROP TABLE copy_in_example"), this) |
| 127 | + | 4 => |
| 128 | + // Table dropped. Done. |
| 129 | + _out.print("Done.") |
| 130 | + close() |
| 131 | + end |
| 132 | + |
| 133 | + be pg_query_failed(session: Session, query: Query, |
| 134 | + failure: (ErrorResponseMessage | ClientQueryError)) |
| 135 | + => |
| 136 | + match failure |
| 137 | + | let e: ErrorResponseMessage => |
| 138 | + _out.print("Query failed: [" + e.severity + "] " + e.code + ": " |
| 139 | + + e.message) |
| 140 | + | let e: ClientQueryError => |
| 141 | + _out.print("Query failed: client error") |
| 142 | + end |
| 143 | + close() |
| 144 | + |
| 145 | +class val ServerInfo |
| 146 | + let host: String |
| 147 | + let port: String |
| 148 | + let username: String |
| 149 | + let password: String |
| 150 | + let database: String |
| 151 | + |
| 152 | + new val create(vars: (Array[String] val | None)) => |
| 153 | + let e = EnvVars(vars) |
| 154 | + host = try e("POSTGRES_HOST")? else "127.0.0.1" end |
| 155 | + port = try e("POSTGRES_PORT")? else "5432" end |
| 156 | + username = try e("POSTGRES_USERNAME")? else "postgres" end |
| 157 | + password = try e("POSTGRES_PASSWORD")? else "postgres" end |
| 158 | + database = try e("POSTGRES_DATABASE")? else "postgres" end |
0 commit comments