diff --git a/src/index.js b/src/index.js index e13a848..3afa5e7 100644 --- a/src/index.js +++ b/src/index.js @@ -3,7 +3,7 @@ import ReactDOM from 'react-dom'; import { Fetch } from './fetch.js'; import { ProvideDB, DBQueryConsole, DBTypeEnum, DBSourceEnum } from './database.js'; import { PhotoView, PhotoThumbView, PhotoTableLine } from './media.js'; -import { find_photos } from './queries.js'; +import { do_image_query, image_query_with_where } from './queries.js'; import './index.css'; @@ -34,7 +34,7 @@ export class PhotoFromDB extends React.Component { } componentDidMount() { - find_photos("", this.props.database, "/test_photos", "/test_photos_thumbs").then(photos => { + do_image_query(image_query_with_where(""), this.props.database, "/test_photos", "/test_photos_thumbs").then(photos => { this.setState({ done: true, photo: photos[0] }); }); } diff --git a/src/queries.js b/src/queries.js index 35bb1e4..e2a9645 100644 --- a/src/queries.js +++ b/src/queries.js @@ -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)) { @@ -22,4 +17,73 @@ export function find_photos(maybe_where, database, collection_path, collection_t resolve(photos); }); }); +} + +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); } \ No newline at end of file