root/trunk/src/shared/Database/SQLStorage.cpp @ 40

Revision 2, 6.3 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
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.
8 *
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.
13 *
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
17 */
18
19#include "SQLStorage.h"
20#include "ProgressBar.h"
21#include "Log.h"
22#include "dbcfile.h"
23
24#ifdef DO_POSTGRESQL
25extern DatabasePostgre  WorldDatabase;
26#else
27extern DatabaseMysql  WorldDatabase;
28#endif
29
30const char CreatureInfofmt[]="iiiiiisssiiiiiiiiiiffiffiiiiiiiiiiiffiiiiiiiiiiiiiiiiiiisiilliiis";
31const char CreatureDataAddonInfofmt[]="iiiiiiis";
32const char CreatureModelfmt[]="iffbi";
33const char CreatureInfoAddonInfofmt[]="iiiiiiis";
34const char EquipmentInfofmt[]="iiiiiiiiii";
35const char GameObjectInfofmt[]="iiissiifiiiiiiiiiiiiiiiiiiiiiiiis";
36const char ItemPrototypefmt[]="iiiisiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiffiffiffiffiffiiiiiiiiiifiiifiiiiiifiiiiiifiiiiiifiiiiiifiiiisiiiiiiiiiiiiiiiiiiiiiiiiifsiiiii";
37const char PageTextfmt[]="isi";
38const char SpellThreatfmt[]="ii";
39const char InstanceTemplatefmt[]="iiiiiiffffs";
40
41SQLStorage sCreatureStorage(CreatureInfofmt,"entry","creature_template");
42SQLStorage sCreatureDataAddonStorage(CreatureDataAddonInfofmt,"guid","creature_addon");
43SQLStorage sCreatureModelStorage(CreatureModelfmt,"modelid","creature_model_info");
44SQLStorage sCreatureInfoAddonStorage(CreatureInfoAddonInfofmt,"entry","creature_template_addon");
45SQLStorage sEquipmentStorage(EquipmentInfofmt,"entry","creature_equip_template");
46SQLStorage sGOStorage(GameObjectInfofmt,"entry","gameobject_template");
47SQLStorage sItemStorage(ItemPrototypefmt,"entry","item_template");
48SQLStorage sPageTextStore(PageTextfmt,"entry","page_text");
49SQLStorage sSpellThreatStore(SpellThreatfmt,"entry","spell_threat");
50SQLStorage sInstanceTemplate(InstanceTemplatefmt,"map","instance_template");
51
52void SQLStorage::Free ()
53{
54    uint32 offset=0;
55    for(uint32 x=0;x<iNumFields;x++)
56        if (format[x]==FT_STRING)
57        {
58            for(uint32 y=0;y<MaxEntry;y++)
59                if(pIndex[y])
60                    delete [] *(char**)((char*)(pIndex[y])+offset);
61
62            offset+=sizeof(char*);
63        }
64        else if (format[x]==FT_LOGIC)
65            offset+=sizeof(bool);
66        else if (format[x]==FT_BYTE)
67            offset+=sizeof(char);
68        else
69            offset+=4;
70
71    delete [] pIndex;
72    delete [] data;
73}
74
75void SQLStorage::Load ()
76{
77    uint32 maxi;
78    Field *fields;
79    QueryResult *result  = WorldDatabase.PQuery("SELECT MAX(%s) FROM %s",entry_field,table);
80    if(!result)
81    {
82        sLog.outError("Error loading %s table (not exist?)\n",table);
83        exit(1);                                            // Stop server at loading non exited table or not accessable table
84    }
85
86    maxi= (*result)[0].GetUInt32()+1;
87    delete result;
88
89    result = WorldDatabase.PQuery("SELECT COUNT(*) FROM %s",table);
90    if(result)
91    {
92        fields = result->Fetch();
93        RecordCount=fields[0].GetUInt32();
94        delete result;
95    }
96    else
97        RecordCount = 0;
98
99    result = WorldDatabase.PQuery("SELECT * FROM %s",table);
100
101    if(!result)
102    {
103        sLog.outError("%s table is empty!\n",table);
104        RecordCount = 0;
105        return;
106    }
107
108    uint32 recordsize=0;
109    uint32 offset=0;
110
111    if(iNumFields!=result->GetFieldCount())
112    {
113        RecordCount = 0;
114        sLog.outError("Error in %s table, probably sql file format was updated (there should be %d fields in sql).\n",table,iNumFields);
115        delete result;
116        exit(1);                                            // Stop server at loading broken or non-compatiable table.
117    }
118
119    //get struct size
120    uint32 sc=0;
121    uint32 bo=0;
122    uint32 bb=0;
123    for(uint32 x=0;x<iNumFields;x++)
124        if(format[x]==FT_STRING)
125            ++sc;
126        else if (format[x]==FT_LOGIC)
127            ++bo;
128        else if (format[x]==FT_BYTE)
129            ++bb;
130    recordsize=(iNumFields-sc-bo-bb)*4+sc*sizeof(char*)+bo*sizeof(bool)+bb*sizeof(char);
131
132    char** newIndex=new char*[maxi];
133    memset(newIndex,0,maxi*sizeof(char*));
134
135    char * _data= new char[RecordCount *recordsize];
136    uint32 count=0;
137    barGoLink bar( RecordCount );
138    do
139    {
140        fields = result->Fetch();
141        bar.step();
142        char *p=(char*)&_data[recordsize*count];
143        newIndex[fields[0].GetUInt32()]=p;
144
145        offset=0;
146        for(uint32 x=0;x<iNumFields;x++)
147            switch(format[x])
148            {
149                case FT_LOGIC:
150                    *((bool*)(&p[offset]))=(fields[x].GetUInt32()>0);
151                    offset+=sizeof(bool);
152                    break;
153                case FT_BYTE:
154                    *((char*)(&p[offset]))=(fields[x].GetUInt8());
155                    offset+=sizeof(char);
156                    break;
157                case FT_INT:
158                    *((uint32*)(&p[offset]))=fields[x].GetUInt32();
159                    offset+=sizeof(uint32);
160                    break;
161                case FT_FLOAT:
162                    *((float*)(&p[offset]))=fields[x].GetFloat();
163                    offset+=sizeof(float);
164                    break;
165                case FT_STRING:
166                    char const* tmp = fields[x].GetString();
167                    char* st;
168                    if(!tmp)
169                    {
170                        st=new char[1];
171                        *st=0;
172                    }
173                    else
174                    {
175                        uint32 l=strlen(tmp)+1;
176                        st=new char[l];
177                        memcpy(st,tmp,l);
178                    }
179                    *((char**)(&p[offset]))=st;
180                    offset+=sizeof(char*);
181                    break;
182            }
183        ++count;
184    }while( result->NextRow() );
185
186    delete result;
187
188    pIndex =newIndex;
189    MaxEntry=maxi;
190    data=_data;
191}
Note: See TracBrowser for help on using the browser.