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 ***************************************************************************/
17 #include <DatabaseManagerImpl.h>
19 #include <TableImpl.h>
20 #include <SqlFactory.h>
21 #include <SqlStatement.h>
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");
31 int main(int argc
, char **argv
)
33 char username
[IDENTIFIER_LENGTH
];
35 char password
[IDENTIFIER_LENGTH
];
39 char name
[IDENTIFIER_LENGTH
];
40 while ((c
= getopt(argc
, argv
, "u:p:n:?")) != EOF
)
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
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
);
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
;
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();
90 while (fNameIter
.hasElement()) {
91 elem
= (Identifier
*) fNameIter
.nextElement();
92 table
->getFieldInfo((const char*)elem
->name
, info
);
94 printf("%s %s ", elem
->name
, AllDataType::getSQLString(info
->type
));
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
);
104 table
->printSQLIndexString();
106 dbMgr
->closeTable(table
);
109 char sqlstring
[1024];
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
);
118 rv
= stmt
->execute(rows
);
122 tuple
= stmt
->fetchAndPrint(true);
123 if (tuple
== NULL
) break;
125 if (rows
% noOfStmts
==0) {
127 sqlconn
->beginTrans();
131 if (rows
% noOfStmts
!=0) { sqlconn
->commit(); printf("COMMIT;\n"); }
138 sqlconn
->disconnect();