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 <Statement.h>
18 #include <SqlFactory.h>
19 #include <SqlStatement.h>
20 #define SQL_STMT_LEN 1024
27 STMT_TYPE stmtType
= SELECT
;
29 AbsSqlConnection
*conn
;
30 AbsSqlStatement
*stmt
;
32 bool autocommitmode
= true;
33 IsolationLevel isoLevel
= READ_COMMITTED
;
38 printf("Usage: csql [-u username] [-p passwd] [-s sqlfile] \n");
39 printf(" username -> username to connect to database\n");
40 printf(" password -> password to connect to database\n");
41 printf(" sqlfile -> filename containing sql statements\n");
46 int main(int argc
, char **argv
)
48 char username
[IDENTIFIER_LENGTH
];
50 char password
[IDENTIFIER_LENGTH
];
55 while ((c
= getopt(argc
, argv
, "u:p:s:?")) != EOF
)
59 case 'u' : strcpy(username
, argv
[optind
- 1]); break;
60 case 'p' : strcpy(password
, argv
[optind
- 1]); break;
61 case 's' : strcpy(filename
, argv
[optind
- 1]); break;
62 case '?' : { opt
= 1; break; } //print help
63 default: printf("Wrong args\n"); exit(1);
67 //printf("%s %s %s", username, password, filename);
73 if (username
[0] == '\0' )
75 strcpy(username
, "root");
76 strcpy(password
, "manager");
78 bool fileFlag
= false;
79 if (filename
[0] !='\0')
81 fp
= fopen(filename
,"r");
84 printf("Unable to open the file %s\n", filename
);
91 conn
= SqlFactory::createConnection(CSql
);
92 //conn = SqlFactory::createConnection(CSqlGateway);
93 rv
= conn
->connect(username
,password
);
94 if (rv
!= OK
) return 1;
95 stmt
= SqlFactory::createStatement(CSql
);
96 //stmt = SqlFactory::createStatement(CSqlGateway);
97 stmt
->setConnection(conn
);
98 rv
= conn
->beginTrans();
99 if (rv
!= OK
) return 2;
100 while (getInput(fileFlag
) == true) continue;
102 //TODO::conn should provide method telling status of the transaction.
103 //if running, then abort it
105 if (filename
[0] !='\0')
114 bool handleTransaction(char *st
)
116 while (isspace (*st
)|| *st
== '(' ) st
++; // Skip white spaces
117 if (strncasecmp (st
, "COMMIT", 6) == 0 ||
118 strncasecmp (st
, "commit", 6) == 0 )
121 conn
->beginTrans(isoLevel
);
124 else if (strncasecmp (st
, "ROLLBACK", 8) == 0||
125 strncasecmp (st
, "rollback", 8) == 0)
128 conn
->beginTrans(isoLevel
);
131 else if (strncasecmp (st
, "SET AUTOCOMMIT ON", 17) == 0)
133 autocommitmode
= true;
134 printf("AUTOCOMMIT Mode is set to ON\n");
137 else if (strncasecmp (st
, "SET AUTOCOMMIT OFF", 18) == 0)
139 autocommitmode
= false;
140 printf("AUTOCOMMIT Mode is set to OFF\n");
143 else if (strncasecmp (st
, "SET ISOLATION LEVEL UNCOMMITTED", 31) == 0)
145 isoLevel
= READ_UNCOMMITTED
;
146 printf("Isolation Level is set to READ_UNCOMMITTED\n");
149 else if (strncasecmp (st
, "SET ISOLATION LEVEL COMMITTED", 29) == 0)
151 isoLevel
= READ_COMMITTED
;
152 printf("Isolation Level is set to READ_COMMITTED\n");
155 else if (strncasecmp (st
, "SET ISOLATION LEVEL REPEATABLE", 30) == 0)
157 isoLevel
= READ_REPEATABLE
;
158 printf("Isolation Level is set to READ_REPEATABLE\n");
163 bool handleEchoAndComment(char *st
)
165 while (isspace (*st
)|| *st
== '(' ) st
++; // Skip white spaces
166 if (strncasecmp (st
, "ECHO", 4) == 0)
170 }else if (strncmp(st
, "--", 2) == 0)
173 }else if (strncasecmp(st
, "help", 2) == 0)
182 printf("CSQL Command List\n");
183 printf("======================================================\n");
184 printf("SET AUTOCOMMIT ON|OFF\n");
185 printf("SET ISOLATION LEVEL UNCOMMITTED|COMMITTED|REPEATABLE\n");
186 printf("CREATE TABLE|INDEX ...\n");
187 printf("INSERT ...\n");
188 printf("UPDATE ...\n");
189 printf("DELETE ...\n");
190 printf("SELECT ...\n");
191 printf("======================================================\n");
193 void setStmtType(char *st
)
195 if (strncasecmp (st
, "SELECT", 6) == 0) {stmtType
=SELECT
; return; }
196 else if (strncasecmp (st
, "CREATE", 6) == 0) {stmtType
=DDL
; return; }
197 else if (strncasecmp (st
, "DROP", 4) == 0) { stmtType
=DDL
; return; }
202 char getQueryFromStdIn(char *buf
)
204 char c
, *bufBegin
=buf
;
209 while( (c
=(char ) getchar()) != EOF
&& c
!= ';')
211 *buf
++ = c
; charCnt
++;
212 if(c
=='\n') //printf("%1d>",ln++);
215 if( charCnt
== SQL_STMT_LEN
) {
216 printf("SQL Statement length is greater than %d. "
217 "Ignoring the statement.\n", SQL_STMT_LEN
);
227 char getQueryFromFile(char *buf
)
229 char c
, *bufBegin
=buf
;
231 while( (c
=(char ) fgetc(fp
)) != EOF
&& c
!= ';')
233 *buf
++ = c
; charCnt
++;
234 if( charCnt
== SQL_STMT_LEN
) {
235 printf("SQL Statement length is greater than %d. "
236 "Ignoring the statement.\n", SQL_STMT_LEN
);
247 bool getInput(bool fromFile
)
249 char buffer
[SQL_STMT_LEN
+ 1];
252 if (fromFile
== false)
253 eof
= getQueryFromStdIn(buffer
);
255 eof
= getQueryFromFile(buffer
);
258 while(*buf
== ' ' || *buf
== '\t' || *buf
== '\n') buf
++;
259 if (eof
== EOF
|| strncasecmp (buf
, "quit", 4) == 0)
261 if (handleTransaction(buf
)) return true;
262 if (handleEchoAndComment(buf
)) return true;
263 if ( *buf
== ';' ) return true; // Null statement.
267 DbRetVal rv
= stmt
->prepare(buf
);
270 printf("Statement prepare failed with error %d\n", rv
);
274 rv
= stmt
->execute(rows
);
277 printf("Statement execute failed with error %d\n", rv
);
281 if (stmtType
== OTHER
)
283 printf("Statement Executed: Rows Affected = %d\n", rows
);
285 else if (stmtType
== DDL
)
287 printf("Statement Executed\n");
291 FieldInfo
*info
= new FieldInfo();
292 printf("---------------------------------------------------------\n");
294 for (int i
= 0 ; i
< stmt
->noOfProjFields() ; i
++)
296 stmt
->getProjFldInfo(i
, info
);
297 printf("%s\t", info
->fldName
);
299 printf("\n---------------------------------------------------------\n");
305 tuple
= (char*)stmt
->fetchAndPrint();
307 if (tuple
== NULL
) { break; }
315 conn
->beginTrans(isoLevel
);