Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(recordsTotal): do not use search string for total count #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions lib/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,18 @@ module.exports = function (options) {

/**
* (private) Build a complete SELECT statement that counts the number of entries.
* @param searchString If specified then produces a statement to count the filtered list of records.
* @param requestQuery
* @param useSearch If true then produces a statement to count the filtered list of records.
* Otherwise the statement counts the unfiltered list of records.
* @return {String} A complete SELECT statement
* @returns {string} A complete SELECT statement
*/
function buildCountStatement(requestQuery) {
var dateSql = buildDatePartial();
function buildCountStatement(requestQuery, useSearch) {
//var dateSql = buildDatePartial();
var result = "SELECT COUNT(";
result += self.sSelectSql ? "*" : (self.sCountColumnName ? self.sCountColumnName : "id");
result += ") FROM ";
result += self.sFromSql ? self.sFromSql : self.sTableName;
result += buildWherePartial(requestQuery);
result += buildWherePartial(requestQuery, useSearch);
// var sSearchQuery = buildSearchPartial( sSearchString );
// var sWheres = sSearchQuery ? [ sSearchQuery ] : [];
// if( self.sWhereAndSql )
Expand All @@ -99,14 +100,19 @@ module.exports = function (options) {
/**
* (private) Build the WHERE clause
* otherwise uses aoColumnDef mData property.
* @param searchString
* @return {String}
* @param requestQuery
* @param useSearch If true, includes the search partial in the query.
* @returns {string}
*/
function buildWherePartial(requestQuery) {
function buildWherePartial(requestQuery, useSearch) {
var sWheres = [];
var searchQuery = buildSearchPartial(requestQuery);
if (searchQuery)
sWheres.push(searchQuery);

if (useSearch) {
var searchQuery = buildSearchPartial(requestQuery);
if (searchQuery)
sWheres.push(searchQuery);
}

if (self.sWhereAndSql)
sWheres.push(self.sWhereAndSql);
var dateSql = buildDatePartial();
Expand Down Expand Up @@ -252,10 +258,10 @@ module.exports = function (options) {
}
queries.recordsTotal = buildCountStatement(requestQuery);
if (searchString) {
queries.recordsFiltered = buildCountStatement(requestQuery);
queries.recordsFiltered = buildCountStatement(requestQuery, true); // Use search string if provided.
}
var query = buildSelectPartial();
query += buildWherePartial(requestQuery);
query += buildWherePartial(requestQuery, true); // Use search string if provided.
query += buildOrderingPartial(requestQuery);
query += buildLimitPartial(requestQuery);
if (self.dbType === 'oracle'){
Expand Down