cleanup
[csql.git] / src / tools / catalog.cxx
blob5a9bad04c04ba7e4a8365a7dce7500d429a64c79
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 <os.h>
17 #include <CSql.h>
18 #include <DatabaseManagerImpl.h>
19 #include <Database.h>
20 #include <TableImpl.h>
21 #include <CacheTableLoader.h>
22 #include <TableConfig.h>
24 void printUsage()
26 printf("Usage: catalog [-u username] [-p passwd] [-l] [-i] [-d] [-T table] [-I index] [-D <lock|trans|proc|chunk>]\n");
27 printf(" l -> list all table with field information\n");
28 printf(" list all Index information\n");
29 printf(" i -> reinitialize catalog tables. Drops all tables.\n");
30 printf(" d -> print db usage statistics\n");
31 printf(" T -> list table information\n");
32 printf(" I -> list index information\n");
33 printf(" D -> print debug information for system tables\n");
34 printf("Note: If multiple options are specified, last one will be considered.\n");
35 return;
39 int main(int argc, char **argv)
41 char username[IDENTIFIER_LENGTH];
42 username [0] = '\0';
43 char password[IDENTIFIER_LENGTH];
44 password [0] = '\0';
45 int c = 0, opt = 0;
46 char name[IDENTIFIER_LENGTH];
47 while ((c = getopt(argc, argv, "u:p:T:I:D:licdsS?")) != EOF)
49 switch (c)
51 case 'u' : { strcpy(username, argv[optind - 1]); opt=1; break; }
52 case 'p' : { strcpy(password, argv[optind - 1]); opt=1; break; }
53 case 'T' : { strcpy(name, argv[optind - 1]); opt = 5; break; }
54 case 'I' : { strcpy(name, argv[optind - 1]); opt = 6; break; }
55 case 'D' : { strcpy(name, argv[optind - 1]); opt = 7; break; }
56 case 'l' : { opt = 2; break; } //list all the table with field info
57 case 'i' : { opt = 3; break; }//reinitialize the catalog table
58 case 'd' : { opt = 4; break; }//print db usage statistics
59 case 's' : { if(opt == 6) opt=11;
60 else printf("Use -I IndexName -s\n");
61 break; }//print db usage statistics
62 case 'S' : { if(opt == 6) opt=12;
63 else printf("Use -I IndexName -S\n");
64 break; }//print db usage statistics
65 case '?' : { opt = 10; break; } //print help
66 default: opt=1; //list all the tables
69 }//while options
70 if (opt == 10) {
71 printUsage();
72 return 0;
75 //printf("%s %s \n", username, password);
76 if (username[0] == '\0' )
78 strcpy(username, I_USER);
79 strcpy(password, I_PASS);
80 opt=1;//if username is not specified, just list all table names
83 Connection conn;
84 DbRetVal rv = conn.open(username, password);
85 if (rv != OK) return 1;
86 DatabaseManagerImpl *dbMgr = (DatabaseManagerImpl*) conn.getDatabaseManager();
87 if (dbMgr == NULL) { printf("Auth failed\n"); return 2;}
88 List tableList = dbMgr->getAllTableNames();
89 ListIterator iter = tableList.getIterator();
90 Identifier *elem = NULL;
91 int ret =0;
92 if (opt == 1) {
93 printf("<TableNames>\n");
94 int count =0;
95 while (iter.hasElement())
97 elem = (Identifier*) iter.nextElement();
98 count++;
99 printf(" <TableName> %s </TableName>\n", elem->name);
101 if (count ==0) printf(" <No tables exist></No tables exist>\n");
102 printf("</TableNames>\n");
104 else if (opt ==2)
106 printf("<Table Information of all tables>\n");
107 int count =0;
108 while (iter.hasElement())
110 elem = (Identifier*) iter.nextElement();
111 printf(" <TableInfo> \n");
112 printf(" <TableName> %s </TableName>\n", elem->name);
113 Table *table = dbMgr->openTable(elem->name);
114 FieldInfo *info = new FieldInfo();
115 List fNameList = table->getFieldNameList();
116 ListIterator fNameIter = fNameList.getIterator();
117 count++;
118 char fieldName[IDENTIFIER_LENGTH];
119 while (fNameIter.hasElement()) {
120 elem = (Identifier*) fNameIter.nextElement();
121 Table::getFieldNameAlone(elem->name, fieldName);
122 table->getFieldInfo((const char*)elem->name, info);
123 printf(" <FieldInfo>\n");
124 printf(" <FieldName> %s </FieldName>\n", fieldName);
125 printf(" <Type> %d </Type>\n", info->type);
126 printf(" <Length> %d </Length>\n", info->length);
127 printf(" <Primary> %d </Primary>\n", info->isPrimary);
128 printf(" <Null> %d </Null>\n", info->isNull);
129 printf(" <Default> %d </Default>\n", info->isDefault);
130 printf(" <DefaultValue> %s </DefaultValue>\n", info->defaultValueBuf);
131 printf(" </FieldInfo>\n");
134 delete info;
135 dbMgr->closeTable(table);
136 printf(" </TableInfo> \n");
139 if (count == 0) printf(" <No tables exist></No tables exist>\n");
140 printf("</Table Information of all tables>\n");
141 printf("<Index Information of all Indexs>\n");
142 CatalogTableINDEXFIELD cIndexField(dbMgr->sysDb());
143 cIndexField.printAllIndex();
144 printf("</Index Information of all Indexs>\n");
146 }else if (opt == 3)
148 if (!dbMgr->isAnyOneRegistered()) {
149 printf("<DropTable>\n");
150 int count =0;
151 while (iter.hasElement())
153 elem = (Identifier*) iter.nextElement();
154 #ifndef MMDB
155 rv=TableConf::config.isTableCached(elem->name);
156 if(rv!=OK){
157 printf(" <TableName> %s </TableName>\n", elem->name);
158 dbMgr->dropTable(elem->name);
160 #else
161 printf(" <TableName> %s </TableName>\n", elem->name);
162 dbMgr->dropTable(elem->name);
163 #endif
164 count++;
166 //TODO::If durability is on, remove chkpt and redo log file
167 if (count ==0) printf(" <No tables exist></No tables exist>\n");
168 printf("</DropTable>\n");
170 } else {
171 printf("Catalog not initialized. Found registered process\n");
172 ret =1;
174 }else if (opt == 4)
176 printf("<Database Usage Statistics>\n");
177 Database *db = dbMgr->sysDb();
178 db->printStatistics();
179 dbMgr->printUsageStatistics();
180 db = dbMgr->db();
181 db->printStatistics();
182 printf("</Database Usage Statistics>\n");
184 }else if (opt == 5)
186 printf("<Table Info> \n");
188 TableImpl *table = (TableImpl*) dbMgr->openTable(name);
189 if (table != NULL) {
190 table->printInfo();
191 dbMgr->closeTable(table);
192 }else {
193 printf(" <Table Not Found> %s </Table Not Found>\n", name);
194 ret =1;
196 printf("</Table Info> \n");
198 }else if (opt == 6)
200 printf("<Index Info> \n");
201 rv = dbMgr->printIndexInfo(name);//TODO::handle no index case to return 1
202 if (rv != OK)
204 printf(" <Index Not Found> %s </Index Not Found>\n", name);
205 ret =1;
207 printf("<Index Info> \n");
208 }else if (opt == 7)
210 if (strcmp(name, "lock") == 0)
212 dbMgr->printDebugLockInfo();
214 else if (strcmp(name, "trans") == 0)
216 dbMgr->printDebugTransInfo();
218 else if (strcmp(name, "proc") == 0)
220 dbMgr->printDebugProcInfo();
222 else if(strcmp(name,"chunk") ==0)
224 Database *db = dbMgr->sysDb();
225 Chunk *chunk;
226 int id=1;
227 printf("<Chunk information>\n");
228 printf(" <System Chunk >\n");
229 chunk=db->getSystemDatabaseChunk(UserChunkTableId);
230 chunk->print();
231 while(id<MAX_CHUNKS)
233 chunk=db->getSystemDatabaseChunk(id);
234 if((chunk->getChunkID())!=0){
235 chunk->print();
237 id++;
239 printf(" </System Chunk >\n");
240 printf(" <User Chunk >\n");
241 chunk=db->getSystemDatabaseChunk(UserChunkTableId);
242 size_t size=chunk->getSize();
243 int noOfDataNodes=os::floor((PAGE_SIZE - sizeof(PageInfo))/size);
244 Page* page=chunk->getFirstPage();
245 int i=0;
246 Chunk *chk;
247 while(page)
249 char *data = ((char*)page) + sizeof(PageInfo);
250 for (i = 0; i< noOfDataNodes; i++)
252 if (*((int*)data) == 1)
254 chk=(Chunk*)((int*)data+1);
255 chk->print();
257 data = data + size;
259 page = (PageInfo*)(((PageInfo*)page)->nextPage_) ;
263 printf(" </User Chunk >\n");
264 printf("</Chunk information>\n");
266 else {
267 printf("Wrong argument passed\n");
268 printUsage();
269 ret =1;
271 } else if (opt == 11){
272 dbMgr->printTreeIndexNodeInfo(name, true);
273 } else if (opt == 12){
274 dbMgr->printTreeIndexNodeInfo(name,false);
276 iter.reset();
277 while (iter.hasElement()) delete iter.nextElement();
278 tableList.reset();
279 conn.close();
280 return ret;