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] [-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");
32 bool isCached(char *tblName
)
34 if (!Conf::config
.useCache()) return false;
35 FILE *fp
= fopen(Conf::config
.getTableConfigFile(),"r");
37 printError(ErrSysInit
, "csqltable.conf file does not exist");
40 char ctablename
[IDENTIFIER_LENGTH
];
41 char condition
[IDENTIFIER_LENGTH
];
46 fscanf(fp
, "%d:%s %s\n", &mode
, ctablename
,condition
);
47 if (strcmp (ctablename
, tblName
) == 0) { isCached
=true; break; }
52 int main(int argc
, char **argv
)
54 char username
[IDENTIFIER_LENGTH
];
56 char password
[IDENTIFIER_LENGTH
];
58 char tblName
[IDENTIFIER_LENGTH
];
61 char name
[IDENTIFIER_LENGTH
];
62 while ((c
= getopt(argc
, argv
, "u:p:n:T:?")) != EOF
)
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
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
);
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;
99 List tableList
= dbMgr
->getAllTableNames();
100 ListIterator iter
= tableList
.getIterator();
101 Identifier
*elem
= NULL
;
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();
113 bool firstField
=true;
114 while (fNameIter
.hasElement()) {
115 elem
= (Identifier
*) fNameIter
.nextElement();
116 table
->getFieldInfo((const char*)elem
->name
, info
);
118 printf("%s %s ", elem
->name
, AllDataType::getSQLString(info
->type
));
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
);
129 table
->printSQLIndexString();
131 dbMgr
->closeTable(table
);
134 char sqlstring
[1024];
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
);
144 rv
= stmt
->execute(rows
);
148 tuple
= stmt
->fetchAndPrint(true);
149 if (tuple
== NULL
) break;
151 if (rows
% noOfStmts
==0) {
153 sqlconn
->beginTrans();
157 if (rows
% noOfStmts
!=0) { sqlconn
->commit(); printf("COMMIT;\n"); }
162 sqlconn
->disconnect();
168 Table
*table
= dbMgr
->openTable(tblName
);
170 printf("csqldump: Table \'%s\' does not exist\n", tblName
);
172 sqlconn
->disconnect();
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
);
187 printf("%s %s ", elem
->name
, AllDataType::getSQLString(info
->type
));
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
);
198 table
->printSQLIndexString();
200 char sqlstring
[1024];
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
);
207 rv
= stmt
->execute(rows
);
211 tuple
= stmt
->fetchAndPrint(true);
212 if (tuple
== NULL
) break;
214 if (rows
% noOfStmts
==0) {
216 sqlconn
->beginTrans();
220 if (rows
% noOfStmts
!=0) { sqlconn
->commit(); printf("COMMIT;\n"); }
225 sqlconn
->disconnect();