Project

General

Profile

« Previous | Next » 

Revision 7437

use RDBMS-specific features to limit the resultset for paging -- postgres and oracle have implementations. we don''t really support mssql so I skipped that one.

View differences:

src/edu/ucsb/nceas/dbadapter/PostgresqlAdapter.java
26 26

  
27 27
package edu.ucsb.nceas.dbadapter;
28 28

  
29
import java.sql.*;
30
import edu.ucsb.nceas.metacat.*;
29
import java.sql.Connection;
30
import java.sql.ResultSet;
31
import java.sql.SQLException;
32
import java.sql.Statement;
31 33

  
32 34
/**
33 35
 * The PostgreSQL db adapter implementation.
......
127 129
      String sql ="select a.docid, a.rev, a.doctype from ( xml_documents as a left outer join  xml_revisions as b on (a.docid=b.docid and  a.rev<=b.rev)) where b.docid is null ";
128 130
      return sql;
129 131
  }
132
  
133
  public String getPagedQuery(String queryWithOrderBy, Integer start, Integer count) {
134
	  String query = queryWithOrderBy;
135
	  if (count != null) {
136
		  query = query + " LIMIT " + count;
137
	  }
138
	  if (start != null) {
139
		  query = query + " OFFSET " + start;
140
	  }
141
	  return query;
142
  }
130 143
}
131 144
    
src/edu/ucsb/nceas/dbadapter/OracleAdapter.java
26 26

  
27 27
package edu.ucsb.nceas.dbadapter;
28 28

  
29
import java.sql.*;
30
import edu.ucsb.nceas.metacat.*;
29
import java.sql.Connection;
30
import java.sql.ResultSet;
31
import java.sql.SQLException;
32
import java.sql.Statement;
31 33

  
32 34
/**
33 35
 * The Oracle db adapter implementation.
......
128 130
      return sql;
129 131
  }
130 132
  
133
  public String getPagedQuery(String queryWithOrderBy, Integer start, Integer count) {
134
	  
135
	  // not limiting rows
136
	  StringBuffer query = new StringBuffer("SELECT " + queryWithOrderBy);
137
	  
138
	  // check for params
139
	  if (start != null) {
140
		  query = new StringBuffer();
141
		  query.append("SELECT * FROM ");
142
		  query.append("( SELECT a.*, ROWNUM rnum FROM ");
143
		  query.append("( " + queryWithOrderBy + " ) a ");
144
		  if (count != null) {
145
			  // both are limited
146
			  query.append(" WHERE ROWNUM <= " + count);
147
		  }
148
		  query.append(" ) ");
149
		  query.append("WHERE rnum  >= " + start);
150
	  }
151
	  
152
	  return query.toString();
153
  }
154
  
131 155
}
132 156
    
src/edu/ucsb/nceas/dbadapter/SqlserverAdapter.java
26 26

  
27 27
package edu.ucsb.nceas.dbadapter;
28 28

  
29
import java.sql.*;
30
import edu.ucsb.nceas.metacat.*;
29
import java.sql.Connection;
30
import java.sql.ResultSet;
31
import java.sql.SQLException;
32
import java.sql.Statement;
31 33

  
32 34
/**
33 35
 * The MS SQL Server db adapter implementation.
......
133 135
      String sql ="select a.docid, a.rev, a.doctype from ( xml_documents a left outer join  xml_revisions b on (a.docid=b.docid and  a.rev<=b.rev)) where b.docid is null ";
134 136
      return sql;
135 137
  }
138
  
139
  public String getPagedQuery(String queryFieldsWithOrderBy, Integer start, Integer count) {
140
	  // TODO: implement MSSQL server
141
	  return null;
142

  
143
  }
136 144
}
137 145
    
src/edu/ucsb/nceas/dbadapter/AbstractDatabase.java
28 28

  
29 29
package edu.ucsb.nceas.dbadapter;
30 30

  
31
import java.sql.*;
31
import java.sql.Connection;
32
import java.sql.SQLException;
32 33

  
33 34
import edu.ucsb.nceas.metacat.properties.PropertyService;
34 35

  
......
155 156
   * @return
156 157
   */
