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 #include <SqlNwConnection.h>
22 #include <SqlNwStatement.h>
23 #define SQL_STMT_LEN 1024
30 STMT_TYPE stmtType
= SELECT
;
32 AbsSqlConnection
*conn
;
33 AbsSqlStatement
*stmt
;
35 bool gateway
=false, silent
=false;
36 bool autocommitmode
= true;
38 IsolationLevel isoLevel
= READ_COMMITTED
;
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");
54 int main(int argc
, char **argv
)
56 char username
[IDENTIFIER_LENGTH
];
58 char password
[IDENTIFIER_LENGTH
];
60 char hostname
[IDENTIFIER_LENGTH
];
67 while ((c
= getopt(argc
, argv
, "u:p:s:H:P:gS?")) != EOF
)
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);
85 //printf("%s %s %s", username, password, filename);
91 if (username
[0] == '\0' )
93 strcpy(username
, "root");
94 strcpy(password
, "manager");
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");
106 printf("Unable to open the file %s\n", filename
);
114 conn
= SqlFactory::createConnection(CSqlGateway
);
116 conn
= new SqlNwConnection();
117 conn
->setInnerConnection(NULL
);
118 SqlNwConnection
*con
= (SqlNwConnection
*)conn
;
119 con
->setHost(hostname
, atoi(port
));
122 conn
= SqlFactory::createConnection(CSql
);
124 rv
= conn
->connect(username
,password
);
125 if (rv
!= OK
) return 1;
127 stmt
= SqlFactory::createStatement(CSqlGateway
);
129 stmt
= new SqlNwStatement();
130 stmt
->setInnerStatement(NULL
);
133 stmt
= SqlFactory::createStatement(CSql
);
134 stmt
->setConnection(conn
);
135 //rv = conn->beginTrans(READ_COMMITTED, TSYNC);
136 rv
= conn
->beginTrans();
137 if (rv
!= OK
) return 2;
138 while (getInput(fileFlag
) == true) continue;
140 //TODO::conn should provide method telling status of the transaction.
141 //if running, then abort it
143 if (filename
[0] !='\0')
152 bool handleTransaction(char *st
)
154 while (isspace (*st
)|| *st
== '(' ) st
++; // Skip white spaces
155 if (strcasecmp (st
, "COMMIT;") == 0 ||
156 strcasecmp (st
, "commit;") == 0 )
159 //conn->beginTrans(isoLevel, TSYNC);
160 conn
->beginTrans(isoLevel
);
163 else if (strcasecmp (st
, "ROLLBACK;") == 0||
164 strcasecmp (st
, "rollback;") == 0)
167 //conn->beginTrans(isoLevel, TSYNC);
168 conn
->beginTrans(isoLevel
);
171 else if (strcasecmp (st
, "SET AUTOCOMMIT ON;") == 0)
173 autocommitmode
= true;
174 if (!silent
) printf("AUTOCOMMIT Mode is set to ON\n");
177 else if (strcasecmp (st
, "SET AUTOCOMMIT OFF;") == 0)
179 autocommitmode
= false;
180 if (!silent
) printf("AUTOCOMMIT Mode is set to OFF\n");
183 else if (strcasecmp (st
, "SET ISOLATION LEVEL UNCOMMITTED;") == 0)
185 isoLevel
= READ_UNCOMMITTED
;
186 printf("Isolation Level is set to READ_UNCOMMITTED\n");
189 else if (strcasecmp (st
, "SET ISOLATION LEVEL COMMITTED;") == 0)
191 isoLevel
= READ_COMMITTED
;
192 printf("Isolation Level is set to READ_COMMITTED\n");
195 else if (strcasecmp (st
, "SET ISOLATION LEVEL REPEATABLE;") == 0)
197 isoLevel
= READ_REPEATABLE
;
198 printf("Isolation Level is set to READ_REPEATABLE\n");
203 bool handleEchoAndComment(char *st
)
205 while (isspace (*st
)|| *st
== '(' ) st
++; // Skip white spaces
206 if (strncasecmp (st
, "ECHO", 4) == 0)
210 }else if (strncmp(st
, "--", 2) == 0)
213 }else if (strncasecmp(st
, "help", 2) == 0)
217 }else if (strcasecmp(st
, "show tables;") == 0)
220 conn
.open("root","manager");
221 DatabaseManagerImpl
*dbMgr
= (DatabaseManagerImpl
*)conn
.getDatabaseManager();
224 printf("Unable to connect to csql server\n");
227 List tableList
= dbMgr
->getAllTableNames();
228 ListIterator iter
= tableList
.getIterator();
229 Identifier
*elem
= NULL
;
231 printf("=============TableNames===================\n");
233 while (iter
.hasElement())
235 elem
= (Identifier
*) iter
.nextElement();
237 printf(" %s \n", elem
->name
);
239 if (count
==0) printf(" No tables exist\n");
240 printf("=========================================\n");
248 printf("CSQL Command List\n");
249 printf("======================================================\n");
250 printf("SHOW TABLES\n");
251 printf("SET AUTOCOMMIT ON|OFF\n");
252 printf("SET ISOLATION LEVEL UNCOMMITTED|COMMITTED|REPEATABLE\n");
253 printf("CREATE TABLE|INDEX ...\n");
254 printf("INSERT ...\n");
255 printf("UPDATE ...\n");
256 printf("DELETE ...\n");
257 printf("SELECT ...\n");
258 printf("======================================================\n");
260 void setStmtType(char *st
)
262 if (strncasecmp (st
, "SELECT", 6) == 0) {stmtType
=SELECT
; return; }
263 else if (strncasecmp (st
, "CREATE", 6) == 0) {stmtType
=DDL
; return; }
264 else if (strncasecmp (st
, "DROP", 4) == 0) { stmtType
=DDL
; return; }
269 char getQueryFromStdIn(char *buf
)
271 char c
, *bufBegin
=buf
;
276 while( (c
=(char ) getchar()) != EOF
&& c
!= ';')
278 *buf
++ = c
; charCnt
++;
279 if(c
=='\n') //printf("%1d>",ln++);
281 if( charCnt
== SQL_STMT_LEN
) {
282 printf("SQL Statement length is greater than %d. "
283 "Ignoring the statement.\n", SQL_STMT_LEN
);
293 char getQueryFromFile(char *buf
)
295 char c
, *bufBegin
=buf
;
297 while( (c
=(char ) fgetc(fp
)) != EOF
&& c
!= ';')
299 *buf
++ = c
; charCnt
++;
300 if( charCnt
== SQL_STMT_LEN
) {
301 printf("SQL Statement length is greater than %d. "
302 "Ignoring the statement.\n", SQL_STMT_LEN
);
313 bool getInput(bool fromFile
)
315 char buffer
[SQL_STMT_LEN
+ 1];
318 if (fromFile
== false)
319 eof
= getQueryFromStdIn(buffer
);
321 eof
= getQueryFromFile(buffer
);
324 while(*buf
== ' ' || *buf
== '\t' || *buf
== '\n') buf
++;
325 if (eof
== EOF
|| strncasecmp (buf
, "quit", 4) == 0)
327 if (handleTransaction(buf
)) return true;
328 if (handleEchoAndComment(buf
)) return true;
329 if ( *buf
== ';' ) return true; // Null statement.
333 DbRetVal rv
= stmt
->prepare(buf
);
336 printf("Statement prepare failed with error %d\n", rv
);
340 rv
= stmt
->execute(rows
);
343 printf("Statement execute failed with error %d\n", rv
);
347 if (stmtType
== OTHER
)
349 if (!silent
) printf("Statement Executed: Rows Affected = %d\n", rows
);
351 else if (stmtType
== DDL
)
353 if (!silent
) printf("Statement Executed\n");
357 FieldInfo
*info
= new FieldInfo();
358 printf("---------------------------------------------------------\n");
360 for (int i
= 0 ; i
< stmt
->noOfProjFields() ; i
++)
362 stmt
->getProjFldInfo(i
+1, info
);
363 printf("%s\t", info
->fldName
);
365 printf("\n---------------------------------------------------------\n");
371 tuple
= (char*)stmt
->fetchAndPrint(false);
373 if (tuple
== NULL
) { break; }
381 //conn->beginTrans(isoLevel, TSYNC);
382 conn
->beginTrans(isoLevel
);