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>
26 printf("Usage: csqldump [-u username] [-p passwd] [-n noOfStmtsPerCommit] [-T tableName]\n");
27 printf(" n -> number of statements per commit\n");
28 printf(" Default value is 100. If system db size is big, then it shall be increased.\n");
29 printf(" T -> Will dump only the table specified with this option.\n");
34 bool isCached(char *tblName
)
36 if (!Conf::config
.useCache()) return false;
37 FILE *fp
= fopen(Conf::config
.getTableConfigFile(),"r");
38 if( fp
== NULL
) { return OK
; }
39 char ctablename
[IDENTIFIER_LENGTH
];
40 char fieldname
[IDENTIFIER_LENGTH
];
41 char condition
[IDENTIFIER_LENGTH
];
42 char field
[IDENTIFIER_LENGTH
];
43 char dsnName
[IDENTIFIER_LENGTH
];
48 fscanf(fp
, "%d %s %s %s %s %s %s %s\n", &mode
, ctablename
,fieldname
,condition
,field
,dsnName
);
49 if (strcmp (ctablename
, tblName
) == 0) { isCached
=true; break; }
55 int main(int argc
, char **argv
)
57 char username
[IDENTIFIER_LENGTH
];
59 char password
[IDENTIFIER_LENGTH
];
61 char tblName
[IDENTIFIER_LENGTH
];
62 char conditionVal
[IDENTIFIER_LENGTH
];
65 bool exclusive
= false;
66 bool Iscondition
=false;
68 char name
[IDENTIFIER_LENGTH
];
69 while ((c
= getopt(argc
, argv
, "u:p:n:T:c:XS?")) != EOF
) {
71 case 'u' : { strcpy(username
, argv
[optind
- 1]); opt
=1; break; }
72 case 'p' : { strcpy(password
, argv
[optind
- 1]); opt
=1; break; }
73 case 'n' : { noOfStmts
= atoi(argv
[optind
- 1]); opt
= 5; break; }
74 case 'T' : { strcpy(tblName
, argv
[optind
- 1]); opt
= 15; break; }
75 case 'c' : {strcpy(conditionVal
,argv
[optind
-1]); Iscondition
=true;break;}
76 case 'X' : { exclusive
= true; break; }
77 case 'S' : { schema
= true; break; }
78 case '?' : { opt
= 10; break; } //print help
79 default: opt
=1; //list all the tables
88 //printf("%s %s \n", username, password);
89 if (username
[0] == '\0' ) {
90 strcpy(username
, I_USER
);
91 strcpy(password
, I_PASS
);
93 SqlConnection
*sqlconn
= (SqlConnection
*) SqlFactory::createConnection(CSqlDirect
);
95 SqlStatement
*stmt
= (SqlStatement
*) SqlFactory::createStatement(CSqlDirect
);
96 if (exclusive
) { 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()) {
121 elem
= (Identifier
*) iter
.nextElement();
122 //if (!exclusive && isCached(elem->name)) continue;
123 printf("CREATE TABLE %s (", elem
->name
);
124 Table
*table
= dbMgr
->openTable(elem
->name
);
125 FieldInfo
*info
= new FieldInfo();
126 List fNameList
= table
->getFieldNameList();
127 ListIterator fNameIter
= fNameList
.getIterator();
129 bool firstField
=true;
130 Identifier
*elem1
= NULL
;
131 char fieldName
[IDENTIFIER_LENGTH
];
132 while (fNameIter
.hasElement()) {
133 elem1
= (Identifier
*) fNameIter
.nextElement();
134 Table::getFieldNameAlone(elem1
->name
, fieldName
);
135 rv
= table
->getFieldInfo(elem1
->name
, info
);
137 printf("unable to retrive info for table %s\n", elem1
->name
);
143 printf("%s %s ", fieldName
, AllDataType::getSQLString(info
->type
));
146 printf(", %s %s ", fieldName
, AllDataType::getSQLString(info
->type
));
147 if (info
->type
== typeString
) printf("(%d)",info
->length
);
148 if (info
->type
== typeBinary
) printf("(%d)",info
->length
);
149 if (info
->isNull
) printf(" NOT NULL ");
150 if (info
->isDefault
) printf(" DEFAULT '%s' ", info
->defaultValueBuf
);
151 if (info
->isAutoIncrement
) printf(" AUTO_INCREMENT ");
154 while (fNameIter
.hasElement()) {
155 elem1
= (Identifier
*) fNameIter
.nextElement();
159 if (table
->isFKTable()){
160 table
->printSQLForeignString();
163 table
->printSQLIndexString();
165 dbMgr
->closeTable(table
);
168 if (schema
) { delete sqlconn
; delete stmt
; return 0; }
170 rv
= sqlconn
->connect(I_USER
, I_PASS
);
172 printf("unable to connect to csql\n");
177 stmt
->setConnection(sqlconn
);
179 rv
= sqlconn
->getExclusiveLock();
181 printf("Unable to get exclusive lock\n");
182 sqlconn
->disconnect();
189 char sqlstring
[1024]="";
191 while (iter
.hasElement()) {
192 elem
= (Identifier
*) iter
.nextElement();
193 //if (!exclusive && isCached(elem->name)) continue;
194 if (!flag
) { printf("SET AUTOCOMMIT OFF;\n"); flag
=true; }
195 sprintf(sqlstring
, "SELECT * FROM %s;", elem
->name
);
196 sqlconn
->beginTrans();
197 DbRetVal rv
= stmt
->prepare(sqlstring
);
199 rv
= stmt
->execute(rows
);
203 tuple
= stmt
->fetchAndPrint(true);
204 if (tuple
== NULL
) break;
206 if (rows
% noOfStmts
==0) {
208 sqlconn
->beginTrans();
212 if (rows
% noOfStmts
!=0) {
220 while (iter
.hasElement()) {
221 elem
= (Identifier
*) iter
.nextElement();
228 DbRetVal rv
= conn
.open(username
, password
);
230 printf("Unable to get connection to csql\n");
235 DatabaseManagerImpl
*dbMgr
= (DatabaseManagerImpl
*) conn
.getDatabaseManager();
236 if (dbMgr
== NULL
) { printf("Auth failed\n"); return 2;}
237 Table
*table
= dbMgr
->openTable(tblName
);
239 printf("csqldump: Table \'%s\' does not exist\n", tblName
);
245 printf("CREATE TABLE %s (", tblName
);
246 FieldInfo
*info
= new FieldInfo();
247 List fNameList
= table
->getFieldNameList();
248 ListIterator fNameIter
= fNameList
.getIterator();
249 bool firstField
=true;
250 Identifier
*elem
= NULL
;
251 char fieldName
[IDENTIFIER_LENGTH
];
252 while (fNameIter
.hasElement()) {
253 elem
= (Identifier
*) fNameIter
.nextElement();
254 Table::getFieldNameAlone(elem
->name
, fieldName
);
255 table
->getFieldInfo((const char*)elem
->name
, info
);
257 printf("%s %s ", fieldName
, AllDataType::getSQLString(info
->type
));
261 printf(", %s %s ", fieldName
, AllDataType::getSQLString(info
->type
));
262 if (info
->type
== typeString
) printf("(%d)",info
->length
);
263 if (info
->type
== typeBinary
) printf("(%d)",info
->length
);
264 if (info
->isNull
) printf(" NOT NULL ");
265 if (info
->isDefault
) printf(" DEFAULT '%s' ", info
->defaultValueBuf
);
268 table
->printSQLIndexString();
271 char sqlstring
[1024];
272 // char sqlstring1[1024];
274 if (!flag
) { printf("SET AUTOCOMMIT OFF;\n"); flag
=true; }
275 rv
= sqlconn
->connect(I_USER
, I_PASS
);
277 printf("unable to connect\n");
280 stmt
->setConnection(sqlconn
);
283 sprintf(sqlstring
, "SELECT * FROM %s WHERE %s;", tblName
,conditionVal
);
285 sprintf(sqlstring
, "SELECT * FROM %s;", tblName
);
286 sqlconn
->beginTrans();
287 rv
= stmt
->prepare(sqlstring
);
289 //***********************************************
291 rv
= stmt
->execute(rows
);
295 tuple
= stmt
->fetchAndPrint(true);
296 if (tuple
== NULL
) break;
298 if (rows
% noOfStmts
==0) {
300 sqlconn
->beginTrans();
304 if (rows
% noOfStmts
!=0) { sqlconn
->commit(); printf("COMMIT;\n"); }
308 sqlconn
->disconnect();