in gui/frontend/src/modules/db-editor/ConnectionTab.tsx [2880:3018]
private convertResultSetToText(rows: IDictionary[], columns: IColumnInfo[], started: boolean, finished: boolean) {
let result = "";
const convertLineBreaks = (value: string): string => {
const result = value.replaceAll(/\r/g, "\\r");
return result.replaceAll(/\n/g, "\\n");
};
// Compute the width of each column, based on the column name and the data, if not yet done.
if (columns.length > 0 && columns[0].width === undefined) {
columns.forEach((column: IColumnInfo) => {
column.title = convertLineBreaks(column.title);
column.width = column.title.length;
switch (column.dataType.type) {
case DBDataType.TinyInt:
case DBDataType.SmallInt:
case DBDataType.MediumInt:
case DBDataType.Int:
case DBDataType.Bigint:
case DBDataType.UInteger:
case DBDataType.Float:
case DBDataType.Real:
case DBDataType.Double:
case DBDataType.Decimal: {
column.rightAlign = true;
break;
}
default:
}
});
}
rows.forEach((row) => {
let value;
columns.forEach((column) => {
switch (column.dataType.type) {
// Binary data
case DBDataType.TinyBlob:
case DBDataType.Blob:
case DBDataType.MediumBlob:
case DBDataType.LongBlob:
case DBDataType.Binary:
case DBDataType.Varbinary: {
value = formatBase64ToHex(String(row[column.field]), 64);
break;
}
default: {
value = String(row[column.field]);
break;
}
}
row[column.field] = value;
let length = value.length;
// Check if the value has line breaks, if so, find the longest line
if (/\r|\n/.exec(value)) {
let maxLength = 0;
const lines = value.split(/\r|\n/);
for (const ln of lines) {
if (ln.length > maxLength) {
maxLength = ln.length;
}
}
length = maxLength;
}
if (length > (column.width ?? 0)) {
column.width = length;
}
});
});
const separator = columns.reduce((previous, current) => {
return previous + "-".repeat(current.width! + 2) + "+";
}, "+");
if (started) {
// Render the column header.
result += separator + "\n";
const line = columns.reduce((previous, current, index) => {
return previous + " " + current.title.padEnd(columns[index].width!) + " |";
}, "|");
result += line + "\n" + separator + "\n";
}
// Render the data.
rows.forEach((row) => {
const line = columns.reduce((previous, current, index) => {
if (columns[index].rightAlign) {
return previous + " " + String(row[current.field]).padStart(columns[index].width!) + " |";
}
if (/\r|\n/.exec(String(row[current.field]))) {
let multiLineStr = previous;
let pre = "| ";
let post = " ";
columns.forEach((c, i) => {
if (i < index) {
pre += `${" ".repeat(c.width!)} | `;
} else if (i > index) {
post += `${" ".repeat(c.width!)} | `;
}
});
pre = pre.slice(0, -1);
post = post.slice(0, -1);
const lines = String(row[current.field]).split(/\r|\n/);
lines.forEach((ln, i) => {
if (i > 0) {
multiLineStr += pre;
}
multiLineStr += ` ${ln}${" ".repeat(columns[index].width! - ln.length)} |`;
if (i < lines.length - 1) {
multiLineStr += post + "\n";
}
});
return multiLineStr;
}
return previous + " " + String(row[current.field]).padEnd(columns[index].width!) + " |";
}, "|");
result += line + "\n";
});
if (finished) {
result += separator + "\n";
}
return result;
}