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
;
31 int main(int argc
, char **argv
)
33 char username
[IDENTIFIER_LENGTH
];
35 char password
[IDENTIFIER_LENGTH
];
40 while ((c
= getopt(argc
, argv
, "u:p:s")) != EOF
)
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);
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");
63 printf("Unable to open the file %s\n", filename
);
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
83 if (filename
[0] !='\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 )
101 else if (strncasecmp (st
, "ROLLBACK", 8) == 0||
102 strncasecmp (st
, "rollback", 8) == 0)
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 )
118 }else if (strncmp(st
, "--", 2) == 0)
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; }
133 char getQueryFromStdIn(char *buf
)
140 while( (c
=(char ) getchar()) != EOF
&& c
!= ';')
144 //printf("%1d>",ln++);
151 char getQueryFromFile(char *buf
)
154 while( (c
=(char ) fgetc(fp
)) != EOF
&& c
!= ';')
163 bool getInput(bool fromFile
)
165 char buffer
[SQL_STMT_LEN
+ 1];
168 if (fromFile
== false)
169 eof
= getQueryFromStdIn(buffer
);
171 eof
= getQueryFromFile(buffer
);
174 while(*buf
== ' ' || *buf
== '\t' || *buf
== '\n') buf
++;
175 if (eof
== EOF
|| strncasecmp (buf
, "quit", 4) == 0)
177 if (handleTransaction(buf
)) return true;
178 if (handleEchoAndComment(buf
)) return true;
182 DbRetVal rv
= stmt
->prepare(buf
);
185 printf("Statement prepare failed with error %d\n", rv
);
189 rv
= stmt
->execute(rows
);
192 printf("Statement execute failed with error %d\n", rv
);
195 if (stmtType
== OTHER
)
197 printf("Statement Executed: Rows Affected = %d\n", rows
);
199 else if (stmtType
== DDL
)
201 printf("Statement Executed\n");
205 FieldInfo
*info
= new FieldInfo();
206 printf("---------------------------------------------------------\n");
208 for (int i
= 0 ; i
< stmt
->noOfProjFields() ; i
++)
210 stmt
->getProjFldInfo(i
, info
);
211 printf("%s\t", info
->fldName
);
213 printf("\n---------------------------------------------------------\n");
219 tuple
= (char*)stmt
->fetchAndPrint();
221 if (tuple
== NULL
) { break; }