[16] | 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 | |
---|
| 11 | struct 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 | |
---|
[18] | 20 | // open database or create database is it does not exist |
---|
| 21 | int 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 | |
---|
[21] | 41 | //int cbr_callback(float *retvals, int argc, char **argv, char **azColName) |
---|
[18] | 42 | int 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 | |
---|
[21] | 52 | int SearchCallback(float *_retvals, int argc, char **argv, char **azColName){ |
---|
| 53 | int i; |
---|
| 54 | for(i=0; i<argc; i++){ |
---|
| 55 | printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); |
---|
| 56 | sscanf((argv[i] ? argv[i] : "NULL"), "%f", _retvals+i); |
---|
| 57 | } |
---|
| 58 | printf("\n"); |
---|
| 59 | return 0; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | |
---|
[18] | 64 | int ExecuteCommand(cbr _cbr){ |
---|
| 65 | int rc; |
---|
| 66 | char *zErrMsg = 0; |
---|
| 67 | |
---|
| 68 | printf("command: %s\n", _cbr->command); |
---|
| 69 | rc = sqlite3_exec(_cbr->db, _cbr->command, callback, 0, &zErrMsg); |
---|
| 70 | if( rc!=SQLITE_OK){ |
---|
| 71 | fprintf(stderr, "SQL error: %s\n", zErrMsg); |
---|
| 72 | sqlite3_free(zErrMsg); |
---|
| 73 | } else{ |
---|
| 74 | printf("command executed.\n"); |
---|
| 75 | } |
---|
| 76 | return rc; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | |
---|
[21] | 80 | int ExecuteSearchCommand(cbr _cbr, float *_retvals){ |
---|
| 81 | int rc; |
---|
| 82 | char *zErrMsg = 0; |
---|
| 83 | |
---|
| 84 | printf("command: %s\n", _cbr->command); |
---|
| 85 | rc = sqlite3_exec(_cbr->db, _cbr->command, SearchCallback, _retvals, &zErrMsg); |
---|
| 86 | //rc = sqlite3_exec(_cbr->db, _cbr->command, SearchCallback, 0, &zErrMsg); |
---|
| 87 | if( rc!=SQLITE_OK){ |
---|
| 88 | fprintf(stderr, "SQL error: %s\n", zErrMsg); |
---|
| 89 | sqlite3_free(zErrMsg); |
---|
| 90 | } else{ |
---|
| 91 | printf("command executed.\n"); |
---|
| 92 | } |
---|
| 93 | return rc; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | |
---|
[16] | 98 | cbr cbr_create(char * _filename, char * _tablename, char * _cols[], unsigned int _len) |
---|
| 99 | { |
---|
[18] | 100 | // cbr is a pointer to struct cbr_s |
---|
[16] | 101 | cbr _cbr = (cbr) malloc(sizeof(struct cbr_s)); |
---|
| 102 | |
---|
[18] | 103 | // create database |
---|
| 104 | |
---|
| 105 | // copy filename |
---|
| 106 | unsigned int i=0; |
---|
[17] | 107 | strcpy(_cbr->filename, _filename); |
---|
[18] | 108 | |
---|
| 109 | // execute create database command |
---|
| 110 | // database handle |
---|
| 111 | //_cbr->db = NULL; |
---|
| 112 | OpenDatabase(_cbr); |
---|
| 113 | |
---|
| 114 | // create table |
---|
| 115 | |
---|
| 116 | // copy tablename |
---|
[17] | 117 | strcpy(_cbr->tablename, _tablename); |
---|
[16] | 118 | |
---|
[18] | 119 | // number of columns in the table |
---|
[16] | 120 | _cbr->num_columns = _len; |
---|
| 121 | |
---|
[18] | 122 | // generate command |
---|
[16] | 123 | strcpy(_cbr->command, "CREATE TABLE "); |
---|
[18] | 124 | strcat(_cbr->command, _cbr->tablename); |
---|
[16] | 125 | strcat(_cbr->command, "("); |
---|
[18] | 126 | for (i=0; i<_cbr->num_columns; i++) { |
---|
[16] | 127 | strcat(_cbr->command, _cols[i]); |
---|
| 128 | strcat(_cbr->command, " FLOAT"); |
---|
[18] | 129 | if (i != _cbr->num_columns-1) // not last entry |
---|
[16] | 130 | strcat(_cbr->command, ", "); |
---|
| 131 | } |
---|
| 132 | strcat(_cbr->command, ");"); |
---|
| 133 | |
---|
[18] | 134 | // execute create table command |
---|
| 135 | ExecuteCommand(_cbr); |
---|
[16] | 136 | |
---|
| 137 | return _cbr; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | void cbr_free(cbr _cbr) |
---|
| 141 | { |
---|
| 142 | // delete table here |
---|
[18] | 143 | // delete table |
---|
| 144 | |
---|
| 145 | /*// generate command, delete all entries of a table |
---|
| 146 | strcpy(_cbr->command, "delete from "); |
---|
| 147 | strcat(_cbr->command, _cbr->tablename);*/ |
---|
[16] | 148 | |
---|
[18] | 149 | // generate command, remove a table with its content |
---|
| 150 | strcpy(_cbr->command, "drop table "); |
---|
| 151 | strcat(_cbr->command, _cbr->tablename); |
---|
| 152 | |
---|
| 153 | // execute delete command |
---|
| 154 | ExecuteCommand(_cbr); |
---|
| 155 | |
---|
[19] | 156 | // clean the database |
---|
| 157 | strcpy(_cbr->command, "vacuum"); |
---|
| 158 | ExecuteCommand(_cbr); |
---|
| 159 | |
---|
[16] | 160 | free(_cbr); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | void cbr_print(cbr _cbr) |
---|
| 164 | { |
---|
[19] | 165 | // generate commandi |
---|
| 166 | strcpy(_cbr->command, "select "); |
---|
[18] | 167 | strcat(_cbr->command, _cbr->tablename); |
---|
[19] | 168 | strcat(_cbr->command, ".* from "); |
---|
| 169 | strcat(_cbr->command, _cbr->tablename); |
---|
[18] | 170 | strcat(_cbr->command, ";"); |
---|
| 171 | |
---|
| 172 | // execute print (select all) command |
---|
| 173 | ExecuteCommand(_cbr); |
---|
| 174 | //printf("database %s, table %s:\n", _cbr->filename, _cbr->tablename); |
---|
[16] | 175 | } |
---|
| 176 | |
---|
| 177 | //static |
---|
| 178 | int cbr_callback(void *notUsed, int argc, char **argv, char **azColName) |
---|
| 179 | { |
---|
| 180 | return 0; |
---|
| 181 | } |
---|
| 182 | |
---|
[20] | 183 | const char * ops_str[] = { |
---|
| 184 | "==", "!=", ">", ">=", "<", "<="}; |
---|
| 185 | |
---|
[21] | 186 | |
---|
| 187 | // cbr search |
---|
[20] | 188 | int cbr_search( |
---|
| 189 | cbr _cbr, |
---|
| 190 | char *_names[], |
---|
| 191 | int * _ops, |
---|
| 192 | float *_vals, |
---|
| 193 | unsigned int _n, |
---|
| 194 | float *_retvals) |
---|
[16] | 195 | { |
---|
| 196 | // set up statement |
---|
| 197 | |
---|
[19] | 198 | // generate command |
---|
| 199 | strcpy(_cbr->command, "select "); |
---|
| 200 | strcat(_cbr->command, _cbr->tablename); |
---|
| 201 | strcat(_cbr->command, ".* from "); |
---|
| 202 | strcat(_cbr->command, _cbr->tablename); |
---|
| 203 | strcat(_cbr->command, " where "); |
---|
| 204 | |
---|
[20] | 205 | unsigned int i; |
---|
| 206 | char str_buffer[64]; |
---|
| 207 | for (i=0; i<_n; i++) { |
---|
| 208 | // ensure valid ops value |
---|
| 209 | if (_ops[i] < 0 || _ops[i] > 5) { |
---|
| 210 | printf("error: cbr_search(), invalid ops id : %d\n", _ops[i]); |
---|
| 211 | exit(1); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | strcat(_cbr->command, _names[i]); |
---|
| 215 | //strcat(_cbr->command, " "); |
---|
| 216 | strcat(_cbr->command, ops_str[_ops[i]]); |
---|
| 217 | sprintf(str_buffer, "%E", _vals[i]); |
---|
| 218 | strcat(_cbr->command, str_buffer); |
---|
| 219 | |
---|
| 220 | if (i<_n-1) |
---|
| 221 | strcat(_cbr->command, " AND "); |
---|
| 222 | else |
---|
| 223 | strcat(_cbr->command, ";"); |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | //printf("command: %s\n", _cbr->command); |
---|
| 227 | ExecuteCommand(_cbr); |
---|
[21] | 228 | ExecuteSearchCommand(_cbr, _retvals); |
---|
| 229 | printf("search result: "); |
---|
| 230 | for (i=0; i<_cbr->num_columns; i++) |
---|
| 231 | printf("%f, ",_retvals[i]); |
---|
| 232 | printf("\n"); |
---|
[20] | 233 | |
---|
[16] | 234 | return 0; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | int cbr_add_row(cbr _cbr, char *_cols[], float *_vals, unsigned int _len) |
---|
| 238 | { |
---|
[18] | 239 | unsigned int i; |
---|
| 240 | |
---|
| 241 | // generate command |
---|
| 242 | printf("%s\n", _cbr->command); |
---|
| 243 | strcpy(_cbr->command, "insert into "); |
---|
| 244 | strcat(_cbr->command, _cbr->tablename); |
---|
| 245 | |
---|
| 246 | strcat(_cbr->command, " ("); |
---|
| 247 | for (i=0; i<_len; i++) { |
---|
| 248 | strcat(_cbr->command, _cols[i]); |
---|
| 249 | if (i != _cbr->num_columns-1) // not last entry |
---|
| 250 | strcat(_cbr->command, ", "); |
---|
| 251 | } |
---|
| 252 | strcat(_cbr->command, ") "); |
---|
| 253 | |
---|
| 254 | strcat(_cbr->command, " values("); |
---|
| 255 | for (i=0; i<_len; i++) { |
---|
| 256 | // ???? how to fill the values if _cbr->num_columns != _len |
---|
| 257 | // assume = in the following |
---|
| 258 | sprintf(_cbr->command, "%s%f", _cbr->command, _vals[i]); |
---|
| 259 | if (i != _cbr->num_columns-1) // not last entry |
---|
| 260 | strcat(_cbr->command, ", "); |
---|
| 261 | } |
---|
| 262 | strcat(_cbr->command, ");"); |
---|
| 263 | |
---|
| 264 | // execute add command |
---|
| 265 | ExecuteCommand(_cbr); |
---|
| 266 | |
---|
[16] | 267 | return 0; |
---|
| 268 | } |
---|
| 269 | |
---|