src/tools/isql.cxx

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by www.databasecache.com                           *
00003  *   Contact: praba_tuty@databasecache.com                                 *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  ***************************************************************************/
00016 #include<CSql.h>
00017 #include<DatabaseManagerImpl.h>
00018 #include <Statement.h>
00019 #include <SqlFactory.h>
00020 #include <SqlStatement.h>
00021 #define SQL_STMT_LEN 1024
00022 enum STMT_TYPE
00023 {
00024     SELECT =0,
00025     DDL ,
00026     OTHER
00027 };
00028 STMT_TYPE stmtType = SELECT;
00029 FILE *fp;
00030 AbsSqlConnection *conn;
00031 AbsSqlStatement *stmt;
00032 
00033 bool gateway=false, silent=false;
00034 bool autocommitmode = true;
00035 IsolationLevel isoLevel = READ_COMMITTED;
00036 void printHelp();
00037 bool getInput(bool);
00038 void printUsage()
00039 {
00040    printf("Usage: csql [-u username] [-p passwd] [-s sqlfile] \n");
00041    printf("       username -> username to connect to database\n");
00042    printf("       password -> password to connect to database\n");
00043    printf("       sqlfile -> filename containing sql statements\n");
00044    return;
00045   
00046 }
00047 
00048 int main(int argc, char **argv)
00049 {
00050     char username[IDENTIFIER_LENGTH];
00051     username [0] = '\0';
00052     char password[IDENTIFIER_LENGTH];
00053     password [0] = '\0';
00054     char filename[512];
00055     filename [0] ='\0';
00056     int c = 0, opt=0;
00057     while ((c = getopt(argc, argv, "u:p:s:gS?")) != EOF) 
00058     {
00059         switch (c)
00060         {
00061             case 'u' : strcpy(username , argv[optind - 1]); break;
00062             case 'p' : strcpy(password , argv[optind - 1]); break;
00063             case 's' : strcpy(filename , argv[optind - 1]); break;
00064             case '?' : { opt = 1; break; } //print help 
00065             case 'S' : { silent = true; break; } //silent 
00066             case 'g' : { gateway = true; break; } //print help 
00067             default: printf("Wrong args\n"); exit(1);
00068 
00069         }
00070     }//while options
00071     //printf("%s %s %s", username, password, filename);
00072     if (opt == 1)
00073     {
00074         printUsage();
00075         return 0;
00076     }
00077     if (username[0] == '\0' )
00078     {
00079         strcpy(username, "root");
00080         strcpy(password, "manager");
00081     }
00082     bool fileFlag = false;
00083     if (filename [0] !='\0')
00084     {
00085         fp = fopen(filename,"r");
00086         if (fp == NULL)
00087         {
00088             printf("Unable to open the file %s\n", filename);
00089             return 1;
00090         }
00091         fileFlag = true;
00092     }
00093     
00094     DbRetVal rv = OK;
00095     if (gateway)
00096       conn = SqlFactory::createConnection(CSqlGateway);
00097     else
00098       conn = SqlFactory::createConnection(CSql);
00099     rv = conn->connect(username,password);
00100     if (rv != OK) return 1;
00101     if (gateway)
00102       stmt =  SqlFactory::createStatement(CSqlGateway);
00103     else
00104       stmt =  SqlFactory::createStatement(CSql);
00105     stmt->setConnection(conn);
00106     //rv = conn->beginTrans(READ_COMMITTED, TSYNC);
00107     rv = conn->beginTrans();
00108     if (rv != OK) return 2;
00109     while (getInput(fileFlag) == true) continue;
00110 
00111     //TODO::conn should provide method telling status of the transaction.
00112     //if running, then abort it
00113     conn->rollback();
00114     if (filename [0] !='\0')
00115     {
00116        fclose(fp);
00117     }
00118     conn->disconnect();
00119     delete stmt;
00120     delete conn;
00121     return 0;
00122 }
00123 bool handleTransaction(char *st)
00124 {
00125     while (isspace (*st)|| *st == '(' ) st++; // Skip white spaces
00126     if (strncasecmp (st, "COMMIT", 6) == 0 ||
00127         strncasecmp (st, "commit", 6) == 0 )
00128     {
00129         conn->commit();
00130         //conn->beginTrans(isoLevel, TSYNC);
00131         conn->beginTrans(isoLevel);
00132         return true;
00133     }
00134     else if (strncasecmp (st, "ROLLBACK", 8) == 0|| 
00135         strncasecmp (st, "rollback", 8) == 0)
00136     {
00137         conn->rollback();
00138         //conn->beginTrans(isoLevel, TSYNC);
00139         conn->beginTrans(isoLevel);
00140         return true;
00141     }
00142     else if (strncasecmp (st, "SET AUTOCOMMIT ON", 17) == 0)
00143     {
00144         autocommitmode = true;
00145         if (!silent) printf("AUTOCOMMIT Mode is set to ON\n");
00146         return true;
00147     }
00148     else if (strncasecmp (st, "SET AUTOCOMMIT OFF", 18) == 0)
00149     {
00150         autocommitmode = false;
00151         if (!silent) printf("AUTOCOMMIT Mode is set to OFF\n");
00152         return true;
00153     }
00154     else if (strncasecmp (st, "SET ISOLATION LEVEL UNCOMMITTED", 31) == 0)
00155     {
00156         isoLevel = READ_UNCOMMITTED;
00157         printf("Isolation Level is set to READ_UNCOMMITTED\n");
00158         return true;
00159     }
00160     else if (strncasecmp (st, "SET ISOLATION LEVEL COMMITTED", 29) == 0)
00161     {
00162         isoLevel = READ_COMMITTED;
00163         printf("Isolation Level is set to READ_COMMITTED\n");
00164         return true;
00165     }
00166     else if (strncasecmp (st, "SET ISOLATION LEVEL REPEATABLE", 30) == 0)
00167     {
00168         isoLevel = READ_REPEATABLE;
00169         printf("Isolation Level is set to READ_REPEATABLE\n");
00170         return true;
00171     }
00172     return false;
00173 }
00174 bool handleEchoAndComment(char *st)
00175 {
00176     while (isspace (*st)|| *st == '(' ) st++; // Skip white spaces
00177     if (strncasecmp (st, "ECHO", 4) == 0)
00178     {
00179         printf("%s\n", st);
00180         return true;
00181     }else if (strncmp(st, "--", 2) == 0)
00182     {
00183         return true;
00184     }else if (strncasecmp(st, "help", 2) == 0)
00185     {
00186         printHelp();
00187         return true;
00188     }else if (strncasecmp(st, "show tables", 11) == 0)
00189     {
00190       Connection conn;
00191       conn.open("root","manager");
00192       DatabaseManagerImpl *dbMgr =  (DatabaseManagerImpl*)conn.getDatabaseManager();
00193       if (dbMgr == NULL)
00194       {
00195           printf("Unable to connect to csql server\n");
00196           return true;
00197       }
00198       List tableList = dbMgr->getAllTableNames();
00199       ListIterator iter = tableList.getIterator();
00200       Identifier *elem = NULL;
00201       int ret =0;
00202       printf("=============TableNames===================\n");
00203       int count =0;
00204       while (iter.hasElement())
00205       {
00206           elem = (Identifier*) iter.nextElement();
00207           count++;
00208           printf("   %s \n", elem->name);
00209       }
00210       if (count ==0) printf("  No tables exist\n");
00211       printf("=========================================\n");
00212       conn.close();
00213       return true;
00214     }
00215     return false;
00216 }
00217 void printHelp()
00218 {
00219     printf("CSQL Command List\n");
00220     printf("======================================================\n");
00221     printf("SHOW TABLES\n");
00222     printf("SET AUTOCOMMIT ON|OFF\n");
00223     printf("SET ISOLATION LEVEL UNCOMMITTED|COMMITTED|REPEATABLE\n");
00224     printf("CREATE TABLE|INDEX ...\n");
00225     printf("INSERT ...\n");
00226     printf("UPDATE ...\n");
00227     printf("DELETE ...\n");
00228     printf("SELECT ...\n");
00229     printf("======================================================\n");
00230 }
00231 void setStmtType(char *st)
00232 {
00233    if (strncasecmp (st, "SELECT", 6) == 0)  {stmtType=SELECT; return; }
00234    else if (strncasecmp (st, "CREATE", 6) == 0) {stmtType=DDL; return; }
00235    else if (strncasecmp (st, "DROP", 4) == 0) { stmtType=DDL; return; }
00236    stmtType = OTHER;
00237    return ;
00238 }
00239 
00240 char getQueryFromStdIn(char *buf)
00241 {
00242     char    c, *bufBegin=buf;
00243     int    ln, charCnt=0;
00244 
00245     ln=1;
00246     printf("CSQL>");
00247     while( (c=(char ) getchar()) != EOF && c != ';')
00248     {
00249         *buf++ = c; charCnt++;
00250         if(c=='\n') //printf("%1d>",ln++);
00251             ln++;
00252 
00253         if( charCnt == SQL_STMT_LEN ) {
00254             printf("SQL Statement length is greater than %d. "
00255             "Ignoring the statement.\n", SQL_STMT_LEN );
00256             *bufBegin++ =';';
00257             *bufBegin ='\0';
00258             return 0;
00259         }
00260     }
00261     *buf++ = ';';
00262     *buf = '\0';
00263     return c;
00264 }
00265 char getQueryFromFile(char *buf)
00266 {
00267     char    c, *bufBegin=buf;
00268     int charCnt=0;
00269     while( (c=(char ) fgetc(fp)) != EOF && c != ';')
00270     {
00271         *buf++ = c; charCnt++; 
00272         if( charCnt == SQL_STMT_LEN ) {
00273             printf("SQL Statement length is greater than %d. "
00274             "Ignoring the statement.\n", SQL_STMT_LEN );
00275             *bufBegin++ =';';
00276             *bufBegin ='\0';
00277             return 0;
00278         }
00279     }
00280     *buf++ = ';';
00281     *buf = '\0';
00282     return c;
00283 }
00284 
00285 bool getInput(bool fromFile)
00286 {
00287     char buffer [SQL_STMT_LEN + 1];
00288 
00289     char eof;
00290     if (fromFile == false)
00291         eof = getQueryFromStdIn(buffer);
00292     else
00293         eof = getQueryFromFile(buffer);
00294                      
00295     char *buf = buffer;
00296     while(*buf == ' ' || *buf == '\t' || *buf == '\n') buf++;
00297     if (eof == EOF || strncasecmp (buf, "quit", 4) == 0)
00298         return false;
00299     if (handleTransaction(buf)) return true;
00300     if (handleEchoAndComment(buf)) return true;
00301     if ( *buf == ';' ) return true; // Null statement.
00302     
00303     setStmtType(buf);
00304 
00305     DbRetVal rv = stmt->prepare(buf);
00306     if (rv != OK) 
00307     {
00308         printf("Statement prepare failed with error %d\n", rv); 
00309         return true; 
00310     }
00311     int rows =0;
00312     rv = stmt->execute(rows);
00313     if (rv != OK) 
00314     {
00315         printf("Statement execute failed with error %d\n", rv);
00316         stmt->free();
00317         return true; 
00318     }
00319     if (stmtType == OTHER)
00320     {
00321         if (!silent) printf("Statement Executed: Rows Affected = %d\n", rows);
00322     }
00323     else if (stmtType == DDL)
00324     {
00325         if (!silent) printf("Statement Executed\n");
00326     }
00327     else
00328     {
00329         FieldInfo *info = new FieldInfo();
00330         printf("---------------------------------------------------------\n");
00331         printf("\t");
00332         for (int i = 0 ; i < stmt->noOfProjFields() ; i++)
00333         {
00334             stmt->getProjFldInfo(i, info);
00335             printf("%s\t", info->fldName);
00336         }
00337         printf("\n---------------------------------------------------------\n");
00338         delete info;
00339         void *tuple = NULL;
00340         while(true)
00341         {
00342             printf("\t");
00343             tuple = (char*)stmt->fetchAndPrint(false);
00344             printf("\n");
00345             if (tuple == NULL) { break; }
00346         }
00347         stmt->close();
00348     }
00349     stmt->free();
00350     if (autocommitmode)
00351     {
00352         conn->commit();
00353         //conn->beginTrans(isoLevel, TSYNC);
00354         conn->beginTrans(isoLevel);
00355         return true;
00356     }
00357     return true;
00358 }

Generated on Mon Jun 9 22:37:15 2008 for csql by  doxygen 1.4.7