qLibc
qdatabase.c File Reference

Database wrapper. More...

Go to the source code of this file.

Functions

qdb_t * qdb (const char *dbtype, const char *addr, int port, const char *username, const char *password, const char *database, bool autocommit)
 Initialize the internal database connector structure.
static bool open_ (qdb_t *db)
 qdb->open(): Connect to database server
static bool close_ (qdb_t *db)
 qdb->close(): Disconnect from database server
static int execute_update (qdb_t *db, const char *query)
 qdb->execute_update(): Executes the update DML
static int execute_updatef (qdb_t *db, const char *format,...)
 qdb->execute_updatef(): Executes the formatted update DML
static qdbresult_t * execute_query (qdb_t *db, const char *query)
 qdb->execute_query(): Execute a query.
static qdbresult_t * execute_queryf (qdb_t *db, const char *format,...)
 qdb->execute_queryf(): Execute a formatted query.
static bool begin_tran (qdb_t *db)
 qdb->begin_tran(): Start transaction
static bool commit (qdb_t *db)
 qdb->commit(): Commit transaction
static bool rollback (qdb_t *db)
 qdb->rellback(): Roll-back and abort transaction
static bool set_fetchtype (qdb_t *db, bool fromdb)
 qdb->set_fetchtype(): Set result fetching type
static bool get_conn_status (qdb_t *db)
 qdb->get_conn_status(): Get last connection status
static bool ping (qdb_t *db)
 qdb->ping(): Checks whether the connection to the server is working.
static const char * get_error (qdb_t *db, unsigned int *errorno)
 qdb->get_error(): Get error number and message
static void free_ (qdb_t *db)
 qdb->free(): Free qdb_t structure
static const char * _resultGetStr (qdbresult_t *result, const char *field)
 qdbresult->get_str(): Get the result as a string by field name.
static const char * _resultGetStrAt (qdbresult_t *result, int idx)
 qdbresult->get_str_at(): Get the result as a string by column number.
static int _resultGetInt (qdbresult_t *result, const char *field)
 qdbresult->get_int(): Get the result as an integer by field name.
static int _resultGetIntAt (qdbresult_t *result, int idx)
 qdbresult->get_int_at(): Get the result as an integer by column number.
static bool _resultGetNext (qdbresult_t *result)
 qdbresult->get_next(): Retrieves the next row of a result set
static int result_get_cols (qdbresult_t *result)
 qdbresult->get_cols(): Get the number of columns in the result set
static int result_get_rows (qdbresult_t *result)
 qdbresult->get_rows(): Get the number of rows in the result set
static int result_get_row (qdbresult_t *result)
 qdbresult->get_row(): Get the current row number
static void result_free (qdbresult_t *result)
 qdbresult->free(): Free the result

Detailed Description

Database wrapper.

Database header files should be included prior to qlibcext.h in your source codes like below.

#include "mysql.h"
#include "qlibcext.h"
qlibc extension header file.
qdb_t *db = NULL;
qdbresult_t *result = NULL;
db = qdb("MYSQL", "dbhost.qdecoder.org", 3306,
"test", "secret", "sampledb", true);
if (db == NULL) {
printf("ERROR: Not supported database type.\n");
return -1;
}
// try to connect
if (db->open(db) == false) {
printf("WARNING: Can't connect to database.\n");
return -1;
}
// get results
result = db->execute_query(db, "SELECT name, population FROM City");
if (result != NULL) {
printf("COLS : %d , ROWS : %d\n",
result->get_cols(result), result->get_rows(result));
while (result->get_next(result) == true) {
char *pszName = result->get_str(result, "name");
int nPopulation = result->get_int(result, "population");
printf("Country : %s , Population : %d\n", pszName, nPopulation);
}
result->free(result);
}
// close connection
db->close(db);
// free db object
db->free(db);
qdb_t * qdb(const char *dbtype, const char *addr, int port, const char *username, const char *password, const char *database, bool autocommit)
Initialize the internal database connector structure.
Definition qdatabase.c:160

