reverting back the changes. causes core dump
[csql.git] / src / tools / catalog.cxx
blob0390889ead029befe01c0b9408f4e2af2c568f49
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>
23 #include <SqlFactory.h>
24 #include <SqlConnection.h>
25 #include <SqlStatement.h>
27 void printUsage()
29 printf("Usage: catalog [-u username] [-p passwd] [-l] [-i] [-d] [-T table] [-I index] [-D <lock|trans|proc|chunk>]\n");
30 printf(" l -> list all table with field information\n");
31 printf(" list all Index information\n");
32 printf(" i -> reinitialize catalog tables. Drops all tables.\n");
33 printf(" d -> print db usage statistics\n");
34 printf(" T -> list table information\n");
35 printf(" I -> list index information\n");
36 printf(" D -> print debug information for system tables\n");
37 printf("Note: If multiple options are specified, last one will be considered.\n");
38 return;
42 SqlConnection *sqlconn;
43 SqlStatement *stmt;
45 int main(int argc, char **argv)
47 char username[IDENTIFIER_LENGTH];
48 username [0] = '\0';
49 char password[IDENTIFIER_LENGTH];
50 password [0] = '\0';
51 int c = 0, opt = 0;
52 char name[IDENTIFIER_LENGTH];
53 while ((c = getopt(argc, argv, "u:p:T:I:D:licdsS?")) != EOF)
55 switch (c)
57 case 'u' : { strcpy(username, argv[optind - 1]); opt=1; break; }
58 case 'p' : { strcpy(password, argv[optind - 1]); opt=1; break; }
59 case 'T' : { strcpy(name, argv[optind - 1]); opt = 5; break; }
60 case 'I' : { strcpy(name, argv[optind - 1]); opt = 6; break; }
61 case 'D' : { strcpy(name, argv[optind - 1]); opt = 7; break; }
62 case 'l' : { opt = 2; break; } //list all the table with field info
63 case 'i' : { opt = 3; break; }//reinitialize the catalog table
64 case 'd' : { opt = 4; break; }//print db usage statistics
65 case 's' : { if(opt == 6) opt=11;
66 else printf("Use -I IndexName -s\n");
67 break; }//print db usage statistics
68 case 'S' : { if(opt == 6) opt=12;
69 else printf("Use -I IndexName -S\n");
70 break; }//print db usage statistics
71 case '?' : { opt = 10; break; } //print help
72 default: opt=1; //list all the tables
75 }//while options
76 if (opt == 10) {
77 printUsage();
78 return 0;
81 //printf("%s %s \n", username, password);
82 if (username[0] == '\0' )
84 strcpy(username, I_USER);
85 strcpy(password, I_PASS);
86 opt=1;//if username is not specified, just list all table names
88 sqlconn = (SqlConnection*) SqlFactory::createConnection(CSqlDirect);
89 DbRetVal rv = sqlconn->connect(username, password);
90 if (rv != OK) {
91 printf("Unable to get connection to csql\n");
92 delete sqlconn; delete stmt; return 1;
94 stmt = (SqlStatement*) SqlFactory::createStatement(CSqlDirect);
95 stmt->setSqlConnection(sqlconn);
96 os::signal(SIGCSQL1, SIG_IGN);
97 DatabaseManagerImpl *dbMgr = (DatabaseManagerImpl*)
98 sqlconn->getConnObject().getDatabaseManager();
99 if (dbMgr == NULL) {
100 printf("Unable to retrive db manager\n");
101 sqlconn->disconnect(); delete stmt; delete sqlconn; return 2;
103 List tableList = dbMgr->getAllTableNames();
104 ListIterator iter = tableList.getIterator();
105 Identifier *elem = NULL;
106 int ret =0;
107 if (opt == 1) {
108 printf("<TableNames>\n");
109 int count =0;
110 while (iter.hasElement())
112 elem = (Identifier*) iter.nextElement();
113 count++;
114 printf(" <TableName> %s </TableName>\n", elem->name);
116 if (count ==0) printf(" <No tables exist></No tables exist>\n");
117 printf("</TableNames>\n");
119 else if (opt ==2)
121 printf("<Table Information of all tables>\n");
122 int count =0;
123 while (iter.hasElement())
125 elem = (Identifier*) iter.nextElement();
126 printf(" <TableInfo> \n");
127 printf(" <TableName> %s </TableName>\n", elem->name);
128 Table *table = dbMgr->openTable(elem->name);
129 if (NULL == table) {
130 printError(ErrSysInternal, "Unable to open table %s", elem->name);
131 break;
133 FieldInfo *info = new FieldInfo();
134 List fNameList = table->getFieldNameList();
135 ListIterator fNameIter = fNameList.getIterator();
136 count++;
137 char fieldName[IDENTIFIER_LENGTH];
138 while (fNameIter.hasElement()) {
139 elem = (Identifier*) fNameIter.nextElement();
140 Table::getFieldNameAlone(elem->name, fieldName);
141 table->getFieldInfo((const char*)elem->name, info);
142 printf(" <FieldInfo>\n");
143 printf(" <FieldName> %s </FieldName>\n", fieldName);
144 printf(" <Type> %d </Type>\n", info->type);
145 printf(" <Length> %d </Length>\n", info->length);
146 printf(" <Primary> %d </Primary>\n", info->isPrimary);
147 printf(" <Null> %d </Null>\n", info->isNull);
148 printf(" <Default> %d </Default>\n", info->isDefault);
149 printf(" <DefaultValue> %s </DefaultValue>\n", info->defaultValueBuf);
150 printf(" </FieldInfo>\n");
153 delete info;
154 dbMgr->closeTable(table);
155 printf(" </TableInfo> \n");
158 if (count == 0) printf(" <No tables exist></No tables exist>\n");
159 printf("</Table Information of all tables>\n");
160 printf("<Index Information of all Indexs>\n");
161 CatalogTableINDEXFIELD cIndexField(dbMgr->sysDb());
162 cIndexField.printAllIndex();
163 printf("</Index Information of all Indexs>\n");
164 } else if (opt == 3) {
165 if (!dbMgr->isAnyOneRegistered()) {
166 printf("<DropTable>\n");
167 int count =0;
168 while (iter.hasElement())
170 elem = (Identifier*) iter.nextElement();
171 #ifndef MMDB
172 rv=TableConf::config.isTableCached(elem->name);
173 if(rv!=OK){
174 printf(" <TableName> %s </TableName>\n", elem->name);
175 dbMgr->dropTable(elem->name);
177 #else
178 printf(" <TableName> %s </TableName>\n", elem->name);
179 dbMgr->dropTable(elem->name);
180 #endif
181 count++;
183 //TODO::If durability is on, remove chkpt and redo log file
184 if (count ==0) printf(" <No tables exist></No tables exist>\n");
185 printf("</DropTable>\n");
187 } else {
188 printf("Catalog not initialized. Found registered process\n");
189 ret =1;
191 }else if (opt == 4)
193 printf("<Database Usage Statistics>\n");
194 Database *db = dbMgr->sysDb();
195 db->printStatistics();
196 dbMgr->printUsageStatistics();
197 db = dbMgr->db();
198 db->printStatistics();
199 printf("</Database Usage Statistics>\n");
201 }else if (opt == 5)
203 printf("<Table Info> \n");
205 TableImpl *table = (TableImpl*) dbMgr->openTable(name);
206 if (table != NULL) {
207 table->printInfo();
208 dbMgr->closeTable(table);
209 }else {
210 printf(" <Table Not Found> %s </Table Not Found>\n", name);
211 ret =1;
213 printf("</Table Info> \n");
215 }else if (opt == 6)
217 printf("<Index Info> \n");
218 rv = dbMgr->printIndexInfo(name);
219 if (rv != OK)
221 printf(" <Index Not Found> %s </Index Not Found>\n", name);
222 ret =1;
224 printf("<Index Info> \n");
225 }else if (opt == 7)
227 if (strcmp(name, "lock") == 0)
229 dbMgr->printDebugLockInfo();
231 else if (strcmp(name, "trans") == 0)
233 dbMgr->printDebugTransInfo();
235 else if (strcmp(name, "proc") == 0)
237 dbMgr->printDebugProcInfo();
239 else if(strcmp(name,"chunk") ==0)
241 Database *db = dbMgr->sysDb();
242 Chunk *chunk;
243 int id=1;
244 printf("<Chunk information>\n");
245 printf(" <System Chunk >\n");
246 chunk=db->getSystemDatabaseChunk(UserChunkTableId);
247 chunk->print();
248 while(id<MAX_CHUNKS)
250 chunk=db->getSystemDatabaseChunk(id);
251 if((chunk->getChunkID())!=0){
252 chunk->print();
254 id++;
256 printf(" </System Chunk >\n");
257 printf(" <User Chunk >\n");
258 chunk=db->getSystemDatabaseChunk(UserChunkTableId);
259 size_t size=chunk->getSize();
260 int noOfDataNodes=os::floor((PAGE_SIZE - sizeof(PageInfo))/size);
261 Page* page=chunk->getFirstPage();
262 int i=0;
263 Chunk *chk;
264 while(page)
266 char *data = ((char*)page) + sizeof(PageInfo);
267 for (i = 0; i< noOfDataNodes; i++)
269 if (*((InUse*)data) == 1)
271 chk=(Chunk*)((InUse*)data+1);
272 chk->print();
274 data = data + size;
276 page = (PageInfo*)(((PageInfo*)page)->nextPage_) ;
280 printf(" </User Chunk >\n");
281 printf("</Chunk information>\n");
283 else {
284 printf("Wrong argument passed\n");
285 printUsage();
286 ret =1;
288 } else if (opt == 11){
289 dbMgr->printTreeIndexNodeInfo(name, true);
290 } else if (opt == 12){
291 dbMgr->printTreeIndexNodeInfo(name,false);
293 iter.reset();
294 while (iter.hasElement()) delete iter.nextElement();
295 tableList.reset();
296 sqlconn->disconnect();
297 delete stmt; delete sqlconn;
298 return ret;