First cut tiny sql engine changes
[csql.git] / src / tools / isql.c
blob7edc5c963d68340ce763fec8bd7a6f06e758a59d
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 <SqlStatement.h>
18 #define SQL_STMT_LEN 1024
19 SqlConnection *conn;
20 SqlStatement *stmt;
21 bool getInputFromUser();
22 int main(int argc, char **argv)
24 char username[IDENTIFIER_LENGTH];
25 username [0] = '\0';
26 char password[IDENTIFIER_LENGTH];
27 password [0] = '\0';
28 char filename[512];
29 filename [0] ='\0';
30 int c = 0;
31 while ((c = getopt(argc, argv, "u:p:s")) != EOF)
33 switch (c)
35 case 'u' : strcpy(username , argv[optind - 1]); break;
36 case 'p' : strcpy(password , argv[optind - 1]); break;
37 case 's' : strcpy(filename , argv[optind ]); break;
38 default: printf("Wrong args\n"); exit(1);
41 }//while options
42 printf("%s %s %s", username, password, filename);
43 if (username[0] == '\0' )
45 strcpy(username, "praba");
46 strcpy(password, "manager");
49 DbRetVal rv = OK;
50 conn = new SqlConnection();
51 conn->connect("praba", "manager");
52 stmt = new SqlStatement();
53 stmt->setConnection(conn);
54 conn->beginTrans();
56 while (getInputFromUser() == true) continue;
58 //TODO::conn should provide method telling status of the transaction.
59 //if running, then abort it
60 conn->rollback();
61 delete stmt;
62 delete conn;
63 return 0;
65 bool handleTransaction(char *st)
67 while (isspace (*st)|| *st == '(' ) st++; // Skip white spaces
68 if (strncasecmp (st, "COMMIT", 6) == 0 ||
69 strncasecmp (st, "commit", 6) == 0 )
71 conn->commit();
72 conn->beginTrans();
73 return true;
75 else if (strncasecmp (st, "ROLLBACK", 8) == 0||
76 strncasecmp (st, "rollback", 8) == 0)
78 conn->rollback();
79 conn->beginTrans();
80 return true;
82 return false;
84 bool isSelectStmt(char *st)
86 if (strncasecmp (st, "SELECT", 6) == 0) return true;
87 return false;
90 char getQuery(char *buf)
92 char c;
93 int ln;
95 ln=1;
96 printf("CSQL>");
97 while( (c=(char ) getchar()) != EOF && c != ';')
99 *buf++ = c;
100 if(c=='\n')
101 printf("%1d>",ln++);
103 *buf++ = ';';
104 *buf = '\0';
105 if(c==EOF)
106 return c;
108 //while( (c=getchar()) != EOF && c != '\n');
110 return c;
113 bool getInputFromUser()
115 char buf [SQL_STMT_LEN + 1];
117 char eof = getQuery(buf);
118 printf("PRABA== %s\n", buf);
119 if (eof == EOF || strncasecmp (buf, "quit", 4) == 0)
120 return false;
121 if (handleTransaction(buf)) return true;
123 bool isSelect = false;
124 if (isSelectStmt(buf)) isSelect = true;
126 DbRetVal rv = stmt->prepare(buf);
127 if (rv != OK)
129 printf("Statement prepare failed with error %d\n", rv);
130 return true;
132 int rows =0;
133 rv = stmt->execute(rows);
134 if (rv != OK)
136 printf("Statement execute failed with error %d\n", rv);
137 return true;
139 if (! isSelect)
141 printf("Statement Executed: Rows Affected = %d\n", rows);
143 else
145 printf("Currently not supported\n");
147 stmt->free();
148 return true;