summaryrefslogtreecommitdiffstats
path: root/reladminif/databaseif.hh
blob: f8b8954f5a270398ade659952183307f47ee6b52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/
#ifndef _DATABASEIF_HH_
#define _DATABASEIF_HH_

/************************************************************************
 *
 *
 * PURPOSE:
 *   
 *
 * COMMENTS:
 *
 ***********************************************************************/

//@ManMemo: Module: {\bf reladminif}.
/*@Doc:
With a DatabaseIf instance a database can be opened or closed. There
is also functionality for creating and deleting DBs. A
database has to be open, before persistence capable classes can be
used (see also \Ref{AdminIf}).

{\bf Example}

{\tt  DatabaseIf database;}

{\tt database.open( "myDatabase" )}

...

{\tt database.close();}
*/

class DatabaseIf;
class TransactionIf;

#include <iostream>
using std::cout;
using std::endl;
using std::ostream;

#include "raslib/error.hh"

class DatabaseIf              
	{
	public:
		friend std::ostream& operator<< (std::ostream& stream, DatabaseIf& db);
		/*@Doc: 
		prints the status of the database (connected, online, offline, name)
		*/
		
		/// opens database with name {\tt dbName}.
		void open( const char* dbName ) throw(r_Error);
		/*@Doc: 
			Precondition: not opened, not connected, db exists
			Postcondition: open, not connected, db exists
			If last opened database was not closed (throw r_Error::r_Error_DatabaseOpen)),
			If database does not exist (throw r_Error::r_Error_DatabaseUnknown). 
			In the current implementation this value is returned, when dbName is not RASBASE,
			regardles if the db exists or not.
		*/

		void close();
		/*@Doc: 
			Precondition: open, not connected, db exists
			Postcondition: not open, not connected, db exists
			closes currently opened database.  only frees name and sets connected/opened to false.
		*/

		static void createDB( const char* dbName, const char* schemaName, const char* volumeName=0 ) throw(r_Error);
		/*@Doc: 
			Precondition: not open, not connected, db does not exist
			Postcondition: not open, not connected, db exists
			creates a new database.  schemaName and volumeName are ignored.
			only successful if dbName is RASBASE
		*/

		static void destroyDB(const char* dbName) throw(r_Error); 
		/*@Doc:
			Precondition: not open, not connected, db exists
			Postcondition: not open, not connected, db does not exist
			destroys an existing database with name {\tt dbName}.
			Database must not be open in order to be destroyed.
			A transaction must not be opened.  
			Returns -1 if database does not exist.
		*/

		
		void garbage();
		/*@Doc: 
		this method does not do anything
		*/
		
		const char* getName() const;
		/*@Doc: 
		returns a pointer to the name of the db.
		*/
		
		DatabaseIf();
		/*@Doc: 
		constructor.  Initializes opened, myName and connected to 0
		*/

		bool isConnected() const;
		/*@Doc: 
		true when there has been an EXEC SQL CONNECT
		*/

		bool isOpen() const;
		/*@Doc: 
		true when it was opened by a transaction
		*/

		~DatabaseIf();
		/*@Doc: 
		executes a baseDBMSClose() if it is still connected.
		*/
		
		static bool databaseExists(const char* dbname) throw (r_Error);
		/*@Doc: 
			Precondition: none checked.  db must be open and connected.
			Postcondition: none
			basedbms error thrown.
			checks if a database has been created.
		*/

		static bool isConsistent() throw (r_Error);
		/*@Doc: 
			Precondition: none checked.  db must be open and connected.
			Postcondition: none
			basedbms error thrown if something really bad happens.
			checks if counters are ok.
		*/

	protected:
		friend class TransactionIf;

		void baseDBMSOpen() throw (r_Error);
		/*@Doc: 
		Precondition: current database = 0
		Postcondition: current database = this
		issues a CONNECT.
		sets the DatabaseIf object in AdminIf to this.
		*/

		void baseDBMSClose();
		/*@Doc: 
			Precondition: current database = this
			Postcondition: current database = 0
			issues a ROLLBACK WORK RELEASE in oracle.
			issues a DISCONNECT in db2.
			sets the DatabaseIf object in AdminIf to 0, if it was the same.
		*/

		static void connect() throw (r_Error);
		/*@Doc:
			Precondition: none checked.
			Postcondition: none.
			issues a CONNECT.
			throws r_Error if there is a problem during connection.
		*/
		
		static void disconnect() throw (r_Error);
		/*@Doc:
			Precondition: none checked.
			Postcondition: none.
			issues a CONNECT.
			throws r_Error if there is a problem during disconnection.
		*/
		
		void checkCompatibility() throw (r_Error);
		/*@Doc:
			Precondition: none checked.
			Postcondition: none.
			throws r_Error if the current rasdaman system does not match the database.
		*/

	private:
		bool opened;
		/*@Doc: 
		TRUE only if database is open.
		*/

		char* myName;
		/*@Doc: 
		Valid only if opened.
		*/

		bool connected;
		/*@Doc: 
		TRUE only if database connection exists ; )
		*/

		static const char* DefaultDatabaseName;
		/*@Doc: 
		only one database is supported.  any database name given is compared to this string.
		access to the db is only granted if the name of the database is the same as this string.
		*/

	};

#endif