SelectCommand.prepareStatement() has a bug preparing statements with INNER JOIN

Issue #10 resolved
Christian Junk
created an issue

The original tests fail if you run them often enough ;) The following code has a logic error:

{{{

!actionscript

var len:int = tables.length; for (i = 0; i < len; i++) { sql += StringUtil.substitute("{0}.{1} t{2}", _schema, tables[i], i); if (i > 0) { for (var fk:String in _joins[tables[i]]) { sql += StringUtil.substitute(" on t{0}.{1}=t{2}.{3} and ", i-1, _joins[tables[i]][fk], i, fk); } } if (i < len-1) { sql += " inner join "; } } if (len > 1) sql = sql.substring(0, sql.length-5); // remove last ' and ' // FIXME: also executed if no and was attached (len = 2) if (_filters.length > 0) { sql += " where "; for each(var filter:IFilter in _filters) { if (filter is Junction) { sql += StringUtil.substitute("{0} and ", Junction(filter).getString(function(table:String):int { return tables.indexOf(table); })); } else { sql += StringUtil.substitute("t{0}.{1} and ", tables.indexOf(ICondition(filter).table), filter); } } sql = sql.substring(0, sql.length-5); // remove last ' and ' } }}}

Sometimes it results in an erroneous statement like:

{{{

!bash

select t0.person_id,t0.email_addr,t0.entity_type,t1.organisation_id,t1.name,t1.my_contact_id from main.human_being t0 inner join main.my_frien where t1.marked_for_deletion<>true }}}

Another time the same call will result in the correct statement:

{{{

!bash

select t0.name,t0.organisation_id,t0.my_contact_id,t1.entity_type,t1.person_id,t1.email_addr from main.my_friends t0 inner join main.human_being t1 on t0.my_contact_id=t1.person_id where t0.marked_for_deletion<>true }}}

It depends on the order of the table names in the //_joins// and //tables// array (nz.co.codec.flexorm.command.SelectCommand).

Comments (8)

  1. Log in to comment