From 5ccc4a72aba9d22ca59972f0703611b0c592fe48 Mon Sep 17 00:00:00 2001 From: Sander Vocke Date: Thu, 30 Jan 2020 08:31:29 +0059 Subject: [PATCH] Fix tag and album results on tag and album searches. --- src/main.js | 4 ++-- src/queries.js | 46 ++++++++++++++++++++++++++++++++-------------- src/resultsview.js | 2 +- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/main.js b/src/main.js index 10a1704..5b21c09 100644 --- a/src/main.js +++ b/src/main.js @@ -150,14 +150,14 @@ export function MainPage() { return ( {({ db_error, db }) => ( <> {(db == null && !db_error) && } {db_error && } - {db != null && } + {db != null && } )} diff --git a/src/queries.js b/src/queries.js index a743b32..1eb5e06 100644 --- a/src/queries.js +++ b/src/queries.js @@ -166,21 +166,21 @@ export class MatchingFilter extends ResultFilter { if (this.match_type == MatchTypeEnum.MATCH_IMAGE_NAME_EQUALS) { return '(Images.name="' + this.match_from + '")'; } else if (this.match_type == MatchTypeEnum.MATCH_IMAGE_NAME_NATURAL) { - return 'REGEXP(LOWER(Images.name), LOWER(".*' + return '(Images.name NOT NULL AND REGEXP(LOWER(Images.name), LOWER(".*' + escape_regex(this.match_from) - + '.*"))'; + + '.*")))'; } else if (this.match_type == MatchTypeEnum.MATCH_ALBUM_EQUALS) { return '(Albums.relativePath="' + this.match_from + '")'; } else if (this.match_type == MatchTypeEnum.MATCH_ALBUM_EQUALS_OR_CHILD) { - return 'REGEXP(Albums.relativePath, "' + return '(Albums.relativePath NOT NULL AND REGEXP(Albums.relativePath, "' + escape_regex(this.match_from) - + '(\/[^\/]+)*")'; + + '(\/[^\/]+)*"))'; } else if (this.match_type == MatchTypeEnum.MATCH_ALBUM_NATURAL) { throw new Error("Natural matching on album names is not yet supported."); } else if (this.match_type == MatchTypeEnum.MATCH_ALBUM_NAME_EQUALS) { - return 'REGEXP(Albums.relativePath, "\/(.*\/)*' + return '(Albums.relativePath NOT NULL AND REGEXP(Albums.relativePath, "\/(.*\/)*' + escape_regex(this.match_from) - + '(\/[^\/]+)*")'; + + '(\/[^\/]+)*"))'; } else if (this.match_type == MatchTypeEnum.MATCH_TAG_EQUALS) { return '(Tags.fullname="' + this.match_from + '")'; } else if (this.match_type == MatchTypeEnum.MATCH_TAG_EQUALS_OR_CHILD) { @@ -190,9 +190,9 @@ export class MatchingFilter extends ResultFilter { } else if (this.match_type == MatchTypeEnum.MATCH_TAG_NATURAL) { throw new Error("Natural matching on tag names is not yet supported."); } else if (this.match_type == MatchTypeEnum.MATCH_TAG_NAME_EQUALS) { - return 'REGEXP(Tags.fullname, "\/(.*\/)*' + return '(Tags.fullname NOT NULL AND REGEXP(Tags.fullname, "\/(.*\/)*' + escape_regex(this.match_from) - + '(\/[^\/]+)*")'; + + '(\/[^\/]+)*"))'; } console.log(this); @@ -269,17 +269,39 @@ export function image_query_with_where(maybe_where) { // This query will return database entries with the fields "id" and "relativePath" for each matching album. export function album_query_with_where(maybe_where) { - return "SELECT Albums.id, Albums.relativePath FROM Albums " + (maybe_where ? maybe_where : "") + ";"; + var query = "SELECT Albums.id, Albums.relativePath FROM Albums"; + + // If there is tags/images stuff in the where clause, we need to do a join on those tables. + if(maybe_where && (maybe_where.includes("Tags.") || maybe_where.includes("Images."))) { + query = query + " LEFT JOIN Images ON Images.album=Albums.id " + + " LEFT JOIN ImageTags ON ImageTags.imageid=Images.id " + + " LEFT JOIN Tags ON Tags.id=ImageTags.tagid"; + } + + query = query + " " + (maybe_where ? maybe_where : "") + " GROUP BY Albums.id;"; + + return query; } // This query will return database entries with the fields "id" and "name" for each matching tag. export function tag_query_with_where(maybe_where) { + var query = "SELECT Tags.id, Tags.name, Tags.fullname FROM Tags LEFT JOIN TagProperties ON Tags.id=TagProperties.tagid"; + // Add a clause to the WHERE to hide internal tags. var exclude_internal = ' (Tags.name="People" OR TagProperties.property IS NULL OR TagProperties.property<>"internalTag")'; var where = maybe_where ? maybe_where + ' AND' + exclude_internal : "WHERE " + exclude_internal; - return "SELECT Tags.id, Tags.name, Tags.fullname FROM Tags LEFT JOIN TagProperties ON Tags.id=TagProperties.tagid " + where + " GROUP BY Tags.fullname;"; + + // If there is albums/images stuff in the where clause, we need to do a join on those tables. + if(where.includes("Albums.") || where.includes("Images.")) { + query = query + " LEFT JOIN ImageTags ON ImageTags.tagid=Tags.id " + + " LEFT JOIN Images ON Images.id=ImageTags.imageid " + + " LEFT JOIN Albums ON Albums.id=Images.album"; + } + + query = query + " " + where + " GROUP BY Tags.id;"; + return query; } export function maybe_image_query(user_query, database) { @@ -393,7 +415,6 @@ export function user_query_from_browsed_album(album_path) { album_path, MatchTypeEnum.MATCH_ALBUM_EQUALS_OR_CHILD, false).simplify(); - /* r.album_filter = new MatchingFilter( ResultTypeEnum.ALBUM, @@ -406,7 +427,6 @@ export function user_query_from_browsed_album(album_path) { album_path, MatchTypeEnum.MATCH_ALBUM_EQUALS_OR_CHILD, false).simplify(); - */ return r; } @@ -418,7 +438,6 @@ export function user_query_from_browsed_tag(tag_path) { tag_path, MatchTypeEnum.MATCH_TAG_EQUALS_OR_CHILD, false).simplify(); - /* r.album_filter = new MatchingFilter( ResultTypeEnum.ALBUM, @@ -431,7 +450,6 @@ export function user_query_from_browsed_tag(tag_path) { tag_path, MatchTypeEnum.MATCH_TAG_EQUALS_OR_CHILD, false).simplify(); - */ return r; } \ No newline at end of file diff --git a/src/resultsview.js b/src/resultsview.js index ebf310c..0a95297 100644 --- a/src/resultsview.js +++ b/src/resultsview.js @@ -66,7 +66,7 @@ export function TagItemView(props) { ) }