Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

  • Loading branch information...
commit cb5d7efce9b27c73f6826f01f741ffeded372a52 1 parent b47f51b
@BourgerieQuentin BourgerieQuentin authored
View
15 lib/stdlib/apis/postgres/postgres.opa
@@ -198,6 +198,7 @@ type Postgres.connection = {
paramdescs : list(int) /** List of the last-received parameter descriptions */
handlers : intmap((string,OpaType.ty,Postgres.abstract_handler)) /** Handlers for unknown data types */
backhandlers : stringmap(int) /** Reverse map for outgoing data */
+ init_session : Postgres.connection -> Postgres.connection
}
/** Defines whether an operation is for a prepared statement or a portal */
@@ -378,7 +379,9 @@ Postgres = {{
params=StringMap.empty
query="" status="" suspended=false in_transaction=false error=none
empty=true completed=[] paramdescs=[] rows=0 rowdescs=[]
- handlers=IntMap.empty backhandlers=StringMap.empty }}
+ handlers=IntMap.empty backhandlers=StringMap.empty
+ init_session=identity
+ }}
| {~failure} -> {~failure}
/** Return the last query made on the connection. */
@@ -465,6 +468,12 @@ Postgres = {{
*/
set_major_version(conn:Postgres.connection, major_version:int) : Postgres.connection = {conn with ~major_version}
+ /**
+ * Add a session initializer.
+ */
+ add_init_session(conn: Postgres.connection, init): Postgres.connection =
+ {conn with init_session= (c -> conn.init_session(init(c)))}
+
/** Install a handler for a given Postgres type id.
*
* The type_id is specific to a given PostgreSQL server so we attach the handler
@@ -646,7 +655,9 @@ Postgres = {{
query="authentication"}
version = Bitwise.lsl(Bitwise.land(conn.major_version,0xffff),16) + Bitwise.land(conn.minor_version,0xffff)
match Pg.start({success=conn.conn}, (version, [("user",conn.conn.conf.user),("database",conn.dbase)])) with
- | {success=c} -> loop({conn with conn=c}, void, ignore_listener).f1
+ | {success=c} ->
+ c = loop({conn with conn=c}, void, ignore_listener).f1
+ c.init_session(c: Postgres.connection)
| ~{failure} -> error(conn,{api_failure=failure}, void, ignore_listener).f1
end
| ~{failure} -> error(conn,{api_failure=failure}, void, ignore_listener).f1
View
33 lib/stdlib/database/postgres/db.opa
@@ -73,6 +73,11 @@ module DbPostgres{
}
@expand
+ function notice(db, msg){
+ gen(@toplevel.Log.notice, db, msg)
+ }
+
+ @expand
function error(db, msg){
gen(@toplevel.Log.error, db, msg)
}
@@ -126,22 +131,26 @@ query: {table}
}
, tables)
/* 3 - Prepare statements */
- List.iter(
- function(~{id, query, types}){
- c = Postgres.parse(c, id, query, types)
- match(Postgres.get_error(c)){
- case {none} :
- Log.debug(c, "Prepared statement: {id}")
- case {some: e} :
- Log.error(c, "An error occurs while prepare statements
+ function init_session(c){
+ List.iter(
+ function(~{id, query, types}){
+ c = Postgres.parse(c, id, query, types)
+ match(Postgres.get_error(c)){
+ case {none} :
+ Log.debug(c, "Prepared statement: {id}")
+ case {some: e} :
+ Log.error(c, "An error occurs while prepare statements
error: {e}
id: {id}
query: {query}
")
- @fail
+ @fail
+ }
}
- }
- , statements)
+ , statements)
+ c
+ }
+ c = init_session(c)
/* 4 - Init queries */
List.iter(
function(query){
@@ -158,7 +167,7 @@ query: {query}
}
, queries)
Postgres.release(c)
- c0
+ Postgres.add_init_session(c0, init_session)
}
)
}
Please sign in to comment.
Something went wrong with that request. Please try again.