Skip to content
This repository
Browse code

[enhance] stdlib, runtime: Add a way to initialize PG sessions

  • Loading branch information...
commit cb5d7efce9b27c73f6826f01f741ffeded372a52 1 parent b47f51b
Quentin Bourgerie BourgerieQuentin authored
15 lib/stdlib/apis/postgres/postgres.opa
@@ -198,6 +198,7 @@ type Postgres.connection = {
198 198 paramdescs : list(int) /** List of the last-received parameter descriptions */
199 199 handlers : intmap((string,OpaType.ty,Postgres.abstract_handler)) /** Handlers for unknown data types */
200 200 backhandlers : stringmap(int) /** Reverse map for outgoing data */
  201 + init_session : Postgres.connection -> Postgres.connection
201 202 }
202 203
203 204 /** Defines whether an operation is for a prepared statement or a portal */
@@ -378,7 +379,9 @@ Postgres = {{
378 379 params=StringMap.empty
379 380 query="" status="" suspended=false in_transaction=false error=none
380 381 empty=true completed=[] paramdescs=[] rows=0 rowdescs=[]
381   - handlers=IntMap.empty backhandlers=StringMap.empty }}
  382 + handlers=IntMap.empty backhandlers=StringMap.empty
  383 + init_session=identity
  384 + }}
382 385 | {~failure} -> {~failure}
383 386
384 387 /** Return the last query made on the connection. */
@@ -465,6 +468,12 @@ Postgres = {{
465 468 */
466 469 set_major_version(conn:Postgres.connection, major_version:int) : Postgres.connection = {conn with ~major_version}
467 470
  471 + /**
  472 + * Add a session initializer.
  473 + */
  474 + add_init_session(conn: Postgres.connection, init): Postgres.connection =
  475 + {conn with init_session= (c -> conn.init_session(init(c)))}
  476 +
468 477 /** Install a handler for a given Postgres type id.
469 478 *
470 479 * The type_id is specific to a given PostgreSQL server so we attach the handler
@@ -646,7 +655,9 @@ Postgres = {{
646 655 query="authentication"}
647 656 version = Bitwise.lsl(Bitwise.land(conn.major_version,0xffff),16) + Bitwise.land(conn.minor_version,0xffff)
648 657 match Pg.start({success=conn.conn}, (version, [("user",conn.conn.conf.user),("database",conn.dbase)])) with
649   - | {success=c} -> loop({conn with conn=c}, void, ignore_listener).f1
  658 + | {success=c} ->
  659 + c = loop({conn with conn=c}, void, ignore_listener).f1
  660 + c.init_session(c: Postgres.connection)
650 661 | ~{failure} -> error(conn,{api_failure=failure}, void, ignore_listener).f1
651 662 end
652 663 | ~{failure} -> error(conn,{api_failure=failure}, void, ignore_listener).f1
33 lib/stdlib/database/postgres/db.opa
@@ -73,6 +73,11 @@ module DbPostgres{
73 73 }
74 74
75 75 @expand
  76 + function notice(db, msg){
  77 + gen(@toplevel.Log.notice, db, msg)
  78 + }
  79 +
  80 + @expand
76 81 function error(db, msg){
77 82 gen(@toplevel.Log.error, db, msg)
78 83 }
@@ -126,22 +131,26 @@ query: {table}
126 131 }
127 132 , tables)
128 133 /* 3 - Prepare statements */
129   - List.iter(
130   - function(~{id, query, types}){
131   - c = Postgres.parse(c, id, query, types)
132   - match(Postgres.get_error(c)){
133   - case {none} :
134   - Log.debug(c, "Prepared statement: {id}")
135   - case {some: e} :
136   - Log.error(c, "An error occurs while prepare statements
  134 + function init_session(c){
  135 + List.iter(
  136 + function(~{id, query, types}){
  137 + c = Postgres.parse(c, id, query, types)
  138 + match(Postgres.get_error(c)){
  139 + case {none} :
  140 + Log.debug(c, "Prepared statement: {id}")
  141 + case {some: e} :
  142 + Log.error(c, "An error occurs while prepare statements
137 143 error: {e}
138 144 id: {id}
139 145 query: {query}
140 146 ")
141   - @fail
  147 + @fail
  148 + }
142 149 }
143   - }
144   - , statements)
  150 + , statements)
  151 + c
  152 + }
  153 + c = init_session(c)
145 154 /* 4 - Init queries */
146 155 List.iter(
147 156 function(query){
@@ -158,7 +167,7 @@ query: {query}
158 167 }
159 168 , queries)
160 169 Postgres.release(c)
161   - c0
  170 + Postgres.add_init_session(c0, init_session)
162 171 }
163 172 )
164 173 }

0 comments on commit cb5d7ef

Please sign in to comment.
Something went wrong with that request. Please try again.