Skip to content

Commit

Permalink
Fixed paging, show row count tooltip for query
Browse files Browse the repository at this point in the history
  • Loading branch information
yuraj11 committed Sep 14, 2015
1 parent 36b8084 commit ed608d6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ <h3>SQLite Viewer</h3>
<div id="bottom-bar" class="text-center">
<div class="inline">
<button class="btn btn-default btn-sm " id="page-prev" type="submit" onclick="setPage(this, false)"><span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span></button>
<a href="javascript:void(0)" onclick="setPage(this)" id="pager">100 / 100</a>
<a href="javascript:void(0)" data-toggle="tooltip" data-placement="top" onclick="setPage(this)" id="pager"></a>
<button class="btn btn-default btn-sm " id="page-next" type="submit" onclick="setPage(this, true)"><span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span></button>
</div>
</div>
Expand All @@ -121,7 +121,7 @@ <h3>SQLite Viewer</h3>
<script src="js/ace/ace.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/mindmup-editabletable.js"></script>
<script src="js/main.js?v=4"></script>
<script src="js/main.js?v=5"></script>

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
Expand Down
59 changes: 49 additions & 10 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
var SQL_FROM_REGEX = /FROM\s+([^\s;]+)/mi;
var SQL_LIMIT_REGEX = /LIMIT\s+(\d+)(?:\s*,\s*(\d+))?/mi;
var SQL_SELECT_REGEX = /SELECT\s+[^;]+\s+FROM\s+/mi;

var db = null;
var rowCounts = [];
var editor = ace.edit("sql-editor");
var bottomBarDefaultPos = null, bottomBarDisplayStyle = null;
var errorBox = $("#error");
var lastCachedQueryCount = {};

var fileReaderOpts = {
readAsDefault: "ArrayBuffer", on: {
Expand Down Expand Up @@ -127,9 +129,34 @@ windowResize();


function getTableRowsCount(name) {
var sel = db.prepare("SELECT COUNT(*) FROM '" + name + "'");
var sel = db.prepare("SELECT COUNT(*) AS count FROM '" + name + "'");
if (sel.step()) {
return sel.getAsObject()["COUNT(*)"]; //weird but works!
return sel.getAsObject().count;
} else {
return -1;
}
}

function getQueryRowCount(query) {
if (query === lastCachedQueryCount.select) {
return lastCachedQueryCount.count;
}

var queryReplaced = query.replace(SQL_SELECT_REGEX, "SELECT COUNT(*) AS count_sv FROM ");

if (queryReplaced !== query) {
queryReplaced = queryReplaced.replace(SQL_LIMIT_REGEX, "");
var sel = db.prepare(queryReplaced);
if (sel.step()) {
var count = sel.getAsObject().count_sv;

lastCachedQueryCount.select = query;
lastCachedQueryCount.count = count;

return count;
} else {
return -1;
}
} else {
return -1;
}
Expand Down Expand Up @@ -217,19 +244,29 @@ function parseLimitFromQuery(query, tableName) {
var result = {};

if (sqlRegex.length > 2 && typeof sqlRegex[2] !== "undefined") {
result.offset = sqlRegex[1];
result.max = sqlRegex[2];
result.offset = parseInt(sqlRegex[1]);
result.max = parseInt(sqlRegex[2]);
} else {
result.offset = 0;
result.max = sqlRegex[1];
result.max = parseInt(sqlRegex[1]);
}

if (result.max == 0) {
result.pages = 0;
result.currentPage = 0;
return result;
}

if (typeof tableName === "undefined") {
tableName = getTableNameFromQuery(query);
}

result.pages = Math.ceil(rowCounts[tableName] / result.max);
var queryRowsCount = getQueryRowCount(query);
if (queryRowsCount != -1) {
result.pages = Math.ceil(queryRowsCount / result.max);
}
result.currentPage = Math.floor(result.offset / result.max) + 1;
result.rowCount = queryRowsCount;

return result;
} else {
Expand Down Expand Up @@ -263,9 +300,12 @@ function setPage(el, next) {

function refreshPagination(query, tableName) {
var limit = parseLimitFromQuery(query, tableName);
if (limit !== null) {
if (limit !== null && limit.pages > 0) {

$("#pager").text(limit.currentPage + " / " + limit.pages);
var pager = $("#pager");
pager.attr("title", "Row count: " + limit.rowCount);
pager.tooltip('fixTitle');
pager.text(limit.currentPage + " / " + limit.pages);

if (limit.currentPage <= 1) {
$("#page-prev").addClass("disabled");
Expand Down Expand Up @@ -335,10 +375,9 @@ function renderQuery(query) {
tbody.append(tr);
}

$('[data-toggle="tooltip"]').tooltip({html: true});

refreshPagination(query, tableName);

$('[data-toggle="tooltip"]').tooltip({html: true});
dataBox.editableTableWidget();

setTimeout(function () {
Expand Down

0 comments on commit ed608d6

Please sign in to comment.