src/tools/csqldump.cxx File Reference

#include <CSql.h>
#include <DatabaseManagerImpl.h>
#include <Database.h>
#include <TableImpl.h>
#include <SqlFactory.h>
#include <SqlStatement.h>

Include dependency graph for csqldump.cxx:

Go to the source code of this file.

Functions

void printUsage ()
bool isCached (char *tblName)
int main (int argc, char **argv)


Function Documentation

bool isCached ( char *  tblName  ) 

Definition at line 32 of file csqldump.cxx.

References Conf::config, ErrSysInit, fp, IDENTIFIER_LENGTH, and printError.

Referenced by main().

00033 {
00034     FILE *fp = fopen(Conf::config.getTableConfigFile(),"r");
00035     if( fp == NULL ) {
00036         printError(ErrSysInit, "csqltable.conf file does not exist");
00037         return ErrSysInit;
00038     }
00039     char ctablename[IDENTIFIER_LENGTH];
00040     int mode;
00041     bool isCached=false;
00042     while(!feof(fp))
00043     {
00044         fscanf(fp, "%d:%s\n", &mode, ctablename);
00045         if (strcmp (ctablename, tblName) == 0) { isCached=true; break; }
00046     }
00047     fclose(fp);
00048     return isCached;
00049 }

Here is the caller graph for this function:

int main ( int  argc,
char **  argv 
)

Definition at line 50 of file csqldump.cxx.

References SqlConnection::beginTrans(), Connection::close(), AbsSqlStatement::close(), DatabaseManagerImpl::closeTable(), SqlConnection::commit(), conn, SqlConnection::connect(), SqlFactory::createConnection(), SqlFactory::createStatement(), CSql, FieldInfo::defaultValueBuf, SqlConnection::disconnect(), AbsSqlStatement::execute(), AbsSqlStatement::fetchAndPrint(), AbsSqlStatement::free(), DatabaseManagerImpl::getAllTableNames(), Connection::getDatabaseManager(), Table::getFieldInfo(), Table::getFieldNameList(), List::getIterator(), AllDataType::getSQLString(), ListIterator::hasElement(), IDENTIFIER_LENGTH, isCached(), FieldInfo::isDefault, FieldInfo::isNull, FieldInfo::length, Identifier::name, ListIterator::nextElement(), OK, Connection::open(), DatabaseManagerImpl::openTable(), AbsSqlStatement::prepare(), Table::printSQLIndexString(), printUsage(), ListIterator::reset(), AbsSqlStatement::setConnection(), stmt, FieldInfo::type, and typeString.

