TableImpl class is added with 3 more fields
[csql.git] / src / tools / catalog.cxx
blob859a3019cdd8dc5cb80f809cce8fb1fabd327c38
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|chunk>]\n");
23 printf(" l -> list all table with field information\n");
24 printf(" list all Index information\n");
25 printf(" i -> reinitialize catalog tables. Drops all tables.\n");
26 printf(" d -> print db usage statistics\n");
27 printf(" T -> list table information\n");
28 printf(" I -> list index information\n");
29 printf(" D -> print debug information for system tables\n");
30 printf("Note: If multiple options are specified, last one will be considered.\n");
31 return;
35 int main(int argc, char **argv)
37 char username[IDENTIFIER_LENGTH];
38 username [0] = '\0';
39 char password[IDENTIFIER_LENGTH];
40 password [0] = '\0';
41 int c = 0, opt = 0;
42 char name[IDENTIFIER_LENGTH];
43 while ((c = getopt(argc, argv, "u:p:T:I:D:licd?")) != EOF)
45 switch (c)
47 case 'u' : { strcpy(username, argv[optind - 1]); opt=1; break; }
48 case 'p' : { strcpy(password, argv[optind - 1]); opt=1; break; }
49 case 'T' : { strcpy(name, argv[optind - 1]); opt = 5; break; }
50 case 'I' : { strcpy(name, argv[optind - 1]); opt = 6; break; }
51 case 'D' : { strcpy(name, argv[optind - 1]); opt = 7; break; }
52 case 'l' : { opt = 2; break; } //list all the table with field info
53 case 'i' : { opt = 3; break; }//reinitialize the catalog table
54 case 'd' : { opt = 4; break; }//print db usage statistics
55 case '?' : { opt = 10; break; } //print help
56 default: opt=1; //list all the tables
59 }//while options
60 if (opt == 10) {
61 printUsage();
62 return 0;
65 //printf("%s %s \n", username, password);
66 if (username[0] == '\0' )
68 strcpy(username, "root");
69 strcpy(password, "manager");
70 opt=1;//if username is not specified, just list all table names
73 Connection conn;
74 DbRetVal rv = conn.open(username, password);
75 if (rv != OK) return 1;
76 DatabaseManagerImpl *dbMgr = (DatabaseManagerImpl*) conn.getDatabaseManager();
77 if (dbMgr == NULL) { printf("Auth failed\n"); return 2;}
78 List tableList = dbMgr->getAllTableNames();
79 ListIterator iter = tableList.getIterator();
80 Identifier *elem = NULL;
81 int ret =0;
82 if (opt == 1) {
83 printf("<TableNames>\n");
84 int count =0;
85 while (iter.hasElement())
87 elem = (Identifier*) iter.nextElement();
88 count++;
89 printf(" <TableName> %s </TableName>\n", elem->name);
91 if (count ==0) printf(" <No tables exist></No tables exist>\n");
92 printf("</TableNames>\n");
94 else if (opt ==2)
96 printf("<Table Information of all tables>\n");
97 int count =0;
98 while (iter.hasElement())
100 elem = (Identifier*) iter.nextElement();
101 printf(" <TableInfo> \n");
102 printf(" <TableName> %s </TableName>\n", elem->name);
103 Table *table = dbMgr->openTable(elem->name);
104 FieldInfo *info = new FieldInfo();
105 List fNameList = table->getFieldNameList();
106 ListIterator fNameIter = fNameList.getIterator();
107 count++;
108 char fieldName[IDENTIFIER_LENGTH];
109 while (fNameIter.hasElement()) {
110 elem = (Identifier*) fNameIter.nextElement();
111 Table::getFieldNameAlone(elem->name, fieldName);
112 table->getFieldInfo((const char*)elem->name, info);
113 printf(" <FieldInfo>\n");
114 printf(" <FieldName> %s </FieldName>\n", fieldName);
115 printf(" <Type> %d </Type>\n", info->type);
116 printf(" <Length> %d </Length>\n", info->length);
117 printf(" <Primary> %d </Primary>\n", info->isPrimary);
118 printf(" <Null> %d </Null>\n", info->isNull);
119 printf(" <Default> %d </Default>\n", info->isDefault);
120 printf(" <DefaultValue> %s </DefaultValue>\n", info->defaultValueBuf);
121 printf(" </FieldInfo>\n");
124 delete info;
125 dbMgr->closeTable(table);
126 printf(" </TableInfo> \n");
129 if (count == 0) printf(" <No tables exist></No tables exist>\n");
130 printf("</Table Information of all tables>\n");
131 printf("<Index Information of all Indexs>\n");
132 CatalogTableINDEXFIELD cIndexField(dbMgr->sysDb());
133 cIndexField.printAllIndex();
134 printf("</Index Information of all Indexs>\n");
136 }else if (opt == 3)
138 if (!dbMgr->isAnyOneRegistered()) {
139 printf("<DropTable>\n");
140 int count =0;
141 while (iter.hasElement())
143 elem = (Identifier*) iter.nextElement();
144 printf(" <TableName> %s </TableName>\n", elem->name);
145 dbMgr->dropTable(elem->name);
146 count++;
148 if (count ==0) printf(" <No tables exist></No tables exist>\n");
149 printf("</DropTable>\n");
151 } else {
152 printf("Catalog not initialized. Found registered process\n");
153 ret =1;
155 }else if (opt == 4)
157 printf("<Database Usage Statistics>\n");
158 Database *db = dbMgr->sysDb();
159 db->printStatistics();
160 dbMgr->printUsageStatistics();
161 db = dbMgr->db();
162 db->printStatistics();
163 printf("</Database Usage Statistics>\n");
165 }else if (opt == 5)
167 printf("<Table Info> \n");
169 TableImpl *table = (TableImpl*) dbMgr->openTable(name);
170 if (table != NULL) {
171 table->printInfo();
172 dbMgr->closeTable(table);
173 }else {
174 printf(" <Table Not Found> %s </Table Not Found>\n", name);
175 ret =1;
177 printf("</Table Info> \n");
179 }else if (opt == 6)
181 printf("<Index Info> \n");
182 rv = dbMgr->printIndexInfo(name);//TODO::handle no index case to return 1
183 if (rv != OK)
185 printf(" <Index Not Found> %s </Index Not Found>\n", name);
186 ret =1;
188 printf("<Index Info> \n");
189 }else if (opt == 7)
191 if (strcmp(name, "lock") == 0)
193 dbMgr->printDebugLockInfo();
195 else if (strcmp(name, "trans") == 0)
197 dbMgr->printDebugTransInfo();
199 else if (strcmp(name, "proc") == 0)
201 dbMgr->printDebugProcInfo();
203 else if(strcmp(name,"chunk") ==0)
205 Database *db = dbMgr->sysDb();
206 Chunk *chunk;
207 int id=1;
208 printf("<Chunk information>\n");
209 printf(" <System Chunk >\n");
210 chunk=db->getSystemDatabaseChunk(UserChunkTableId);
211 chunk->print();
212 while(id<MAX_CHUNKS)
214 chunk=db->getSystemDatabaseChunk(id);
215 if((chunk->getChunkID())!=0){
216 chunk->print();
218 id++;
220 printf(" </System Chunk >\n");
221 printf(" <User Chunk >\n");
222 chunk=db->getSystemDatabaseChunk(UserChunkTableId);
223 size_t size=chunk->getSize();
224 int noOfDataNodes=os::floor((PAGE_SIZE - sizeof(PageInfo))/size);
225 Page* page=chunk->getFirstPage();
226 int i=0;
227 Chunk *chk;
228 while(page)
230 char *data = ((char*)page) + sizeof(PageInfo);
231 for (i = 0; i< noOfDataNodes; i++)
233 if (*((int*)data) == 1)
235 chk=(Chunk*)((int*)data+1);
236 chk->print();
238 data = data + size;
240 page = (PageInfo*)(((PageInfo*)page)->nextPage_) ;
244 printf(" </User Chunk >\n");
245 printf("</Chunk information>\n");
247 else {
248 printf("Wrong argument passed\n");
249 printUsage();
250 ret =1;
253 tableList.reset();
254 conn.close();
255 return ret;