*** empty log message ***
[csql.git] / src / tools / catalog.cxx
blobb2e646a6a23a4bb229b17d64fd16f85ac0753943
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:vlicdsS?")) != 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 case 'v' : {
73 if (opt == 5) opt = 15;
74 else if (opt == 6) opt=16;
75 break;
77 default: opt=1; //list all the tables
80 }//while options
81 if (opt == 10) {
82 printUsage();
83 return 0;
86 //printf("%s %s \n", username, password);
87 if (username[0] == '\0' )
89 strcpy(username, I_USER);
90 strcpy(password, I_PASS);
91 opt=1;//if username is not specified, just list all table names
93 sqlconn = (SqlConnection*) SqlFactory::createConnection(CSqlDirect);
94 DbRetVal rv = sqlconn->connect(username, password);
95 if (rv != OK) {
96 printf("Unable to get connection to csql\n");
97 delete sqlconn; delete stmt; return 1;
99 stmt = (SqlStatement*) SqlFactory::createStatement(CSqlDirect);
100 stmt->setSqlConnection(sqlconn);
101 os::signal(SIGCSQL1, SIG_IGN);
102 DatabaseManagerImpl *dbMgr = (DatabaseManagerImpl*)
103 sqlconn->getConnObject().getDatabaseManager();
104 if (dbMgr == NULL) {
105 printf("Unable to retrive db manager\n");
106 sqlconn->disconnect(); delete stmt; delete sqlconn; return 2;
108 List tableList = dbMgr->getAllTableNames();
109 ListIterator iter = tableList.getIterator();
110 Identifier *elem = NULL;
111 int ret =0;
112 if (opt == 1) {
113 printf("<TableNames>\n");
114 int count =0;
115 while (iter.hasElement())
117 elem = (Identifier*) iter.nextElement();
118 count++;
119 printf(" <TableName> %s </TableName>\n", elem->name);
121 if (count ==0) printf(" <No tables exist></No tables exist>\n");
122 printf("</TableNames>\n");
124 else if (opt ==2)
126 printf("<Table Information of all tables>\n");
127 int count =0;
128 while (iter.hasElement())
130 elem = (Identifier*) iter.nextElement();
131 printf(" <TableInfo> \n");
132 printf(" <TableName> %s </TableName>\n", elem->name);
133 Table *table = dbMgr->openTable(elem->name);
134 if (NULL == table) {
135 printError(ErrSysInternal, "Unable to open table %s", elem->name);
136 break;
138 FieldInfo *info = new FieldInfo();
139 List fNameList = table->getFieldNameList();
140 ListIterator fNameIter = fNameList.getIterator();
141 count++;
142 char fieldName[IDENTIFIER_LENGTH];
143 while (fNameIter.hasElement()) {
144 elem = (Identifier*) fNameIter.nextElement();
145 Table::getFieldNameAlone(elem->name, fieldName);
146 table->getFieldInfo((const char*)elem->name, info);
147 printf(" <FieldInfo>\n");
148 printf(" <FieldName> %s </FieldName>\n", fieldName);
149 printf(" <Type> %d </Type>\n", info->type);
150 printf(" <Length> %d </Length>\n", info->length);
151 printf(" <Primary> %d </Primary>\n", info->isPrimary);
152 printf(" <Null> %d </Null>\n", info->isNull);
153 printf(" <Default> %d </Default>\n", info->isDefault);
154 printf(" <DefaultValue> %s </DefaultValue>\n", info->defaultValueBuf);
155 printf(" </FieldInfo>\n");
158 delete info;
159 dbMgr->closeTable(table);
160 printf(" </TableInfo> \n");
163 if (count == 0) printf(" <No tables exist></No tables exist>\n");
164 printf("</Table Information of all tables>\n");
165 printf("<Index Information of all Indexs>\n");
166 CatalogTableINDEXFIELD cIndexField(dbMgr->sysDb());
167 cIndexField.printAllIndex();
168 printf("</Index Information of all Indexs>\n");
169 } else if (opt == 3) {
170 if (!dbMgr->isAnyOneRegistered()) {
171 printf("<DropTable>\n");
172 int count =0;
173 while (iter.hasElement())
175 elem = (Identifier*) iter.nextElement();
176 #ifndef MMDB
177 rv=TableConf::config.isTableCached(elem->name);
178 if(rv!=OK){
179 printf(" <TableName> %s </TableName>\n", elem->name);
180 dbMgr->dropTable(elem->name);
182 #else
183 printf(" <TableName> %s </TableName>\n", elem->name);
184 dbMgr->dropTable(elem->name);
185 #endif
186 count++;
188 //TODO::If durability is on, remove chkpt and redo log file
189 if (count ==0) printf(" <No tables exist></No tables exist>\n");
190 printf("</DropTable>\n");
192 } else {
193 printf("Catalog not initialized. Found registered process\n");
194 ret =1;
196 }else if (opt == 4)
198 printf("<Database Usage Statistics>\n");
199 Database *db = dbMgr->sysDb();
200 db->printStatistics();
201 dbMgr->printUsageStatistics();
202 db = dbMgr->db();
203 db->printStatistics();
204 printf("</Database Usage Statistics>\n");
206 }else if (opt == 5)
208 printf("<Table Info> \n");
210 TableImpl *table = (TableImpl*) dbMgr->openTable(name);
211 if (table != NULL) {
212 table->printInfo();
213 dbMgr->closeTable(table);
214 }else {
215 printf(" <Table Not Found> %s </Table Not Found>\n", name);
216 ret =1;
218 printf("</Table Info> \n");
220 }else if (opt == 6)
222 printf("<Index Info> \n");
223 rv = dbMgr->printIndexInfo(name);
224 if (rv != OK)
226 printf(" <Index Not Found> %s </Index Not Found>\n", name);
227 ret =1;
229 printf("<Index Info> \n");
230 }else if (opt == 7)
232 if (strcmp(name, "lock") == 0)
234 dbMgr->printDebugLockInfo();
236 else if (strcmp(name, "trans") == 0)
238 dbMgr->printDebugTransInfo();
240 else if (strcmp(name, "proc") == 0)
242 dbMgr->printDebugProcInfo();
244 else if(strcmp(name,"chunk") ==0)
246 dbMgr->printDebugChunkInfo();
248 else if(strcmp(name,"mutex") ==0)
250 dbMgr->printDebugMutexInfo();
251 }else {
252 printUsage();
253 ret =1;
255 } else if (opt == 11){
256 dbMgr->printTreeIndexNodeInfo(name, true);
257 } else if (opt == 12){
258 dbMgr->printTreeIndexNodeInfo(name,false);
259 } else if (opt ==15) {
260 } else if (opt ==16) {
261 rv = dbMgr->printIndexDebugInfo(name);
262 if (rv != OK)
264 printf(" <Index Not Found> %s </Index Not Found>\n", name);
265 ret =1;
268 iter.reset();
269 while (iter.hasElement()) delete iter.nextElement();
270 tableList.reset();
271 sqlconn->disconnect();
272 delete stmt; delete sqlconn;
273 return ret;