2 * Copyright (C) 2005-2013 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "Database/DatabaseEnv.h"
24 #include "DBCFileLoader.h"
28 template<class DerivedLoader
, class StorageClass
> friend class SQLStorageLoaderBase
;
31 char const* GetTableName() const { return m_tableName
; }
32 char const* EntryFieldName() const { return m_entry_field
; }
34 FieldFormat
GetDstFormat(uint32 idx
) const { return (FieldFormat
)m_dst_format
[idx
]; };
35 const char* GetDstFormat() const { return m_dst_format
; };
36 FieldFormat
GetSrcFormat(uint32 idx
) const { return (FieldFormat
)m_src_format
[idx
]; };
37 const char* GetSrcFormat() const { return m_src_format
; };
39 uint32
GetMaxEntry() const { return m_maxEntry
; };
40 uint32
GetRecordCount() const { return m_recordCount
; };
45 friend class SQLStorageBase
;
48 T
const* getValue() const { return reinterpret_cast<T
const*>(pointer
); }
50 void operator ++() { pointer
+= recordSize
; }
51 T
const* operator *() const { return getValue(); }
52 T
const* operator ->() const { return getValue(); }
53 bool operator <(const SQLSIterator
& r
) const { return pointer
< r
.pointer
; }
54 void operator =(const SQLSIterator
& r
) { pointer
= r
.pointer
; recordSize
= r
.recordSize
; }
57 SQLSIterator(char* ptr
, uint32 _recordSize
) : pointer(ptr
), recordSize(_recordSize
) {}
63 SQLSIterator
<T
> getDataBegin() const { return SQLSIterator
<T
>(m_data
, m_recordSize
); }
65 SQLSIterator
<T
> getDataEnd() const { return SQLSIterator
<T
>(m_data
+ m_recordCount
* m_recordSize
, m_recordSize
); }
69 virtual ~SQLStorageBase() { Free(); }
71 void Initialize(const char* tableName
, const char* entry_field
, const char* src_format
, const char* dst_format
);
73 uint32
GetDstFieldCount() const { return m_dstFieldCount
; }
74 uint32
GetSrcFieldCount() const { return m_srcFieldCount
; }
75 uint32
GetRecordSize() const { return m_recordSize
; }
77 virtual void prepareToLoad(uint32 maxRecordId
, uint32 recordCount
, uint32 recordSize
);
78 virtual void JustCreatedRecord(uint32 recordId
, char* record
) = 0;
82 char* createRecord(uint32 recordId
);
84 // Information about the table
85 const char* m_tableName
;
86 const char* m_entry_field
;
87 const char* m_src_format
;
88 const char* m_dst_format
;
90 // Information about the records
91 uint32 m_dstFieldCount
;
92 uint32 m_srcFieldCount
;
101 class SQLStorage
: public SQLStorageBase
103 template<class DerivedLoader
, class StorageClass
> friend class SQLStorageLoaderBase
;
106 SQLStorage(const char* fmt
, const char* _entry_field
, const char* sqlname
);
108 SQLStorage(const char* src_fmt
, const char* dst_fmt
, const char* _entry_field
, const char* sqlname
);
110 ~SQLStorage() { Free(); }
113 T
const* LookupEntry(uint32 id
) const
115 if (id
>= GetMaxEntry())
117 return reinterpret_cast<T
const*>(m_Index
[id
]);
122 void EraseEntry(uint32 id
);
125 void prepareToLoad(uint32 maxRecordId
, uint32 recordCount
, uint32 recordSize
) override
;
126 void JustCreatedRecord(uint32 recordId
, char* record
) override
128 m_Index
[recordId
] = record
;
131 void Free() override
;
138 class SQLHashStorage
: public SQLStorageBase
140 template<class DerivedLoader
, class StorageClass
> friend class SQLStorageLoaderBase
;
143 SQLHashStorage(const char* fmt
, const char* _entry_field
, const char* sqlname
);
144 SQLHashStorage(const char* src_fmt
, const char* dst_fmt
, const char* _entry_field
, const char* sqlname
);
146 ~SQLHashStorage() { Free(); }
149 T
const* LookupEntry(uint32 id
) const
151 RecordMap::const_iterator find
= m_indexMap
.find(id
);
152 if (find
!= m_indexMap
.end())
153 return reinterpret_cast<T
const*>(find
->second
);
159 void EraseEntry(uint32 id
);
162 void prepareToLoad(uint32 maxRecordId
, uint32 recordCount
, uint32 recordSize
) override
;
163 void JustCreatedRecord(uint32 recordId
, char* record
) override
165 m_indexMap
[recordId
] = record
;
168 void Free() override
;
171 typedef UNORDERED_MAP
<uint32
/*recordId*/, char* /*record*/> RecordMap
;
172 RecordMap m_indexMap
;
175 class SQLMultiStorage
: public SQLStorageBase
177 template<class DerivedLoader
, class StorageClass
> friend class SQLStorageLoaderBase
;
178 template<typename T
> friend class SQLMultiSIterator
;
179 template<typename T
> friend class SQLMSIteratorBounds
;
182 typedef std::multimap
<uint32
/*recordId*/, char* /*record*/> RecordMultiMap
;
185 SQLMultiStorage(const char* fmt
, const char* _entry_field
, const char* sqlname
);
186 SQLMultiStorage(const char* src_fmt
, const char* dst_fmt
, const char* _entry_field
, const char* sqlname
);
188 ~SQLMultiStorage() { Free(); }
191 class SQLMultiSIterator
193 friend class SQLMultiStorage
;
196 T
const* getValue() const { return reinterpret_cast<T
const*>(citerator
->second
); }
197 uint32
getKey() const { return citerator
->first
; }
199 void operator ++() { ++citerator
; }
200 T
const* operator *() const { return getValue(); }
201 T
const* operator ->() const { return getValue(); }
202 bool operator !=(const SQLMultiSIterator
& r
) const { return citerator
!= r
.citerator
; }
205 SQLMultiSIterator(RecordMultiMap::const_iterator _itr
) : citerator(_itr
) {}
206 RecordMultiMap::const_iterator citerator
;
210 class SQLMSIteratorBounds
212 friend class SQLMultiStorage
;
215 const SQLMultiSIterator
<T
> first
;
216 const SQLMultiSIterator
<T
> second
;
219 SQLMSIteratorBounds(std::pair
<RecordMultiMap::const_iterator
, RecordMultiMap::const_iterator
> pair
) : first(pair
.first
), second(pair
.second
) {}
223 SQLMSIteratorBounds
<T
> getBounds(uint32 key
) const { return SQLMSIteratorBounds
<T
>(m_indexMultiMap
.equal_range(key
)); }
227 void EraseEntry(uint32 id
);
230 void prepareToLoad(uint32 maxRecordId
, uint32 recordCount
, uint32 recordSize
) override
;
231 void JustCreatedRecord(uint32 recordId
, char* record
) override
233 m_indexMultiMap
.insert(RecordMultiMap::value_type(recordId
, record
));
236 void Free() override
;
239 RecordMultiMap m_indexMultiMap
;
242 template <class DerivedLoader
, class StorageClass
>
243 class SQLStorageLoaderBase
246 void Load(StorageClass
& storage
, bool error_at_empty
= true);
248 template<class S
, class D
>
249 void convert(uint32 field_pos
, S src
, D
& dst
);
251 void convert_to_str(uint32 field_pos
, S src
, char*& dst
);
253 void convert_from_str(uint32 field_pos
, char const* src
, D
& dst
);
254 void convert_str_to_str(uint32 field_pos
, char const* src
, char*& dst
);
255 template<class S
, class D
>
256 void default_fill(uint32 field_pos
, S src
, D
& dst
);
257 void default_fill_to_str(uint32 field_pos
, char const* src
, char*& dst
);
261 void convert_from_str(uint32 field_pos
, char* src
, D
& dst
);
262 void convert_str_to_str(uint32 field_pos
, char* src
, char*& dst
);
266 void storeValue(V value
, StorageClass
& store
, char* record
, uint32 field_pos
, uint32
& offset
);
267 void storeValue(char const* value
, StorageClass
& store
, char* record
, uint32 field_pos
, uint32
& offset
);
270 void storeValue(char* value
, StorageClass
& store
, char* record
, uint32 field_pos
, uint32
& offset
);
273 class SQLStorageLoader
: public SQLStorageLoaderBase
<SQLStorageLoader
, SQLStorage
>
277 class SQLHashStorageLoader
: public SQLStorageLoaderBase
<SQLHashStorageLoader
, SQLHashStorage
>
281 class SQLMultiStorageLoader
: public SQLStorageLoaderBase
<SQLMultiStorageLoader
, SQLMultiStorage
>
285 #include "SQLStorageImpl.h"