code reorg
[csql.git] / src / server / Connection.cxx
blobb5ef9954b50f7db13e116f456382be0860f21a15
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>
21 Connection::~Connection()
23 if (NULL != session) {
24 session->rollback();
25 //session->close();
26 delete session;
27 session = NULL;
29 Index::destroy();
32 DbRetVal Connection::open(const char *username, const char *password)
34 if (username == NULL || password == NULL )
36 printError(ErrBadArg, "Username or password should not be NULL\n");
37 return ErrBadArg;
39 if (strlen(username) > 64 || strlen(password) >64) return ErrBadArg;
40 if (session == NULL) session = new SessionImpl();
41 else
43 printError(ErrAlready, "User already logged in");
44 return ErrAlready;
46 DbRetVal rv = session->open(username, password);
47 if (rv != OK) { delete session; session = NULL; return rv; }
48 rv = logger.startLogger(Conf::config.getLogFile());
49 if (rv != OK) { delete session; session = NULL; return rv; }
50 logFinest(logger, "User logged in %s",username);
51 Index::init();
52 return OK;
55 DbRetVal Connection::close()
57 if (session == NULL) return ErrNoConnection;
58 logFinest(logger, "User logged out");
59 logger.stopLogger();
60 session->rollback();
61 delete session; // this inturn calls session->close
62 session = NULL;
63 return OK;
66 DatabaseManager* Connection::getDatabaseManager()
68 if (session == NULL) return NULL;
69 return session->getDatabaseManager();
72 UserManager* Connection::getUserManager()
74 if (session == NULL) return NULL;
75 return session->getUserManager();
78 DbRetVal Connection::startTransaction(IsolationLevel level)
80 if (session == NULL) return ErrNoConnection;
81 if (level == WRITE_OSYNC) level = READ_REPEATABLE;
82 return session->startTransaction(level);
86 DbRetVal Connection::commit()
88 if (session == NULL) return ErrNoConnection;
89 return session->commit();
93 DbRetVal Connection::rollback()
95 if (session == NULL) return ErrNoConnection;
96 return session->rollback();