solaris changes
[csql.git] / src / tools / catalog.cxx
blob2019b6a871e4f6765fac71ac01bdd5e814d680e4
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 FieldInfo *info = new FieldInfo();
116 List fNameList = table->getFieldNameList();
117 ListIterator fNameIter = fNameList.getIterator();
118 count++;
119 char fieldName[IDENTIFIER_LENGTH];
120 while (fNameIter.hasElement()) {
121 elem = (Identifier*) fNameIter.nextElement();
122 Table::getFieldNameAlone(elem->name, fieldName);
123 table->getFieldInfo((const char*)elem->name, info);
124 printf(" <FieldInfo>\n");
125 printf(" <FieldName> %s </FieldName>\n", fieldName);
126 printf(" <Type> %d </Type>\n", info->type);
127 printf(" <Length> %d </Length>\n", info->length);
128 printf(" <Primary> %d </Primary>\n", info->isPrimary);
129 printf(" <Null> %d </Null>\n", info->isNull);
130 printf(" <Default> %d </Default>\n", info->isDefault);
131 printf(" <DefaultValue> %s </DefaultValue>\n", info->defaultValueBuf);
132 printf(" </FieldInfo>\n");
135 delete info;
136 dbMgr->closeTable(table);
137 printf(" </TableInfo> \n");
140 if (count == 0) printf(" <No tables exist></No tables exist>\n");
141 printf("</Table Information of all tables>\n");
142 printf("<Index Information of all Indexs>\n");
143 CatalogTableINDEXFIELD cIndexField(dbMgr->sysDb());
144 cIndexField.printAllIndex();
145 printf("</Index Information of all Indexs>\n");
147 }else if (opt == 3)
149 if (!dbMgr->isAnyOneRegistered()) {
150 printf("<DropTable>\n");
151 int count =0;
152 while (iter.hasElement())
154 elem = (Identifier*) iter.nextElement();
155 #ifndef MMDB
156 rv=TableConf::config.isTableCached(elem->name);
157 if(rv!=OK){
158 printf(" <TableName> %s </TableName>\n", elem->name);
159 dbMgr->dropTable(elem->name);
161 #else
162 printf(" <TableName> %s </TableName>\n", elem->name);
163 dbMgr->dropTable(elem->name);
164 #endif
165 count++;
167 //TODO::If durability is on, remove chkpt and redo log file
168 if (count ==0) printf(" <No tables exist></No tables exist>\n");
169 printf("</DropTable>\n");
171 } else {
172 printf("Catalog not initialized. Found registered process\n");
173 ret =1;
175 }else if (opt == 4)
177 printf("<Database Usage Statistics>\n");
178 Database *db = dbMgr->sysDb();
179 db->printStatistics();
180 dbMgr->printUsageStatistics();
181 db = dbMgr->db();
182 db->printStatistics();
183 printf("</Database Usage Statistics>\n");
185 }else if (opt == 5)
187 printf("<Table Info> \n");
189 TableImpl *table = (TableImpl*) dbMgr->openTable(name);
190 if (table != NULL) {
191 table->printInfo();
192 dbMgr->closeTable(table);
193 }else {
194 printf(" <Table Not Found> %s </Table Not Found>\n", name);
195 ret =1;
197 printf("</Table Info> \n");
199 }else if (opt == 6)
201 printf("<Index Info> \n");
202 rv = dbMgr->printIndexInfo(name);//TODO::handle no index case to return 1
203 if (rv != OK)
205 printf(" <Index Not Found> %s </Index Not Found>\n", name);
206 ret =1;
208 printf("<Index Info> \n");
209 }else if (opt == 7)
211 if (strcmp(name, "lock") == 0)
213 dbMgr->printDebugLockInfo();
215 else if (strcmp(name, "trans") == 0)
217 dbMgr->printDebugTransInfo();
219 else if (strcmp(name, "proc") == 0)
221 dbMgr->printDebugProcInfo();
223 else if(strcmp(name,"chunk") ==0)
225 Database *db = dbMgr->sysDb();
226 Chunk *chunk;
227 int id=1;
228 printf("<Chunk information>\n");
229 printf(" <System Chunk >\n");
230 chunk=db->getSystemDatabaseChunk(UserChunkTableId);
231 chunk->print();
232 while(id<MAX_CHUNKS)
234 chunk=db->getSystemDatabaseChunk(id);
235 if((chunk->getChunkID())!=0){
236 chunk->print();
238 id++;
240 printf(" </System Chunk >\n");
241 printf(" <User Chunk >\n");
242 chunk=db->getSystemDatabaseChunk(UserChunkTableId);
243 size_t size=chunk->getSize();
244 int noOfDataNodes=os::floor((PAGE_SIZE - sizeof(PageInfo))/size);
245 Page* page=chunk->getFirstPage();
246 int i=0;
247 Chunk *chk;
248 while(page)
250 char *data = ((char*)page) + sizeof(PageInfo);
251 for (i = 0; i< noOfDataNodes; i++)
253 if (*((InUse*)data) == 1)
255 chk=(Chunk*)((InUse*)data+1);
256 chk->print();
258 data = data + size;
260 page = (PageInfo*)(((PageInfo*)page)->nextPage_) ;
264 printf(" </User Chunk >\n");
265 printf("</Chunk information>\n");
267 else {
268 printf("Wrong argument passed\n");
269 printUsage();
270 ret =1;
272 } else if (opt == 11){
273 dbMgr->printTreeIndexNodeInfo(name, true);
274 } else if (opt == 12){
275 dbMgr->printTreeIndexNodeInfo(name,false);
277 iter.reset();
278 while (iter.hasElement()) delete iter.nextElement();
279 tableList.reset();
280 conn.close();
281 return ret;