Handling comments -- from isql intrepreter
[csql.git] / src / tools / isql.cxx
blobf2a0f54e059cd7fdf483242f4ecff64295350e51
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
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. *
9 * *
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. *
14 * *
15 ***************************************************************************/
16 #include<CSql.h>
17 #include <Statement.h>
18 #include <SqlStatement.h>
19 #define SQL_STMT_LEN 1024
20 enum STMT_TYPE
22 SELECT =0,
23 DDL ,
24 OTHER
26 STMT_TYPE stmtType = SELECT;
27 FILE *fp;
28 SqlConnection *conn;
29 SqlStatement *stmt;
30 bool getInput(bool);
31 int main(int argc, char **argv)
33 char username[IDENTIFIER_LENGTH];
34 username [0] = '\0';
35 char password[IDENTIFIER_LENGTH];
36 password [0] = '\0';
37 char filename[512];
38 filename [0] ='\0';
39 int c = 0;
40 while ((c = getopt(argc, argv, "u:p:s")) != EOF)
42 switch (c)
44 case 'u' : strcpy(username , argv[optind - 1]); break;
45 case 'p' : strcpy(password , argv[optind - 1]); break;
46 case 's' : strcpy(filename , argv[optind ]); break;
47 default: printf("Wrong args\n"); exit(1);
50 }//while options
51 //printf("%s %s %s", username, password, filename);
52 if (username[0] == '\0' )
54 strcpy(username, "root");
55 strcpy(password, "manager");
57 bool fileFlag = false;
58 if (filename [0] !='\0')
60 fp = fopen(filename,"r");
61 if (fp == NULL)
63 printf("Unable to open the file %s\n", filename);
64 return 1;
66 fileFlag = true;
69 DbRetVal rv = OK;
70 conn = new SqlConnection();
71 rv = conn->connect("root", "manager");
72 if (rv != OK) return 1;
73 stmt = new SqlStatement();
74 stmt->setConnection(conn);
75 rv = conn->beginTrans();
76 if (rv != OK) return 2;
78 while (getInput(fileFlag) == true) continue;
80 //TODO::conn should provide method telling status of the transaction.
81 //if running, then abort it
82 conn->rollback();
83 if (filename [0] !='\0')
85 fclose(fp);
87 delete stmt;
88 delete conn;
89 return 0;
91 bool handleTransaction(char *st)
93 while (isspace (*st)|| *st == '(' ) st++; // Skip white spaces
94 if (strncasecmp (st, "COMMIT", 6) == 0 ||
95 strncasecmp (st, "commit", 6) == 0 )
97 conn->commit();
98 conn->beginTrans();
99 return true;
101 else if (strncasecmp (st, "ROLLBACK", 8) == 0||
102 strncasecmp (st, "rollback", 8) == 0)
104 conn->rollback();
105 conn->beginTrans();
106 return true;
108 return false;
110 bool handleEchoAndComment(char *st)
112 while (isspace (*st)|| *st == '(' ) st++; // Skip white spaces
113 if (strncasecmp (st, "ECHO", 4) == 0 ||
114 strncasecmp (st, "echo", 4) == 0 )
116 printf("%s\n", st);
117 return true;
118 }else if (strncmp(st, "--", 2) == 0)
120 return true;
122 return false;
124 void setStmtType(char *st)
126 if (strncasecmp (st, "SELECT", 6) == 0) {stmtType=SELECT; return; }
127 else if (strncasecmp (st, "CREATE", 6) == 0) {stmtType=DDL; return; }
128 else if (strncasecmp (st, "DROP", 4) == 0) { stmtType=DDL; return; }
129 stmtType = OTHER;
130 return ;
133 char getQueryFromStdIn(char *buf)
135 char c;
136 int ln;
138 ln=1;
139 printf("CSQL>");
140 while( (c=(char ) getchar()) != EOF && c != ';')
142 *buf++ = c;
143 if(c=='\n')
144 //printf("%1d>",ln++);
145 ln++;
147 *buf++ = ';';
148 *buf = '\0';
149 return c;
151 char getQueryFromFile(char *buf)
153 char c;
154 while( (c=(char ) fgetc(fp)) != EOF && c != ';')
156 *buf++ = c;
158 *buf++ = ';';
159 *buf = '\0';
160 return c;
163 bool getInput(bool fromFile)
165 char buffer [SQL_STMT_LEN + 1];
167 char eof;
168 if (fromFile == false)
169 eof = getQueryFromStdIn(buffer);
170 else
171 eof = getQueryFromFile(buffer);
173 char *buf = buffer;
174 while(*buf == ' ' || *buf == '\t' || *buf == '\n') buf++;
175 if (eof == EOF || strncasecmp (buf, "quit", 4) == 0)
176 return false;
177 if (handleTransaction(buf)) return true;
178 if (handleEchoAndComment(buf)) return true;
180 setStmtType(buf);
182 DbRetVal rv = stmt->prepare(buf);
183 if (rv != OK)
185 printf("Statement prepare failed with error %d\n", rv);
186 return true;
188 int rows =0;
189 rv = stmt->execute(rows);
190 if (rv != OK)
192 printf("Statement execute failed with error %d\n", rv);
193 return true;
195 if (stmtType == OTHER)
197 printf("Statement Executed: Rows Affected = %d\n", rows);
199 else if (stmtType == DDL)
201 printf("Statement Executed\n");
203 else
205 FieldInfo *info = new FieldInfo();
206 printf("---------------------------------------------------------\n");
207 printf("\t");
208 for (int i = 0 ; i < stmt->noOfProjFields() ; i++)
210 stmt->getProjFldInfo(i, info);
211 printf("%s\t", info->fldName);
213 printf("\n---------------------------------------------------------\n");
214 delete info;
215 void *tuple = NULL;
216 while(true)
218 printf("\t");
219 tuple = (char*)stmt->fetchAndPrint();
220 printf("\n");
221 if (tuple == NULL) { break; }
224 stmt->free();
225 return true;