Full table read caching implementation, also contains deleteWhere and truncate for...
[csql.git] / src / tools / cachetable.cxx
blob2742324f8e8bfb58d36f8571cdd68856ad0f313d
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 [-R] [-s] [-r]\n");
22 printf(" username -> username to connect with csql.\n");
23 printf(" passwd -> password for the above username to connect with csql.\n");
24 printf(" tablename -> table name to be cached in csql from target db.\n");
25 printf(" R -> recover all cached tables from the target database.\n");
26 printf(" s -> load only the records from target db. Assumes table is already created in csql\n");
27 printf(" r -> reload the table. get the latest image of table from target db\n");
28 printf(" u -> unload the table. if used with -s option, removes only records and preserves the schema\n");
29 printf(" no option -> get table definition and records from target db and create in csql.\n");
30 return;
33 void addToCacheTableFile(char *tablename)
35 FILE *fp;
36 printf("CACHE_TABLE_FILE is %s\n", Conf::config.getCacheTableFile());
37 fp = fopen(Conf::config.getCacheTableFile(),"a");
38 if( fp == NULL ) {
39 printError(ErrSysInit, "Invalid path/filename in CACHE_TABLE_FILE.\nTable will not be"
40 "recovered in case of crash");
41 return;
43 fprintf(fp, "%s\n", tablename);
44 fclose(fp);
46 int main(int argc, char **argv)
48 char username[IDENTIFIER_LENGTH];
49 username [0] = '\0';
50 char password[IDENTIFIER_LENGTH];
51 password [0] = '\0';
52 int c = 0, opt = 10;
53 char tablename[IDENTIFIER_LENGTH];
54 bool tableDefinition = true;
55 bool tableNameSpecified = false;
56 while ((c = getopt(argc, argv, "U:P:t:Rsru?")) != EOF)
58 switch (c)
60 case 'U' : { strcpy(username, argv[optind - 1]); opt=10; break; }
61 case 'P' : { strcpy(password, argv[optind - 1]); opt=10; break; }
62 case 't' : { strcpy(tablename, argv[optind - 1]);
63 if (opt==10) opt = 2;
64 tableNameSpecified = true;
65 break;
67 case '?' : { opt = 10; break; } //print help
68 case 'R' : { opt = 3; break; } //recover all the tables
69 case 's' : { tableDefinition=false; break; } //do not get the schema information from target db
70 case 'r' : { opt = 4; break; } //reload the table
71 case 'u' : { opt = 5; break; } //unload the table
72 default: opt=10;
75 }//while options
76 if (opt == 10) {
77 printUsage();
78 return 0;
81 //printf("%s %s \n", username, password);
82 if (username[0] == '\0' )
84 strcpy(username, "root");
85 strcpy(password, "manager");
87 DbRetVal rv = OK;
88 CacheTableLoader cacheLoader;
89 cacheLoader.setConnParam(username, password);
90 if (opt==2) {
92 cacheLoader.setTable(tablename);
93 rv = cacheLoader.load(tableDefinition);
94 if (rv != OK) exit (1);
95 rv = cacheLoader.addToCacheTableFile();
96 if (rv != OK) exit (2);
97 }else if (opt==3) //recover
99 rv = cacheLoader.recoverAllCachedTables();
100 if (rv != OK) exit (1);
101 }else if (opt==4) //reload
103 if (!tableNameSpecified)
105 printf("Table name is not specified. Check usage with ? \n");
106 return 1;
108 cacheLoader.setTable(tablename);
109 rv = cacheLoader.reload();
110 if (rv != OK) exit (1);
111 }else if (opt==5) //unload
113 if (!tableNameSpecified)
115 printf("Table name is not specified. Check usage with ? option\n");
116 return 1;
118 cacheLoader.setTable(tablename);
119 rv = cacheLoader.unload(tableDefinition);
120 if (rv != OK) exit (1);
121 rv = cacheLoader.removeFromCacheTableFile();
122 if (rv != OK) exit (2);
125 return 0;