From a90cb4e45fe07475bbc40e10b4d73df6975a63e7 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Wed, 14 Aug 2024 14:41:11 +0530 Subject: [PATCH] [mob] Fix queries --- mobile/lib/db/ml/db.dart | 49 +++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/mobile/lib/db/ml/db.dart b/mobile/lib/db/ml/db.dart index 11c9cf26d4..7d1a8b1700 100644 --- a/mobile/lib/db/ml/db.dart +++ b/mobile/lib/db/ml/db.dart @@ -29,6 +29,7 @@ class FaceMLDataDB { static final Logger _logger = Logger("FaceMLDataDB"); static const _databaseName = "ente.face_ml_db_v3.db"; + // static const _databaseVersion = 1; FaceMLDataDB._privateConstructor(); @@ -256,14 +257,20 @@ class FaceMLDataDB { final Map> result = {}; final selectQuery = ''' - SELECT fc.$fcClusterID, fe.$embeddingColumn - FROM $faceClustersTable fc - INNER JOIN $facesTable fe ON fc.$fcFaceId = fe.$faceIDColumn - WHERE fc.$fcClusterID IN (${clusterIDs.join(',')}) - ${limit != null ? 'LIMIT $limit' : ''} - '''; + SELECT fc.$fcClusterID, fe.$embeddingColumn + FROM $faceClustersTable fc + INNER JOIN $facesTable fe ON fc.$fcFaceId = fe.$faceIDColumn + WHERE fc.$fcClusterID IN (${List.filled(clusterIDs.length, '?').join(',')}) + ${limit != null ? 'LIMIT ?' : ''} +'''; - final List> maps = await db.getAll(selectQuery); + final List selectQueryParams = [...clusterIDs]; + if (limit != null) { + selectQueryParams.add(limit); + } + + final List> maps = + await db.getAll(selectQuery, selectQueryParams); for (final map in maps) { final clusterID = map[fcClusterID] as String; @@ -381,15 +388,14 @@ class FaceMLDataDB { ) async { final db = await instance.asyncDB; final Map> result = {}; + final List> maps = await db.getAll( - 'SELECT $fcClusterID, $fcFaceId FROM $faceClustersTable WHERE $fcClusterID IN (${clusterIDs.join(",")})', - ); - final List> maps = await db.query( - faceClustersTable, - columns: [fcClusterID, fcFaceId], - where: - '$fcClusterID IN (${List.filled(clusterIDs.length, '?').join(',')})', - whereArgs: clusterIDs, + ''' + SELECT $fcClusterID, $fcFaceId + FROM $faceClustersTable + WHERE $fcClusterID IN (${List.filled(clusterIDs.length, '?').join(',')}) + ''', + [...clusterIDs], ); for (final map in maps) { @@ -807,8 +813,12 @@ class FaceMLDataDB { final db = instance.asyncDB; return db.then((db) async { final List> maps = await db.getAll( - 'SELECT $fcClusterID, $fcFaceId FROM $faceClustersTable ' - 'WHERE $fcClusterID IN (${clusterIDs.join(",")})', + ''' + SELECT $fcClusterID, $fcFaceId + FROM $faceClustersTable + WHERE $fcClusterID IN (${List.filled(clusterIDs.length, '?').join(',')}) + ''', + [...clusterIDs], ); final Map> result = {}; for (final map in maps) { @@ -876,9 +886,12 @@ class FaceMLDataDB { ) async { final db = await instance.asyncDB; final Map result = {}; + final rows = await db.getAll( - 'SELECT * FROM $clusterSummaryTable WHERE $clusterIDColumn IN (${clusterIDs.join(",")})', + 'SELECT * FROM $clusterSummaryTable WHERE $clusterIDColumn IN (${List.filled(clusterIDs.length, '?').join(',')})', + [...clusterIDs], ); + for (final r in rows) { final id = r[clusterIDColumn] as String; final avg = r[avgColumn] as Uint8List;