Wednesday, August 19, 2015

Display Current Sequence of All tables In xhtml


At a project Postgre sequences were cached and sometimes at server restarts we were seeing
strange behaviours. Also when someone inserts a sequence manually we were again having difficulties.
Here is simple page to see your database state.

public ArrayList>  initDbStateList(List            tableNames ,HashMap tableNamePrimary )
   {
	   ArrayList> data = new ArrayList>();
	   
	   
      
      for( String tname : tableNames )
      {
         HashMap map = new HashMap();
         
         map.put("tname", tname);
         map.put("seqid", 0L);
         map.put("maxid", 0L);
         
         try {
            
         
         
         String sql = "select nextval('MYDB."+ tname+"_sequence"+"');"; 
         Long seqid = -1L; 
                  
         try {
            seqid = queryForLong(sql);
         }
         catch (Exception e) {
            logger.error(e, e);
         }
          
         
         map.put("seqid", seqid);
         
         String maxsql = " select max("+tableNamePrimary.get(tname)+") from nufus."+tname+"; ";          
         Long maxid = queryForLong(maxsql);
         map.put("maxid", maxid);
         
         if( seqid != null && maxid != null )
         {
            if( seqid < maxid)
            {
               map.put("problematic", "YES");
            }
         }
         
         
         data.add(map);
         }
         catch (Exception e) {
            map.put("problematic", "NONEXISTENT");
            e.printStackTrace();
         }
      
      
	   }
     
	   
      return data;
   }

 
And datatable to display the result will be like :
				
					        
		    			
		    		        
		    		    
		    		        
		    		    
		    		        
		    		 
		    
		
 

No comments:

Post a Comment