1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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. *
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. *
15 ***************************************************************************/
17 #include <CacheTableLoader.h>
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");
33 void addToCacheTableFile(char *tablename
)
36 printf("CACHE_TABLE_FILE is %s\n", Conf::config
.getCacheTableFile());
37 fp
= fopen(Conf::config
.getCacheTableFile(),"a");
39 printError(ErrSysInit
, "Invalid path/filename in CACHE_TABLE_FILE.\nTable will not be"
40 "recovered in case of crash");
43 fprintf(fp
, "%s\n", tablename
);
46 int main(int argc
, char **argv
)
48 char username
[IDENTIFIER_LENGTH
];
50 char password
[IDENTIFIER_LENGTH
];
53 char tablename
[IDENTIFIER_LENGTH
];
54 bool tableDefinition
= true;
55 bool tableNameSpecified
= false;
56 while ((c
= getopt(argc
, argv
, "U:P:t:Rsru?")) != EOF
)
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]);
64 tableNameSpecified
= true;
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
81 //printf("%s %s \n", username, password);
82 if (username
[0] == '\0' )
84 strcpy(username
, "root");
85 strcpy(password
, "manager");
88 CacheTableLoader cacheLoader
;
89 cacheLoader
.setConnParam(username
, password
);
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");
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");
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);