function onResult()

in jones-mysql/impl/MySQLConnection.js [863:948]


  function onResult(row) {
    var i;
    var sector;
    var tuple = null;
    var parentSectorIndex;
    var relationship;
    var nullValue;
    op.rows++;
    // process the row by sector, left to right
    if (udebug.is_detail()) {udebug.log_detail('onResult processing row with', op.sectors.length, 'sectors:\n', row);}
    processSector(op, 0, row); // experimental for now
    // do each sector in turn; the parent sector will always be processed before any of its children
    for (i = 0; i < op.sectors.length; ++i) {
      sector = op.sectors[i];
      udebug.log_detail('onResult sector:', i, sector.projection.name);
      tuple = op.tuples[i];
      if (i == 0) {
        // root object handling; root will never be null
        if (!isRowSectorKeyEqual(row, sector, tuple)) {
          // create a new domain object from this row
          if (op.tuples[0] !== undefined) {
            // collect the current root object before creating a new root object
            op.roots.push(op.tuples[0]);
          }
          op.tuples[0] = sector.tableHandler.newResultObjectFromRow(row,
              sector.offset, sector.keyFields, sector.nonKeyFields,
              sector.toManyRelationships, sector.toOneRelationships);
          // the child tuples belong to the previous tuple
          resetTuples(op, 0);
        }
        // we are done with this (root) sector
        continue;
      }
      parentSectorIndex = sector.parentSectorIndex;
      // if the keys in the row for this sector are null set the parent field to default
      if (isRowSectorKeyNull(row, sector)) {
        if (op.tuples[parentSectorIndex] != null) {
        // if there is a parent, set the parent relationship value to the default
          if (sector.parentFieldMapping.toMany) {
            // null toMany relationships are represented by an empty array
            nullValue = [];
          } else {
            nullValue = null;
          }
          op.tuples[parentSectorIndex][sector.parentFieldMapping.fieldName] = nullValue;
        }
        // reset the children of this tuple since they belong to the previous value
        op.tuples[i] = null;
        resetTuples(op, i);
        // and we are done with this sector
        continue;
      }
      // compare the keys of the row with the keys of the current object
      if (isRowSectorKeyEqual(row, sector, tuple)) {
        // we have already processed this object
        continue;
      }
      // keys do not match the current object; see if it matches one of the parent objects
      if (sector.parentFieldMapping.toMany) {
        tuple = findResultTupleInParent(op, row, sector);
      } else {
        tuple = op.tuples[parentSectorIndex][sector.parentFieldMapping.fieldName];
      }
      if (tuple == null) {
        // haven't seen this before; create a new tuple from the row
        tuple = sector.tableHandler.newResultObjectFromRow(row,
            sector.offset, sector.keyFields, sector.nonKeyFields,
            sector.toManyRelationships, sector.toOneRelationships);
        // the rest of the tuples belong to the previous object
        // assign the new object to the relationship field of the previous object
        if (sector.parentFieldMapping.toMany) {
          // relationship is an array
          relationship = op.tuples[parentSectorIndex][sector.parentFieldMapping.fieldName];
          if (!relationship) {
            relationship = op.tuples[parentSectorIndex][sector.parentFieldMapping.fieldName] = [];
          }
          relationship.push(tuple);
        } else {
          // relationship is a reference
          op.tuples[parentSectorIndex][sector.parentFieldMapping.fieldName] = tuple;
        }
      }
      op.tuples[i] = tuple;
      resetTuples(op, i);
    }
  }