Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
razvanMiu committed Jan 14, 2021
1 parent 1b00c6b commit 41df833
Show file tree
Hide file tree
Showing 12 changed files with 583 additions and 40 deletions.
88 changes: 88 additions & 0 deletions src/components/manage/Blocks/DataBase/DB.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const collation = {
_: ' ',
latin_ci_ai: ' COLLATE Latin1_General_CI_AI ',
latin_ci_as: ' COLLATE Latin1_General_CI_AS ',
};

class DB {
static table(query, path = '', pagination = {}) {
return new Table(query, path, pagination);
}
}

class Table {
constructor(query, path, pagination) {
this.query = query;
this.path = path || '/';
this.pagination = pagination || {};
}
get() {
const { p, nrOfHits } = this.pagination;
return `${this.path}?query=${this.query}${
typeof p !== 'undefined' ? '&p=' + p : ''
}${typeof nrOfHits !== 'undefined' ? '&nrOfHits=' + nrOfHits : ''}`;
}
encode() {
this.query = encodeURI(this.query);
return this;
}
where(whereStatements, additionalWhereStatements) {
let queryString = '';
let additionalString = '';
if (additionalWhereStatements.length > 0) {
additionalString = additionalWhereStatements.join(' AND ');
}
if (whereStatements?.length > 0) {
const whereString = whereStatements
.map((where, index) => {
let whereString = '';
if (Array.isArray(where.value) && !where.value.length) return null;
if (typeof where.value === 'string' && !where.value.length)
return null;
if (Array.isArray(where.value)) {
const baseSql = `(${where.discodataKey
.split('.')
.map((item) => '[' + item + ']')
.join('.')}${collation[where.collation || '_']}LIKE '${
where.isExact ? '' : '%'
}:option${where.isExact ? '' : '%'}')`;
return `(${where.value
.map((option) => baseSql.replace(':option', option))
.join(' OR ')})`;
} else {
whereString = `${where.discodataKey
.split('.')
.map((item) => '[' + item + ']')
.join('.')}${collation[where.collation || '_']}LIKE '${
where.regex && typeof where.regex === 'string'
? where.regex.replace(':value', where.value)
: where.value
}'`;
}
return whereString;
})
.filter((value) => value)
.join(' AND ');
if (whereString.length) {
queryString += ` WHERE ${whereString}${
additionalString ? ` AND ${additionalString}` : ''
}`;
} else if (additionalString.length > 0) {
queryString += ` WHERE ${additionalString}`;
}
} else if (additionalWhereStatements.length > 0) {
queryString += ` WHERE ${additionalString}`;
}
if (this.query.includes(':where')) {
this.query = this.query.replace(
':where',
queryString.replace(' WHERE ', ''),
);
} else {
this.query += queryString;
}
return this;
}
}

export default DB;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { connect } from 'react-redux';
import { isArray } from 'lodash';
import { Dropdown, Header } from 'semantic-ui-react';
import { setQueryParam } from 'volto-datablocks/actions';
import DiscodataSqlBuilder from 'volto-datablocks/DiscodataSqlBuilder/View';
import { ViewDiscodataSqlBuilder } from 'volto-datablocks/components';
import ReactTooltip from 'react-tooltip';
import Icon from '@plone/volto/components/theme/Icon/Icon';
import moment from 'moment';
Expand Down Expand Up @@ -290,7 +290,7 @@ const CountrySelector = (props) => {

return (
<div className="custom-selector big red">
<DiscodataSqlBuilder
<ViewDiscodataSqlBuilder
data={{
'@type': 'discodata_sql_builder',
sql: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { connect } from 'react-redux';
import { compose } from 'redux';
import qs from 'query-string';
import View from './View';
import DiscodataSqlBuilderEdit from 'volto-datablocks/DiscodataSqlBuilder/Edit';
import { EditDiscodataSqlBuilder } from 'volto-datablocks/components';

const makeChoices = (keys) => keys && keys.map((k) => [k, k]);

Expand Down Expand Up @@ -433,14 +433,14 @@ const Edit = (props) => {
/* eslint-disable-next-line */
}, [props.data, props.discodata_resources, props.discodata_query.search]);
return (
<DiscodataSqlBuilderEdit
<EditDiscodataSqlBuilder
{...props}
optionalSchema={state.schema}
title="Discodata components block"
>
<h3>Discodata components - edit mode</h3>
<View {...props} mode="edit" />
</DiscodataSqlBuilderEdit>
</EditDiscodataSqlBuilder>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { arrayToTree } from 'performant-array-to-tree';
import qs from 'query-string';
import { Table, Dropdown, List, Header } from 'semantic-ui-react';
import './style.css';
import DiscodataSqlBuilderView from 'volto-datablocks/DiscodataSqlBuilder/View';
import { ViewDiscodataSqlBuilder } from 'volto-datablocks/components';
import { setQueryParam, deleteQueryParam } from 'volto-datablocks/actions';
import cx from 'classnames';
import Icon from '@plone/volto/components/theme/Icon/Icon';
Expand Down Expand Up @@ -718,7 +718,7 @@ const View = (props) => {
let root = arrayToTree(componentsArray);
if (!__CLIENT__) return '';
return (
<DiscodataSqlBuilderView {...props}>
<ViewDiscodataSqlBuilder {...props}>
<div className="facility-block-wrapper">
<div>
{(state.selectedResource &&
Expand All @@ -735,7 +735,7 @@ const View = (props) => {
(props.data.mode === 'edit' ? <p>Add components</p> : '')}
</div>
</div>
</DiscodataSqlBuilderView>
</ViewDiscodataSqlBuilder>
);
};

Expand Down
Loading

0 comments on commit 41df833

Please sign in to comment.