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[-D] -c \"condition\" -f \"selected field names\" -p fieldname\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");
36 int main(int argc
, char **argv
)
38 char username
[IDENTIFIER_LENGTH
];
40 char password
[IDENTIFIER_LENGTH
];
44 char tablename
[IDENTIFIER_LENGTH
];
45 char fieldname
[IDENTIFIER_LENGTH
];
46 char condition
[IDENTIFIER_LENGTH
];
47 char fieldlist
[IDENTIFIER_LENGTH
];
48 char syncModeStr
[IDENTIFIER_LENGTH
];
49 bool conditionval
= false;
50 bool fieldlistval
= false;
51 bool tableDefinition
= true;
52 bool tableNameSpecified
= false;
53 bool fieldNameSpecified
= false;
54 while ((c
= getopt(argc
, argv
, "U:P:t:f:c:p:RDsru?")) != EOF
)
58 case 'U' : { strcpy(username
, argv
[optind
- 1]); opt
=10; break; }
59 case 'P' : { strcpy(password
, argv
[optind
- 1]); opt
=10; break; }
60 case 't' : { strcpy(tablename
, argv
[optind
- 1]);
62 tableNameSpecified
= true;
65 case 'p' : { strcpy(fieldname
, argv
[optind
- 1]);
66 if(opt
==2){fieldNameSpecified
= true;break;}
70 if(opt
==2) {isDirect
=true;break;}
72 case 'c' : {strcpy(condition
,argv
[optind
- 1]); conditionval
= true; break; }// condition for selelcted records by :Jitendra
73 case 'f' : {strcpy(fieldlist
,argv
[optind
- 1]);fieldlistval
= true ;break; }
74 case '?' : { opt
= 10; break; } //print help
75 case 'R' : { opt
= 3; break; } //recover all the tables
76 case 's' : { tableDefinition
=false; break; } //do not get the schema information from target db
77 case 'r' : { opt
= 4; break; } //reload the table
78 case 'u' : { opt
= 5; break; } //unload the table
88 //printf("%s %s \n", username, password);
89 if (username
[0] == '\0' )
91 strcpy(username
, "root");
92 strcpy(password
, "manager");
95 CacheTableLoader cacheLoader
;
96 cacheLoader
.setConnParam(username
, password
);
99 cacheLoader
.setCondition(condition
);}// new one
101 cacheLoader
.setFieldListVal(fieldlist
);}
103 cacheLoader
.setTable(tablename
);
104 if(fieldNameSpecified
){ cacheLoader
.setFieldName(fieldname
); }
105 rv
= cacheLoader
.isTableCached();
107 rv
= cacheLoader
.load(tableDefinition
);
109 cacheLoader
.addToCacheTableFile(isDirect
);
113 printf("Table is already cached, unload table by \" cachetable -t <tablename> -u\" and then try \n");
116 }else if (opt
==3) //recover
118 rv
= cacheLoader
.recoverAllCachedTables();
119 if (rv
!= OK
) exit (1);
120 }else if (opt
==4) //reload
122 if (!tableNameSpecified
)
124 printf("Table name is not specified. Check usage with ? \n");
127 cacheLoader
.setTable(tablename
);
128 rv
= cacheLoader
.reload();
129 if (rv
!= OK
) exit (1);
130 }else if (opt
==5) //unload
132 if (!tableNameSpecified
)
134 printf("Table name is not specified. Check usage with ? option\n");
137 cacheLoader
.setTable(tablename
);
138 rv
= cacheLoader
.unload(tableDefinition
);
139 if (rv
!= OK
) exit (1);
140 rv
= cacheLoader
.removeFromCacheTableFile();
141 if (rv
!= OK
) exit (2);