Nebula
Loading...
Searching...
No Matches
writer.h
Go to the documentation of this file.
1#ifndef DB_WRITER_H
2#define DB_WRITER_H
3//------------------------------------------------------------------------------
13#include "core/refcounted.h"
14#include "attr/attrid.h"
15#include "db/column.h"
16#include "db/valuetable.h"
17#include "db/database.h"
18
19//------------------------------------------------------------------------------
20namespace Db
21{
23{
25public:
27 Writer();
29 virtual ~Writer();
30
32 void SetDatabase(const Ptr<Database>& db);
34 void SetTableName(const Util::String& n);
36 void AddColumn(const Db::Column& col);
38 void SetFlushTable(bool flushTable);
39
41 bool Open();
43 bool OpenFromValueTable(const Ptr<ValueTable>& values);
45 bool IsOpen() const;
47 void Close();
48
50 void BeginRow();
52 void SetBool(Attr::BoolAttrId id, bool b);
54 void SetInt(Attr::IntAttrId id, int i);
56 void SetFloat(Attr::FloatAttrId id, float f);
58 void SetString(Attr::StringAttrId id, const Util::String& s);
60 void SetVec4(Attr::Vec4AttrId id, const Math::vec4& v);
62 void SetMat4(Attr::Mat4AttrId id, const Math::mat4& m);
64 void SetGuid(Attr::GuidAttrId id, const Util::Guid& guid);
66 void SetBlob(Attr::BlobAttrId id, const Util::Blob& blob);
68 void EndRow();
69
70private:
73
75 bool isOpen;
83};
84
85//------------------------------------------------------------------------------
89inline void
91{
92 this->database = db;
93}
94
95//------------------------------------------------------------------------------
99inline void
101{
102 this->tableName = t;
103}
104
105//------------------------------------------------------------------------------
109inline void
111{
112 n_assert(!this->columnMap.Contains(col.GetAttrId()));
113 this->columns.Append(col);
114 this->columnMap.Add(col.GetAttrId(), this->columns.Size() - 1);
115}
116
117//------------------------------------------------------------------------------
120inline bool
122{
123 return this->isOpen;
124}
125
126//------------------------------------------------------------------------------
129inline void
131{
132 n_assert(this->inBeginRow);
133 this->valueTable->SetBool(id, this->rowIndex, b);
134}
135
136//------------------------------------------------------------------------------
139inline void
141{
142 n_assert(this->inBeginRow);
143 this->valueTable->SetInt(id, this->rowIndex, i);
144}
145
146//------------------------------------------------------------------------------
149inline void
151{
152 n_assert(this->inBeginRow);
153 this->valueTable->SetFloat(id, this->rowIndex, f);
154}
155
156//------------------------------------------------------------------------------
159inline void
161{
162 n_assert(this->inBeginRow);
163 this->valueTable->SetString(id, this->rowIndex, s);
164}
165
166//------------------------------------------------------------------------------
169inline void
171{
172 n_assert(this->inBeginRow);
173 this->valueTable->SetVec4(id, this->rowIndex, v);
174}
175
176//------------------------------------------------------------------------------
179inline void
181{
182 n_assert(this->inBeginRow);
183 this->valueTable->SetMat4(id, this->rowIndex, m);
184}
185
186//------------------------------------------------------------------------------
189inline void
191{
192 n_assert(this->inBeginRow);
193 this->valueTable->SetGuid(id, this->rowIndex, guid);
194}
195
196//------------------------------------------------------------------------------
199inline void
201{
202 n_assert(this->inBeginRow);
203 this->valueTable->SetBlob(id, this->rowIndex, blob);
204}
205
206//------------------------------------------------------------------------------
209inline void
211{
212 this->flushTable = flush;
213}
214
215}; // namespace Db
216//------------------------------------------------------------------------------
217#endif
An attribute ID is used to carry attribute types (no values) around.
Definition attrid.h:20
Typed attribute id for blob type.
Definition blobattrid.h:18
Typed attribute id for bool type.
Definition boolattrid.h:18
Typed attribute id for float type.
Definition floatattrid.h:18
Typed attribute id for guid type.
Definition guidattrid.h:18
Typed attribute id for integer type.
Definition intattrid.h:18
Typed attribute id for mat4 type.
Definition mat4attrid.h:18
Typed attribute id for string type.
Definition stringattrid.h:18
Typed attribute id for vec4 type.
Definition vec4attrid.h:18
The common base class of Nebula.
Definition refcounted.h:38
Describes a column in a database table.
Definition column.h:23
const Attr::AttrId & GetAttrId() const
get attribute id
Definition column.h:77
A wrapper class to bulk-write data to the database in a simple way.
Definition writer.h:23
void SetGuid(Attr::GuidAttrId id, const Util::Guid &guid)
set guid attribute in current row
Definition writer.h:190
IndexT rowIndex
Definition writer.h:82
void SetDatabase(const Ptr< Database > &db)
set database object
Definition writer.h:90
bool isOpen
Definition writer.h:75
bool inBeginRow
Definition writer.h:76
void SetInt(Attr::IntAttrId id, int i)
set int attribute in current row
Definition writer.h:140
void Close()
close the writer - write to DB
Definition writer.cc:106
void SetBlob(Attr::BlobAttrId id, const Util::Blob &blob)
set blob attribute in current row
Definition writer.h:200
__DeclareClass(Writer)
void SetMat4(Attr::Mat4AttrId id, const Math::mat4 &m)
set mat4 attribute in current row
Definition writer.h:180
virtual ~Writer()
destructor
Definition writer.cc:31
void SetBool(Attr::BoolAttrId id, bool b)
set bool attribute in current row
Definition writer.h:130
void BeginRow()
begin writing a new row
Definition writer.cc:177
IndexT FindAttrIndex(Attr::AttrId id) const
check if attribute exists in current row
void SetFlushTable(bool flushTable)
set true if the table should be deleted before writing new data
Definition writer.h:210
void EndRow()
end current row
Definition writer.cc:192
Ptr< Database > database
Definition writer.h:74
Writer()
constructor
Definition writer.cc:19
void AddColumn(const Db::Column &col)
add a column definition
Definition writer.h:110
bool flushTable
Definition writer.h:81
void SetTableName(const Util::String &n)
set the database table name
Definition writer.h:100
void SetFloat(Attr::FloatAttrId id, float f)
set float attribute in current row
Definition writer.h:150
bool Open()
open the writer
Definition writer.cc:44
void SetString(Attr::StringAttrId id, const Util::String &s)
set string attribute in current row
Definition writer.h:160
Util::Dictionary< Attr::AttrId, IndexT > columnMap
Definition writer.h:79
Ptr< ValueTable > valueTable
Definition writer.h:80
void SetVec4(Attr::Vec4AttrId id, const Math::vec4 &v)
set vec4 attribute in current row
Definition writer.h:170
bool IsOpen() const
return true if open
Definition writer.h:121
Util::Array< Column > columns
Definition writer.h:78
bool OpenFromValueTable(const Ptr< ValueTable > &values)
special case: open from existing value table
Definition writer.cc:67
Util::String tableName
Definition writer.h:77
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
The Util::Blob class encapsulates a chunk of raw memory into a C++ object which can be copied,...
Definition blob.h:22
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
IndexT Add(const KeyValuePair< KEYTYPE, VALUETYPE > &kvp)
add a key/value pair
Definition dictionary.h:267
Implements a GUID.
#define n_assert(exp)
Definition debug.h:50
Definition column.cc:10
A 4x4 single point precision float matrix.
Definition mat4.h:49
A 4D vector.
Definition vec4.h:24
Nebula's universal string class.
Definition string.h:50
int IndexT
Definition types.h:48