[11999.5] Support loading tables with default filling into SQL-Loader

* Add special field markers to fill a column in SQLStorage with default values
* Add a safeguard to DBCStorage::EraseEntry, Add helper function DBCStorage::InsertEntry to store content

Note: If required similar adjustments to DBCStorage might be reasonable if required
This commit is contained in:
Schmoozerd 2012-06-12 23:09:16 +02:00
parent 1c1bc5c659
commit 969c10a8d9
5 changed files with 102 additions and 34 deletions

View file

@ -24,8 +24,10 @@
enum enum
{ {
FT_NA='x', //not used or unknown, 4 byte size FT_NA='x', // ignore/ default, 4 byte size, in Source String means field is ignored, in Dest String means field is filled with default value
FT_NA_BYTE='X', //not used or unknown, byte FT_NA_BYTE='X', // ignore/ default, 1 byte size, see above
FT_NA_FLOAT='F', // ignore/ default, float size, see above
FT_NA_POINTER='p', // fill default value into dest, pointer size, Use this only with static data (otherwise mem-leak)
FT_STRING='s', //char* FT_STRING='s', //char*
FT_FLOAT='f', //float FT_FLOAT='f', //float
FT_INT='i', //uint32 FT_INT='i', //uint32

View file

@ -29,7 +29,7 @@ class DBCStorage
explicit DBCStorage(const char *f) : nCount(0), fieldCount(0), fmt(f), indexTable(NULL), m_dataTable(NULL) { } explicit DBCStorage(const char *f) : nCount(0), fieldCount(0), fmt(f), indexTable(NULL), m_dataTable(NULL) { }
~DBCStorage() { Clear(); } ~DBCStorage() { Clear(); }
T const* LookupEntry(uint32 id) const { return (id>=nCount)?NULL:indexTable[id]; } T const* LookupEntry(uint32 id) const { return (id >= nCount) ? NULL : indexTable[id]; }
uint32 GetNumRows() const { return nCount; } uint32 GetNumRows() const { return nCount; }
char const* GetFormat() const { return fmt; } char const* GetFormat() const { return fmt; }
uint32 GetFieldCount() const { return fieldCount; } uint32 GetFieldCount() const { return fieldCount; }
@ -88,7 +88,8 @@ class DBCStorage
nCount = 0; nCount = 0;
} }
void EraseEntry(uint32 id) { indexTable[id] = NULL; } void EraseEntry(uint32 id) { assert(id < nCount && "To be erased entry must be in bounds!") ; indexTable[id] = NULL; }
void InsertEntry(T* entry, uint32 id) { assert(id < nCount && "To be inserted entry must be in bounds!"); indexTable[id] = entry; }
private: private:
uint32 nCount; uint32 nCount;

View file

