adding support for mutex from D flag
[csql.git] / src / tools / recover.cxx
blob38d65e3a8d13cd1d53075cdda1d17366541420d9
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
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. *
9 * *
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. *
14 * *
15 ***************************************************************************/
16 #include <os.h>
17 #include<CSql.h>
18 //#include<DatabaseManagerImpl.h>
19 //#include <Statement.h>
20 #include <SqlFactory.h>
21 #include <SqlConnection.h>
22 //#include <SqlNwConnection.h>
23 //#include <SqlNwStatement.h>
24 //#include <readline/readline.h>
25 //#include <readline/history.h>
26 //#define SQL_STMT_LEN 1024
28 //FILE *fp;
30 AbsSqlConnection *conn = NULL;
31 AbsSqlStatement *stmt = NULL;
33 int main(int argc, char **argv)
35 char username[IDENTIFIER_LENGTH];
36 username [0] = '\0';
37 char password[IDENTIFIER_LENGTH];
38 password [0] = '\0';
39 char filename[512];
40 filename [0] ='\0';
41 int c = 0, opt=0;
42 int connOpt = 0;
43 /* while ((c = getopt(argc, argv, "u:p:")) != EOF)
45 switch (c)
47 case 'u' : strcpy(username , argv[optind - 1]); break;
48 case 'p' : strcpy(password , argv[optind - 1]); break;
49 default: printf("Wrong args\n"); exit(1);
52 }*/
53 char schFile[1024];
54 char cmd[1024];
56 Connection dbconn;
57 DbRetVal rv = dbconn.open(I_USER, I_PASS);
58 if (rv != OK) return 1;
59 dbconn.close();
61 sprintf(schFile, "%s/db.chkpt.schema", Conf::config.getDbFile());
62 if (FILE *file = fopen(schFile, "r")) {
63 fclose(file);
64 sprintf(cmd, "csql -X -s %s",schFile);
65 int ret = system(cmd);
66 if (ret != 0) {
67 printf("Tables cannot be recovered. schema file corrupted\n");
68 return 1;
71 conn = SqlFactory::createConnection(CSqlDirect);
72 rv = conn->connect(I_USER, I_PASS);
73 if (rv != OK) {
74 delete conn;
75 return 2;
77 SqlConnection *sqlcon = (SqlConnection *) conn;
78 rv = sqlcon->getExclusiveLock();
79 if (rv != OK) {
80 conn->disconnect();
81 delete conn;
82 return 3;
85 DatabaseManager *dbMgr = conn->getConnObject().getDatabaseManager();
86 dbMgr->recover();
88 conn->disconnect();
89 delete conn;
91 return 0;