Skip to content

Commit ff21893

Browse files
committed
Added copy as insert and copy as update
Fixes #649
1 parent 941c0ac commit ff21893

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

static/index.html

+2
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ <h3 class="text-center">SSH Connection</h3>
333333
<ul class="dropdown-menu" role="menu">
334334
<li><a href="#" data-action="display_value">Display Value</a></li>
335335
<li><a href="#" data-action="copy_value">Copy Value</a></li>
336+
<li><a href="#" data-action="copy_as_insert">Copy row as INSERT</a></li>
337+
<li><a href="#" data-action="copy_as_update">Copy row as UPDATE</a></li>
336338
<li><a href="#" data-action="filter_by_value">Filter Rows By Value</a></li>
337339
</ul>
338340
</div>

static/js/app.js

+72
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,12 @@ function bindTableHeaderMenu() {
12031203
var menuItem = $(e.target);
12041204

12051205
switch(menuItem.data("action")) {
1206+
case "copy_as_insert":
1207+
copyAsInsert(context);
1208+
break;
1209+
case "copy_as_update":
1210+
copyAsUpdate(context);
1211+
break;
12061212
case "display_value":
12071213
var value = $(context).text();
12081214
$("#content_modal .content").text(value);
@@ -1225,6 +1231,72 @@ function bindTableHeaderMenu() {
12251231
});
12261232
}
12271233

1234+
function copyAsInsert(context){
1235+
var values = getValuesFromContext(context);
1236+
var columns = getColumnsFromResults();
1237+
var tableName = $("#results").data("table");
1238+
if(tableName === undefined){
1239+
alert('table must be selected.');
1240+
return;
1241+
}
1242+
var str = "INSERT INTO "+tableName+"("+columns.join(',')+") VALUES("+values.map(function(o){return o.value}).join(",")+")";
1243+
copyToClipboard(str);
1244+
}
1245+
1246+
function copyAsUpdate(context){
1247+
var values = getValuesFromContext(context);
1248+
var columns = getColumnsFromResults();
1249+
var tableName = $("#results").data("table");
1250+
if(tableName === undefined){
1251+
alert('table must be selected.');
1252+
return;
1253+
}
1254+
var where = [];
1255+
var set = [];
1256+
columns.forEach(function(row, index){
1257+
var val = values[index];
1258+
set.push(row+"="+val.value);
1259+
if(val.isNull){
1260+
where.push(row+" IS "+val.value);
1261+
return;
1262+
}
1263+
where.push(row+"="+val.value);
1264+
})
1265+
var str = "UPDATE "+tableName+" SET "+set.join(',')+' WHERE '+ where.join(' AND ');
1266+
copyToClipboard(str);
1267+
}
1268+
1269+
function getColumnsFromResults(){
1270+
let columns = [];
1271+
$("#results_header th").each(function(){
1272+
columns.push(this.innerText);
1273+
})
1274+
return columns;
1275+
}
1276+
1277+
function getValuesFromContext(context){
1278+
let values = [];
1279+
$(context).parent().children().each(function(){
1280+
const isNumber = !isNaN(this.innerText);
1281+
const isNull = $(this).find("span[class*='null']").length;
1282+
let obj = {isNull:false, value:''};
1283+
if (isNull){
1284+
obj.isNull = true;
1285+
obj.value = 'NULL';
1286+
values.push(obj);
1287+
return;
1288+
}
1289+
if(isNumber){
1290+
obj.value = this.innerText;
1291+
values.push(obj);
1292+
return;
1293+
}
1294+
obj.value = "'"+this.innerText+"'";
1295+
values.push(obj);
1296+
})
1297+
return values;
1298+
}
1299+
12281300
function bindCurrentDatabaseMenu() {
12291301
$("#current_database").contextmenu({
12301302
target: "#current_database_context_menu",

0 commit comments

Comments
 (0)