moving around sources
This commit is contained in:
@@ -1,4 +0,0 @@
|
|||||||
%%
|
|
||||||
%%
|
|
||||||
|
|
||||||
-record(edbc_column_info, {name, type, default, notnull, pk}).
|
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
[
|
[
|
||||||
{description, "sqlite nif interface"},
|
{description, "sqlite nif interface"},
|
||||||
{vsn, "1"},
|
{vsn, "1"},
|
||||||
{modules, [esqlite]},
|
{modules, [esqlite, esqlite_nif]},
|
||||||
{registered, []},
|
{registered, []},
|
||||||
{applications, [
|
{applications, [
|
||||||
kernel,
|
kernel,
|
||||||
|
|||||||
@@ -1,78 +0,0 @@
|
|||||||
%%
|
|
||||||
%% Generic database interface. Sort of...
|
|
||||||
%%
|
|
||||||
%%
|
|
||||||
|
|
||||||
-module(gen_db).
|
|
||||||
|
|
||||||
-export([behaviour_info/1]).
|
|
||||||
|
|
||||||
-export([open/2, close/1, run/2, run/3, execute/2, execute/3, commit/1, rollback/1, tables/1, describe_table/2, column_names/2]).
|
|
||||||
|
|
||||||
-include("edbc.hrl").
|
|
||||||
|
|
||||||
behaviour_info(callbacks) ->
|
|
||||||
[{open, 1},
|
|
||||||
{close, 1},
|
|
||||||
{commit, 1},
|
|
||||||
{rollback, 1},
|
|
||||||
{run, 3},
|
|
||||||
{execute, 3},
|
|
||||||
{tables, 1},
|
|
||||||
{describe_table, 2}];
|
|
||||||
behaviour_info(_Other) ->
|
|
||||||
undefined.
|
|
||||||
|
|
||||||
-record(connection, {driver, connection_data}).
|
|
||||||
|
|
||||||
%% @doc Open a connection to a new database
|
|
||||||
%%
|
|
||||||
open(Driver, Args) ->
|
|
||||||
{ok, ConnectionData} = Driver:open(Args),
|
|
||||||
{ok, #connection{driver=Driver, connection_data=ConnectionData}}.
|
|
||||||
|
|
||||||
%% @doc Close a database connection.
|
|
||||||
%%
|
|
||||||
close(#connection{driver=Driver, connection_data=Data}) ->
|
|
||||||
Driver:close(Data).
|
|
||||||
|
|
||||||
%% @doc Commit all changes
|
|
||||||
commit(#connection{driver=Driver, connection_data=Data}) ->
|
|
||||||
Driver:commit(Data).
|
|
||||||
|
|
||||||
%% @doc Rollback all changes
|
|
||||||
rollback(#connection{driver=Driver, connection_data=Data}) ->
|
|
||||||
Driver:rollback(Data).
|
|
||||||
|
|
||||||
%% @doc Return a list with tablenames...
|
|
||||||
tables(#connection{driver=Driver, connection_data=Data}) ->
|
|
||||||
Driver:tables(Data).
|
|
||||||
|
|
||||||
%% @doc
|
|
||||||
describe_table(TableName, #connection{driver=Driver, connection_data=Data}) ->
|
|
||||||
Driver:describe_table(TableName, Data).
|
|
||||||
|
|
||||||
%% @doc
|
|
||||||
column_names(TableName, Connection) ->
|
|
||||||
[Info#edbc_column_info.name || Info <- describe_table(TableName, Connection) ].
|
|
||||||
|
|
||||||
|
|
||||||
%% @doc Execute the statement, without returning results.
|
|
||||||
run(Sql, Connection) ->
|
|
||||||
run(Sql, [], Connection).
|
|
||||||
|
|
||||||
run(Sql, Params, #connection{driver=Driver, connection_data=Data}) ->
|
|
||||||
Driver:run(Sql, Params, Data).
|
|
||||||
|
|
||||||
%% @doc Execute the statement, return the result
|
|
||||||
execute(Sql, Connection) ->
|
|
||||||
execute(Sql, [], Connection).
|
|
||||||
|
|
||||||
execute(Sql, Params, #connection{driver=Driver, connection_data=Data}) ->
|
|
||||||
Driver:execute(Sql, Params, Data).
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,95 +0,0 @@
|
|||||||
%%
|
|
||||||
%% Exploring making a database driver for the generic gen_db interface
|
|
||||||
%%
|
|
||||||
|
|
||||||
-module(sqlite).
|
|
||||||
|
|
||||||
-include("edbc.hrl").
|
|
||||||
|
|
||||||
-behaviour(gen_db).
|
|
||||||
|
|
||||||
-export([open/1, run/3, execute/3, close/1, commit/1, rollback/1, tables/1, describe_table/2]).
|
|
||||||
|
|
||||||
%% @doc Open a database connection
|
|
||||||
%%
|
|
||||||
open([DatabaseName]) ->
|
|
||||||
{ok, C} = esqlite3:open(DatabaseName),
|
|
||||||
ok = esqlite3:exec(<<"BEGIN TRANSACTION;">>, C),
|
|
||||||
{ok, C}.
|
|
||||||
|
|
||||||
|
|
||||||
%% @doc Close the connection
|
|
||||||
%%
|
|
||||||
close(Connection) ->
|
|
||||||
ok = esqlite3:close(Connection).
|
|
||||||
|
|
||||||
%% @doc
|
|
||||||
%%
|
|
||||||
run(Sql, [], Connection) ->
|
|
||||||
case esqlite3:exec(Sql, Connection) of
|
|
||||||
{error, Error} -> {error, ?MODULE, Error};
|
|
||||||
ok -> ok
|
|
||||||
end;
|
|
||||||
run(Sql, Args, Connection) ->
|
|
||||||
case esqlite3:prepare(Sql, Connection) of
|
|
||||||
{error, Error} ->
|
|
||||||
{error, ?MODULE, Error};
|
|
||||||
{ok, Stmt} ->
|
|
||||||
case esqlite3:bind(Stmt, Args) of
|
|
||||||
{error, Error} -> {error, ?MODULE, Error};
|
|
||||||
ok ->
|
|
||||||
case esqlite3:fetchone(Stmt) of
|
|
||||||
{error, Error} -> {error, ?MODULE, Error};
|
|
||||||
_ -> ok
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% @doc Execute a query and return the results
|
|
||||||
%%
|
|
||||||
execute(Sql, [], Connection) ->
|
|
||||||
case esqlite3:prepare(Sql, Connection) of
|
|
||||||
{error, Error} ->
|
|
||||||
{error, ?MODULE, Error};
|
|
||||||
{ok, Stmt} ->
|
|
||||||
{ok, esqlite3:column_names(Stmt), esqlite3:fetchall(Stmt)}
|
|
||||||
end;
|
|
||||||
execute(Sql, Args, Connection) ->
|
|
||||||
case esqlite3:prepare(Sql, Connection) of
|
|
||||||
{error, Error} ->
|
|
||||||
{error, ?MODULE, Error};
|
|
||||||
{ok, Stmt} ->
|
|
||||||
case esqlite3:bind(Stmt, Args) of
|
|
||||||
ok ->
|
|
||||||
Names = esqlite3:column_names(Stmt),
|
|
||||||
Result = esqlite3:fetchall(Stmt),
|
|
||||||
{ok, Names, Result};
|
|
||||||
{error, Error} ->
|
|
||||||
{error, ?MODULE, Error}
|
|
||||||
end
|
|
||||||
end.
|
|
||||||
|
|
||||||
%%
|
|
||||||
commit(Connection) ->
|
|
||||||
ok = run(<<"COMMIT;">>, [], Connection).
|
|
||||||
|
|
||||||
%%
|
|
||||||
rollback(Connection) ->
|
|
||||||
ok = run(<<"ROLLBACK;">>, [], Connection).
|
|
||||||
|
|
||||||
%%
|
|
||||||
tables(Connection) ->
|
|
||||||
esqlite3:map(fun({TableName}) -> list_to_atom(TableName) end,
|
|
||||||
<<"SELECT name FROM sqlite_master WHERE type='table' ORDER by name;">>, Connection).
|
|
||||||
|
|
||||||
%%
|
|
||||||
describe_table(TableName, Connection) when is_atom(TableName) ->
|
|
||||||
esqlite3:map(fun({_Cid, ColumnName, ColumnType, NotNull, Default, PrimaryKey}) ->
|
|
||||||
#edbc_column_info{name=list_to_atom(ColumnName),
|
|
||||||
type=ColumnType,
|
|
||||||
default=Default,
|
|
||||||
notnull=NotNull =/= 0,
|
|
||||||
pk=PrimaryKey =/= 0}
|
|
||||||
end,
|
|
||||||
[<<"PRAGMA table_info('">>, atom_to_list(TableName), <<"');">>], Connection).
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user