Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions dbi/dbi/dbi.fec
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,35 @@ class Dbi
*/
private string lastQuery;

/**
* @function constructor
* @declaration function constructor( string type, string username, string password,
* string hostname, string database )
* @brief Create a database connection.
* @param string type
* The database driver to use. (e.g. sqlite/sqlite3)
* @param string database
* The database to connect to. (filename)
* @param string dbdir
* The directory path root to resolve database filename.
* @param int timeout
* The sqlite3_timeout option to dbd-sqlite3 (in milliseconds)

* @description The constructor will return a null object if the connection to the database fails.
* You should check for this in your code.
*
*/

native function constructor( string type, string database, string dbdir, number timeout )
{
self->odata = ferite_dbi_connect_sq3( script, type, database, dbdir, timeout );
if( self->odata == NULL )
FE_RETURN_NULL_OBJECT;
{
FeriteVariable *recordQueryMetrics = ferite_object_get_var( script, self, "recordQueryMetrics" );
VAB(recordQueryMetrics) = FE_FALSE;
}
}

/**
* @function constructor
Expand Down Expand Up @@ -193,6 +222,37 @@ class Dbi
FE_RETURN_VAR( obj );
}

/**
* @function connect
* @static
* @declaration function connect( string type, string username, string password,
* string hostname, number port, string database )
* @brief Static function. connect's a dbi object to a database.
* @return An object of the class dbi.
* @param string type
* The database driver to use. (e.g. sqlite/sqlite3)
* @param string database
* The database file to connect to.
* @param string dbdir
* The directory root of databases in ("/tmp" is ok, instead
* of "/tmp/")
*/

static native function connect( string type, string database, string dbdir,
number timeout )
{
DBIData *dbi = ferite_dbi_connect_sq3( script, type, database, dbdir, (int)timeout );
FeriteClass *cls = NULL;
FeriteVariable *obj = NULL;

if( dbi == NULL )
FE_RETURN_NULL_OBJECT;
cls = ferite_find_class( script, script->mainns, "Dbi" );
obj = ferite_build_object( script, cls );
VAO(obj)->odata = dbi;
FE_RETURN_VAR( obj );
}

/**
* @function disconnect
* @declaration function disconnect( )
Expand Down
34 changes: 34 additions & 0 deletions dbi/dbi/utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,40 @@
#include "utility.h"


DBIData *ferite_dbi_connect_sq3( FeriteScript *script, FeriteString *protocol,
FeriteString *database, FeriteString *dbdir,
int timeout )
{

DBIData *dbi = fmalloc( sizeof( DBIData ) );
dbi->connected = 0;
dbi->conn = NULL;
dbi->version = dbi_version( );
dbi->result_list = NULL;

if( ( dbi->conn = dbi_conn_new( protocol->data ) ) == NULL ) {
ferite_error(script, -1, "Failed to activate driver [%s] libdbi version: %s\n",
protocol->data, dbi->version );
ffree( dbi );
return NULL;
}

dbi_conn_set_option( dbi->conn, "dbname", database->data );
dbi_conn_set_option( dbi->conn, "sqlite3_dbdir", dbdir->data );

dbi_conn_set_option_numeric( dbi->conn, "sqlite3_timeout", timeout );

if ( dbi_conn_connect( dbi->conn ) != 0 ) {
const char *error_msg = NULL;
dbi_conn_error( dbi->conn, &error_msg );
ferite_error( script, 0, "Unable to connect to database: '%s'\n", error_msg );
ffree( dbi );
return NULL;
}
dbi->connected = 1;
return dbi;
}

DBIData *ferite_dbi_connect( FeriteScript *script, FeriteString *protocol,
FeriteString *username, FeriteString *password,
FeriteString *hostname, int port,
Expand Down
3 changes: 3 additions & 0 deletions dbi/dbi/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ struct llist
struct llist *next;
};

DBIData *ferite_dbi_connect_sq3( FeriteScript *script, FeriteString *protocol,
FeriteString *database, FeriteString *dbdir,
int timeout );
DBIData *ferite_dbi_connect( FeriteScript *script, FeriteString *protocol,
FeriteString *username, FeriteString *password,
FeriteString *hostname, int port, FeriteString *database );
Expand Down