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