00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <CSql.h>
00017 #include <CacheTableLoader.h>
00018
00019 void printUsage()
00020 {
00021 printf("Usage: cachetable [-U username] [-P passwd] -t tablename \n"
00022 " [-R] [-s] [-r]\n");
00023 printf(" username -> username to connect with csql.\n");
00024 printf(" passwd -> password for the above username to connect with csql.\n");
00025 printf(" tablename -> table name to be cached in csql from target db.\n");
00026 printf(" R -> recover all cached tables from the target database.\n");
00027 printf(" s -> load only the records from target db. Assumes table is already created in csql\n");
00028 printf(" r -> reload the table. get the latest image of table from target db\n");
00029 printf(" u -> unload the table. if used with -s option, removes only records and preserves the schema\n");
00030 printf(" no option -> get table definition and records from target db and create in csql.\n");
00031 return;
00032 }
00033
00034 int main(int argc, char **argv)
00035 {
00036 char username[IDENTIFIER_LENGTH];
00037 username [0] = '\0';
00038 char password[IDENTIFIER_LENGTH];
00039 password [0] = '\0';
00040 int c = 0, opt = 10;
00041 char tablename[IDENTIFIER_LENGTH];
00042 char syncModeStr[IDENTIFIER_LENGTH];
00043 bool tableDefinition = true;
00044 bool tableNameSpecified = false;
00045 while ((c = getopt(argc, argv, "U:P:t:Rsru?")) != EOF)
00046 {
00047 switch (c)
00048 {
00049 case 'U' : { strcpy(username, argv[optind - 1]); opt=10; break; }
00050 case 'P' : { strcpy(password, argv[optind - 1]); opt=10; break; }
00051 case 't' : { strcpy(tablename, argv[optind - 1]);
00052 if (opt==10) opt = 2;
00053 tableNameSpecified = true;
00054 break;
00055 }
00056 case '?' : { opt = 10; break; }
00057 case 'R' : { opt = 3; break; }
00058 case 's' : { tableDefinition=false; break; }
00059 case 'r' : { opt = 4; break; }
00060 case 'u' : { opt = 5; break; }
00061 default: opt=10;
00062
00063 }
00064 }
00065 if (opt == 10) {
00066 printUsage();
00067 return 0;
00068 }
00069
00070
00071 if (username[0] == '\0' )
00072 {
00073 strcpy(username, "root");
00074 strcpy(password, "manager");
00075 }
00076 DbRetVal rv = OK;
00077 CacheTableLoader cacheLoader;
00078 cacheLoader.setConnParam(username, password);
00079 if (opt==2) {
00080 cacheLoader.setTable(tablename);
00081 rv = cacheLoader.load(tableDefinition);
00082 if (rv != OK) exit (1);
00083 rv = cacheLoader.addToCacheTableFile();
00084 if (rv != OK) exit (2);
00085 }else if (opt==3)
00086 {
00087 rv = cacheLoader.recoverAllCachedTables();
00088 if (rv != OK) exit (1);
00089 }else if (opt==4)
00090 {
00091 if (!tableNameSpecified)
00092 {
00093 printf("Table name is not specified. Check usage with ? \n");
00094 return 1;
00095 }
00096 cacheLoader.setTable(tablename);
00097 rv = cacheLoader.reload();
00098 if (rv != OK) exit (1);
00099 }else if (opt==5)
00100 {
00101 if (!tableNameSpecified)
00102 {
00103 printf("Table name is not specified. Check usage with ? option\n");
00104 return 1;
00105 }
00106 cacheLoader.setTable(tablename);
00107 rv = cacheLoader.unload(tableDefinition);
00108 if (rv != OK) exit (1);
00109 rv = cacheLoader.removeFromCacheTableFile();
00110 if (rv != OK) exit (2);
00111
00112 }
00113 return 0;
00114 }