1847432 : csql tool does not accept file name
[csql.git] / src / tools / isql.cxx
blob4d5c1334e6757143c11066d1857195a17837aedf
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 handleEcho(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;
119 return false;
121 void setStmtType(char *st)
123 if (strncasecmp (st, "SELECT", 6) == 0) {stmtType=SELECT; return; }
124 else if (strncasecmp (st, "CREATE", 6) == 0) {stmtType=DDL; return; }
125 else if (strncasecmp (st, "DROP", 4) == 0) { stmtType=DDL; return; }
126 stmtType = OTHER;
127 return ;
130 char getQueryFromStdIn(char *buf)
132 char c;
133 int ln;
135 ln=1;
136 printf("CSQL>");
137 while( (c=(char ) getchar()) != EOF && c != ';')
139 *buf++ = c;
140 if(c=='\n')
141 //printf("%1d>",ln++);
142 ln++;
144 *buf++ = ';';
145 *buf = '\0';
146 return c;
148 char getQueryFromFile(char *buf)
150 char c;
151 while( (c=(char ) fgetc(fp)) != EOF && c != ';')
153 *buf++ = c;
155 *buf++ = ';';
156 *buf = '\0';
157 return c;
160 bool getInput(bool fromFile)
162 char buffer [SQL_STMT_LEN + 1];
164 char eof;
165 if (fromFile == false)
166 eof = getQueryFromStdIn(buffer);
167 else
168 eof = getQueryFromFile(buffer);
170 char *buf = buffer;
171 while(*buf == ' ' || *buf == '\t' || *buf == '\n') buf++;
172 if (eof == EOF || strncasecmp (buf, "quit", 4) == 0)
173 return false;
174 if (handleTransaction(buf)) return true;
175 if (handleEcho(buf)) return true;
177 setStmtType(buf);
179 DbRetVal rv = stmt->prepare(buf);
180 if (rv != OK)
182 printf("Statement prepare failed with error %d\n", rv);
183 return true;
185 int rows =0;
186 rv = stmt->execute(rows);
187 if (rv != OK)
189 printf("Statement execute failed with error %d\n", rv);
190 return true;
192 if (stmtType == OTHER)
194 printf("Statement Executed: Rows Affected = %d\n", rows);
196 else if (stmtType == DDL)
198 printf("Statement Executed\n");
200 else
202 FieldInfo *info = new FieldInfo();
203 printf("---------------------------------------------------------\n");
204 printf("\t");
205 for (int i = 0 ; i < stmt->noOfProjFields() ; i++)
207 stmt->getProjFldInfo(i, info);
208 printf("%s\t", info->fldName);
210 printf("\n---------------------------------------------------------\n");
211 delete info;
212 void *tuple = NULL;
213 while(true)
215 printf("\t");
216 tuple = (char*)stmt->fetchAndPrint();
217 printf("\n");
218 if (tuple == NULL) { break; }
221 stmt->free();
222 return true;