1852859 show tables doesnt display table names in csql tool
[csql.git] / src / tools / isql.cxx
blob7046f167f1aed979498b41521d2c2ee52ace0fd4
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<CSql.h>
17 #include<DatabaseManagerImpl.h>
18 #include <Statement.h>
19 #include <SqlFactory.h>
20 #include <SqlStatement.h>
21 #define SQL_STMT_LEN 1024
22 enum STMT_TYPE
24 SELECT =0,
25 DDL ,
26 OTHER
28 STMT_TYPE stmtType = SELECT;
29 FILE *fp;
30 AbsSqlConnection *conn;
31 AbsSqlStatement *stmt;
33 bool autocommitmode = true;
34 IsolationLevel isoLevel = READ_COMMITTED;
35 void printHelp();
36 bool getInput(bool);
37 void printUsage()
39 printf("Usage: csql [-u username] [-p passwd] [-s sqlfile] \n");
40 printf(" username -> username to connect to database\n");
41 printf(" password -> password to connect to database\n");
42 printf(" sqlfile -> filename containing sql statements\n");
43 return;
47 int main(int argc, char **argv)
49 char username[IDENTIFIER_LENGTH];
50 username [0] = '\0';
51 char password[IDENTIFIER_LENGTH];
52 password [0] = '\0';
53 char filename[512];
54 filename [0] ='\0';
55 int c = 0, opt=0;
56 while ((c = getopt(argc, argv, "u:p:s:?")) != EOF)
58 switch (c)
60 case 'u' : strcpy(username , argv[optind - 1]); break;
61 case 'p' : strcpy(password , argv[optind - 1]); break;
62 case 's' : strcpy(filename , argv[optind - 1]); break;
63 case '?' : { opt = 1; break; } //print help
64 default: printf("Wrong args\n"); exit(1);
67 }//while options
68 //printf("%s %s %s", username, password, filename);
69 if (opt == 1)
71 printUsage();
72 return 0;
74 if (username[0] == '\0' )
76 strcpy(username, "root");
77 strcpy(password, "manager");
79 bool fileFlag = false;
80 if (filename [0] !='\0')
82 fp = fopen(filename,"r");
83 if (fp == NULL)
85 printf("Unable to open the file %s\n", filename);
86 return 1;
88 fileFlag = true;
91 DbRetVal rv = OK;
92 conn = SqlFactory::createConnection(CSql);
93 //conn = SqlFactory::createConnection(CSqlGateway);
94 rv = conn->connect(username,password);
95 if (rv != OK) return 1;
96 stmt = SqlFactory::createStatement(CSql);
97 //stmt = SqlFactory::createStatement(CSqlGateway);
98 stmt->setConnection(conn);
99 rv = conn->beginTrans();
100 if (rv != OK) return 2;
101 while (getInput(fileFlag) == true) continue;
103 //TODO::conn should provide method telling status of the transaction.
104 //if running, then abort it
105 conn->rollback();
106 if (filename [0] !='\0')
108 fclose(fp);
110 conn->disconnect();
111 delete stmt;
112 delete conn;
113 return 0;
115 bool handleTransaction(char *st)
117 while (isspace (*st)|| *st == '(' ) st++; // Skip white spaces
118 if (strncasecmp (st, "COMMIT", 6) == 0 ||
119 strncasecmp (st, "commit", 6) == 0 )
121 conn->commit();
122 conn->beginTrans(isoLevel);
123 return true;
125 else if (strncasecmp (st, "ROLLBACK", 8) == 0||
126 strncasecmp (st, "rollback", 8) == 0)
128 conn->rollback();
129 conn->beginTrans(isoLevel);
130 return true;
132 else if (strncasecmp (st, "SET AUTOCOMMIT ON", 17) == 0)
134 autocommitmode = true;
135 printf("AUTOCOMMIT Mode is set to ON\n");
136 return true;
138 else if (strncasecmp (st, "SET AUTOCOMMIT OFF", 18) == 0)
140 autocommitmode = false;
141 printf("AUTOCOMMIT Mode is set to OFF\n");
142 return true;
144 else if (strncasecmp (st, "SET ISOLATION LEVEL UNCOMMITTED", 31) == 0)
146 isoLevel = READ_UNCOMMITTED;
147 printf("Isolation Level is set to READ_UNCOMMITTED\n");
148 return true;
150 else if (strncasecmp (st, "SET ISOLATION LEVEL COMMITTED", 29) == 0)
152 isoLevel = READ_COMMITTED;
153 printf("Isolation Level is set to READ_COMMITTED\n");
154 return true;
156 else if (strncasecmp (st, "SET ISOLATION LEVEL REPEATABLE", 30) == 0)
158 isoLevel = READ_REPEATABLE;
159 printf("Isolation Level is set to READ_REPEATABLE\n");
160 return true;
162 return false;
164 bool handleEchoAndComment(char *st)
166 while (isspace (*st)|| *st == '(' ) st++; // Skip white spaces
167 if (strncasecmp (st, "ECHO", 4) == 0)
169 printf("%s\n", st);
170 return true;
171 }else if (strncmp(st, "--", 2) == 0)
173 return true;
174 }else if (strncasecmp(st, "help", 2) == 0)
176 printHelp();
177 return true;
178 }else if (strncasecmp(st, "show tables", 11) == 0)
180 Connection conn;
181 conn.open("root","manager");
182 DatabaseManagerImpl *dbMgr = (DatabaseManagerImpl*)conn.getDatabaseManager();
183 List tableList = dbMgr->getAllTableNames();
184 ListIterator iter = tableList.getIterator();
185 Identifier *elem = NULL;
186 int ret =0;
187 printf("=============TableNames===================\n");
188 int count =0;
189 while (iter.hasElement())
191 elem = (Identifier*) iter.nextElement();
192 count++;
193 printf(" %s \n", elem->name);
195 if (count ==0) printf(" No tables exist\n");
196 printf("=========================================\n");
197 conn.close();
198 return true;
200 return false;
202 void printHelp()
204 printf("CSQL Command List\n");
205 printf("======================================================\n");
206 printf("SHOW TABLES\n");
207 printf("SET AUTOCOMMIT ON|OFF\n");
208 printf("SET ISOLATION LEVEL UNCOMMITTED|COMMITTED|REPEATABLE\n");
209 printf("CREATE TABLE|INDEX ...\n");
210 printf("INSERT ...\n");
211 printf("UPDATE ...\n");
212 printf("DELETE ...\n");
213 printf("SELECT ...\n");
214 printf("======================================================\n");
216 void setStmtType(char *st)
218 if (strncasecmp (st, "SELECT", 6) == 0) {stmtType=SELECT; return; }
219 else if (strncasecmp (st, "CREATE", 6) == 0) {stmtType=DDL; return; }
220 else if (strncasecmp (st, "DROP", 4) == 0) { stmtType=DDL; return; }
221 stmtType = OTHER;
222 return ;
225 char getQueryFromStdIn(char *buf)
227 char c, *bufBegin=buf;
228 int ln, charCnt=0;
230 ln=1;
231 printf("CSQL>");
232 while( (c=(char ) getchar()) != EOF && c != ';')
234 *buf++ = c; charCnt++;
235 if(c=='\n') //printf("%1d>",ln++);
236 ln++;
238 if( charCnt == SQL_STMT_LEN ) {
239 printf("SQL Statement length is greater than %d. "
240 "Ignoring the statement.\n", SQL_STMT_LEN );
241 *bufBegin++ =';';
242 *bufBegin ='\0';
243 return 0;
246 *buf++ = ';';
247 *buf = '\0';
248 return c;
250 char getQueryFromFile(char *buf)
252 char c, *bufBegin=buf;
253 int charCnt=0;
254 while( (c=(char ) fgetc(fp)) != EOF && c != ';')
256 *buf++ = c; charCnt++;
257 if( charCnt == SQL_STMT_LEN ) {
258 printf("SQL Statement length is greater than %d. "
259 "Ignoring the statement.\n", SQL_STMT_LEN );
260 *bufBegin++ =';';
261 *bufBegin ='\0';
262 return 0;
265 *buf++ = ';';
266 *buf = '\0';
267 return c;
270 bool getInput(bool fromFile)
272 char buffer [SQL_STMT_LEN + 1];
274 char eof;
275 if (fromFile == false)
276 eof = getQueryFromStdIn(buffer);
277 else
278 eof = getQueryFromFile(buffer);
280 char *buf = buffer;
281 while(*buf == ' ' || *buf == '\t' || *buf == '\n') buf++;
282 if (eof == EOF || strncasecmp (buf, "quit", 4) == 0)
283 return false;
284 if (handleTransaction(buf)) return true;
285 if (handleEchoAndComment(buf)) return true;
286 if ( *buf == ';' ) return true; // Null statement.
288 setStmtType(buf);
290 DbRetVal rv = stmt->prepare(buf);
291 if (rv != OK)
293 printf("Statement prepare failed with error %d\n", rv);
294 return true;
296 int rows =0;
297 rv = stmt->execute(rows);
298 if (rv != OK)
300 printf("Statement execute failed with error %d\n", rv);
301 stmt->free();
302 return true;
304 if (stmtType == OTHER)
306 printf("Statement Executed: Rows Affected = %d\n", rows);
308 else if (stmtType == DDL)
310 printf("Statement Executed\n");
312 else
314 FieldInfo *info = new FieldInfo();
315 printf("---------------------------------------------------------\n");
316 printf("\t");
317 for (int i = 0 ; i < stmt->noOfProjFields() ; i++)
319 stmt->getProjFldInfo(i, info);
320 printf("%s\t", info->fldName);
322 printf("\n---------------------------------------------------------\n");
323 delete info;
324 void *tuple = NULL;
325 while(true)
327 printf("\t");
328 tuple = (char*)stmt->fetchAndPrint();
329 printf("\n");
330 if (tuple == NULL) { break; }
332 stmt->close();
334 stmt->free();
335 if (autocommitmode)
337 conn->commit();
338 conn->beginTrans(isoLevel);
339 return true;
342 return true;