adding statment length
[csql.git] / src / tools / cachetable.cxx
blob1267a001fcf9426d1d33607333668fc858f0235e
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(" F -> Forceful caching.\n");
40 printf(" s -> Load only the records from target db.\n");
41 printf(" Assumes table is already created in csql.\n");
42 printf(" u -> Unload the table. If used with -s option,\n");
43 printf(" removes only records and preserves the schema.\n");
44 printf(" r -> Reload the table. Get the latest image of table from target db.\n");
45 printf(" S -> Cache Description for cached tables.\n");
46 printf(" R -> Recover all cached tables from the target database.\n");
47 return;
50 int main(int argc, char **argv)
52 DbRetVal rv = OK;
53 Connection conn;
54 rv = conn.open(I_USER, I_PASS);
55 if (rv != OK) return 1;
56 os::signal(SIGCSQL1, SIG_IGN);
57 if (!Conf::config.useCache())
59 printf("CACHE_TABLE is set to FALSE in csql.conf file.\n");
60 conn.close();
61 return 2;
63 else{ conn.close(); }
65 char username[IDENTIFIER_LENGTH];
66 username [0] = '\0';
67 char password[IDENTIFIER_LENGTH];
68 password [0] = '\0';
69 int c = 0, opt = 10;
70 bool isDirect=false;
71 char tablename[IDENTIFIER_LENGTH];
72 char fieldname[IDENTIFIER_LENGTH];
73 char condition[IDENTIFIER_LENGTH];
74 char fieldlist[IDENTIFIER_LENGTH];
75 char syncModeStr[IDENTIFIER_LENGTH];
76 char dsnName[IDENTIFIER_LENGTH];
77 bool Isuid=false;
78 bool Ispid=false;
79 bool conditionval = false;
80 bool fieldlistval = false;
81 bool tableDefinition = true;
82 bool tableNameSpecified = false;
83 bool fieldNameSpecified = false;
84 bool forceEnable=false;
85 bool isDsn=false;
86 while ((c = getopt(argc, argv, "U:P:t:d:f:c:p:FRDSsru?")) != EOF)
88 switch (c)
90 case 'U' : { strcpy(username, argv[optind - 1]); opt=10; break; }
91 case 'P' : { strcpy(password, argv[optind - 1]); opt=10; break; }
92 case 't' : { strcpy(tablename, argv[optind - 1]);
93 if (opt==10) opt = 2;
94 tableNameSpecified = true;
95 break;
97 case 'd' : { strcpy(dsnName,argv[optind - 1]);isDsn=true;break;}
98 case 'p' : { strcpy(fieldname, argv[optind - 1]);
99 if(opt==2){fieldNameSpecified = true;break;}
102 case 'D' : {
103 if(opt==2) {isDirect=true;break;}
105 case 'c' : {strcpy(condition,argv[optind - 1]); conditionval = true; break; }
106 case 'f' : {strcpy(fieldlist,argv[optind - 1]);fieldlistval = true ;break; }
107 case 'F' : { if(opt==2 && fieldNameSpecified ) forceEnable=true; break;}
108 case '?' : { opt = 10; break; } //print help
109 case 'R' : { opt = 3; break; } //recover all the tables
110 case 's' : { tableDefinition=false; break; } //do not get the schema information from target db
111 case 'r' : { opt = 4; break; } //reload the table
112 case 'u' : { opt = 5; break; } //unload the table
113 case 'S' : {opt=6;break;}
114 default: opt=10;
117 }//while options
118 if (opt == 10) {
119 printUsage();
120 return 0;
123 if (username[0] == '\0' )
125 strcpy(username, I_USER);
126 strcpy(password, I_PASS);
128 CacheTableLoader cacheLoader;
129 cacheLoader.setConnParam(username, password);
130 TableConf::config.setConnParam(username, password);
131 if(conditionval){
132 cacheLoader.setCondition(condition);// new one
133 TableConf::config.setCondition(condition);
136 if(isDsn){
137 cacheLoader.setDsnName(dsnName);
138 TableConf::config.setDsnName(dsnName);
141 if(fieldlistval) {
142 cacheLoader.setFieldListVal(fieldlist);
143 TableConf::config.setFieldListVal(fieldlist);
145 if(forceEnable) {
146 cacheLoader.setForceFlag(forceEnable);
147 TableConf::config.setForceFlag(forceEnable);
149 bool isCached = false;
150 unsigned int mode = TableConf::config.getTableMode(tablename);
152 if (opt==2) {
153 cacheLoader.setTable(tablename);
154 TableConf::config.setTable(tablename);
155 if(fieldNameSpecified){
156 cacheLoader.setFieldName(fieldname);
157 TableConf::config.setFieldName(fieldname);
160 isCached = TableConf::config.isTableCached(mode);
161 if (isCached) {
162 printf("Table is already cached, unload table by\n");
163 printf("\"cachetable -t <tablename> -u\" and then try \n");
164 return 3;
166 rv = cacheLoader.load(tableDefinition);
167 if(rv != OK) return 4;
168 TableConf::config.addToCacheTableFile(isDirect);
169 } else if (opt==3) {//recover
170 rv = cacheLoader.recoverAllCachedTables();
171 if (rv != OK) return 5;
172 } else if (opt==4) {//reload
173 if (!tableNameSpecified)
175 printf("Table name is not specified. Check usage with ? \n");
176 return 6;
178 cacheLoader.setTable(tablename);
179 TableConf::config.setTable(tablename);
180 rv = cacheLoader.reload();
181 if (rv != OK) return 7;
183 } else if (opt==5) {//unload
184 if (!tableNameSpecified)
186 printf("Table name is not specified. Check usage with ? option\n");
187 return 8;
189 cacheLoader.setTable(tablename);
190 TableConf::config.setTable(tablename);
191 isCached = TableConf::config.isTableCached(mode);
192 if (!mode) {
193 printError(ErrNotCached, "Table is not Cached");
194 return 9;
196 TableConf::config.removeFromCacheTableFile();
197 } else if(opt==6) {
198 if(tableNameSpecified) {
199 cacheLoader.setTable(tablename);
200 TableConf::config.setTable(tablename);
202 rv = TableConf::config.CacheInfo(tableNameSpecified);
203 if (rv !=OK) {
204 printf("\nError (%d): None of the table found in Cache,You need to cache the table from Target DB.\n\n",rv);
205 exit(2);
208 return 0;