@ -22,7 +22,7 @@
void SQLStorage::EraseEntry(uint32 id) void SQLStorage::EraseEntry(uint32 id)
{ {
uint32 offset = 0; uint32 offset = 0;
for(uint32 x = 0; x < iNumFields; ++x) for (uint32 x = 0; x < oNumFields; ++x)
{ {
switch(dst_format[x]) switch(dst_format[x])
{ {
@ -43,8 +43,13 @@ void SQLStorage::EraseEntry(uint32 id)
break; break;
} }
case FT_NA: case FT_NA:
offset += sizeof(uint32); break;
case FT_NA_BYTE: case FT_NA_BYTE:
break; offset += sizeof(char); break;
case FT_NA_FLOAT:
offset += sizeof(float); break;
case FT_NA_POINTER:
offset += sizeof(char*); break;
case FT_IND: case FT_IND:
case FT_SORT: case FT_SORT:
assert(false && "SQL storage not have sort field types"); assert(false && "SQL storage not have sort field types");
@ -61,7 +66,7 @@ void SQLStorage::EraseEntry(uint32 id)
void SQLStorage::Free () void SQLStorage::Free ()
{ {
uint32 offset = 0; uint32 offset = 0;
for(uint32 x = 0; x < iNumFields; ++x) for (uint32 x = 0; x < oNumFields; ++x)
{ {
switch(dst_format[x]) switch(dst_format[x])
{ {
@ -83,8 +88,13 @@ void SQLStorage::Free ()
break; break;
} }
case FT_NA: case FT_NA:
offset += sizeof(uint32); break;
case FT_NA_BYTE: case FT_NA_BYTE:
break; offset += sizeof(char); break;
case FT_NA_FLOAT:
offset += sizeof(float); break;
case FT_NA_POINTER:
offset += sizeof(char*); break;
case FT_IND: case FT_IND:
case FT_SORT: case FT_SORT:
assert(false && "SQL storage not have sort field types"); assert(false && "SQL storage not have sort field types");

View file

@ -62,6 +62,7 @@ class SQLStorage
uint32 RecordCount; uint32 RecordCount;
uint32 MaxEntry; uint32 MaxEntry;
uint32 iNumFields; uint32 iNumFields;
uint32 oNumFields;
char const* GetTableName() const { return table; } char const* GetTableName() const { return table; }
@ -77,6 +78,7 @@ class SQLStorage
data=NULL; data=NULL;
pIndex=NULL; pIndex=NULL;
iNumFields = strlen(src_format); iNumFields = strlen(src_format);
oNumFields = strlen(dst_format);
MaxEntry = 0; MaxEntry = 0;
} }
@ -103,6 +105,9 @@ struct SQLStorageLoaderBase
template<class D> template<class D>
void convert_from_str(uint32 field_pos, char const* src, D& dst); void convert_from_str(uint32 field_pos, char const* src, D& dst);
void convert_str_to_str(uint32 field_pos, char const* src, char *&dst); void convert_str_to_str(uint32 field_pos, char const* src, char *&dst);
template<class S, class D>
void default_fill(uint32 field_pos, S src, D &dst);
void default_fill_to_str(uint32 field_pos, char const* src, char * & dst);
// trap, no body // trap, no body
template<class D> template<class D>

View file

@ -33,7 +33,7 @@ void SQLStorageLoaderBase<T>::convert(uint32 /*field_pos*/, S src, D &dst)
template<class T> template<class T>
void SQLStorageLoaderBase<T>::convert_str_to_str(uint32 /*field_pos*/, char const *src, char *&dst) void SQLStorageLoaderBase<T>::convert_str_to_str(uint32 /*field_pos*/, char const *src, char *&dst)
{ {
if(!src) if (!src)
{ {
dst = new char[1]; dst = new char[1];
*dst = 0; *dst = 0;
@ -61,6 +61,20 @@ void SQLStorageLoaderBase<T>::convert_from_str(uint32 /*field_pos*/, char const*
dst = 0; dst = 0;
} }
template<class T>
template<class S, class D>
void SQLStorageLoaderBase<T>::default_fill(uint32 /*field_pos*/, S src, D &dst)
{
dst = D(src);
}
template<class T>
void SQLStorageLoaderBase<T>::default_fill_to_str(uint32 /*field_pos*/, char const* /*src*/, char * & dst)
{
dst = new char[1];
*dst = 0;
}
template<class T> template<class T>
template<class V> template<class V>
void SQLStorageLoaderBase<T>::storeValue(V value, SQLStorage &store, char *p, uint32 x, uint32 &offset) void SQLStorageLoaderBase<T>::storeValue(V value, SQLStorage &store, char *p, uint32 x, uint32 &offset)
@ -89,7 +103,16 @@ void SQLStorageLoaderBase<T>::storeValue(V value, SQLStorage &store, char *p, ui
offset+=sizeof(char*); offset+=sizeof(char*);
break; break;
case FT_NA: case FT_NA:
subclass->default_fill(x, value, *((int32*)(&p[offset])) );
offset+=sizeof(uint32);
break;
case FT_NA_BYTE: case FT_NA_BYTE:
subclass->default_fill(x, value, *((char*)(&p[offset])) );
offset+=sizeof(char);
break;
case FT_NA_FLOAT:
subclass->default_fill(x, value, *((float*)(&p[offset])) );
offset+=sizeof(float);
break; break;
case FT_IND: case FT_IND:
case FT_SORT: case FT_SORT:
@ -113,22 +136,23 @@ void SQLStorageLoaderBase<T>::storeValue(char const* value, SQLStorage &store, c
break; break;
case FT_BYTE: case FT_BYTE:
subclass->convert_from_str(x, value, *((char*)(&p[offset])) ); subclass->convert_from_str(x, value, *((char*)(&p[offset])) );
offset+=sizeof(char); offset += sizeof(char);
break; break;
case FT_INT: case FT_INT:
subclass->convert_from_str(x, value, *((uint32*)(&p[offset])) ); subclass->convert_from_str(x, value, *((uint32*)(&p[offset])) );
offset+=sizeof(uint32); offset += sizeof(uint32);
break; break;
case FT_FLOAT: case FT_FLOAT:
subclass->convert_from_str(x, value, *((float*)(&p[offset])) ); subclass->convert_from_str(x, value, *((float*)(&p[offset])) );
offset+=sizeof(float); offset += sizeof(float);
break; break;
case FT_STRING: case FT_STRING:
subclass->convert_str_to_str(x, value, *((char**)(&p[offset])) ); subclass->convert_str_to_str(x, value, *((char**)(&p[offset])) );
offset+=sizeof(char*); offset += sizeof(char*);
break; break;
case FT_NA: case FT_NA_POINTER:
case FT_NA_BYTE: subclass->default_fill_to_str(x, value, *((char**)(&p[offset])) );
offset += sizeof(char*);
break; break;
case FT_IND: case FT_IND:
case FT_SORT: case FT_SORT:
@ -182,7 +206,7 @@ void SQLStorageLoaderBase<T>::Load(SQLStorage &store, bool error_at_empty /*= tr
uint32 recordsize = 0; uint32 recordsize = 0;
uint32 offset = 0; uint32 offset = 0;
if(store.iNumFields != result->GetFieldCount()) if (store.iNumFields != result->GetFieldCount())
{ {
store.RecordCount = 0; store.RecordCount = 0;
sLog.outError("Error in %s table, probably sql file format was updated (there should be %d fields in sql).\n", store.table, store.iNumFields); sLog.outError("Error in %s table, probably sql file format was updated (there should be %d fields in sql).\n", store.table, store.iNumFields);
@ -191,8 +215,8 @@ void SQLStorageLoaderBase<T>::Load(SQLStorage &store, bool error_at_empty /*= tr
exit(1); // Stop server at loading broken or non-compatible table. exit(1); // Stop server at loading broken or non-compatible table.
} }
//get struct size // get struct size
for(uint32 x = 0; x < store.iNumFields; ++x) for (uint32 x = 0; x < store.oNumFields; ++x)
{ {
switch(store.dst_format[x]) switch(store.dst_format[x])
{ {
@ -207,8 +231,13 @@ void SQLStorageLoaderBase<T>::Load(SQLStorage &store, bool error_at_empty /*= tr
case FT_STRING: case FT_STRING:
recordsize += sizeof(char*); break; recordsize += sizeof(char*); break;
case FT_NA: case FT_NA:
recordsize += sizeof(uint32); break;
case FT_NA_BYTE: case FT_NA_BYTE:
break; recordsize += sizeof(char); break;
case FT_NA_FLOAT:
recordsize += sizeof(float); break;
case FT_NA_POINTER:
recordsize += sizeof(char*); break;
case FT_IND: case FT_IND:
case FT_SORT: case FT_SORT:
assert(false && "SQL storage not have sort field types"); assert(false && "SQL storage not have sort field types");
@ -219,45 +248,66 @@ void SQLStorageLoaderBase<T>::Load(SQLStorage &store, bool error_at_empty /*= tr
} }
} }
char** newIndex=new char*[maxi]; char** newIndex = new char*[maxi];
memset(newIndex,0,maxi*sizeof(char*)); memset(newIndex, 0, maxi*sizeof(char*));
char * _data= new char[store.RecordCount *recordsize]; char* _data= new char[store.RecordCount * recordsize];
uint32 count = 0; uint32 count = 0;
BarGoLink bar(store.RecordCount); BarGoLink bar(store.RecordCount);
do do
{ {
fields = result->Fetch(); fields = result->Fetch();
bar.step(); bar.step();
char *p=(char*)&_data[recordsize*count]; char* p = (char*)&_data[recordsize * count];
newIndex[fields[0].GetUInt32()]=p; newIndex[fields[0].GetUInt32()] = p;
offset=0; offset = 0;
for(uint32 x = 0; x < store.iNumFields; x++) // dependend on dest-size
switch(store.src_format[x]) // iterate two indexes: x over dest, y over source, y++ IFF x1=x/X
for (uint32 x = 0, y = 0; x < store.oNumFields; ++x)
{
switch (store.dst_format[x])
{
// For default fill continue and do not increase y
case FT_NA: storeValue((uint32)0, store, p, x, offset); continue;
case FT_NA_BYTE: storeValue((char)0, store, p, x, offset); continue;
case FT_NA_FLOAT: storeValue((float)0.0f, store, p, x, offset); continue;
case FT_NA_POINTER: storeValue((char const*)NULL, store, p, x, offset); continue;
}
// It is required that the input has at least as many columns set as the output requires
if (y >= store.iNumFields)
assert(false && "SQL storage has too few columns!");
switch(store.src_format[y])
{ {
case FT_LOGIC: case FT_LOGIC:
storeValue((bool)(fields[x].GetUInt32() > 0), store, p, x, offset); break; storeValue((bool)(fields[y].GetUInt32() > 0), store, p, x, offset); break;
case FT_BYTE: case FT_BYTE:
storeValue((char)fields[x].GetUInt8(), store, p, x, offset); break; storeValue((char)fields[y].GetUInt8(), store, p, x, offset); break;
case FT_INT: case FT_INT:
storeValue((uint32)fields[x].GetUInt32(), store, p, x, offset); break; storeValue((uint32)fields[y].GetUInt32(), store, p, x, offset); break;
case FT_FLOAT: case FT_FLOAT:
storeValue((float)fields[x].GetFloat(), store, p, x, offset); break; storeValue((float)fields[y].GetFloat(), store, p, x, offset); break;
case FT_STRING: case FT_STRING:
storeValue((char const*)fields[x].GetString(), store, p, x, offset); break; storeValue((char const*)fields[y].GetString(), store, p, x, offset); break;
case FT_NA: case FT_NA:
case FT_NA_BYTE: case FT_NA_BYTE:
case FT_NA_FLOAT:
break; break;
case FT_IND: case FT_IND:
case FT_SORT: case FT_SORT:
assert(false && "SQL storage not have sort field types"); case FT_NA_POINTER:
assert(false && "SQL storage not have sort or pointer field types");
break; break;
default: default:
assert(false && "unknown format character"); assert(false && "unknown format character");
} }
++y;
}
++count; ++count;
}while( result->NextRow() ); }
while (result->NextRow());
delete result; delete result;