From bc875c2e9622a55af4dd540aeded1199c28afeca Mon Sep 17 00:00:00 2001 From: "Patrik J. Braun" Date: Wed, 1 Jan 2020 23:24:22 +0100 Subject: [PATCH] improving person name case special support --- src/backend/model/database/sql/PersonManager.ts | 13 +++++++++---- .../model/database/sql/enitites/EntityUtils.ts | 2 +- .../model/database/sql/enitites/PersonEntry.ts | 3 ++- test/backend/unit/model/sql/PersonManager.ts | 1 + test/backend/unit/model/sql/TestHelper.ts | 9 ++++++--- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/backend/model/database/sql/PersonManager.ts b/src/backend/model/database/sql/PersonManager.ts index 987278d1..33600d61 100644 --- a/src/backend/model/database/sql/PersonManager.ts +++ b/src/backend/model/database/sql/PersonManager.ts @@ -53,7 +53,7 @@ export class PersonManager implements IPersonManager { queryStr += ':n' + i + ' COLLATE utf8_general_ci'; namesObj['n' + i] = n; }); - const rawAndEntities = await (connection + const query: SelectQueryBuilder = await (connection .getRepository(MediaEntity) .createQueryBuilder('media') as SelectQueryBuilder) .select(['media.name', 'media.id', 'person.name', 'directory.name', @@ -61,10 +61,15 @@ export class PersonManager implements IPersonManager { .leftJoin('media.directory', 'directory') .leftJoinAndSelect('media.metadata.faces', 'faces') .leftJoin('faces.person', 'person') - .groupBy('person.name, media.name, media.id, directory.name, faces.id') - .orWhere(`lower(person.name) IN (:...names)`, {names: names.map(n => n.toLowerCase())}).getRawAndEntities(); - + .groupBy('person.name, media.name, media.id, directory.name, faces.id'); + // TODO: improve it. SQLITE does not support case-insensitive special characters like ÁÉÚŐ + for (let i = 0; i < names.length; ++i) { + const opt: any = {}; + opt['n' + i] = names[i]; + query.orWhere(`person.name LIKE :n${i} COLLATE utf8_general_ci`, opt); + } + const rawAndEntities = await query.getRawAndEntities(); for (let i = 0; i < rawAndEntities.raw.length; ++i) { this.samplePhotos[rawAndEntities.raw[i].person_name.toLowerCase()] = Utils.clone(rawAndEntities.entities.find(m => m.name === rawAndEntities.raw[i].media_name)); diff --git a/src/backend/model/database/sql/enitites/EntityUtils.ts b/src/backend/model/database/sql/enitites/EntityUtils.ts index 3679670b..00ea6531 100644 --- a/src/backend/model/database/sql/enitites/EntityUtils.ts +++ b/src/backend/model/database/sql/enitites/EntityUtils.ts @@ -5,7 +5,7 @@ import {ColumnOptions} from 'typeorm/decorator/options/ColumnOptions'; export class ColumnCharsetCS implements ColumnOptions { public get charset(): string { - return Config.Server.Database.type === ServerConfig.DatabaseType.mysql ? 'utf8' : null; + return Config.Server.Database.type === ServerConfig.DatabaseType.mysql ? 'utf8' : 'utf8'; } public get collation(): string { diff --git a/src/backend/model/database/sql/enitites/PersonEntry.ts b/src/backend/model/database/sql/enitites/PersonEntry.ts index 054f188d..6488ee30 100644 --- a/src/backend/model/database/sql/enitites/PersonEntry.ts +++ b/src/backend/model/database/sql/enitites/PersonEntry.ts @@ -1,6 +1,7 @@ import {Column, Entity, Index, OneToMany, PrimaryGeneratedColumn, Unique} from 'typeorm'; import {FaceRegionEntry} from './FaceRegionEntry'; import {PersonDTO} from '../../../../../common/entities/PersonDTO'; +import {columnCharsetCS} from './EntityUtils'; @Entity() @@ -11,7 +12,7 @@ export class PersonEntry implements PersonDTO { @PrimaryGeneratedColumn({unsigned: true}) id: number; - @Column() + @Column(columnCharsetCS) name: string; @Column('int', {unsigned: true, default: 0}) diff --git a/test/backend/unit/model/sql/PersonManager.ts b/test/backend/unit/model/sql/PersonManager.ts index cba14bdc..8f178e2e 100644 --- a/test/backend/unit/model/sql/PersonManager.ts +++ b/test/backend/unit/model/sql/PersonManager.ts @@ -106,6 +106,7 @@ describe('PersonManager', (sqlHelper: SQLTestHelper) => { delete map[k]; } } + expect(await pm.getSamplePhotos(p.metadata.faces.map(f => f.name.toLowerCase()))).to.deep.equal(map); }); diff --git a/test/backend/unit/model/sql/TestHelper.ts b/test/backend/unit/model/sql/TestHelper.ts index 5bc32dc1..12151c50 100644 --- a/test/backend/unit/model/sql/TestHelper.ts +++ b/test/backend/unit/model/sql/TestHelper.ts @@ -109,14 +109,17 @@ export class TestHelper { box: {height: 10, width: 10, left: 10, top: 10}, name: 'Boba Fett' }, { - box: {height: 10, width: 10, left: 101, top: 101}, + box: {height: 10, width: 10, left: 102, top: 102}, name: 'Luke Skywalker' }, { - box: {height: 10, width: 10, left: 101, top: 101}, + box: {height: 10, width: 10, left: 103, top: 103}, name: 'Han Solo' }, { - box: {height: 10, width: 10, left: 101, top: 101}, + box: {height: 10, width: 10, left: 104, top: 104}, name: 'Unkle Ben' + }, { + box: {height: 10, width: 10, left: 105, top: 105}, + name: 'Arvíztűrő Tükörfúrógép' }] as any[]; return p; }