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
;
34 SqlApiImplType type
= CSqlUnknown
;
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 if (gateway
) type
= CSqlNetworkGateway
;
115 else type
= CSqlNetwork
;
116 conn
= SqlFactory::createConnection(type
);
117 SqlNwConnection
*con
= (SqlNwConnection
*)conn
;
118 con
->setHost(hostname
, atoi(port
));
121 if (gateway
) type
= CSqlGateway
;
123 conn
= SqlFactory::createConnection(type
);
125 rv
= conn
->connect(username
,password
);
126 if (rv
!= OK
) return 1;
127 stmt
= SqlFactory::createStatement(type
);
128 stmt
->setConnection(conn
);
129 //rv = conn->beginTrans(READ_COMMITTED, TSYNC);
130 rv
= conn
->beginTrans();
131 if (rv
!= OK
) return 2;
132 while (getInput(fileFlag
) == true) continue;
134 //TODO::conn should provide method telling status of the transaction.
135 //if running, then abort it
137 if (filename
[0] !='\0')
146 bool handleTransaction(char *st
)
148 while (isspace (*st
)|| *st
== '(' ) st
++; // Skip white spaces
149 if (strcasecmp (st
, "COMMIT;") == 0 ||
150 strcasecmp (st
, "commit;") == 0 )
153 //conn->beginTrans(isoLevel, TSYNC);
154 conn
->beginTrans(isoLevel
);
157 else if (strcasecmp (st
, "ROLLBACK;") == 0||
158 strcasecmp (st
, "rollback;") == 0)
161 //conn->beginTrans(isoLevel, TSYNC);
162 conn
->beginTrans(isoLevel
);
165 else if (strcasecmp (st
, "SET AUTOCOMMIT ON;") == 0)
167 autocommitmode
= true;
168 if (!silent
) printf("AUTOCOMMIT Mode is set to ON\n");
171 else if (strcasecmp (st
, "SET AUTOCOMMIT OFF;") == 0)
173 autocommitmode
= false;
174 if (!silent
) printf("AUTOCOMMIT Mode is set to OFF\n");
177 else if (strcasecmp (st
, "SET ISOLATION LEVEL UNCOMMITTED;") == 0)
179 isoLevel
= READ_UNCOMMITTED
;
180 printf("Isolation Level is set to READ_UNCOMMITTED\n");
183 else if (strcasecmp (st
, "SET ISOLATION LEVEL COMMITTED;") == 0)
185 isoLevel
= READ_COMMITTED
;
186 printf("Isolation Level is set to READ_COMMITTED\n");
189 else if (strcasecmp (st
, "SET ISOLATION LEVEL REPEATABLE;") == 0)
191 isoLevel
= READ_REPEATABLE
;
192 printf("Isolation Level is set to READ_REPEATABLE\n");
197 bool handleEchoAndComment(char *st
)
199 while (isspace (*st
)|| *st
== '(' ) st
++; // Skip white spaces
200 if (strncasecmp (st
, "ECHO", 4) == 0)
204 }else if (strncmp(st
, "--", 2) == 0)
207 }else if (strncasecmp(st
, "help", 2) == 0)
211 }else if (strcasecmp(st
, "show tables;") == 0) {
213 List tableList
= stmt
->getAllTableNames(rv
);
214 ListIterator iter
= tableList
.getIterator();
215 Identifier
*elem
= NULL
;
217 printf("=============TableNames===================\n");
219 while (iter
.hasElement())
221 elem
= (Identifier
*) iter
.nextElement();
223 printf(" %s \n", elem
->name
);
225 if (count
==0) printf(" No tables exist\n");
226 printf("==========================================\n");
234 printf("CSQL Command List\n");
235 printf("======================================================\n");
236 printf("SHOW TABLES\n");
237 printf("SET AUTOCOMMIT ON|OFF\n");
238 printf("SET ISOLATION LEVEL UNCOMMITTED|COMMITTED|REPEATABLE\n");
239 printf("CREATE TABLE|INDEX ...\n");
240 printf("INSERT ...\n");
241 printf("UPDATE ...\n");
242 printf("DELETE ...\n");
243 printf("SELECT ...\n");
244 printf("======================================================\n");
246 void setStmtType(char *st
)
248 if (strncasecmp (st
, "SELECT", 6) == 0) {stmtType
=SELECT
; return; }
249 else if (strncasecmp (st
, "CREATE", 6) == 0) {stmtType
=DDL
; return; }
250 else if (strncasecmp (st
, "DROP", 4) == 0) { stmtType
=DDL
; return; }
255 char getQueryFromStdIn(char *buf
)
257 char c
, *bufBegin
=buf
;
262 while( (c
=(char ) getchar()) != EOF
&& c
!= ';')
264 *buf
++ = c
; charCnt
++;
265 if(c
=='\n') //printf("%1d>",ln++);
267 if( charCnt
== SQL_STMT_LEN
) {
268 printf("SQL Statement length is greater than %d. "
269 "Ignoring the statement.\n", SQL_STMT_LEN
);
279 char getQueryFromFile(char *buf
)
281 char c
, *bufBegin
=buf
;
283 while( (c
=(char ) fgetc(fp
)) != EOF
&& c
!= ';')
285 *buf
++ = c
; charCnt
++;
286 if( charCnt
== SQL_STMT_LEN
) {
287 printf("SQL Statement length is greater than %d. "
288 "Ignoring the statement.\n", SQL_STMT_LEN
);
299 bool getInput(bool fromFile
)
301 char buffer
[SQL_STMT_LEN
+ 1];
304 if (fromFile
== false)
305 eof
= getQueryFromStdIn(buffer
);
307 eof
= getQueryFromFile(buffer
);
310 while(*buf
== ' ' || *buf
== '\t' || *buf
== '\n') buf
++;
311 if (eof
== EOF
|| strncasecmp (buf
, "quit", 4) == 0)
313 if (handleTransaction(buf
)) return true;
314 if (handleEchoAndComment(buf
)) return true;
315 if ( *buf
== ';' ) return true; // Null statement.
319 DbRetVal rv
= stmt
->prepare(buf
);
322 printf("Statement prepare failed with error %d\n", rv
);
326 rv
= stmt
->execute(rows
);
329 printf("Statement execute failed with error %d\n", rv
);
333 if (stmtType
== OTHER
)
335 if (!silent
) printf("Statement Executed: Rows Affected = %d\n", rows
);
337 else if (stmtType
== DDL
)
339 if (!silent
) printf("Statement Executed\n");
343 FieldInfo
*info
= new FieldInfo();
344 printf("---------------------------------------------------------\n");
346 for (int i
= 0 ; i
< stmt
->noOfProjFields() ; i
++)
348 stmt
->getProjFldInfo(i
+1, info
);
349 printf("%s\t", info
->fldName
);
351 printf("\n---------------------------------------------------------\n");
357 tuple
= (char*)stmt
->fetchAndPrint(false);
359 if (tuple
== NULL
) { break; }
367 //conn->beginTrans(isoLevel, TSYNC);
368 conn
->beginTrans(isoLevel
);