1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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. *
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. *
15 ***************************************************************************/
18 #include <DatabaseManagerImpl.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>
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");
42 SqlConnection
*sqlconn
;
45 int main(int argc
, char **argv
)
47 char username
[IDENTIFIER_LENGTH
];
49 char password
[IDENTIFIER_LENGTH
];
52 char name
[IDENTIFIER_LENGTH
];
53 while ((c
= getopt(argc
, argv
, "u:p:T:I:D:licdsS?")) != EOF
)
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
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
);
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();
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
;
108 printf("<TableNames>\n");
110 while (iter
.hasElement())
112 elem
= (Identifier
*) iter
.nextElement();
114 printf(" <TableName> %s </TableName>\n", elem
->name
);
116 if (count
==0) printf(" <No tables exist></No tables exist>\n");
117 printf("</TableNames>\n");
121 printf("<Table Information of all tables>\n");
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
);
130 printError(ErrSysInternal
, "Unable to open table %s", elem
->name
);
133 FieldInfo
*info
= new FieldInfo();
134 List fNameList
= table
->getFieldNameList();
135 ListIterator fNameIter
= fNameList
.getIterator();
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");
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");
168 while (iter
.hasElement())
170 elem
= (Identifier
*) iter
.nextElement();
172 rv
=TableConf::config
.isTableCached(elem
->name
);
174 printf(" <TableName> %s </TableName>\n", elem
->name
);
175 dbMgr
->dropTable(elem
->name
);
178 printf(" <TableName> %s </TableName>\n", elem
->name
);
179 dbMgr
->dropTable(elem
->name
);
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");
188 printf("Catalog not initialized. Found registered process\n");
193 printf("<Database Usage Statistics>\n");
194 Database
*db
= dbMgr
->sysDb();
195 db
->printStatistics();
196 dbMgr
->printUsageStatistics();
198 db
->printStatistics();
199 printf("</Database Usage Statistics>\n");
203 printf("<Table Info> \n");
205 TableImpl
*table
= (TableImpl
*) dbMgr
->openTable(name
);
208 dbMgr
->closeTable(table
);
210 printf(" <Table Not Found> %s </Table Not Found>\n", name
);
213 printf("</Table Info> \n");
217 printf("<Index Info> \n");
218 rv
= dbMgr
->printIndexInfo(name
);
221 printf(" <Index Not Found> %s </Index Not Found>\n", name
);
224 printf("<Index Info> \n");
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 dbMgr
->printDebugChunkInfo();
243 else if(strcmp(name
,"mutex") ==0)
245 dbMgr
->printDebugMutexInfo();
250 } else if (opt
== 11){
251 dbMgr
->printTreeIndexNodeInfo(name
, true);
252 } else if (opt
== 12){
253 dbMgr
->printTreeIndexNodeInfo(name
,false);
256 while (iter
.hasElement()) delete iter
.nextElement();
258 sqlconn
->disconnect();
259 delete stmt
; delete sqlconn
;