浏览代码

Header files split

Jan Janak 23 年之前
父节点
当前提交
c3f0d85a16
共有 6 个文件被更改,包括 206 次插入133 次删除
  1. 11 133
      db/db.h
  2. 34 0
      db/db_con.h
  3. 15 0
      db/db_key.h
  4. 53 0
      db/db_res.h
  5. 32 0
      db/db_row.h
  6. 61 0
      db/db_val.h

+ 11 - 133
db/db.h

@@ -2,138 +2,14 @@
  * $Id$
  */
 
-#ifndef __DB_H__
-#define __DB_H__
+#ifndef DB_H
+#define DB_H
 
-#include <time.h>
-
-/*
- * Generic database interface
- */
-
-
-/* =================== db_key =============== */
-
-/*
- * Type of a database key (column)
- */
-typedef const char* db_key_t;
-
-
-/* =================== db_val =============== */
-
-/*
- * Accepted column types
- */
-typedef enum {
-	DB_INT,
-        DB_DOUBLE,
-	DB_STRING,
-	DB_DATETIME
-} db_type_t;
-
-
-/*
- * Column value structure
- */
-typedef struct {
-	db_type_t type;                  /* Type of the value */
-	int nul;                         /* Means that the column in database has no value */
-	union {
-		int          int_val;    /* integer value */
-		double       double_val; /* double value */
-		time_t       time_val;   /* unix time value */
-		const char*  string_val; /* NULL terminated string */
-	} val;                           /* union of all possible types */
-} db_val_t;
-
-
-/*
- * Useful macros for accessing attributes of db_val structure
- */
-
-/* Get value type */
-#define VAL_TYPE(dv)   ((dv)->type)
-
-/* Get null flag (means that value in dabase is null) */
-#define VAL_NULL(dv)   ((dv)->nul)
-
-/* Get integer value */
-#define VAL_INT(dv)    ((dv)->val.int_val)
-
-/* Get double value */
-#define VAL_DOUBLE(dv) ((dv)->val.double_val)
-
-/* Get time_t value */
-#define VAL_TIME(dv)   ((dv)->val.time_val)
-
-/* Get char* value */
-#define VAL_STRING(dv) ((dv)->val.string_val)
-
-
-/* ==================== db_con ======================= */
-
-/*
- * This structure represents a database connection
- * and pointer to this structure is used as a connection
- * handle
- */
-typedef struct {
-	char* table;     /* Default table to use */
-	void* con;       /* Mysql Connection */
-	void* res;       /* Result of previous operation */
-	void* row;       /* Actual row in the result */
-	int   connected; /* TRUE if connection is established */
-} db_con_t;
-
-
-/* ===================== db_row ====================== */
-
-/*
- * Structure holding result of query_table function (ie. table row)
- */
-typedef struct db_row {
-	db_val_t* values;  /* Columns in the row */
-	int n;             /* Number of columns in the row */
-} db_row_t;
-
-/* Useful macros for manipulating db_row structure attributes */
-
-/* Get row members */
-#define ROW_VALUES(rw) ((rw)->values)
-
-/* Get number of member in the row */
-#define ROW_N(rw)      ((rw)->n)
-
-
-/* ===================== db_res ====================== */
-
-typedef struct db_res {
-	struct {
-		db_key_t* names;   /* Column names */
-		db_type_t* types;  /* Column types */
-		int n;             /* Number of columns */
-	} col;
-	struct db_row* rows;       /* Rows */
-	int n;                     /* Number of rows */
-} db_res_t;
-
-/* Useful macros for manipulating db_res attributes */
-
-/* Column names */
-#define RES_NAMES(re) ((re)->col.names)
-
-/* Column types */
-#define RES_TYPES(re) ((re)->col.types)
-
-/* Number of columns */
-#define RES_COL_N(re) ((re)->col.n)
-
-/* Rows */
-#define RES_ROWS(re)  ((re)->rows)
-
-/* Number of rows */
-#define RES_ROW_N(re) ((re)->n)
+#include "db_key.h"
+#include "db_val.h"
+#include "db_con.h"
+#include "db_row.h"
+#include "db_res.h"
 
 
 /*
@@ -235,8 +111,6 @@ typedef struct db_func{
  * returns TRUE if everything went OK
  * FALSE otherwise
  */
-int bind_dbmod(void);
-
 
 extern db_func_t dbf;
 
@@ -249,5 +123,9 @@ extern db_func_t dbf;
 #define db_insert     (dbf.insert)
 #define db_delete     (dbf.delete)
 #define db_update     (dbf.update)
+
+
+int bind_dbmod(void);
  
+
 #endif

+ 34 - 0
db/db_con.h