Definition in file qdatabase.c.

Function Documentation

◆ qdb()

qdb_t * qdb ( const char * dbtype,
const char * addr,
int port,
const char * username,
const char * password,
const char * database,
bool autocommit )

Initialize the internal database connector structure.

Parameters
dbtypedatabase server type. Currently only "MYSQL" is supported
addrIP address or FQDN
portport number
usernamedatabase username
passworddatabase password
databasedatabase name
autocommitset autocommit on when true, or off when false
Returns
qdb_t pointer on success, or NULL on failure.
qdb_t *db = qdb("MYSQL",
"dbhost.qdecoder.org", 3306, "test", "secret",
"sampledb", true);
if (db == NULL) {
printf("ERROR: Not supported database type.\n");
return -1;
}

Definition at line 160 of file qdatabase.c.

◆ open_()

bool open_ ( qdb_t * db)
static

qdb->open(): Connect to database server

Parameters
dbpointer to qdb_t object
Returns
true on success, otherwise false.

Definition at line 229 of file qdatabase.c.

◆ close_()

bool close_ ( qdb_t * db)
static

qdb->close(): Disconnect from database server

Parameters
dbpointer to qdb_t object
Returns
true on success, otherwise false.
Note
Unless you call qdb->free(), qdb_t object will keep the database information. So you can re-connect to database using qdb->open().

Definition at line 319 of file qdatabase.c.

◆ execute_update()

int execute_update ( qdb_t * db,
const char * query )
static

qdb->execute_update(): Executes the update DML

Parameters
dbpointer to qdb_t object
queryquery string
Returns
a number of affected rows

Definition at line 347 of file qdatabase.c.

◆ execute_updatef()

int execute_updatef ( qdb_t * db,
const char * format,
... )
static

qdb->execute_updatef(): Executes the formatted update DML

Parameters
dbpointer to qdb_t object
formatquery string format
Returns
a number of affected rows, otherwise returns -1

Definition at line 377 of file qdatabase.c.

◆ execute_query()

qdbresult_t * execute_query ( qdb_t * db,
const char * query )
static

qdb->execute_query(): Execute a query.

Parameters
dbpointer to qdb_t object
queryquery string
Returns
qdbresult_t pointer on success, or NULL on failure.

Definition at line 396 of file qdatabase.c.

◆ execute_queryf()

qdbresult_t * execute_queryf ( qdb_t * db,
const char * format,
... )
static

qdb->execute_queryf(): Execute a formatted query.

Parameters
dbpointer to qdb_t object
formatquery string format
Returns
qdbresult_t pointer on success, or NULL on failure.

Definition at line 452 of file qdatabase.c.

◆ begin_tran()

bool begin_tran ( qdb_t * db)
static

qdb->begin_tran(): Start transaction

Parameters
dbpointer to qdb_t object
Returns
true on success, otherwise false
db->begin_tran(db);
(... insert/update/delete ...)
db->commit(db);

Definition at line 475 of file qdatabase.c.

◆ commit()

bool commit ( qdb_t * db)
static

qdb->commit(): Commit transaction

Parameters
dbpointer to qdb_t object
Returns
true on success, otherwise false

Definition at line 505 of file qdatabase.c.

◆ rollback()

bool rollback ( qdb_t * db)
static

qdb->rellback(): Roll-back and abort transaction

Parameters
dbpointer to qdb_t object
Returns
true on success, otherwise false

Definition at line 530 of file qdatabase.c.

◆ set_fetchtype()

bool set_fetchtype ( qdb_t * db,
bool fromdb )
static

qdb->set_fetchtype(): Set result fetching type

Parameters
dbpointer to qdb_t object
fromdbfalse for storing the results to client (default mode), true for fetching directly from server,
Returns
true if successful otherwise false
Note
If qdb->set_fetchtype(db, true) is called, the results does not actually read into the client. Instead, each row must be retrieved individually by making calls to qdbresult->get_next(). This reads the result of a query directly from the server without storing it in local buffer, which is somewhat faster and uses much less memory than default behavior qdb->set_fetchtype(db, false).

