2 * Copyright (C) 2005-2009 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
19 #include "ProgressBar.h"
24 template<class S
, class D
>
25 void SQLStorageLoaderBase
<T
>::convert(uint32 field_pos
, S src
, D
&dst
)
31 void SQLStorageLoaderBase
<T
>::convert_str_to_str(uint32 field_pos
, char *src
, char *&dst
)
40 uint32 l
= strlen(src
) + 1;
48 void SQLStorageLoaderBase
<T
>::convert_to_str(uint32 field_pos
, S src
, char * & dst
)
56 void SQLStorageLoaderBase
<T
>::convert_from_str(uint32 field_pos
, char * src
, D
& dst
)
63 void SQLStorageLoaderBase
<T
>::storeValue(V value
, SQLStorage
&store
, char *p
, int x
, uint32
&offset
)
65 T
* subclass
= (static_cast<T
*>(this));
66 switch(store
.dst_format
[x
])
69 subclass
->convert(x
, value
, *((bool*)(&p
[offset
])) );
73 subclass
->convert(x
, value
, *((char*)(&p
[offset
])) );
77 subclass
->convert(x
, value
, *((uint32
*)(&p
[offset
])) );
78 offset
+=sizeof(uint32
);
81 subclass
->convert(x
, value
, *((float*)(&p
[offset
])) );
82 offset
+=sizeof(float);
85 subclass
->convert_to_str(x
, value
, *((char**)(&p
[offset
])) );
86 offset
+=sizeof(char*);
92 void SQLStorageLoaderBase
<T
>::storeValue(char * value
, SQLStorage
&store
, char *p
, int x
, uint32
&offset
)
94 T
* subclass
= (static_cast<T
*>(this));
95 switch(store
.dst_format
[x
])
98 subclass
->convert_from_str(x
, value
, *((bool*)(&p
[offset
])) );
102 subclass
->convert_from_str(x
, value
, *((char*)(&p
[offset
])) );
103 offset
+=sizeof(char);
106 subclass
->convert_from_str(x
, value
, *((uint32
*)(&p
[offset
])) );
107 offset
+=sizeof(uint32
);
110 subclass
->convert_from_str(x
, value
, *((float*)(&p
[offset
])) );
111 offset
+=sizeof(float);
114 subclass
->convert_str_to_str(x
, value
, *((char**)(&p
[offset
])) );
115 offset
+=sizeof(char*);
121 void SQLStorageLoaderBase
<T
>::Load(SQLStorage
&store
)
125 QueryResult
*result
= WorldDatabase
.PQuery("SELECT MAX(%s) FROM %s", store
.entry_field
, store
.table
);
128 sLog
.outError("Error loading %s table (not exist?)\n", store
.table
);
129 exit(1); // Stop server at loading non exited table or not accessable table
132 maxi
= (*result
)[0].GetUInt32()+1;
135 result
= WorldDatabase
.PQuery("SELECT COUNT(*) FROM %s", store
.table
);
138 fields
= result
->Fetch();
139 store
.RecordCount
= fields
[0].GetUInt32();
143 store
.RecordCount
= 0;
145 result
= WorldDatabase
.PQuery("SELECT * FROM %s", store
.table
);
149 sLog
.outError("%s table is empty!\n", store
.table
);
150 store
.RecordCount
= 0;
154 uint32 recordsize
= 0;
157 if(store
.iNumFields
!= result
->GetFieldCount())
159 store
.RecordCount
= 0;
160 sLog
.outError("Error in %s table, probably sql file format was updated (there should be %d fields in sql).\n", store
.table
, store
.iNumFields
);
162 exit(1); // Stop server at loading broken or non-compatible table.
169 for(uint32 x
=0; x
< store
.iNumFields
; x
++)
170 if(store
.dst_format
[x
]==FT_STRING
)
172 else if (store
.dst_format
[x
]==FT_LOGIC
)
174 else if (store
.dst_format
[x
]==FT_BYTE
)
176 recordsize
=(store
.iNumFields
-sc
-bo
-bb
)*4+sc
*sizeof(char*)+bo
*sizeof(bool)+bb
*sizeof(char);
178 char** newIndex
=new char*[maxi
];
179 memset(newIndex
,0,maxi
*sizeof(char*));
181 char * _data
= new char[store
.RecordCount
*recordsize
];
183 barGoLink
bar( store
.RecordCount
);
186 fields
= result
->Fetch();
188 char *p
=(char*)&_data
[recordsize
*count
];
189 newIndex
[fields
[0].GetUInt32()]=p
;
192 for(uint32 x
= 0; x
< store
.iNumFields
; x
++)
193 switch(store
.src_format
[x
])
196 storeValue((bool)(fields
[x
].GetUInt32() > 0), store
, p
, x
, offset
); break;
198 storeValue((char)fields
[x
].GetUInt8(), store
, p
, x
, offset
); break;
200 storeValue((uint32
)fields
[x
].GetUInt32(), store
, p
, x
, offset
); break;
202 storeValue((float)fields
[x
].GetFloat(), store
, p
, x
, offset
); break;
204 storeValue((char*)fields
[x
].GetString(), store
, p
, x
, offset
); break;
207 }while( result
->NextRow() );
211 store
.pIndex
= newIndex
;
212 store
.MaxEntry
= maxi
;