system/src/sc/lang/sql/SchemaManager.java
public ArrayList<SQLFileModel> sortSQLModels(List<SQLFileModel> inList) {
      List<GraphSortNode> graphNodes = new ArrayList<GraphSortNode>();
      StringBuilder metadata = new StringBuilder();
      for (SQLFileModel in:inList) {
         if (in == null || in.srcType == null) {
            System.err.println("*** Null entry or src type in sortSQLModels");
            continue;
         }
         metadata.append("   Type: ");
         metadata.append(in.srcType.getFullTypeName());
         if (in.typeMetadata != null) {
            metadata.append(in.typeMetadata);
         }
         metadata.append("\n");

         List<SQLCommand> cmds = in.sqlCommands;
         if (cmds != null) {
            for (SQLCommand cmd:cmds) {
               GraphSortNode node = new GraphSortNode();
               node.cmd = cmd;
               graphNodes.add(node);
            }
         }
      }

      int num = graphNodes.size();

      for (int i = 0; i < num; i++) {
         GraphSortNode outer = graphNodes.get(i);
         for (int j = i+1; j < num; j++) {
            GraphSortNode inner = graphNodes.get(j);
            boolean hasTo = false;
            if (outer.cmd.hasReferenceTo(inner.cmd)) {
               // outer has a reference to inner
               outer.refsFrom.add(inner);
               inner.refsTo.add(outer);
               hasTo = true;
            }
            if (inner.cmd.hasReferenceTo(outer.cmd)) {
               if (hasTo) {
                  System.err.println("*** Error - cycle detected in dependencies");
               }
               else {
                  inner.refsFrom.add(outer);
                  outer.refsTo.add(inner);
               }
            }
         }
      }

      int assignedCt = 0;

      /** First pass is to just find all of the independent nodes */
      boolean remaining = false;
      for (int i = 0; i < num; i++) {
         GraphSortNode node = graphNodes.get(i);
         if (node.refsFrom.size() == 0) {
            node.depthAssigned = true;
            node.depth = 0;
            assignedCt++;
         }
         else
            remaining = true;
      }

      while (remaining) {
         remaining = false;
         for (int i = 0; i < num; i++) {
            GraphSortNode assignNode = graphNodes.get(i);
            if (!assignNode.depthAssigned) {
               boolean fromDepthAssigned = true;
               int maxFromDepth = -1;
               for (int j = 0; j < assignNode.refsFrom.size(); j++) {
                  GraphSortNode fromNode = assignNode.refsFrom.get(j);
                  if (fromNode.depthAssigned) {
                     if (maxFromDepth == -1 || maxFromDepth < fromNode.depth)
                        maxFromDepth = fromNode.depth;
                  }
                  else {
                     fromDepthAssigned = false;
                     break;
                  }
               }
               if (fromDepthAssigned) {
                  assignNode.depth = maxFromDepth + 1;
                  assignNode.depthAssigned = true;
               }
               else // There is a node in our refFrom that still has not been assigned a depth
                  remaining = true;
            }
         }
      }
      SemanticNodeList<SQLCommand> resCmds = new SemanticNodeList<SQLCommand>(inList.size());
      int currentDepth = 0;
      while (resCmds.size() < num) {
         for (int i = 0; i < num; i++) {
            GraphSortNode node = graphNodes.get(i);
            if (node.depthAssigned && node.depth == currentDepth)
               resCmds.add(node.cmd);
         }
         currentDepth++;
      }

      ArrayList<SQLFileModel> res = new ArrayList<SQLFileModel>(1);
      SQLFileModel mergedModel = new SQLFileModel();
      mergedModel.typeMetadata = metadata;
      mergedModel.setProperty("sqlCommands", resCmds);
      res.add(mergedModel);
      return res;
   }
/**
    * Sort the SQLFileModels based on table references. This assumes that the SQLCommands
    * inside of each model have the same dependencies - i.e. we don't have to interleave
    * commands for different types.  Because the file models form a 'directed acyclic graph'
    * we need to first build the graph, then do a breadth first search on that graph to
    * sort the commands.
    * TODO: this could be made more efficient - start with an index for finding the
    * references?
    */
