typo
[csql.git] / src / tools / catalog.cxx
blob248eac2aff5ff3c37c17d0bbbb2778df5a560594
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 <DatabaseManagerImpl.h>
18 #include <Database.h>
19 #include <TableImpl.h>
20 void printUsage()
22 printf("Usage: catalog [-u username] [-p passwd] [-l] [-i] [-d] [-T table] [-I index] [-D <lock|trans|proc>]\n");
23 printf(" l -> list all table with field information\n");
24 printf(" i -> reinitialize catalog tables. Drops all tables.\n");
25 printf(" d -> print db usage statistics\n");
26 printf(" T -> list table information\n");
27 printf(" I -> list index information\n");
28 printf(" D -> print debug information for system tables\n");
29 printf("Note: If multiple options are specified, last one will be considered.\n");
30 return;
34 int main(int argc, char **argv)
36 char username[IDENTIFIER_LENGTH];
37 username [0] = '\0';
38 char password[IDENTIFIER_LENGTH];
39 password [0] = '\0';
40 int c = 0, opt = 0;
41 char name[IDENTIFIER_LENGTH];
42 while ((c = getopt(argc, argv, "u:p:T:I:D:lid?")) != EOF)
44 switch (c)
46 case 'u' : { strcpy(username, argv[optind - 1]); opt=1; break; }
47 case 'p' : { strcpy(password, argv[optind - 1]); opt=1; break; }
48 case 'T' : { strcpy(name, argv[optind - 1]); opt = 5; break; }
49 case 'I' : { strcpy(name, argv[optind - 1]); opt = 6; break; }
50 case 'D' : { strcpy(name, argv[optind - 1]); opt = 7; break; }
51 case 'l' : { opt = 2; break; } //list all the table with field info
52 case 'i' : { opt = 3; break; }//reinitialize the catalog table
53 case 'd' : { opt = 4; break; }//print db usage statistics
54 case '?' : { opt = 10; break; } //print help
55 default: opt=1; //list all the tables
58 }//while options
59 if (opt == 10) {
60 printUsage();
61 return 0;
64 //printf("%s %s \n", username, password);
65 if (username[0] == '\0' )
67 strcpy(username, "root");
68 strcpy(password, "manager");
69 opt=1;//if username is not specified, just list all table names
72 Connection conn;
73 DbRetVal rv = conn.open(username, password);
74 if (rv != OK) return 1;
75 DatabaseManagerImpl *dbMgr = (DatabaseManagerImpl*) conn.getDatabaseManager();
76 if (dbMgr == NULL) { printf("Auth failed\n"); return 2;}
77 List tableList = dbMgr->getAllTableNames();
78 ListIterator iter = tableList.getIterator();
79 Identifier *elem = NULL;
80 int ret =0;
81 if (opt == 1) {
82 printf("<TableNames>\n");
83 int count =0;
84 while (iter.hasElement())
86 elem = (Identifier*) iter.nextElement();
87 count++;
88 printf(" <TableName> %s </TableName>\n", elem->name);
90 if (count ==0) printf(" <No tables exist></No tables exist>\n");
91 printf("</TableNames>\n");
93 else if (opt ==2)
95 printf("<Table Information of all tables>\n");
96 int count =0;
97 while (iter.hasElement())
99 elem = (Identifier*) iter.nextElement();
100 printf(" <TableInfo> \n");
101 printf(" <TableName> %s </TableName>\n", elem->name);
102 Table *table = dbMgr->openTable(elem->name);
103 FieldInfo *info = new FieldInfo();
104 List fNameList = table->getFieldNameList();
105 ListIterator fNameIter = fNameList.getIterator();
106 count++;
107 while (fNameIter.hasElement()) {
108 elem = (Identifier*) fNameIter.nextElement();
109 table->getFieldInfo((const char*)elem->name, info);
110 printf(" <FieldInfo>\n");
111 printf(" <FieldName> %s </FieldName>\n", elem->name);
112 printf(" <Type> %d </Type>\n", info->type);
113 printf(" <Length> %d </Length>\n", info->length);
114 printf(" <Primary> %d </Primary>\n", info->isPrimary);
115 printf(" <Null> %d </Null>\n", info->isNull);
116 printf(" <Default> %d </Default>\n", info->isDefault);
117 printf(" <DefaultValue> %s </DefaultValue>\n", info->defaultValueBuf);
118 printf(" </FieldInfo>\n");
121 delete info;
122 dbMgr->closeTable(table);
123 printf(" </TableInfo> \n");
126 if (count == 0) printf(" <No tables exist></No tables exist>\n");
127 printf("</Table Information of all tables>\n");
128 }else if (opt == 3)
130 if (!dbMgr->isAnyOneRegistered()) {
131 printf("<DropTable>\n");
132 int count =0;
133 while (iter.hasElement())
135 elem = (Identifier*) iter.nextElement();
136 printf(" <TableName> %s </TableName>\n", elem->name);
137 dbMgr->dropTable(elem->name);
138 count++;
140 if (count ==0) printf(" <No tables exist></No tables exist>\n");
141 printf("</DropTable>\n");
143 } else {
144 printf("Catalog not initialized. Found registered process\n");
145 ret =1;
147 }else if (opt == 4)
149 printf("<Database Usage Statistics>\n");
150 Database *db = dbMgr->sysDb();
151 db->printStatistics();
152 dbMgr->printUsageStatistics();
153 db = dbMgr->db();
154 db->printStatistics();
155 printf("</Database Usage Statistics>\n");
157 }else if (opt == 5)
159 printf("<Table Info> \n");
161 TableImpl *table = (TableImpl*) dbMgr->openTable(name);
162 if (table != NULL) {
163 table->printInfo();
164 dbMgr->closeTable(table);
165 }else {
166 printf(" <Table Not Found> %s </Table Not Found>\n", name);
167 ret =1;
169 printf("</Table Info> \n");
171 }else if (opt == 6)
173 printf("<Index Info> \n");
174 rv = dbMgr->printIndexInfo(name);//TODO::handle no index case to return 1
175 if (rv != OK)
177 printf(" <Index Not Found> %s </Index Not Found>\n", name);
178 ret =1;
180 printf("<Index Info> \n");
181 }else if (opt == 7)
183 if (strcmp(name, "lock") == 0)
185 dbMgr->printDebugLockInfo();
187 else if (strcmp(name, "trans") == 0)
189 dbMgr->printDebugTransInfo();
191 else if (strcmp(name, "proc") == 0)
193 dbMgr->printDebugProcInfo();
195 else {
196 printf("Wrong argument passed\n");
197 printUsage();
198 ret =1;
201 tableList.reset();
202 conn.close();
203 return ret;