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

Added SQL INSERT feature #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Allows you to seamlessly switch between CSV, TSV, HTML, SQL, markdown and a litt
| Preserves alignment | Between all views |
| Markdown formatting | Adds spaces, makes things more legible |
| Form view | Create tables with buttons! |
| INSERT | Generate an INSERT query to add data to a table |

I've made this as a little learning exercise to play with Primer, Atom, GitHub Desktop and GitHub itself.

Expand Down
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<a href="#" class="tabnav-tab selected" id="tab-csv" onclick="changeTab('csv');"><span class="octicon octicon-file-text"></span> CSV/TSV</a>
<a href="#" class="tabnav-tab" id="tab-md" onclick="changeTab('md');"><span class="octicon octicon-code"></span> Markdown</a>
<a href="#" class="tabnav-tab" id="tab-sql" onclick="changeTab('sql');"><span class="octicon octicon-database"></span> SQL</a>
<a href="#" class="tabnav-tab" id="tab-insert" onclick="changeTab('insert');"><span class="octicon octicon-arrow-right"></span> INSERT</a>
<a href="#" class="tabnav-tab" id="tab-html" onclick="changeTab('html');"><span class="octicon octicon-file-code"></span> HTML</a>
<a href="#" class="tabnav-tab" id="tab-form" onclick="changeTab('form');"><span class="octicon octicon-pencil"></span> Form</a>
<a href="#" class="tabnav-tab" id="tab-preview" onclick="changeTab('preview');"><span class="octicon octicon-browser"></span> Preview</a>
Expand Down
114 changes: 112 additions & 2 deletions magic.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ function changeTab(newTab) {
if (tab==="csv") { array = csv2array(input); }
if (tab==="html") { array = html2array(input); }
if (tab==="sql") { array = sql2array(input); }
if (tab==="insert") { array = insert2array(input); }
if (tab==="form") { array = form2array(); }
if (tab==="preview") { array = array_storge; }

Expand All @@ -169,6 +170,11 @@ function changeTab(newTab) {
new_layout=true;
}

if (newTab==="insert") {
output = array2insert(array);
new_layout=true;
}

if (newTab==="form") {
output = array2form(array);
new_layout=false;
Expand All @@ -194,13 +200,13 @@ function changeTab(newTab) {
$('.tabnav-tab').removeClass('selected');
$('#tab-'+newTab).addClass('selected');

if ((tab==='md')||tab==='sql') {
if ((tab==='md')||tab==='sql'||tab==='insert') {
$('textarea').removeClass('md');
if (tab==='md') $('#md-options').hide();
if (tab==='sql') $('#sql-info').hide();
}

if ((newTab==='md')||newTab==='sql') {
if ((newTab==='md')||newTab==='sql'||newTab==='insert') {
$('textarea').addClass('md');
if (newTab==='md') $('#md-options').show();
if (newTab==='sql') $('#sql-info').show();
Expand Down Expand Up @@ -755,6 +761,110 @@ function array2sqlDashes(cell_sizes, rowlength) {

}

function insert2array(insert) {

var array = [];array[0] = [];

// Strip all newlines & double spaces
insert = insert.replace(/\n/g, '');
insert = insert.replace(/ /g, ' ');

// Split into header & body
var parts = insert.split(') VALUES ');
var n = parts[0].indexOf('(')+1;
var header = parts[0].substring(n, parts[0].length);

// Split header into columns
var columns = header.split(', ');

for (var c = 0; c < columns.length; c++) {

var column = columns[c];

// Remove escaping slashes for single and double quotes
column = column.replace(/\"/g, '"');
column = column.replace(/\'/g, "'");

array[0][c] = column;

}

// Strip opening & closing brackets + ending semi-colon
var body = parts[1].substring(1, parts[1].length-2);

// Split body into rows
var rows = body.split('), (');

for (var r = 0; r < rows.length; r++) {

var cells = rows[r].replace(/', /g, "'#$#").split('#$#');

array[r+1] = [];

for (var c = 0; c < cells.length; c++) {

var cell = cells[c];

// Strip opening and closing single qoutes
cell = cell.substring(1, cell.length-1);

// Remove escaping slashes for single and double quotes
cell = cell.replace("\\'", "'");
cell = cell.replace('\\"', '"');

array[r+1][c] = cell;

}

}

return array;
}

function array2insert(array) {

var insert = '';

for (var r = 0; r < array.length; r++) {

var row = array[r];

insert += ' (';

for (var c = 0; c < row.length; c++) {

var item = row[c];

// Escape quotes
item = item.replace(/"/g, '\\"');
item = item.replace(/'/g, "\\'");

if (c>0) { insert += ', '; }
insert += "'"+ item +"'";

}

// Last row should end with a semi-colon instead of a comma
insert += (r===(array.length-1)) ? ');\n' : '),\n';

// First row are the columns
if (r===0) {
insert = insert.replace(' (', '(');
insert = insert.replace('(\'', '(');
insert = insert.replace('\')', ')');
insert = insert.replace(/', '/g, ', ');
insert = insert.replace('),\n', ')');

var columns = insert;
insert = '';
}

}

return 'INSERT INTO [TABLENAME] '+ columns +' VALUES\n'+ insert;

}

function form2array() {

var array = [];
Expand Down