show table implemented partially
[csql.git] / src / tools / isql.cxx
blob6b0c96eb02b61727b44b7e7779398c6fe8d7bcdc
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 #include <SqlNwConnection.h>
22 #include <SqlNwStatement.h>
23 #define SQL_STMT_LEN 1024
24 enum STMT_TYPE
26 SELECT =0,
27 DDL ,
28 OTHER
30 STMT_TYPE stmtType = SELECT;
31 FILE *fp;
32 AbsSqlConnection *conn;
33 AbsSqlStatement *stmt;
34 SqlApiImplType type = CSqlUnknown;
35 bool gateway=false, silent=false;
36 bool autocommitmode = true;
37 bool network = false;
38 IsolationLevel isoLevel = READ_COMMITTED;
39 void printHelp();
40 bool getInput(bool);
41 void printUsage()
43 printf("Usage: csql [ [-u username] [-p passwd] ] [ [-H hostname] [-P port] ]\n");
44 printf(" [-s sqlfile] \n");
45 printf(" username -> username to connect to database\n");
46 printf(" password -> password to connect to database\n");
47 printf(" hostname -> hostname to connect to database through network\n");
48 printf(" port -> port no\n");
49 printf(" sqlfile -> filename containing sql statements\n");
50 return;
54 int main(int argc, char **argv)
56 char username[IDENTIFIER_LENGTH];
57 username [0] = '\0';
58 char password[IDENTIFIER_LENGTH];
59 password [0] = '\0';
60 char hostname[IDENTIFIER_LENGTH];
61 hostname[0] = '\0';
62 char port[8];
63 port[0] ='\0';
64 char filename[512];
65 filename [0] ='\0';
66 int c = 0, opt=0;
67 while ((c = getopt(argc, argv, "u:p:s:H:P:gS?")) != EOF)
69 switch (c)
71 case 'u' : strcpy(username , argv[optind - 1]); break;
72 case 'p' : strcpy(password , argv[optind - 1]); break;
73 case 's' : strcpy(filename , argv[optind - 1]); break;
74 case '?' : { opt = 1; break; } //print help
75 case 'S' : { silent = true; break; } //silent
76 case 'g' : { gateway = true; break; } //print help
77 case 'H' : { strcpy (hostname, argv[optind - 1]);
78 network = true; break; }
79 case 'P' : { strcpy (port, argv[optind - 1]);
80 network = true; break; }
81 default: printf("Wrong args\n"); exit(1);
84 }//while options
85 //printf("%s %s %s", username, password, filename);
86 if (opt == 1)
88 printUsage();
89 return 0;
91 if (username[0] == '\0' )
93 strcpy(username, "root");
94 strcpy(password, "manager");
96 if (network) {
97 if (hostname[0] == '\0') { printUsage(); return 0; }
98 if (port[0] == '\0') { printUsage(); return 0; }
100 bool fileFlag = false;
101 if (filename [0] !='\0')
103 fp = fopen(filename,"r");
104 if (fp == NULL)
106 printf("Unable to open the file %s\n", filename);
107 return 1;
109 fileFlag = true;
112 DbRetVal rv = OK;
113 if (network) {
114 if (gateway) type = CSqlNetworkGateway;
115 else type = CSqlNetwork;
116 conn = SqlFactory::createConnection(type);
117 SqlNwConnection *con = (SqlNwConnection *)conn;
118 con->setHost(hostname, atoi(port));
120 else {
121 if (gateway) type = CSqlGateway;
122 else type = CSql;
123 conn = SqlFactory::createConnection(type);
125 rv = conn->connect(username,password);
126 if (rv != OK) return 1;
127 stmt = SqlFactory::createStatement(type);
128 stmt->setConnection(conn);
129 //rv = conn->beginTrans(READ_COMMITTED, TSYNC);
130 rv = conn->beginTrans();
131 if (rv != OK) return 2;
132 while (getInput(fileFlag) == true) continue;
134 //TODO::conn should provide method telling status of the transaction.
135 //if running, then abort it
136 conn->rollback();
137 if (filename [0] !='\0')
139 fclose(fp);
141 conn->disconnect();
142 delete stmt;
143 delete conn;
144 return 0;
146 bool handleTransaction(char *st)
148 while (isspace (*st)|| *st == '(' ) st++; // Skip white spaces
149 if (strcasecmp (st, "COMMIT;") == 0 ||
150 strcasecmp (st, "commit;") == 0 )
152 conn->commit();
153 //conn->beginTrans(isoLevel, TSYNC);
154 conn->beginTrans(isoLevel);
155 return true;
157 else if (strcasecmp (st, "ROLLBACK;") == 0||
158 strcasecmp (st, "rollback;") == 0)
160 conn->rollback();
161 //conn->beginTrans(isoLevel, TSYNC);
162 conn->beginTrans(isoLevel);
163 return true;
165 else if (strcasecmp (st, "SET AUTOCOMMIT ON;") == 0)
167 autocommitmode = true;
168 if (!silent) printf("AUTOCOMMIT Mode is set to ON\n");
169 return true;
171 else if (strcasecmp (st, "SET AUTOCOMMIT OFF;") == 0)
173 autocommitmode = false;
174 if (!silent) printf("AUTOCOMMIT Mode is set to OFF\n");
175 return true;
177 else if (strcasecmp (st, "SET ISOLATION LEVEL UNCOMMITTED;") == 0)
179 isoLevel = READ_UNCOMMITTED;
180 printf("Isolation Level is set to READ_UNCOMMITTED\n");
181 return true;
183 else if (strcasecmp (st, "SET ISOLATION LEVEL COMMITTED;") == 0)
185 isoLevel = READ_COMMITTED;
186 printf("Isolation Level is set to READ_COMMITTED\n");
187 return true;
189 else if (strcasecmp (st, "SET ISOLATION LEVEL REPEATABLE;") == 0)
191 isoLevel = READ_REPEATABLE;
192 printf("Isolation Level is set to READ_REPEATABLE\n");
193 return true;
195 return false;
197 bool handleEchoAndComment(char *st)
199 while (isspace (*st)|| *st == '(' ) st++; // Skip white spaces
200 if (strncasecmp (st, "ECHO", 4) == 0)
202 printf("%s\n", st);
203 return true;
204 }else if (strncmp(st, "--", 2) == 0)
206 return true;
207 }else if (strncasecmp(st, "help", 2) == 0)
209 printHelp();
210 return true;
211 }else if (strcasecmp(st, "show tables;") == 0) {
212 List tableList = stmt->getAllTableNames();
213 ListIterator iter = tableList.getIterator();
214 Identifier *elem = NULL;
215 int ret =0;
216 printf("=============TableNames===================\n");
217 int count =0;
218 while (iter.hasElement())
220 elem = (Identifier*) iter.nextElement();
221 count++;
222 printf(" %s \n", elem->name);
224 if (count ==0) printf(" No tables exist\n");
225 printf("==========================================\n");
227 return true;
229 return false;
231 void printHelp()
233 printf("CSQL Command List\n");
234 printf("======================================================\n");
235 printf("SHOW TABLES\n");
236 printf("SET AUTOCOMMIT ON|OFF\n");
237 printf("SET ISOLATION LEVEL UNCOMMITTED|COMMITTED|REPEATABLE\n");
238 printf("CREATE TABLE|INDEX ...\n");
239 printf("INSERT ...\n");
240 printf("UPDATE ...\n");
241 printf("DELETE ...\n");
242 printf("SELECT ...\n");
243 printf("======================================================\n");
245 void setStmtType(char *st)
247 if (strncasecmp (st, "SELECT", 6) == 0) {stmtType=SELECT; return; }
248 else if (strncasecmp (st, "CREATE", 6) == 0) {stmtType=DDL; return; }
249 else if (strncasecmp (st, "DROP", 4) == 0) { stmtType=DDL; return; }
250 stmtType = OTHER;
251 return ;
254 char getQueryFromStdIn(char *buf)
256 char c, *bufBegin=buf;
257 int ln, charCnt=0;
259 ln=1;
260 printf("CSQL>");
261 while( (c=(char ) getchar()) != EOF && c != ';')
263 *buf++ = c; charCnt++;
264 if(c=='\n') //printf("%1d>",ln++);
265 ln++;
266 if( charCnt == SQL_STMT_LEN ) {
267 printf("SQL Statement length is greater than %d. "
268 "Ignoring the statement.\n", SQL_STMT_LEN );
269 *bufBegin++ =';';
270 *bufBegin ='\0';
271 return 0;
274 *buf++ = ';';
275 *buf = '\0';
276 return c;
278 char getQueryFromFile(char *buf)
280 char c, *bufBegin=buf;
281 int charCnt=0;
282 while( (c=(char ) fgetc(fp)) != EOF && c != ';')
284 *buf++ = c; charCnt++;
285 if( charCnt == SQL_STMT_LEN ) {
286 printf("SQL Statement length is greater than %d. "
287 "Ignoring the statement.\n", SQL_STMT_LEN );
288 *bufBegin++ =';';
289 *bufBegin ='\0';
290 return 0;
293 *buf++ = ';';
294 *buf = '\0';
295 return c;
298 bool getInput(bool fromFile)
300 char buffer [SQL_STMT_LEN + 1];
302 char eof;
303 if (fromFile == false)
304 eof = getQueryFromStdIn(buffer);
305 else
306 eof = getQueryFromFile(buffer);
308 char *buf = buffer;
309 while(*buf == ' ' || *buf == '\t' || *buf == '\n') buf++;
310 if (eof == EOF || strncasecmp (buf, "quit", 4) == 0)
311 return false;
312 if (handleTransaction(buf)) return true;
313 if (handleEchoAndComment(buf)) return true;
314 if ( *buf == ';' ) return true; // Null statement.
316 setStmtType(buf);
318 DbRetVal rv = stmt->prepare(buf);
319 if (rv != OK)
321 printf("Statement prepare failed with error %d\n", rv);
322 return true;
324 int rows =0;
325 rv = stmt->execute(rows);
326 if (rv != OK)
328 printf("Statement execute failed with error %d\n", rv);
329 stmt->free();
330 return true;
332 if (stmtType == OTHER)
334 if (!silent) printf("Statement Executed: Rows Affected = %d\n", rows);
336 else if (stmtType == DDL)
338 if (!silent) printf("Statement Executed\n");
340 else
342 FieldInfo *info = new FieldInfo();
343 printf("---------------------------------------------------------\n");
344 printf("\t");
345 for (int i = 0 ; i < stmt->noOfProjFields() ; i++)
347 stmt->getProjFldInfo(i+1, info);
348 printf("%s\t", info->fldName);
350 printf("\n---------------------------------------------------------\n");
351 delete info;
352 void *tuple = NULL;
353 while(true)
355 printf("\t");
356 tuple = (char*)stmt->fetchAndPrint(false);
357 printf("\n");
358 if (tuple == NULL) { break; }
360 stmt->close();
362 stmt->free();
363 if (autocommitmode)
365 conn->commit();
366 //conn->beginTrans(isoLevel, TSYNC);
367 conn->beginTrans(isoLevel);
368 return true;
370 return true;