39 | | static cbr myCBR; |
40 | | |
| 34 | |
| 35 | class ossieCBR : public CBR |
| 36 | { |
| 37 | public: |
| 38 | ossieCBR(); |
| 39 | |
| 40 | ossieCBR(char * _filename, char * _tablename, char * _cols[], unsigned int _len); |
| 41 | |
| 42 | ~ossieCBR(){}; |
| 43 | |
| 44 | int32_t Update(char *_where[], char*_set[], float *_wherevals, float *_setvals, |
| 45 | unsigned int _wherelen, unsigned int _setlen); |
| 46 | |
| 47 | int32_t Search(char *_names[], int * _ops, float *_vals, unsigned int _n, |
| 48 | float *_retvals, int evf); |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | |
| 53 | ossieCBR::ossieCBR(char * _filename, char * _tablename, char * _cols[], unsigned int _len) |
| 54 | { |
| 55 | // create database |
| 56 | |
| 57 | // copy filename |
| 58 | unsigned int i=0; |
| 59 | strcpy(filename, _filename); |
| 60 | |
| 61 | // execute create database command |
| 62 | // database handle |
| 63 | //db = NULL; |
| 64 | OpenDatabase(); |
| 65 | |
| 66 | // create table |
| 67 | |
| 68 | // copy tablename |
| 69 | strcpy(tablename, _tablename); |
| 70 | |
| 71 | // number of columns in the table |
| 72 | numColumns = _len; |
| 73 | |
| 74 | // generate command |
| 75 | strcpy(command, "CREATE TABLE "); |
| 76 | strcat(command, tablename); |
| 77 | strcat(command, "("); |
| 78 | for (i=0; i<numColumns; i++) { |
| 79 | strcat(command, _cols[i]); |
| 80 | strcat(command, " FLOAT"); |
| 81 | if (i != numColumns-1) // not last entry |
| 82 | strcat(command, ", "); |
| 83 | } |
| 84 | strcat(command, ", timestamp DATE, PRIMARY KEY(tx_power));"); |
| 85 | |
| 86 | // execute create table command |
| 87 | ExecuteCommand(); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | |
| 92 | // cbr search |
| 93 | int32_t |
| 94 | ossieCBR::Search( |
| 95 | char *_names[], |
| 96 | int * _ops, |
| 97 | float *_vals, |
| 98 | unsigned int _n, |
| 99 | float *_retvals, |
| 100 | int evf) |
| 101 | { |
| 102 | int rc; |
| 103 | float lower_limit; |
| 104 | float upper_limit; |
| 105 | // generate command |
| 106 | strcpy(command, "select "); |
| 107 | strcat(command, tablename); |
| 108 | strcat(command, ".* from "); |
| 109 | strcat(command, tablename); |
| 110 | strcat(command, " where "); |
| 111 | |
| 112 | unsigned int i; |
| 113 | char str_buffer[64]; |
| 114 | //printf("number of ops %d:\n", _n); |
| 115 | for (i=0; i<_n; i++) { |
| 116 | // ensure valid ops value |
| 117 | if (_ops[i] < 0 || _ops[i] > 5) { |
| 118 | printf("error: cbr_search(), invalid ops id : %d\n", _ops[i]); |
| 119 | exit(1); |
| 120 | } |
| 121 | |
| 122 | strcat(command, _names[i]); |
| 123 | strcat(command, " between "); |
| 124 | |
| 125 | lower_limit = _vals[i]*(1-((float)evf/100)); |
| 126 | upper_limit = _vals[i]*(1+((float)evf/100)); |
| 127 | |
| 128 | printf("%f %f %f\n", lower_limit, upper_limit, _vals[i]); |
| 129 | |
| 130 | sprintf(command, "%s%f", command, lower_limit); |
| 131 | strcat(command, " and "); |
| 132 | sprintf(command, "%s%f", command, upper_limit); |
| 133 | |
| 134 | |
| 135 | if (i<_n-1) |
| 136 | strcat(command, " AND "); |
| 137 | else |
| 138 | strcat(command, " order by abs(utility) asc, timestamp desc;"); |
| 139 | } |
| 140 | |
| 141 | printf("search command: %s\n", command); |
| 142 | |
| 143 | rc = ExecuteSearchCommand(_retvals); |
| 144 | |
| 145 | printf("search result: "); |
| 146 | for (i=0; i<numColumns +1; i++) |
| 147 | printf("%f, ",_retvals[i]); |
| 148 | printf("\n"); |
| 149 | |
| 150 | return rc; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | // update a row |
| 155 | int32_t |
| 156 | ossieCBR::Update(char *_where[], char*_set[], float *_wherevals, float *_setvals, |
| 157 | uint32_t _wherelen, uint32_t _setlen) |
| 158 | { |
| 159 | unsigned int i; |
| 160 | |
| 161 | // generate command |
| 162 | //printf("%s\n", command); |
| 163 | strcpy(command, "UPDATE "); |
| 164 | strcat(command, tablename); |
| 165 | |
| 166 | strcat(command, " SET "); |
| 167 | for (i=0; i<_setlen; i++) { |
| 168 | strcat(command, _set[i]); |
| 169 | strcat(command, " = "); |
| 170 | sprintf(command, "%s%f", command, _setvals[i]); |
| 171 | strcat(command, " "); |
| 172 | if (i != _setlen-1) // not last entry |
| 173 | strcat(command, ", "); |
| 174 | } |
| 175 | |
| 176 | strcat(command, ", timestamp = DATETIME('NOW')"); |
| 177 | |
| 178 | strcat(command, " WHERE "); |
| 179 | |
| 180 | for (i=0; i<_wherelen; i++) { |
| 181 | strcat(command, _where[i]); |
| 182 | strcat(command, " = "); |
| 183 | sprintf(command, "%s%f", command, _wherevals[i]); |
| 184 | strcat(command, " "); |
| 185 | if (i != _wherelen-1) // not last entry |
| 186 | strcat(command, "AND "); |
| 187 | } |
| 188 | strcat(command, ";"); |
| 189 | |
| 190 | //printf("update command: %s\n", command); |
| 191 | // execute add command |
| 192 | ExecuteCommand(); |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static ossieCBR *myCBR; |