Skip to content

Commit

Permalink
Merge 2156b3f into 530a801
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancescoBorzi committed Nov 7, 2019
2 parents 530a801 + 2156b3f commit 79c59d4
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 9 deletions.
83 changes: 83 additions & 0 deletions src/app/services/query.service.spec.ts
Expand Up @@ -24,6 +24,15 @@ interface MockTwoKeysRow extends TableRow {
attribute2: number;
}

interface MockTwoKeysComplexRow extends TableRow {
pk11: number;
pk12: number;
pk2: number;
name: string;
attribute1: number;
attribute2: number;
}

describe('QueryService', () => {
let service: QueryService;
let configService: ConfigService;
Expand Down Expand Up @@ -361,6 +370,80 @@ describe('QueryService', () => {
);
});
});

describe('using both keys and having primaryKey1 as complex', () => {
const primaryComplexKey1 = ['pk11', 'pk12'];
const myComplexRows: MockTwoKeysComplexRow[] = [
{ pk11: 1234, pk12: 5678, pk2: 1, name: 'Shin', attribute1: 28, attribute2: 4 },
{ pk11: 1234, pk12: 5678, pk2: 2, name: 'Helias', attribute1: 12, attribute2: 4 },
{ pk11: 1234, pk12: 5678, pk2: 3, name: 'Kalhac', attribute1: 12, attribute2: 4 },
];

it('should correctly work when all rows are deleted', () => {
expect(service.getDiffDeleteInsertTwoKeysQuery(tableName, primaryComplexKey1, primaryKey2, myComplexRows, [])).toEqual(
'DELETE FROM `my_table` WHERE (`pk11` = 1234) AND (`pk12` = 5678)'
);
});

it('should correctly work when adding new rows to an empty set', () => {
expect(service.getDiffDeleteInsertTwoKeysQuery(tableName, primaryComplexKey1, primaryKey2, [], myComplexRows)).toEqual(
'DELETE' + ' FROM `my_table` WHERE (`pk11` = 1234) AND (`pk12` = 5678) AND (`pk2` IN (1, 2, 3));\n' +
'INSERT' + ' INTO `my_table` (`pk11`, `pk12`, `pk2`, `name`, `attribute1`, `attribute2`) VALUES\n' +
'(1234, 5678, 1, \'Shin\', 28, 4),\n' +
'(1234, 5678, 2, \'Helias\', 12, 4),\n' +
'(1234, 5678, 3, \'Kalhac\', 12, 4);\n'
);
});

it('should correctly work when editing rows', () => {
const newRows = myComplexRows.map(x => Object.assign({}, x));
// edit two existing rows
newRows[1].name = 'Helias2';
newRows[2].name = 'Kalhac2';

expect(service.getDiffDeleteInsertTwoKeysQuery(tableName, primaryComplexKey1, primaryKey2, myComplexRows, newRows)).toEqual(
'DELETE' + ' FROM `my_table` WHERE (`pk11` = 1234) AND (`pk12` = 5678) AND (`pk2` IN (2, 3));\n' +
'INSERT' + ' INTO `my_table` (`pk11`, `pk12`, `pk2`, `name`, `attribute1`, `attribute2`) VALUES\n' +
'(1234, 5678, 2, \'Helias2\', 12, 4),\n' +
'(1234, 5678, 3, \'Kalhac2\', 12, 4);\n'
);
});

it('should correctly work when adding rows', () => {
const newRows = myComplexRows.map(x => Object.assign({}, x));
// add two new rows
newRows.push({ pk11: 1234, pk12: 5678, pk2: 4, name: 'Yehonal', attribute1: 99, attribute2: 0 });
newRows.push({ pk11: 1234, pk12: 5678, pk2: 5, name: 'Barbz', attribute1: 68, attribute2: 1 });

expect(service.getDiffDeleteInsertTwoKeysQuery(tableName, primaryComplexKey1, primaryKey2, myComplexRows, newRows)).toEqual(
'DELETE' + ' FROM `my_table` WHERE (`pk11` = 1234) AND (`pk12` = 5678) AND (`pk2` IN (4, 5));\n' +
'INSERT' + ' INTO `my_table` (`pk11`, `pk12`, `pk2`, `name`, `attribute1`, `attribute2`) VALUES\n' +
'(1234, 5678, 4, \'Yehonal\', 99, 0),\n' +
'(1234, 5678, 5, \'Barbz\', 68, 1);\n'
);
});

it('should correctly work when removing rows', () => {
const newRows = [ { ...myComplexRows[0] }, { ...myComplexRows[2] }]; // delete second row

expect(service.getDiffDeleteInsertTwoKeysQuery(tableName, primaryComplexKey1, primaryKey2, myComplexRows, newRows)).toEqual(
'DELETE' + ' FROM `my_table` WHERE (`pk11` = 1234) AND (`pk12` = 5678) AND (`pk2` IN (2));\n'
);
});

it('should correctly work when removing, editing and adding rows all together', () => {
const newRows = [ { ...myComplexRows[0] }, { ...myComplexRows[2] }]; // delete second row
newRows[1].name = 'Kalhac2'; // edit row
newRows.push({ pk11: 1234, pk12: 5678, pk2: 4, name: 'Yehonal', attribute1: 99, attribute2: 0 }); // add a new row

expect(service.getDiffDeleteInsertTwoKeysQuery(tableName, primaryComplexKey1, primaryKey2, myComplexRows, newRows)).toEqual(
'DELETE' + ' FROM `my_table` WHERE (`pk11` = 1234) AND (`pk12` = 5678) AND (`pk2` IN (2, 3, 4));\n' +
'INSERT' + ' INTO `my_table` (`pk11`, `pk12`, `pk2`, `name`, `attribute1`, `attribute2`) VALUES\n' +
'(1234, 5678, 3, \'Kalhac2\', 12, 4),\n' +
'(1234, 5678, 4, \'Yehonal\', 99, 0);\n'
);
});
});
});

