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 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 )
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; }
130 char getQueryFromStdIn(char *buf
)
137 while( (c
=(char ) getchar()) != EOF
&& c
!= ';')
141 //printf("%1d>",ln++);
148 char getQueryFromFile(char *buf
)
151 while( (c
=(char ) fgetc(fp
)) != EOF
&& c
!= ';')
160 bool getInput(bool fromFile
)
162 char buffer
[SQL_STMT_LEN
+ 1];
165 if (fromFile
== false)
166 eof
= getQueryFromStdIn(buffer
);
168 eof
= getQueryFromFile(buffer
);
171 while(*buf
== ' ' || *buf
== '\t' || *buf
== '\n') buf
++;
172 if (eof
== EOF
|| strncasecmp (buf
, "quit", 4) == 0)
174 if (handleTransaction(buf
)) return true;
175 if (handleEcho(buf
)) return true;
179 DbRetVal rv
= stmt
->prepare(buf
);
182 printf("Statement prepare failed with error %d\n", rv
);
186 rv
= stmt
->execute(rows
);
189 printf("Statement execute failed with error %d\n", rv
);
192 if (stmtType
== OTHER
)
194 printf("Statement Executed: Rows Affected = %d\n", rows
);
196 else if (stmtType
== DDL
)
198 printf("Statement Executed\n");
202 FieldInfo
*info
= new FieldInfo();
203 printf("---------------------------------------------------------\n");
205 for (int i
= 0 ; i
< stmt
->noOfProjFields() ; i
++)
207 stmt
->getProjFldInfo(i
, info
);
208 printf("%s\t", info
->fldName
);
210 printf("\n---------------------------------------------------------\n");
216 tuple
= (char*)stmt
->fetchAndPrint();
218 if (tuple
== NULL
) { break; }