Bug in ComponentTable (persistence support)

Hi Paul, I can’t use EntityComponents without fields.
I wanted to use some components just as flag, so they don’t store any value.
This works fine, as far as I don’t try to use the SqlEntityData. I tracked it down already to the class ComponentTable.
My fix seems to work, but it is not very tested yet.
Here are the changes:

In createInsertSql() i changed this line:
[java]

    sql.append(", entityId");[/java] 

to this:
[java]
sql.append((dbFieldNames.length > 0) ? “, entityId” : “entityId”);[/java]
and this

[java]

    sql.append( ", ?");;[/java] 

to this:
[java]
sql.append((dbFieldNames.length > 0) ? “, ?” : “?”);[/java]
So there are no commas in the sql when there shouldn’t be some.

In initialize() i changed this
[java] if (!dbFields.isEmpty()) {
checkStructure(defs, dbFields);
return;
}[/java]
to this
[java] if (!dbFields.isEmpty() || md.getTables("", “”, tableName, new String[]{“TABLE”}) != null) {
checkStructure(defs, dbFields);
return;
}[/java]
Because if I have a table with no data-rows, it wouldn’t be recognized as table and treated as if there wasn’t a table.

In getComponents() I added a condition:
[java] if (dbFieldNames.length == 0) {
sql.append(" * “);
}
Joiner.on(”, ").appendTo(sql, dbFieldNames);[/java]
Else it would do SELECT () FROM.

And in setComponent() I wrapped the part which tries the update into a
[java] if (fields.length > 0) {
///
}else{
if(getComponent(session, entityId)!= null){
return;
}
}[/java]
If the fields are empty, then there is not data which should be updated, it is just necessary to know if there is a component of this type or not, and if there is one, not to try to insert a new one.

As I said, it’s not exhaustively tested, but it worked at least once :wink:

@pspeed
This issue seems to still exist…
And since I’ve read lately that a @user can help sometimes to get attention, I dare to push this thread :wink:

For some reason, this post never showed up in “Latest posts” for me. Thanks for at-mentioning me so I would see it.

Generally, if I haven’t responded to a Zay-ES or Lemur post in a day or two then it’s because I didn’t see it somehow.

I’m going to read the post more thoroughly and then respond but I wanted you to know that I saw it now.

I think this should probably be fixed so as to avoid other users stumbling across it… and so I will look into a fix.

However, on the “help people design” side, it seems strange to want components with no values. Can you describe your use-case?

I used it to mark entities to be processed by an ArrowSystem, which would handle flight and collision. This definitively was …strange at least, not to say a dirty hack to get it working for testing. The whole idea of the ArrowSystem handling both flight, rotation and collision probably stands in opposite of the ES approach.

One usecase where I think a flag-like component could maybe make sense, would be a DeadComponent, to mark entities who are dead. I wrote a simple DamageSystem which uses this aproach, and sets a DeadComponent. But this is contextless and was just a mere exercise in zay-es, so I’m not sure I would use a Component like this in a real project.

Still thought that this was probably a bug, or at least should be commented to not use it this way :wink:

Ah, I remember I had a Dead component in Monkey Trap, too… but it had the time of death as an attribute since everything in that game is fake-turn-based. Time of action/result is important.

Also, Monkey Trap isn’t persistent.

Generally, I don’t really like it there either but it did make sense at the time. Perhaps a more general state component was in order. Oh well.

I’ve added this table bug to my todo list but it’s not a very high priority until someone hits it for real, I guess.

@pspeed does the persistence build the querys live as the inital post seeems so, I always assumed that you use prepared statements?

@Empire Phoenix said: @pspeed does the persistence build the querys live as the inital post seeems so, I always assumed that you use prepared statements?

I do use prepared statements. They are constructed and cached upon first use for that particular database session.