Definition at line 565 of file qdatabase.c.

◆ get_conn_status()

bool get_conn_status ( qdb_t * db)
static

qdb->get_conn_status(): Get last connection status

Parameters
dbpointer to qdb_t object
Returns
true if the connection flag is set to alive, otherwise false
Note
This function just returns the connection status flag.

Definition at line 581 of file qdatabase.c.

◆ ping()

bool ping ( qdb_t * db)
static

qdb->ping(): Checks whether the connection to the server is working.

Parameters
dbpointer to qdb_t object
Returns
true if connection is alive, false if connection is not available and failed to reconnect
Note
If the connection has gone down, an attempt to reconnect.

Definition at line 598 of file qdatabase.c.

◆ get_error()

const char * get_error ( qdb_t * db,
unsigned int * errorno )
static

qdb->get_error(): Get error number and message

Parameters
dbpointer to qdb_t object
errornoif not NULL, error number will be stored
Returns
pointer to error message string
Note
Do not free returned error message

Definition at line 628 of file qdatabase.c.

◆ free_()

void free_ ( qdb_t * db)
static

qdb->free(): Free qdb_t structure

Parameters
dbpointer to qdb_t object

Definition at line 650 of file qdatabase.c.

◆ _resultGetStr()

const char * _resultGetStr ( qdbresult_t * result,
const char * field )
static

qdbresult->get_str(): Get the result as a string by field name.

Parameters
resultpointer to qdbresult_t
fieldcolumn name
Returns
string pointer on success, or NULL on failure.
Note
Do not free returned string.

Definition at line 681 of file qdatabase.c.

◆ _resultGetStrAt()

const char * _resultGetStrAt ( qdbresult_t * result,
int idx )
static

qdbresult->get_str_at(): Get the result as a string by column number.

Parameters
resultpointer to qdbresult_t
idxcolumn number (first column is 1)
Returns
string pointer on success, or NULL on failure.

Definition at line 708 of file qdatabase.c.

◆ _resultGetInt()

int _resultGetInt ( qdbresult_t * result,
const char * field )
static

qdbresult->get_int(): Get the result as an integer by field name.

Parameters
resultpointer to qdbresult_t
fieldcolumn name
Returns
an integer converted value.

Definition at line 731 of file qdatabase.c.

◆ _resultGetIntAt()

int _resultGetIntAt ( qdbresult_t * result,
int idx )
static

qdbresult->get_int_at(): Get the result as an integer by column number.

Parameters
resultpointer to qdbresult_t
idxcolumn number (first column is 1)
Returns
an integer converted value.

Definition at line 745 of file qdatabase.c.

◆ _resultGetNext()

bool _resultGetNext ( qdbresult_t * result)
static

qdbresult->get_next(): Retrieves the next row of a result set

Parameters
resultpointer to qdbresult_t
Returns
true if successful, false if no more rows are left

Definition at line 758 of file qdatabase.c.

◆ result_get_cols()

int result_get_cols ( qdbresult_t * result)
static

qdbresult->get_cols(): Get the number of columns in the result set

Parameters
resultpointer to qdbresult_t
Returns
the number of columns in the result set

Definition at line 778 of file qdatabase.c.

◆ result_get_rows()

int result_get_rows ( qdbresult_t * result)
static

qdbresult->get_rows(): Get the number of rows in the result set

Parameters
resultpointer to qdbresult_t
Returns
the number of rows in the result set

Definition at line 794 of file qdatabase.c.

◆ result_get_row()

int result_get_row ( qdbresult_t * result)
static

qdbresult->get_row(): Get the current row number

Parameters
resultpointer to qdbresult_t
Returns
current fetching row number of the result set
Note
This number is sequential counter which is started from 1.

Definition at line 813 of file qdatabase.c.

◆ result_free()

void result_free ( qdbresult_t * result)
static

qdbresult->free(): Free the result

Parameters
resultpointer to qdbresult_t

Definition at line 827 of file qdatabase.c.