changes for Gateway with OSYNC Mode.
[csql.git] / src / tools / cachetable.cxx
blob536b0ebce38df56fae0fe33f4490e19123cd12d5
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 \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(" R -> recover all cached tables from the target database.\n");
27 printf(" s -> load only the records from target db. Assumes table is already created in csql\n");
28 printf(" r -> reload the table. get the latest image of table from target db\n");
29 printf(" u -> unload the table. if used with -s option, removes only records and preserves the schema\n");
30 printf(" m -> synchronous mode\n");
31 printf(" no option -> get table definition and records from target db and create in csql.\n");
32 return;
35 int main(int argc, char **argv)
37 char username[IDENTIFIER_LENGTH];
38 username [0] = '\0';
39 char password[IDENTIFIER_LENGTH];
40 password [0] = '\0';
41 int c = 0, opt = 10;
42 char tablename[IDENTIFIER_LENGTH];
43 char syncModeStr[IDENTIFIER_LENGTH];
44 bool tableDefinition = true;
45 bool tableNameSpecified = false;
46 while ((c = getopt(argc, argv, "U:P:t:Rsru?")) != EOF)
48 switch (c)
50 case 'U' : { strcpy(username, argv[optind - 1]); opt=10; break; }
51 case 'P' : { strcpy(password, argv[optind - 1]); opt=10; break; }
52 case 't' : { strcpy(tablename, argv[optind - 1]);
53 if (opt==10) opt = 2;
54 tableNameSpecified = true;
55 break;
57 case '?' : { opt = 10; break; } //print help
58 case 'R' : { opt = 3; break; } //recover all the tables
59 case 's' : { tableDefinition=false; break; } //do not get the schema information from target db
60 case 'r' : { opt = 4; break; } //reload the table
61 case 'u' : { opt = 5; break; } //unload the table
62 default: opt=10;
65 }//while options
66 if (opt == 10) {
67 printUsage();
68 return 0;
71 //printf("%s %s \n", username, password);
72 if (username[0] == '\0' )
74 strcpy(username, "root");
75 strcpy(password, "manager");
77 DbRetVal rv = OK;
78 CacheTableLoader cacheLoader;
79 cacheLoader.setConnParam(username, password);
80 if (opt==2) {
81 cacheLoader.setTable(tablename);
82 rv = cacheLoader.load(tableDefinition);
83 if (rv != OK) exit (1);
84 rv = cacheLoader.addToCacheTableFile();
85 if (rv != OK) exit (2);
86 }else if (opt==3) //recover
88 rv = cacheLoader.recoverAllCachedTables();
89 if (rv != OK) exit (1);
90 }else if (opt==4) //reload
92 if (!tableNameSpecified)
94 printf("Table name is not specified. Check usage with ? \n");
95 return 1;
97 cacheLoader.setTable(tablename);
98 rv = cacheLoader.reload();
99 if (rv != OK) exit (1);
100 }else if (opt==5) //unload
102 if (!tableNameSpecified)
104 printf("Table name is not specified. Check usage with ? option\n");
105 return 1;
107 cacheLoader.setTable(tablename);
108 rv = cacheLoader.unload(tableDefinition);
109 if (rv != OK) exit (1);
110 rv = cacheLoader.removeFromCacheTableFile();
111 if (rv != OK) exit (2);
114 return 0;