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(" 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");
50 int main(int argc
, char **argv
)
54 rv
= conn
.open(I_USER
, I_PASS
);
55 if (rv
!= OK
) return 1;
57 if (!Conf::config
.useCache())
59 printf("CACHE_TABLE is set to FALSE in csql.conf file.\n");
65 char username
[IDENTIFIER_LENGTH
];
67 char password
[IDENTIFIER_LENGTH
];
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
];
79 bool conditionval
= false;
80 bool fieldlistval
= false;
81 bool tableDefinition
= true;
82 bool tableNameSpecified
= false;
83 bool fieldNameSpecified
= false;
84 bool forceEnable
=false;
86 while ((c
= getopt(argc
, argv
, "U:P:t:d:f:c:p:FRDSsru?")) != EOF
)
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]);
94 tableNameSpecified
= true;
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;}
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;}
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
);
132 cacheLoader
.setCondition(condition
);// new one
133 TableConf::config
.setCondition(condition
);
137 cacheLoader
.setDsnName(dsnName
);
138 TableConf::config
.setDsnName(dsnName
);
142 cacheLoader
.setFieldListVal(fieldlist
);
143 TableConf::config
.setFieldListVal(fieldlist
);
146 cacheLoader
.setForceFlag(forceEnable
);
147 TableConf::config
.setForceFlag(forceEnable
);
149 bool isCached
= false;
150 unsigned int mode
= TableConf::config
.getTableMode(tablename
);
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
);
162 printf("Table is already cached, unload table by\n");
163 printf("\"cachetable -t <tablename> -u\" and then try \n");
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");
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");
189 cacheLoader
.setTable(tablename
);
190 TableConf::config
.setTable(tablename
);
191 isCached
= TableConf::config
.isTableCached(mode
);
193 printError(ErrNotCached
, "Table is not Cached");
196 TableConf::config
.removeFromCacheTableFile();
198 if(tableNameSpecified
) {
199 cacheLoader
.setTable(tablename
);
200 TableConf::config
.setTable(tablename
);
202 rv
= TableConf::config
.CacheInfo(tableNameSpecified
);
204 printf("\nError (%d): None of the table found in Cache,You need to cache the table from Target DB.\n\n",rv
);