adding statment length
[csql.git] / src / tools / catalog.cxx
blob48a3c18fc2cf0dfaf44e597d4abe17c0683e39e0
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 os::signal(SIGCSQL1, SIG_IGN);
87 DatabaseManagerImpl *dbMgr = (DatabaseManagerImpl*) conn.getDatabaseManager();
88 if (dbMgr == NULL) { printf("Auth failed\n"); return 2;}
89 List tableList = dbMgr->getAllTableNames();
90 ListIterator iter = tableList.getIterator();
91 Identifier *elem = NULL;
92 int ret =0;
93 if (opt == 1) {
94 printf("<TableNames>\n");
95 int count =0;
96 while (iter.hasElement())
98 elem = (Identifier*) iter.nextElement();
99 count++;
100 printf(" <TableName> %s </TableName>\n", elem->name);
102 if (count ==0) printf(" <No tables exist></No tables exist>\n");
103 printf("</TableNames>\n");
105 else if (opt ==2)
107 printf("<Table Information of all tables>\n");
108 int count =0;
109 while (iter.hasElement())
111 elem = (Identifier*) iter.nextElement();
112 printf(" <TableInfo> \n");
113 printf(" <TableName> %s </TableName>\n", elem->name);
114 Table *table = dbMgr->openTable(elem->name);
115 if (NULL == table) {
116 printError(ErrSysInternal, "Unable to open table %s", elem->name);
117 break;
119 FieldInfo *info = new FieldInfo();
120 List fNameList = table->getFieldNameList();
121 ListIterator fNameIter = fNameList.getIterator();
122 count++;
123 char fieldName[IDENTIFIER_LENGTH];
124 while (fNameIter.hasElement()) {
125 elem = (Identifier*) fNameIter.nextElement();
126 Table::getFieldNameAlone(elem->name, fieldName);
127 table->getFieldInfo((const char*)elem->name, info);
128 printf(" <FieldInfo>\n");
129 printf(" <FieldName> %s </FieldName>\n", fieldName);
130 printf(" <Type> %d </Type>\n", info->type);
131 printf(" <Length> %d </Length>\n", info->length);
132 printf(" <Primary> %d </Primary>\n", info->isPrimary);
133 printf(" <Null> %d </Null>\n", info->isNull);
134 printf(" <Default> %d </Default>\n", info->isDefault);
135 printf(" <DefaultValue> %s </DefaultValue>\n", info->defaultValueBuf);
136 printf(" </FieldInfo>\n");
139 delete info;
140 dbMgr->closeTable(table);
141 printf(" </TableInfo> \n");
144 if (count == 0) printf(" <No tables exist></No tables exist>\n");
145 printf("</Table Information of all tables>\n");
146 printf("<Index Information of all Indexs>\n");
147 CatalogTableINDEXFIELD cIndexField(dbMgr->sysDb());
148 cIndexField.printAllIndex();
149 printf("</Index Information of all Indexs>\n");
151 }else if (opt == 3)
153 if (!dbMgr->isAnyOneRegistered()) {
154 printf("<DropTable>\n");
155 int count =0;
156 while (iter.hasElement())
158 elem = (Identifier*) iter.nextElement();
159 #ifndef MMDB
160 rv=TableConf::config.isTableCached(elem->name);
161 if(rv!=OK){
162 printf(" <TableName> %s </TableName>\n", elem->name);
163 dbMgr->dropTable(elem->name);
165 #else
166 printf(" <TableName> %s </TableName>\n", elem->name);
167 dbMgr->dropTable(elem->name);
168 #endif
169 count++;
171 //TODO::If durability is on, remove chkpt and redo log file
172 if (count ==0) printf(" <No tables exist></No tables exist>\n");
173 printf("</DropTable>\n");
175 } else {
176 printf("Catalog not initialized. Found registered process\n");
177 ret =1;
179 }else if (opt == 4)
181 printf("<Database Usage Statistics>\n");
182 Database *db = dbMgr->sysDb();
183 db->printStatistics();
184 dbMgr->printUsageStatistics();
185 db = dbMgr->db();
186 db->printStatistics();
187 printf("</Database Usage Statistics>\n");
189 }else if (opt == 5)
191 printf("<Table Info> \n");
193 TableImpl *table = (TableImpl*) dbMgr->openTable(name);
194 if (table != NULL) {
195 table->printInfo();
196 dbMgr->closeTable(table);
197 }else {
198 printf(" <Table Not Found> %s </Table Not Found>\n", name);
199 ret =1;
201 printf("</Table Info> \n");
203 }else if (opt == 6)
205 printf("<Index Info> \n");
206 rv = dbMgr->printIndexInfo(name);//TODO::handle no index case to return 1
207 if (rv != OK)
209 printf(" <Index Not Found> %s </Index Not Found>\n", name);
210 ret =1;
212 printf("<Index Info> \n");
213 }else if (opt == 7)
215 if (strcmp(name, "lock") == 0)
217 dbMgr->printDebugLockInfo();
219 else if (strcmp(name, "trans") == 0)
221 dbMgr->printDebugTransInfo();
223 else if (strcmp(name, "proc") == 0)
225 dbMgr->printDebugProcInfo();
227 else if(strcmp(name,"chunk") ==0)
229 Database *db = dbMgr->sysDb();
230 Chunk *chunk;
231 int id=1;
232 printf("<Chunk information>\n");
233 printf(" <System Chunk >\n");
234 chunk=db->getSystemDatabaseChunk(UserChunkTableId);
235 chunk->print();
236 while(id<MAX_CHUNKS)
238 chunk=db->getSystemDatabaseChunk(id);
239 if((chunk->getChunkID())!=0){
240 chunk->print();
242 id++;
244 printf(" </System Chunk >\n");
245 printf(" <User Chunk >\n");
246 chunk=db->getSystemDatabaseChunk(UserChunkTableId);
247 size_t size=chunk->getSize();
248 int noOfDataNodes=os::floor((PAGE_SIZE - sizeof(PageInfo))/size);
249 Page* page=chunk->getFirstPage();
250 int i=0;
251 Chunk *chk;
252 while(page)
254 char *data = ((char*)page) + sizeof(PageInfo);
255 for (i = 0; i< noOfDataNodes; i++)
257 if (*((InUse*)data) == 1)
259 chk=(Chunk*)((InUse*)data+1);
260 chk->print();
262 data = data + size;
264 page = (PageInfo*)(((PageInfo*)page)->nextPage_) ;
268 printf(" </User Chunk >\n");
269 printf("</Chunk information>\n");
271 else {
272 printf("Wrong argument passed\n");
273 printUsage();
274 ret =1;
276 } else if (opt == 11){
277 dbMgr->printTreeIndexNodeInfo(name, true);
278 } else if (opt == 12){
279 dbMgr->printTreeIndexNodeInfo(name,false);
281 iter.reset();
282 while (iter.hasElement()) delete iter.nextElement();
283 tableList.reset();
284 conn.close();
285 return ret;