|
| 1 | +""" |
| 2 | +Composite type support via `CodecRegistry.with_composite_type()`. Creates a |
| 3 | +PostgreSQL composite type, discovers its OID from `pg_type`, registers it |
| 4 | +with field descriptors, and queries it with `PreparedQuery` to get |
| 5 | +`PgComposite` results. Uses two sessions to demonstrate the typical |
| 6 | +two-phase pattern for dynamic OIDs: the first session discovers the OID, |
| 7 | +the second uses a `CodecRegistry` built with that OID. |
| 8 | +
|
| 9 | +Shows positional access via `apply()`, named access via `field()`, and |
| 10 | +sending a `PgComposite` as a query parameter. |
| 11 | +""" |
| 12 | +use "cli" |
| 13 | +use "collections" |
| 14 | +use lori = "lori" |
| 15 | +// in your code this `use` statement would be: |
| 16 | +// use "postgres" |
| 17 | +use "../../postgres" |
| 18 | + |
| 19 | +actor Main |
| 20 | + new create(env: Env) => |
| 21 | + let server_info = ServerInfo(env.vars) |
| 22 | + let auth = lori.TCPConnectAuth(env.root) |
| 23 | + Client(auth, server_info, env.out) |
| 24 | + |
| 25 | +actor Client is (SessionStatusNotify & ResultReceiver) |
| 26 | + let _auth: lori.TCPConnectAuth |
| 27 | + let _info: ServerInfo |
| 28 | + let _out: OutStream |
| 29 | + var _session: Session |
| 30 | + var _phase: USize = 0 |
| 31 | + var _composite_oid: U32 = 0 |
| 32 | + |
| 33 | + new create(auth: lori.TCPConnectAuth, info: ServerInfo, out: OutStream) => |
| 34 | + _auth = auth |
| 35 | + _info = info |
| 36 | + _out = out |
| 37 | + _session = Session( |
| 38 | + ServerConnectInfo(_auth, _info.host, _info.port), |
| 39 | + DatabaseConnectInfo(_info.username, _info.password, _info.database), |
| 40 | + this) |
| 41 | + |
| 42 | + be close() => |
| 43 | + _session.close() |
| 44 | + |
| 45 | + be pg_session_authenticated(session: Session) => |
| 46 | + match _phase |
| 47 | + | 0 => |
| 48 | + // Phase 1: set up the composite type and discover its OID. |
| 49 | + _out.print("Authenticated (phase 1: discover composite OID).") |
| 50 | + session.execute( |
| 51 | + SimpleQuery("DROP TYPE IF EXISTS address"), this) |
| 52 | + | 4 => |
| 53 | + // Phase 2: query with composite-aware registry. |
| 54 | + _out.print("Authenticated (phase 2: query with registered composite).") |
| 55 | + _phase = 5 |
| 56 | + |
| 57 | + // SELECT a composite literal |
| 58 | + session.execute(PreparedQuery( |
| 59 | + "SELECT ROW('123 Main St','Springfield',62704)::address AS addr", |
| 60 | + recover val Array[FieldDataTypes] end), this) |
| 61 | + end |
| 62 | + |
| 63 | + be pg_session_authentication_failed( |
| 64 | + s: Session, |
| 65 | + reason: AuthenticationFailureReason) |
| 66 | + => |
| 67 | + _out.print("Failed to authenticate.") |
| 68 | + |
| 69 | + be pg_query_result(session: Session, result: Result) => |
| 70 | + _phase = _phase + 1 |
| 71 | + |
| 72 | + match _phase |
| 73 | + | 1 => |
| 74 | + // Old type dropped. Create the composite type. |
| 75 | + _out.print("Creating composite type 'address'...") |
| 76 | + session.execute(SimpleQuery( |
| 77 | + "CREATE TYPE address AS (street text, city text, zip_code int4)"), |
| 78 | + this) |
| 79 | + | 2 => |
| 80 | + // Type created. Query its OID from pg_type. |
| 81 | + _out.print("Querying composite OID from pg_type...") |
| 82 | + session.execute( |
| 83 | + SimpleQuery("SELECT oid FROM pg_type WHERE typname = 'address'"), |
| 84 | + this) |
| 85 | + | 3 => |
| 86 | + // Got the OID. Parse it. |
| 87 | + var oid: U32 = 0 |
| 88 | + match result |
| 89 | + | let rs: ResultSet => |
| 90 | + try |
| 91 | + match rs.rows()(0)?.fields(0)?.value |
| 92 | + | let s: String => oid = s.u32()? |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | + if oid == 0 then |
| 97 | + _out.print("Failed to discover composite OID.") |
| 98 | + close() |
| 99 | + return |
| 100 | + end |
| 101 | + _composite_oid = oid |
| 102 | + _out.print("Discovered composite OID: " + oid.string()) |
| 103 | + |
| 104 | + // Register and query the array OID too if needed |
| 105 | + _out.print("Querying array OID from pg_type...") |
| 106 | + session.execute( |
| 107 | + SimpleQuery("SELECT typarray FROM pg_type WHERE oid = " |
| 108 | + + oid.string()), |
| 109 | + this) |
| 110 | + | 4 => |
| 111 | + // Got the array OID. Build registry, close, reconnect. |
| 112 | + var array_oid: U32 = 0 |
| 113 | + match result |
| 114 | + | let rs: ResultSet => |
| 115 | + try |
| 116 | + match rs.rows()(0)?.fields(0)?.value |
| 117 | + | let s: String => array_oid = s.u32()? |
| 118 | + end |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + let descriptors: Array[(String, U32)] val = recover val |
| 123 | + [as (String, U32): ("street", 25); ("city", 25); ("zip_code", 23)] |
| 124 | + end |
| 125 | + let registry = try |
| 126 | + let r = CodecRegistry |
| 127 | + .with_composite_type(_composite_oid, descriptors)? |
| 128 | + if array_oid > 0 then |
| 129 | + r.with_array_type(array_oid, _composite_oid)? |
| 130 | + else |
| 131 | + r |
| 132 | + end |
| 133 | + else |
| 134 | + _out.print("Failed to register composite type.") |
| 135 | + close() |
| 136 | + return |
| 137 | + end |
| 138 | + session.close() |
| 139 | + _session = Session( |
| 140 | + ServerConnectInfo(_auth, _info.host, _info.port), |
| 141 | + DatabaseConnectInfo(_info.username, _info.password, _info.database), |
| 142 | + this where registry = registry) |
| 143 | + | 6 => |
| 144 | + // PreparedQuery result. The composite value arrives as PgComposite. |
| 145 | + match result |
| 146 | + | let rs: ResultSet => |
| 147 | + _out.print("ResultSet (" + rs.rows().size().string() + " rows):") |
| 148 | + for row in rs.rows().values() do |
| 149 | + for field in row.fields.values() do |
| 150 | + match field.value |
| 151 | + | let c: PgComposite => |
| 152 | + _out.print(" " + field.name + " (composite):") |
| 153 | + // Positional access |
| 154 | + try |
| 155 | + match c(0)? |
| 156 | + | let s: String => _out.print(" [0] street = " + s) |
| 157 | + | None => _out.print(" [0] street = NULL") |
| 158 | + end |
| 159 | + end |
| 160 | + // Named access |
| 161 | + try |
| 162 | + match c.field("city")? |
| 163 | + | let s: String => _out.print(" city = " + s) |
| 164 | + | None => _out.print(" city = NULL") |
| 165 | + end |
| 166 | + end |
| 167 | + try |
| 168 | + match c.field("zip_code")? |
| 169 | + | let v: I32 => |
| 170 | + _out.print(" zip_code = " + v.string()) |
| 171 | + | None => _out.print(" zip_code = NULL") |
| 172 | + end |
| 173 | + end |
| 174 | + // String representation |
| 175 | + _out.print(" string() = " + c.string()) |
| 176 | + | None => _out.print(" " + field.name + " = NULL") |
| 177 | + end |
| 178 | + end |
| 179 | + end |
| 180 | + end |
| 181 | + |
| 182 | + // Now send a PgComposite as a parameter |
| 183 | + _out.print("Sending PgComposite as query parameter...") |
| 184 | + let addr = PgComposite.from_fields(_composite_oid, |
| 185 | + recover val |
| 186 | + [as (String, U32, (FieldData | None)): |
| 187 | + ("street", 25, "42 Elm St") |
| 188 | + ("city", 25, "Portland") |
| 189 | + ("zip_code", 23, I32(97201))] |
| 190 | + end) |
| 191 | + session.execute(PreparedQuery( |
| 192 | + "SELECT $1::address AS roundtrip", |
| 193 | + recover val [as FieldDataTypes: addr] end), this) |
| 194 | + | 7 => |
| 195 | + // Roundtrip result |
| 196 | + match result |
| 197 | + | let rs: ResultSet => |
| 198 | + _out.print("Roundtrip result:") |
| 199 | + try |
| 200 | + match rs.rows()(0)?.fields(0)?.value |
| 201 | + | let c: PgComposite => |
| 202 | + _out.print(" " + c.string()) |
| 203 | + end |
| 204 | + end |
| 205 | + end |
| 206 | + // Clean up |
| 207 | + _out.print("Dropping composite type...") |
| 208 | + session.execute(SimpleQuery("DROP TYPE address"), this) |
| 209 | + | 8 => |
| 210 | + _out.print("Done.") |
| 211 | + close() |
| 212 | + end |
| 213 | + |
| 214 | + be pg_query_failed(session: Session, query: Query, |
| 215 | + failure: (ErrorResponseMessage | ClientQueryError)) |
| 216 | + => |
| 217 | + match failure |
| 218 | + | let e: ErrorResponseMessage => |
| 219 | + _out.print("Query failed: [" + e.severity + "] " + e.code + ": " |
| 220 | + + e.message) |
| 221 | + | let _: SessionClosed => |
| 222 | + return |
| 223 | + | let e: ClientQueryError => |
| 224 | + _out.print("Query failed: client error") |
| 225 | + end |
| 226 | + close() |
| 227 | + |
| 228 | +class val ServerInfo |
| 229 | + let host: String |
| 230 | + let port: String |
| 231 | + let username: String |
| 232 | + let password: String |
| 233 | + let database: String |
| 234 | + |
| 235 | + new val create(vars: (Array[String] val | None)) => |
| 236 | + let e = EnvVars(vars) |
| 237 | + host = try e("POSTGRES_HOST")? else "127.0.0.1" end |
| 238 | + port = try e("POSTGRES_PORT")? else "5432" end |
| 239 | + username = try e("POSTGRES_USERNAME")? else "postgres" end |
| 240 | + password = try e("POSTGRES_PASSWORD")? else "postgres" end |
| 241 | + database = try e("POSTGRES_DATABASE")? else "postgres" end |
0 commit comments