1 /***************************************************************************
3 * Copyright (C) 2007 by www.databasecache.com *
4 * Contact: praba_tuty@databasecache.com *
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. *
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. *
16 ***************************************************************************/
19 #include <CacheTableLoader.h>
20 #include <TableConfig.h>
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");
52 int main(int argc
, char **argv
)
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");
67 char username
[IDENTIFIER_LENGTH
];
69 char password
[IDENTIFIER_LENGTH
];
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
];
81 bool conditionval
= false;
82 bool fieldlistval
= false;
83 bool tableDefinition
= true;
84 bool tableNameSpecified
= false;
85 bool fieldNameSpecified
= false;
86 bool forceEnable
=false;
88 bool isDsnSpecified
=false;
90 while ((c
= getopt(argc
, argv
, "U:P:t:d:f:c:p:l:FRDSsru?")) != EOF
)
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]);
98 tableNameSpecified
= true;
101 case 'd' : { strcpy(dsnName
,argv
[optind
- 1]);
102 if(!tableNameSpecified
)opt
=50;
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;}
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;}
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
);
140 cacheLoader
.setCondition(condition
);// new one
141 TableConf::config
.setCondition(condition
);
145 cacheLoader
.setDsnName(dsnName
);
146 TableConf::config
.setDsnName(dsnName
);
150 cacheLoader
.setFieldListVal(fieldlist
);
151 TableConf::config
.setFieldListVal(fieldlist
);
154 cacheLoader
.setForceFlag(forceEnable
);
155 TableConf::config
.setForceFlag(forceEnable
);
157 bool isCached
= false;
158 unsigned int mode
= TableConf::config
.getTableMode(tablename
);
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
);
170 printf("Table is already cached, unload table by\n");
171 printf("\"cachetable -t <tablename> -u\" and then try \n");
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");
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");
197 cacheLoader
.setTable(tablename
);
198 TableConf::config
.setTable(tablename
);
199 isCached
= TableConf::config
.isTableCached(mode
);
201 printError(ErrNotCached
, "Table is not Cached");
204 TableConf::config
.removeFromCacheTableFile();
206 if(tableNameSpecified
) {
207 cacheLoader
.setTable(tablename
);
208 TableConf::config
.setTable(tablename
);
210 rv
= TableConf::config
.CacheInfo(tableNameSpecified
);
212 printf("\nError (%d): None of the table found in Cache,You need to cache the table from Target DB.\n\n",rv
);
217 cacheLoader
.setDsnName(dsnName
);
218 TableConf::config
.setDsnName(dsnName
);
220 rv
= TableConf::config
.tablesOnDsn();
222 printf("Error(%d):There is no cache table present in the specified(%s)DS.\n\n",rv
,dsnName
);
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");
230 rv
= cacheLoader
.cacheAllTablesFromDs(dsnName
,tableDefinition
, isDirect
,username
,password
);