perf optimization: changing strncpy to strcpy
[csql.git] / src / cache / CacheTableLoader.cxx
blob902ddc444315f9d2017fc4095a2decc625e2deef
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<CacheTableLoader.h>
17 #include<Util.h>
18 DbRetVal CacheTableLoader::addToCacheTableFile(bool isDirect)
20 FILE *fp;
21 fp = fopen(Conf::config.getTableConfigFile(),"a");
22 if( fp == NULL ) {
23 printError(ErrSysInit, "Invalid path/filename in TABLE_CONFIG_FILE.\nTable will not be"
24 "recovered in case of crash");
25 return ErrSysInit;
27 //TODO::if already table present in the file, it means that the
28 //table is replicated. in this case change mode from
29 //2 to 3 (repl to replcache)
30 if( strcmp(fieldName,"")==0 ){ strcpy(fieldName,"NULL"); }
31 if(isDirect)
33 if((strcmp(conditionVal,"")!=0)&&(strcmp(fieldlistVal,"")!=0))
35 fprintf(fp,"%d:%s %s %s %s\n",6,tableName,fieldName,conditionVal,fieldlistVal);
37 else if((strcmp(conditionVal,"")!=0)&&(strcmp(fieldlistVal,"")==0))
39 strcpy(fieldlistVal,"NULL");
40 fprintf(fp,"%d:%s %s %s %s\n",6,tableName,fieldName,conditionVal,fieldlistVal);
42 else if((strcmp(conditionVal,"")==0)&&(strcmp(fieldlistVal,"")!=0))
44 strcpy(conditionVal,"NULL");
45 fprintf(fp,"%d:%s %s %s %s\n",6,tableName,fieldName,conditionVal,fieldlistVal);
47 else
49 strcpy(fieldlistVal,"NULL");
50 strcpy(conditionVal,"NULL");
51 fprintf(fp,"%d:%s %s %s %s\n",5,tableName,fieldName,conditionVal,fieldlistVal);
54 else
56 if((strcmp(conditionVal,"")!=0)&&(strcmp(fieldlistVal,"")!=0))
58 fprintf(fp,"%d:%s %s %s %s\n",2,tableName,fieldName,conditionVal,fieldlistVal);
60 else if((strcmp(conditionVal,"")!=0)&&(strcmp(fieldlistVal,"")==0))
62 strcpy(fieldlistVal,"NULL");
63 fprintf(fp,"%d:%s %s %s %s\n",2,tableName,fieldName,conditionVal,fieldlistVal);
65 else if((strcmp(conditionVal,"")==0)&&(strcmp(fieldlistVal,"")!=0))
67 strcpy(conditionVal,"NULL");
68 fprintf(fp,"%d:%s %s %s %s\n",2,tableName,fieldName,conditionVal,fieldlistVal);
70 else
72 strcpy(fieldlistVal,"NULL");
73 strcpy(conditionVal,"NULL");
74 fprintf(fp,"%d:%s %s %s %s\n",1,tableName,fieldName,conditionVal,fieldlistVal);
78 fclose(fp);
79 return OK;
82 DbRetVal CacheTableLoader::removeFromCacheTableFile()
84 FILE *fp, *tmpfp;
85 char tmpFileName[MAX_FILE_PATH_LEN];
86 sprintf(tmpFileName, "%s.tmp", Conf::config.getTableConfigFile());
87 tmpfp = fopen(tmpFileName,"w");
88 if( tmpfp == NULL ) {
89 printError(ErrSysInit, "Invalid path/filename in TABLE_CONFIG_FILE.\n");
90 return ErrSysInit;
92 fp = fopen(Conf::config.getTableConfigFile(),"r");
93 if( fp == NULL ) {
94 printError(ErrSysInit, "csqltable.conf file does not exist");
95 return ErrSysInit;
97 char tablename[IDENTIFIER_LENGTH];
98 char fieldname[IDENTIFIER_LENGTH];
99 char condition[IDENTIFIER_LENGTH];
100 char field[IDENTIFIER_LENGTH];
101 int mode;
102 while(!feof(fp))
104 fscanf(fp, "%d:%s %s %s %s\n", &mode, tablename,fieldname,condition,field);
105 if (strcmp (tablename, tableName) == 0) continue;
106 fprintf(tmpfp, "%d:%s %s %s %s\n", mode, tablename,fieldname,condition,field);
108 fclose(tmpfp);
109 fclose(fp);
110 char sysCommand[MAX_FILE_PATH_LEN * 2];
111 sprintf(sysCommand, "mv %s %s", tmpFileName, Conf::config.getTableConfigFile());
112 int ret = system(sysCommand);
113 if (ret != 0)
115 printError(ErrSysInit, "Check csqltable.conf file permission. unable to remove %s from file", tableName);
116 return ErrSysInit;
118 return OK;
121 // new function is added by: Jitendra
122 DbRetVal CacheTableLoader :: isTablePresent()
124 DbRetVal rv = OK;
125 FILE *fp;
126 Connection conn;
127 rv = conn.open(userName,password);
128 if(rv !=OK) return ErrSysInit;
129 // check for CACHE_TABLE variable
132 fp = fopen(Conf :: config.getTableConfigFile(),"r");
133 if(fp == NULL)
135 printError(ErrSysInit, "cachetable.conf file does not exist");
136 return OK;
138 conn.close();
140 char tablename[IDENTIFIER_LENGTH];
141 char condition[IDENTIFIER_LENGTH];
142 char fieldname[IDENTIFIER_LENGTH];
143 char field[IDENTIFIER_LENGTH];
144 int mode;
146 while(!feof(fp))
148 tablename[0] = '\0'; condition[0] = '\0';
149 fscanf(fp,"%d:%s %s %s %s\n",&mode,tablename,fieldname,condition,field);
151 if(strcmp(tableName,tablename)==0)
153 fclose(fp);
154 return OK;
157 fclose(fp);
158 return ErrNotExists;
163 DbRetVal CacheTableLoader::load(bool tabDefinition)
165 Connection conn;
166 DbRetVal rv = conn.open(userName, password);
167 if (rv != OK) return ErrSysInit;
168 // check for CACHE_TABLE variable
171 DatabaseManager *dbMgr = (DatabaseManager*) conn.getDatabaseManager();
172 if (dbMgr == NULL) { printError(ErrSysInit, "Auth failed\n"); return ErrSysInit; }
173 if (tabDefinition == false) {
174 Table *tbl = dbMgr->openTable(tableName);
175 if (tbl == NULL) {
176 conn.close();
177 return ErrNotExists;
179 if (tbl->numTuples()) {
180 printError(ErrNotEmpty, "The table '\%s\' is not empty", tableName);
181 dbMgr->closeTable(tbl);
182 conn.close();
183 return ErrNotEmpty;
186 conn.startTransaction();
187 rv = load(dbMgr, tabDefinition);
188 conn.commit();
189 conn.close();
190 return rv;
193 DbRetVal CacheTableLoader::load(DatabaseManager *dbMgr, bool tabDefinition)
195 char dsn[72];
196 sprintf(dsn, "DSN=%s;", Conf::config.getDSN());
197 SQLCHAR outstr[1024];
198 SQLSMALLINT outstrlen;
199 DbRetVal rv = OK;
200 int retValue =0;
201 SQLHENV henv;
202 SQLHDBC hdbc;
203 SQLHSTMT hstmt;
204 retValue = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
205 if (retValue) {printError(ErrSysInit, "Unable to allocate ODBC handle \n"); return ErrSysInit; }
206 SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
207 retValue = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
208 if (retValue) {printError(ErrSysInit, "Unable to allocate ODBC handle \n"); return ErrSysInit; }
209 retValue = SQLDriverConnect(hdbc, NULL, (SQLCHAR*)dsn, SQL_NTS,
210 outstr, sizeof(outstr), &outstrlen,
211 SQL_DRIVER_NOPROMPT);
212 if (SQL_SUCCEEDED(retValue)) {
213 printDebug(DM_Gateway, "Connected to target database using dsn = %s\n", dsn);
214 } else {
215 fprintf(stderr, "Failed to connect to target database\n");
216 return ErrSysInit;
219 retValue=SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt);
220 if (retValue) {printError(ErrSysInit, "Unable to allocate ODBC handle \n"); return ErrSysInit; }
221 char stmtBuf[1024];
223 if(((strcmp(conditionVal,"")==0) || (strcmp(conditionVal,"NULL")==0)) && ((strcmp(fieldlistVal,"")==0) || (strcmp(fieldlistVal,"NULL")==0)))
225 sprintf(stmtBuf, "SELECT * FROM %s;", tableName);
227 else if(((strcmp(conditionVal,"")!=0) || (strcmp(conditionVal,"NULL")!=0)) && ((strcmp(fieldlistVal,"")==0) || (strcmp(fieldlistVal,"NULL")==0)))
229 sprintf(stmtBuf,"SELECT * FROM %s where %s;",tableName,conditionVal);
232 else if(((strcmp(conditionVal,"")==0) || (strcmp(conditionVal,"NULL")==0)) && ((strcmp(fieldlistVal,"")!=0) || (strcmp(fieldlistVal,"NULL")!=0)))
234 sprintf(stmtBuf,"SELECT %s FROM %s;",fieldlistVal,tableName);
236 else
237 sprintf(stmtBuf,"SELECT %s FROM %s where %s;",fieldlistVal,tableName,conditionVal);
239 retValue = SQLPrepare (hstmt, (unsigned char *) stmtBuf, SQL_NTS);
240 if (retValue) {printError(ErrSysInit, "Unable to Prepare ODBC statement \n"); return ErrSysInit; }
242 if (tabDefinition) {
243 short totalFields=0;
244 retValue = SQLNumResultCols (hstmt, &totalFields);
245 if (retValue) {printError(ErrSysInit, "Unable to retrieve ODBC total columns\n"); return ErrSysInit; }
246 UWORD icol;
247 UCHAR colName[IDENTIFIER_LENGTH];
248 SWORD colNameMax;
249 SWORD nameLength;
250 SWORD colType;
251 SQLULEN colLength;
252 SWORD scale;
253 SWORD nullable;
254 TableDef tabDef;
255 icol = 1; colNameMax = IDENTIFIER_LENGTH;
256 char columnname[IDENTIFIER_LENGTH];
257 char indexname[IDENTIFIER_LENGTH];
258 short type; short unique;
259 SQLHSTMT hstmtmeta;
260 retValue=SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmtmeta);
261 if (retValue)
263 printError(ErrSysInit, "Unable to allocate ODBC handle \n");
264 return ErrSysInit;
266 retValue=SQLPrimaryKeys(hstmtmeta, NULL, SQL_NTS, NULL, SQL_NTS, (SQLCHAR*) tableName, SQL_NTS);
267 retValue = SQLBindCol(hstmtmeta, 4, SQL_C_CHAR,columnname, 129,NULL);
268 HashIndexInitInfo *inf = new HashIndexInitInfo();
269 bool isPriIndex=false;
270 char indname[IDENTIFIER_LENGTH];
271 if(SQLFetch( hstmtmeta ) == SQL_SUCCESS)
273 inf->list.append(columnname);
274 while( SQLFetch( hstmtmeta ) == SQL_SUCCESS )
276 inf->list.append(columnname);
278 inf->indType = hashIndex;
279 inf->bucketSize = 10007;
280 inf->isUnique = true; inf->isPrimary = true;
281 strcpy(inf->tableName, tableName);
282 strcpy(indexname,"PRIMARY");
283 sprintf(indname, "%s_%s", tableName, indexname);
284 isPriIndex=true;
286 char *name = NULL;
287 bool iskeyfieldExist=true;
288 if(isPriIndex && (strcmp(fieldlistVal,"")!=0) && (strcmp(fieldlistVal,"NULL")!=0))
290 inf->list.resetIter();
291 while ((name=inf->list.nextFieldName())!=NULL)
293 iskeyfieldExist=isFieldExist(name);
294 if(!iskeyfieldExist) { break; }
296 }else if(!isPriIndex){iskeyfieldExist = false;}
297 if(((strcmp(fieldName,"")!=0) && (strcmp(fieldName,"NULL")!=0)) && !(isFieldExist(fieldName)))
299 if(Conf::config.useTwoWayCache() && (strcmp(fieldlistVal,"")!=0) && (strcmp(fieldlistVal,"NULL")!=0))
301 printError(ErrSysInit, "Bidirectonal caching fail for no primary key in %s \n", tableName);
302 return ErrSysInit;
306 if(!iskeyfieldExist && ((strcmp(fieldName,"")==0) || (strcmp(fieldName,"NULL")==0)))
308 if(Conf::config.useTwoWayCache())
310 printError(ErrSysInit, "Bidirectonal caching fail for no primary key in %s \n", tableName);
311 return ErrSysInit;
314 bool isKeyFld=false;
315 bool isNullfld=false;
316 while (icol <= totalFields) {
317 retValue = SQLDescribeCol(hstmt, icol, colName, colNameMax,
318 &nameLength, &colType, &colLength,
319 &scale, &nullable);
320 if (retValue) {printError(ErrSysInit, "Unable to retrieve ODBC column info\n"); return ErrSysInit; }
322 printDebug(DM_Gateway, "Describe Column %s %d %d \n", colName, colType, colLength);
323 icol++;
324 if(strcmp((char*)colName,fieldName)== 0)
326 isKeyFld=true;
327 isNullfld=true;
329 bool isPriFld=false;
330 if (nullable) {
331 inf->list.resetIter();
332 while ((name=inf->list.nextFieldName())!=NULL)
334 if(0==strcmp((char*)colName,name))
336 tabDef.addField((char*)colName, AllDataType::convertFromSQLType(colType), colLength +1, NULL, true);
337 isPriFld=true;
338 break;
341 if(!isPriFld) {
342 if(!isNullfld)
343 tabDef.addField((char*)colName, AllDataType::convertFromSQLType(colType), colLength +1);
344 else{
345 tabDef.addField((char*)colName, AllDataType::convertFromSQLType(colType), colLength +1, NULL, true);
346 isNullfld=false;
350 else
351 tabDef.addField((char*)colName, AllDataType::convertFromSQLType(colType), colLength +1, NULL, true);
353 if(((strcmp(fieldName,"")!=0) && (strcmp(fieldName,"NULL")!=0))&& !isKeyFld)
355 printError(ErrSysInit, "Unable to cache Table for %s with key field %s\n", tableName,fieldName);
356 SQLDisconnect(hdbc);
357 return ErrSysInit;
359 rv = dbMgr->createTable(tableName, tabDef);
360 if (rv != OK)
362 printError(ErrSysInit, "Table creation failed in csql for %s\n", tableName);
363 SQLDisconnect(hdbc);
364 return ErrSysInit;
366 if(isPriIndex ){
367 rv = dbMgr->createIndex(indname, inf);
368 if (rv != OK)
370 printError(ErrSysInit, "Index creation failed in csql for %s\n", tableName);
371 SQLDisconnect(hdbc);
372 return ErrSysInit;
375 else
377 if(Conf::config.useTwoWayCache() && iskeyfieldExist)
379 printError(ErrSysInit, "Bidirectonal caching fail for no primary key in %s \n", tableName);
380 return ErrSysInit;
383 retValue = SQLCloseCursor(hstmtmeta);
384 isKeyFld= false;
385 retValue = SQLStatistics(hstmtmeta, NULL, 0, NULL, SQL_NTS,
386 (SQLCHAR*) tableName, SQL_NTS, SQL_INDEX_ALL, SQL_QUICK);
387 retValue = SQLBindCol(hstmtmeta, 4, SQL_C_SHORT,
388 &unique, 2, NULL);
389 retValue = SQLBindCol(hstmtmeta, 6, SQL_C_CHAR,
390 indexname, 129, NULL);
391 retValue = SQLBindCol(hstmtmeta, 7, SQL_C_SHORT,
392 &type, 2, NULL);
393 retValue = SQLBindCol(hstmtmeta, 9, SQL_C_CHAR,
394 columnname, 129,NULL);
395 while ((retValue = SQLFetch(hstmtmeta)) == SQL_SUCCESS)
396 { //if (type != SQL_TABLE_STAT)
398 printDebug(DM_Gateway, "Column: %-18s Index Name: %-18s unique:%hd type:%hd\n",
399 columnname, indexname, unique, type);
401 if(0 == strcmp(columnname,fieldName)){isKeyFld=true;}
402 bool isPrimary=false;
403 inf->list.resetIter();
404 while ((name=inf->list.nextFieldName())!=NULL)
406 if(0==strcmp(columnname,name))
408 isPrimary=true;
409 break;
412 if(isPrimary){continue;}
413 if (type == 3) {
414 HashIndexInitInfo *info = new HashIndexInitInfo();
415 info->indType = hashIndex;
416 info->bucketSize = 10007;
417 info->list.append(columnname);
418 if (!unique) {info->isUnique = true; info->isPrimary = false;}
419 strcpy(info->tableName, tableName);
420 char indname[128];
421 sprintf(indname, "%s_%s", tableName, indexname);
422 rv = dbMgr->createIndex(indname, info);
423 if (rv != OK)
425 printError(ErrSysInit, "Index creation failed in csql for %s\n", tableName);
426 SQLDisconnect(hdbc);
427 return ErrSysInit;
429 delete info;
430 } else {
431 printError(ErrSysInit,"CSQL does not support this index type\n");
432 SQLDisconnect(hdbc);
433 return ErrSysInit;
435 }// while meta data fetch for index creation
436 SQLCloseCursor (hstmtmeta);
437 SQLFreeHandle (SQL_HANDLE_STMT, hstmtmeta);
438 if( !isKeyFld && ((strcmp(fieldName,"")!=0) && (strcmp(fieldName,"NULL")!=0)))
440 printError(ErrSysInit, "Unable to cache Table for %s with key field %s\n", tableName,fieldName);
441 dbMgr->dropTable(tableName);
442 SQLDisconnect(hdbc);
443 return ErrSysInit;
446 Table *table = dbMgr->openTable(tableName);
447 if (table == NULL) {
448 printError(ErrSysInit,"unable to open table %s\n", tableName);
449 dbMgr->closeTable(table);
450 if (tabDefinition) dbMgr->dropTable(tableName);
451 SQLDisconnect(hdbc);
452 return ErrSysInit;
454 table->setUndoLogging(false);
455 //rv = table->lock(false); //no need to release this lock as it is upgrade from S to X
456 if (rv != OK)
458 dbMgr->closeTable(table);
459 if (tabDefinition) dbMgr->dropTable(tableName);
460 SQLDisconnect(hdbc);
461 return ErrSysInit;
463 List fNameList = table->getFieldNameList();
464 ListIterator fNameIter = fNameList.getIterator();
465 FieldInfo *info = new FieldInfo();
466 int fcount =1; void *valBuf; int fieldsize=0;
467 Identifier *elem = NULL;
469 BindBuffer *bBuf;
470 List valBufList;
471 SQLINTEGER len[IDENTIFIER_LENGTH];
472 while (fNameIter.hasElement()) {
473 elem = (Identifier*) fNameIter.nextElement();
474 table->getFieldInfo((const char*)elem->name, info);
475 valBuf = AllDataType::alloc(info->type, info->length);
476 table->bindFld(elem->name, valBuf);
477 fieldsize=0;
478 switch( info->type)
480 case typeString:
481 fieldsize = info->length;
482 break;
483 case typeDate:
484 bBuf = new BindBuffer();
485 bBuf->csql = valBuf;
486 bBuf->type = typeDate;
487 fieldsize = sizeof(DATE_STRUCT);
488 bBuf->targetdb = malloc(fieldsize);
489 valBuf = bBuf->targetdb;
490 valBufList.append(bBuf);
491 break;
492 case typeTime:
493 bBuf = new BindBuffer();
494 bBuf->csql = valBuf;
495 bBuf->type = typeTime;
496 fieldsize = sizeof(TIME_STRUCT);
497 bBuf->targetdb = malloc(fieldsize);
498 valBuf = bBuf->targetdb;
499 valBufList.append(bBuf);
500 break;
501 case typeTimeStamp:
502 bBuf = new BindBuffer();
503 bBuf->csql = valBuf;
504 bBuf->type = typeTimeStamp;
505 fieldsize = sizeof(TIMESTAMP_STRUCT);
506 bBuf->targetdb = malloc(fieldsize);
507 valBuf = bBuf->targetdb;
508 valBufList.append(bBuf);
509 break;
511 retValue = SQLBindCol (hstmt, fcount, AllDataType::convertToSQLType(info->type),
512 valBuf, fieldsize, &len[fcount]);
513 fcount++;
514 if (retValue) {printError(ErrSysInit, "Unable to bind columns in ODBC\n"); return ErrSysInit; }
516 delete info;
517 retValue = SQLExecute (hstmt);
518 if (retValue) {printError(ErrSysInit, "Unable to execute ODBC statement\n"); return ErrSysInit; }
519 int fldpos=0;
520 while(true)
522 retValue = SQLFetch (hstmt);
523 if (retValue) break;
524 ListIterator bindIter = valBufList.getIterator();
525 while (bindIter.hasElement()) {
526 bBuf = (BindBuffer*) bindIter.nextElement();
527 switch (bBuf->type) {
528 case typeDate: {
529 Date *dtCSQL = (Date*) bBuf->csql;
530 DATE_STRUCT *dtTarget = (DATE_STRUCT*) bBuf->targetdb;
531 dtCSQL->set(dtTarget->year,dtTarget->month,dtTarget->day);
532 break;
534 case typeTime: {
535 Time *dtCSQL = (Time*) bBuf->csql;
536 TIME_STRUCT *dtTarget = (TIME_STRUCT*) bBuf->targetdb;
537 dtCSQL->set(dtTarget->hour,dtTarget->minute,dtTarget->second);
538 break;
540 case typeTimeStamp: {
541 TimeStamp *dtCSQL = (TimeStamp*) bBuf->csql;
542 TIMESTAMP_STRUCT *dtTarget = (TIMESTAMP_STRUCT*) bBuf->targetdb;
543 dtCSQL->setDate(dtTarget->year,dtTarget->month,dtTarget->day);
544 dtCSQL->setTime(dtTarget->hour,dtTarget->minute,dtTarget->second, dtTarget->fraction);
545 break;
549 fldpos=0;
550 table->resetNullinfo();
551 while(fldpos < fcount-1)
553 if(len[++fldpos] == SQL_NULL_DATA){
554 table->markFldNull(fldpos);
558 table->insertTuple();
560 //TODO::leak:: valBufList and its targetdb buffer
561 valBufList.reset();
562 dbMgr->closeTable(table);
563 SQLCloseCursor (hstmt);
564 SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
565 SQLDisconnect (hdbc);
566 SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
567 SQLFreeHandle (SQL_HANDLE_ENV, henv);
568 return OK;
571 DbRetVal CacheTableLoader::reload()
573 DbRetVal rv = unload(false);
574 if (rv != OK) return rv;
575 rv = load(false);
576 return rv;
579 DbRetVal CacheTableLoader::unload(bool tabDefinition)
581 Connection conn;
582 DbRetVal rv = conn.open(userName, password);
583 if (rv != OK) return ErrSysInit;
585 if (isTableCached() != OK) {
586 printError(ErrNotCached, "The table \'%s\' is not cached", tableName);
587 return ErrNotCached;
589 DatabaseManager *dbMgr = (DatabaseManager*) conn.getDatabaseManager();
590 if (dbMgr == NULL) { printError(ErrSysInit, "Auth failed\n"); return ErrSysInit; }
591 if (!tabDefinition)
593 Table *table = dbMgr->openTable(tableName);
594 if (table == NULL) { conn.close(); return ErrBadCall; }
595 rv = conn.startTransaction();
596 if (rv != OK) { dbMgr->closeTable(table); conn.close(); return ErrBadCall; }
597 table->truncate();
598 conn.commit();
599 dbMgr->closeTable(table);
601 else
603 rv = dbMgr->dropTable(tableName);
605 conn.close();
607 return rv;
610 DbRetVal CacheTableLoader::refresh()
612 return OK;
615 DbRetVal CacheTableLoader::recoverAllCachedTables()
617 FILE *fp;
618 Connection conn;
619 DbRetVal rv = conn.open(userName, password);
620 if(rv !=OK) return ErrSysInit;
622 //Note: if connection is not open, configuration veriables may be incorrect
624 fp = fopen(Conf::config.getTableConfigFile(),"r");
625 if( fp == NULL ) {
626 printError(ErrSysInit, "cachetable.conf file does not exist");
627 return OK;
629 conn.close();
630 //TODO::take exclusive lock on database
631 char tablename[IDENTIFIER_LENGTH];
632 char fieldname[IDENTIFIER_LENGTH];
633 char condition[IDENTIFIER_LENGTH];
634 char field[IDENTIFIER_LENGTH];
635 int mode;
636 rv = OK;
637 while(!feof(fp))
639 fscanf(fp, "%d:%s %s %s %s\n", &mode, tablename,fieldname,condition,field);
640 //if (mode ==2 ) //just replicated table and not cached
641 //continue;
642 printDebug(DM_Gateway, "Recovering Table from target db: %s\n", tablename);
643 setCondition(condition);
644 setTable(tablename);
645 setFieldName(fieldname);
646 setFieldListVal(field);
647 printf("Recovering table %s %s %s\n", tablename,condition,field);
648 rv = load();
649 if (rv != OK) return rv;
651 fclose(fp);
652 return OK;
655 DbRetVal CacheTableLoader::isTableCached()
657 FILE *fp;
658 char tmpFileName[MAX_FILE_PATH_LEN];
659 fp = fopen(Conf::config.getTableConfigFile(),"r");
660 if( fp == NULL ) {
661 printError(ErrSysInit, "csqltable.conf file does not exist");
662 return ErrSysInit;
664 char tablename[IDENTIFIER_LENGTH];
665 char fieldname[IDENTIFIER_LENGTH];
666 char condition[IDENTIFIER_LENGTH];
667 char field[IDENTIFIER_LENGTH];
668 int mode;
669 while(!feof(fp))
671 fscanf(fp, "%d:%s %s %s %s\n", &mode, tablename,fieldname,condition,field);
672 if (strcmp (tablename, tableName) == 0) {
673 fclose(fp);
674 return OK;
677 fclose(fp);
678 return ErrNotExists;
681 int CacheTableLoader::getTableMode(char *tabname)
683 FILE *fp;
684 fp = fopen(Conf::config.getTableConfigFile(),"r");
685 if( fp == NULL ) {
686 printError(ErrSysInit, "cachetable.conf file does not exist");
687 fclose(fp);
688 return 0;
690 char tablename[IDENTIFIER_LENGTH];
691 char fieldname[IDENTIFIER_LENGTH];
692 char condition[IDENTIFIER_LENGTH];
693 char field[IDENTIFIER_LENGTH];
694 int mode;
695 while(!feof(fp))
697 fscanf(fp,"%d:%s %s %s %s\n",&mode,tablename,fieldname,fieldname,condition);
698 if(0==strcmp(tabname,tablename)){
699 fclose(fp);
700 return mode;
703 fclose(fp);
704 return 0;
708 bool CacheTableLoader::isFieldExist(char *fieldname)
710 char tmpfieldname[IDENTIFIER_LENGTH];
711 int i=0,j=0;
712 while(fieldlistVal[j]!=0)
714 if(fieldlistVal[j] != ',')
715 tmpfieldname[i++]=fieldlistVal[j++];
716 else
718 tmpfieldname[i]='\0';
719 if(strcmp(fieldname,tmpfieldname)==0)
720 return true;
721 else { i=0; j++; }
724 tmpfieldname[i]='\0';
725 if(strcmp(fieldname,tmpfieldname)==0)
726 return true;
727 else
728 return false;