script to convert MySQL dumps from INSERT statements to INSERT ... ON DUPLICATE UPDATE statements. (Converted a Fiddle that changed INSERT into UPDATE. For limitations, See SO: https://stackoverflow.com/a/55337940/5411817)
$(document).ready(function() {
$('#btn').click(function() {
// to store the output result
let out_sh = "";
//const insertQuery = $('#query').val();
const userInput = $('#query').val();
// If query is written as severan queries (single line inserts rather than multi row inserts in a single statement, then need to loop through all the statements).
// IF there is more than 1 semicolon, or maybe better yet, more than one
// INSERT INTO statement..
// I'll do a split on ; instead ! Cus I want to KEEP the INSERT INTO portion
// and I want to get rid of the ; anyway
// NO, this is bad. ANY query containing a ; in its data would cause this to break
//const insertQueries = userInput.split(';');
//const insertQueries = userInput.trim().split('INSERT INTO ');
// do a split on INSERT only, I'm Using INTO to do another split later on.
// Note this is terrible hack.
// WORSE, That I dp splits on parens and commas!!!
// Wont work on REAL data !!!
const insertQueries = userInput.trim().split('INSERT ');
// delete the last part, if it does not contain an INSERT statement
// multiline queries should end in a ;
// however, if there is only a single statement, the user may not have an ending ; (ie MySQL Workbench Snippets by default do not include ;)
/* if (true) {}
console.log('num insertQueries: ', insertQueries.length);
console.log('first: ', insertQueries[0]);
console.log('[last]: ', insertQueries[insertQueries.length-1]);
console.log('the insertQueries: ', insertQueries);
*/
// check endpoints of split, if the "query is empty delete it
// generally will be first element if split on INSERT INTO
// or Last element (if split on semicolon - bad idea b/c data has semicolons)
// Could do filter instead, and do something more robust,
// would rather just DELETE the bad input queries.
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.