for (int t1 = 1; t1 <= lNumTables; t1++) {
t2 = t1;
// ------------------------------------
// Parameters for call to GetColumnName
// ------------------------------------
ParameterHolder_integer qq_tableIndex = new ParameterHolder_integer(t2);
ParameterHolder_TextData qq_columnName = new ParameterHolder_TextData();
this.getColumnName(i, qq_tableIndex, qq_columnName);
t2 = qq_tableIndex.getInt();
c = (TextData)qq_columnName.getObject();
sq.addConstraint(t2, c, ConstraintOperation.OP_EQ, this.getKey().getValues().get(i-1));
}
}
//
// Add user specified constraints.
//
sq.addConstraint(this.getConstraint());
break;
}
case BusinessQuery.OP_INSERT: {
//
// For inserts the constraints represent the values to be inserted.
//
sq.addValue(this.getValues());
break;
}
case BusinessQuery.OP_DELETE: {
//
// For deletes we need only specify the key attributes as constraints.
//
for (int i = 1; i <= lNumKeyAttrs; i++) {
for (int t1 = 1; t1 <= lNumTables; t1++) {
t2 = t1;
// ------------------------------------
// Parameters for call to GetColumnName
// ------------------------------------
ParameterHolder_integer qq_tableIndex = new ParameterHolder_integer(t2);
ParameterHolder_TextData qq_columnName = new ParameterHolder_TextData();
this.getColumnName(i, qq_tableIndex, qq_columnName);
t2 = qq_tableIndex.getInt();
c = (TextData)qq_columnName.getObject();
sq.addConstraint(t2, c, ConstraintOperation.OP_EQ, this.getKey().getValues().get(i-1));
}
}
//
// Add user specified constraints.
//
sq.addConstraint(this.getConstraint());
break;
}
case BusinessQuery.OP_SELECT: {
//
// Add attributes to the select list.
//
int attrs = 0;
int index = 0;
int baseIndex = 1;
int lNumDBAttrs = this.getNumDBAttrs();
// cache virtual attribute
if (this.getTargetAttrs() != null) {
for (IntegerData attr : this.getTargetAttrs()) {
if (baseIndex+30 <= lNumKeyAttrs) {
//
// All attributes are key attributes -- make sure they're all set.
//
attr.setValue(2147483647);
}
else if (baseIndex <= lNumKeyAttrs) {
//
// Or in key attributes
//
attr.setValue(attr.getValue()|((int)(new DoubleData().power(2, lNumKeyAttrs-baseIndex+1).getValue()-1)));
}
index = baseIndex;
attrs = attr.getValue();
while ((attrs > 0) && (index <= lNumDBAttrs)) {
if ((attrs&1) > 0) {
t = 1;
// ------------------------------------
// Parameters for call to GetColumnName
// ------------------------------------
ParameterHolder_integer qq_tableIndex = new ParameterHolder_integer(t);
ParameterHolder_TextData qq_columnName = new ParameterHolder_TextData();
this.getColumnName(index, qq_tableIndex, qq_columnName);
t = qq_tableIndex.getInt();
c = (TextData)qq_columnName.getObject();
sq.addColumn(t, c, (DataValue)null);
if (index <= lNumKeyAttrs && lNumTables > 1) {
for (int tindex = 2; tindex <= lNumTables; tindex++) {
t2 = tindex;
// ------------------------------------
// Parameters for call to GetColumnName
// ------------------------------------
ParameterHolder_integer qq_tableIndex1 = new ParameterHolder_integer(t2);
ParameterHolder_TextData qq_columnName1 = new ParameterHolder_TextData();
this.getColumnName(index, qq_tableIndex1, qq_columnName1);
t2 = qq_tableIndex1.getInt();
c = (TextData)qq_columnName1.getObject();
if (this.getConstraint() == null) {
this.setConstraint(new QueryConstraint());
}
this.getConstraint().addAttr(this, index, 1);
this.getConstraint().addAttr(this, index, tindex);
this.getConstraint().addOperation(ConstraintOperation.OP_EQ);
}
}
}
attrs = attrs/2;
index = index+1;
}
baseIndex = baseIndex+31;
}
}
//
// Finally do foreign entities.
//
if (this.getForeignClasses() != null) {
for (BusinessQuery fe : this.getForeignClasses()) {
if ((fe.getExecuteInfo().getStatus()&QueryExecuteInfo.ST_JOINED) > 0) {
//
// Check what kind of join we are going to do
//
int op = 0;
if ((fe.getParentMult()&BusinessQuery.ASSOC_MULT_OPTIONAL) == 0) {
op = ConstraintOperation.OP_EQUIJOIN;
sq.setOptions(sq.getOptions()|SqlQuery.OPT_INNER_JOIN);
}
else {
op = ConstraintOperation.OP_LEFTJOIN;
sq.setOptions(sq.getOptions()|SqlQuery.OPT_OUTER_JOIN);
}
//
// Put in a join to the foreign table
//
this.addJoinConstraint(op, fe);
//
// Now have the foreign entity describe itself as we just did and
// append this to the sqlQuery we are building up.
//
// ---------------------------------
// Parameters for call to BuildQuery
// ---------------------------------
ParameterHolder_SqlQuery qq_query = new ParameterHolder_SqlQuery(sq);
fe.buildQuery(qq_query);
sq = (SqlQuery)qq_query.getObject();
}
else if (fe.getTargetAttrs() == null) {
//
// This is the case where we have a 1-to-many association
// which has a constraint on the foreign class which is
// suppossed to limit the primary. We need an "in" sql
// condition of the form:
// where (<join-column-list>) in
// (select <join-column-list> from <foreignTable> ...)
//
TextData queryText = null;
SqlQuery subQuery = null;
// ---------------------------------
// Parameters for call to BuildQuery
// ---------------------------------
ParameterHolder_SqlQuery qq_query = new ParameterHolder_SqlQuery(subQuery);
fe.buildQuery(qq_query);
subQuery = (SqlQuery)qq_query.getObject();
//
// Don't really want this query to look like its executed.
//
fe.getExecuteInfo().setStatus(fe.getExecuteInfo().getStatus()&~QueryExecuteInfo.ST_BUILT);
//
// First build the sub-query and get its text. Note:
// this query really has the wrong column list. We'll
// fix that up and reset the query text in a minute, but
// need to get the text now as input data isn't
// computed until we do and we need to know that now.
//
queryText = subQuery.getText().get(0);
if (subQuery.getData().get(0).getValues().size() > 0) {
//
// Push input data for the sub-query, if any.
//
this.getConstraint().addSqlData(subQuery.getData().get(0));
}
// Next push the suq-query, we'll fix this up at the end.
//
this.getConstraint().addValue(queryText);
subQuery.clearColumnList();
//
// Now clear the column list and recompute it. At the
// same time push on the column list for the outer query.
//
Array_Of_QueryAttrMap<QueryAttrMap> qq_localVector = this.getForeignAttrMap(fe.getParentAttr());
if (qq_localVector != null) {
for (QueryAttrMap attr : qq_localVector) {
t = 1;
// ------------------------------------
// Parameters for call to GetColumnName
// ------------------------------------
ParameterHolder_integer qq_tableIndex = new ParameterHolder_integer(t);
ParameterHolder_TextData qq_columnName = new ParameterHolder_TextData();
fe.getColumnName(attr.getForeign(), qq_tableIndex, qq_columnName);
t = qq_tableIndex.getInt();
c = (TextData)qq_columnName.getObject();
subQuery.addColumn(t, c, (DataValue)null);
this.getConstraint().addAttr(this, attr.getLocal(), 1);
}
}