private void generateSourceChunks()

in src/main/java/com/datacompare/service/FetchMetadata.java [573:701]


	private void generateSourceChunks(Connection connection, String schemaName, String tableName, String sortKey,
			String primaryKey, String filter, String filterType, String cols) throws SQLException {

		logger.info("Started preparing chunks");

		chunks.clear();
		
		Long totalRecords = getTotalRecords(connection, schemaName, tableName, filter); 

		setRowCount(totalRecords);

		long ntileSize = (long)Math.ceil(totalRecords / getFetchSize());
		
		ntileSize = (ntileSize <= 0) ? 1 : ntileSize;
		
		StringBuilder sql = new StringBuilder();
		
		if(isHasNoUniqueKey()) {
			
			sql.append("SELECT min(ROWNUM) AS startRange, max(ROWNUM) AS endRange,count(*) AS chunkSize, nt FROM (SELECT ROWNUM")
			.append(" ,ntile(").append(ntileSize).append(") OVER (ORDER BY ").append(cols).append(" ) nt FROM ").append(schemaName)
			.append(".").append(tableName);
			
		} else {

			sql.append("SELECT min(").append(primaryKey).append(") AS startRange, max(").append(primaryKey)
					.append(") AS endRange,count(*) AS chunkSize, nt FROM (SELECT ").append(primaryKey).append(" ,ntile(")
					.append(ntileSize).append(") OVER (ORDER BY ").append(primaryKey).append(" ) nt FROM ").append(schemaName)
					.append(".").append(tableName);
		}
			/*if(filter != null && !filter.isEmpty()) {
				
				sql.append(" WHERE ").append(filter);
			}*/

		
		if("ORACLE".equals(getDbType())) {
			
			sql.append(") GROUP BY nt ORDER BY nt");
			
		} else {
			
			sql.append(") as a GROUP BY nt ORDER BY nt");
		}
		
		logger.info("Fetch Chunks SQL Query: " + sql.toString()); 

		Statement stmt = connection.createStatement();

		ResultSet rs = stmt.executeQuery(sql.toString());
        int count=0;
		while (rs.next()) {

			int columnType= rs.getMetaData().getColumnType(1);

			long startRange=0;
			long endRange=0;
			if(!isNoNumericColumnType(columnType)){
				 startRange = rs.getLong("startRange");
				 endRange = rs.getLong("endRange");
			}
			long chunkSize = rs.getLong("chunkSize");

			StringBuilder condition = new StringBuilder();
            boolean whereapplied=false;
			boolean filterapplied=false;

			if(!isNoNumericColumnType(columnType)) {
				condition.append("where ");
				whereapplied=true;
			}

			if (filter != null && !filter.isEmpty() && !"Sample".equals(filterType)) {
			 if(!whereapplied) {
				 condition.append("where ");
				 whereapplied=true;
			 }
				condition.append(filter);
				filterapplied=true;
			}
			if(isHasNoUniqueKey()) {

				if (isNoNumericColumnType(columnType) ){
					condition.append(" order by 1");
				} else {

					if(filterapplied)
					condition.append(" and ");
                    if(count==0) {
						condition.append("key1").append(" >= ").append(startRange).append(" and ").append("key1")
								.append(" <= ").append(endRange).append(" order by 1");
					}
					else{
						condition.append("key1").append(" > ").append(startRange).append(" and ").append("key1")
								.append(" <= ").append(endRange).append(" order by 1");

					}
				}
			} else {

				if (isNoNumericColumnType(columnType) ){
					condition.append(" order by ").append(sortKey);
				} else {

					if(filterapplied)
						condition.append(" and ");
					if(count==0) {
						condition.append(primaryKey).append(" >=").append(startRange).append(" and ").append(primaryKey)
								.append(" <= ").append(endRange).append(" order by ").append(sortKey);
					}else{
						condition.append(primaryKey).append(" > ").append(startRange).append(" and ").append(primaryKey)
								.append(" <= ").append(endRange).append(" order by ").append(sortKey);
					}
				}
			}

			logger.debug("Chunk Range, Min: " + startRange + ", Max: " + endRange + ", Size: " + chunkSize); 
			
			chunks.add(condition.toString());
			count++;
		}
		
		JdbcUtil jdbcUtil = new JdbcUtil();
		
		jdbcUtil.closeResultSet(rs);
		jdbcUtil.closeStatement(stmt); 

		logger.info("Completed preparing chunks");
	}