// Copyright (c) 2003,2004,2005 Juraj Kolesár #include "mysql/mysql.h" #include "../sysdep.h" /* ABOUT {{{ */ /* Project TUCNIAK -its goal is to create package of mostly accounting programs used in SOHO -it is designed primary for Slovak (Czech) companies that buy and sell anything. It's primary outputs will be done according to slovak law, but in future we want to make these according to European Union standards -"tucniak" means "penguin" in slovak, but it's other meaning could be also "fat boy" tdb - Tucniak database is library for linking project to any SQL server. THIS IS MYSQL IMPLEMENTATION This solution makes project independent of concrete DB SQL server, because we use only ANSI SQL commands. Changing the SQL server is then depending only on porting tdb to concrete server. Now we are supporting only MySQL, but volunteers for creating support for other servers are welcome. /* }}} */ /* {{{ T_DB ******************************************************************** This object represents the connection to database (session) all other objects use this initialized object to access db */ #define __TDB_PARAM_LENGTH 20 struct tdb { MYSQL *connection; char host[__TDB_PARAM_LENGTH]; char database[__TDB_PARAM_LENGTH]; char user[__TDB_PARAM_LENGTH]; char password[__TDB_PARAM_LENGTH]; }; typedef struct tdb T_DB; /* }}} */ /* {{{ T_QS ******************************************************************** T_QS (Query Select, QueStion for Something,...) object is advenced representation of SQL SELECT command, which you can handle only by chaning some of the atributes and you don't have to create all command string from scratch. Parameter 'from' is necessary others are optional (for more see 'tdb_qs.h') tdb_qs contains set of functions that provide easier handling with SQL comands in upper layer libraries, so you don't have to write all SQL creating code again and again. */ #define __T_QS_PARAM_LENGTH 256 struct tdb_qs { char columns[__T_QS_PARAM_LENGTH]; //columns part of SELECT command char from[__T_QS_PARAM_LENGTH]; // FROM part of SELECT command char filter_static[__T_QS_PARAM_LENGTH]; char filter_dynamic[__T_QS_PARAM_LENGTH]; char order[__T_QS_PARAM_LENGTH]; char group[__T_QS_PARAM_LENGTH]; }; typedef struct tdb_qs T_QS; /* }}} */ /* {{{ T_CELL ****************************************************************** data object designed for handling one col of one row (table cell). */ #define T_CELL_VALUE_WIDTH 40 struct tdb_cell { char value[T_CELL_VALUE_WIDTH]; char name[40]; //name of the table column int width; //storage size char type; //data type }; typedef struct tdb_cell T_CELL; #define T_CELL_STR 0 #define T_CELL_DATE 1 /* }}} */ /* TDB uses glib dynamic structures (GList, GSList) to store data from DB */ /* {{{ T_CELLIST **************************************************************/ #define T_CELLIST GList /*list with data pointers referencing to T_CELL structure /* }}} */ /* {{{ T_COLS *****************************************************************/ #define T_COLS GSList /*single list of pointers to T_CELL usualy describing columns in table /* }}} */ /* {{{ T_ROW ******************************************************************/ #define T_ROW GSList /*single list of pointers to strings usualy containig row data in T_TABLE /* }}} */ /* {{{ T_TABLE ****************************************************************/ #define T_TABLE GList /*list of pointers to T_ROW creating 2-dimensional storage array (data table) /* }}} */ /* {{{ T_MATRIX ***************************************************************/ struct tdb_table { T_TABLE *table; MYSQL_RES *memory; }; typedef struct tdb_table T_MATRIX; /* T_MATRIX is local structure. Its member 'memory' could be changed according to used database. Main idea is to make TUCNIAK independent from db. Of course majority tdb_* procedures than have to be changed according to used db. But all 'upper layer' functions use only the 'T_TABLE' member that is independent from used db. /* }}} */ /* {{{1 ///MACROS///*/ /* {{{2 SET_TDB (T_DB, db, host, user, password) *******************************/ #define SET_TDB(TDB, M_DB, M_HOST, M_USER, M_PASSWORD) \ strcpy(TDB.database, M_DB); \ strcpy(TDB.host, M_HOST); \ strcpy(TDB.user, M_USER); \ strcpy(TDB.password, M_PASSWORD) /* }}}2 */ /* {{{2 SET_CELL (T_CELL, value, name, width) *********************************/ #define SET_CELL(cell, Mvalue, Mname, Mwidth) \ strcpy(cell.value, Mvalue); \ strcpy(cell.name, Mname); \ cell.width = Mwidth; \ cell.type = 0 /* }}}2 */ /* {{{2 FILL_CELLIST (T_CELLIST, cell_array, count, cursor) *******************/ #define FILL_CELLIST(cellist, cell_array, count, cursor) \ if (cellist != NULL) { \ g_list_free(cellist); \ cellist = NULL; \ } \ for (cursor=0; cursor < count; cursor++) \ cellist = g_list_append(cellist, &(cell_array[cursor])) /* }}}2 */ /* {{{2 FILL_COLS (T_COLS, cell_array, count, cursor) **************************/ #define FILL_COLS(cols, cell_array, count, cursor) \ if (cols != NULL) { \ g_slist_free(cols); \ cols = NULL; \ } \ for (cursor=0; cursor < count; cursor++) \ cols = g_slist_append(cols, &(cell_array[cursor])) /* }}}2 */ /* {{{2 INIT_QS (T_QS) *********************************************************/ #define INIT_QS(TQS) \ strcpy(TQS.columns, ""); \ strcpy(TQS.from, ""); \ strcpy(TQS.filter_static, ""); \ strcpy(TQS.filter_dynamic, ""); \ strcpy(TQS.order, ""); \ strcpy(TQS.group, "") /* }}}2 */ /* }}}1 */ /******************************************************************************/ #include "tdb_init.h" #include "tdb_cell.h" #include "tdb_qs.h" #include "tdb_query.h" #include "tdb_table.h"