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 <SqlStatement.h>
18 #define SQL_STMT_LEN 1024
21 bool getInputFromUser();
22 int main(int argc
, char **argv
)
24 char username
[IDENTIFIER_LENGTH
];
26 char password
[IDENTIFIER_LENGTH
];
31 while ((c
= getopt(argc
, argv
, "u:p:s")) != EOF
)
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);
42 printf("%s %s %s", username
, password
, filename
);
43 if (username
[0] == '\0' )
45 strcpy(username
, "praba");
46 strcpy(password
, "manager");
50 conn
= new SqlConnection();
51 conn
->connect("praba", "manager");
52 stmt
= new SqlStatement();
53 stmt
->setConnection(conn
);
56 while (getInputFromUser() == true) continue;
58 //TODO::conn should provide method telling status of the transaction.
59 //if running, then abort it
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 )
75 else if (strncasecmp (st
, "ROLLBACK", 8) == 0||
76 strncasecmp (st
, "rollback", 8) == 0)
84 bool isSelectStmt(char *st
)
86 if (strncasecmp (st
, "SELECT", 6) == 0) return true;
90 char getQuery(char *buf
)
97 while( (c
=(char ) getchar()) != EOF
&& c
!= ';')
108 //while( (c=getchar()) != EOF && c != '\n');
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)
121 if (handleTransaction(buf
)) return true;
123 bool isSelect
= false;
124 if (isSelectStmt(buf
)) isSelect
= true;
126 DbRetVal rv
= stmt
->prepare(buf
);
129 printf("Statement prepare failed with error %d\n", rv
);
133 rv
= stmt
->execute(rows
);
136 printf("Statement execute failed with error %d\n", rv
);
141 printf("Statement Executed: Rows Affected = %d\n", rows
);
145 printf("Currently not supported\n");