describe('getDiffDeleteInsertOneKeyQuery', () => {
Expand Down
27 changes: 18 additions & 9 deletions src/app/services/query.service.ts
Expand Up @@ -195,21 +195,28 @@ export class QueryService {

// Tracks difference between two groups of rows (with TWO keys) and generate DELETE/INSERT query
getDiffDeleteInsertTwoKeysQuery<T extends TableRow>(
tableName: string, // the name of the table (example: 'creature_loot_template')
primaryKey1: string|null, // first primary key (example: 'Entry')
primaryKey2: string, // second primary key (example: 'Item')
currentRows: T[], // object of the original rows
newRows: T[], // array of the new rows
tableName: string, // the name of the table (example: 'creature_loot_template')
primaryKey1: string|string[], // first primary key (example: 'Entry' or ['source_type', 'entryorguid'])
primaryKey2: string, // second primary key (example: 'Item')
currentRows: T[], // object of the original rows
newRows: T[], // array of the new rows
): string {

if (!newRows || !currentRows) { return ''; }
if (newRows.length === 0 && currentRows.length === 0) {
return this.QUERY_NO_CHANGES;
}

const deleteQuery: Delete = squel.delete(squelConfig).from(tableName);

if (primaryKey1 && newRows.length === 0) {
// all rows have been deleted
return `DELETE FROM \`${tableName}\` WHERE \`${primaryKey1}\` = ${currentRows[0][primaryKey1]};\n`;
if (Array.isArray(primaryKey1)) {
this.addWhereConditionsToQuery(deleteQuery, currentRows[0], primaryKey1);
return deleteQuery.toString();
} else {
return `DELETE FROM \`${tableName}\` WHERE \`${primaryKey1}\` = ${currentRows[0][primaryKey1]};\n`;
}
}

const involvedRows: (string|number)[] = []; // -> needed for DELETE query
Expand All @@ -221,12 +228,14 @@ export class QueryService {
if ( involvedRows.length === 0 ) {
return this.QUERY_NO_CHANGES;
}

const deleteQuery: Delete = squel.delete(squelConfig).from(tableName);
const insertQuery: Insert = squel.insert(squelConfig).into(tableName);

if (primaryKey1) {
deleteQuery.where('`' + primaryKey1 + '` = ' + newRows[0][primaryKey1]);
if (Array.isArray(primaryKey1)) {
this.addWhereConditionsToQuery(deleteQuery, newRows[0], primaryKey1);
} else {
deleteQuery.where('`' + primaryKey1 + '` = ' + newRows[0][primaryKey1]);
}
}
deleteQuery.where('`' + primaryKey2 + '` IN ?', involvedRows);

Expand Down

0 comments on commit 79c59d4

Please sign in to comment.