root/trunk/src/shared/Database/SQLStorage.h @ 262

Revision 260, 3.1 kB (checked in by yumileroy, 17 years ago)

*DB script table stucture change. Source Mangos. Also fix some bugs. Hopefully this rev will make program usable again.

Original author: megamage
Date: 2008-11-20 10:43:20-06:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21#ifndef SQLSTORAGE_H
22#define SQLSTORAGE_H
23
24#include "Common.h"
25#include "Database/DatabaseEnv.h"
26
27class SQLStorage
28{
29    template<class T>
30    friend struct SQLStorageLoaderBase;
31
32    public:
33
34        SQLStorage(const char* fmt, const char * _entry_field, const char * sqlname)
35        {
36            src_format = fmt;
37            dst_format = fmt;
38            init(_entry_field, sqlname);
39        }
40
41        SQLStorage(const char* src_fmt, const char* dst_fmt, const char * _entry_field, const char * sqlname)
42        {
43            src_format = src_fmt;
44            dst_format = dst_fmt;
45            init(_entry_field, sqlname);
46        }
47
48
49        ~SQLStorage()
50        {
51            Free();
52        }
53
54        template<class T>
55            T const* LookupEntry(uint32 id) const
56        {
57            if( id == 0 )
58                return NULL;
59            if(id >= MaxEntry)
60                return NULL;
61            return reinterpret_cast<T const*>(pIndex[id]);
62        }
63
64        uint32 RecordCount;
65        uint32 MaxEntry;
66        uint32 iNumFields;
67
68        void Load();
69        void Free();
70
71    private:
72        void init(const char * _entry_field, const char * sqlname)
73        {
74            entry_field = _entry_field;
75            table=sqlname;
76            data=NULL;
77            pIndex=NULL;
78            iNumFields = strlen(src_format);
79            MaxEntry = 0;
80        }
81
82        char** pIndex;
83
84        char *data;
85        const char *src_format;
86        const char *dst_format;
87        const char *table;
88        const char *entry_field;
89        //bool HasString;
90};
91
92template <class T>
93struct SQLStorageLoaderBase
94{
95    public:
96        void Load(SQLStorage &storage);
97
98        template<class S, class D>
99            void convert(uint32 field_pos, S src, D &dst);
100        template<class S>
101            void convert_to_str(uint32 field_pos, S src, char * & dst);
102        template<class D>
103            void convert_from_str(uint32 field_pos, char * src, D& dst);
104        void convert_str_to_str(uint32 field_pos, char *src, char *&dst);
105
106    private:
107        template<class V>
108            void storeValue(V value, SQLStorage &store, char *p, int x, uint32 &offset);
109        void storeValue(char * value, SQLStorage &store, char *p, int x, uint32 &offset);
110};
111
112struct SQLStorageLoader : public SQLStorageLoaderBase<SQLStorageLoader>
113{
114};
115
116#endif
Note: See TracBrowser for help on using the browser.