root/vtcross/trunk/src/cbr.c @ 20

Revision 20, 5.7 KB (checked in by ahe, 16 years ago)

adding search statement

Line 
1//
2// Case-based reasoner
3//
4
5#include <stdlib.h>
6#include <string.h>
7#include <stdio.h>
8
9#include "cbr.h"
10
11struct cbr_s {
12    char filename[64];
13    char tablename[64];
14    char command[2048];
15    sqlite3 *db;
16    unsigned int num_columns;
17};
18
19
20// open database or create database is it does not exist
21int OpenDatabase(cbr _cbr){
22    int rc;
23    //sqlite3 **db;
24
25    printf("database name: %s\n", _cbr->filename);
26    //rc = sqlite3_open(_cbr->filename, db);
27    rc = sqlite3_open(_cbr->filename, &(_cbr->db));
28    if (rc) {
29        //fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(*db));
30        fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(_cbr->db));
31        sqlite3_close(_cbr->db);
32        exit(1);
33    } else{
34        printf("database opened.\n");
35    }
36    //_cbr->db = *db;
37    return rc;
38}
39
40
41//int cbr_callback(void *notUsed, int argc, char **argv, char **azColName)
42int callback(void *notUsed, int argc, char **argv, char **azColName){
43    int i;
44    for(i=0; i<argc; i++){
45        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
46    }
47    printf("\n");
48    return 0;
49}
50
51
52int ExecuteCommand(cbr _cbr){
53    int rc;
54    char *zErrMsg = 0;
55
56    printf("command: %s\n", _cbr->command);
57    rc = sqlite3_exec(_cbr->db, _cbr->command, callback, 0, &zErrMsg);
58    if( rc!=SQLITE_OK){
59        fprintf(stderr, "SQL error: %s\n", zErrMsg);
60        sqlite3_free(zErrMsg);
61    } else{
62        printf("command executed.\n");
63    }
64    return rc;
65}
66
67
68cbr cbr_create(char * _filename, char * _tablename, char * _cols[], unsigned int _len)
69{
70    // cbr is a pointer to struct cbr_s
71    cbr _cbr = (cbr) malloc(sizeof(struct cbr_s));
72
73    // create database
74
75    // copy filename
76    unsigned int i=0;
77    strcpy(_cbr->filename, _filename);
78
79    // execute create database command
80    // database handle
81    //_cbr->db = NULL;
82    OpenDatabase(_cbr);
83
84    // create table
85
86    // copy tablename
87    strcpy(_cbr->tablename, _tablename);
88
89    // number of columns in the table
90    _cbr->num_columns = _len;
91
92    // generate command
93    strcpy(_cbr->command, "CREATE TABLE ");
94    strcat(_cbr->command, _cbr->tablename);
95    strcat(_cbr->command, "(");
96    for (i=0; i<_cbr->num_columns; i++) {
97        strcat(_cbr->command, _cols[i]);
98        strcat(_cbr->command, " FLOAT");
99        if (i != _cbr->num_columns-1) // not last entry
100            strcat(_cbr->command, ", ");
101    }
102    strcat(_cbr->command, ");");
103
104    // execute create table command
105    ExecuteCommand(_cbr);
106
107    return _cbr;
108}
109
110void cbr_free(cbr _cbr)
111{
112    // delete table here
113    // delete table
114   
115    /*// generate command, delete all entries of a table
116    strcpy(_cbr->command, "delete from ");
117    strcat(_cbr->command, _cbr->tablename);*/
118
119    // generate command, remove a table with its content
120    strcpy(_cbr->command, "drop table ");
121    strcat(_cbr->command, _cbr->tablename);
122
123    // execute delete command
124    ExecuteCommand(_cbr);
125
126    // clean the database
127    strcpy(_cbr->command, "vacuum");
128    ExecuteCommand(_cbr);
129
130    free(_cbr);
131}
132
133void cbr_print(cbr _cbr)
134{
135    // generate commandi
136    strcpy(_cbr->command, "select ");
137    strcat(_cbr->command, _cbr->tablename);
138    strcat(_cbr->command, ".* from ");
139    strcat(_cbr->command, _cbr->tablename);
140    strcat(_cbr->command, ";");
141
142    // execute print (select all)  command
143    ExecuteCommand(_cbr);
144    //printf("database %s, table %s:\n", _cbr->filename, _cbr->tablename);
145}
146
147//static
148int cbr_callback(void *notUsed, int argc, char **argv, char **azColName)
149{
150    return 0;
151}
152
153const char * ops_str[] = {
154    "==", "!=", ">", ">=", "<", "<="};
155
156int cbr_search(
157    cbr _cbr,
158    char *_names[],
159    int * _ops,
160    float *_vals,
161    unsigned int _n,
162    float *_retvals)
163{
164    // set up statement
165
166    /*char* zErrMsg = NULL;
167    //sqlite3_exec(_cbr->db, argv[2], callback, NULL, &zErrMsg);
168    sqlite3_exec(_cbr->db, _cbr->command, cbr_callback, NULL, &zErrMsg);*/
169
170    // generate command
171    strcpy(_cbr->command, "select ");
172    strcat(_cbr->command, _cbr->tablename);
173    strcat(_cbr->command, ".* from ");
174    strcat(_cbr->command, _cbr->tablename);
175    strcat(_cbr->command, " where ");
176
177    unsigned int i;
178    char str_buffer[64];
179    for (i=0; i<_n; i++) {
180        // ensure valid ops value
181        if (_ops[i] < 0 || _ops[i] > 5) {
182            printf("error: cbr_search(), invalid ops id : %d\n", _ops[i]);
183            exit(1);
184        }
185
186        strcat(_cbr->command, _names[i]);
187        //strcat(_cbr->command, " ");
188        strcat(_cbr->command, ops_str[_ops[i]]);
189        sprintf(str_buffer, "%E", _vals[i]);
190        strcat(_cbr->command, str_buffer);
191
192        if (i<_n-1)
193            strcat(_cbr->command, " AND ");
194        else
195            strcat(_cbr->command, ";");
196    }
197
198    //printf("command: %s\n", _cbr->command);
199    ExecuteCommand(_cbr);
200
201    return 0;
202}
203
204int cbr_add_row(cbr _cbr, char *_cols[], float *_vals, unsigned int _len)
205{
206    unsigned int i;
207   
208    // generate command
209    printf("%s\n", _cbr->command);
210    strcpy(_cbr->command, "insert into ");
211    strcat(_cbr->command, _cbr->tablename);
212   
213    strcat(_cbr->command, " (");
214    for (i=0; i<_len; i++) {
215        strcat(_cbr->command, _cols[i]);
216        if (i != _cbr->num_columns-1) // not last entry
217            strcat(_cbr->command, ", ");
218    }
219    strcat(_cbr->command, ") ");
220
221    strcat(_cbr->command, " values(");
222    for (i=0; i<_len; i++) {
223        // ???? how to fill the values if _cbr->num_columns != _len
224        // assume = in the following
225        sprintf(_cbr->command, "%s%f", _cbr->command, _vals[i]);
226        if (i != _cbr->num_columns-1) // not last entry
227            strcat(_cbr->command, ", ");
228    }
229    strcat(_cbr->command, ");");
230   
231    // execute add command
232    ExecuteCommand(_cbr);
233
234    return 0;
235}
236
Note: See TracBrowser for help on using the browser.