windows changes
[csql.git] / src / storage / Connection.cxx
blob9089b764f498edacc9909ff2ce24d1e85eb18a74
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<SessionImpl.h>
17 #include<Debug.h>
18 #include<os.h>
19 #include<Index.h>
20 #include<Config.h>
22 Connection::~Connection()
24 if (NULL != session) {
25 session->rollback();
26 //session->close();
27 delete session;
28 session = NULL;
30 Index::destroy();
32 char *Connection::getUserName()
34 return session->getUserName();
36 DbRetVal Connection::open(const char *username, const char *password)
38 os::umask(002);
39 if (username == NULL || password == NULL )
41 printError(ErrBadArg, "Username or password should not be NULL\n");
42 return ErrBadArg;
44 if (strlen(username) > IDENTIFIER_LENGTH || strlen(password) > IDENTIFIER_LENGTH) return ErrBadArg;
45 if (session == NULL) session = new SessionImpl();
46 else
48 printError(ErrAlready, "User already logged in");
49 return ErrAlready;
51 DbRetVal rv = session->open(username, password);
52 if (rv != OK) { delete session; session = NULL; return rv; }
53 rv = Conf::logger.startLogger(Conf::config.getLogFile());
54 if (rv != OK) { delete session; session = NULL; return rv; }
55 logFine(Conf::logger, "User logged in %s",username);
56 Index::init();
57 return OK;
60 DbRetVal Connection::close()
62 if (session == NULL) return ErrNoConnection;
63 logFine(Conf::logger, "User logged out");
64 Conf::logger.stopLogger();
65 session->rollback();
66 delete session; // this inturn calls session->close
67 session = NULL;
68 return OK;
71 DatabaseManager* Connection::getDatabaseManager()
73 if (session == NULL) return NULL;
74 return session->getDatabaseManager();
77 UserManager* Connection::getUserManager()
79 if (session == NULL) return NULL;
80 return session->getUserManager();
83 DbRetVal Connection::startTransaction(IsolationLevel level)
85 if (session == NULL) return ErrNoConnection;
86 if (level == WRITE_OSYNC) level = READ_REPEATABLE;
87 return session->startTransaction(level);
91 DbRetVal Connection::commit()
93 if (session == NULL) return ErrNoConnection;
94 return session->commit();
98 DbRetVal Connection::rollback()
100 if (session == NULL) return ErrNoConnection;
101 return session->rollback();
103 DbRetVal Connection::getExclusiveLock()
105 if (session == NULL) return ErrNoConnection;
106 return session->getExclusiveLock();