modules/query/TripsTable.js
function _getMatchingTripId(db, originId, destinationId, departure, serviceIdString, callback) {

  // Find a matching trip in the gtfs_stop_times table
  let select = "SELECT trip_id FROM gtfs_trips " +
    "WHERE service_id IN " + serviceIdString + " " +
    "AND trip_id IN (" +
    "SELECT trip_id FROM gtfs_stop_times WHERE stop_id='" + destinationId + "' " +
    "AND trip_id IN (" +
    "SELECT trip_id FROM gtfs_stop_times " +
    "WHERE stop_id='" + originId + "' AND departure_time_seconds=" + departure.getTimeSeconds() +
    "));";

  // Query the database
  db.select(select, function(err, results) {

    // Database Query Error
    if ( err ) {
      return callback(err);
    }

    // No results found
    if ( results.length === 0 ) {
      return callback(null, undefined);
    }

    // Find the best match
    let found = false;
    let count = 0;
    for ( let j = 0; j < results.length; j++ ) {
      let row = results[j];

      // Get StopTimes for origin and destination
      StopTimesTable.getStopTimeByTripStop(db, row.trip_id, originId, departure.getDateInt(), function(orErr, originStopTime) {
        StopTimesTable.getStopTimeByTripStop(db, row.trip_id, destinationId, departure.getDateInt(), function(deErr, destinationStopTime) {
          if ( orErr ) {
            return callback(orErr);
          }
          if ( deErr ) {
            return callback(deErr);
          }

          // Check stop sequence
          // If origin comes before destination, use that trip
          if ( originStopTime.stopSequence <= destinationStopTime.stopSequence ) {
            found = true;
            return callback(null, row.trip_id);
          }

          // No match found
          count ++;
          if ( !found && count === results.length ) {
            return callback(null, undefined);
          }

        });
      });

    }

  });

}
function _getMatchingTripId(db, originId, destinationId, departure, serviceIdString, callback) {

  // Find a matching trip in the gtfs_stop_times table
  let select = "SELECT trip_id FROM gtfs_trips " +
    "WHERE service_id IN " + serviceIdString + " " +
    "AND trip_id IN (" +
    "SELECT trip_id FROM gtfs_stop_times WHERE stop_id='" + destinationId + "' " +
    "AND trip_id IN (" +
    "SELECT trip_id FROM gtfs_stop_times " +
    "WHERE stop_id='" + originId + "' AND departure_time_seconds=" + departure.getTimeSeconds() +
    "));";

  // Query the database
  db.select(select, function(err, results) {

    // Database Query Error
    if ( err ) {
      return callback(err);
    }

    // No results found
    if ( results.length === 0 ) {
      return callback(null, undefined);
    }

    // Find the best match
    let found = false;
    let count = 0;
    for ( let j = 0; j < results.length; j++ ) {
      let row = results[j];

      // Get StopTimes for origin and destination
      StopTimesTable.getStopTimeByTripStop(db, row.trip_id, originId, departure.getDateInt(), function(orErr, originStopTime) {
        StopTimesTable.getStopTimeByTripStop(db, row.trip_id, destinationId, departure.getDateInt(), function(deErr, destinationStopTime) {
          if ( orErr ) {
            return callback(orErr);
          }
          if ( deErr ) {
            return callback(deErr);
          }

          // Check stop sequence
          // If origin comes before destination, use that trip
          if ( originStopTime.stopSequence <= destinationStopTime.stopSequence ) {
            found = true;
            return callback(null, row.trip_id);
          }

          // No match found
          count ++;
          if ( !found && count === results.length ) {
            return callback(null, undefined);
          }

        });
      });

    }

  });

}
function _getMatchingTripId ( db , originId , destinationId , departure , serviceIdString , callback ) { // Find a matching trip in the gtfs_stop_times table let select = "SELECT trip_id FROM gtfs_trips " + "WHERE service_id IN " + serviceIdString + " " + "AND trip_id IN (" + "SELECT trip_id FROM gtfs_stop_times WHERE stop_id='" + destinationId + "' " + "AND trip_id IN (" + "SELECT trip_id FROM gtfs_stop_times " + "WHERE stop_id='" + originId + "' AND departure_time_seconds=" + departure . getTimeSeconds ( ) + "));" ; // Query the database db . select ( select , function ( err , results ) { // Database Query Error if ( err ) { return callback ( err ) ; } // No results found if ( results . length === 0 ) { return callback ( null , undefined ) ; } // Find the best match let found = false ; let count = 0 ; for ( let j = 0 ; j < results . length ; j ++ ) { let row = results [ j ] ; // Get StopTimes for origin and destination StopTimesTable . getStopTimeByTripStop ( db , row . trip_id , originId , departure . getDateInt ( ) , function ( orErr , originStopTime ) { StopTimesTable . getStopTimeByTripStop ( db , row . trip_id , destinationId , departure . getDateInt ( ) , function ( deErr , destinationStopTime ) { if ( orErr ) { return callback ( orErr ) ; } if ( deErr ) { return callback ( deErr ) ; } // Check stop sequence // If origin comes before destination, use that trip if ( originStopTime . stopSequence <= destinationStopTime . stopSequence ) { found = true ; return callback ( null , row . trip_id ) ; } // No match found count ++ ; if ( ! found && count === results . length ) { return callback ( null , undefined ) ; } } ) ; } ) ; } } ) ; }
Get the Trip ID of the trip matching the origin, destination, and departure information
@param {RightTrackDB} db The Right Track DB to query
@param {string} originId Origin Stop ID
@param {string} destinationId Destination Stop ID
@param {DateTime} departure DateTime of departure
@param {string} serviceIdString Effective Service ID string
@param {function} callback Callback function(err, tripId)
@private
Get the Trip ID of the trip matching the origin destination and departure information