Browse code

Header files split

Jan Janak authored on 24/07/2002 14:45:10
Showing 6 changed files
... ...
@@ -2,138 +2,14 @@
2 2
  * $Id$
3 3
  */
4 4
 
5
-#ifndef __DB_H__
6
-#define __DB_H__
5
+#ifndef DB_H
6
+#define DB_H
7 7
 
8
-#include <time.h>
9
-
10
-/*
11
- * Generic database interface
12
- */
13
-
14
-
15
-/* =================== db_key =============== */
16
-
17
-/*
18
- * Type of a database key (column)
19
- */
20
-typedef const char* db_key_t;
21
-
22
-
23
-/* =================== db_val =============== */
24
-
25
-/*
26
- * Accepted column types
27
- */
28
-typedef enum {
29
-	DB_INT,
30
-        DB_DOUBLE,
31
-	DB_STRING,
32
-	DB_DATETIME
33
-} db_type_t;
34
-
35
-
36
-/*
37
- * Column value structure
38
- */
39
-typedef struct {
40
-	db_type_t type;                  /* Type of the value */
41
-	int nul;                         /* Means that the column in database has no value */
42
-	union {
43
-		int          int_val;    /* integer value */
44
-		double       double_val; /* double value */
45
-		time_t       time_val;   /* unix time value */
46
-		const char*  string_val; /* NULL terminated string */
47
-	} val;                           /* union of all possible types */
48
-} db_val_t;
49
-
50
-
51
-/*
52
- * Useful macros for accessing attributes of db_val structure
53
- */
54
-
55
-/* Get value type */
56
-#define VAL_TYPE(dv)   ((dv)->type)
57
-
58
-/* Get null flag (means that value in dabase is null) */
59
-#define VAL_NULL(dv)   ((dv)->nul)
60
-
61
-/* Get integer value */
62
-#define VAL_INT(dv)    ((dv)->val.int_val)
63
-
64
-/* Get double value */
65
-#define VAL_DOUBLE(dv) ((dv)->val.double_val)
66
-
67
-/* Get time_t value */
68
-#define VAL_TIME(dv)   ((dv)->val.time_val)
69
-
70
-/* Get char* value */
71
-#define VAL_STRING(dv) ((dv)->val.string_val)
72
-
73
-
74
-/* ==================== db_con ======================= */
75
-
76
-/*
77
- * This structure represents a database connection
78
- * and pointer to this structure is used as a connection
79
- * handle
80
- */
81
-typedef struct {
82
-	char* table;     /* Default table to use */
83
-	void* con;       /* Mysql Connection */
84
-	void* res;       /* Result of previous operation */
85
-	void* row;       /* Actual row in the result */
86
-	int   connected; /* TRUE if connection is established */
87
-} db_con_t;
88
-
89
-
90
-/* ===================== db_row ====================== */
91
-
92
-/*
93
- * Structure holding result of query_table function (ie. table row)
94
- */
95
-typedef struct db_row {
96
-	db_val_t* values;  /* Columns in the row */
97
-	int n;             /* Number of columns in the row */
98
-} db_row_t;
99
-
100
-/* Useful macros for manipulating db_row structure attributes */
101
-
102
-/* Get row members */
103
-#define ROW_VALUES(rw) ((rw)->values)
104
-
105
-/* Get number of member in the row */
106
-#define ROW_N(rw)      ((rw)->n)
107
-
108
-
109
-/* ===================== db_res ====================== */
110
-
111
-typedef struct db_res {
112
-	struct {
113
-		db_key_t* names;   /* Column names */
114
-		db_type_t* types;  /* Column types */
115
-		int n;             /* Number of columns */
116
-	} col;
117
-	struct db_row* rows;       /* Rows */
118
-	int n;                     /* Number of rows */
119
-} db_res_t;
120
-
121
-/* Useful macros for manipulating db_res attributes */
122
-
123
-/* Column names */
124
-#define RES_NAMES(re) ((re)->col.names)
125
-
126
-/* Column types */
127
-#define RES_TYPES(re) ((re)->col.types)
128
-
129
-/* Number of columns */
130
-#define RES_COL_N(re) ((re)->col.n)
131
-
132
-/* Rows */
133
-#define RES_ROWS(re)  ((re)->rows)
134
-
135
-/* Number of rows */
136
-#define RES_ROW_N(re) ((re)->n)
8
+#include "db_key.h"
9
+#include "db_val.h"
10
+#include "db_con.h"
11
+#include "db_row.h"
12
+#include "db_res.h"
137 13
 
138 14
 
139 15
 /*
... ...
@@ -235,8 +111,6 @@ typedef struct db_func{
235 111
  * returns TRUE if everything went OK
236 112
  * FALSE otherwise
237 113
  */
238
-int bind_dbmod(void);
239
-
240 114
 
241 115
 extern db_func_t dbf;
242 116
 
... ...
@@ -249,5 +123,9 @@ extern db_func_t dbf;
249 123
 #define db_insert     (dbf.insert)
250 124
 #define db_delete     (dbf.delete)
251 125
 #define db_update     (dbf.update)
126
+
127
+
128
+int bind_dbmod(void);
252 129
  
130
+
253 131
 #endif
