@ -1,5 +1,9 @@
import { Photo , PhotoView , PhotoThumbView , create _photo } from './media.js' ;
import { create _photo , create _album } from './media.js' ;
export function escape _regex ( string ) {
return string . replace ( /[.*+?^${}()|[\]\\]/g , '\\$&' ) ; // $& means the whole matched string
}
export function do _image _query ( query , database , collection _path , collection _thumbs _path ) {
export function do _image _query ( query , database , collection _path , collection _thumbs _path ) {
return new Promise ( function ( resolve , reject ) {
return new Promise ( function ( resolve , reject ) {
@ -19,9 +23,26 @@ export function do_image_query(query, database, collection_path, collection_thum
} ) ;
} ) ;
}
}
export function do _album _query ( query , database , collection _path , collection _thumbs _path ) {
return new Promise ( function ( resolve , reject ) {
var queries = [ ] ;
queries . push ( query ) ;
database . queries _async ( queries ) . then ( res => {
var albums = [ ] ;
if ( res && Array . isArray ( res ) ) {
res . forEach ( row => {
var album _name = new String ( row [ "relativePath" ] ) . substring ( row [ "relativePath" ] . lastIndexOf ( '/' ) + 1 ) ;
albums . push ( create _album ( row [ "id" ] , album _name , row [ "relativePath" ] ) ) ;
} ) ;
}
resolve ( albums ) ;
} ) ;
} ) ;
}
export const MatchTypeEnum = {
export const MatchTypeEnum = {
MATCH _EQUALS : 1 ,
MATCH _EQUALS : 1 ,
MATCH _LIKE : 2 ,
MATCH _REGEXP : 2 ,
} ;
} ;
export const MatchAgainstEnum = {
export const MatchAgainstEnum = {
@ -52,24 +73,6 @@ export class ConstFilter extends ResultFilter {
}
}
}
}
function match _column _name ( result _type , match _against ) {
if ( match _against == MatchAgainstEnum . MATCH _IMAGE _NAME ) {
return "Images.name" ;
}
if ( match _against == MatchAgainstEnum . MATCH _ALBUM _NAME ) {
return "Album.name" ;
}
if ( match _against == MatchAgainstEnum . MATCH _RESULT _NAME ) {
if ( result _type == ResultTypeEnum . IMAGE ) {
return match _column _name ( result _type , MatchAgainstEnum . MATCH _IMAGE _NAME ) ;
}
if ( result _type == ResultTypeEnum . ALBUM ) {
return match _column _name ( result _type , MatchAgainstEnum . MATCH _ALBUM _NAME ) ;
}
}
throw new Error ( "Could not get column name for matching properties passed." ) ;
}
export class MatchingFilter extends ResultFilter {
export class MatchingFilter extends ResultFilter {
constructor ( rtype , against , from , mtype , negate ) {
constructor ( rtype , against , from , mtype , negate ) {
super ( rtype ) ;
super ( rtype ) ;
@ -79,8 +82,8 @@ export class MatchingFilter extends ResultFilter {
this . negate = negate ;
this . negate = negate ;
}
}
// What kind of filtering to apply
// What string to match against, e.g. "Image.name" (field) or "\"Image.name\"" (literal)
match _against = MatchAgainstEnum . MATCH _RESULT _NAME ;
match _against = "" ;
// optional string used in the filtering
// optional string used in the filtering
match _from = "" ;
match _from = "" ;
@ -92,18 +95,17 @@ export class MatchingFilter extends ResultFilter {
negate = false ;
negate = false ;
to _sql _where ( ) {
to _sql _where ( ) {
var match _against _str = match _column _name ( this . result _type , this . match _against ) ;
var match _type _str = false ;
var match _type _str = false ;
if ( this . match _type == MatchTypeEnum . MATCH _EQUALS ) {
if ( this . match _type == MatchTypeEnum . MATCH _EQUALS ) {
match _type _str = "=" ;
match _type _str = "=" ;
} else if ( this . match _type == MatchTypeEnum . MATCH _LIKE ) {
} else if ( this . match _type == MatchTypeEnum . MATCH _REGEXP ) {
match _type _str = " LIKE " ;
match _type _str = " REGEXP " ;
} else {
} else {
throw new Error ( 'Unsupported match type: ' + this . match _type ) ;
throw new Error ( 'Unsupported match type: ' + this . match _type ) ;
}
}
return "(" + ( this . negate ? "NOT " : "" ) + match _against _str + match _type _str + this . match _from + ")" ;
return "(" + ( this . negate ? "NOT " : "" ) + this . match _against + match _type _str + this . match _from + ")" ;
}
}
}
}
@ -143,6 +145,11 @@ 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 : "" ) + ";" ;
return "SELECT Images.id, Images.name, Images.uniqueHash, Albums.relativePath FROM Images INNER JOIN Albums ON Images.album=Albums.id " + ( maybe _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 : "" ) + ";" ;
}
export function maybe _image _query ( user _query ) {
export function maybe _image _query ( user _query ) {
var where = false ;
var where = false ;
if ( user _query . image _filter ) {
if ( user _query . image _filter ) {
@ -151,9 +158,31 @@ export function maybe_image_query(user_query) {
return image _query _with _where ( where ) ;
return image _query _with _where ( where ) ;
}
}
export function maybe _album _query ( user _query ) {
var where = false ;
if ( user _query . album _filter ) {
where = "WHERE " + user _query . album _filter . to _sql _where ( ) ;
}
return album _query _with _where ( where ) ;
}
function filter _from _text _segment ( result _type , segment ) {
function filter _from _text _segment ( result _type , segment ) {
return new MatchingFilter ( result _type , MatchAgainstEnum . MATCH _RESULT _NAME ,
var match _type = false ;
"\"" + segment [ 'text' ] + "\"" , MatchTypeEnum . MATCH _EQUALS , segment [ 'negated' ] ) ;
var match _text = false ;
var match _against = false ;
if ( result _type == ResultTypeEnum . IMAGE ) {
match _type = MatchTypeEnum . MATCH _EQUALS ;
match _text = "\"" + segment [ 'text' ] + "\"" ;
match _against = "Images.name" ;
} else if ( result _type == ResultTypeEnum . ALBUM ) {
// Match against the album "name", which is the basename of its relative path.
match _type = MatchTypeEnum . MATCH _REGEXP ;
match _text = "\"" + '\/(.*\/)*' + escape _regex ( segment [ 'text' ] ) + "\"" ;
match _against = "Albums.relativePath" ;
}
return new MatchingFilter ( result _type , match _against , match _text , match _type , segment [ 'negated' ] ) ;
}
}
export function user _query _from _search _string ( search _string ) {
export function user _query _from _search _string ( search _string ) {
@ -176,6 +205,7 @@ export function user_query_from_search_string(search_string) {
console . log ( search _string ) ;
console . log ( search _string ) ;
console . log ( r ) ;
console . log ( r ) ;
console . log ( maybe _image _query ( r ) ) ;
console . log ( maybe _image _query ( r ) ) ;
console . log ( maybe _album _query ( r ) ) ;
return r ;
return r ;
}
}