Skip to content
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
30 changes: 19 additions & 11 deletions generalFuncs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ const extensionId = 'lz-scripts';
async function runForInsert( query) {
let allowIdentity = vscode.workspace.getConfiguration('LzScripts').get('addIdentityColumns') === true;
let allowMultipleInsert = vscode.workspace.getConfiguration('LzScripts').get('allowInsertPerRow') === true;
let includeColumnNamesAsComment = vscode.workspace.getConfiguration('LzScripts').get('includeColumnNamesAsComment') === true;
let insetStr = [];

let tableInfos = extractTableInfo(query);
let tableInfo = tableInfos.length > 0 ? tableInfos[0] : {};
let table = IsEmptyObj(tableInfo) ? '' : tableInfo.table.replaceAll('[','').replaceAll(']','');
let table = IsEmptyObj(tableInfo) ? '' : tableInfo.table.replaceAll('[','').replaceAll(']','').trim();

const mssqlApi = await getMssqlApi();
const connectionUri = await connectToActiveEditor();
Expand Down Expand Up @@ -68,11 +69,14 @@ async function runForInsert( query) {
if (data.columnInfo[col].isIdentity === true && !allowIdentity)
continue;

if (data.columnInfo[col].dataTypeName !== 'timestamp')
dataStr += (`/* ${data.columnInfo[col].columnName} */ ` + (
data.rows[row][col].isNull === true ? 'null' :
if (data.columnInfo[col].dataTypeName !== 'timestamp') {
if (includeColumnNamesAsComment) {
dataStr += `/* ${data.columnInfo[col].columnName} */ `;
}
dataStr += ((data.rows[row][col].isNull === true ? 'null' :
getValue(data.rows[row][col].displayValue, data.columnInfo[col]))
+ ' ,');
}
}

dataStr = dataStr.slice(0, dataStr.length - 1);
Expand All @@ -90,11 +94,14 @@ async function runForInsert( query) {
if (data.columnInfo[col].isIdentity === true && !allowIdentity)
continue;

if (data.columnInfo[col].dataTypeName !== 'timestamp')
dataStr += (`/* ${data.columnInfo[col].columnName} */ ` + (
data.rows[row][col].isNull === true ? 'null' :
if (data.columnInfo[col].dataTypeName !== 'timestamp') {
if (includeColumnNamesAsComment) {
dataStr += `/* ${data.columnInfo[col].columnName} */ `;
}
dataStr += ((data.rows[row][col].isNull === true ? 'null' :
getValue(data.rows[row][col].displayValue, data.columnInfo[col]))
+ ' ,');
}
}

dataStr = dataStr.slice(0, dataStr.length - 1);
Expand All @@ -105,18 +112,19 @@ async function runForInsert( query) {
}


return PostProcessing(insetStr, tableInfo.full, vscode.workspace.getConfiguration('LzScripts'));
return PostProcessing(insetStr, tableInfo.full, data, vscode.workspace.getConfiguration('LzScripts'));
}


/**
* @param {string[]} insetStr
* @param {any} data
* @param {vscode.WorkspaceConfiguration} config
* @param {string} table
*/
function PostProcessing(insetStr, table, config) {
function PostProcessing(insetStr, table, data, config) {
let allowIdentity = config.get('addIdentityColumns') === true;
if (allowIdentity) {
if (allowIdentity && data.columnInfo.some(x=>x.isIdentity === true)) {
insetStr.unshift(`set identity_insert ${table} on; \ngo; \n`);
insetStr.push(`set identity_insert ${table} off; \ngo; \n`);
}
Expand Down Expand Up @@ -199,7 +207,7 @@ function IsEmptyObj(obj)
}

function extractTableInfo(sql) {
let regex = /\bFROM\s+([\w\.\[\]]+)|\bJOIN\s+([\w\.\[\]]+)|\bUPDATE\s+([\w\.\[\]]+)|\bINTO\s+([\w\.\[\]]+)/ig;
let regex = /\bFROM\s+([\w\.\s\[\]]+)|\bJOIN\s+([\w\.\s\[\]]+)|\bUPDATE\s+([\w\.\s\[\]]+)|\bINTO\s+([\w\.\s\[\]]+)/ig;
let match;
let tables = [];

Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
"type": "boolean",
"default": false,
"description": "Generate Insert for each data row found"
},
"LzScripts.includeColumnNamesAsComment": {
"type": "boolean",
"default": true,
"description": "Include column name as a comment in values"
}
}
},
Expand Down