254 132
new file mode 100644
... ...
@@ -0,0 +1,34 @@
1
+/* 
2
+ * $Id$ 
3
+ */
4
+
5
+
6
+#ifndef DB_CON_H
7
+#define DB_CON_H
8
+
9
+
10
+/*
11
+ * This structure represents a database connection
12
+ * and pointer to this structure is used as a connection
13
+ * handle
14
+ */
15
+typedef struct {
16
+	char* table;    /* Default table to use */
17
+	void* con;      /* Mysql Connection */
18
+	void* res;      /* Result of previous operation */
19
+	void* row;      /* Actual row in the result */
20
+	int connected;
21
+} db_con_t;
22
+
23
+
24
+#define CON_CONNECTED(cn)  ((cn)->connected)
25
+#define CON_TABLE(cn)      ((cn)->table)
26
+#define CON_CONNECTION(cn) ((cn)->con)
27
+#define CON_RESULT(cn)     ((cn)->res)
28
+#define CON_ROW(cn)        ((cn)->row)
29
+
30
+
31
+int use_table(db_con_t* _h, const char* _t);
32
+
33
+
34
+#endif
0 35
new file mode 100644
... ...
@@ -0,0 +1,15 @@
1
+/* 
2
+ * $Id$ 
3
+ */
4
+
5
+#ifndef DB_KEY_H
6
+#define DB_KEY_H
7
+
8
+
9
+/*
10
+ * Type of a database key (column)
11
+ */
12
+typedef const char* db_key_t;
13
+
14
+
15
+#endif
0 16
new file mode 100644
... ...
@@ -0,0 +1,53 @@
1
+/* 
2
+ * $Id$ 
3
+ */
4
+
5
+#ifndef DB_RES_H
6
+#define DB_RES_H
7
+
8
+#include "db_row.h"
9
+#include "db_key.h"
10
+#include "db_val.h"
11
+#include "db_con.h"
12
+
13
+
14
+struct db_row;
15
+
16
+
17
+typedef struct db_res {
18
+	struct {
19
+		db_key_t* names;   /* Column names */
20
+		db_type_t* types;  /* Column types */
21
+		int n;             /* Number of columns */
22
+	} col;
23
+	struct db_row* rows;       /* Rows */
24
+	int n;                     /* Number of rows */
25
+} db_res_t;
26
+
27
+
28
+#define RES_NAMES(re) ((re)->col.names)
29
+#define RES_TYPES(re) ((re)->col.types)
30
+#define RES_COL_N(re) ((re)->col.n)
31
+#define RES_ROWS(re)  ((re)->rows)
32
+#define RES_ROW_N(re) ((re)->n)
33
+
34
+
35
+/*
36
+ * Create a new result structure 
37
+ */
38
+db_res_t* new_result(void);
39
+
40
+
41
+/*
42
+ * Fill the structure with data from database
43
+ */
44
+int convert_result(db_con_t* _h, db_res_t* _r);
45
+
46
+
47
+/*
48
+ * Free all memory allocated by the structure
49
+ */
50
+int free_result(db_res_t* _r);
51
+
52
+
53
+#endif
0 54
new file mode 100644
... ...
@@ -0,0 +1,32 @@
1
+/* 
2
+ * $Id$ 
3
+ */
4
+
5
+#ifndef DB_ROW_H
6
+#define DB_ROW_H
7
+
8
+#include "db_val.h"
9
+#include "db_con.h"
10
+#include "db_res.h"
11
+
12
+
13
+struct db_res;
14
+
15
+/*
16
+ * Structure holding result of query_table function (ie. table row)
17
+ */
18
+typedef struct db_row {
19
+	db_val_t* values;  /* Columns in the row */
20
+	int n;             /* Number of columns in the row */
21
+} db_row_t;
22
+
23
+
24
+#define ROW_VALUES(rw) ((rw)->values)
25
+#define ROW_N(rw)      ((rw)->n)
26
+
27
+
28
+int convert_row(db_con_t* _h, struct db_res* _res, db_row_t* _r);
29
+int free_row(db_row_t* _r);
30
+
31
+
32
+#endif
0 33
new file mode 100644
... ...
@@ -0,0 +1,61 @@
1
+/* 
2
+ * $Id$ 
3
+ */
4
+
5
+#ifndef DB_VAL_H
6
+#define DB_VAL_H
7
+
8
+#include <time.h>
9
+
10
+
11
+/*
12
+ * Accepted column types
13
+ */
14
+typedef enum {
15
+	DB_INT,
16
+        DB_DOUBLE,
17
+	DB_STRING,
18
+	DB_DATETIME
19
+} db_type_t;
20
+
21
+
22
+/*
23
+ * Column value structure
24
+ */
25
+typedef struct {
26
+	db_type_t type;                  /* Type of the value */
27
+	int nul;                         /* Means that the column in database has no value */
28
+	union {
29
+		int          int_val;    /* integer value */
30
+		double       double_val; /* double value */
31
+		time_t       time_val;   /* unix time value */
32
+		const char*  string_val; /* NULL terminated string */
33
+	} val;                           /* union of all possible types */
34
+} db_val_t;
35
+
36
+
37
+/*
38
+ * Useful macros for accessing attributes of db_val structure
39
+ */
40
+
41
+#define VAL_TYPE(dv)   ((dv)->type)
42
+#define VAL_NULL(dv)   ((dv)->nul)
43
+#define VAL_INT(dv)    ((dv)->val.int_val)
44
+#define VAL_DOUBLE(dv) ((dv)->val.double_val)
45
+#define VAL_TIME(dv)   ((dv)->val.time_val)
46
+#define VAL_STRING(dv) ((dv)->val.string_val)
47
+
48
+
49
+/*
50
+ * Convert string to given type
51
+ */
52
+int str2val(db_type_t _t, db_val_t* _v, const char* _s);
53
+
54
+
55
+/*
56
+ * Convert given type to string
57
+ */
58
+int val2str(db_val_t* _v, char* _s, int* _len);
59
+
60
+
61
+#endif