Changed open/close and prepare
This commit is contained in:
File diff suppressed because it is too large
Load Diff
195
c_src/queue.c
195
c_src/queue.c
@@ -1,195 +0,0 @@
|
||||
// This file is part of Emonk released under the MIT license.
|
||||
// See the LICENSE file for more information.
|
||||
|
||||
/* Adapted by: Maas-Maarten Zeeman <mmzeeman@xs4all.nl */
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "queue.h"
|
||||
|
||||
struct qitem_t
|
||||
{
|
||||
struct qitem_t* next;
|
||||
void* data;
|
||||
};
|
||||
|
||||
typedef struct qitem_t qitem;
|
||||
|
||||
struct queue_t
|
||||
{
|
||||
ErlNifMutex *lock;
|
||||
ErlNifCond *cond;
|
||||
qitem *head;
|
||||
qitem *tail;
|
||||
void *message;
|
||||
int length;
|
||||
};
|
||||
|
||||
queue *
|
||||
queue_create()
|
||||
{
|
||||
queue *ret;
|
||||
|
||||
ret = (queue *) enif_alloc(sizeof(struct queue_t));
|
||||
if(ret == NULL) goto error;
|
||||
|
||||
ret->lock = NULL;
|
||||
ret->cond = NULL;
|
||||
ret->head = NULL;
|
||||
ret->tail = NULL;
|
||||
ret->message = NULL;
|
||||
ret->length = 0;
|
||||
|
||||
ret->lock = enif_mutex_create("queue_lock");
|
||||
if(ret->lock == NULL) goto error;
|
||||
|
||||
ret->cond = enif_cond_create("queue_cond");
|
||||
if(ret->cond == NULL) goto error;
|
||||
|
||||
return ret;
|
||||
|
||||
error:
|
||||
if(ret->lock != NULL)
|
||||
enif_mutex_destroy(ret->lock);
|
||||
if(ret->cond != NULL)
|
||||
enif_cond_destroy(ret->cond);
|
||||
if(ret != NULL)
|
||||
enif_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void
|
||||
queue_destroy(queue *queue)
|
||||
{
|
||||
ErlNifMutex *lock;
|
||||
ErlNifCond *cond;
|
||||
int length;
|
||||
|
||||
enif_mutex_lock(queue->lock);
|
||||
lock = queue->lock;
|
||||
cond = queue->cond;
|
||||
length = queue->length;
|
||||
|
||||
queue->lock = NULL;
|
||||
queue->cond = NULL;
|
||||
queue->head = NULL;
|
||||
queue->tail = NULL;
|
||||
queue->length = -1;
|
||||
enif_mutex_unlock(lock);
|
||||
|
||||
assert(length == 0 && "Attempting to destroy a non-empty queue.");
|
||||
enif_cond_destroy(cond);
|
||||
enif_mutex_destroy(lock);
|
||||
enif_free(queue);
|
||||
}
|
||||
|
||||
int
|
||||
queue_has_item(queue *queue)
|
||||
{
|
||||
int ret;
|
||||
|
||||
enif_mutex_lock(queue->lock);
|
||||
ret = (queue->head != NULL);
|
||||
enif_mutex_unlock(queue->lock);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
queue_push(queue *queue, void *item)
|
||||
{
|
||||
qitem * entry = (qitem *) enif_alloc(sizeof(struct qitem_t));
|
||||
if(entry == NULL)
|
||||
return 0;
|
||||
|
||||
entry->data = item;
|
||||
entry->next = NULL;
|
||||
|
||||
enif_mutex_lock(queue->lock);
|
||||
|
||||
assert(queue->length >= 0 && "Invalid queue size at push");
|
||||
|
||||
if(queue->tail != NULL)
|
||||
queue->tail->next = entry;
|
||||
|
||||
queue->tail = entry;
|
||||
|
||||
if(queue->head == NULL)
|
||||
queue->head = queue->tail;
|
||||
|
||||
queue->length += 1;
|
||||
|
||||
enif_cond_signal(queue->cond);
|
||||
enif_mutex_unlock(queue->lock);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void*
|
||||
queue_pop(queue *queue)
|
||||
{
|
||||
qitem *entry;
|
||||
void* item;
|
||||
|
||||
enif_mutex_lock(queue->lock);
|
||||
|
||||
/* Wait for an item to become available.
|
||||
*/
|
||||
while(queue->head == NULL)
|
||||
enif_cond_wait(queue->cond, queue->lock);
|
||||
|
||||
assert(queue->length >= 0 && "Invalid queue size at pop.");
|
||||
|
||||
/* Woke up because queue->head != NULL
|
||||
* Remove the entry and return the payload.
|
||||
*/
|
||||
entry = queue->head;
|
||||
queue->head = entry->next;
|
||||
entry->next = NULL;
|
||||
|
||||
if(queue->head == NULL) {
|
||||
assert(queue->tail == entry && "Invalid queue state: Bad tail.");
|
||||
queue->tail = NULL;
|
||||
}
|
||||
|
||||
queue->length -= 1;
|
||||
|
||||
enif_mutex_unlock(queue->lock);
|
||||
|
||||
item = entry->data;
|
||||
enif_free(entry);
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
int
|
||||
queue_send(queue *queue, void *item)
|
||||
{
|
||||
enif_mutex_lock(queue->lock);
|
||||
assert(queue->message == NULL && "Attempting to send multiple messages.");
|
||||
queue->message = item;
|
||||
enif_cond_signal(queue->cond);
|
||||
enif_mutex_unlock(queue->lock);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void *
|
||||
queue_receive(queue *queue)
|
||||
{
|
||||
void *item;
|
||||
|
||||
enif_mutex_lock(queue->lock);
|
||||
|
||||
/* Wait for an item to become available.
|
||||
*/
|
||||
while(queue->message == NULL)
|
||||
enif_cond_wait(queue->cond, queue->lock);
|
||||
|
||||
item = queue->message;
|
||||
queue->message = NULL;
|
||||
|
||||
enif_mutex_unlock(queue->lock);
|
||||
|
||||
return item;
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
// This file is part of Emonk released under the MIT license.
|
||||
// See the LICENSE file for more information.
|
||||
|
||||
/* adapted by: Maas-Maarten Zeeman <mmzeeman@xs4all.nl */
|
||||
|
||||
#ifndef ESQLITE_QUEUE_H
|
||||
#define ESQLITE_QUEUE_H
|
||||
|
||||
#include "erl_nif.h"
|
||||
|
||||
typedef struct queue_t queue;
|
||||
|
||||
queue * queue_create();
|
||||
void queue_destroy(queue *queue);
|
||||
|
||||
int queue_has_item(queue *queue);
|
||||
|
||||
int queue_push(queue *queue, void* item);
|
||||
void* queue_pop(queue *queue);
|
||||
|
||||
int queue_send(queue *queue, void* item);
|
||||
void* queue_receive(queue *);
|
||||
|
||||
#endif
|
||||
@@ -1,5 +1,5 @@
|
||||
|
||||
NifSharedSources = ["c_src/esqlite3_nif.c", "c_src/queue.c"].
|
||||
NifSharedSources = ["c_src/esqlite3_nif.c"].
|
||||
NifStaticSources = NifSharedSources ++ ["c_src/sqlite3/sqlite3.c"].
|
||||
CFlagsDefault = "$CFLAGS -Os -DSQLITE_DQS=0 -DSQLITE_THREADSAFE=1 -DSQLITE_DEFAULT_MEMSTATUS=0 -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 -DSQLITE_LIKE_DOESNT_MATCH_BLOBS -DSQLITE_MAX_EXPR_DEPTH=0 -DSQLITE_OMIT_DEPRECATED -DSQLITE_OMIT_PROGRESS_CALLBACK -DSQLITE_USE_ALLOCA -DSQLITE_OMIT_AUTOINIT -DSQLITE_USE_URI -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_MATH_FUNCTIONS -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_GEOPOLY".
|
||||
DrvLdFlagsDefault = "-shared -lsqlite3".
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{application, esqlite,
|
||||
[
|
||||
{description, "sqlite nif interface"},
|
||||
{vsn, "0.7.3"},
|
||||
{vsn, "0.8.0"},
|
||||
{modules, [esqlite3, esqlite3_nif]},
|
||||
{registered, []},
|
||||
{licenses, ["Apache"]},
|
||||
|
||||
964
src/esqlite3.erl
964
src/esqlite3.erl
File diff suppressed because it is too large
Load Diff
@@ -21,34 +21,38 @@
|
||||
%% low-level exports
|
||||
-export([
|
||||
open/1,
|
||||
set_update_hook/4,
|
||||
exec/4,
|
||||
changes/3,
|
||||
insert/4,
|
||||
last_insert_rowid/3,
|
||||
get_autocommit/3,
|
||||
prepare/4,
|
||||
multi_step/5,
|
||||
reset/4,
|
||||
finalize/4,
|
||||
bind/5,
|
||||
column_names/4,
|
||||
column_types/4,
|
||||
backup_init/6,
|
||||
backup_step/5,
|
||||
backup_remaining/4,
|
||||
backup_pagecount/4,
|
||||
backup_finish/4,
|
||||
interrupt/1,
|
||||
close/3
|
||||
close/1,
|
||||
prepare/2,
|
||||
|
||||
interrupt/1
|
||||
% set_update_hook/4,
|
||||
% exec/4,
|
||||
% changes/3,
|
||||
% insert/4,
|
||||
% last_insert_rowid/3,
|
||||
% get_autocommit/3,
|
||||
% multi_step/5,
|
||||
% reset/4,
|
||||
% finalize/4,
|
||||
% bind/5,
|
||||
|
||||
% column_names/4,
|
||||
% column_types/4,
|
||||
|
||||
% backup_init/6,
|
||||
% backup_step/5,
|
||||
% backup_remaining/4,
|
||||
% backup_pagecount/4,
|
||||
% backup_finish/4,
|
||||
]).
|
||||
|
||||
-type raw_connection() :: reference().
|
||||
-type raw_statement() :: reference().
|
||||
-type raw_backup() :: reference().
|
||||
-type esqlite3() :: reference().
|
||||
-type esqlite3_stmt() :: reference().
|
||||
%-type esqlite3_backup() :: reference().
|
||||
-type sql() :: iodata().
|
||||
|
||||
-export_type([raw_connection/0, raw_statement/0, raw_backup/0, sql/0]).
|
||||
-export_type([esqlite3/0, esqlite3_stmt/0, sql/0]).
|
||||
%-export_type([esqlite3_backup/0]).
|
||||
|
||||
-on_load(init/0).
|
||||
|
||||
@@ -64,15 +68,32 @@ init() ->
|
||||
%% @doc Open the specified sqlite3 database.
|
||||
%%
|
||||
-spec open(Filename) -> OpenResult
|
||||
when Filename :: string()
|
||||
OpenResult :: {ok, ref()} | {error, _}.
|
||||
when Filename :: string(),
|
||||
OpenResult :: {ok, esqlite3()} | {error, _}.
|
||||
open(_Filename) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
-spec set_update_hook(reference(), pid(), pid()) -> ok | {error, _}.
|
||||
set_update_hook(_Db, _Ref, _Dest, _Pid) ->
|
||||
%% @doc Close the connection.
|
||||
%%
|
||||
-spec close(Connection) -> CloseResult
|
||||
when Connection :: esqlite3(),
|
||||
CloseResult :: ok | {error, _}.
|
||||
close(_Db) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Compile a sql statement.
|
||||
%%
|
||||
-spec prepare(Connection, Sql) -> PrepareResult
|
||||
when Connection :: esqlite3(),
|
||||
Sql :: sql(),
|
||||
PrepareResult :: {ok, esqlite3_stmt()} | {error, _}.
|
||||
prepare(_Connection, _Sql) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
% -spec set_update_hook((), pid(), pid()) -> ok | {error, _}.
|
||||
%set_update_hook(_Db, _Ref, _Dest, _Pid) ->
|
||||
% erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Exec the query.
|
||||
%%
|
||||
%% Sends an asynchronous exec command over the connection and returns
|
||||
@@ -81,111 +102,99 @@ set_update_hook(_Db, _Ref, _Dest, _Pid) ->
|
||||
%% When the statement is executed Dest will receive message {Ref, answer()}
|
||||
%% with answer() integer | {error, reason()}
|
||||
%%
|
||||
-spec exec(raw_connection(), reference(), pid(), sql()) -> ok | {error, _}.
|
||||
exec(_Db, _Ref, _Dest, _Sql) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
%-spec exec(esqlite3(), reference(), pid(), sql()) -> ok | {error, _}.
|
||||
%%exec(_Db, _Ref, _Dest, _Sql) ->
|
||||
% erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Get the number of affected rows of last statement
|
||||
%%
|
||||
%% When the statement is executed Dest will receive message {Ref, answer()}
|
||||
%% with answer() integer | {error, reason()}
|
||||
-spec changes(raw_connection(), reference(), pid()) -> ok | {error, _}.
|
||||
changes(_Db, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
%-spec changes(esqlite3(), reference(), pid()) -> ok | {error, _}.
|
||||
%changes(_Db, _Ref, _Dest) ->
|
||||
% erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc
|
||||
%%
|
||||
-spec prepare(raw_connection(), reference(), pid(), sql()) -> ok | {error, _}.
|
||||
prepare(_Db, _Ref, _Dest, _Sql) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
%-spec multi_step(esqlite3(), esqlite3_stmt(), pos_integer(), reference(), pid()) -> ok | {error, _}.
|
||||
%multi_step(_Db, _Stmt, _Chunk_Size, _Ref, _Dest) ->
|
||||
% erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc
|
||||
%%
|
||||
-spec multi_step(raw_connection(), raw_statement(), pos_integer(), reference(), pid()) -> ok | {error, _}.
|
||||
multi_step(_Db, _Stmt, _Chunk_Size, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
%-spec reset(esqlite3(), esqlite3_stmt(), reference(), pid()) -> ok | {error, _}.
|
||||
%reset(_Db, _Stmt, _Ref, _Dest) ->
|
||||
% erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc
|
||||
%%
|
||||
-spec reset(raw_connection(), raw_statement(), reference(), pid()) -> ok | {error, _}.
|
||||
reset(_Db, _Stmt, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc
|
||||
%%
|
||||
-spec finalize(raw_connection(), raw_statement(), reference(), pid()) -> ok | {error, _}.
|
||||
finalize(_Db, _Stmt, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
%-spec finalize(esqlite3(), esqlite3_stmt(), reference(), pid()) -> ok | {error, _}.
|
||||
%finalize(_Db, _Stmt, _Ref, _Dest) ->
|
||||
% erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Bind parameters to a prepared statement.
|
||||
%%
|
||||
-spec bind(raw_connection(), raw_statement(), reference(), pid(), list(any())) -> ok | {error, _}.
|
||||
bind(_Db, _Stmt, _Ref, _Dest, _Args) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
%-spec bind(esqlite3(), esqlite3_stmt(), reference(), pid(), list(any())) -> ok | {error, _}.
|
||||
%bind(_Db, _Stmt, _Ref, _Dest, _Args) ->
|
||||
% erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Retrieve the column names of the prepared statement
|
||||
%%
|
||||
-spec column_names(raw_connection(), raw_statement(), reference(), pid()) -> ok | {error, _}.
|
||||
column_names(_Db, _Stmt, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
%-spec column_names(esqlite3(), esqlite3_stmt(), reference(), pid()) -> ok | {error, _}.
|
||||
%column_names(_Db, _Stmt, _Ref, _Dest) ->
|
||||
% erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Retrieve the column types of the prepared statement
|
||||
%%
|
||||
-spec column_types(raw_connection(), raw_statement(), reference(), pid()) -> ok | {error, _}.
|
||||
column_types(_Db, _Stmt, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
%-spec column_types(esqlite3(), esqlite3_stmt(), reference(), pid()) -> ok | {error, _}.
|
||||
%column_types(_Db, _Stmt, _Ref, _Dest) ->
|
||||
% erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Initialize a backup procedure of a database.
|
||||
-spec backup_init(raw_connection(), string(), raw_connection(), string(), reference(), pid()) -> ok | {error, _}.
|
||||
backup_init(_DestDb, _DestName, _SourceDb, _SourceName, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
%-spec backup_init(esqlite3(), string(), esqlite3_stmt(), string(), reference(), pid()) -> ok | {error, _}.
|
||||
%backup_init(_DestDb, _DestName, _SourceDb, _SourceName, _Ref, _Dest) ->
|
||||
% erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Do a backup step.
|
||||
-spec backup_step(raw_connection(), raw_backup(), integer(), reference(), pid()) -> ok | {error, _}.
|
||||
backup_step(_Db, _Backup, _NPages, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
%-spec backup_step(esqlite3(), esqlite3_backup(), integer(), reference(), pid()) -> ok | {error, _}.
|
||||
%backup_step(_Db, _Backup, _NPages, _Ref, _Dest) ->
|
||||
% erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Get the amount of remaining pages which need to be backed up.
|
||||
-spec backup_remaining(raw_connection(), raw_backup(), reference(), pid()) -> ok | {error, _}.
|
||||
backup_remaining(_Db, _Backup, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
%-spec backup_remaining(esqlite3(), esqlite3_backup(), reference(), pid()) -> ok | {error, _}.
|
||||
%backup_remaining(_Db, _Backup, _Ref, _Dest) ->
|
||||
% erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Get the total number of pages which need to be backed up.
|
||||
-spec backup_pagecount(raw_connection(), raw_backup(), reference(), pid()) -> ok | {error, _}.
|
||||
backup_pagecount(_Db, _Backup, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
%-spec backup_pagecount(esqlite3(), esqlite3_backup(), reference(), pid()) -> ok | {error, _}.
|
||||
%backup_pagecount(_Db, _Backup, _Ref, _Dest) ->
|
||||
% erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Finish the backup.
|
||||
-spec backup_finish(raw_connection(), raw_backup(), reference(), pid()) -> ok | {error, _}.
|
||||
backup_finish(_Db, _Backup, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
%-spec backup_finish(esqlite3(), esqlite3_backup(), reference(), pid()) -> ok | {error, _}.
|
||||
%backup_finish(_Db, _Backup, _Ref, _Dest) ->
|
||||
% erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Interrupt all active queries.
|
||||
-spec interrupt(raw_connection()) -> ok.
|
||||
-spec interrupt(esqlite3()) -> ok.
|
||||
interrupt(_Db) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Close the connection.
|
||||
%%
|
||||
-spec close(raw_connection(), reference(), pid()) -> ok | {error, _}.
|
||||
close(_Db, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Insert record
|
||||
%%
|
||||
-spec insert(raw_connection(), reference(), pid(), sql()) -> ok | {error, _}.
|
||||
insert(_Db, _Ref, _Dest, _Sql) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
%-spec insert(esqlite3(), reference(), pid(), sql()) -> ok | {error, _}.
|
||||
%insert(_Db, _Ref, _Dest, _Sql) ->
|
||||
% erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Get the last insert rowid.
|
||||
%%
|
||||
-spec last_insert_rowid(raw_connection(), reference(), pid()) -> ok | {error, _}.
|
||||
last_insert_rowid(_Db, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
%-spec last_insert_rowid(esqlite3(), reference(), pid()) -> ok | {error, _}.
|
||||
%last_insert_rowid(_Db, _Ref, _Dest) ->
|
||||
% erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Get automcommit
|
||||
%%
|
||||
-spec get_autocommit(raw_connection(), reference(), pid()) -> ok | {error, _}.
|
||||
get_autocommit(_Db, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
%-spec get_autocommit(esqlite3(), reference(), pid()) -> ok | {error, _}.
|
||||
%get_autocommit(_Db, _Ref, _Dest) ->
|
||||
% erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
|
||||
Reference in New Issue
Block a user