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(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
107 if (filename
[0] !='\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 )
123 //conn->beginTrans(isoLevel, TSYNC);
124 conn
->beginTrans(isoLevel
);
127 else if (strncasecmp (st
, "ROLLBACK", 8) == 0||
128 strncasecmp (st
, "rollback", 8) == 0)
131 //conn->beginTrans(isoLevel, TSYNC);
132 conn
->beginTrans(isoLevel
);
135 else if (strncasecmp (st
, "SET AUTOCOMMIT ON", 17) == 0)
137 autocommitmode
= true;
138 printf("AUTOCOMMIT Mode is set to ON\n");
141 else if (strncasecmp (st
, "SET AUTOCOMMIT OFF", 18) == 0)
143 autocommitmode
= false;
144 printf("AUTOCOMMIT Mode is set to OFF\n");
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");
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");
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");
167 bool handleEchoAndComment(char *st
)
169 while (isspace (*st
)|| *st
== '(' ) st
++; // Skip white spaces
170 if (strncasecmp (st
, "ECHO", 4) == 0)
174 }else if (strncmp(st
, "--", 2) == 0)
177 }else if (strncasecmp(st
, "help", 2) == 0)
181 }else if (strncasecmp(st
, "show tables", 11) == 0)
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
;
190 printf("=============TableNames===================\n");
192 while (iter
.hasElement())
194 elem
= (Identifier
*) iter
.nextElement();
196 printf(" %s \n", elem
->name
);
198 if (count
==0) printf(" No tables exist\n");
199 printf("=========================================\n");
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; }
228 char getQueryFromStdIn(char *buf
)
230 char c
, *bufBegin
=buf
;
235 while( (c
=(char ) getchar()) != EOF
&& c
!= ';')
237 *buf
++ = c
; charCnt
++;
238 if(c
=='\n') //printf("%1d>",ln++);
241 if( charCnt
== SQL_STMT_LEN
) {
242 printf("SQL Statement length is greater than %d. "
243 "Ignoring the statement.\n", SQL_STMT_LEN
);
253 char getQueryFromFile(char *buf
)
255 char c
, *bufBegin
=buf
;
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
);
273 bool getInput(bool fromFile
)
275 char buffer
[SQL_STMT_LEN
+ 1];
278 if (fromFile
== false)
279 eof
= getQueryFromStdIn(buffer
);
281 eof
= getQueryFromFile(buffer
);
284 while(*buf
== ' ' || *buf
== '\t' || *buf
== '\n') buf
++;
285 if (eof
== EOF
|| strncasecmp (buf
, "quit", 4) == 0)
287 if (handleTransaction(buf
)) return true;
288 if (handleEchoAndComment(buf
)) return true;
289 if ( *buf
== ';' ) return true; // Null statement.
293 DbRetVal rv
= stmt
->prepare(buf
);
296 printf("Statement prepare failed with error %d\n", rv
);
300 rv
= stmt
->execute(rows
);
303 printf("Statement execute failed with error %d\n", rv
);
307 if (stmtType
== OTHER
)
309 printf("Statement Executed: Rows Affected = %d\n", rows
);
311 else if (stmtType
== DDL
)
313 printf("Statement Executed\n");
317 FieldInfo
*info
= new FieldInfo();
318 printf("---------------------------------------------------------\n");
320 for (int i
= 0 ; i
< stmt
->noOfProjFields() ; i
++)
322 stmt
->getProjFldInfo(i
, info
);
323 printf("%s\t", info
->fldName
);
325 printf("\n---------------------------------------------------------\n");
331 tuple
= (char*)stmt
->fetchAndPrint();
333 if (tuple
== NULL
) { break; }
341 //conn->beginTrans(isoLevel, TSYNC);
342 conn
->beginTrans(isoLevel
);