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 internal 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(): Executes the query
 
static qdbresult_t * execute_queryf (qdb_t *db, const char *format,...)
 qdb->execute_queryf(): Executes the 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(): De-allocate qdb_t structure
 
static const char * _resultGetStr (qdbresult_t *result, const char *field)
 qdbresult->get_str(): Get the result as string by field name
 
static const char * _resultGetStrAt (qdbresult_t *result, int idx)
 qdbresult->get_str_at(): Get the result as string by column number
 
static int _resultGetInt (qdbresult_t *result, const char *field)
 qdbresult->get_int(): Get the result as integer by field name
 
static int _resultGetIntAt (qdbresult_t *result, int idx)
 qdbresult->get_int_at(): Get the result as 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(): De-allocate 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 internal connector structure.
Definition qdatabase.c:162

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 internal connector structure.

Parameters
dbtypedatabase server type. currently "MYSQL" is only supported
addrip or fqdn address.
portport number
usernamedatabase username
passworddatabase password
databasedatabase server type. currently "MYSQL" is only supported
autocommitsets autocommit mode on if autocommit is true, off if autocommit is false
Returns
a pointer of qdb_t object in case of successful, otherwise returns NULL.
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 162 of file qdatabase.c.

◆ open_()

static bool open_ ( qdb_t *  db)
static

qdb->open(): Connect to database server

Parameters
dba pointer of qdb_t object
Returns
true if successful, otherwise returns false.

Definition at line 232 of file qdatabase.c.

◆ close_()

static bool close_ ( qdb_t *  db)
static

qdb->close(): Disconnect from database server

Parameters
dba pointer of qdb_t object
Returns
true if successful, otherwise returns 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 323 of file qdatabase.c.

◆ execute_update()

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

qdb->execute_update(): Executes the update DML

Parameters
dba pointer of qdb_t object
queryquery string
Returns
a number of affected rows

Definition at line 352 of file qdatabase.c.

◆ execute_updatef()

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

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

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

Definition at line 383 of file qdatabase.c.

◆ execute_query()

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

qdb->execute_query(): Executes the query

Parameters
dba pointer of qdb_t object
queryquery string
Returns
a pointer of qdbresult_t if successful, otherwise returns NULL

Definition at line 403 of file qdatabase.c.

◆ execute_queryf()

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

qdb->execute_queryf(): Executes the formatted query

Parameters
dba pointer of qdb_t object
formatquery string format
Returns
a pointer of qdbresult_t if successful, otherwise returns NULL

Definition at line 460 of file qdatabase.c.

◆ begin_tran()

static bool begin_tran ( qdb_t *  db)
static

qdb->begin_tran(): Start transaction

Parameters
dba pointer of qdb_t object
Returns
true if successful, otherwise returns false
db->begin_tran(db);
(... insert/update/delete ...)
db->commit(db);

Definition at line 484 of file qdatabase.c.

◆ commit()

static bool commit ( qdb_t *  db)
static

qdb->commit(): Commit transaction

Parameters
dba pointer of qdb_t object
Returns
true if successful, otherwise returns false

Definition at line 515 of file qdatabase.c.

◆ rollback()

static bool rollback ( qdb_t *  db)
static

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

Parameters
dba pointer of qdb_t object
Returns
true if successful, otherwise returns false

Definition at line 541 of file qdatabase.c.

◆ set_fetchtype()

static bool set_fetchtype ( qdb_t *  db,
bool  fromdb 
)
static

qdb->set_fetchtype(): Set result fetching type

Parameters
dba pointer of qdb_t object
fromdbfalse for storing the results to client (default mode), true for fetching directly from server,
Returns
true if successful otherwise returns 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 577 of file qdatabase.c.

◆ get_conn_status()

static bool get_conn_status ( qdb_t *  db)
static

qdb->get_conn_status(): Get last connection status

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

Definition at line 594 of file qdatabase.c.

◆ ping()

static bool ping ( qdb_t *  db)
static

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

Parameters
dba pointer of 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 612 of file qdatabase.c.

◆ get_error()

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

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

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

Definition at line 643 of file qdatabase.c.

◆ free_()

static void free_ ( qdb_t *  db)
static

qdb->free(): De-allocate qdb_t structure

Parameters
dba pointer of qdb_t object

Definition at line 666 of file qdatabase.c.

◆ _resultGetStr()

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

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

Parameters
resulta pointer of qdbresult_t
fieldcolumn name
Returns
a string pointer if successful, otherwise returns NULL.
Note
Do not free returned string.

Definition at line 698 of file qdatabase.c.

◆ _resultGetStrAt()

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

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

Parameters
resulta pointer of qdbresult_t
idxcolumn number (first column is 1)
Returns
a string pointer if successful, otherwise returns NULL.

Definition at line 726 of file qdatabase.c.

◆ _resultGetInt()

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

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

Parameters
resulta pointer of qdbresult_t
fieldcolumn name
Returns
a integer converted value

Definition at line 750 of file qdatabase.c.

◆ _resultGetIntAt()

static int _resultGetIntAt ( qdbresult_t *  result,
int  idx 
)
static

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

Parameters
resulta pointer of qdbresult_t
idxcolumn number (first column is 1)
Returns
a integer converted value

Definition at line 765 of file qdatabase.c.

◆ _resultGetNext()

static bool _resultGetNext ( qdbresult_t *  result)
static

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

Parameters
resulta pointer of qdbresult_t
Returns
true if successful, false if no more rows are left

Definition at line 779 of file qdatabase.c.

◆ result_get_cols()

static int result_get_cols ( qdbresult_t *  result)
static

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

Parameters
resulta pointer of qdbresult_t
Returns
the number of columns in the result set

Definition at line 800 of file qdatabase.c.

◆ result_get_rows()

static int result_get_rows ( qdbresult_t *  result)
static

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

Parameters
resulta pointer of qdbresult_t
Returns
the number of rows in the result set

Definition at line 817 of file qdatabase.c.

◆ result_get_row()

static int result_get_row ( qdbresult_t *  result)
static

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

Parameters
resulta pointer of qdbresult_t
Returns
current fetching row number of the result set
Note
This number is sequencial counter which is started from 1.

Definition at line 837 of file qdatabase.c.

◆ result_free()

static void result_free ( qdbresult_t *  result)
static

qdbresult->free(): De-allocate the result

Parameters
resulta pointer of qdbresult_t

Definition at line 852 of file qdatabase.c.