Nebula
Loading...
Searching...
No Matches
database.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
12#include "core/refcounted.h"
13#include "io/uri.h"
14#include "util/dictionary.h"
15#include "db/table.h"
16#include "db/command.h"
17
18//------------------------------------------------------------------------------
19namespace Attr
20{
21 DeclareString(AttrName, 'ATNA', Attr::ReadWrite);
22 DeclareString(AttrType, 'ATTY', Attr::ReadWrite);
23 DeclareBool(AttrReadWrite, 'ATRW', Attr::ReadWrite);
24 DeclareBool(AttrDynamic, 'ATDY', Attr::ReadWrite);
25}
26//------------------------------------------------------------------------------
27namespace Db
28{
30{
32public:
35 {
37 ReadWriteExisting, // fail if database doesn't exist
38 ReadWriteCreate, // create new database if doesn't exist
39 };
40
42 Database();
44 virtual ~Database();
45
47 void SetURI(const IO::URI& uri);
49 const IO::URI& GetURI() const;
55 void SetExclusiveMode(bool b);
57 bool GetExclusiveMode() const;
59 void SetIgnoreUnknownColumns(bool b);
61 bool GetIgnoreUnknownColumns() const;
62
64 virtual bool Open();
66 virtual void Close();
68 bool IsOpen() const;
70 const Util::String& GetError() const;
71
73 virtual bool AttachDatabase(const IO::URI& uri, const Util::String& dbName);
75 virtual void DetachDatabase(const Util::String& dbName);
77 virtual void BeginTransaction();
79 virtual void EndTransaction();
81 virtual void AddTable(const Ptr<Table>& table);
83 virtual void DeleteTable(const Util::String& tableName);
85 bool HasTable(const Util::String& tableName) const;
87 SizeT GetNumTables() const;
89 const Ptr<Table>& GetTableByIndex(IndexT i) const;
91 const Ptr<Table>& GetTableByName(const Util::String& tableName) const;
92
94 void SetInMemoryDatabase(bool b);
96 virtual void CopyInMemoryDatabaseToFile(const IO::URI& fileUri);
97
98protected:
100 void SetError(const Util::String& error);
102 IndexT FindTableIndex(const Util::String& tableName) const;
103
104 bool isOpen;
111 Util::Array<Ptr<Table> > tables; // NOTE: not in a Dictionary, because name may change externally!
112};
113
114//------------------------------------------------------------------------------
117inline void
122
123//------------------------------------------------------------------------------
126inline bool
128{
129 return this->ignoreUnknownColumns;
130}
131
132//------------------------------------------------------------------------------
135inline void
137{
138 this->error = err;
139}
140
141//------------------------------------------------------------------------------
144inline const Util::String&
146{
147 return this->error;
148}
149
150//------------------------------------------------------------------------------
153inline bool
155{
156 return this->isOpen;
157}
158
159//------------------------------------------------------------------------------
162inline void
164{
165 this->uri = u;
166}
167
168//------------------------------------------------------------------------------
171inline const IO::URI&
173{
174 return this->uri;
175}
176
177//------------------------------------------------------------------------------
180inline void
185
186//------------------------------------------------------------------------------
191{
192 return this->accessMode;
193}
194
195//------------------------------------------------------------------------------
198inline void
200{
201 this->memoryDatabase = b;
202}
203
204//------------------------------------------------------------------------------
207inline void
209{
210 this->exclusiveMode = b;
211}
212
213//------------------------------------------------------------------------------
216inline bool
218{
219 return this->exclusiveMode;
220}
221} // namespace Db
222//------------------------------------------------------------------------------
#define DeclareString(NAME, FOURCC, ACCESSMODE)
Definition attributedefinition.h:94
#define DeclareBool(NAME, FOURCC, ACCESSMODE)
Definition attributedefinition.h:78
The common base class of Nebula.
Definition refcounted.h:38
Wraps an entire database into a C++ object.
Definition database.h:30
virtual void BeginTransaction()
begin a transaction on the database
Definition database.cc:82
virtual void AddTable(const Ptr< Table > &table)
create a new table in the database
Definition database.cc:107
const IO::URI & GetURI() const
get the database URI
Definition database.h:172
void SetURI(const IO::URI &uri)
set the data source URI (usually filename of database file)
Definition database.h:163
virtual void DeleteTable(const Util::String &tableName)
delete a table from the database
Definition database.cc:122
virtual void Close()
close the database
Definition database.cc:62
bool ignoreUnknownColumns
Definition database.h:105
IO::URI uri
Definition database.h:106
Util::String error
Definition database.h:107
bool GetExclusiveMode() const
get exclusive mode
Definition database.h:217
virtual void DetachDatabase(const Util::String &dbName)
detach an attached database
Definition database.cc:221
bool GetIgnoreUnknownColumns() const
get ignore unknown columns flag
Definition database.h:127
const Ptr< Table > & GetTableByIndex(IndexT i) const
get table by index
Definition database.cc:180
void SetError(const Util::String &error)
set error string
Definition database.h:136
IndexT FindTableIndex(const Util::String &tableName) const
find a table index by name
Definition database.cc:136
bool IsOpen() const
return true if database is open
Definition database.h:154
AccessMode GetAccessMode() const
get access mode
Definition database.h:190
virtual bool AttachDatabase(const IO::URI &uri, const Util::String &dbName)
attach another database to the current database
Definition database.cc:209
virtual void EndTransaction()
end a transaction on the database
Definition database.cc:92
bool HasTable(const Util::String &tableName) const
return true if a table exists by name
Definition database.cc:157
Util::Array< Ptr< Table > > tables
Definition database.h:111
AccessMode
access modes
Definition database.h:35
@ ReadWriteCreate
Definition database.h:38
@ ReadOnly
Definition database.h:36
@ ReadWriteExisting
Definition database.h:37
AccessMode accessMode
Definition database.h:108
Database()
constructor
Definition database.cc:25
__DeclareClass(Database)
SizeT GetNumTables() const
get number of tables in the database
Definition database.cc:168
void SetIgnoreUnknownColumns(bool b)
ignore unknown columns (not existing as attribute), yes/no
Definition database.h:118
bool isOpen
Definition database.h:104
bool exclusiveMode
Definition database.h:110
void SetAccessMode(AccessMode m)
set the access mode (ReadOnly or ReadWrite)
Definition database.h:181
virtual ~Database()
destructor
Definition database.cc:38
void SetExclusiveMode(bool b)
enable/disable exclusive mode
Definition database.h:208
bool memoryDatabase
Definition database.h:109
void SetInMemoryDatabase(bool b)
set in-memory database only
Definition database.h:199
virtual bool Open()
open the database
Definition database.cc:49
const Ptr< Table > & GetTableByName(const Util::String &tableName) const
get table by name
Definition database.cc:192
virtual void CopyInMemoryDatabaseToFile(const IO::URI &fileUri)
copy in memory database to file
Definition database.cc:232
const Util::String & GetError() const
get last error string
Definition database.h:145
An URI object can split a Uniform Resource Identifier string into its components or build a string fr...
Definition uri.h:67
Nebula's smart pointer class which manages the life time of RefCounted objects.
Definition ptr.h:38
Nebula's dynamic array class.
Definition array.h:60
Definition accessmode.h:13
@ ReadWrite
Definition accessmode.h:17
Definition column.cc:10
Nebula's universal string class.
Definition string.h:50
int SizeT
Definition types.h:49
int IndexT
Definition types.h:48