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 <SqlStatement.h>
19 #define SQL_STMT_LEN 1024
26 STMT_TYPE stmtType
= SELECT
;
33 printf("Usage: csql [-u username] [-p passwd] [-s sqlfile] \n");
34 printf(" username -> username to connect to database\n");
35 printf(" password -> password to connect to database\n");
36 printf(" sqlfile -> filename containing sql statements\n");
41 int main(int argc
, char **argv
)
43 char username
[IDENTIFIER_LENGTH
];
45 char password
[IDENTIFIER_LENGTH
];
50 while ((c
= getopt(argc
, argv
, "u:p:s:?")) != EOF
)
54 case 'u' : strcpy(username
, argv
[optind
- 1]); break;
55 case 'p' : strcpy(password
, argv
[optind
- 1]); break;
56 case 's' : strcpy(filename
, argv
[optind
- 1]); break;
57 case '?' : { opt
= 1; break; } //print help
58 default: printf("Wrong args\n"); exit(1);
62 //printf("%s %s %s", username, password, filename);
68 if (username
[0] == '\0' )
70 strcpy(username
, "root");
71 strcpy(password
, "manager");
73 bool fileFlag
= false;
74 if (filename
[0] !='\0')
76 fp
= fopen(filename
,"r");
79 printf("Unable to open the file %s\n", filename
);
86 conn
= new SqlConnection();
87 rv
= conn
->connect(username
,password
);
88 if (rv
!= OK
) return 1;
89 stmt
= new SqlStatement();
90 stmt
->setConnection(conn
);
91 rv
= conn
->beginTrans();
92 if (rv
!= OK
) return 2;
94 while (getInput(fileFlag
) == true) continue;
96 //TODO::conn should provide method telling status of the transaction.
97 //if running, then abort it
99 if (filename
[0] !='\0')
107 bool handleTransaction(char *st
)
109 while (isspace (*st
)|| *st
== '(' ) st
++; // Skip white spaces
110 if (strncasecmp (st
, "COMMIT", 6) == 0 ||
111 strncasecmp (st
, "commit", 6) == 0 )
117 else if (strncasecmp (st
, "ROLLBACK", 8) == 0||
118 strncasecmp (st
, "rollback", 8) == 0)
126 bool handleEchoAndComment(char *st
)
128 while (isspace (*st
)|| *st
== '(' ) st
++; // Skip white spaces
129 if (strncasecmp (st
, "ECHO", 4) == 0 ||
130 strncasecmp (st
, "echo", 4) == 0 )
134 }else if (strncmp(st
, "--", 2) == 0)
140 void setStmtType(char *st
)
142 if (strncasecmp (st
, "SELECT", 6) == 0) {stmtType
=SELECT
; return; }
143 else if (strncasecmp (st
, "CREATE", 6) == 0) {stmtType
=DDL
; return; }
144 else if (strncasecmp (st
, "DROP", 4) == 0) { stmtType
=DDL
; return; }
149 char getQueryFromStdIn(char *buf
)
156 while( (c
=(char ) getchar()) != EOF
&& c
!= ';')
160 //printf("%1d>",ln++);
167 char getQueryFromFile(char *buf
)
170 while( (c
=(char ) fgetc(fp
)) != EOF
&& c
!= ';')
179 bool getInput(bool fromFile
)
181 char buffer
[SQL_STMT_LEN
+ 1];
184 if (fromFile
== false)
185 eof
= getQueryFromStdIn(buffer
);
187 eof
= getQueryFromFile(buffer
);
190 while(*buf
== ' ' || *buf
== '\t' || *buf
== '\n') buf
++;
191 if (eof
== EOF
|| strncasecmp (buf
, "quit", 4) == 0)
193 if (handleTransaction(buf
)) return true;
194 if (handleEchoAndComment(buf
)) return true;
198 DbRetVal rv
= stmt
->prepare(buf
);
201 printf("Statement prepare failed with error %d\n", rv
);
205 rv
= stmt
->execute(rows
);
208 printf("Statement execute failed with error %d\n", rv
);
212 if (stmtType
== OTHER
)
214 printf("Statement Executed: Rows Affected = %d\n", rows
);
216 else if (stmtType
== DDL
)
218 printf("Statement Executed\n");
222 FieldInfo
*info
= new FieldInfo();
223 printf("---------------------------------------------------------\n");
225 for (int i
= 0 ; i
< stmt
->noOfProjFields() ; i
++)
227 stmt
->getProjFldInfo(i
, info
);
228 printf("%s\t", info
->fldName
);
230 printf("\n---------------------------------------------------------\n");
236 tuple
= (char*)stmt
->fetchAndPrint();
238 if (tuple
== NULL
) { break; }