function onExistingTableMetadata()

in database-jones/Adapter/api/SessionFactory.js [333:433]


  function onExistingTableMetadata(err, tableMetadata) {
    tableSpecification = userContext.tableSpecification;
    constructor = userContext.handlerCtor;
    var invalidateCallback;

    tableKey = tableSpecification.qualifiedTableName;
    if(udebug.is_detail()) {
      udebug.log_detail('onExistingTableMetadata for ', tableSpecification.qualifiedTableName + ' with err: ' + err);
    }
    if (err) {
      onTableHandler(err, null);
    } else {
      // check to see if the metadata has already been cached
      if (sessionFactory.tableMetadatas[tableKey] === undefined) {
        // put the table metadata into the table metadata map
        sessionFactory.tableMetadatas[tableKey] = tableMetadata;
        invalidateCallback = function() {
          // use " = undefined" here to keep tableKey in the tableMetadatas object
          udebug.log('invalidateCallback called for session factory table metadata for', tableKey);
          sessionFactory.tableMetadatas[tableKey] = undefined;
        };
        tableMetadata.registerInvalidateCallback(invalidateCallback);
      }
      // we have the table metadata; now create the default table handler if not cached
      // do not use the existing cached table handler if processing a new table mapping
      if (userContext.tableIndicatorType === 'tablemapping' ||
          (session.tableHandlers[tableKey] === undefined &&
          sessionFactory.tableHandlers[tableKey] === undefined)) {
        if(udebug.is_detail()) { udebug.log_detail('creating the default table handler for ', tableKey); }
        dbTableHandler = new DBTableHandler(tableMetadata, tableMapping);
        if (dbTableHandler.isValid) {
          // cache the table handler for the table name and table mapping cases
          if (userContext.cacheTableHandlerInSessionFactory) {
            udebug.log('caching the default table handler in the session factory for', tableKey);
            sessionFactory.tableHandlers[tableKey] = dbTableHandler;
            invalidateCallback = function() {
              // use " = undefined" here to keep tableKey in the tableHandlers object
              udebug.log('invalidateCallback called for session factory default table handlers for', tableKey);
              sessionFactory.tableHandlers[tableKey] = undefined;
            };
            tableMetadata.registerInvalidateCallback(invalidateCallback);
          }
          if (userContext.cacheTableHandlerInSession) {
            udebug.log('caching the default table handler in the session for', tableKey);
            session.tableHandlers[tableKey] = dbTableHandler;
            invalidateCallback = function() {
              // use " = undefined" here to keep tableKey in the tableHandlers object
              udebug.log('invalidateCallback called for session default table handlers for', tableKey);
              session.tableHandlers[tableKey] = undefined;
            };
            tableMetadata.registerInvalidateCallback(invalidateCallback);
          }
        } else {
          err = new Error(dbTableHandler.errorMessages);
          udebug.log('onExistingTableMetadata got invalid dbTableHandler', dbTableHandler.errorMessages);
        }
      } else {
        dbTableHandler = session.tableHandlers[tableKey] || sessionFactory.tableHandlers[tableKey];
        udebug.log('onExistingTableMetadata got default dbTableHandler but' +
            ' someone else put it in the cache first for ', tableKey);
      }
      if (constructor) {
        constructorJones = constructor.prototype.jones;
        if (constructorJones === undefined) {
          onTableHandler(new Error('Internal error: constructor.prototype.jones is undefined.'));
          return;
        }
        dbTableHandler = constructorJones.dbTableHandler;
        if (dbTableHandler === undefined) {
          // if a domain object mapping, cache the table handler in the prototype
          tableMapping = constructorJones.mapping;
          dbTableHandler = new DBTableHandler(tableMetadata, tableMapping, constructor);
          if (dbTableHandler.isValid) {
            stats.TableHandler.success++;
            constructorJones.dbTableHandler = dbTableHandler;
            invalidateCallback = function() {
              if(udebug.is_detail()) {
                  udebug.log_detail('invalidateCallback called for constructor', constructor.name,
                  'for table', tableMetadata.database+'.'+tableMetadata.name);
                  constructorJones.dbTableHandler = null;
              }
            };
            tableMetadata.registerInvalidateCallback(invalidateCallback);
            if(udebug.is_detail()) {
              udebug.log('caching the table handler in the prototype for constructor.');
            }
          } else {
            err = new Error(dbTableHandler.errorMessages);
            if(udebug.is_detail()) { udebug.log_detail('got invalid dbTableHandler', dbTableHandler.errorMessages); }
          }
        } else {
          stats.TableHandler.idempotent++;
          if(udebug.is_detail()) {
            if(udebug.is_detail()) { udebug.log_detail('got dbTableHandler but someone else put it in the prototype first.'); }
          }
        }
      }
      userContext.handlerCtor = undefined;
      onTableHandler(err, dbTableHandler);
    }
  }