~ubuntu-branches/debian/squeeze/erlang/squeeze

« back to all changes in this revision

Viewing changes to erts/example/pg_async.erl

  • Committer: Bazaar Package Importer
  • Author(s): Erlang Packagers, Sergei Golovan
  • Date: 2006-12-03 17:07:44 UTC
  • mfrom: (2.1.11 feisty)
  • Revision ID: james.westby@ubuntu.com-20061203170744-rghjwupacqlzs6kv
Tags: 1:11.b.2-4
[ Sergei Golovan ]
Fixed erlang-base and erlang-base-hipe prerm scripts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
-module(pg_async).
 
2
 
 
3
-define(DRV_CONNECT, $C).
 
4
-define(DRV_DISCONNECT, $D).
 
5
-define(DRV_SELECT, $S).
 
6
 
 
7
-export([connect/1, disconnect/1, select/2]).
 
8
 
 
9
connect(ConnectStr) ->
 
10
    case erl_ddll:load_driver(".", "pg_async") of
 
11
        ok -> ok;
 
12
        {error, already_loaded} -> ok;
 
13
        E -> exit(E)
 
14
    end,
 
15
    Port = open_port({spawn, ?MODULE}, [binary]),
 
16
    port_control(Port, ?DRV_CONNECT, ConnectStr),
 
17
    case return_port_data(Port) of
 
18
        ok -> 
 
19
            {ok, Port};
 
20
        Error ->
 
21
            Error
 
22
    end.    
 
23
 
 
24
disconnect(Port) ->
 
25
    port_control(Port, ?DRV_DISCONNECT, ""),
 
26
    R = return_port_data(Port),
 
27
    port_close(Port),
 
28
    R.
 
29
 
 
30
select(Port, Query) ->
 
31
    port_control(Port, ?DRV_SELECT, Query),
 
32
    return_port_data(Port).
 
33
 
 
34
return_port_data(Port) ->
 
35
    receive
 
36
        {Port, {data, Data}} ->
 
37
            binary_to_term(Data)
 
38
    end.
 
39