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 #include <readline/readline.h>
24 #include <readline/history.h>
25 #define SQL_STMT_LEN 1024
32 STMT_TYPE stmtType
= SELECT
;
34 AbsSqlConnection
*conn
;
35 AbsSqlStatement
*stmt
;
36 SqlApiImplType type
= CSqlUnknown
;
37 bool gateway
=false, silent
=false;
38 bool autocommitmode
= true;
40 IsolationLevel isoLevel
= READ_COMMITTED
;
45 printf("Usage: csql [ [-u username] [-p passwd] ] [ [-H hostname] [-P port] ]\n");
46 printf(" [-s sqlfile] \n");
47 printf(" username -> username to connect to database\n");
48 printf(" password -> password to connect to database\n");
49 printf(" hostname -> hostname to connect to database through network\n");
50 printf(" port -> port no\n");
51 printf(" sqlfile -> filename containing sql statements\n");
56 int main(int argc
, char **argv
)
58 char username
[IDENTIFIER_LENGTH
];
60 char password
[IDENTIFIER_LENGTH
];
62 char hostname
[IDENTIFIER_LENGTH
];
69 while ((c
= getopt(argc
, argv
, "u:p:s:H:P:gS?")) != EOF
)
73 case 'u' : strcpy(username
, argv
[optind
- 1]); break;
74 case 'p' : strcpy(password
, argv
[optind
- 1]); break;
75 case 's' : strcpy(filename
, argv
[optind
- 1]); break;
76 case '?' : { opt
= 1; break; } //print help
77 case 'S' : { silent
= true; break; } //silent
78 case 'g' : { gateway
= true; break; } //print help
79 case 'H' : { strcpy (hostname
, argv
[optind
- 1]);
80 network
= true; break; }
81 case 'P' : { strcpy (port
, argv
[optind
- 1]);
82 network
= true; break; }
83 default: printf("Wrong args\n"); exit(1);
87 //printf("%s %s %s", username, password, filename);
93 if (username
[0] == '\0' )
95 strcpy(username
, "root");
96 strcpy(password
, "manager");
99 if (hostname
[0] == '\0') { printUsage(); return 0; }
100 if (port
[0] == '\0') { printUsage(); return 0; }
102 bool fileFlag
= false;
103 if (filename
[0] !='\0')
105 fp
= fopen(filename
,"r");
108 printf("Unable to open the file %s\n", filename
);
116 if (gateway
) type
= CSqlNetworkGateway
;
117 else type
= CSqlNetwork
;
118 conn
= SqlFactory::createConnection(type
);
119 SqlNwConnection
*con
= (SqlNwConnection
*)conn
;
120 con
->setHost(hostname
, atoi(port
));
123 if (gateway
) type
= CSqlGateway
;
125 conn
= SqlFactory::createConnection(type
);
127 rv
= conn
->connect(username
,password
);
128 if (rv
!= OK
) return 1;
129 stmt
= SqlFactory::createStatement(type
);
130 stmt
->setConnection(conn
);
131 //rv = conn->beginTrans(READ_COMMITTED, TSYNC);
132 rv
= conn
->beginTrans();
133 if (rv
!= OK
) return 2;
134 while (getInput(fileFlag
) == true) continue;
136 //TODO::conn should provide method telling status of the transaction.
137 //if running, then abort it
139 if (filename
[0] !='\0')
148 bool handleTransaction(char *st
)
150 while (isspace (*st
)|| *st
== '(' ) st
++; // Skip white spaces
151 if (strcasecmp (st
, "COMMIT;") == 0 ||
152 strcasecmp (st
, "commit;") == 0 )
155 //conn->beginTrans(isoLevel, TSYNC);
156 conn
->beginTrans(isoLevel
);
159 else if (strcasecmp (st
, "ROLLBACK;") == 0||
160 strcasecmp (st
, "rollback;") == 0)
163 //conn->beginTrans(isoLevel, TSYNC);
164 conn
->beginTrans(isoLevel
);
167 else if (strcasecmp (st
, "SET AUTOCOMMIT ON;") == 0)
169 autocommitmode
= true;
170 if (!silent
) printf("AUTOCOMMIT Mode is set to ON\n");
173 else if (strcasecmp (st
, "SET AUTOCOMMIT OFF;") == 0)
175 autocommitmode
= false;
176 if (!silent
) printf("AUTOCOMMIT Mode is set to OFF\n");
179 else if (strcasecmp (st
, "SET ISOLATION LEVEL UNCOMMITTED;") == 0)
181 isoLevel
= READ_UNCOMMITTED
;
182 printf("Isolation Level is set to READ_UNCOMMITTED\n");
185 else if (strcasecmp (st
, "SET ISOLATION LEVEL COMMITTED;") == 0)
187 isoLevel
= READ_COMMITTED
;
188 printf("Isolation Level is set to READ_COMMITTED\n");
191 else if (strcasecmp (st
, "SET ISOLATION LEVEL REPEATABLE;") == 0)
193 isoLevel
= READ_REPEATABLE
;
194 printf("Isolation Level is set to READ_REPEATABLE\n");
199 bool handleEchoAndComment(char *st
)
201 while (isspace (*st
)|| *st
== '(' ) st
++; // Skip white spaces
202 if (strncasecmp (st
, "ECHO", 4) == 0)
206 }else if (strncmp(st
, "--", 2) == 0)
209 }else if (strncasecmp(st
, "help", 2) == 0)
213 }else if (strcasecmp(st
, "show tables;") == 0) {
215 List tableList
= stmt
->getAllTableNames(rv
);
216 ListIterator iter
= tableList
.getIterator();
217 Identifier
*elem
= NULL
;
219 printf("=============TableNames===================\n");
221 while (iter
.hasElement())
223 elem
= (Identifier
*) iter
.nextElement();
225 printf(" %s \n", elem
->name
);
227 if (count
==0) printf(" No tables exist\n");
228 printf("==========================================\n");
236 printf("CSQL Command List\n");
237 printf("======================================================\n");
238 printf("SHOW TABLES\n");
239 printf("SET AUTOCOMMIT ON|OFF\n");
240 printf("SET ISOLATION LEVEL UNCOMMITTED|COMMITTED|REPEATABLE\n");
241 printf("CREATE TABLE|INDEX ...\n");
242 printf("INSERT ...\n");
243 printf("UPDATE ...\n");
244 printf("DELETE ...\n");
245 printf("SELECT ...\n");
246 printf("======================================================\n");
248 void setStmtType(char *st
)
250 if (strncasecmp (st
, "SELECT", 6) == 0) {stmtType
=SELECT
; return; }
251 else if (strncasecmp (st
, "CREATE", 6) == 0) {stmtType
=DDL
; return; }
252 else if (strncasecmp (st
, "DROP", 4) == 0) { stmtType
=DDL
; return; }
257 char getQueryFromStdIn(char *buf
)
259 char c
=EOF
, *bufBegin
=buf
;
264 while( (c=(char ) getchar()) != EOF && c != ';')
266 *buf++ = c; charCnt++;
267 if(c=='\n') //printf("%1d>",ln++);
269 if( charCnt == SQL_STMT_LEN ) {
270 printf("SQL Statement length is greater than %d. "
271 "Ignoring the statement.\n", SQL_STMT_LEN );
280 char *line
= readline("CSQL>");
281 if (line
) {strcpy(buf
, line
); add_history(line
); }
284 char getQueryFromFile(char *buf
)
286 char c
, *bufBegin
=buf
;
288 while( (c
=(char ) fgetc(fp
)) != EOF
&& c
!= ';')
290 *buf
++ = c
; charCnt
++;
291 if( charCnt
== SQL_STMT_LEN
) {
292 printf("SQL Statement length is greater than %d. "
293 "Ignoring the statement.\n", SQL_STMT_LEN
);
304 bool getInput(bool fromFile
)
306 char buffer
[SQL_STMT_LEN
+ 1];
309 if (fromFile
== false)
310 eof
= getQueryFromStdIn(buffer
);
312 eof
= getQueryFromFile(buffer
);
315 while(*buf
== ' ' || *buf
== '\t' || *buf
== '\n') buf
++;
316 if (eof
== EOF
|| strncasecmp (buf
, "quit", 4) == 0)
318 if (handleTransaction(buf
)) return true;
319 if (handleEchoAndComment(buf
)) return true;
320 if ( *buf
== ';' ) return true; // Null statement.
324 DbRetVal rv
= stmt
->prepare(buf
);
327 printf("Statement prepare failed with error %d\n", rv
);
331 rv
= stmt
->execute(rows
);
334 printf("Statement execute failed with error %d\n", rv
);
338 if (stmtType
== OTHER
)
340 if (!silent
) printf("Statement Executed: Rows Affected = %d\n", rows
);
342 else if (stmtType
== DDL
)
344 if (!silent
) printf("Statement Executed\n");
348 FieldInfo
*info
= new FieldInfo();
349 printf("---------------------------------------------------------\n");
351 for (int i
= 0 ; i
< stmt
->noOfProjFields() ; i
++)
353 stmt
->getProjFldInfo(i
+1, info
);
354 printf("%s\t", info
->fldName
);
356 printf("\n---------------------------------------------------------\n");
362 tuple
= (char*)stmt
->fetchAndPrint(false);
364 if (tuple
== NULL
) { break; }
372 //conn->beginTrans(isoLevel, TSYNC);
373 conn
->beginTrans(isoLevel
);