commiting changes from enterprise version for V2.4
[csql.git] / src / tools / cachetable.cxx
blobda99f084143e0aeb78fe3c9799fe0989df3caee9
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 <CSql.h>
17 #include <CacheTableLoader.h>
19 void printUsage()
21 printf("Usage: cachetable [-U username] [-P passwd] -t tablename[-D] -c \"condition\" -f \"selected field names\" -p fieldname -S\n"
22 " [-R] [-s] [-r]\n");
23 printf(" username -> username to connect with csql.\n");
24 printf(" passwd -> password for the above username to connect with csql.\n");
25 printf(" tablename -> table name to be cached in csql from target db.\n");
26 printf(" fieldname -> field name to be specified for the bidirectional caching on which trigger to be run .\n");
27 printf(" R -> recover all cached tables from the target database.\n");
28 printf(" s -> load only the records from target db. Assumes table is already created in csql\n");
29 printf(" r -> reload the table. get the latest image of table from target db\n");
30 printf(" u -> unload the table. if used with -s option, removes only records and preserves the schema\n");
31 printf(" no option -> get table definition and records from target db and create in csql.\n");
32 printf(" D -> Enable direct access option to target database\n");
33 printf(" S -> Cache Description\n");
34 return;
37 int main(int argc, char **argv)
39 DbRetVal rv = OK;
40 Connection conn;
41 rv = conn.open("root","manager");
42 if(rv != OK) return 1;
44 if(!Conf::config.useCache())
46 printf("CACHE_TABLE is set to FALSE in csql.conf file.\n");
47 conn.close();
48 return 1;
50 else{ conn.close(); }
52 char username[IDENTIFIER_LENGTH];
53 username [0] = '\0';
54 char password[IDENTIFIER_LENGTH];
55 password [0] = '\0';
56 int c = 0, opt = 10;
57 bool isDirect=false;
58 char tablename[IDENTIFIER_LENGTH];
59 char fieldname[IDENTIFIER_LENGTH];
60 char condition[IDENTIFIER_LENGTH];
61 char fieldlist[IDENTIFIER_LENGTH];
62 char syncModeStr[IDENTIFIER_LENGTH];
63 bool conditionval = false;
64 bool fieldlistval = false;
65 bool tableDefinition = true;
66 bool tableNameSpecified = false;
67 bool fieldNameSpecified = false;
68 while ((c = getopt(argc, argv, "U:P:t:f:c:p:RDSsru?")) != EOF)
70 switch (c)
72 case 'U' : { strcpy(username, argv[optind - 1]); opt=10; break; }
73 case 'P' : { strcpy(password, argv[optind - 1]); opt=10; break; }
74 case 't' : { strcpy(tablename, argv[optind - 1]);
75 if (opt==10) opt = 2;
76 tableNameSpecified = true;
77 break;
79 case 'p' : { strcpy(fieldname, argv[optind - 1]);
80 if(opt==2){fieldNameSpecified = true;break;}
83 case 'D' : {
84 if(opt==2) {isDirect=true;break;}
86 case 'c' : {strcpy(condition,argv[optind - 1]); conditionval = true; break; }// condition for selelcted records by :Jitendra
87 case 'f' : {strcpy(fieldlist,argv[optind - 1]);fieldlistval = true ;break; }
88 case '?' : { opt = 10; break; } //print help
89 case 'R' : { opt = 3; break; } //recover all the tables
90 case 's' : { tableDefinition=false; break; } //do not get the schema information from target db
91 case 'r' : { opt = 4; break; } //reload the table
92 case 'u' : { opt = 5; break; } //unload the table
93 case 'S' : { opt = 6; break; }
94 default: opt=10;
97 }//while options
98 if (opt == 10) {
99 printUsage();
100 return 0;
103 //printf("%s %s \n", username, password);
104 if (username[0] == '\0' )
106 strcpy(username, "root");
107 strcpy(password, "manager");
109 CacheTableLoader cacheLoader;
110 cacheLoader.setConnParam(username, password);
112 if(conditionval){
113 cacheLoader.setCondition(condition);}// new one
114 if(fieldlistval){
115 cacheLoader.setFieldListVal(fieldlist);}
116 if (opt==2) {
117 cacheLoader.setTable(tablename);
118 if(fieldNameSpecified){ cacheLoader.setFieldName(fieldname); }
119 rv = CacheTableLoader::isTableCached(tablename);
120 if(rv!=OK){
121 rv = cacheLoader.load(tableDefinition);
122 if(rv == OK){
123 cacheLoader.addToCacheTableFile(isDirect);
124 }else exit(2);
125 } else
127 printf("Table is already cached, unload table by \" cachetable -t <tablename> -u\" and then try \n");
128 exit(3);
130 }else if (opt==3) //recover
132 rv = cacheLoader.recoverAllCachedTables();
133 if (rv != OK) exit (1);
134 }else if (opt==4) //reload
136 if (!tableNameSpecified)
138 printf("Table name is not specified. Check usage with ? \n");
139 return 1;
141 cacheLoader.setTable(tablename);
142 rv = cacheLoader.reload();
143 if (rv != OK) exit (1);
144 }else if (opt==5) //unload
146 if (!tableNameSpecified)
148 printf("Table name is not specified. Check usage with ? option\n");
149 return 1;
151 cacheLoader.setTable(tablename);
152 rv = cacheLoader.unload(tableDefinition);
153 if (rv != OK) exit (1);
154 rv = cacheLoader.removeFromCacheTableFile();
155 if (rv != OK) exit (2);
157 }else if(opt==6)
159 if(tableNameSpecified)
161 cacheLoader.setTable(tablename);
163 rv = cacheLoader.CacheInfo(tableNameSpecified);
164 if(rv !=OK)
166 printf("\nError (%d): None of the table found in Cache,You need to cache the table from Target DB.\n\n",rv);
167 exit(2);
170 return 0;