157 158
  public abstract String getReplicationDocumentListSQL();
159
  
160
  /**
161
   * for generating a query for paging
162
   * @param queryWithOrderBy - the complete query with SELECT, FROM, WHERE and ORDER BY clauses
163
   * @param start the row number to start from
164
   * @param count the number of records from start to return
165
   * @return query specific to the RDBMS in use
166
   */
167
  public abstract String getPagedQuery(String queryWithOrderBy, Integer start, Integer count);
168
  
158 169
}
159 170
    
src/edu/ucsb/nceas/metacat/EventLog.java
43 43

  
44 44
import edu.ucsb.nceas.metacat.database.DBConnection;
45 45
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
46
import edu.ucsb.nceas.metacat.database.DatabaseService;
46 47
import edu.ucsb.nceas.metacat.properties.PropertyService;
47 48
import edu.ucsb.nceas.metacat.util.DocumentUtil;
48 49
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
......
418 419
        	query.append(") ");
419 420
            clauseAdded = true;
420 421
        }
422
        // always limit by only dataone events
423
        if (true) {
424
	        if (clauseAdded) {
425
	            query.append(" and ");
426
	        }
427
	    	query.append("event in (");
428
	    	for (int i = 0; i < Event.values().length; i++) {
429
	    		if (i > 0) {
430
	        		query.append(", ");
431
	    		}
432
	    		query.append("?");
433
	    		paramValues.add(Event.values()[i].xmlValue());
434
	    	}
435
	    	query.append(") ");
436
	        clauseAdded = true;
437
        }
421 438
        if (startDate != null) {
422 439
            if (clauseAdded) {
423 440
                query.append(" and ");
......
434 451
            clauseAdded = true;
435 452
            endIndex = startIndex + 1;
436 453
        }
454

  
455
        // order by
456
        query.append(" order by entryid ");
457

  
458
		// limit by count
459
        String pagedQuery = DatabaseService.getInstance().getDBAdapter().getPagedQuery(query.toString(), start, count);
460

  
437 461
        DBConnection dbConn = null;
438 462
        int serialNumber = -1;
439 463
        try {
......
442 466
            serialNumber = dbConn.getCheckOutSerialNumber();
443 467

  
444 468
            // Execute the query statement
445
            PreparedStatement stmt = dbConn.prepareStatement(query.toString());
469
            PreparedStatement stmt = dbConn.prepareStatement(pagedQuery);
446 470
            //set the param values
447 471
            int parameterIndex = 1;
448 472
            for (String val: paramValues) {
......
495 519
			log.setLogEntryList(logs);
496 520
			// d1 paging
497 521
		    int total = logs.size();
498
		    if (start != null && count != null) {
499
		    	int toIndex = start + count;
500
		    	// do not exceed total
501
		    	toIndex = Math.min(toIndex, total);
502
		    	// do not start greater than total
503
		    	start = Math.min(start, total);
504
		    	// sub set of the list
505
		    	logs = new ArrayList<LogEntry>(logs.subList(start, toIndex));
506
		    }
522
		    // NOTE: now using query limits
523
//		    if (start != null && count != null) {
524
//		    	int toIndex = start + count;
525
//		    	// do not exceed total
526
//		    	toIndex = Math.min(toIndex, total);
527
//		    	// do not start greater than total
528
//		    	start = Math.min(start, total);
529
//		    	// sub set of the list
530
//		    	logs = new ArrayList<LogEntry>(logs.subList(start, toIndex));
531
//		    }
507 532

  
508 533
		    log.setLogEntryList(logs);
509 534
		    log.setStart(start);

Also available in: Unified diff