setting the autocommit mode to off first before displaying the records
[csql.git] / src / tools / csqldump.cxx
blob68f7ac8c633e7c5fdf2de2a1c3803535138802ea
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
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. *
9 * *
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. *
14 * *
15 ***************************************************************************/
16 #include <CSql.h>
17 #include <DatabaseManagerImpl.h>
18 #include <Database.h>
19 #include <TableImpl.h>
20 #include <SqlFactory.h>
21 #include <SqlStatement.h>
22 void printUsage()
24 printf("Usage: csqldump [-u username] [-p passwd] [-n noOfStmtsPerCommit] \n");
25 printf(" n -> number of statements per commit\n");
26 printf(" Default value is 100. If system db size is big, then it shall be increased.\n");
27 return;
31 int main(int argc, char **argv)
33 char username[IDENTIFIER_LENGTH];
34 username [0] = '\0';
35 char password[IDENTIFIER_LENGTH];
36 password [0] = '\0';
37 int c = 0, opt = 0;
38 int noOfStmts =100;
39 char name[IDENTIFIER_LENGTH];
40 while ((c = getopt(argc, argv, "u:p:n:?")) != EOF)
42 switch (c)
44 case 'u' : { strcpy(username, argv[optind - 1]); opt=1; break; }
45 case 'p' : { strcpy(password, argv[optind - 1]); opt=1; break; }
46 case 'n' : { noOfStmts = atoi(argv[optind - 1]); opt = 5; break; }
47 case '?' : { opt = 10; break; } //print help
48 default: opt=1; //list all the tables
51 }//while options
52 if (opt == 10) {
53 printUsage();
54 return 0;
57 //printf("%s %s \n", username, password);
58 if (username[0] == '\0' )
60 strcpy(username, "root");
61 strcpy(password, "manager");
63 SqlConnection *sqlconn = (SqlConnection*) SqlFactory::createConnection(CSql);
64 sqlconn->connect("root", "manager");
65 SqlStatement *stmt = (SqlStatement*) SqlFactory::createStatement(CSql);
66 stmt->setConnection(sqlconn);
68 Connection conn;
69 DbRetVal rv = conn.open(username, password);
70 if (rv != OK) return 1;
71 DatabaseManagerImpl *dbMgr = (DatabaseManagerImpl*) conn.getDatabaseManager();
72 if (dbMgr == NULL) { printf("Auth failed\n"); return 2;}
73 List tableList = dbMgr->getAllTableNames();
74 ListIterator iter = tableList.getIterator();
75 Identifier *elem = NULL;
76 int ret =0;
77 opt=5;
78 if (opt == 5) {
79 int count =0;
80 while (iter.hasElement())
82 elem = (Identifier*) iter.nextElement();
83 printf("CREATE TABLE %s (", elem->name);
84 Table *table = dbMgr->openTable(elem->name);
85 FieldInfo *info = new FieldInfo();
86 List fNameList = table->getFieldNameList();
87 ListIterator fNameIter = fNameList.getIterator();
88 count++;
89 bool firstField=true;
90 while (fNameIter.hasElement()) {
91 elem = (Identifier*) fNameIter.nextElement();
92 table->getFieldInfo((const char*)elem->name, info);
93 if (firstField) {
94 printf("%s %s ", elem->name, AllDataType::getSQLString(info->type));
95 firstField = false;
97 else
98 printf(", %s %s ", elem->name, AllDataType::getSQLString(info->type));
99 if (info->type == typeString) printf("(%d)",info->length -1);
100 if (info->isNull) printf(" NOT NULL ");
101 if (info->isDefault) printf(" DEFAULT '%s' ", info->defaultValueBuf);
103 printf(");\n");
104 table->printSQLIndexString();
105 delete info;
106 dbMgr->closeTable(table);
108 iter.reset();
109 char sqlstring[1024];
110 bool flag=false;
111 while (iter.hasElement()) {
112 if (!flag) { printf("SET AUTOCOMMIT OFF;\n"); flag=true; }
113 elem = (Identifier*) iter.nextElement();
114 sprintf(sqlstring, "SELECT * FROM %s;", elem->name);
115 sqlconn->beginTrans();
116 DbRetVal rv = stmt->prepare(sqlstring);
117 int rows = 0;
118 rv = stmt->execute(rows);
119 void *tuple = NULL;
120 rows = 0;
121 while(true) {
122 tuple = stmt->fetchAndPrint(true);
123 if (tuple == NULL) break;
124 rows++;
125 if (rows % noOfStmts ==0) {
126 sqlconn->commit();
127 sqlconn->beginTrans();
128 printf("COMMIT;\n");
131 if (rows % noOfStmts !=0) { sqlconn->commit(); printf("COMMIT;\n"); }
132 stmt->close();
133 stmt->free();
137 conn.close();
138 sqlconn->disconnect();
140 return ret;