renaming to sqlite3

This commit is contained in:
Max Lapshin
2009-10-13 00:35:27 +04:00
parent 247d23002d
commit eb221011fc
9 changed files with 56 additions and 60 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
doc

View File

@@ -1,3 +1,3 @@
{'src/sqlite_lib.erl',[debug_info,{outdir,"ebin"}]}. {'src/sqlite3_lib.erl',[debug_info,{outdir,"ebin"}]}.
{'src/sqlite.erl',[debug_info,{outdir,"ebin"}]}. {'src/sqlite3.erl',[debug_info,{outdir,"ebin"}]}.
{'src/sqlite_test.erl',[debug_info,{outdir,"ebin"}]}. {'src/sqlite3_test.erl',[debug_info,{outdir,"ebin"}]}.

View File

@@ -9,13 +9,13 @@ compile:
cd priv && make cd priv && make
static: static:
dialyzer --build_plt --output_plt sqlite.plt -r ebin $(PLT_SRC) dialyzer --build_plt --output_plt sqlite3.plt -r ebin $(PLT_SRC)
clean: clean:
- rm -rf ebin/*.beam doc/* sqlite.plt src/test/*.beam - rm -rf ebin/*.beam doc/* sqlite3.plt src/test/*.beam
- rm -rf ct_run* all_runs.html variables* index.html - rm -rf ct_run* all_runs.html variables* index.html
find . -name "*~" | xargs rm find . -name "*~" | xargs rm
cd priv && make clean cd priv && make clean
docs: docs:
$(ERL) -noshell -run edoc_run application "'sqlite'" '"."' '[{title,"Welcome to sqlite"},{hidden,false},{private,false}]' -s erlang halt $(ERL) -noshell -run edoc_run application "'sqlite3'" '"."' '[{title,"Welcome to sqlite3"},{hidden,false},{private,false}]' -s erlang halt

3
ebin/.gitignore vendored
View File

@@ -0,0 +1,3 @@
*.beam
*.so

View File

@@ -1,9 +0,0 @@
{author, {"Tee Teoh", "tty.erlang@gmail.com", {2008,6,8}}}.
{packager, {"Tee Teoh", "tty.erlang@gmail.com"}}.
{category, ["database","driver"]}.
{name, "sqlite"}.
{vsn, "1.0.0"}.
{depends, ["kernel","proplists","lists","string","stdlib"]}.
{keywords, ["database","sqlite","driver"]}.
{summary, "sqlite driver"}.
{abstract, "Manages a sqlite database."}.

View File

@@ -1,12 +1,12 @@
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
%%% File : sqlite.erl %%% File : sqlite3.erl
%%% @author Tee Teoh %%% @author Tee Teoh
%%% @copyright 21 Jun 2008 by Tee Teoh %%% @copyright 21 Jun 2008 by Tee Teoh
%%% @version 1.0.0 %%% @version 1.0.0
%%% @doc Library module for sqlite %%% @doc Library module for sqlite3
%%% @end %%% @end
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
-module(sqlite). -module(sqlite3).
-behaviour(gen_server). -behaviour(gen_server).
@@ -37,7 +37,7 @@
%% @spec start_link(Db) -> {ok,Pid} | ignore | {error,Error} %% @spec start_link(Db) -> {ok,Pid} | ignore | {error,Error}
%% Db = atom() %% Db = atom()
%% @doc %% @doc
%% Opens a sqlite dbase creating one if necessary. The dbase must %% Opens a sqlite3 dbase creating one if necessary. The dbase must
%% be called Db.db in the current path. start_link/1 can be use %% be called Db.db in the current path. start_link/1 can be use
%% with stop/0, sql_exec/1, create_table/2, list_tables/0, %% with stop/0, sql_exec/1, create_table/2, list_tables/0,
%% table_info/1, write/2, read/2, delete/2 and drop_table/1. %% table_info/1, write/2, read/2, delete/2 and drop_table/1.
@@ -56,10 +56,10 @@ start_link(Db) ->
%% @spec start_link(Db, Options) -> {ok,Pid} | ignore | {error,Error} %% @spec start_link(Db, Options) -> {ok,Pid} | ignore | {error,Error}
%% Db = atom() %% Db = atom()
%% @doc %% @doc
%% Opens a sqlite dbase creating one if necessary. By default the %% Opens a sqlite3 dbase creating one if necessary. By default the
%% dbase will be called Db.db in the current path. This can be changed %% dbase will be called Db.db in the current path. This can be changed
%% by passing the option {db, DbFile :: String()}. DbFile must be the %% by passing the option {db, DbFile :: String()}. DbFile must be the
%% full path to the sqlite db file. start_link/1 can be use with stop/0, %% full path to the sqlite3 db file. start_link/1 can be use with stop/0,
%% sql_exec/1, create_table/2, list_tables/0, table_info/1, write/2, %% sql_exec/1, create_table/2, list_tables/0, table_info/1, write/2,
%% read/2, delete/2 and drop_table/1. There can be only one start_link %% read/2, delete/2 and drop_table/1. There can be only one start_link
%% call per node. %% call per node.
@@ -78,8 +78,8 @@ start_link(Db, Options) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec open(Db :: atom()) -> {ok, Pid::pid()} | ignore | {error, Error} %% @spec open(Db :: atom()) -> {ok, Pid::pid()} | ignore | {error, Error}
%% @doc %% @doc
%% Opens a sqlite dbase creating one if necessary. The dbase must be %% Opens a sqlite3 dbase creating one if necessary. The dbase must be
%% called Db.db in the current path. Can be use to open multiple sqlite %% called Db.db in the current path. Can be use to open multiple sqlite3
%% dbases per node. Must be use in conjunction with stop/1, sql_exec/2, %% dbases per node. Must be use in conjunction with stop/1, sql_exec/2,
%% create_table/3, list_tables/1, table_info/2, write/3, read/3, delete/3 %% create_table/3, list_tables/1, table_info/2, write/3, read/3, delete/3
%% and drop_table/2. %% and drop_table/2.
@@ -92,10 +92,10 @@ open(Db) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec open(Db::atom(), Options::[tuple()]) -> {ok, Pid::pid()} | ignore | {error, Error} %% @spec open(Db::atom(), Options::[tuple()]) -> {ok, Pid::pid()} | ignore | {error, Error}
%% @doc %% @doc
%% Opens a sqlite dbase creating one if necessary. By default the dbase %% Opens a sqlite3 dbase creating one if necessary. By default the dbase
%% will be called Db.db in the current path. This can be changed by %% will be called Db.db in the current path. This can be changed by
%% passing the option {db, DbFile :: String()}. DbFile must be the full %% passing the option {db, DbFile :: String()}. DbFile must be the full
%% path to the sqlite db file. Can be use to open multiple sqlite dbases %% path to the sqlite3 db file. Can be use to open multiple sqlite3 dbases
%% per node. Must be use in conjunction with stop/1, sql_exec/2, %% per node. Must be use in conjunction with stop/1, sql_exec/2,
%% create_table/3, list_tables/1, table_info/2, write/3, read/3, delete/3 %% create_table/3, list_tables/1, table_info/2, write/3, read/3, delete/3
%% and drop_table/2. %% and drop_table/2.
@@ -108,7 +108,7 @@ open(Db, Options) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec close(Db::atom()) -> ok %% @spec close(Db::atom()) -> ok
%% @doc %% @doc
%% Closes the Db sqlite dbase. %% Closes the Db sqlite3 dbase.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec(close/1::(atom()) -> 'ok'). -spec(close/1::(atom()) -> 'ok').
@@ -118,7 +118,7 @@ close(Db) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec stop() -> ok %% @spec stop() -> ok
%% @doc %% @doc
%% Closes the sqlite dbase. %% Closes the sqlite3 dbase.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec(stop/0::() -> 'ok'). -spec(stop/0::() -> 'ok').
@@ -365,29 +365,29 @@ handle_call(list_tables, _From, #state{port = Port} = State) ->
handle_call({table_info, Tbl}, _From, #state{port = Port} = State) -> handle_call({table_info, Tbl}, _From, #state{port = Port} = State) ->
% make sure we only get table info. % make sure we only get table info.
% SQL Injection warning % SQL Injection warning
SQL = io_lib:format("select sql from sqlite_master where tbl_name = '~p' and type='table';", [Tbl]), SQL = io_lib:format("select sql from sqlite3_master where tbl_name = '~p' and type='table';", [Tbl]),
[{Info}] = exec(Port, {sql_exec, SQL}), [{Info}] = exec(Port, {sql_exec, SQL}),
Reply = parse_table_info(Info), Reply = parse_table_info(Info),
{reply, Reply, State}; {reply, Reply, State};
handle_call({create_table, Tbl, Options}, _From, #state{port = Port} = State) -> handle_call({create_table, Tbl, Options}, _From, #state{port = Port} = State) ->
SQL = sqlite_lib:create_table_sql(Tbl, Options), SQL = sqlite3_lib:create_table_sql(Tbl, Options),
Cmd = {sql_exec, SQL}, Cmd = {sql_exec, SQL},
Reply = exec(Port, Cmd), Reply = exec(Port, Cmd),
{reply, Reply, State}; {reply, Reply, State};
handle_call({write, Tbl, Data}, _From, #state{port = Port} = State) -> handle_call({write, Tbl, Data}, _From, #state{port = Port} = State) ->
% insert into t1 (data,num) values ('This is sample data',3); % insert into t1 (data,num) values ('This is sample data',3);
Reply = exec(Port, {sql_exec, sqlite_lib:write_sql(Tbl, Data)}), Reply = exec(Port, {sql_exec, sqlite3_lib:write_sql(Tbl, Data)}),
{reply, Reply, State}; {reply, Reply, State};
handle_call({read, Tbl, {Key, Value}}, _From, #state{port = Port} = State) -> handle_call({read, Tbl, {Key, Value}}, _From, #state{port = Port} = State) ->
% select * from Tbl where Key = Value; % select * from Tbl where Key = Value;
Reply = exec(Port, {sql_exec, sqlite_lib:read_sql(Tbl, Key, Value)}), Reply = exec(Port, {sql_exec, sqlite3_lib:read_sql(Tbl, Key, Value)}),
{reply, Reply, State}; {reply, Reply, State};
handle_call({delete, Tbl, {Key, Value}}, _From, #state{port = Port} = State) -> handle_call({delete, Tbl, {Key, Value}}, _From, #state{port = Port} = State) ->
% delete from Tbl where Key = Value; % delete from Tbl where Key = Value;
Reply = exec(Port, {sql_exec, sqlite_lib:delete_sql(Tbl, Key, Value)}), Reply = exec(Port, {sql_exec, sqlite3_lib:delete_sql(Tbl, Key, Value)}),
{reply, Reply, State}; {reply, Reply, State};
handle_call({drop_table, Tbl}, _From, #state{port = Port} = State) -> handle_call({drop_table, Tbl}, _From, #state{port = Port} = State) ->
Reply = exec(Port, {sql_exec, sqlite_lib:drop_table(Tbl)}), Reply = exec(Port, {sql_exec, sqlite3_lib:drop_table(Tbl)}),
{reply, Reply, State}; {reply, Reply, State};
handle_call(_Request, _From, State) -> handle_call(_Request, _From, State) ->
Reply = ok, Reply = ok,
@@ -450,7 +450,7 @@ code_change(_OldVsn, State, _Extra) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
create_cmd(Dbase) -> create_cmd(Dbase) ->
"sqlite_port " ++ Dbase. "sqlite3_port " ++ Dbase.
exec(Port, Cmd) -> exec(Port, Cmd) ->
port_command(Port, term_to_binary(Cmd)), port_command(Port, term_to_binary(Cmd)),
@@ -476,7 +476,7 @@ parse_table_info(Info) ->
build_table_info([], Acc) -> build_table_info([], Acc) ->
lists:reverse(Acc); lists:reverse(Acc);
build_table_info([[ColName, ColType] | Tl], Acc) -> build_table_info([[ColName, ColType] | Tl], Acc) ->
build_table_info(Tl, [{list_to_atom(ColName), sqlite_lib:col_type(ColType)}| Acc]); build_table_info(Tl, [{list_to_atom(ColName), sqlite3_lib:col_type(ColType)}| Acc]);
build_table_info([[ColName, ColType, "PRIMARY", "KEY"] | Tl], Acc) -> build_table_info([[ColName, ColType, "PRIMARY", "KEY"] | Tl], Acc) ->
build_table_info(Tl, [{list_to_atom(ColName), sqlite_lib:col_type(ColType)}| Acc]). build_table_info(Tl, [{list_to_atom(ColName), sqlite3_lib:col_type(ColType)}| Acc]).

View File

@@ -1,12 +1,12 @@
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
%%% File : sqlite_lib.erl %%% File : sqlite3_lib.erl
%%% @author Tee Teoh %%% @author Tee Teoh
%%% @copyright 21 Jun 2008 by Tee Teoh %%% @copyright 21 Jun 2008 by Tee Teoh
%%% @version 1.0.0 %%% @version 1.0.0
%%% @doc Library module for sqlite %%% @doc Library module for sqlite3
%%% @end %%% @end
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
-module(sqlite_lib). -module(sqlite3_lib).
%% API %% API
-export([col_type/1]). -export([col_type/1]).
@@ -18,7 +18,7 @@
%%==================================================================== %%====================================================================
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec col_type(Type :: term()) -> term() %% @spec col_type(Type :: term()) -> term()
%% @doc Maps sqlite column type. %% @doc Maps sqlite3 column type.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec(col_type/1::(atom() | string()) -> atom() | string()). -spec(col_type/1::(atom() | string()) -> atom() | string()).
col_type(integer) -> col_type(integer) ->
@@ -77,10 +77,10 @@ write_col_sql(Cols) ->
-spec(create_table_sql/2::(atom(), [{atom(), string()}]) -> string()). -spec(create_table_sql/2::(atom(), [{atom(), string()}]) -> string()).
create_table_sql(Tbl, [{ColName, Type} | Tl]) -> create_table_sql(Tbl, [{ColName, Type} | Tl]) ->
CT = io_lib:format("CREATE TABLE ~p ", [Tbl]), CT = io_lib:format("CREATE TABLE ~p ", [Tbl]),
Start = io_lib:format("(~p ~s PRIMARY KEY, ", [ColName, sqlite_lib:col_type(Type)]), Start = io_lib:format("(~p ~s PRIMARY KEY, ", [ColName, sqlite3_lib:col_type(Type)]),
End = string:join( End = string:join(
lists:map(fun({Name0, Type0}) -> lists:map(fun({Name0, Type0}) ->
io_lib:format("~p ~s", [Name0, sqlite_lib:col_type(Type0)]) io_lib:format("~p ~s", [Name0, sqlite3_lib:col_type(Type0)])
end, Tl), ", ") ++ ");", end, Tl), ", ") ++ ");",
lists:flatten(CT ++ Start ++ End). lists:flatten(CT ++ Start ++ End).
@@ -99,8 +99,8 @@ write_sql(Tbl, Data) ->
lists:flatten( lists:flatten(
io_lib:format("INSERT INTO ~p (~s) values (~s);", io_lib:format("INSERT INTO ~p (~s) values (~s);",
[Tbl, [Tbl,
sqlite_lib:write_col_sql(Cols), sqlite3_lib:write_col_sql(Cols),
sqlite_lib:write_value_sql(Values)])). sqlite3_lib:write_value_sql(Values)])).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec read_sql(Tbl, Key, Value) -> string() %% @spec read_sql(Tbl, Key, Value) -> string()

View File

@@ -1,11 +1,11 @@
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
%%% File : sqlite_test.erl %%% File : sqlite3_test.erl
%%% Author : Tee Teoh <tteoh@teemac.ott.cti.com> %%% Author : Tee Teoh <tteoh@teemac.ott.cti.com>
%%% Description : %%% Description :
%%% %%%
%%% Created : 10 Jun 2008 by Tee Teoh <tteoh@teemac.ott.cti.com> %%% Created : 10 Jun 2008 by Tee Teoh <tteoh@teemac.ott.cti.com>
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
-module(sqlite_test). -module(sqlite3_test).
%% API %% API
-export([create_table_test/0]). -export([create_table_test/0]).
@@ -20,18 +20,18 @@
%% Description: %% Description:
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
create_table_test() -> create_table_test() ->
sqlite:open(ct), sqlite3:open(ct),
sqlite:create_table(ct, user, [{name, text}, {age, integer}, {wage, integer}]), sqlite3:create_table(ct, user, [{name, text}, {age, integer}, {wage, integer}]),
[user] = sqlite:list_tables(ct), [user] = sqlite3:list_tables(ct),
[{name, text}, {age, integer}, {wage, integer}] = sqlite:table_info(ct, user), [{name, text}, {age, integer}, {wage, integer}] = sqlite3:table_info(ct, user),
sqlite:write(ct, user, [{name, "abby"}, {age, 20}, {wage, 2000}]), sqlite3:write(ct, user, [{name, "abby"}, {age, 20}, {wage, 2000}]),
sqlite:write(ct, user, [{name, "marge"}, {age, 30}, {wage, 3000}]), sqlite3:write(ct, user, [{name, "marge"}, {age, 30}, {wage, 3000}]),
sqlite:sql_exec(ct, "select * from user;"), sqlite3:sql_exec(ct, "select * from user;"),
sqlite:read(ct, user, {name, "abby"}), sqlite3:read(ct, user, {name, "abby"}),
sqlite:delete(ct, user, {name, "abby"}), sqlite3:delete(ct, user, {name, "abby"}),
sqlite:drop_table(ct, user), sqlite3:drop_table(ct, user),
%sqlite:delete_db(ct) %sqlite3:delete_db(ct)
sqlite:close(ct). sqlite3:close(ct).
% create, read, update, delete % create, read, update, delete
%%==================================================================== %%====================================================================
%% Internal functions %% Internal functions

View File

@@ -1,5 +1,5 @@
#!/usr/bin/env escript #!/usr/bin/env escript
%%! -smp enable -pa ebin %%! -smp enable -pa ebin -name testsqlite3
main(_) -> main(_) ->
sqlite3_store:start_link(). ct:run("src").