src/tools/cachetable.cxx

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by www.databasecache.com                           *
00003  *   Contact: praba_tuty@databasecache.com                                 *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
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; } //print help 
00057             case 'R' : { opt = 3; break; } //recover all the tables
00058             case 's' : { tableDefinition=false; break; } //do not get the schema information from target db
00059             case 'r' : { opt = 4; break; } //reload the table
00060             case 'u' : { opt = 5; break; } //unload the table
00061             default: opt=10; 
00062 
00063         }
00064     }//while options
00065     if (opt == 10) {
00066         printUsage();
00067         return 0;
00068     }
00069 
00070     //printf("%s %s \n", username, password);
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) //recover
00086     {
00087         rv = cacheLoader.recoverAllCachedTables();
00088         if (rv != OK) exit (1);
00089     }else if (opt==4) //reload
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) //unload
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 }

Generated on Mon Jun 9 22:37:15 2008 for csql by  doxygen 1.4.7