Browse Source

move encoding from pgsql_sock:send to pgsql_wire

Anton Lebedevich 13 years ago
parent
commit
47a7c932ff
2 changed files with 16 additions and 16 deletions
  1. 2 12
      src/pgsql_sock.erl
  2. 14 4
      src/pgsql_wire.erl

+ 2 - 12
src/pgsql_sock.erl

@@ -4,7 +4,7 @@
 
 
 -behavior(gen_server).
 -behavior(gen_server).
 
 
--export([start_link/4, send/2, send/3, cancel/3]).
+-export([start_link/4, cancel/3]).
 -export([decode_string/1, lower_atom/1]).
 -export([decode_string/1, lower_atom/1]).
 
 
 -export([handle_call/3, handle_cast/2, handle_info/2]).
 -export([handle_call/3, handle_cast/2, handle_info/2]).
@@ -13,23 +13,13 @@
 -include("pgsql.hrl").
 -include("pgsql.hrl").
 -include("pgsql_binary.hrl").
 -include("pgsql_binary.hrl").
 
 
--record(state, {c, mod, sock, tail}).
+-record(state, {mod, sock, decoder}).
 
 
 %% -- client interface --
 %% -- client interface --
 
 
 start_link(C, Host, Username, Opts) ->
 start_link(C, Host, Username, Opts) ->
     gen_server:start_link(?MODULE, [C, Host, Username, Opts], []).
     gen_server:start_link(?MODULE, [C, Host, Username, Opts], []).
 
 
-send(S, Type, Data) ->
-    Bin = iolist_to_binary(Data),
-    Msg = <<Type:8, (byte_size(Bin) + 4):?int32, Bin/binary>>,
-    gen_server:cast(S, {send, Msg}).
-
-send(S, Data) ->
-    Bin = iolist_to_binary(Data),
-    Msg = <<(byte_size(Bin) + 4):?int32, Bin/binary>>,
-    gen_server:cast(S, {send, Msg}).
-
 cancel(S, Pid, Key) ->
 cancel(S, Pid, Key) ->
     gen_server:cast(S, {cancel, Pid, Key}).
     gen_server:cast(S, {cancel, Pid, Key}).
 
 

+ 14 - 4
src/pgsql_wire.erl

@@ -2,15 +2,25 @@
 
 
 -export([init/1,
 -export([init/1,
          decode/2,
          decode/2,
-         encode/2]).
+         encode/2,
+         encode/3]).
 
 
--record(state, {}).
+-include("pgsql_binary.hrl").
+
+-record(state, {options, tail}).
 
 
 init(Options) ->
 init(Options) ->
-    #state{}.
+    #state{options = Options}.
 
 
 decode(Data, State = #state{}) ->
 decode(Data, State = #state{}) ->
     {ok, State}.
     {ok, State}.
 
 
 encode(Data, State = #state{}) ->
 encode(Data, State = #state{}) ->
-    {Data, State}.
+    Bin = iolist_to_binary(Data),
+    Msg = <<(byte_size(Bin) + 4):?int32, Bin/binary>>,
+    {Msg, State}.
+
+encode(Type, Data, State = #state{}) ->
+    Bin = iolist_to_binary(Data),
+    Msg = <<Type:8, (byte_size(Bin) + 4):?int32, Bin/binary>>,
+    {Msg, State}.