Removing dependency for Cache module in MMDB build
[csql.git] / src / storage / TableConfig.cxx
blob9a5852fdbd52f4253f5c698bd98652e94dfc819f
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
267 fp = fopen(Conf :: config.getTableConfigFile(),"r");
268 if(fp == NULL)
270 printError(ErrSysInit, "cachetable.conf file does not exist");
271 return OK;
273 conn.close();
275 char tablename[IDENTIFIER_LENGTH];
276 char condition[IDENTIFIER_LENGTH];
277 char fieldname[IDENTIFIER_LENGTH];
278 char field[IDENTIFIER_LENGTH];
279 char dsnName[IDENTIFIER_LENGTH];
280 int mode;
282 while(!feof(fp))
284 tablename[0] = '\0'; condition[0] = '\0';
285 fscanf(fp,"%d %s %s %s %s %s \n",&mode,tablename,fieldname,condition,field,dsnName);
287 if(strcmp(tableName,tablename)==0)
289 fclose(fp);
290 return OK;
293 fclose(fp);
294 return ErrNotExists;
298 DbRetVal TableConfig::isTableCached(char *tabName)
300 FILE *fp;
301 char tmpFileName[MAX_FILE_PATH_LEN];
302 Conf::config.readAllValues(os::getenv("CSQL_CONFIG_FILE"));
303 fp = fopen(Conf::config.getTableConfigFile(),"r");
304 if( fp == NULL ) {
305 printError(ErrSysInit, "csqltable.conf file does not exist");
306 return ErrSysInit;
308 char tablename[IDENTIFIER_LENGTH]; tablename[0]='\0';
309 char fieldname[IDENTIFIER_LENGTH];
310 char condition[IDENTIFIER_LENGTH];
311 char field[IDENTIFIER_LENGTH];
312 char dsnName[IDENTIFIER_LENGTH];
313 unsigned int tabMode = 0;
315 while(!feof(fp))
317 fscanf(fp, "%d %s %s %s %s %s \n", &tabMode, tablename,fieldname,
318 condition,field,dsnName);
319 if (strcmp (tablename, tabName) == 0) {
320 fclose(fp);
321 bool isCached = isTableCached(tabMode);
322 if (isCached) return OK;
323 else return ErrNotCached;
326 fclose(fp);
327 return ErrNotCached;
331 unsigned int TableConfig::getTableMode(char *tabname)
333 FILE *fp;
334 fp = fopen(Conf::config.getTableConfigFile(),"r");
335 if( fp == NULL ) {
336 printError(ErrSysInit, "cachetable.conf file does not exist");
337 fclose(fp);
338 return 0;
340 char tablename[IDENTIFIER_LENGTH]; tablename[0] = '\0';
341 char fieldname[IDENTIFIER_LENGTH];
342 char condition[IDENTIFIER_LENGTH];
343 char field[IDENTIFIER_LENGTH];
344 char dsnName[IDENTIFIER_LENGTH];
345 unsigned int tabMode=0;
346 while(!feof(fp)) {
347 fscanf(fp,"%d %s %s %s %s %s \n",&tabMode,tablename,fieldname,
348 fieldname,condition,dsnName);
349 if (0 == strcmp(tabname,tablename)) {
350 fclose(fp);
351 return tabMode;
354 fclose(fp);
355 return 0;
359 bool TableConfig::isFieldExist(char *fieldname)
361 char tmpfieldname[IDENTIFIER_LENGTH];
362 int i=0,j=0;
363 while(fieldlistVal[j]!=0)
365 if(fieldlistVal[j] != ',')
366 tmpfieldname[i++]=fieldlistVal[j++];
367 else
369 tmpfieldname[i]='\0';
370 if(strcmp(fieldname,tmpfieldname)==0)
371 return true;
372 else { i=0; j++; }
375 tmpfieldname[i]='\0';
376 if(strcmp(fieldname,tmpfieldname)==0)
377 return true;
378 else
379 return false;
381 DbRetVal TableConfig::CacheInfo(bool isTabPresent)
383 FILE *fp;
384 fp = fopen(Conf::config.getTableConfigFile(),"r");
385 if( fp == NULL ) {
386 printError(ErrSysInit, "cachetable.conf file does not exist");
387 fclose(fp);
388 return OK;
391 char tablename[IDENTIFIER_LENGTH];
392 char pkfield[IDENTIFIER_LENGTH];
393 char condition[IDENTIFIER_LENGTH];
394 char field[IDENTIFIER_LENGTH];
395 char dsnName[IDENTIFIER_LENGTH];
396 unsigned int mode = 0;
398 printf("\n=================================================================================================================\n");
399 printf("|Mode|\tTable Name\t|\tPrimary Key\t|\tCondition\t|\tField List\t|\tDSN\t|\n");
400 printf("=================================================================================================================\n");
401 while(!feof(fp))
403 fscanf(fp,"%d %s %s %s %s %s \n",&mode,tablename,pkfield,condition,field,dsnName);
404 if(!mode) {
405 printf("| Cached table does not exist. |\n");
406 printf("=================================================================================================================\n");
407 return OK;
410 if(isTabPresent)
412 if(strcmp(tableName,tablename)==0)
414 printf("|%2d |%16s\t|%16s\t|%16s\t|%16s\t|%10s\t|\n",mode,tablename,pkfield,getRealConditionFromFile(condition),field,dsnName);
415 printf("-----------------------------------------------------------------------------------------------------------------\n\n");
416 fclose(fp);
417 return OK;
420 else
423 printf("|%2d |%16s\t|%16s\t|%16s\t|%16s\t|%10s\t|\n",mode,tablename,pkfield,getRealConditionFromFile(condition),field,dsnName);
424 printf("-----------------------------------------------------------------------------------------------------------------\n");
427 printf("\n");
428 fclose(fp);
429 return OK;
432 DbRetVal TableConfig::getDsnAndTdb(char *dsn,char *newdsn, char *tdb)
434 char dsnId[IDENTIFIER_LENGTH]; dsnId[0]='\0';
435 char user[IDENTIFIER_LENGTH]; user[0] = '\0';
436 char passwd[IDENTIFIER_LENGTH]; passwd[0] = '\0';
437 char tdbname[IDENTIFIER_LENGTH]; tdbname[0]='\0';
438 FILE *fp=NULL;
439 bool isDSNExist=false;
440 fp = fopen(Conf :: config.getDsConfigFile(),"r");
441 if(fp==NULL)
443 printError(ErrSysInit, "csqlds.conf file does not exist");
444 return ErrSysInit;
446 while(!feof(fp)) {
447 fscanf(fp,"%s %s %s %s\n",dsnId,user,passwd,tdbname);
448 if(strcmp(dsnId,dsn)==0) {
449 if( strcmp(user,"NULL")!=0 && strcmp(passwd,"NULL")!=0) {
450 sprintf(newdsn,"DSN=%s;UID=%s;PWD=%s;",dsn,user,passwd);isDSNExist=true; break;
452 else
454 sprintf(newdsn,"DSN=%s;",dsn); isDSNExist=true;break;
459 fclose(fp);
460 if(isDSNExist)
462 strcpy(tdb, tdbname);
463 return OK;
465 else
467 printError(ErrNotExists,"dsn is not present in the csqlds.conf file\n");
468 return ErrNotExists;
472 //get the DSN for respective cached table.
473 //This function is used in "SqlGwStatement.cxx:prepare()"
474 DbRetVal TableConfig::getDsnForTable(char *tab, char *dsnname)
476 FILE *fp;
477 fp = fopen(Conf::config.getTableConfigFile(),"r");
478 if( fp == NULL){
479 printError(ErrSysInit, "cachetable.conf file does not exist");
480 fclose(fp);
481 return ErrOS;
483 char tablename[IDENTIFIER_LENGTH];tablename[0]='\0';
484 char pkfield[IDENTIFIER_LENGTH];pkfield[0]='\0';
485 char condition[IDENTIFIER_LENGTH];condition[0]='\0';
486 char field[IDENTIFIER_LENGTH];field[0]='\0';
487 char dsnName[IDENTIFIER_LENGTH];dsnName[0]='\0';
488 int mode;
489 char *dsn=NULL;
491 while(!feof(fp)){
492 fscanf(fp,"%d %s %s %s %s %s \n",&mode,tablename,pkfield,condition,field,dsnName);
493 if(strcmp(tab,tablename)==0){
494 strcpy(dsnname, dsnName);
495 fclose(fp);
496 return OK;
500 fclose(fp);
501 return ErrNotFound;