00051 {
00052     char username[IDENTIFIER_LENGTH];
00053     username [0] = '\0';
00054     char password[IDENTIFIER_LENGTH];
00055     password [0] = '\0';
00056     char tblName[IDENTIFIER_LENGTH];
00057     int c = 0, opt = 0;
00058     int noOfStmts =100;
00059     char name[IDENTIFIER_LENGTH];
00060     while ((c = getopt(argc, argv, "u:p:n:T:?")) != EOF) 
00061     {
00062         switch (c)
00063         {
00064             case 'u' : { strcpy(username, argv[optind - 1]); opt=1; break; }
00065             case 'p' : { strcpy(password, argv[optind - 1]); opt=1; break; }
00066             case 'n' : { noOfStmts = atoi(argv[optind - 1]); opt = 5; break; }
00067             case 'T' : { strcpy(tblName,  argv[optind - 1]); opt = 15; break; }
00068             case '?' : { opt = 10; break; } //print help 
00069             default: opt=1; //list all the tables
00070 
00071         }
00072     }//while options
00073     if (opt == 10) {
00074         printUsage();
00075         return 0;
00076     }
00077 
00078     //printf("%s %s \n", username, password);
00079     if (username[0] == '\0' )
00080     {
00081         strcpy(username, "root");
00082         strcpy(password, "manager");
00083     }
00084     SqlConnection *sqlconn = (SqlConnection*) SqlFactory::createConnection(CSql);
00085     sqlconn->connect("root", "manager");
00086     SqlStatement *stmt = (SqlStatement*) SqlFactory::createStatement(CSql);
00087     stmt->setConnection(sqlconn);
00088     
00089     Connection conn;
00090     DbRetVal rv = conn.open(username, password);
00091     if (rv != OK) return 1;
00092     DatabaseManagerImpl *dbMgr = (DatabaseManagerImpl*) conn.getDatabaseManager();
00093     
00094     if (dbMgr == NULL) { printf("Auth failed\n"); return 2;}
00095     if (opt == 0 || opt == 1) opt = 5;
00096     if (opt == 5) {
00097         List tableList = dbMgr->getAllTableNames();
00098         ListIterator iter = tableList.getIterator();
00099         Identifier *elem = NULL;
00100         int count =0;
00101         while (iter.hasElement())
00102         {
00103             elem = (Identifier*) iter.nextElement();
00104             if (isCached(elem->name)) continue;
00105             printf("CREATE TABLE %s (", elem->name);
00106             Table *table = dbMgr->openTable(elem->name);
00107                 FieldInfo *info = new FieldInfo();
00108             List fNameList = table->getFieldNameList();
00109             ListIterator fNameIter = fNameList.getIterator();
00110             count++;
00111             bool firstField=true;
00112             while (fNameIter.hasElement()) {
00113                  elem = (Identifier*) fNameIter.nextElement();
00114                  table->getFieldInfo((const char*)elem->name, info);
00115                  if (firstField) {
00116                      printf("%s %s ", elem->name, AllDataType::getSQLString(info->type));
00117                      firstField = false;
00118                  }
00119                  else
00120                      printf(", %s %s ", elem->name, AllDataType::getSQLString(info->type));
00121                  if (info->type == typeString) printf("(%d)",info->length -1);
00122                  if (info->isNull) printf(" NOT NULL ");
00123                  if (info->isDefault) printf(" DEFAULT '%s' ", info->defaultValueBuf);
00124             }
00125             printf(");\n");
00126             table->printSQLIndexString();
00127             delete info;
00128             dbMgr->closeTable(table);
00129        }
00130        iter.reset();
00131        char sqlstring[1024];
00132        bool flag=false;
00133        while (iter.hasElement()) {
00134            elem = (Identifier*) iter.nextElement();
00135            if (isCached(elem->name)) continue;
00136            if (!flag) { printf("SET AUTOCOMMIT OFF;\n"); flag=true; } 
00137            sprintf(sqlstring, "SELECT * FROM %s;", elem->name);
00138            sqlconn->beginTrans();
00139            DbRetVal rv = stmt->prepare(sqlstring);
00140            int rows = 0;
00141            rv = stmt->execute(rows);
00142            void *tuple = NULL;
00143            rows = 0;
00144            while(true) {
00145                tuple = stmt->fetchAndPrint(true);
00146                if (tuple == NULL) break;
00147                rows++;
00148                if (rows % noOfStmts ==0) {
00149                    sqlconn->commit();
00150                    sqlconn->beginTrans();
00151                    printf("COMMIT;\n");
00152                }
00153            }
00154            if (rows % noOfStmts !=0) { sqlconn->commit(); printf("COMMIT;\n"); }
00155            stmt->close();
00156            stmt->free();
00157        }   
00158        conn.close();
00159        sqlconn->disconnect();
00160        delete sqlconn;
00161        delete stmt;
00162        return 0;
00163     } 
00164     if (opt == 15) {
00165         Table *table = dbMgr->openTable(tblName);
00166         if (table == NULL) {
00167             printf("csqldump: Table \'%s\' does not exist\n", tblName);
00168             conn.close();
00169             sqlconn->disconnect();
00170             delete sqlconn;
00171             delete stmt;
00172             return 3;
00173         }
00174         printf("CREATE TABLE %s (", tblName);
00175             FieldInfo *info = new FieldInfo();
00176         List fNameList = table->getFieldNameList();
00177         ListIterator fNameIter = fNameList.getIterator();
00178         bool firstField=true;
00179         Identifier *elem = NULL;
00180         while (fNameIter.hasElement()) {
00181             elem = (Identifier*) fNameIter.nextElement();
00182             table->getFieldInfo((const char*)elem->name, info);
00183             if (firstField) {
00184                 printf("%s %s ", elem->name, AllDataType::getSQLString(info->type));
00185                 firstField = false;
00186             }
00187             else
00188                 printf(", %s %s ", elem->name, AllDataType::getSQLString(info->type));
00189             if (info->type == typeString) printf("(%d)",info->length -1);
00190             if (info->isNull) printf(" NOT NULL ");
00191             if (info->isDefault) printf(" DEFAULT '%s' ", info->defaultValueBuf);
00192         }
00193         printf(");\n");
00194         table->printSQLIndexString();
00195         delete info;
00196         char sqlstring[1024];
00197         bool flag=false;
00198         if (!flag) { printf("SET AUTOCOMMIT OFF;\n"); flag=true; } 
00199         sprintf(sqlstring, "SELECT * FROM %s;", tblName);
00200         sqlconn->beginTrans();
00201         DbRetVal rv = stmt->prepare(sqlstring);
00202         int rows = 0;
00203         rv = stmt->execute(rows);
00204         void *tuple = NULL;
00205         rows = 0;
00206         while(true) {
00207             tuple = stmt->fetchAndPrint(true);
00208             if (tuple == NULL) break;
00209             rows++;
00210             if (rows % noOfStmts ==0) {
00211                 sqlconn->commit();
00212                 sqlconn->beginTrans();
00213                 printf("COMMIT;\n");
00214             }
00215         }       
00216         if (rows % noOfStmts !=0) { sqlconn->commit(); printf("COMMIT;\n"); }
00217         stmt->close();
00218         stmt->free();
00219     }   
00220     conn.close();
00221     sqlconn->disconnect();
00222     delete sqlconn;
00223     delete stmt;
00224     
00225     return 0;
00226 }

Here is the call graph for this function:

void printUsage (  ) 

Definition at line 22 of file csqldump.cxx.

00023 {
00024    printf("Usage: csqldump [-u username] [-p passwd] [-n noOfStmtsPerCommit] [-T tableName]\n");
00025    printf("       n -> number of statements per commit\n");
00026    printf("            Default value is 100. If system db size is big, then it shall be increased.\n");
00027    printf("       T -> Will dump only the table specified with this option.\n");
00028    return;
00029   
00030 }


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