@ -1,15 +1,10 @@
import { Photo , PhotoView , PhotoThumbView , create _photo } from './media.js' ;
// This query will return database entries with the fields "id", "uniqueHash", "relativePath" (of the album) and "name" for each matching image.
export function images _query ( maybe _where ) {
return "SELECT Images.id, Images.name, Images.uniqueHash, Albums.relativePath FROM Images INNER JOIN Albums ON Images.album=Albums.id " + ( maybe _where ? maybe _where : "" ) + ";" ;
}
export function find _photos ( maybe _where , database , collection _path , collection _thumbs _path ) {
export function do _image _query ( query , database , collection _path , collection _thumbs _path ) {
return new Promise ( function ( resolve , reject ) {
var queries = [ ] ;
queries . push ( images _ query( maybe _where ) ) ;
queries . push ( query ) ;
database . queries _async ( queries ) . then ( res => {
var photos = [ ] ;
if ( res && Array . isArray ( res ) ) {
@ -23,3 +18,72 @@ export function find_photos(maybe_where, database, collection_path, collection_t
} ) ;
} ) ;
}
export const MatchTypeEnum = {
MATCH _EQUALS : 1 ,
MATCH _LIKE : 2 ,
} ;
export const ImageFilterTypeEnum = {
MATCH _IMAGE _NAME : 1 ,
}
export const ResultTypeEnum = {
IMAGE : 1 ,
}
export class ImageFilter {
type = ImageFilterTypeEnum . MATCH _IMAGE _NAME ;
match _against = "" ;
match _type = MatchTypeEnum . MATCH _EQUALS ;
}
export class UserQuery {
image _filters = [ ] ;
include _result _types = [ ResultTypeEnum . IMAGE ] ;
}
export function image _filter _to _where ( image _filter ) {
var match _from = false ;
var match _to = image _filter . match _against ;
var match _operator = false ;
if ( image _filter . type == ImageFilterTypeEnum . MATCH _IMAGE _NAME ) {
match _from = "Image.name" ;
} else {
throw new Error ( "Unsupported image filter type: " + image _filter . type ) ;
}
if ( image _filter . match _type == MatchTypeEnum . MATCH _EQUALS ) {
match _operator = "=" ;
} else if ( image _filter . match _type == MatchTypeEnum . MATCH _LIKE ) {
match _operator = " LIKE " ;
}
return match _from + match _operator + match _to ;
}
// This query will return database entries with the fields "id", "uniqueHash", "relativePath" (of the album) and "name" for each matching image.
export function image _query _with _where ( maybe _where ) {
return "SELECT Images.id, Images.name, Images.uniqueHash, Albums.relativePath FROM Images INNER JOIN Albums ON Images.album=Albums.id " + ( maybe _where ? maybe _where : "" ) + ";" ;
}
export function maybe _image _query ( user _query ) {
if ( ! user _query . include _result _types . includes ( ResultTypeEnum . IMAGE ) ) {
return false ;
}
var where = false ;
if ( user _query . image _filters . length ) {
where = "WHERE " ;
var i = 0 ;
user _query . image _filters . forEach ( elem => {
if ( i > 0 ) {
where += "AND" ;
}
where += image _filter _to _where ( elem ) + " " ;
i ++ ;
} ) ;
}
return image _query _with _where ( where ) ;
}