diff --git a/generalFuncs.js b/generalFuncs.js index 296de1a..4d6283a 100644 --- a/generalFuncs.js +++ b/generalFuncs.js @@ -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(); @@ -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); @@ -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); @@ -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`); } @@ -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 = []; diff --git a/package.json b/package.json index f900481..faecda5 100644 --- a/package.json +++ b/package.json @@ -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" } } },