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>
18 #include <Statement.h>
19 #include <SqlFactory.h>
20 #include <SqlStatement.h>
21 #define SQL_STMT_LEN 1024
28 STMT_TYPE stmtType
= SELECT
;
30 AbsSqlConnection
*conn
;
31 AbsSqlStatement
*stmt
;
33 bool autocommitmode
= true;
34 IsolationLevel isoLevel
= READ_COMMITTED
;
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");
47 int main(int argc
, char **argv
)
49 char username
[IDENTIFIER_LENGTH
];
51 char password
[IDENTIFIER_LENGTH
];
56 while ((c
= getopt(argc
, argv
, "u:p:s:?")) != EOF
)
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);
68 //printf("%s %s %s", username, password, filename);
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");
85 printf("Unable to open the file %s\n", filename
);
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
106 if (filename
[0] !='\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 )
122 conn
->beginTrans(isoLevel
);
125 else if (strncasecmp (st
, "ROLLBACK", 8) == 0||
126 strncasecmp (st
, "rollback", 8) == 0)
129 conn
->beginTrans(isoLevel
);
132 else if (strncasecmp (st
, "SET AUTOCOMMIT ON", 17) == 0)
134 autocommitmode
= true;
135 printf("AUTOCOMMIT Mode is set to ON\n");
138 else if (strncasecmp (st
, "SET AUTOCOMMIT OFF", 18) == 0)
140 autocommitmode
= false;
141 printf("AUTOCOMMIT Mode is set to OFF\n");
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");
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");
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");
164 bool handleEchoAndComment(char *st
)
166 while (isspace (*st
)|| *st
== '(' ) st
++; // Skip white spaces
167 if (strncasecmp (st
, "ECHO", 4) == 0)
171 }else if (strncmp(st
, "--", 2) == 0)
174 }else if (strncasecmp(st
, "help", 2) == 0)
178 }else if (strncasecmp(st
, "show tables", 11) == 0)
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
;
187 printf("=============TableNames===================\n");
189 while (iter
.hasElement())
191 elem
= (Identifier
*) iter
.nextElement();
193 printf(" %s \n", elem
->name
);
195 if (count
==0) printf(" No tables exist\n");
196 printf("=========================================\n");
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; }
225 char getQueryFromStdIn(char *buf
)
227 char c
, *bufBegin
=buf
;
232 while( (c
=(char ) getchar()) != EOF
&& c
!= ';')
234 *buf
++ = c
; charCnt
++;
235 if(c
=='\n') //printf("%1d>",ln++);
238 if( charCnt
== SQL_STMT_LEN
) {
239 printf("SQL Statement length is greater than %d. "
240 "Ignoring the statement.\n", SQL_STMT_LEN
);
250 char getQueryFromFile(char *buf
)
252 char c
, *bufBegin
=buf
;
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
);
270 bool getInput(bool fromFile
)
272 char buffer
[SQL_STMT_LEN
+ 1];
275 if (fromFile
== false)
276 eof
= getQueryFromStdIn(buffer
);
278 eof
= getQueryFromFile(buffer
);
281 while(*buf
== ' ' || *buf
== '\t' || *buf
== '\n') buf
++;
282 if (eof
== EOF
|| strncasecmp (buf
, "quit", 4) == 0)
284 if (handleTransaction(buf
)) return true;
285 if (handleEchoAndComment(buf
)) return true;
286 if ( *buf
== ';' ) return true; // Null statement.
290 DbRetVal rv
= stmt
->prepare(buf
);
293 printf("Statement prepare failed with error %d\n", rv
);
297 rv
= stmt
->execute(rows
);
300 printf("Statement execute failed with error %d\n", rv
);
304 if (stmtType
== OTHER
)
306 printf("Statement Executed: Rows Affected = %d\n", rows
);
308 else if (stmtType
== DDL
)
310 printf("Statement Executed\n");
314 FieldInfo
*info
= new FieldInfo();
315 printf("---------------------------------------------------------\n");
317 for (int i
= 0 ; i
< stmt
->noOfProjFields() ; i
++)
319 stmt
->getProjFldInfo(i
, info
);
320 printf("%s\t", info
->fldName
);
322 printf("\n---------------------------------------------------------\n");
328 tuple
= (char*)stmt
->fetchAndPrint();
330 if (tuple
== NULL
) { break; }
338 conn
->beginTrans(isoLevel
);