fix(basic.gblib): Allow MERGE keyword in storage #380. @othonlima
This commit is contained in:
parent
1447f31fb6
commit
f3205594f0
2 changed files with 42 additions and 27 deletions
|
@ -96,6 +96,10 @@ export class GuaribasConversation extends Model<GuaribasConversation> {
|
||||||
@Column(DataType.DATE)
|
@Column(DataType.DATE)
|
||||||
declare createdAt: Date;
|
declare createdAt: Date;
|
||||||
|
|
||||||
|
@Column(DataType.DATE)
|
||||||
|
@UpdatedAt
|
||||||
|
declare updatedAt: Date;
|
||||||
|
|
||||||
@Column(DataType.STRING(255))
|
@Column(DataType.STRING(255))
|
||||||
declare text: string;
|
declare text: string;
|
||||||
|
|
||||||
|
|
|
@ -666,9 +666,9 @@ export class SystemKeywords {
|
||||||
let i = 0;
|
let i = 0;
|
||||||
Object.keys(fieldsValues).forEach(fieldSrc => {
|
Object.keys(fieldsValues).forEach(fieldSrc => {
|
||||||
const field = fieldsNames[i].charAt(0).toUpperCase() + fieldsNames[i].slice(1);
|
const field = fieldsNames[i].charAt(0).toUpperCase() + fieldsNames[i].slice(1);
|
||||||
|
|
||||||
dst[field] = fieldsValues[fieldSrc];
|
dst[field] = fieldsValues[fieldSrc];
|
||||||
|
|
||||||
i++;
|
i++;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1934,6 +1934,7 @@ export class SystemKeywords {
|
||||||
const minBoot = GBServer.globals.minBoot;
|
const minBoot = GBServer.globals.minBoot;
|
||||||
let t;
|
let t;
|
||||||
let fieldsNames = [];
|
let fieldsNames = [];
|
||||||
|
let fieldsSizes = [];
|
||||||
|
|
||||||
if (storage) {
|
if (storage) {
|
||||||
t = minBoot.core.sequelize.models[file];
|
t = minBoot.core.sequelize.models[file];
|
||||||
|
@ -1945,6 +1946,10 @@ export class SystemKeywords {
|
||||||
fieldsNames.push(e);
|
fieldsNames.push(e);
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Object.keys(t.fieldRawAttributesMap).forEach(e => {
|
||||||
|
fieldsSizes.push(t.fieldRawAttributesMap[e].size);
|
||||||
|
})
|
||||||
|
|
||||||
rows = await t.findAll({});
|
rows = await t.findAll({});
|
||||||
if (rows.length > 0) {
|
if (rows.length > 0) {
|
||||||
header = Object.keys(rows[0].dataValues)
|
header = Object.keys(rows[0].dataValues)
|
||||||
|
@ -2011,7 +2016,7 @@ export class SystemKeywords {
|
||||||
|
|
||||||
// Scans all items in incoming data.
|
// Scans all items in incoming data.
|
||||||
|
|
||||||
for (let i = 1; i < data.length; i++) {
|
for (let i = 0; i < data.length; i++) {
|
||||||
|
|
||||||
// Scans all sheet lines and compare keys.
|
// Scans all sheet lines and compare keys.
|
||||||
|
|
||||||
|
@ -2028,8 +2033,8 @@ export class SystemKeywords {
|
||||||
|
|
||||||
key1 = key1.charAt(0).toLowerCase() + key1.slice(1);
|
key1 = key1.charAt(0).toLowerCase() + key1.slice(1);
|
||||||
|
|
||||||
Object.keys(row).forEach(e=>{
|
Object.keys(row).forEach(e => {
|
||||||
if (e.toLowerCase().indexOf(key1.toLowerCase()) !== -1){
|
if (e.toLowerCase().indexOf(key1.toLowerCase()) !== -1) {
|
||||||
key1Value = row[e];
|
key1Value = row[e];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -2041,36 +2046,37 @@ export class SystemKeywords {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (found) {
|
if (found) {
|
||||||
|
let merge = false;
|
||||||
for (let j = 0; j < header.length; j++) {
|
for (let j = 0; j < header.length; j++) {
|
||||||
|
|
||||||
const columnName = header[j];
|
const columnName = header[j];
|
||||||
|
let columnNameFound = false;
|
||||||
|
|
||||||
let value;
|
let value;
|
||||||
Object.keys(row).forEach(e=>{
|
Object.keys(row).forEach(e => {
|
||||||
if (columnName.toLowerCase().indexOf(e.toLowerCase()) !== -1){
|
if (columnName.toLowerCase().indexOf(e.toLowerCase()) !== -1) {
|
||||||
value = row[e];
|
value = row[e];
|
||||||
|
columnNameFound = true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (value === undefined) { value = null; }
|
if (value === undefined) { value = null; }
|
||||||
|
|
||||||
let valueFound;
|
let valueFound;
|
||||||
Object.keys(found).forEach(e=>{
|
Object.keys(found).forEach(e => {
|
||||||
if (columnName.toLowerCase().indexOf(e.toLowerCase()) !== -1){
|
if (columnName.toLowerCase().indexOf(e.toLowerCase()) !== -1) {
|
||||||
valueFound = found[e];
|
valueFound = found[e];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (value != valueFound) {
|
if (value != valueFound && columnNameFound) {
|
||||||
|
|
||||||
if (storage) {
|
if (storage) {
|
||||||
|
|
||||||
let obj = {};
|
let obj = {};
|
||||||
obj[columnName]= value;
|
obj[columnName] = value;
|
||||||
let criteria = {};
|
let criteria = {};
|
||||||
criteria[ key1Original] = key1Value;
|
criteria[key1Original] = key1Value;
|
||||||
await t.update(obj, { where: criteria });
|
await t.update(obj, { where: criteria });
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
@ -2080,30 +2086,35 @@ export class SystemKeywords {
|
||||||
|
|
||||||
await this.set({ pid, handle: null, file, address, value });
|
await this.set({ pid, handle: null, file, address, value });
|
||||||
}
|
}
|
||||||
merges++;
|
merge = true;
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
skipped++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
merge ? merges++ : skipped++;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
let fieldsValues = [];
|
let fieldsValues = [];
|
||||||
|
|
||||||
for (let j = 0; j < fieldsNames.length; j++) {
|
for (let j = 0; j < fieldsNames.length; j++) {
|
||||||
let add = false;
|
let add = false;
|
||||||
Object.keys(row).forEach(p=>{
|
Object.keys(row).forEach(p => {
|
||||||
if (fieldsNames[j].toLowerCase().indexOf(p.toLowerCase()) !== -1){
|
if (fieldsNames[j].toLowerCase().indexOf(p.toLowerCase()) !== -1) {
|
||||||
fieldsValues.push(row[p]);
|
|
||||||
|
let value = row[p];
|
||||||
|
if (typeof (value) === 'string') {
|
||||||
|
value = value.substr(0, fieldsSizes[j]);
|
||||||
|
}
|
||||||
|
|
||||||
|
fieldsValues.push(value);
|
||||||
add = true;
|
add = true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (!add){
|
if (!add) {
|
||||||
fieldsValues.push(null);
|
fieldsValues.push(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (storage) {
|
if (storage) {
|
||||||
await this.saveToStorage({ pid, table: file, fieldsValues, fieldsNames });
|
await this.saveToStorage({ pid, table: file, fieldsValues, fieldsNames });
|
||||||
|
@ -2120,7 +2131,7 @@ export class SystemKeywords {
|
||||||
GBLog.info(`BASIC: MERGE ran but updated zero rows.`);
|
GBLog.info(`BASIC: MERGE ran but updated zero rows.`);
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
GBLog.info(`BASIC: MERGE updated (merges:${merges}, additions:${adds}, skipped: ${skipped}.`);
|
GBLog.info(`BASIC: MERGE updated (merges:${merges}, additions:${adds}, skipped: ${skipped}).`);
|
||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue