private InputColumn < ? > findOrderByColumn ( final AnalysisJobBuilder jobBuilder ) { final Table sourceTable = jobBuilder . getSourceTables ( ) . get ( 0 ) ; // preferred strategy: Use the primary key final List < Column > primaryKeys = sourceTable . getPrimaryKeys ( ) ; if ( primaryKeys . size ( ) == 1 ) { final Column primaryKey = primaryKeys . get ( 0 ) ; final InputColumn < ? > sourceColumn = jobBuilder . getSourceColumnByName ( primaryKey . getName ( ) ) ; if ( sourceColumn == null ) { jobBuilder . addSourceColumn ( primaryKey ) ; logger . info ( "Added PK source column for ORDER BY clause on slave jobs: {}" , sourceColumn ) ; return jobBuilder . getSourceColumnByName ( primaryKey . getName ( ) ) ; } else { logger . info ( "Using existing PK source column for ORDER BY clause on slave jobs: {}" , sourceColumn ) ; return sourceColumn ; } } else { if ( logger . isDebugEnabled ( ) ) { logger . debug ( "Found {} primary keys, cannot select a single for ORDER BY clause on slave jobs: {}" , primaryKeys . size ( ) , primaryKeys . size ( ) ) ; } } // secondary strategy: See if there's a source column called something // like 'ID' or so, and use that. final List < MetaModelInputColumn > sourceColumns = jobBuilder . getSourceColumns ( ) ; final String tableName = sourceTable . getName ( ) . toLowerCase ( ) ; for ( final MetaModelInputColumn sourceColumn : sourceColumns ) { String name = sourceColumn . getName ( ) ; if ( name != null ) { name = StringUtils . replaceWhitespaces ( name , "" ) ; name = StringUtils . replaceAll ( name , "_" , "" ) ; name = StringUtils . replaceAll ( name , "-" , "" ) ; name = name . toLowerCase ( ) ; if ( "id" . equals ( name ) || ( tableName + "id" ) . equals ( name ) || ( tableName + "number" ) . equals ( name ) || ( tableName + "key" ) . equals ( name ) ) { logger . info ( "Using existing source column for ORDER BY clause on slave jobs: {}" , sourceColumn ) ; return sourceColumn ; } } } // last resort: Pick any source column and sort on that (might not work // if the column contains a lot of repeated values) final MetaModelInputColumn sourceColumn = sourceColumns . get ( 0 ) ; logger . warn ( "Couldn't pick a good source column for ORDER BY clause on slave jobs. Picking the first column: {}" , sourceColumn ) ; return sourceColumn ; }
Finds a source column which is appropriate for an ORDER BY clause in the generated paginated queries