1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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. *
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. *
15 ***************************************************************************/
18 #include <DatabaseManagerImpl.h>
20 #include <TableImpl.h>
21 #include <SqlFactory.h>
22 #include <SqlStatement.h>
25 printf("Usage: csqldump [-u username] [-p passwd] [-n noOfStmtsPerCommit] [-T tableName]\n");
26 printf(" n -> number of statements per commit\n");
27 printf(" Default value is 100. If system db size is big, then it shall be increased.\n");
28 printf(" T -> Will dump only the table specified with this option.\n");
33 bool isCached(char *tblName
)
35 if (!Conf::config
.useCache()) return false;
36 FILE *fp
= fopen(Conf::config
.getTableConfigFile(),"r");
40 char ctablename
[IDENTIFIER_LENGTH
];
41 char fieldname
[IDENTIFIER_LENGTH
];
42 char condition
[IDENTIFIER_LENGTH
];
43 char field
[IDENTIFIER_LENGTH
];
48 fscanf(fp
, "%d:%s %s %s %s\n", &mode
, ctablename
,fieldname
,condition
,field
);
49 if (strcmp (ctablename
, tblName
) == 0) { isCached
=true; break; }
54 int main(int argc
, char **argv
)
56 char username
[IDENTIFIER_LENGTH
];
58 char password
[IDENTIFIER_LENGTH
];
60 char tblName
[IDENTIFIER_LENGTH
];
63 bool exclusive
= false;
64 char name
[IDENTIFIER_LENGTH
];
65 while ((c
= getopt(argc
, argv
, "u:p:n:T:X?")) != EOF
)
69 case 'u' : { strcpy(username
, argv
[optind
- 1]); opt
=1; break; }
70 case 'p' : { strcpy(password
, argv
[optind
- 1]); opt
=1; break; }
71 case 'n' : { noOfStmts
= atoi(argv
[optind
- 1]); opt
= 5; break; }
72 case 'T' : { strcpy(tblName
, argv
[optind
- 1]); opt
= 15; break; }
73 case 'X' : { exclusive
= true; break; }
74 case '?' : { opt
= 10; break; } //print help
75 default: opt
=1; //list all the tables
84 //printf("%s %s \n", username, password);
85 if (username
[0] == '\0' )
87 strcpy(username
, "root");
88 strcpy(password
, "manager");
90 SqlConnection
*sqlconn
= (SqlConnection
*) SqlFactory::createConnection(CSqlDirect
);
92 SqlStatement
*stmt
= (SqlStatement
*) SqlFactory::createStatement(CSqlDirect
);
94 stmt
->setLoading(true);
98 if (opt
== 0 || opt
== 1) opt
= 5;
101 DbRetVal rv
= conn
.open(username
, password
);
103 printf("Unable to get connection to csql\n");
108 DatabaseManagerImpl
*dbMgr
= (DatabaseManagerImpl
*) conn
.getDatabaseManager();
110 printf("Unable to retrive db manager\n");
116 List tableList
= dbMgr
->getAllTableNames();
117 ListIterator iter
= tableList
.getIterator();
118 Identifier
*elem
= NULL
;
120 while (iter
.hasElement())
122 elem
= (Identifier
*) iter
.nextElement();
123 if (!exclusive
&& isCached(elem
->name
)) continue;
124 printf("CREATE TABLE %s (", elem
->name
);
125 Table
*table
= dbMgr
->openTable(elem
->name
);
126 FieldInfo
*info
= new FieldInfo();
127 List fNameList
= table
->getFieldNameList();
128 ListIterator fNameIter
= fNameList
.getIterator();
130 bool firstField
=true;
131 char fieldName
[IDENTIFIER_LENGTH
];
132 while (fNameIter
.hasElement()) {
133 elem
= (Identifier
*) fNameIter
.nextElement();
134 Table::getFieldNameAlone(elem
->name
, fieldName
);
135 rv
= table
->getFieldInfo(elem
->name
, info
);
137 printf("unable to retrive info for table %s\n", elem
->name
);
143 printf("%s %s ", fieldName
, AllDataType::getSQLString(info
->type
));
147 printf(", %s %s ", fieldName
, AllDataType::getSQLString(info
->type
));
148 if (info
->type
== typeString
) printf("(%d)",info
->length
);
149 if (info
->type
== typeBinary
) printf("(%d)",info
->length
);
150 if (info
->isNull
) printf(" NOT NULL ");
151 if (info
->isDefault
) printf(" DEFAULT '%s' ", info
->defaultValueBuf
);
152 if (info
->isAutoIncrement
) printf(" AUTO_INCREMENT ");
155 table
->printSQLIndexString();
157 dbMgr
->closeTable(table
);
160 rv
= sqlconn
->connect("root", "manager");
162 printf("unable to connect to csql\n");
167 stmt
->setConnection(sqlconn
);
169 rv
= sqlconn
->getExclusiveLock();
171 printf("Unable to get exclusive lock\n");
172 sqlconn
->disconnect();
179 char sqlstring
[1024];
181 while (iter
.hasElement()) {
182 elem
= (Identifier
*) iter
.nextElement();
183 if (!exclusive
&& isCached(elem
->name
)) continue;
184 if (!flag
) { printf("SET AUTOCOMMIT OFF;\n"); flag
=true; }
185 sprintf(sqlstring
, "SELECT * FROM %s;", elem
->name
);
186 sqlconn
->beginTrans();
187 DbRetVal rv
= stmt
->prepare(sqlstring
);
189 rv
= stmt
->execute(rows
);
193 tuple
= stmt
->fetchAndPrint(true);
194 if (tuple
== NULL
) break;
196 if (rows
% noOfStmts
==0) {
198 sqlconn
->beginTrans();
202 if (rows
% noOfStmts
!=0) { sqlconn
->commit(); printf("COMMIT;\n"); }
209 DbRetVal rv
= conn
.open(username
, password
);
211 printf("Unable to get connection to csql\n");
216 DatabaseManagerImpl
*dbMgr
= (DatabaseManagerImpl
*) conn
.getDatabaseManager();
217 if (dbMgr
== NULL
) { printf("Auth failed\n"); return 2;}
218 Table
*table
= dbMgr
->openTable(tblName
);
220 printf("csqldump: Table \'%s\' does not exist\n", tblName
);
226 printf("CREATE TABLE %s (", tblName
);
227 FieldInfo
*info
= new FieldInfo();
228 List fNameList
= table
->getFieldNameList();
229 ListIterator fNameIter
= fNameList
.getIterator();
230 bool firstField
=true;
231 Identifier
*elem
= NULL
;
232 char fieldName
[IDENTIFIER_LENGTH
];
233 while (fNameIter
.hasElement()) {
234 elem
= (Identifier
*) fNameIter
.nextElement();
235 Table::getFieldNameAlone(elem
->name
, fieldName
);
236 table
->getFieldInfo((const char*)elem
->name
, info
);
238 printf("%s %s ", fieldName
, AllDataType::getSQLString(info
->type
));
242 printf(", %s %s ", fieldName
, AllDataType::getSQLString(info
->type
));
243 if (info
->type
== typeString
) printf("(%d)",info
->length
);
244 if (info
->type
== typeBinary
) printf("(%d)",info
->length
);
245 if (info
->isNull
) printf(" NOT NULL ");
246 if (info
->isDefault
) printf(" DEFAULT '%s' ", info
->defaultValueBuf
);
249 table
->printSQLIndexString();
252 char sqlstring
[1024];
254 if (!flag
) { printf("SET AUTOCOMMIT OFF;\n"); flag
=true; }
255 rv
= sqlconn
->connect("root", "manager");
257 printf("unable to connect\n");
260 stmt
->setConnection(sqlconn
);
261 sprintf(sqlstring
, "SELECT * FROM %s;", tblName
);
262 sqlconn
->beginTrans();
263 rv
= stmt
->prepare(sqlstring
);
265 rv
= stmt
->execute(rows
);
269 tuple
= stmt
->fetchAndPrint(true);
270 if (tuple
== NULL
) break;
272 if (rows
% noOfStmts
==0) {
274 sqlconn
->beginTrans();
278 if (rows
% noOfStmts
!=0) { sqlconn
->commit(); printf("COMMIT;\n"); }
282 sqlconn
->disconnect();