Working version of sqlite

This commit is contained in:
Tee Teoh
2008-06-04 23:14:51 -04:00
parent 91ff5f6a88
commit 7a4ae6f539
3 changed files with 209 additions and 33 deletions

View File

@@ -10,7 +10,8 @@
-behaviour(gen_server).
%% API
-export([start_link/1]).
-export([open/1, open/2]).
-export([start_link/1, start_link/2]).
-export([stop/1, close/1]).
-export([sql_exec/2]).
@@ -36,16 +37,23 @@ start_link(Db) ->
%% {db, DbFile :: String()}
%%--------------------------------------------------------------------
start_link(Db, Options) ->
gen_server:start_link({local, Db}, ?MODULE, [], Options).
gen_server:start_link({local, Db}, ?MODULE, Options, []).
open(Db) ->
?MODULE:start_link(Db).
open(Db, Options) ->
?MODULE:start_link(Db, Options).
close(Db) ->
gen_server:call(Db, close).
stop(Db) ->
close(Db).
?MODULE:close(Db).
sql_exec(Db, SQL) ->
gen_server:call(Db, {sql_exec, SQL}).
%%====================================================================
%% gen_server callbacks
%%====================================================================
@@ -58,6 +66,83 @@ sql_exec(Db, SQL) ->
%% Description: Initiates the server
%%--------------------------------------------------------------------
init(Options) ->
Dbase = proplist:get_value(db, Options),
Port = open_port({spawn, create_cmd(Dbase)
Dbase = proplists:get_value(db, Options),
Port = open_port({spawn, create_cmd(Dbase)}, [{packet, 2}, binary]),
{ok, #state{port = Port, ops = Options}}.
%%--------------------------------------------------------------------
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% Description: Handling call messages
%%--------------------------------------------------------------------
handle_call(close, _From, State) ->
Reply = ok,
{stop, normal, Reply, State};
handle_call({sql_exec, SQL}, _From, #state{port = Port} = State) ->
port_command(Port, term_to_binary({sql_exec, SQL})),
Reply = receive
{Port, {data, Data}} when is_binary(Data) ->
List = binary_to_term(Data),
if is_list(List) ->
lists:reverse(List);
true -> List
end;
_ ->
ok
end,
{reply, Reply, State};
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
%%--------------------------------------------------------------------
%% Function: handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% Description: Handling cast messages
%%--------------------------------------------------------------------
handle_cast(_Msg, State) ->
{noreply, State}.
%%--------------------------------------------------------------------
%% Function: handle_info(Info, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% Description: Handling all non call/cast messages
%%--------------------------------------------------------------------
handle_info(_Info, State) ->
{noreply, State}.
%%--------------------------------------------------------------------
%% Function: terminate(Reason, State) -> void()
%% Description: This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any necessary
%% cleaning up. When it returns, the gen_server terminates with Reason.
%% The return value is ignored.
%%--------------------------------------------------------------------
terminate(normal, #state{port = Port}) ->
port_command(Port, term_to_binary({close, nop})),
port_close(Port),
ok;
terminate(_Reason, _State) ->
ok.
%%--------------------------------------------------------------------
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
%% Description: Convert process state when code is changed
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
create_cmd(Dbase) ->
"sqlite_port " ++ Dbase.