2224636 dropTable() fails when csqltable.conf is missing
[csql.git] / src / tools / csqldump.cxx
blob247568326b363a7f9fac56a32c6a912d06fb8294
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] [-T tableName]\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 printf(" T -> Will dump only the table specified with this option.\n");
28 return;
32 bool isCached(char *tblName)
34 if (!Conf::config.useCache()) return false;
35 FILE *fp = fopen(Conf::config.getTableConfigFile(),"r");
36 if( fp == NULL ) {
37 printError(ErrSysInit, "csqltable.conf file does not exist");
38 return ErrSysInit;
40 char ctablename[IDENTIFIER_LENGTH];
41 char condition[IDENTIFIER_LENGTH];
42 int mode;
43 bool isCached=false;
44 while(!feof(fp))
46 fscanf(fp, "%d:%s %s\n", &mode, ctablename,condition);
47 if (strcmp (ctablename, tblName) == 0) { isCached=true; break; }
49 fclose(fp);
50 return isCached;
52 int main(int argc, char **argv)
54 char username[IDENTIFIER_LENGTH];
55 username [0] = '\0';
56 char password[IDENTIFIER_LENGTH];
57 password [0] = '\0';
58 char tblName[IDENTIFIER_LENGTH];
59 int c = 0, opt = 0;
60 int noOfStmts =100;
61 char name[IDENTIFIER_LENGTH];
62 while ((c = getopt(argc, argv, "u:p:n:T:?")) != EOF)
64 switch (c)
66 case 'u' : { strcpy(username, argv[optind - 1]); opt=1; break; }
67 case 'p' : { strcpy(password, argv[optind - 1]); opt=1; break; }
68 case 'n' : { noOfStmts = atoi(argv[optind - 1]); opt = 5; break; }
69 case 'T' : { strcpy(tblName, argv[optind - 1]); opt = 15; break; }
70 case '?' : { opt = 10; break; } //print help
71 default: opt=1; //list all the tables
74 }//while options
75 if (opt == 10) {
76 printUsage();
77 return 0;
80 //printf("%s %s \n", username, password);
81 if (username[0] == '\0' )
83 strcpy(username, "root");
84 strcpy(password, "manager");
86 SqlConnection *sqlconn = (SqlConnection*) SqlFactory::createConnection(CSql);
87 sqlconn->connect("root", "manager");
88 SqlStatement *stmt = (SqlStatement*) SqlFactory::createStatement(CSql);
89 stmt->setConnection(sqlconn);
91 Connection conn;
92 DbRetVal rv = conn.open(username, password);
93 if (rv != OK) return 1;
94 DatabaseManagerImpl *dbMgr = (DatabaseManagerImpl*) conn.getDatabaseManager();
96 if (dbMgr == NULL) { printf("Auth failed\n"); return 2;}
97 if (opt == 0 || opt == 1) opt = 5;
98 if (opt == 5) {
99 List tableList = dbMgr->getAllTableNames();
100 ListIterator iter = tableList.getIterator();
101 Identifier *elem = NULL;
102 int count =0;
103 while (iter.hasElement())
105 elem = (Identifier*) iter.nextElement();
106 if (isCached(elem->name)) continue;
107 printf("CREATE TABLE %s (", elem->name);
108 Table *table = dbMgr->openTable(elem->name);
109 FieldInfo *info = new FieldInfo();
110 List fNameList = table->getFieldNameList();
111 ListIterator fNameIter = fNameList.getIterator();
112 count++;
113 bool firstField=true;
114 while (fNameIter.hasElement()) {
115 elem = (Identifier*) fNameIter.nextElement();
116 table->getFieldInfo((const char*)elem->name, info);
117 if (firstField) {
118 printf("%s %s ", elem->name, AllDataType::getSQLString(info->type));
119 firstField = false;
121 else
122 printf(", %s %s ", elem->name, AllDataType::getSQLString(info->type));
123 if (info->type == typeString) printf("(%d)",info->length -1);
124 if (info->type == typeBinary) printf("(%d)",info->length);
125 if (info->isNull) printf(" NOT NULL ");
126 if (info->isDefault) printf(" DEFAULT '%s' ", info->defaultValueBuf);
128 printf(");\n");
129 table->printSQLIndexString();
130 delete info;
131 dbMgr->closeTable(table);
133 iter.reset();
134 char sqlstring[1024];
135 bool flag=false;
136 while (iter.hasElement()) {
137 elem = (Identifier*) iter.nextElement();
138 if (isCached(elem->name)) continue;
139 if (!flag) { printf("SET AUTOCOMMIT OFF;\n"); flag=true; }
140 sprintf(sqlstring, "SELECT * FROM %s;", elem->name);
141 sqlconn->beginTrans();
142 DbRetVal rv = stmt->prepare(sqlstring);
143 int rows = 0;
144 rv = stmt->execute(rows);
145 void *tuple = NULL;
146 rows = 0;
147 while(true) {
148 tuple = stmt->fetchAndPrint(true);
149 if (tuple == NULL) break;
150 rows++;
151 if (rows % noOfStmts ==0) {
152 sqlconn->commit();
153 sqlconn->beginTrans();
154 printf("COMMIT;\n");
157 if (rows % noOfStmts !=0) { sqlconn->commit(); printf("COMMIT;\n"); }
158 stmt->close();
159 stmt->free();
161 conn.close();
162 sqlconn->disconnect();
163 delete sqlconn;
164 delete stmt;
165 return 0;
167 if (opt == 15) {
168 Table *table = dbMgr->openTable(tblName);
169 if (table == NULL) {
170 printf("csqldump: Table \'%s\' does not exist\n", tblName);
171 conn.close();
172 sqlconn->disconnect();
173 delete sqlconn;
174 delete stmt;
175 return 3;
177 printf("CREATE TABLE %s (", tblName);
178 FieldInfo *info = new FieldInfo();
179 List fNameList = table->getFieldNameList();
180 ListIterator fNameIter = fNameList.getIterator();
181 bool firstField=true;
182 Identifier *elem = NULL;
183 while (fNameIter.hasElement()) {
184 elem = (Identifier*) fNameIter.nextElement();
185 table->getFieldInfo((const char*)elem->name, info);
186 if (firstField) {
187 printf("%s %s ", elem->name, AllDataType::getSQLString(info->type));
188 firstField = false;
190 else
191 printf(", %s %s ", elem->name, AllDataType::getSQLString(info->type));
192 if (info->type == typeString) printf("(%d)",info->length -1);
193 if (info->type == typeBinary) printf("(%d)",info->length);
194 if (info->isNull) printf(" NOT NULL ");
195 if (info->isDefault) printf(" DEFAULT '%s' ", info->defaultValueBuf);
197 printf(");\n");
198 table->printSQLIndexString();
199 delete info;
200 char sqlstring[1024];
201 bool flag=false;
202 if (!flag) { printf("SET AUTOCOMMIT OFF;\n"); flag=true; }
203 sprintf(sqlstring, "SELECT * FROM %s;", tblName);
204 sqlconn->beginTrans();
205 DbRetVal rv = stmt->prepare(sqlstring);
206 int rows = 0;
207 rv = stmt->execute(rows);
208 void *tuple = NULL;
209 rows = 0;
210 while(true) {
211 tuple = stmt->fetchAndPrint(true);
212 if (tuple == NULL) break;
213 rows++;
214 if (rows % noOfStmts ==0) {
215 sqlconn->commit();
216 sqlconn->beginTrans();
217 printf("COMMIT;\n");
220 if (rows % noOfStmts !=0) { sqlconn->commit(); printf("COMMIT;\n"); }
221 stmt->close();
222 stmt->free();
224 conn.close();
225 sqlconn->disconnect();
226 delete sqlconn;
227 delete stmt;
229 return 0;