Sort the SQLFileModels based on table references. This assumes that the SQLCommands
inside of each model have the same dependencies - i.e. we don't have to interleave
commands for different types.  Because the file models form a 'directed acyclic graph'
we need to first build the graph, then do a breadth first search on that graph to
sort the commands.
Sort the SQLFileModels based on table references . This assumes that the SQLCommands inside of each model have the same dependencies - i . e . we don ' t have to interleave commands for different types . Because the file models form a ' directed acyclic graph ' we need to first build the graph then do a breadth first search on that graph to sort the commands .
public ArrayList<SQLFileModel> sortSQLModels(List<SQLFileModel> inList) {
      List<GraphSortNode> graphNodes = new ArrayList<GraphSortNode>();
      StringBuilder metadata = new StringBuilder();
      for (SQLFileModel in:inList) {
         if (in == null || in.srcType == null) {
            System.err.println("*** Null entry or src type in sortSQLModels");
            continue;
         }
         metadata.append("   Type: ");
         metadata.append(in.srcType.getFullTypeName());
         if (in.typeMetadata != null) {
            metadata.append(in.typeMetadata);
         }
         metadata.append("\n");
         List<SQLCommand> cmds = in.sqlCommands;
         if (cmds != null) {
            for (SQLCommand cmd:cmds) {
               GraphSortNode node = new GraphSortNode();
               node.cmd = cmd;
               graphNodes.add(node);
            }
         }
      }
      int num = graphNodes.size();
      for (int i = 0; i < num; i++) {
         GraphSortNode outer = graphNodes.get(i);
         for (int j = i+1; j < num; j++) {
            GraphSortNode inner = graphNodes.get(j);
            boolean hasTo = false;
            if (outer.cmd.hasReferenceTo(inner.cmd)) {
               outer.refsFrom.add(inner);
               inner.refsTo.add(outer);
               hasTo = true;
            }
            if (inner.cmd.hasReferenceTo(outer.cmd)) {
               if (hasTo) {
                  System.err.println("*** Error - cycle detected in dependencies");
               }
               else {
                  inner.refsFrom.add(outer);
                  outer.refsTo.add(inner);
               }
            }
         }
      }
      int assignedCt = 0;
      /** First pass is to just find all of the independent nodes */
      boolean remaining = false;
      for (int i = 0; i < num; i++) {
         GraphSortNode node = graphNodes.get(i);
         if (node.refsFrom.size() == 0) {
            node.depthAssigned = true;
            node.depth = 0;
            assignedCt++;
         }
         else
            remaining = true;
      }
      while (remaining) {
         remaining = false;
         for (int i = 0; i < num; i++) {
            GraphSortNode assignNode = graphNodes.get(i);
            if (!assignNode.depthAssigned) {
               boolean fromDepthAssigned = true;
               int maxFromDepth = -1;
               for (int j = 0; j < assignNode.refsFrom.size(); j++) {
                  GraphSortNode fromNode = assignNode.refsFrom.get(j);
                  if (fromNode.depthAssigned) {
                     if (maxFromDepth == -1 || maxFromDepth < fromNode.depth)
                        maxFromDepth = fromNode.depth;
                  }
                  else {
                     fromDepthAssigned = false;
                     break;
                  }
               }
               if (fromDepthAssigned) {
                  assignNode.depth = maxFromDepth + 1;
                  assignNode.depthAssigned = true;
               }
               else 
                  remaining = true;
            }
         }
      }
      SemanticNodeList<SQLCommand> resCmds = new SemanticNodeList<SQLCommand>(inList.size());
      int currentDepth = 0;
      while (resCmds.size() < num) {
         for (int i = 0; i < num; i++) {
            GraphSortNode node = graphNodes.get(i);
            if (node.depthAssigned && node.depth == currentDepth)
               resCmds.add(node.cmd);
         }
         currentDepth++;
      }
      ArrayList<SQLFileModel> res = new ArrayList<SQLFileModel>(1);
      SQLFileModel mergedModel = new SQLFileModel();
      mergedModel.typeMetadata = metadata;
      mergedModel.setProperty("sqlCommands", resCmds);
      res.add(mergedModel);
      return res;
   }
