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 ***************************************************************************/
17 #include <DatabaseManagerImpl.h>
19 #include <TableImpl.h>
22 printf("Usage: catalog [-u username] [-p passwd] [-l] [-i] [-d] [-T table] [-I index] [-D <lock|trans|proc>]\n");
23 printf(" l -> list all table with field information\n");
24 printf(" i -> reinitialize catalog tables. Drops all tables.\n");
25 printf(" d -> print db usage statistics\n");
26 printf(" T -> list table information\n");
27 printf(" I -> list index information\n");
28 printf(" D -> print debug information for system tables\n");
29 printf("Note: If multiple options are specified, last one will be considered.\n");
34 int main(int argc
, char **argv
)
36 char username
[IDENTIFIER_LENGTH
];
38 char password
[IDENTIFIER_LENGTH
];
41 char name
[IDENTIFIER_LENGTH
];
42 while ((c
= getopt(argc
, argv
, "u:p:T:I:D:lid?")) != EOF
)
46 case 'u' : { strcpy(username
, argv
[optind
- 1]); opt
=1; break; }
47 case 'p' : { strcpy(password
, argv
[optind
- 1]); opt
=1; break; }
48 case 'T' : { strcpy(name
, argv
[optind
- 1]); opt
= 5; break; }
49 case 'I' : { strcpy(name
, argv
[optind
- 1]); opt
= 6; break; }
50 case 'D' : { strcpy(name
, argv
[optind
- 1]); opt
= 7; break; }
51 case 'l' : { opt
= 2; break; } //list all the table with field info
52 case 'i' : { opt
= 3; break; }//reinitialize the catalog table
53 case 'd' : { opt
= 4; break; }//print db usage statistics
54 case '?' : { opt
= 10; break; } //print help
55 default: opt
=1; //list all the tables
64 //printf("%s %s \n", username, password);
65 if (username
[0] == '\0' )
67 strcpy(username
, "root");
68 strcpy(password
, "manager");
69 opt
=1;//if username is not specified, just list all table names
73 DbRetVal rv
= conn
.open(username
, password
);
74 if (rv
!= OK
) return 1;
75 DatabaseManagerImpl
*dbMgr
= (DatabaseManagerImpl
*) conn
.getDatabaseManager();
76 if (dbMgr
== NULL
) { printf("Auth failed\n"); return 2;}
77 List tableList
= dbMgr
->getAllTableNames();
78 ListIterator iter
= tableList
.getIterator();
79 Identifier
*elem
= NULL
;
82 printf("<TableNames>\n");
84 while (iter
.hasElement())
86 elem
= (Identifier
*) iter
.nextElement();
88 printf(" <TableName> %s </TableName>\n", elem
->name
);
90 if (count
==0) printf(" <No tables exist></No tables exist>\n");
91 printf("</TableNames>\n");
95 printf("<Table Information of all tables>\n");
97 while (iter
.hasElement())
99 elem
= (Identifier
*) iter
.nextElement();
100 printf(" <TableInfo> \n");
101 printf(" <TableName> %s </TableName>\n", elem
->name
);
102 Table
*table
= dbMgr
->openTable(elem
->name
);
103 FieldInfo
*info
= new FieldInfo();
104 List fNameList
= table
->getFieldNameList();
105 ListIterator fNameIter
= fNameList
.getIterator();
107 while (fNameIter
.hasElement()) {
108 elem
= (Identifier
*) fNameIter
.nextElement();
109 table
->getFieldInfo((const char*)elem
->name
, info
);
110 printf(" <FieldInfo>\n");
111 printf(" <FieldName> %s </FieldName>\n", elem
->name
);
112 printf(" <Type> %d </Type>\n", info
->type
);
113 printf(" <Length> %d </Length>\n", info
->length
);
114 printf(" <Primary> %d </Primary>\n", info
->isPrimary
);
115 printf(" <Null> %d </Null>\n", info
->isNull
);
116 printf(" <Default> %d </Default>\n", info
->isDefault
);
117 printf(" <DefaultValue> %s </DefaultValue>\n", info
->defaultValueBuf
);
118 printf(" </FieldInfo>\n");
122 dbMgr
->closeTable(table
);
123 printf(" </TableInfo> \n");
126 if (count
== 0) printf(" <No tables exist></No tables exist>\n");
127 printf("</Table Information of all tables>\n");
130 if (!dbMgr
->isAnyOneRegistered()) {
131 printf("<DropTable>\n");
133 while (iter
.hasElement())
135 elem
= (Identifier
*) iter
.nextElement();
136 printf(" <TableName> %s </TableName>\n", elem
->name
);
137 dbMgr
->dropTable(elem
->name
);
140 if (count
==0) printf(" <No tables exist></No tables exist>\n");
141 printf("</DropTable>\n");
144 printf("Catalog not initialized. Found registered process\n");
149 printf("<Database Usage Statistics>\n");
150 Database
*db
= dbMgr
->sysDb();
151 db
->printStatistics();
152 dbMgr
->printUsageStatistics();
154 db
->printStatistics();
155 printf("</Database Usage Statistics>\n");
159 printf("<Table Info> \n");
161 TableImpl
*table
= (TableImpl
*) dbMgr
->openTable(name
);
164 dbMgr
->closeTable(table
);
166 printf(" <Table Not Found> %s </Table Not Found>\n", name
);
169 printf("</Table Info> \n");
173 printf("<Index Info> \n");
174 rv
= dbMgr
->printIndexInfo(name
);//TODO::handle no index case to return 1
177 printf(" <Index Not Found> %s </Index Not Found>\n", name
);
180 printf("<Index Info> \n");
183 if (strcmp(name
, "lock") == 0)
185 dbMgr
->printDebugLockInfo();
187 else if (strcmp(name
, "trans") == 0)
189 dbMgr
->printDebugTransInfo();
191 else if (strcmp(name
, "proc") == 0)
193 dbMgr
->printDebugProcInfo();
196 printf("Wrong argument passed\n");