changing close to closescan
[csql.git] / src / tools / csqldump.cxx
blob1a7d2871f3f8ad9d58fcbb1c220f1e7f965340dd
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 return OK;
39 char ctablename[IDENTIFIER_LENGTH];
40 char fieldname[IDENTIFIER_LENGTH];
41 char condition[IDENTIFIER_LENGTH];
42 char field[IDENTIFIER_LENGTH];
43 int mode;
44 bool isCached=false;
45 while(!feof(fp))
47 fscanf(fp, "%d:%s %s %s %s\n", &mode, ctablename,fieldname,condition,field);
48 if (strcmp (ctablename, tblName) == 0) { isCached=true; break; }
50 fclose(fp);
51 return isCached;
53 int main(int argc, char **argv)
55 char username[IDENTIFIER_LENGTH];
56 username [0] = '\0';
57 char password[IDENTIFIER_LENGTH];
58 password [0] = '\0';
59 char tblName[IDENTIFIER_LENGTH];
60 int c = 0, opt = 0;
61 int noOfStmts =100;
62 char name[IDENTIFIER_LENGTH];
63 while ((c = getopt(argc, argv, "u:p:n:T:?")) != EOF)
65 switch (c)
67 case 'u' : { strcpy(username, argv[optind - 1]); opt=1; break; }
68 case 'p' : { strcpy(password, argv[optind - 1]); opt=1; break; }
69 case 'n' : { noOfStmts = atoi(argv[optind - 1]); opt = 5; break; }
70 case 'T' : { strcpy(tblName, argv[optind - 1]); opt = 15; break; }
71 case '?' : { opt = 10; break; } //print help
72 default: opt=1; //list all the tables
75 }//while options
76 if (opt == 10) {
77 printUsage();
78 return 0;
81 //printf("%s %s \n", username, password);
82 if (username[0] == '\0' )
84 strcpy(username, "root");
85 strcpy(password, "manager");
87 SqlConnection *sqlconn = (SqlConnection*) SqlFactory::createConnection(CSql);
88 sqlconn->connect("root", "manager");
89 SqlStatement *stmt = (SqlStatement*) SqlFactory::createStatement(CSql);
90 stmt->setConnection(sqlconn);
92 Connection conn;
93 DbRetVal rv = conn.open(username, password);
94 if (rv != OK) return 1;
95 DatabaseManagerImpl *dbMgr = (DatabaseManagerImpl*) conn.getDatabaseManager();
97 if (dbMgr == NULL) { printf("Auth failed\n"); return 2;}
98 if (opt == 0 || opt == 1) opt = 5;
99 if (opt == 5) {
100 List tableList = dbMgr->getAllTableNames();
101 ListIterator iter = tableList.getIterator();
102 Identifier *elem = NULL;
103 int count =0;
104 while (iter.hasElement())
106 elem = (Identifier*) iter.nextElement();
107 if (isCached(elem->name)) continue;
108 printf("CREATE TABLE %s (", elem->name);
109 Table *table = dbMgr->openTable(elem->name);
110 FieldInfo *info = new FieldInfo();
111 List fNameList = table->getFieldNameList();
112 ListIterator fNameIter = fNameList.getIterator();
113 count++;
114 bool firstField=true;
115 char fieldName[IDENTIFIER_LENGTH];
116 while (fNameIter.hasElement()) {
117 elem = (Identifier*) fNameIter.nextElement();
118 Table::getFieldNameAlone(elem->name, fieldName);
119 rv = table->getFieldInfo(elem->name, info);
120 if (rv !=OK) return rv;
121 if (firstField) {
122 printf("%s %s ", fieldName, AllDataType::getSQLString(info->type));
123 firstField = false;
125 else
126 printf(", %s %s ", fieldName, AllDataType::getSQLString(info->type));
127 if (info->type == typeString) printf("(%d)",info->length );
128 if (info->type == typeBinary) printf("(%d)",info->length);
129 if (info->isNull) printf(" NOT NULL ");
130 if (info->isDefault) printf(" DEFAULT '%s' ", info->defaultValueBuf);
132 printf(");\n");
133 table->printSQLIndexString();
134 delete info;
135 dbMgr->closeTable(table);
137 iter.reset();
138 char sqlstring[1024];
139 bool flag=false;
140 while (iter.hasElement()) {
141 elem = (Identifier*) iter.nextElement();
142 if (isCached(elem->name)) continue;
143 if (!flag) { printf("SET AUTOCOMMIT OFF;\n"); flag=true; }
144 sprintf(sqlstring, "SELECT * FROM %s;", elem->name);
145 sqlconn->beginTrans();
146 DbRetVal rv = stmt->prepare(sqlstring);
147 int rows = 0;
148 rv = stmt->execute(rows);
149 void *tuple = NULL;
150 rows = 0;
151 while(true) {
152 tuple = stmt->fetchAndPrint(true);
153 if (tuple == NULL) break;
154 rows++;
155 if (rows % noOfStmts ==0) {
156 sqlconn->commit();
157 sqlconn->beginTrans();
158 printf("COMMIT;\n");
161 if (rows % noOfStmts !=0) { sqlconn->commit(); printf("COMMIT;\n"); }
162 stmt->close();
163 stmt->free();
165 conn.close();
166 sqlconn->disconnect();
167 delete sqlconn;
168 delete stmt;
169 return 0;
171 if (opt == 15) {
172 Table *table = dbMgr->openTable(tblName);
173 if (table == NULL) {
174 printf("csqldump: Table \'%s\' does not exist\n", tblName);
175 conn.close();
176 sqlconn->disconnect();
177 delete sqlconn;
178 delete stmt;
179 return 3;
181 printf("CREATE TABLE %s (", tblName);
182 FieldInfo *info = new FieldInfo();
183 List fNameList = table->getFieldNameList();
184 ListIterator fNameIter = fNameList.getIterator();
185 bool firstField=true;
186 Identifier *elem = NULL;
187 char fieldName[IDENTIFIER_LENGTH];
188 while (fNameIter.hasElement()) {
189 elem = (Identifier*) fNameIter.nextElement();
190 Table::getFieldNameAlone(elem->name, fieldName);
191 table->getFieldInfo((const char*)elem->name, info);
192 if (firstField) {
193 printf("%s %s ", fieldName, AllDataType::getSQLString(info->type));
194 firstField = false;
196 else
197 printf(", %s %s ", fieldName, AllDataType::getSQLString(info->type));
198 if (info->type == typeString) printf("(%d)",info->length);
199 if (info->type == typeBinary) printf("(%d)",info->length);
200 if (info->isNull) printf(" NOT NULL ");
201 if (info->isDefault) printf(" DEFAULT '%s' ", info->defaultValueBuf);
203 printf(");\n");
204 table->printSQLIndexString();
205 delete info;
206 char sqlstring[1024];
207 bool flag=false;
208 if (!flag) { printf("SET AUTOCOMMIT OFF;\n"); flag=true; }
209 sprintf(sqlstring, "SELECT * FROM %s;", tblName);
210 sqlconn->beginTrans();
211 DbRetVal rv = stmt->prepare(sqlstring);
212 int rows = 0;
213 rv = stmt->execute(rows);
214 void *tuple = NULL;
215 rows = 0;
216 while(true) {
217 tuple = stmt->fetchAndPrint(true);
218 if (tuple == NULL) break;
219 rows++;
220 if (rows % noOfStmts ==0) {
221 sqlconn->commit();
222 sqlconn->beginTrans();
223 printf("COMMIT;\n");
226 if (rows % noOfStmts !=0) { sqlconn->commit(); printf("COMMIT;\n"); }
227 stmt->close();
228 stmt->free();
230 conn.close();
231 sqlconn->disconnect();
232 delete sqlconn;
233 delete stmt;
235 return 0;