src/tools/isql.cxx File Reference

#include <CSql.h>
#include <DatabaseManagerImpl.h>
#include <Statement.h>
#include <SqlFactory.h>
#include <SqlStatement.h>

Include dependency graph for isql.cxx:

Go to the source code of this file.

Defines

#define SQL_STMT_LEN   1024

Enumerations

enum  STMT_TYPE { SELECT = 0, DDL, OTHER }

Functions

void printHelp ()
bool getInput (bool)
void printUsage ()
int main (int argc, char **argv)
bool handleTransaction (char *st)
bool handleEchoAndComment (char *st)
void setStmtType (char *st)
char getQueryFromStdIn (char *buf)
char getQueryFromFile (char *buf)

Variables

STMT_TYPE stmtType = SELECT
FILE * fp
AbsSqlConnectionconn
AbsSqlStatementstmt
bool gateway = false
bool silent = false
bool autocommitmode = true
IsolationLevel isoLevel = READ_COMMITTED


Define Documentation

#define SQL_STMT_LEN   1024

Definition at line 21 of file isql.cxx.

Referenced by getInput(), getQueryFromFile(), and getQueryFromStdIn().


Enumeration Type Documentation

enum STMT_TYPE

Enumerator:
SELECT 
DDL 
OTHER 

Definition at line 22 of file isql.cxx.

00023 {
00024     SELECT =0,
00025     DDL ,
00026     OTHER
00027 };


Function Documentation

bool getInput ( bool   ) 

Definition at line 285 of file isql.cxx.

References autocommitmode, AbsSqlStatement::close(), Connection::commit(), conn, DDL, AbsSqlStatement::execute(), AbsSqlStatement::fetchAndPrint(), FieldInfo::fldName, AbsSqlStatement::free(), AbsSqlStatement::getProjFldInfo(), getQueryFromFile(), getQueryFromStdIn(), handleEchoAndComment(), handleTransaction(), isoLevel, AbsSqlStatement::noOfProjFields(), OK, OTHER, AbsSqlStatement::prepare(), setStmtType(), silent, SQL_STMT_LEN, stmt, and stmtType.

Referenced by main().

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 }

Here is the call graph for this function:

Here is the caller graph for this function:

char getQueryFromFile ( char *  buf  ) 

Definition at line 265 of file isql.cxx.

References fp, and SQL_STMT_LEN.

Referenced by getInput().

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 }

Here is the caller graph for this function:

char getQueryFromStdIn ( char *  buf  ) 

Definition at line 240 of file isql.cxx.

References SQL_STMT_LEN.

Referenced by getInput().

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 }

Here is the caller graph for this function:

bool handleEchoAndComment ( char *  st  ) 

Definition at line 174 of file isql.cxx.

References Connection::close(), conn, DatabaseManagerImpl::getAllTableNames(), Connection::getDatabaseManager(), List::getIterator(), ListIterator::hasElement(), Identifier::name, ListIterator::nextElement(), Connection::open(), and printHelp().

Referenced by getInput().

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 }

Here is the call graph for this function:

Here is the caller graph for this function:

bool handleTransaction ( char *  st  ) 

Definition at line 123 of file isql.cxx.

References autocommitmode, Connection::commit(), conn, isoLevel, READ_COMMITTED, READ_REPEATABLE, READ_UNCOMMITTED, Connection::rollback(), and silent.

Referenced by getInput().

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 }

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  argc,
char **  argv 
)

Definition at line 48 of file isql.cxx.

References conn, SqlFactory::createConnection(), SqlFactory::createStatement(), CSql, CSqlGateway, fp, gateway, getInput(), IDENTIFIER_LENGTH, OK, printUsage(), Connection::rollback(), AbsSqlStatement::setConnection(), silent, and stmt.

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 }

Here is the call graph for this function:

void printHelp (  ) 

Definition at line 217 of file isql.cxx.

Referenced by handleEchoAndComment().

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 }

Here is the caller graph for this function:

void printUsage (  ) 

Definition at line 38 of file isql.cxx.

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 }

void setStmtType ( char *  st  ) 

Definition at line 231 of file isql.cxx.

References DDL, OTHER, SELECT, and stmtType.

Referenced by getInput().

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 }

Here is the caller graph for this function:


Variable Documentation

bool autocommitmode = true

Definition at line 34 of file isql.cxx.

Referenced by getInput(), and handleTransaction().

AbsSqlConnection* conn

Definition at line 30 of file isql.cxx.

FILE* fp

Definition at line 29 of file isql.cxx.

Referenced by CacheTableLoader::addToCacheTableFile(), getQueryFromFile(), isCached(), main(), NetworkTable::readNetworkConfig(), CacheTableLoader::recoverAllCachedTables(), and CacheTableLoader::removeFromCacheTableFile().

bool gateway = false

Definition at line 33 of file isql.cxx.

Referenced by main().

IsolationLevel isoLevel = READ_COMMITTED

Definition at line 35 of file isql.cxx.

Referenced by SqlConnection::beginTrans(), getInput(), and handleTransaction().

bool silent = false

Definition at line 33 of file isql.cxx.

Referenced by getInput(), handleTransaction(), and main().

AbsSqlStatement* stmt

Definition at line 31 of file isql.cxx.

Referenced by copyFromOdbc(), SqlFactory::createStatement(), SelStatement::fetchAndPrint(), getInput(), getRecordsFromTargetDb(), StatementFactory::getStatement(), insert(), Java_csql_jdbc_JSqlStatement_alloc(), Java_csql_jdbc_JSqlStatement_close(), Java_csql_jdbc_JSqlStatement_execute(), Java_csql_jdbc_JSqlStatement_free(), Java_csql_jdbc_JSqlStatement_freeStmt(), Java_csql_jdbc_JSqlStatement_next(), Java_csql_jdbc_JSqlStatement_setByte(), Java_csql_jdbc_JSqlStatement_setConnectionPtr(), Java_csql_jdbc_JSqlStatement_setDate(), Java_csql_jdbc_JSqlStatement_setDouble(), Java_csql_jdbc_JSqlStatement_setFloat(), Java_csql_jdbc_JSqlStatement_setInt(), Java_csql_jdbc_JSqlStatement_setLong(), Java_csql_jdbc_JSqlStatement_setShort(), Java_csql_jdbc_JSqlStatement_setString(), Java_csql_jdbc_JSqlStatement_setTime(), Java_csql_jdbc_JSqlStatement_setTimestamp(), main(), SqlGwStatement::prepare(), remove(), SqlGwStatement::setAdapter(), AbsSqlStatement::setInnerStatement(), PacketExecute::unmarshall(), verifyCount(), and verifyPrimKeyFldVal().

STMT_TYPE stmtType = SELECT

Definition at line 28 of file isql.cxx.

Referenced by getInput(), and setStmtType().


Generated on Mon Jun 9 22:42:55 2008 for csql by  doxygen 1.4.7