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");
39 char ctablename
[IDENTIFIER_LENGTH
];
40 char fieldname
[IDENTIFIER_LENGTH
];
41 char condition
[IDENTIFIER_LENGTH
];
42 char field
[IDENTIFIER_LENGTH
];
47 fscanf(fp
, "%d:%s %s %s %s\n", &mode
, ctablename
,fieldname
,condition
,field
);
48 if (strcmp (ctablename
, tblName
) == 0) { isCached
=true; break; }
53 int main(int argc
, char **argv
)
55 char username
[IDENTIFIER_LENGTH
];
57 char password
[IDENTIFIER_LENGTH
];
59 char tblName
[IDENTIFIER_LENGTH
];
62 char name
[IDENTIFIER_LENGTH
];
63 while ((c
= getopt(argc
, argv
, "u:p:n:T:?")) != EOF
)
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
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
);
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;
100 List tableList
= dbMgr
->getAllTableNames();
101 ListIterator iter
= tableList
.getIterator();
102 Identifier
*elem
= NULL
;
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();
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
;
122 printf("%s %s ", fieldName
, AllDataType::getSQLString(info
->type
));
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
);
133 table
->printSQLIndexString();
135 dbMgr
->closeTable(table
);
138 char sqlstring
[1024];
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
);
148 rv
= stmt
->execute(rows
);
152 tuple
= stmt
->fetchAndPrint(true);
153 if (tuple
== NULL
) break;
155 if (rows
% noOfStmts
==0) {
157 sqlconn
->beginTrans();
161 if (rows
% noOfStmts
!=0) { sqlconn
->commit(); printf("COMMIT;\n"); }
166 sqlconn
->disconnect();
172 Table
*table
= dbMgr
->openTable(tblName
);
174 printf("csqldump: Table \'%s\' does not exist\n", tblName
);
176 sqlconn
->disconnect();
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
);
193 printf("%s %s ", fieldName
, AllDataType::getSQLString(info
->type
));
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
);
204 table
->printSQLIndexString();
206 char sqlstring
[1024];
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
);
213 rv
= stmt
->execute(rows
);
217 tuple
= stmt
->fetchAndPrint(true);
218 if (tuple
== NULL
) break;
220 if (rows
% noOfStmts
==0) {
222 sqlconn
->beginTrans();
226 if (rows
% noOfStmts
!=0) { sqlconn
->commit(); printf("COMMIT;\n"); }
231 sqlconn
->disconnect();