public ArrayList < SQLFileModel > sortSQLModels ( List < SQLFileModel > inList ) { List < GraphSortNode > graphNodes = new ArrayList < GraphSortNode > ( ) ; StringBuilder metadata = new StringBuilder ( ) ; for ( SQLFileModel in : inList ) { if ( in == null || in . srcType == null ) { System . err . println ( "*** Null entry or src type in sortSQLModels" ) ; continue ; } metadata . append ( "   Type: " ) ; metadata . append ( in . srcType . getFullTypeName ( ) ) ; if ( in . typeMetadata != null ) { metadata . append ( in . typeMetadata ) ; } metadata . append ( "\n" ) ; List < SQLCommand > cmds = in . sqlCommands ; if ( cmds != null ) { for ( SQLCommand cmd : cmds ) { GraphSortNode node = new GraphSortNode ( ) ; node . cmd = cmd ; graphNodes . add ( node ) ; } } } int num = graphNodes . size ( ) ; for ( int i = 0 ; i < num ; i ++ ) { GraphSortNode outer = graphNodes . get ( i ) ; for ( int j = i + 1 ; j < num ; j ++ ) { GraphSortNode inner = graphNodes . get ( j ) ; boolean hasTo = false ; if ( outer . cmd . hasReferenceTo ( inner . cmd ) ) { outer . refsFrom . add ( inner ) ; inner . refsTo . add ( outer ) ; hasTo = true ; } if ( inner . cmd . hasReferenceTo ( outer . cmd ) ) { if ( hasTo ) { System . err . println ( "*** Error - cycle detected in dependencies" ) ; } else { inner . refsFrom . add ( outer ) ; outer . refsTo . add ( inner ) ; } } } } int assignedCt = 0 ; /** First pass is to just find all of the independent nodes */ boolean remaining = false ; for ( int i = 0 ; i < num ; i ++ ) { GraphSortNode node = graphNodes . get ( i ) ; if ( node . refsFrom . size ( ) == 0 ) { node . depthAssigned = true ; node . depth = 0 ; assignedCt ++ ; } else remaining = true ; } while ( remaining ) { remaining = false ; for ( int i = 0 ; i < num ; i ++ ) { GraphSortNode assignNode = graphNodes . get ( i ) ; if ( ! assignNode . depthAssigned ) { boolean fromDepthAssigned = true ; int maxFromDepth = - 1 ; for ( int j = 0 ; j < assignNode . refsFrom . size ( ) ; j ++ ) { GraphSortNode fromNode = assignNode . refsFrom . get ( j ) ; if ( fromNode . depthAssigned ) { if ( maxFromDepth == - 1 || maxFromDepth < fromNode . depth ) maxFromDepth = fromNode . depth ; } else { fromDepthAssigned = false ; break ; } } if ( fromDepthAssigned ) { assignNode . depth = maxFromDepth + 1 ; assignNode . depthAssigned = true ; } else remaining = true ; } } } SemanticNodeList < SQLCommand > resCmds = new SemanticNodeList < SQLCommand > ( inList . size ( ) ) ; int currentDepth = 0 ; while ( resCmds . size ( ) < num ) { for ( int i = 0 ; i < num ; i ++ ) { GraphSortNode node = graphNodes . get ( i ) ; if ( node . depthAssigned && node . depth == currentDepth ) resCmds . add ( node . cmd ) ; } currentDepth ++ ; } ArrayList < SQLFileModel > res = new ArrayList < SQLFileModel > ( 1 ) ; SQLFileModel mergedModel = new SQLFileModel ( ) ; mergedModel . typeMetadata = metadata ; mergedModel . setProperty ( "sqlCommands" , resCmds ) ; res . add ( mergedModel ) ; return res ; }
Sort the SQLFileModels based on table references.
Sort the SQLFileModels based on table references .
// outer has a reference to inner // There is a node in our refFrom that still has not been assigned a depth