Bug in putting the prepare packet in the list
[csql.git] / src / tools / isql.cxx
blob0fc8396859a965c604eb61a56be2e56342aa0657
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(READ_COMMITTED, TSYNC);
100 rv = conn->beginTrans();
101 if (rv != OK) return 2;
102 while (getInput(fileFlag) == true) continue;
104 //TODO::conn should provide method telling status of the transaction.
105 //if running, then abort it
106 conn->rollback();
107 if (filename [0] !='\0')
109 fclose(fp);
111 conn->disconnect();
112 delete stmt;
113 delete conn;
114 return 0;
116 bool handleTransaction(char *st)
118 while (isspace (*st)|| *st == '(' ) st++; // Skip white spaces
119 if (strncasecmp (st, "COMMIT", 6) == 0 ||
120 strncasecmp (st, "commit", 6) == 0 )
122 conn->commit();
123 //conn->beginTrans(isoLevel, TSYNC);
124 conn->beginTrans(isoLevel);
125 return true;
127 else if (strncasecmp (st, "ROLLBACK", 8) == 0||
128 strncasecmp (st, "rollback", 8) == 0)
130 conn->rollback();
131 //conn->beginTrans(isoLevel, TSYNC);
132 conn->beginTrans(isoLevel);
133 return true;
135 else if (strncasecmp (st, "SET AUTOCOMMIT ON", 17) == 0)
137 autocommitmode = true;
138 printf("AUTOCOMMIT Mode is set to ON\n");
139 return true;
141 else if (strncasecmp (st, "SET AUTOCOMMIT OFF", 18) == 0)
143 autocommitmode = false;
144 printf("AUTOCOMMIT Mode is set to OFF\n");
145 return true;
147 else if (strncasecmp (st, "SET ISOLATION LEVEL UNCOMMITTED", 31) == 0)
149 isoLevel = READ_UNCOMMITTED;
150 printf("Isolation Level is set to READ_UNCOMMITTED\n");
151 return true;
153 else if (strncasecmp (st, "SET ISOLATION LEVEL COMMITTED", 29) == 0)
155 isoLevel = READ_COMMITTED;
156 printf("Isolation Level is set to READ_COMMITTED\n");
157 return true;
159 else if (strncasecmp (st, "SET ISOLATION LEVEL REPEATABLE", 30) == 0)
161 isoLevel = READ_REPEATABLE;
162 printf("Isolation Level is set to READ_REPEATABLE\n");
163 return true;
165 return false;
167 bool handleEchoAndComment(char *st)
169 while (isspace (*st)|| *st == '(' ) st++; // Skip white spaces
170 if (strncasecmp (st, "ECHO", 4) == 0)
172 printf("%s\n", st);
173 return true;
174 }else if (strncmp(st, "--", 2) == 0)
176 return true;
177 }else if (strncasecmp(st, "help", 2) == 0)
179 printHelp();
180 return true;
181 }else if (strncasecmp(st, "show tables", 11) == 0)
183 Connection conn;
184 conn.open("root","manager");
185 DatabaseManagerImpl *dbMgr = (DatabaseManagerImpl*)conn.getDatabaseManager();
186 List tableList = dbMgr->getAllTableNames();
187 ListIterator iter = tableList.getIterator();
188 Identifier *elem = NULL;
189 int ret =0;
190 printf("=============TableNames===================\n");
191 int count =0;
192 while (iter.hasElement())
194 elem = (Identifier*) iter.nextElement();
195 count++;
196 printf(" %s \n", elem->name);
198 if (count ==0) printf(" No tables exist\n");
199 printf("=========================================\n");
200 conn.close();
201 return true;
203 return false;
205 void printHelp()
207 printf("CSQL Command List\n");
208 printf("======================================================\n");
209 printf("SHOW TABLES\n");
210 printf("SET AUTOCOMMIT ON|OFF\n");
211 printf("SET ISOLATION LEVEL UNCOMMITTED|COMMITTED|REPEATABLE\n");
212 printf("CREATE TABLE|INDEX ...\n");
213 printf("INSERT ...\n");
214 printf("UPDATE ...\n");
215 printf("DELETE ...\n");
216 printf("SELECT ...\n");
217 printf("======================================================\n");
219 void setStmtType(char *st)
221 if (strncasecmp (st, "SELECT", 6) == 0) {stmtType=SELECT; return; }
222 else if (strncasecmp (st, "CREATE", 6) == 0) {stmtType=DDL; return; }
223 else if (strncasecmp (st, "DROP", 4) == 0) { stmtType=DDL; return; }
224 stmtType = OTHER;
225 return ;
228 char getQueryFromStdIn(char *buf)
230 char c, *bufBegin=buf;
231 int ln, charCnt=0;
233 ln=1;
234 printf("CSQL>");
235 while( (c=(char ) getchar()) != EOF && c != ';')
237 *buf++ = c; charCnt++;
238 if(c=='\n') //printf("%1d>",ln++);
239 ln++;
241 if( charCnt == SQL_STMT_LEN ) {
242 printf("SQL Statement length is greater than %d. "
243 "Ignoring the statement.\n", SQL_STMT_LEN );
244 *bufBegin++ =';';
245 *bufBegin ='\0';
246 return 0;
249 *buf++ = ';';
250 *buf = '\0';
251 return c;
253 char getQueryFromFile(char *buf)
255 char c, *bufBegin=buf;
256 int charCnt=0;
257 while( (c=(char ) fgetc(fp)) != EOF && c != ';')
259 *buf++ = c; charCnt++;
260 if( charCnt == SQL_STMT_LEN ) {
261 printf("SQL Statement length is greater than %d. "
262 "Ignoring the statement.\n", SQL_STMT_LEN );
263 *bufBegin++ =';';
264 *bufBegin ='\0';
265 return 0;
268 *buf++ = ';';
269 *buf = '\0';
270 return c;
273 bool getInput(bool fromFile)
275 char buffer [SQL_STMT_LEN + 1];
277 char eof;
278 if (fromFile == false)
279 eof = getQueryFromStdIn(buffer);
280 else
281 eof = getQueryFromFile(buffer);
283 char *buf = buffer;
284 while(*buf == ' ' || *buf == '\t' || *buf == '\n') buf++;
285 if (eof == EOF || strncasecmp (buf, "quit", 4) == 0)
286 return false;
287 if (handleTransaction(buf)) return true;
288 if (handleEchoAndComment(buf)) return true;
289 if ( *buf == ';' ) return true; // Null statement.
291 setStmtType(buf);
293 DbRetVal rv = stmt->prepare(buf);
294 if (rv != OK)
296 printf("Statement prepare failed with error %d\n", rv);
297 return true;
299 int rows =0;
300 rv = stmt->execute(rows);
301 if (rv != OK)
303 printf("Statement execute failed with error %d\n", rv);
304 stmt->free();
305 return true;
307 if (stmtType == OTHER)
309 printf("Statement Executed: Rows Affected = %d\n", rows);
311 else if (stmtType == DDL)
313 printf("Statement Executed\n");
315 else
317 FieldInfo *info = new FieldInfo();
318 printf("---------------------------------------------------------\n");
319 printf("\t");
320 for (int i = 0 ; i < stmt->noOfProjFields() ; i++)
322 stmt->getProjFldInfo(i, info);
323 printf("%s\t", info->fldName);
325 printf("\n---------------------------------------------------------\n");
326 delete info;
327 void *tuple = NULL;
328 while(true)
330 printf("\t");
331 tuple = (char*)stmt->fetchAndPrint();
332 printf("\n");
333 if (tuple == NULL) { break; }
335 stmt->close();
337 stmt->free();
338 if (autocommitmode)
340 conn->commit();
341 //conn->beginTrans(isoLevel, TSYNC);
342 conn->beginTrans(isoLevel);
343 return true;
345 return true;