reverting back the changes. causes core dump
[csql.git] / src / tools / cachetable.cxx
blob0706fe034f733bd8f055dac547a767d68a62827b
1 /***************************************************************************
3 * Copyright (C) 2007 by www.databasecache.com *
4 * Contact: praba_tuty@databasecache.com *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 ***************************************************************************/
17 #include <os.h>
18 #include <CSql.h>
19 #include <CacheTableLoader.h>
20 #include <TableConfig.h>
22 void printUsage()
24 printf("Usage: cachetable [ -U <username> ] [ -P <password> ]\n");
25 printf(" [ -t <tblName> [-D] [ -c \"condition\" ]\n");
26 printf(" [-f \"fieldListToCache\"]\n");
27 printf(" [ -p <fieldName> [-F] ]\n");
28 printf(" [ -u [-s] ]\n");
29 printf(" [ -s | -r ] ]\n");
30 printf(" [ -S | -R ]\n\n");
31 printf(" U -> Username to connect with csql.\n");
32 printf(" P -> Password for above username.\n");
33 printf(" t -> Table name to be cached in csql from target db.\n");
34 printf(" D -> Enable direct access option to target database.\n");
35 printf(" c -> Conditional expression used in std SQL WHERE clause.\n");
36 printf(" f -> List of field names to be cached. Comma separated.\n");
37 printf(" p -> Not Null unique index field name for Bidirectional\n");
38 printf(" caching on which trigger needs to be run.\n");
39 printf(" d -> Cache the tables from the specified DSN.\n");
40 printf(" l -> Retrievs all the cached table names for the specified DSN.\n");
41 printf(" F -> Forceful caching.\n");
42 printf(" s -> Load only the records from target db.\n");
43 printf(" Assumes table is already created in csql.\n");
44 printf(" u -> Unload the table. If used with -s option,\n");
45 printf(" removes only records and preserves the schema.\n");
46 printf(" r -> Reload the table. Get the latest image of table from target db.\n");
47 printf(" S -> Cache Description for cached tables.\n");
48 printf(" R -> Recover all cached tables from the target database.\n");
49 return;
52 int main(int argc, char **argv)
54 DbRetVal rv = OK;
55 Connection conn;
56 rv = conn.open(I_USER, I_PASS);
57 if (rv != OK) return 1;
58 os::signal(SIGCSQL1, SIG_IGN);
59 if (!Conf::config.useCache())
61 printf("CACHE_TABLE is set to FALSE in csql.conf file.\n");
62 conn.close();
63 return 2;
65 else{ conn.close(); }
67 char username[IDENTIFIER_LENGTH];
68 username [0] = '\0';
69 char password[IDENTIFIER_LENGTH];
70 password [0] = '\0';
71 int c = 0, opt = 10;
72 bool isDirect=false;
73 char tablename[IDENTIFIER_LENGTH];
74 char fieldname[IDENTIFIER_LENGTH];
75 char condition[IDENTIFIER_LENGTH];
76 char fieldlist[IDENTIFIER_LENGTH];
77 char syncModeStr[IDENTIFIER_LENGTH];
78 char dsnName[IDENTIFIER_LENGTH];
79 bool Isuid=false;
80 bool Ispid=false;
81 bool conditionval = false;
82 bool fieldlistval = false;
83 bool tableDefinition = true;
84 bool tableNameSpecified = false;
85 bool fieldNameSpecified = false;
86 bool forceEnable=false;
87 bool isDsn=false;
88 bool isDsnSpecified=false;
90 while ((c = getopt(argc, argv, "U:P:t:d:f:c:p:l:FRDSsru?")) != EOF)
92 switch (c)
94 case 'U' : { strcpy(username, argv[optind - 1]); opt=10; break; }
95 case 'P' : { strcpy(password, argv[optind - 1]); opt=10; break; }
96 case 't' : { strcpy(tablename, argv[optind - 1]);
97 if (opt==10) opt = 2;
98 tableNameSpecified = true;
99 break;
101 case 'd' : { strcpy(dsnName,argv[optind - 1]);
102 if(!tableNameSpecified)opt=50;
103 isDsn=true;break;
105 case 'l' : {strcpy(dsnName,argv[optind - 1]); isDsnSpecified=true; opt=7; break;}
106 case 'p' : { strcpy(fieldname, argv[optind - 1]);
107 if(opt==2){fieldNameSpecified = true;break;}
110 case 'D' : {
111 if(opt==2) {isDirect=true;break;}
113 case 'c' : {strcpy(condition,argv[optind - 1]); conditionval = true; break; }
114 case 'f' : {strcpy(fieldlist,argv[optind - 1]);fieldlistval = true ;break; }
115 case 'F' : { if(opt==2 && fieldNameSpecified ) forceEnable=true; break;}
116 case '?' : { opt = 10; break; } //print help
117 case 'R' : { opt = 3; break; } //recover all the tables
118 case 's' : { tableDefinition=false; break; } //do not get the schema information from target db
119 case 'r' : { opt = 4; break; } //reload the table
120 case 'u' : { opt = 5; break; } //unload the table
121 case 'S' : {opt=6;break;}
122 default: opt=10;
125 }//while options
126 if (opt == 10) {
127 printUsage();
128 return 0;
131 if (username[0] == '\0' )
133 strcpy(username, I_USER);
134 strcpy(password, I_PASS);
136 CacheTableLoader cacheLoader;
137 cacheLoader.setConnParam(username, password);
138 TableConf::config.setConnParam(username, password);
139 if(conditionval){
140 cacheLoader.setCondition(condition);// new one
141 TableConf::config.setCondition(condition);
144 if(isDsn){
145 cacheLoader.setDsnName(dsnName);
146 TableConf::config.setDsnName(dsnName);
149 if(fieldlistval) {
150 cacheLoader.setFieldListVal(fieldlist);
151 TableConf::config.setFieldListVal(fieldlist);
153 if(forceEnable) {
154 cacheLoader.setForceFlag(forceEnable);
155 TableConf::config.setForceFlag(forceEnable);
157 bool isCached = false;
158 unsigned int mode = TableConf::config.getTableMode(tablename);
160 if (opt==2) {
161 cacheLoader.setTable(tablename);
162 TableConf::config.setTable(tablename);
163 if(fieldNameSpecified){
164 cacheLoader.setFieldName(fieldname);
165 TableConf::config.setFieldName(fieldname);
168 isCached = TableConf::config.isTableCached(mode);
169 if (isCached) {
170 printf("Table is already cached, unload table by\n");
171 printf("\"cachetable -t <tablename> -u\" and then try \n");
172 return 3;
174 rv = cacheLoader.load(tableDefinition);
175 if(rv != OK) return 4;
176 TableConf::config.addToCacheTableFile(isDirect);
177 } else if (opt==3) {//recover
178 rv = cacheLoader.recoverAllCachedTables();
179 if (rv != OK) return 5;
180 } else if (opt==4) {//reload
181 if (!tableNameSpecified)
183 printf("Table name is not specified. Check usage with ? \n");
184 return 6;
186 cacheLoader.setTable(tablename);
187 TableConf::config.setTable(tablename);
188 rv = cacheLoader.reload();
189 if (rv != OK) return 7;
191 } else if (opt==5) {//unload
192 if (!tableNameSpecified)
194 printf("Table name is not specified. Check usage with ? option\n");
195 return 8;
197 cacheLoader.setTable(tablename);
198 TableConf::config.setTable(tablename);
199 isCached = TableConf::config.isTableCached(mode);
200 if (!mode) {
201 printError(ErrNotCached, "Table is not Cached");
202 return 9;
204 TableConf::config.removeFromCacheTableFile();
205 } else if(opt==6) {
206 if(tableNameSpecified) {
207 cacheLoader.setTable(tablename);
208 TableConf::config.setTable(tablename);
210 rv = TableConf::config.CacheInfo(tableNameSpecified);
211 if (rv !=OK) {
212 printf("\nError (%d): None of the table found in Cache,You need to cache the table from Target DB.\n\n",rv);
213 exit(2);
215 }else if(opt==7){
216 if(isDsnSpecified){
217 cacheLoader.setDsnName(dsnName);
218 TableConf::config.setDsnName(dsnName);
220 rv = TableConf::config.tablesOnDsn();
221 if(rv != OK){
222 printf("Error(%d):There is no cache table present in the specified(%s)DS.\n\n",rv,dsnName);
223 return 10;
225 }else if(opt==50 && tableDefinition){
226 if(conditionval || fieldlistval || tableNameSpecified){
227 printf("Not a valid commnad. Use \"cachetable -d <dsName>\" to cache all tables.\n");
228 return 12;
230 rv = cacheLoader.cacheAllTablesFromDs(dsnName,tableDefinition, isDirect,username,password);
231 if(rv!=OK)return 12;
233 return 0;