Continue work on query parsing.

master
Sander Vocke 6 years ago
parent 1a747febd3
commit f3cf9928b8
  1. 5
      package-lock.json
  2. 1
      package.json
  3. 2
      src/database.js
  4. 27
      src/index.js
  5. 2
      src/media.js
  6. 49
      src/queries.js

5
package-lock.json generated

@ -12143,6 +12143,11 @@
"ajv-keywords": "^3.4.1" "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": { "select-hose": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/select-hose/-/select-hose-2.0.0.tgz", "resolved": "https://registry.npmjs.org/select-hose/-/select-hose-2.0.0.tgz",

@ -11,6 +11,7 @@
"react": "^16.12.0", "react": "^16.12.0",
"react-dom": "^16.12.0", "react-dom": "^16.12.0",
"react-scripts": "3.3.0", "react-scripts": "3.3.0",
"search-string": "^3.1.0",
"sql.js": "^1.1.0" "sql.js": "^1.1.0"
}, },
"scripts": { "scripts": {

@ -296,7 +296,7 @@ export class ProvideDB extends React.Component {
export class DBQueryBar extends React.Component { export class DBQueryBar extends React.Component {
onChangeHandler = (event) => { onChangeHandler = (event) => {
this.props.onChange(event.target.value); if(this.props.onChange) { this.props.onChange(event.target.value) };
} }
render() { render() {

@ -3,7 +3,7 @@ import ReactDOM from 'react-dom';
import { Fetch } from './fetch.js'; import { Fetch } from './fetch.js';
import { ProvideDB, DBQueryConsole, DBQueryBar, DBTypeEnum, DBSourceEnum } from './database.js'; import { ProvideDB, DBQueryConsole, DBQueryBar, DBTypeEnum, DBSourceEnum } from './database.js';
import { PhotoView, PhotoThumbView, PhotoTableLine } from './media.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'; 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 (
<>
<div>
<DBQueryBar onChange={this.onQueryChangeHandler}/>
<button onClick={this.onQuerySubmitHandler}>Submit</button>
</div>
</>
);
}
}
const TestDBFetch = ({ sqlite_file }) => ( const TestDBFetch = ({ sqlite_file }) => (
<ProvideDB db_file={sqlite_file} db_source_type={DBTypeEnum.SQLJS_SQLITE} db_target_type={DBTypeEnum.ALASQL_NATIVE} <ProvideDB db_file={sqlite_file} db_source_type={DBTypeEnum.SQLJS_SQLITE} db_target_type={DBTypeEnum.ALASQL_NATIVE}
db_source_name="sqlite_db" db_target_name="db" db_source={DBSourceEnum.ATTACHFILE}> db_source_name="sqlite_db" db_target_name="db" db_source={DBSourceEnum.ATTACHFILE}>
@ -117,6 +140,8 @@ ReactDOM.render(
<TestDBPlayground db_name="playground_db" /> <TestDBPlayground db_name="playground_db" />
<h1>Test DB fetching:</h1> <h1>Test DB fetching:</h1>
<TestDBFetch sqlite_file={process.env.PUBLIC_URL + "/test_photos_db/digikam4.db"} /> <TestDBFetch sqlite_file={process.env.PUBLIC_URL + "/test_photos_db/digikam4.db"} />
<h1>Test query parsing:</h1>
<StringToQueryConsole/>
</>, </>,
document.getElementById('root') document.getElementById('root')
); );

@ -13,7 +13,7 @@ export class Photo extends Media {
} }
export function create_photo(maybe_id, maybe_name, maybe_path, maybe_thumbnail_path) { 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_id) { p.state.id = maybe_id; }
if(maybe_name) { p.state.name = maybe_name; } if(maybe_name) { p.state.name = maybe_name; }
if(maybe_path) { p.state.path = maybe_path; } if(maybe_path) { p.state.path = maybe_path; }

@ -24,22 +24,33 @@ export const MatchTypeEnum = {
MATCH_LIKE: 2, MATCH_LIKE: 2,
}; };
export const ImageFilterTypeEnum = { export const ResultFilterTypeEnum = {
MATCH_IMAGE_NAME: 1, MATCH_NAME: 1,
} }
export const ResultTypeEnum = { export const ResultTypeEnum = {
IMAGE: 1, IMAGE: 1,
} }
export class ImageFilter { export class ResultFilter {
type = ImageFilterTypeEnum.MATCH_IMAGE_NAME; // What kind of filtering to apply
type = ResultFilterTypeEnum.MATCH_NAME;
// optional string used in the filtering
match_against = ""; match_against = "";
// How to use the matching string
match_type = MatchTypeEnum.MATCH_EQUALS; 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 { export class UserQuery {
image_filters = []; result_filters = [];
include_result_types = [ ResultTypeEnum.IMAGE ]; 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_to = image_filter.match_against;
var match_operator = false; var match_operator = false;
if(image_filter.type == ImageFilterTypeEnum.MATCH_IMAGE_NAME) { if(image_filter.type == ResultFilterTypeEnum.MATCH_NAME) {
match_from = "Image.name"; match_from = "Image.name";
} else { } else {
throw new Error("Unsupported image filter type: " + image_filter.type); throw new Error("Unsupported image filter type: " + image_filter.type);
@ -87,3 +98,29 @@ export function maybe_image_query(user_query) {
return image_query_with_where(where); 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);
}
Loading…
Cancel
Save