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 os::signal(SIGCSQL1
, SIG_IGN
);
109 DatabaseManagerImpl
*dbMgr
= (DatabaseManagerImpl
*) conn
.getDatabaseManager();
111 printf("Unable to retrive db manager\n");
117 List tableList
= dbMgr
->getAllTableNames();
118 ListIterator iter
= tableList
.getIterator();
119 Identifier
*elem
= NULL
;
121 while (iter
.hasElement()) {
122 elem
= (Identifier
*) iter
.nextElement();
123 //if (!exclusive && isCached(elem->name)) continue;
124 printf("CREATE TABLE %s (", elem
->name
);
125 Table
*table
= dbMgr
->openTable(elem
->name
);
127 printError(ErrSysInternal
, "Unable to open table %s", elem
->name
);
130 FieldInfo
*info
= new FieldInfo();
131 List fNameList
= table
->getFieldNameList();
132 ListIterator fNameIter
= fNameList
.getIterator();
134 bool firstField
=true;
135 Identifier
*elem1
= NULL
;
136 char fieldName
[IDENTIFIER_LENGTH
];
137 while (fNameIter
.hasElement()) {
138 elem1
= (Identifier
*) fNameIter
.nextElement();
139 Table::getFieldNameAlone(elem1
->name
, fieldName
);
140 rv
= table
->getFieldInfo(elem1
->name
, info
);
142 printf("unable to retrive info for table %s\n", elem1
->name
);
148 printf("%s %s ", fieldName
, AllDataType::getSQLString(info
->type
));
151 printf(", %s %s ", fieldName
, AllDataType::getSQLString(info
->type
));
152 if (info
->type
== typeString
) printf("(%d)",info
->length
);
153 if (info
->type
== typeBinary
) printf("(%d)",info
->length
);
154 if (info
->type
== typeVarchar
) printf("(%d)",info
->length
);
155 if (info
->isNull
) printf(" NOT NULL ");
156 if (info
->isDefault
) printf(" DEFAULT '%s' ", info
->defaultValueBuf
);
157 if (info
->isAutoIncrement
) printf(" AUTO_INCREMENT ");
160 while (fNameIter
.hasElement()) {
161 elem1
= (Identifier
*) fNameIter
.nextElement();
165 if (table
->isFKTable()){
166 table
->printSQLForeignString();
169 table
->printSQLIndexString();
171 dbMgr
->closeTable(table
);
174 if (schema
) { delete sqlconn
; delete stmt
; return 0; }
176 rv
= sqlconn
->connect(I_USER
, I_PASS
);
178 printf("unable to connect to csql\n");
183 stmt
->setConnection(sqlconn
);
185 rv
= sqlconn
->getExclusiveLock();
187 printf("Unable to get exclusive lock\n");
188 sqlconn
->disconnect();
195 char sqlstring
[1024]="";
197 while (iter
.hasElement()) {
198 elem
= (Identifier
*) iter
.nextElement();
199 //if (!exclusive && isCached(elem->name)) continue;
200 if (!flag
) { printf("SET AUTOCOMMIT OFF;\n"); flag
=true; }
201 sprintf(sqlstring
, "SELECT * FROM %s;", elem
->name
);
202 sqlconn
->beginTrans();
203 DbRetVal rv
= stmt
->prepare(sqlstring
);
205 rv
= stmt
->execute(rows
);
209 tuple
= stmt
->fetchAndPrint(true);
210 if (tuple
== NULL
) break;
212 if (rows
% noOfStmts
==0) {
214 sqlconn
->beginTrans();
218 if (rows
% noOfStmts
!=0) {
226 while (iter
.hasElement()) {
227 elem
= (Identifier
*) iter
.nextElement();
234 DbRetVal rv
= conn
.open(username
, password
);
236 printf("Unable to get connection to csql\n");
241 os::signal(SIGCSQL1
, SIG_IGN
);
242 DatabaseManagerImpl
*dbMgr
= (DatabaseManagerImpl
*) conn
.getDatabaseManager();
243 if (dbMgr
== NULL
) { printf("Auth failed\n"); return 2;}
244 Table
*table
= dbMgr
->openTable(tblName
);
246 printf("csqldump: Table \'%s\' does not exist\n", tblName
);
252 printf("CREATE TABLE %s (", tblName
);
253 FieldInfo
*info
= new FieldInfo();
254 List fNameList
= table
->getFieldNameList();
255 ListIterator fNameIter
= fNameList
.getIterator();
256 bool firstField
=true;
257 Identifier
*elem
= NULL
;
258 char fieldName
[IDENTIFIER_LENGTH
];
259 while (fNameIter
.hasElement()) {
260 elem
= (Identifier
*) fNameIter
.nextElement();
261 Table::getFieldNameAlone(elem
->name
, fieldName
);
262 table
->getFieldInfo((const char*)elem
->name
, info
);
264 printf("%s %s ", fieldName
, AllDataType::getSQLString(info
->type
));
268 printf(", %s %s ", fieldName
, AllDataType::getSQLString(info
->type
));
269 if (info
->type
== typeString
) printf("(%d)",info
->length
);
270 if (info
->type
== typeBinary
) printf("(%d)",info
->length
);
271 if (info
->isNull
) printf(" NOT NULL ");
272 if (info
->isDefault
) printf(" DEFAULT '%s' ", info
->defaultValueBuf
);
275 table
->printSQLIndexString();
278 char sqlstring
[1024];
279 // char sqlstring1[1024];
281 if (!flag
) { printf("SET AUTOCOMMIT OFF;\n"); flag
=true; }
282 rv
= sqlconn
->connect(I_USER
, I_PASS
);
284 printf("unable to connect\n");
287 stmt
->setConnection(sqlconn
);
290 sprintf(sqlstring
, "SELECT * FROM %s WHERE %s;", tblName
,conditionVal
);
292 sprintf(sqlstring
, "SELECT * FROM %s;", tblName
);
293 sqlconn
->beginTrans();
294 rv
= stmt
->prepare(sqlstring
);
296 //***********************************************
298 rv
= stmt
->execute(rows
);
302 tuple
= stmt
->fetchAndPrint(true);
303 if (tuple
== NULL
) break;
305 if (rows
% noOfStmts
==0) {
307 sqlconn
->beginTrans();
311 if (rows
% noOfStmts
!=0) { sqlconn
->commit(); printf("COMMIT;\n"); }
315 sqlconn
->disconnect();