fix for trie index
[csql.git] / src / storage / TableConfig.cxx
blob6169b71cdfddc8f915f350e2e985cbde723e0c0b
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #include<os.h>
17 #include<TableConfig.h>
18 #include<Util.h>
20 TableConfig TableConf::config;
22 char *TableConfig::getConditionVal(char *condition)
24 char str[124];
25 int i=0;
26 char *ptr, *ptr1 = str;
27 while(condition[i]!='\0')
29 if(condition[i] == ' ')
31 ptr = (condition+(i+1));
32 if(strncasecmp(ptr,"and ",4)==0) {
33 *ptr1='#';ptr1++; strncpy(ptr1,ptr,3); ptr1+=3;
34 *ptr1='#';ptr1++; i+=4;
36 else if(strncasecmp(ptr,"or ",3)==0) {
37 *ptr1='#';ptr1++;strncpy(ptr1,ptr,2); ptr1+=2;
38 *ptr1='#';ptr1++; i+=3;
40 else if(strncasecmp(ptr,"between ",8)==0) {
41 *ptr1='#';ptr1++;strncpy(ptr1,ptr,7); ptr1+=7;
42 *ptr1='#';ptr1++; i+=8;
44 else if(strncasecmp(ptr,"in ",3)==0) {
45 *ptr1='#'; ptr1++; strncpy(ptr1,ptr,2); ptr1+=2;
46 *ptr1='#';ptr1++; i+=3;
48 i++;
49 }else{
50 *ptr1 = condition[i++];
51 ptr1++;
54 *ptr1='\0';
55 strcpy(condition,str);
56 // printf("Condition %s\n",condition);
57 return condition;
60 char *TableConfig::getRealConditionFromFile(char *condition)
62 char str[124];
63 int i=0;
64 char *ptr = str;
65 while(condition[i]!='\0')
67 if(condition[i]=='#'){
68 *ptr=' ';
69 ptr++;i++;
70 }else{
71 *ptr=condition[i];
72 ptr++;
73 i++;
76 *ptr='\0';
77 strcpy(condition,str);
78 // printf("Condition %s\n",condition);
79 return condition;
82 DbRetVal TableConfig::addToCacheTableFile(bool isDirect)
84 FILE *fp;
85 fp = fopen(Conf::config.getTableConfigFile(),"a");
86 if( fp == NULL ) {
87 printError(ErrSysInit, "Invalid path/filename in TABLE_CONFIG_FILE.\nTable will not be recovered in case of crash");
88 return ErrSysInit;
90 unsigned int mode = 0;
91 bool isFldName = strcmp(fieldName,"") != 0;
92 bool isCondVal = strcmp(conditionVal,"") != 0;
93 bool isFldListVal = strcmp(fieldlistVal,"") != 0;
94 bool isDsn = strcmp(dsnName,"") != 0;
96 if (!isCondVal && !isFldListVal && !isDirect)
97 mode |= SIMPLE_CACHE;
98 if (isCondVal) mode |= CONDNL_CACHE;
99 if (isDirect) mode |= DIRECT_CACHE;
100 if (isFldListVal) mode |= FLDLVL_CACHE;
102 if (!isFldName) strcpy(fieldName,"NULL");
103 bool isCached = isTableCached(mode);
104 if (!isCached) strcpy(dsnName, "NULL");
105 else if (!isDsn && isCached) strcpy(dsnName, Conf::config.getDSN());
107 if (isCondVal && isFldListVal) {
108 fprintf(fp,"%d %s %s %s %s %s \n", mode, tableName, fieldName,
109 getConditionVal(conditionVal), fieldlistVal, dsnName);
111 else if (isCondVal && !isFldListVal) {
112 strcpy(fieldlistVal,"NULL");
113 fprintf(fp,"%d %s %s %s %s %s \n", mode, tableName, fieldName,
114 getConditionVal(conditionVal), fieldlistVal, dsnName);
116 else if (!isCondVal && isFldListVal) {
117 strcpy(conditionVal,"NULL");
118 fprintf(fp,"%d %s %s %s %s %s \n", mode, tableName, fieldName,
119 conditionVal, fieldlistVal, dsnName);
120 } else {
121 strcpy(fieldlistVal,"NULL");
122 strcpy(conditionVal,"NULL");
123 fprintf(fp,"%d %s %s %s %s %s \n", mode, tableName, fieldName,
124 conditionVal, fieldlistVal, dsnName);
126 fclose(fp);
127 return OK;
130 DbRetVal TableConfig::removeFromCacheTableFile()
132 FILE *fp, *tmpfp;
133 char tmpFileName[MAX_FILE_PATH_LEN];
134 sprintf(tmpFileName, "%s.tmp", Conf::config.getTableConfigFile());
135 tmpfp = fopen(tmpFileName,"w");
136 if( tmpfp == NULL ) {
137 printError(ErrSysInit, "Invalid path/filename in TABLE_CONFIG_FILE.\n");
138 return ErrSysInit;
140 fp = fopen(Conf::config.getTableConfigFile(),"r");
141 if( fp == NULL ) {
142 printError(ErrSysInit, "csqltable.conf file does not exist");
143 return ErrSysInit;
145 char tablename[IDENTIFIER_LENGTH];
146 char fieldname[IDENTIFIER_LENGTH];
147 char condition[IDENTIFIER_LENGTH];
148 char field[IDENTIFIER_LENGTH];
149 char dsnName[IDENTIFIER_LENGTH];
151 unsigned int mode;
152 while(!feof(fp))
154 fscanf(fp, "%d %s %s %s %s %s \n", &mode, tablename,fieldname,condition,field,dsnName);
155 if (strcmp (tablename, tableName) == 0) continue;
156 fprintf(tmpfp, "%d %s %s %s %s %s \n", mode, tablename,fieldname,condition,field,dsnName);
158 fclose(tmpfp);
159 fclose(fp);
160 char sysCommand[MAX_FILE_PATH_LEN * 2];
161 sprintf(sysCommand, "mv %s %s", tmpFileName, Conf::config.getTableConfigFile());
162 int ret = system(sysCommand);
163 if (ret != 0)
165 printError(ErrSysInit, "Check csqltable.conf file permission. unable to remove %s from file", tableName);
166 return ErrSysInit;
168 ret = unlink(tmpFileName);
169 return OK;
172 DbRetVal TableConfig::updateIntoCacheTableFile(bool upgrade, bool isDirect)
174 FILE *fp, *tmpfp;
175 char tmpFileName[MAX_FILE_PATH_LEN];
176 sprintf(tmpFileName, "%s.tmp", Conf::config.getTableConfigFile());
177 tmpfp = fopen(tmpFileName,"w");
178 if( tmpfp == NULL ) {
179 printError(ErrSysInit, "Invalid path/filename in TABLE_CONFIG_FILE.\n");
180 return ErrSysInit;
182 fp = fopen(Conf::config.getTableConfigFile(),"r+");
183 if( fp == NULL ) {
184 printError(ErrSysInit, "csqltable.conf file does not exist");
185 return ErrSysInit;
188 unsigned int mode = 0;
189 bool isFldName = strcmp(fieldName,"") != 0;
190 bool isCondVal = strcmp(conditionVal,"") != 0;
191 bool isFldListVal = strcmp(fieldlistVal,"") != 0;
192 bool isDsn = strcmp(dsnName,"") != 0;
194 char tablename[IDENTIFIER_LENGTH];
195 char fieldname[IDENTIFIER_LENGTH];
196 char condition[IDENTIFIER_LENGTH];
197 char field[IDENTIFIER_LENGTH];
198 char dsnName[IDENTIFIER_LENGTH];
200 char updtablename[IDENTIFIER_LENGTH];
201 char updfieldname[IDENTIFIER_LENGTH];
202 char updcondition[IDENTIFIER_LENGTH];
203 char updfield[IDENTIFIER_LENGTH];
204 char upddsnName[IDENTIFIER_LENGTH];
206 unsigned int tblMode = 0;
207 unsigned int modeToUpd = 0;
208 bool found = false;
209 //if (mode == 1 && upgrade) updMode = 8;
210 //else if (mode == 8 && !upgrade) updMode = -8;
211 //else if( mode == 1 && !upgrade) updMode = -1;
212 while(!feof(fp))
214 fscanf(fp, "%d %s %s %s %s %s \n", &tblMode, tablename,fieldname,condition,field,dsnName);
215 if (strcmp (tablename, tableName) == 0) {
216 modeToUpd = tblMode;
217 strcpy(updtablename, tablename);
218 strcpy(updfieldname, fieldname);
219 strcpy(updcondition, condition);
220 strcpy(updfield, field);
221 strcpy(upddsnName, dsnName);
222 //else strcpy(upddsnName, "NULL");
223 found = true;
224 continue;
226 fprintf(tmpfp, "%d %s %s %s %s %s \n", tblMode, tablename,fieldname,condition,field,dsnName);
228 if (found) {
229 bool isCached = isTableCached(modeToUpd);
230 if (isCached) {
231 unsetCacheMode(&modeToUpd);
232 strcpy(updfieldname, "NULL");
233 strcpy(updcondition, "NULL");
234 strcpy(updfield, "NULL");
235 strcpy(upddsnName, "NULL");
238 fprintf(tmpfp, "%d %s %s %s %s %s\n", modeToUpd, updtablename,
239 updfieldname, updcondition, updfield, upddsnName);
240 fclose(tmpfp);
241 fclose(fp);
242 char sysCommand[MAX_FILE_PATH_LEN * 2];
243 sprintf(sysCommand, "mv %s %s", tmpFileName,
244 Conf::config.getTableConfigFile());
245 int ret = system(sysCommand);
246 if (ret != 0)
248 printError(ErrSysInit, "Check csqltable.conf file permission. unable to remove %s from file", tableName);
249 return ErrSysInit;
251 } else { fclose (fp); fclose(tmpfp); }
252 unlink(tmpFileName);
253 return OK;
256 // new function is added by: Jitendra
257 DbRetVal TableConfig :: isTablePresent()
259 DbRetVal rv = OK;
260 FILE *fp;
261 Connection conn;
262 rv = conn.open(userName,password);
263 if(rv !=OK) return ErrSysInit;
264 // check for CACHE_TABLE variable
266 fp = fopen(Conf :: config.getTableConfigFile(),"r");
267 if(fp == NULL) {
268 printError(ErrSysInit, "csqltable.conf file does not exist");
269 return ErrNotExists;
271 conn.close();
273 char tablename[IDENTIFIER_LENGTH];
274 char condition[IDENTIFIER_LENGTH];
275 char fieldname[IDENTIFIER_LENGTH];
276 char field[IDENTIFIER_LENGTH];
277 char dsnName[IDENTIFIER_LENGTH];
278 int mode;
280 while(!feof(fp))
282 tablename[0] = '\0'; condition[0] = '\0';
283 fscanf(fp,"%d %s %s %s %s %s \n",&mode,tablename,fieldname,condition,field,dsnName);
284 if(strcmp(tableName,tablename)==0) {
285 fclose(fp);
286 return OK;
289 fclose(fp);
290 return ErrNotExists;
294 DbRetVal TableConfig::isTableCached(char *tabName)
296 FILE *fp;
297 char tmpFileName[MAX_FILE_PATH_LEN];
298 Conf::config.readAllValues(os::getenv("CSQL_CONFIG_FILE"));
299 fp = fopen(Conf::config.getTableConfigFile(),"r");
300 if( fp == NULL ) {
301 printError(ErrSysInit, "csqltable.conf file does not exist");
302 return ErrSysInit;
304 char tablename[IDENTIFIER_LENGTH]; tablename[0]='\0';
305 char fieldname[IDENTIFIER_LENGTH];
306 char condition[IDENTIFIER_LENGTH];
307 char field[IDENTIFIER_LENGTH];
308 char dsnName[IDENTIFIER_LENGTH];
309 unsigned int tabMode = 0;
311 while(!feof(fp))
313 fscanf(fp, "%d %s %s %s %s %s \n", &tabMode, tablename,fieldname,
314 condition,field,dsnName);
315 if (strcmp (tablename, tabName) == 0) {
316 fclose(fp);
317 bool isCached = isTableCached(tabMode);
318 if (isCached) return OK;
319 else return ErrNotCached;
322 fclose(fp);
323 return ErrNotCached;
327 unsigned int TableConfig::getTableMode(char *tabname)
329 FILE *fp;
330 fp = fopen(Conf::config.getTableConfigFile(),"r");
331 if( fp == NULL ) {
332 printError(ErrSysInit, "csqltable.conf file does not exist");
333 fclose(fp);
334 return 0;
336 char tablename[IDENTIFIER_LENGTH]; tablename[0] = '\0';
337 char fieldname[IDENTIFIER_LENGTH];
338 char condition[IDENTIFIER_LENGTH];
339 char field[IDENTIFIER_LENGTH];
340 char dsnName[IDENTIFIER_LENGTH];
341 unsigned int tabMode=0;
342 while(!feof(fp)) {
343 fscanf(fp,"%d %s %s %s %s %s \n",&tabMode,tablename,fieldname,
344 fieldname,condition,dsnName);
345 if (0 == strcmp(tabname,tablename)) {
346 fclose(fp);
347 return tabMode;
350 fclose(fp);
351 return 0;
355 bool TableConfig::isFieldExist(char *fieldname)
357 char tmpfieldname[IDENTIFIER_LENGTH];
358 int i=0,j=0;
359 while(fieldlistVal[j]!=0)
361 if(fieldlistVal[j] != ',')
362 tmpfieldname[i++]=fieldlistVal[j++];
363 else
365 tmpfieldname[i]='\0';
366 if(strcmp(fieldname,tmpfieldname)==0)
367 return true;
368 else { i=0; j++; }
371 tmpfieldname[i]='\0';
372 if(strcmp(fieldname,tmpfieldname)==0)
373 return true;
374 else
375 return false;
377 DbRetVal TableConfig::CacheInfo(bool isTabPresent)
379 FILE *fp;
380 fp = fopen(Conf::config.getTableConfigFile(),"r");
381 if( fp == NULL ) {
382 printError(ErrSysInit, "csqltable.conf file does not exist");
383 fclose(fp);
384 return OK;
387 char tablename[IDENTIFIER_LENGTH];
388 char pkfield[IDENTIFIER_LENGTH];
389 char condition[IDENTIFIER_LENGTH];
390 char field[IDENTIFIER_LENGTH];
391 char dsnName[IDENTIFIER_LENGTH];
392 unsigned int mode = 0;
394 printf("\n=================================================================================================================\n");
395 printf("|Mode|\tTable Name\t|\tPrimary Key\t|\tCondition\t|\tField List\t|\tDSN\t|\n");
396 printf("=================================================================================================================\n");
397 while(!feof(fp))
399 fscanf(fp,"%d %s %s %s %s %s \n",&mode,tablename,pkfield,condition,field,dsnName);
400 if(!mode) {
401 printf("| Cached table does not exist. |\n");
402 printf("=================================================================================================================\n");
403 return OK;
406 if(isTabPresent)
408 if(strcmp(tableName,tablename)==0)
410 printf("|%2d |%16s\t|%16s\t|%16s\t|%16s\t|%10s\t|\n",mode,tablename,pkfield,getRealConditionFromFile(condition),field,dsnName);
411 printf("-----------------------------------------------------------------------------------------------------------------\n\n");
412 fclose(fp);
413 return OK;
416 else
419 printf("|%2d |%16s\t|%16s\t|%16s\t|%16s\t|%10s\t|\n",mode,tablename,pkfield,getRealConditionFromFile(condition),field,dsnName);
420 printf("-----------------------------------------------------------------------------------------------------------------\n");
423 printf("\n");
424 fclose(fp);
425 return OK;
428 DbRetVal TableConfig::getDsnAndTdb(char *dsn,char *newdsn, char *tdb)
430 char dsnId[IDENTIFIER_LENGTH]; dsnId[0]='\0';
431 char user[IDENTIFIER_LENGTH]; user[0] = '\0';
432 char passwd[IDENTIFIER_LENGTH]; passwd[0] = '\0';
433 char tdbname[IDENTIFIER_LENGTH]; tdbname[0]='\0';
434 FILE *fp=NULL;
435 bool isDSNExist=false;
436 fp = fopen(Conf :: config.getDsConfigFile(),"r");
437 if(fp==NULL)
439 printError(ErrSysInit, "csqlds.conf file does not exist");
440 return ErrSysInit;
442 while(!feof(fp)) {
443 fscanf(fp,"%s %s %s %s\n",dsnId,user,passwd,tdbname);
444 if(strcmp(dsnId,dsn)==0) {
445 if( strcmp(user,"NULL")!=0 && strcmp(passwd,"NULL")!=0) {
446 sprintf(newdsn,"DSN=%s;UID=%s;PWD=%s;",dsn,user,passwd);isDSNExist=true; break;
448 else
450 sprintf(newdsn,"DSN=%s;",dsn); isDSNExist=true;break;
455 fclose(fp);
456 if(isDSNExist)
458 strcpy(tdb, tdbname);
459 return OK;
461 else
463 printError(ErrNotExists,"dsn is not present in the csqlds.conf file\n");
464 return ErrNotExists;
468 //get the DSN for respective cached table.
469 //This function is used in "SqlGwStatement.cxx:prepare()"
470 DbRetVal TableConfig::getDsnForTable(char *tab, char *dsnname)
472 FILE *fp;
473 fp = fopen(Conf::config.getTableConfigFile(),"r");
474 if( fp == NULL){
475 printError(ErrSysInit, "csqltable.conf file does not exist");
476 fclose(fp);
477 return ErrOS;
479 char tablename[IDENTIFIER_LENGTH];tablename[0]='\0';
480 char pkfield[IDENTIFIER_LENGTH];pkfield[0]='\0';
481 char condition[IDENTIFIER_LENGTH];condition[0]='\0';
482 char field[IDENTIFIER_LENGTH];field[0]='\0';
483 char dsnName[IDENTIFIER_LENGTH];dsnName[0]='\0';
484 int mode;
485 char *dsn=NULL;
487 while(!feof(fp)){
488 fscanf(fp,"%d %s %s %s %s %s \n",&mode,tablename,pkfield,condition,field,dsnName);
489 if(strcmp(tab,tablename)==0){
490 strcpy(dsnname, dsnName);
491 fclose(fp);
492 return OK;
496 fclose(fp);
497 return ErrNotFound;
499 DbRetVal TableConfig::tablesOnDsn()
501 FILE *fp;
502 fp = fopen(Conf::config.getTableConfigFile(),"r");
503 if( fp == NULL){
504 printError(ErrSysInit, "csqltable.conf file does not exist");
505 fclose(fp);
506 return ErrOS;
508 char tablename[IDENTIFIER_LENGTH];tablename[0]='\0';
509 char pkfield[IDENTIFIER_LENGTH];pkfield[0]='\0';
510 char condition[IDENTIFIER_LENGTH];condition[0]='\0';
511 char field[IDENTIFIER_LENGTH];field[0]='\0';
512 char dsn[IDENTIFIER_LENGTH];dsn[0]='\0';
513 int mode;
514 int totalTables=0;
515 printf("==========================================\n");
516 printf("|The list of cached tables present in DS.|\n");
517 printf("==========================================\n");
518 while(!feof(fp)){
519 fscanf(fp,"%d %s %s %s %s %s \n",&mode,tablename,pkfield,condition,field,dsn);
520 if(strcmp(dsn,dsnName)==0){
521 printf("%s\n",tablename);
522 totalTables++;
525 if(totalTables==0){
526 fclose(fp);
527 return ErrNotFound;
529 fclose(fp);
530 return OK;