Просмотр исходного кода

Fix support for unnamed statements

This commit fixes a bug in egpsql's handling of unnamed prepared
statements. Prior to this fix epgsql would generate a name for
unnamed SQL statement sent to the DB server w/PARSE messages. This
caused the server to persist the statements until they were
explicitly deallocated. Deallocating the statements was impossible
because the generated name was never exposed over the public API.

The fix is to stop generating names and allow the caller to specify
a name when named statement semantics are desired. Doing so fixes
name collisions when parsing the same statement twice on the same
connection. It also more closely follows PG's statement semantics.
Kevin Smith 11 лет назад
Родитель
Сommit
2f0ad9d81d
1 измененных файлов с 12 добавлено и 4 удалено
  1. 12 4
      src/pgsql.erl

+ 12 - 4
src/pgsql.erl

@@ -7,7 +7,7 @@
          close/1,
          close/1,
          get_parameter/2,
          get_parameter/2,
          squery/2,
          squery/2,
-         equery/2, equery/3,
+         equery/2, equery/3, equery/4,
          parse/2, parse/3, parse/4,
          parse/2, parse/3, parse/4,
          describe/2, describe/3,
          describe/2, describe/3,
          bind/3, bind/4,
          bind/3, bind/4,
@@ -58,7 +58,15 @@ equery(C, Sql) ->
 
 
 %% TODO add fast_equery command that doesn't need parsed statement
 %% TODO add fast_equery command that doesn't need parsed statement
 equery(C, Sql, Parameters) ->
 equery(C, Sql, Parameters) ->
-    Name = ["equery-", atom_to_list(node()), pid_to_list(self())],
+    case parse(C, "", Sql, []) of
+        {ok, #statement{types = Types} = S} ->
+            Typed_Parameters = lists:zip(Types, Parameters),
+            gen_server:call(C, {equery, S, Typed_Parameters}, infinity);
+        Error ->
+            Error
+    end.
+
+equery(C, Name, Sql, Parameters) ->
     case parse(C, Name, Sql, []) of
     case parse(C, Name, Sql, []) of
         {ok, #statement{types = Types} = S} ->
         {ok, #statement{types = Types} = S} ->
             Typed_Parameters = lists:zip(Types, Parameters),
             Typed_Parameters = lists:zip(Types, Parameters),
@@ -70,10 +78,10 @@ equery(C, Sql, Parameters) ->
 %% parse
 %% parse
 
 
 parse(C, Sql) ->
 parse(C, Sql) ->
-    parse(C, "", Sql, []).
+    parse(C, Sql, []).
 
 
 parse(C, Sql, Types) ->
 parse(C, Sql, Types) ->
-    parse(C, "", Sql, Types).
+    sync_on_error(C, gen_server:call(C, {parse, Sql, Types}, infinity)).
 
 
 parse(C, Name, Sql, Types) ->
 parse(C, Name, Sql, Types) ->
     sync_on_error(C, gen_server:call(C, {parse, Name, Sql, Types}, infinity)).
     sync_on_error(C, gen_server:call(C, {parse, Name, Sql, Types}, infinity)).