new(basic.gblib): Allow MERGE keyword in storage #380.

This commit is contained in:
Rodrigo Rodriguez 2023-10-07 18:40:10 -03:00
parent 5f751cfbe7
commit f163204c15
2 changed files with 84 additions and 41 deletions

View file

@ -1105,7 +1105,7 @@ export class KeywordsExpressions {
}); });
let fieldsNames = fieldsNamesOnly.join(','); let fieldsNames = fieldsNamesOnly.join(',');
return `await sys.saveToStorage({pid: pid, table: "${table}", fields: [${fieldsAsText}], fieldsNames: [${fieldsNames}] })`; return `await sys.saveToStorage({pid: pid, table: "${table}", fieldsValues: [${fieldsAsText}], fieldsNames: [${fieldsNames}] })`;
} }
]; ];

View file

@ -654,9 +654,9 @@ export class SystemKeywords {
* @example SAVE "Billing", columnName1, columnName2 * @example SAVE "Billing", columnName1, columnName2
* *
*/ */
public async saveToStorage({ pid, table, fields, fieldsNames }) : Promise<any> { public async saveToStorage({ pid, table, fieldsValues, fieldsNames }): Promise<any> {
GBLog.info(`BASIC: Saving '${table}' (SAVE). Values: ${fields.join(',')}.`); GBLog.info(`BASIC: Saving '${table}' (SAVE). Values: ${fieldsValues.join(',')}.`);
const minBoot = GBServer.globals.minBoot as any; const minBoot = GBServer.globals.minBoot as any;
const definition = minBoot.core.sequelize.models[table]; const definition = minBoot.core.sequelize.models[table];
@ -665,7 +665,7 @@ export class SystemKeywords {
fieldsNames.forEach(field => { fieldsNames.forEach(field => {
field = field.charAt(0).toUpperCase() + field.slice(1); field = field.charAt(0).toUpperCase() + field.slice(1);
data[field] = fields[index++]; data[field] = fieldsValues[index++];
}); });
return await definition.create(data); return await definition.create(data);
@ -1503,7 +1503,20 @@ export class SystemKeywords {
public generatePassword(pid) { public generatePassword(pid) {
return GBAdminService.getRndPassword(); return GBAdminService.getRndPassword();
} }
static aa;
private flattenJSON(obj, res, extraKey) {
for (let key in obj) {
if (typeof obj[key] !== 'object') {
res[extraKey + key] = obj[key];
} else {
this.flattenJSON(obj[key], res, `${extraKey}${key}.`);
};
};
return res;
}
static aa;
/** /**
* Calls any REST API by using GET HTTP method. * Calls any REST API by using GET HTTP method.
* *
@ -1772,7 +1785,7 @@ static aa;
let result; let result;
if (!SystemKeywords.aa){ if (!SystemKeywords.aa) {
SystemKeywords.aa = 1; SystemKeywords.aa = 1;
return r1; return r1;
} else { } else {
@ -1793,7 +1806,7 @@ static aa;
return null; return null;
} }
const res = JSON.parse(await result.text()); let res = JSON.parse(await result.text());
if (pageMode === "auto") { if (pageMode === "auto") {
@ -2073,11 +2086,9 @@ static aa;
* *
*/ */
public async merge({ pid, file, data, key1, key2 }): Promise<any> { public async merge({ pid, file, data, key1, key2 }): Promise<any> {
GBLog.info(`BASIC: MERGE running on ${file} and key1: ${key1}, key2: ${key2}...`);
GBLog.info(`BASIC: MERGE running on ${file} and key1: ${key1}, key2: ${key2}...`);
const { min, user, params } = await DialogKeywords.getProcessInfo(pid); const { min, user, params } = await DialogKeywords.getProcessInfo(pid);
const botId = min.instance.botId;
const path = DialogKeywords.getGBAIPath(botId, 'gbdata');
// MAX LINES property. // MAX LINES property.
@ -2090,8 +2101,21 @@ static aa;
// Choose data sources based on file type (HTML Table, data variable or sheet file) // Choose data sources based on file type (HTML Table, data variable or sheet file)
let storage = file.indexOf('.xlsx') !== -1;
let results; let results;
let header, rows; let header = [], rows = [];
const minBoot = GBServer.globals.minBoot;
let t;
if (storage) {
t = minBoot.core.sequelize.models[file];
rows = await t.findAll({});
header = rows['dataNames'];
} else {
const botId = min.instance.botId;
const path = DialogKeywords.getGBAIPath(botId, 'gbdata');
let { baseUrl, client } = await GBDeployer.internalGetDriveClient(min); let { baseUrl, client } = await GBDeployer.internalGetDriveClient(min);
let document; let document;
@ -2109,6 +2133,7 @@ static aa;
header = results.text[0]; header = results.text[0];
rows = results.text; rows = results.text;
}
// As BASIC uses arrays starting with 1 (one) as index, // As BASIC uses arrays starting with 1 (one) as index,
// a ghost element is added at 0 (zero) position. // a ghost element is added at 0 (zero) position.
@ -2152,12 +2177,15 @@ static aa;
// Scans all items in incoming data. // Scans all items in incoming data.
for (let i = 1; i < data.length; i++) { for (let i = 1; i < data.length; i++) {
// Scans all sheet lines and compare keys. // Scans all sheet lines and compare keys.
const row = data[i]; const row = data[i];
let found; let found;
let key1Value;
if (key1Index) { if (key1Index) {
const key1Value = row[key1]; key1Value = row[key1];
const foundRow = key1Index[key1Value]; const foundRow = key1Index[key1Value];
if (foundRow) { if (foundRow) {
found = table[foundRow[0]]; found = table[foundRow[0]];
@ -2169,6 +2197,13 @@ static aa;
for (let j = 0; j < keys.length; j++) { for (let j = 0; j < keys.length; j++) {
const columnName = header[j]; const columnName = header[j];
const value = row[keys[j]]; const value = row[keys[j]];
if (storage) {
const obj = { id: keys[j], columnName: value };
await t.update(obj, { where: { key1: key1Value } });
} else {
const cell = `${this.numberToLetters(j)}${i + 1}`; const cell = `${this.numberToLetters(j)}${i + 1}`;
const address = `${cell}:${cell}`; const address = `${cell}:${cell}`;
@ -2177,14 +2212,22 @@ static aa;
merges++; merges++;
} }
} }
}
} else { } else {
let args = []; let fieldsValues = [];
let keys = Object.keys(row); let fieldsNames = Object.keys(row);
for (let j = 0; j < keys.length; j++) { for (let j = 0; j < fieldsNames.length; j++) {
args.push(row[keys[j]]); fieldsValues.push(row[fieldsNames[j]]);
} }
await this.save({ pid, file, args }); if (storage) {
await this.saveToStorage({ pid, table: file, fieldsValues, fieldsNames });
}
else {
await this.save({ pid, file, args: fieldsValues });
}
adds++; adds++;
} }
} }