Nebula
Loading...
Searching...
No Matches
table.h
Go to the documentation of this file.
1#pragma once
2#ifndef DB_TABLE_H
3#define DB_TABLE_H
4//------------------------------------------------------------------------------
19#include "core/refcounted.h"
20#include "db/column.h"
21#include "util/array.h"
22#include "util/string.h"
23#include "util/dictionary.h"
24#include "util/hashtable.h"
25#include "db/valuetable.h"
26
27//------------------------------------------------------------------------------
28namespace Db
29{
30class Database;
31class Dataset;
32
33class Table : public Core::RefCounted
34{
36public:
44
46 Table();
48 virtual ~Table();
49
51 virtual void SetName(const Util::String& n);
53 const Util::String& GetName() const;
55 bool IsConnected() const;
56
58 const Ptr<Database>& GetDatabase() const;
60 virtual void CreateMultiColumnIndex(const Util::Array<Attr::AttrId>& columnIds);
63
65 virtual void CommitChanges(bool resetModifiedState=true, bool useTransaction=true);
67 bool HasUncommittedColumns() const;
69 virtual void CommitUncommittedColumns();
71 virtual void CommitDeletedRows();
72
74 void AddColumn(const Column& c);
76 SizeT GetNumColumns() const;
78 const Column& GetColumn(IndexT i) const;
80 bool HasColumn(const Attr::AttrId& id) const;
82 bool HasColumn(const Util::String& name) const;
84 bool HasColumn(const Util::FourCC& fcc) const;
86 const Column& GetColumn(const Attr::AttrId& id) const;
88 const Column& GetColumn(const Util::String& name) const;
90 const Column& GetColumn(const Util::FourCC& fcc) const;
92 const Util::Array<Column>& GetColumns() const;
94 bool HasPrimaryColumn() const;
96 const Column& GetPrimaryColumn() const;
97
98protected:
99 friend class Database;
100 friend class Dataset;
101 friend class Writer;
102
104 virtual void Connect(const Ptr<Database>& db, ConnectMode connectMode=Default, bool ignoreUnknownColumns=false);
106 virtual void Disconnect(bool dropTable=false);
108 virtual void BindValueTable(const Ptr<ValueTable>& valueTable);
110 virtual void UnbindValueTable();
111
121};
122
123//------------------------------------------------------------------------------
126inline
127const Ptr<Database>&
129{
130 return this->database;
131}
132
133//------------------------------------------------------------------------------
136inline
137bool
139{
140 return (InvalidIndex != this->primaryColumnIndex);
141}
142
143//------------------------------------------------------------------------------
146inline
147const Column&
149{
151 return this->columns[this->primaryColumnIndex];
152}
153
154//------------------------------------------------------------------------------
157inline
158bool
160{
161 return this->isConnected;
162}
163
164//------------------------------------------------------------------------------
167inline
168const Util::String&
170{
171 return this->name;
172}
173
174//------------------------------------------------------------------------------
177inline
178SizeT
180{
181 return this->columns.Size();
182}
183
184//------------------------------------------------------------------------------
187inline
188const Column&
190{
191 return this->columns[i];
192}
193
194//------------------------------------------------------------------------------
197inline
198bool
200{
201 return this->attrIdIndexMap.Contains(id);
202}
203
204//------------------------------------------------------------------------------
207inline
208bool
210{
211 return this->nameIndexMap.Contains(name);
212}
213
214//------------------------------------------------------------------------------
217inline
218bool
220{
221 return this->fourccIndexMap.Contains(fcc);
222}
223
224//------------------------------------------------------------------------------
227inline
228const Column&
230{
231 n_assert(this->HasColumn(id));
232 return this->columns[this->attrIdIndexMap[id]];
233}
234
235//------------------------------------------------------------------------------
238inline
239const Column&
241{
242 n_assert(this->HasColumn(name));
243 return this->columns[this->nameIndexMap[name]];
244}
245
246//------------------------------------------------------------------------------
249inline
250const Column&
252{
253 n_assert(this->HasColumn(fcc));
254 return this->columns[this->fourccIndexMap[fcc]];
255}
256
257//------------------------------------------------------------------------------
260inline
263{
264 return this->columns;
265}
266
267} // namespace Db
268//------------------------------------------------------------------------------
269#endif
An attribute ID is used to carry attribute types (no values) around.
Definition attrid.h:20
The common base class of Nebula.
Definition refcounted.h:38
Describes a column in a database table.
Definition column.h:23
Wraps an entire database into a C++ object.
Definition database.h:30
A dataset is an efficient in-memory-cache for rlational database data.
Definition dataset.h:43
Describes a table in a database, or more abstract, a set of typed columns grouped under a common name...
Definition table.h:34
bool IsConnected() const
return true if the table is connected to a database
Definition table.h:159
const Util::String & GetName() const
get name of table
Definition table.h:169
virtual void Connect(const Ptr< Database > &db, ConnectMode connectMode=Default, bool ignoreUnknownColumns=false)
connect the table object with a database
Definition table.cc:92
virtual void CreateMultiColumnIndex(const Util::Array< Attr::AttrId > &columnIds)
create a complex multicolumn index on a connected table
Definition table.cc:122
const Column & GetColumn(IndexT i) const
get column at index
Definition table.h:189
virtual Ptr< Dataset > CreateDataset()
create a dataset associated with this table
Definition table.cc:44
Util::Dictionary< Attr::AttrId, IndexT > attrIdIndexMap
Definition table.h:119
bool HasUncommittedColumns() const
check if there are any uncommitted columns in the table
Definition table.cc:174
virtual ~Table()
destructor
Definition table.cc:31
bool isConnected
Definition table.h:112
bool HasPrimaryColumn() const
return true if a primary key column exists
Definition table.h:138
virtual void BindValueTable(const Ptr< ValueTable > &valueTable)
bind a value table to the table object, unbinds previous table
Definition table.cc:148
const Ptr< Database > & GetDatabase() const
get pointer to database this table is connected to
Definition table.h:128
Ptr< ValueTable > valueTable
Definition table.h:114
SizeT GetNumColumns() const
get number of columns in table
Definition table.h:179
IndexT primaryColumnIndex
Definition table.h:120
virtual void CommitChanges(bool resetModifiedState=true, bool useTransaction=true)
commit any changes (add columns, insert new rows, update modified rows)
Definition table.cc:135
bool HasColumn(const Attr::AttrId &id) const
return true if a column exists by attribute id
Definition table.h:199
Util::HashTable< Util::String, IndexT > nameIndexMap
Definition table.h:117
Util::Dictionary< Util::FourCC, IndexT > fourccIndexMap
Definition table.h:118
Util::String name
Definition table.h:115
ConnectMode
connection modes
Definition table.h:39
@ Default
Definition table.h:42
@ ForceCreate
Definition table.h:40
@ AssumeExists
Definition table.h:41
virtual void CommitDeletedRows()
commit deleted rows only
Definition table.cc:204
const Util::Array< Column > & GetColumns() const
get all columns in the table
Definition table.h:262
Ptr< Database > database
Definition table.h:113
virtual void Disconnect(bool dropTable=false)
disconnect the table from a database, optionally remove table from database as well
Definition table.cc:109
void AddColumn(const Column &c)
add a column to the table
Definition table.cc:57
virtual void CommitUncommittedColumns()
commit uncommitted columns only
Definition table.cc:194
virtual void UnbindValueTable()
unbind current value table from the table object, commits any changes to the database
Definition table.cc:161
virtual void SetName(const Util::String &n)
set name of table
Definition table.cc:75
__DeclareClass(Table)
Util::Array< Column > columns
Definition table.h:116
Table()
constructor
Definition table.cc:21
const Column & GetPrimaryColumn() const
get the primary key column, fails hard if none exists
Definition table.h:148
A wrapper class to bulk-write data to the database in a simple way.
Definition writer.h:23
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
A collection of key/value pairs with quick value retrieval by key at roughly O(log n).
Definition dictionary.h:34
bool Contains(const KEYTYPE &key) const
return true if key exists in the array
Definition dictionary.h:368
A four-character-code is a quasi-human-readable 32-bit-id.
Definition fourcc.h:19
Organizes key/value pairs by a hash code.
Definition hashtable.h:42
bool Contains(const KEYTYPE &key) const
return true if key exists in the array
Definition hashtable.h:552
#define n_assert(exp)
Definition debug.h:50
Definition column.cc:10
Nebula's universal string class.
Definition string.h:50
static const int InvalidIndex
Definition types.h:54
int SizeT
Definition types.h:49
int IndexT
Definition types.h:48