Skip to content

Commit

Permalink
Added copy as insert and copy as update
Browse files Browse the repository at this point in the history
Fixes #649
  • Loading branch information
jonaz committed Feb 28, 2023
1 parent 941c0ac commit 59b43e2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
2 changes: 2 additions & 0 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ <h3 class="text-center">SSH Connection</h3>
<ul class="dropdown-menu" role="menu">
<li><a href="#" data-action="display_value">Display Value</a></li>
<li><a href="#" data-action="copy_value">Copy Value</a></li>
<li><a href="#" data-action="copy_as_insert">Copy row as INSERT</a></li>
<li><a href="#" data-action="copy_as_update">Copy row as UPDATE</a></li>
<li><a href="#" data-action="filter_by_value">Filter Rows By Value</a></li>
</ul>
</div>
Expand Down
59 changes: 59 additions & 0 deletions static/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,34 @@ function bindTableHeaderMenu() {
onItem: function(context, e) {
var menuItem = $(e.target);

// .text()
switch(menuItem.data("action")) {
case "copy_as_insert":
var values = getValuesFromContext(context);
var columns = getColumnsFromResults();
var tableName = $("#results").data("table");
var str = "INSERT INTO "+tableName+"("+columns.join(',')+") VALUES("+values.map(function(o){return o.value}).join(",")+")";
copyToClipboard(str);
break;
case "copy_as_update":
var values = getValuesFromContext(context);
var columns = getColumnsFromResults();
var tableName = $("#results").data("table");
var where = [];
var set = [];
columns.forEach(function(row, index){
var val = values[index];
set.push(row+"="+val.value);
if(val.isNull){
where.push(row+" IS "+val.value);
return;
}
where.push(row+"="+val.value);
})
var str = "UPDATE "+tableName+" SET "+set.join(',')+' WHERE '+ where.join(' AND ');
copyToClipboard(str);

break;
case "display_value":
var value = $(context).text();
$("#content_modal .content").text(value);
Expand All @@ -1225,6 +1252,38 @@ function bindTableHeaderMenu() {
});
}

function getColumnsFromResults(){
let columns = [];
$("#results_header th").each(function(){
columns.push(this.innerText);
})
return columns;
}

function getValuesFromContext(context){
let values = [];
$(context).parent().children().each(function(){
const isNumber = !isNaN(this.innerText);
const isNull = $(this).find("span[class*='null']").length;
let obj = {isNull:false, value:''};
if (isNull){
obj.isNull = true;
obj.value = 'NULL';
values.push(obj);
return;
}
if(isNumber){
obj.value = this.innerText;
values.push(obj);
return;
}
// values.push("'"+this.innerText+"'");
obj.value = "'"+this.innerText+"'";
values.push(obj);
})
return values;
}

function bindCurrentDatabaseMenu() {
$("#current_database").contextmenu({
target: "#current_database_context_menu",
Expand Down

0 comments on commit 59b43e2

Please sign in to comment.