src/tools/catalog.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 <DatabaseManagerImpl.h>
00018 #include <Database.h>
00019 #include <TableImpl.h>
00020 void printUsage()
00021 {
00022    printf("Usage: catalog [-u username] [-p passwd] [-l] [-i] [-d] [-T table] [-I index] [-D <lock|trans|proc>]\n");
00023    printf("       l -> list all table with field information\n");
00024    printf("       i -> reinitialize catalog tables. Drops all tables.\n");
00025    printf("       d -> print db usage statistics\n");
00026    printf("       T -> list table information\n");
00027    printf("       I -> list index information\n");
00028    printf("       D -> print debug information for system tables\n");
00029    printf("Note: If multiple options are specified, last one will be considered.\n");
00030    return;
00031   
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 = 0;
00041     char name[IDENTIFIER_LENGTH];
00042     while ((c = getopt(argc, argv, "u:p:T:I:D:lid?")) != EOF) 
00043     {
00044         switch (c)
00045         {
00046             case 'u' : { strcpy(username, argv[optind - 1]); opt=1; break; }
00047             case 'p' : { strcpy(password, argv[optind - 1]); opt=1; break; }
00048             case 'T' : { strcpy(name, argv[optind - 1]); opt = 5; break; }
00049             case 'I' : { strcpy(name, argv[optind - 1]); opt = 6; break; }
00050             case 'D' : { strcpy(name, argv[optind - 1]); opt = 7; break; }
00051             case 'l' : { opt = 2; break; } //list all the table with field info
00052             case 'i' : { opt = 3; break;  }//reinitialize the catalog table
00053             case 'd' : { opt = 4; break;  }//print db usage statistics
00054             case '?' : { opt = 10; break; } //print help 
00055             default: opt=1; //list all the tables
00056 
00057         }
00058     }//while options
00059     if (opt == 10) {
00060         printUsage();
00061         return 0;
00062     }
00063 
00064     //printf("%s %s \n", username, password);
00065     if (username[0] == '\0' )
00066     {
00067         strcpy(username, "root");
00068         strcpy(password, "manager");
00069         opt=1;//if username is not specified, just list all table names
00070     }
00071     
00072     Connection conn;
00073     DbRetVal rv = conn.open(username, password);
00074     if (rv != OK) return 1;
00075     DatabaseManagerImpl *dbMgr = (DatabaseManagerImpl*) conn.getDatabaseManager();
00076     if (dbMgr == NULL) { printf("Auth failed\n"); return 2;}
00077     List tableList = dbMgr->getAllTableNames();
00078     ListIterator iter = tableList.getIterator();
00079     Identifier *elem = NULL;
00080     int ret =0;
00081     if (opt == 1) {
00082         printf("<TableNames>\n");
00083         int count =0;
00084         while (iter.hasElement())
00085         {
00086             elem = (Identifier*) iter.nextElement();
00087             count++;
00088             printf("  <TableName> %s </TableName>\n", elem->name);
00089         }
00090         if (count ==0) printf("  <No tables exist></No tables exist>\n");
00091         printf("</TableNames>\n");
00092     }
00093     else if (opt ==2) 
00094     {
00095         printf("<Table Information of all tables>\n");
00096         int count =0;
00097         while (iter.hasElement())
00098         {
00099             elem = (Identifier*) iter.nextElement();
00100             printf("  <TableInfo> \n");
00101             printf("    <TableName> %s </TableName>\n", elem->name);
00102             Table *table = dbMgr->openTable(elem->name);
00103             FieldInfo *info = new FieldInfo();
00104             List fNameList = table->getFieldNameList();
00105             ListIterator fNameIter = fNameList.getIterator();
00106             count++;
00107             while (fNameIter.hasElement()) {
00108                  elem = (Identifier*) fNameIter.nextElement();
00109                  table->getFieldInfo((const char*)elem->name, info);
00110                  printf("      <FieldInfo>\n");
00111                  printf("        <FieldName> %s </FieldName>\n", elem->name);
00112                  printf("        <Type> %d </Type>\n", info->type);
00113                  printf("        <Length> %d </Length>\n", info->length);
00114                  printf("        <Primary> %d </Primary>\n", info->isPrimary);
00115                  printf("        <Null> %d </Null>\n", info->isNull);
00116                  printf("        <Default> %d </Default>\n", info->isDefault);
00117                  printf("        <DefaultValue> %s </DefaultValue>\n", info->defaultValueBuf);
00118                  printf("      </FieldInfo>\n");
00119 
00120             }
00121             delete info;
00122             dbMgr->closeTable(table);
00123             printf("  </TableInfo> \n");
00124 
00125         }
00126         if (count == 0) printf("  <No tables exist></No tables exist>\n");
00127         printf("</Table Information of all tables>\n");
00128     }else if (opt == 3)
00129     {
00130         if (!dbMgr->isAnyOneRegistered()) {
00131             printf("<DropTable>\n");
00132             int count =0; 
00133             while (iter.hasElement())
00134             {
00135                 elem = (Identifier*) iter.nextElement();
00136                 printf("  <TableName> %s </TableName>\n", elem->name);
00137                 dbMgr->dropTable(elem->name);
00138                 count++;
00139             }
00140             if (count ==0) printf("  <No tables exist></No tables exist>\n");
00141             printf("</DropTable>\n");
00142 
00143         } else {
00144             printf("Catalog not initialized. Found registered process\n");
00145             ret =1;
00146         }
00147     }else if (opt == 4)
00148     {
00149         printf("<Database Usage Statistics>\n");
00150         Database *db = dbMgr->sysDb();
00151         db->printStatistics();
00152         dbMgr->printUsageStatistics();
00153         db = dbMgr->db();
00154         db->printStatistics();
00155         printf("</Database Usage Statistics>\n");
00156 
00157     }else if (opt == 5)
00158     {
00159         printf("<Table Info> \n");
00160 
00161         TableImpl *table = (TableImpl*) dbMgr->openTable(name);
00162         if (table != NULL) {
00163             table->printInfo();
00164             dbMgr->closeTable(table);
00165         }else {
00166            printf("  <Table Not Found> %s </Table Not Found>\n", name);
00167            ret =1;
00168         }
00169         printf("</Table Info> \n");
00170 
00171     }else if (opt == 6)
00172     {
00173         printf("<Index Info> \n");
00174         rv = dbMgr->printIndexInfo(name);//TODO::handle no index case to return 1
00175         if (rv != OK)
00176         {
00177            printf("  <Index Not Found> %s </Index Not Found>\n", name);
00178            ret =1;
00179         }
00180         printf("<Index Info> \n");
00181     }else if (opt == 7)
00182     {
00183         if (strcmp(name, "lock")  == 0) 
00184         {
00185             dbMgr->printDebugLockInfo();
00186         }
00187         else if (strcmp(name, "trans")  == 0)
00188         {
00189             dbMgr->printDebugTransInfo();
00190         }
00191         else if (strcmp(name, "proc")  == 0)
00192         {
00193             dbMgr->printDebugProcInfo();
00194         }
00195         else {
00196            printf("Wrong argument passed\n");
00197            printUsage();
00198            ret =1;
00199         }
00200     }
00201     tableList.reset();
00202     conn.close();
00203     return ret;
00204 }

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