@@ -0,0 +1,34 @@
+/* 
+ * $Id$ 
+ */
+
+
+#ifndef DB_CON_H
+#define DB_CON_H
+
+
+/*
+ * This structure represents a database connection
+ * and pointer to this structure is used as a connection
+ * handle
+ */
+typedef struct {
+	char* table;    /* Default table to use */
+	void* con;      /* Mysql Connection */
+	void* res;      /* Result of previous operation */
+	void* row;      /* Actual row in the result */
+	int connected;
+} db_con_t;
+
+
+#define CON_CONNECTED(cn)  ((cn)->connected)
+#define CON_TABLE(cn)      ((cn)->table)
+#define CON_CONNECTION(cn) ((cn)->con)
+#define CON_RESULT(cn)     ((cn)->res)
+#define CON_ROW(cn)        ((cn)->row)
+
+
+int use_table(db_con_t* _h, const char* _t);
+
+
+#endif

+ 15 - 0
db/db_key.h

@@ -0,0 +1,15 @@
+/* 
+ * $Id$ 
+ */
+
+#ifndef DB_KEY_H
+#define DB_KEY_H
+
+
+/*
+ * Type of a database key (column)
+ */
+typedef const char* db_key_t;
+
+
+#endif

+ 53 - 0
db/db_res.h

@@ -0,0 +1,53 @@
+/* 
+ * $Id$ 
+ */
+
+#ifndef DB_RES_H
+#define DB_RES_H
+
+#include "db_row.h"
+#include "db_key.h"
+#include "db_val.h"
+#include "db_con.h"
+
+
+struct db_row;
+
+
+typedef struct db_res {
+	struct {
+		db_key_t* names;   /* Column names */
+		db_type_t* types;  /* Column types */
+		int n;             /* Number of columns */
+	} col;
+	struct db_row* rows;       /* Rows */
+	int n;                     /* Number of rows */
+} db_res_t;
+
+
+#define RES_NAMES(re) ((re)->col.names)
+#define RES_TYPES(re) ((re)->col.types)
+#define RES_COL_N(re) ((re)->col.n)
+#define RES_ROWS(re)  ((re)->rows)
+#define RES_ROW_N(re) ((re)->n)
+
+
+/*
+ * Create a new result structure 
+ */
+db_res_t* new_result(void);
+
+
+/*
+ * Fill the structure with data from database
+ */
+int convert_result(db_con_t* _h, db_res_t* _r);
+
+
+/*
+ * Free all memory allocated by the structure
+ */
+int free_result(db_res_t* _r);
+
+
+#endif

+ 32 - 0
db/db_row.h

@@ -0,0 +1,32 @@
+/* 
+ * $Id$ 
+ */
+
+#ifndef DB_ROW_H
+#define DB_ROW_H
+
+#include "db_val.h"
+#include "db_con.h"
+#include "db_res.h"
+
+
+struct db_res;
+
+/*
+ * Structure holding result of query_table function (ie. table row)
+ */
+typedef struct db_row {
+	db_val_t* values;  /* Columns in the row */
+	int n;             /* Number of columns in the row */
+} db_row_t;
+
+
+#define ROW_VALUES(rw) ((rw)->values)
+#define ROW_N(rw)      ((rw)->n)
+
+
+int convert_row(db_con_t* _h, struct db_res* _res, db_row_t* _r);
+int free_row(db_row_t* _r);
+
+
+#endif

+ 61 - 0
db/db_val.h

@@ -0,0 +1,61 @@
+/* 
+ * $Id$ 
+ */
+
+#ifndef DB_VAL_H
+#define DB_VAL_H
+
+#include <time.h>
+
+
+/*
+ * Accepted column types
+ */
+typedef enum {
+	DB_INT,
+        DB_DOUBLE,
+	DB_STRING,
+	DB_DATETIME
+} db_type_t;
+
+
+/*
+ * Column value structure
+ */
+typedef struct {
+	db_type_t type;                  /* Type of the value */
+	int nul;                         /* Means that the column in database has no value */
+	union {
+		int          int_val;    /* integer value */
+		double       double_val; /* double value */
+		time_t       time_val;   /* unix time value */
+		const char*  string_val; /* NULL terminated string */
+	} val;                           /* union of all possible types */
+} db_val_t;
+
+
+/*
+ * Useful macros for accessing attributes of db_val structure
+ */
+
+#define VAL_TYPE(dv)   ((dv)->type)
+#define VAL_NULL(dv)   ((dv)->nul)
+#define VAL_INT(dv)    ((dv)->val.int_val)
+#define VAL_DOUBLE(dv) ((dv)->val.double_val)
+#define VAL_TIME(dv)   ((dv)->val.time_val)
+#define VAL_STRING(dv) ((dv)->val.string_val)
+
+
+/*
+ * Convert string to given type
+ */
+int str2val(db_type_t _t, db_val_t* _v, const char* _s);
+
+
+/*
+ * Convert given type to string
+ */
+int val2str(db_val_t* _v, char* _s, int* _len);
+
+
+#endif