Skip to content
Browse files

[feature] postgres: Added primitive update and delete functions.

  • Loading branch information...
1 parent 47feecd commit 767d18adb9b0c5e31f5f3cb332b361b280f7a715 @nrs135 nrs135 committed Dec 3, 2012
Showing with 18 additions and 0 deletions.
  1. +6 −0 lib/stdlib/apis/postgres/postgres.opa
  2. +12 −0 lib/stdlib/apis/postgres/types.opa
View
6 lib/stdlib/apis/postgres/postgres.opa
@@ -833,6 +833,12 @@ Postgres = {{
insert(conn:Postgres.connection, dbase:string, value:'a, k:Postgres.listener) : Postgres.connection =
query(conn,PostgresTypes.insert(conn, dbase, value),k)
+ update(conn:Postgres.connection, dbase:string, value:'a, select:'b, k:Postgres.listener) : Postgres.connection =
+ query(conn,PostgresTypes.update(conn, dbase, value, select),k)
+
+ delete(conn:Postgres.connection, dbase:string, select:'b, k:Postgres.listener) : Postgres.connection =
+ query(conn,PostgresTypes.delete(conn, dbase, select),k)
+
}}
// End of file postgres.opa
View
12 lib/stdlib/apis/postgres/types.opa
@@ -587,6 +587,9 @@ PostgresTypes = {{
field_types(conn:Postgres.connection, oparowl:Postgres.oparowl) : list(string) =
List.map(((_,val) -> postgres_type(conn,val)),oparowl)
+ field_assigns(oparowl:Postgres.oparowl) : list(string) =
+ List.map(((key,val) -> "{key}={string_of_field_value(val)}"),oparowl)
+
@private csl(l) = String.concat(",",l)
create(conn:Postgres.connection, dbase:string, temp:bool, v:'a) : string =
@@ -599,6 +602,15 @@ PostgresTypes = {{
row = of_opa(conn, v)
"INSERT INTO {dbase}({csl(field_names(row))}) VALUES({csl(field_values(row))})"
+ update(conn:Postgres.connection, dbase:string, value:'a, select:'b) : string =
+ rowa = of_opa(conn, value)
+ rowb = of_opa(conn, select)
+ "UPDATE {dbase} SET {csl(field_assigns(rowa))} WHERE {String.concat(" and ",field_assigns(rowb))}"
+
+ delete(conn:Postgres.connection, dbase:string, select:'b) : string =
+ rowb = of_opa(conn, select)
+ "DELETE FROM {dbase} WHERE {String.concat(" and ",field_assigns(rowb))}"
+
@private StringMap_keys(sm) : list(string) = StringMap.fold((k, _, keys -> [k|keys]),sm,[])
@private intersect(l1, l2) =

0 comments on commit 767d18a

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