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 1. 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
,
49 fieldname
,condition
,field
,dsnName
);
50 if (strcmp (ctablename
, tblName
) == 0) { isCached
=true; break; }
56 SqlConnection
*sqlconn
;
59 int main(int argc
, char **argv
)
61 char username
[IDENTIFIER_LENGTH
];
63 char password
[IDENTIFIER_LENGTH
];
65 char tblName
[IDENTIFIER_LENGTH
];
66 char conditionVal
[IDENTIFIER_LENGTH
];
69 bool exclusive
= false;
70 bool Iscondition
=false;
72 char name
[IDENTIFIER_LENGTH
];
73 while ((c
= getopt(argc
, argv
, "u:p:n:T:c:XS?")) != EOF
) {
75 case 'u' : { strcpy(username
, argv
[optind
- 1]); opt
=1; break; }
76 case 'p' : { strcpy(password
, argv
[optind
- 1]); opt
=1; break; }
77 case 'n' : { noOfStmts
= atoi(argv
[optind
- 1]); opt
= 5; break; }
78 case 'T' : { strcpy(tblName
, argv
[optind
- 1]); opt
= 15; break; }
80 strcpy(conditionVal
,argv
[optind
-1]);
81 Iscondition
=true;break;
83 case 'X' : { exclusive
= true; break; }
84 case 'S' : { schema
= true; break; }
85 case '?' : { opt
= 10; break; } //print help
86 default: opt
=1; //list all the tables
95 //printf("%s %s \n", username, password);
96 if (username
[0] == '\0' ) {
97 strcpy(username
, I_USER
);
98 strcpy(password
, I_PASS
);
100 sqlconn
= (SqlConnection
*) SqlFactory::createConnection(CSqlDirect
);
101 DbRetVal rv
= sqlconn
->connect(username
, password
);
103 printf("Unable to get connection to csql\n");
104 delete sqlconn
; delete stmt
; return 1;
106 stmt
= (SqlStatement
*) SqlFactory::createStatement(CSqlDirect
);
107 stmt
->setSqlConnection(sqlconn
);
108 if (exclusive
) { stmt
->setLoading(true); }
109 if (opt
== 0 || opt
== 1) opt
= 5;
111 os::signal(SIGCSQL1
, SIG_IGN
);
112 DatabaseManagerImpl
*dbMgr
= (DatabaseManagerImpl
*)
113 sqlconn
->getConnObject().getDatabaseManager();
115 printf("Unable to retrive db manager\n");
116 sqlconn
->disconnect(); delete stmt
; delete sqlconn
; return 2;
118 List tableList
= dbMgr
->getAllTableNames();
119 ListIterator iter
= tableList
.getIterator();
120 Identifier
*elem
= NULL
;
122 while (iter
.hasElement()) {
123 elem
= (Identifier
*) iter
.nextElement();
124 //if (!exclusive && isCached(elem->name)) continue;
125 printf("CREATE TABLE %s (", elem
->name
);
126 Table
*table
= dbMgr
->openTable(elem
->name
);
128 printError(ErrSysInternal
,
129 "Unable to open table %s", elem
->name
);
132 FieldInfo
*info
= new FieldInfo();
133 List fNameList
= table
->getFieldNameList();
134 ListIterator fNameIter
= fNameList
.getIterator();
136 bool firstField
=true;
137 Identifier
*elem1
= NULL
;
138 char fieldName
[IDENTIFIER_LENGTH
];
139 while (fNameIter
.hasElement()) {
140 elem1
= (Identifier
*) fNameIter
.nextElement();
141 Table::getFieldNameAlone(elem1
->name
, fieldName
);
142 rv
= table
->getFieldInfo(elem1
->name
, info
);
144 printf("unable to retrive info for table %s\n",
146 sqlconn
->disconnect(); delete stmt
; delete sqlconn
;
150 printf("%s %s ", fieldName
,
151 AllDataType::getSQLString(info
->type
));
154 printf(", %s %s ", fieldName
,
155 AllDataType::getSQLString(info
->type
));
156 if (info
->type
== typeString
) printf("(%d)",info
->length
);
157 if (info
->type
== typeBinary
) printf("(%d)",info
->length
);
158 if (info
->type
== typeVarchar
) printf("(%d)",info
->length
);
159 if (info
->isNull
) printf(" NOT NULL ");
160 if (info
->isDefault
) printf(" DEFAULT '%s' ",
161 info
->defaultValueBuf
);
162 if (info
->isAutoIncrement
) printf(" AUTO_INCREMENT ");
165 while (fNameIter
.hasElement()) {
166 elem1
= (Identifier
*) fNameIter
.nextElement();
170 if (table
->isFKTable()){
171 table
->printSQLForeignString();
174 table
->printSQLIndexString();
176 dbMgr
->closeTable(table
);
179 sqlconn
->disconnect();
180 delete sqlconn
; delete stmt
; return 0;
183 rv = sqlconn->connect(I_USER, I_PASS);
185 printf("unable to connect to csql\n");
191 stmt
->setSqlConnection(sqlconn
);
193 rv
= sqlconn
->getExclusiveLock();
195 printf("Unable to get exclusive lock\n");
196 sqlconn
->disconnect(); delete sqlconn
; delete stmt
; return 4;
200 char sqlstring
[1024]="";
202 while (iter
.hasElement()) {
203 elem
= (Identifier
*) iter
.nextElement();
204 //if (!exclusive && isCached(elem->name)) continue;
206 if (noOfStmts
!=1) printf("SET AUTOCOMMIT OFF;\n");
209 sprintf(sqlstring
, "SELECT * FROM %s;", elem
->name
);
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();
223 if (noOfStmts
!=1) printf("COMMIT;\n");
226 if (rows
% noOfStmts
!=0) {
228 if (noOfStmts
!=1) printf("COMMIT;\n");
234 while (iter
.hasElement()) {
235 elem
= (Identifier
*) iter
.nextElement();
241 os::signal(SIGCSQL1
, SIG_IGN
);
242 DatabaseManagerImpl
*dbMgr
= (DatabaseManagerImpl
*)
243 sqlconn
->getConnObject().getDatabaseManager();
245 printf("Unable to retrive db manager\n");
246 sqlconn
->disconnect(); delete stmt
; delete sqlconn
; return 5;
248 Table
*table
= dbMgr
->openTable(tblName
);
250 printf("csqldump: Table \'%s\' does not exist\n", tblName
);
251 sqlconn
->disconnect(); delete stmt
; delete sqlconn
; return 6;
253 printf("CREATE TABLE %s (", tblName
);
254 FieldInfo
*info
= new FieldInfo();
255 List fNameList
= table
->getFieldNameList();
256 ListIterator fNameIter
= fNameList
.getIterator();
257 bool firstField
=true;
258 Identifier
*elem
= NULL
;
259 char fieldName
[IDENTIFIER_LENGTH
];
260 while (fNameIter
.hasElement()) {
261 elem
= (Identifier
*) fNameIter
.nextElement();
262 Table::getFieldNameAlone(elem
->name
, fieldName
);
263 table
->getFieldInfo((const char*)elem
->name
, info
);
265 printf("%s %s ", fieldName
,
266 AllDataType::getSQLString(info
->type
));
270 printf(", %s %s ", fieldName
,
271 AllDataType::getSQLString(info
->type
));
272 if (info
->type
== typeString
) printf("(%d)",info
->length
);
273 if (info
->type
== typeBinary
) printf("(%d)",info
->length
);
274 if (info
->isNull
) printf(" NOT NULL ");
275 if (info
->isDefault
) printf(" DEFAULT '%s' ",
276 info
->defaultValueBuf
);
279 table
->printSQLIndexString();
281 char sqlstring
[SQL_STMT_LEN
];
282 if (noOfStmts
!= 1) printf("SET AUTOCOMMIT OFF;\n");
285 sprintf(sqlstring
, "SELECT * FROM %s WHERE %s;", tblName
,
288 sprintf(sqlstring
, "SELECT * FROM %s;", tblName
);
289 sqlconn
->beginTrans();
290 rv
= stmt
->prepare(sqlstring
);
292 //***********************************************
294 rv
= stmt
->execute(rows
);
298 tuple
= stmt
->fetchAndPrint(true);
299 if (tuple
== NULL
) break;
301 if (rows
% noOfStmts
==0) {
303 sqlconn
->beginTrans();
304 if (noOfStmts
!=1) printf("COMMIT;\n");
307 if (rows
% noOfStmts
!=0) {
309 if (noOfStmts
!=1) printf("COMMIT;\n");
314 sqlconn
->disconnect();