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 gateway
=false, silent
=false;
34 bool autocommitmode
= true;
35 IsolationLevel isoLevel
= READ_COMMITTED
;
40 printf("Usage: csql [-u username] [-p passwd] [-s sqlfile] \n");
41 printf(" username -> username to connect to database\n");
42 printf(" password -> password to connect to database\n");
43 printf(" sqlfile -> filename containing sql statements\n");
48 int main(int argc
, char **argv
)
50 char username
[IDENTIFIER_LENGTH
];
52 char password
[IDENTIFIER_LENGTH
];
57 while ((c
= getopt(argc
, argv
, "u:p:s:gS?")) != EOF
)
61 case 'u' : strcpy(username
, argv
[optind
- 1]); break;
62 case 'p' : strcpy(password
, argv
[optind
- 1]); break;
63 case 's' : strcpy(filename
, argv
[optind
- 1]); break;
64 case '?' : { opt
= 1; break; } //print help
65 case 'S' : { silent
= true; break; } //silent
66 case 'g' : { gateway
= true; break; } //print help
67 default: printf("Wrong args\n"); exit(1);
71 //printf("%s %s %s", username, password, filename);
77 if (username
[0] == '\0' )
79 strcpy(username
, "root");
80 strcpy(password
, "manager");
82 bool fileFlag
= false;
83 if (filename
[0] !='\0')
85 fp
= fopen(filename
,"r");
88 printf("Unable to open the file %s\n", filename
);
96 conn
= SqlFactory::createConnection(CSqlGateway
);
98 conn
= SqlFactory::createConnection(CSql
);
99 rv
= conn
->connect(username
,password
);
100 if (rv
!= OK
) return 1;
102 stmt
= SqlFactory::createStatement(CSqlGateway
);
104 stmt
= SqlFactory::createStatement(CSql
);
105 stmt
->setConnection(conn
);
106 //rv = conn->beginTrans(READ_COMMITTED, TSYNC);
107 rv
= conn
->beginTrans();
108 if (rv
!= OK
) return 2;
109 while (getInput(fileFlag
) == true) continue;
111 //TODO::conn should provide method telling status of the transaction.
112 //if running, then abort it
114 if (filename
[0] !='\0')
123 bool handleTransaction(char *st
)
125 while (isspace (*st
)|| *st
== '(' ) st
++; // Skip white spaces
126 if (strcasecmp (st
, "COMMIT;") == 0 ||
127 strcasecmp (st
, "commit;") == 0 )
130 //conn->beginTrans(isoLevel, TSYNC);
131 conn
->beginTrans(isoLevel
);
134 else if (strcasecmp (st
, "ROLLBACK;") == 0||
135 strcasecmp (st
, "rollback;") == 0)
138 //conn->beginTrans(isoLevel, TSYNC);
139 conn
->beginTrans(isoLevel
);
142 else if (strcasecmp (st
, "SET AUTOCOMMIT ON;") == 0)
144 autocommitmode
= true;
145 if (!silent
) printf("AUTOCOMMIT Mode is set to ON\n");
148 else if (strcasecmp (st
, "SET AUTOCOMMIT OFF;") == 0)
150 autocommitmode
= false;
151 if (!silent
) printf("AUTOCOMMIT Mode is set to OFF\n");
154 else if (strcasecmp (st
, "SET ISOLATION LEVEL UNCOMMITTED;") == 0)
156 isoLevel
= READ_UNCOMMITTED
;
157 printf("Isolation Level is set to READ_UNCOMMITTED\n");
160 else if (strcasecmp (st
, "SET ISOLATION LEVEL COMMITTED;") == 0)
162 isoLevel
= READ_COMMITTED
;
163 printf("Isolation Level is set to READ_COMMITTED\n");
166 else if (strcasecmp (st
, "SET ISOLATION LEVEL REPEATABLE;") == 0)
168 isoLevel
= READ_REPEATABLE
;
169 printf("Isolation Level is set to READ_REPEATABLE\n");
174 bool handleEchoAndComment(char *st
)
176 while (isspace (*st
)|| *st
== '(' ) st
++; // Skip white spaces
177 if (strncasecmp (st
, "ECHO", 4) == 0)
181 }else if (strncmp(st
, "--", 2) == 0)
184 }else if (strncasecmp(st
, "help", 2) == 0)
188 }else if (strcasecmp(st
, "show tables;") == 0)
191 conn
.open("root","manager");
192 DatabaseManagerImpl
*dbMgr
= (DatabaseManagerImpl
*)conn
.getDatabaseManager();
195 printf("Unable to connect to csql server\n");
198 List tableList
= dbMgr
->getAllTableNames();
199 ListIterator iter
= tableList
.getIterator();
200 Identifier
*elem
= NULL
;
202 printf("=============TableNames===================\n");
204 while (iter
.hasElement())
206 elem
= (Identifier
*) iter
.nextElement();
208 printf(" %s \n", elem
->name
);
210 if (count
==0) printf(" No tables exist\n");
211 printf("=========================================\n");
219 printf("CSQL Command List\n");
220 printf("======================================================\n");
221 printf("SHOW TABLES\n");
222 printf("SET AUTOCOMMIT ON|OFF\n");
223 printf("SET ISOLATION LEVEL UNCOMMITTED|COMMITTED|REPEATABLE\n");
224 printf("CREATE TABLE|INDEX ...\n");
225 printf("INSERT ...\n");
226 printf("UPDATE ...\n");
227 printf("DELETE ...\n");
228 printf("SELECT ...\n");
229 printf("======================================================\n");
231 void setStmtType(char *st
)
233 if (strncasecmp (st
, "SELECT", 6) == 0) {stmtType
=SELECT
; return; }
234 else if (strncasecmp (st
, "CREATE", 6) == 0) {stmtType
=DDL
; return; }
235 else if (strncasecmp (st
, "DROP", 4) == 0) { stmtType
=DDL
; return; }
240 char getQueryFromStdIn(char *buf
)
242 char c
, *bufBegin
=buf
;
247 while( (c
=(char ) getchar()) != EOF
&& c
!= ';')
249 *buf
++ = c
; charCnt
++;
250 if(c
=='\n') //printf("%1d>",ln++);
253 if( charCnt
== SQL_STMT_LEN
) {
254 printf("SQL Statement length is greater than %d. "
255 "Ignoring the statement.\n", SQL_STMT_LEN
);
265 char getQueryFromFile(char *buf
)
267 char c
, *bufBegin
=buf
;
269 while( (c
=(char ) fgetc(fp
)) != EOF
&& c
!= ';')
271 *buf
++ = c
; charCnt
++;
272 if( charCnt
== SQL_STMT_LEN
) {
273 printf("SQL Statement length is greater than %d. "
274 "Ignoring the statement.\n", SQL_STMT_LEN
);
285 bool getInput(bool fromFile
)
287 char buffer
[SQL_STMT_LEN
+ 1];
290 if (fromFile
== false)
291 eof
= getQueryFromStdIn(buffer
);
293 eof
= getQueryFromFile(buffer
);
296 while(*buf
== ' ' || *buf
== '\t' || *buf
== '\n') buf
++;
297 if (eof
== EOF
|| strncasecmp (buf
, "quit", 4) == 0)
299 if (handleTransaction(buf
)) return true;
300 if (handleEchoAndComment(buf
)) return true;
301 if ( *buf
== ';' ) return true; // Null statement.
305 DbRetVal rv
= stmt
->prepare(buf
);
308 printf("Statement prepare failed with error %d\n", rv
);
312 rv
= stmt
->execute(rows
);
315 printf("Statement execute failed with error %d\n", rv
);
319 if (stmtType
== OTHER
)
321 if (!silent
) printf("Statement Executed: Rows Affected = %d\n", rows
);
323 else if (stmtType
== DDL
)
325 if (!silent
) printf("Statement Executed\n");
329 FieldInfo
*info
= new FieldInfo();
330 printf("---------------------------------------------------------\n");
332 for (int i
= 0 ; i
< stmt
->noOfProjFields() ; i
++)
334 stmt
->getProjFldInfo(i
, info
);
335 printf("%s\t", info
->fldName
);
337 printf("\n---------------------------------------------------------\n");
343 tuple
= (char*)stmt
->fetchAndPrint(false);
345 if (tuple
== NULL
) { break; }
353 //conn->beginTrans(isoLevel, TSYNC);
354 conn
->beginTrans(isoLevel
);