From f3cf9928b821ac3404964611adf8b5329243ea1b Mon Sep 17 00:00:00 2001 From: Sander Vocke Date: Fri, 17 Jan 2020 17:40:41 +0100 Subject: [PATCH] Continue work on query parsing. --- package-lock.json | 5 +++++ package.json | 1 + src/database.js | 2 +- src/index.js | 27 +++++++++++++++++++++++++- src/media.js | 2 +- src/queries.js | 49 +++++++++++++++++++++++++++++++++++++++++------ 6 files changed, 77 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4d788a7..0d67abe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12143,6 +12143,11 @@ "ajv-keywords": "^3.4.1" } }, + "search-string": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/search-string/-/search-string-3.1.0.tgz", + "integrity": "sha512-yY3b0VlaXfKi2B//34PN5AFF+GQvwme6Kj4FjggmoSBOa7B8AHfS1nYZbsrYu+IyGeYOAkF8ywL9LN9dkrOo6g==" + }, "select-hose": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/select-hose/-/select-hose-2.0.0.tgz", diff --git a/package.json b/package.json index a57ccdc..09f9bf4 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "react": "^16.12.0", "react-dom": "^16.12.0", "react-scripts": "3.3.0", + "search-string": "^3.1.0", "sql.js": "^1.1.0" }, "scripts": { diff --git a/src/database.js b/src/database.js index aa2ddc1..7a0775f 100644 --- a/src/database.js +++ b/src/database.js @@ -296,7 +296,7 @@ export class ProvideDB extends React.Component { export class DBQueryBar extends React.Component { onChangeHandler = (event) => { - this.props.onChange(event.target.value); + if(this.props.onChange) { this.props.onChange(event.target.value) }; } render() { diff --git a/src/index.js b/src/index.js index f561b92..0dc3fdd 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, DBQueryBar, DBTypeEnum, DBSourceEnum } from './database.js'; import { PhotoView, PhotoThumbView, PhotoTableLine } from './media.js'; -import { do_image_query, image_query_with_where } from './queries.js'; +import { do_image_query, image_query_with_where, user_query_from_search_string } from './queries.js'; import './index.css'; @@ -78,6 +78,29 @@ export class ImageWhereConsole extends React.Component { } } +export class StringToQueryConsole extends React.Component { + state = { + string: false, + query: false, + } + + onQueryChangeHandler = str => { this.setState({ string: str }); } + onQuerySubmitHandler = () => { + this.setState({ query: user_query_from_search_string(this.state.string) }); + } + + render() { + return ( + <> +
+ + +
+ + ); + } +} + const TestDBFetch = ({ sqlite_file }) => ( @@ -117,6 +140,8 @@ ReactDOM.render(

Test DB fetching:

+

Test query parsing:

+ , document.getElementById('root') ); diff --git a/src/media.js b/src/media.js index 8415639..23254c0 100644 --- a/src/media.js +++ b/src/media.js @@ -13,7 +13,7 @@ export class Photo extends Media { } export function create_photo(maybe_id, maybe_name, maybe_path, maybe_thumbnail_path) { - var p = new Photo; + var p = new Photo(); if(maybe_id) { p.state.id = maybe_id; } if(maybe_name) { p.state.name = maybe_name; } if(maybe_path) { p.state.path = maybe_path; } diff --git a/src/queries.js b/src/queries.js index e2a9645..abf3f2e 100644 --- a/src/queries.js +++ b/src/queries.js @@ -24,22 +24,33 @@ export const MatchTypeEnum = { MATCH_LIKE: 2, }; -export const ImageFilterTypeEnum = { - MATCH_IMAGE_NAME: 1, +export const ResultFilterTypeEnum = { + MATCH_NAME: 1, } export const ResultTypeEnum = { IMAGE: 1, } -export class ImageFilter { - type = ImageFilterTypeEnum.MATCH_IMAGE_NAME; +export class ResultFilter { + // What kind of filtering to apply + type = ResultFilterTypeEnum.MATCH_NAME; + + // optional string used in the filtering match_against = ""; + + // How to use the matching string match_type = MatchTypeEnum.MATCH_EQUALS; + + // On which types of results to apply the filter + result_types = []; + + // If true, negates the filter + negate = false; } export class UserQuery { - image_filters = []; + result_filters = []; include_result_types = [ ResultTypeEnum.IMAGE ]; } @@ -48,7 +59,7 @@ export function image_filter_to_where(image_filter) { var match_to = image_filter.match_against; var match_operator = false; - if(image_filter.type == ImageFilterTypeEnum.MATCH_IMAGE_NAME) { + if(image_filter.type == ResultFilterTypeEnum.MATCH_NAME) { match_from = "Image.name"; } else { throw new Error("Unsupported image filter type: " + image_filter.type); @@ -86,4 +97,30 @@ export function maybe_image_query(user_query) { } return image_query_with_where(where); +} + +function filter_from_text_segment(segment) { + var r = new ResultFilter(); + r.match_against = segment['text']; + r.negate = segment['negated']; + r.match_type = MatchTypeEnum.MATCH_EQUALS; + r.type = ResultFilterTypeEnum.MATCH_NAME; + return r; +} + +export function user_query_from_search_string(search_string) { + const parser = require('search-string'); + + var parsed = parser.parse(search_string); + var conditions = parsed.getParsedQuery(); + var texts = parsed.getTextSegments(); + + var r = new UserQuery(); + + texts.forEach(text => { + r.result_filters.push(filter_from_text_segment(text)); + }); + + console.log(search_string); + console.log(r); } \ No newline at end of file