Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CB-4348 fix: parse sql script #2212

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions webapp/packages/core-blocks/src/ActionIconButton.m.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
margin: 2px !important;
width: 24px !important;
height: 24px !important;
box-sizing: border-box;
overflow: hidden;
flex-shrink: 0;
flex-grow: 0;
Expand Down
7 changes: 5 additions & 2 deletions webapp/packages/core-blocks/src/IconButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
import { observer } from 'mobx-react-lite';
import type React from 'react';
import { Button, ButtonProps } from 'reakit/Button';
import { ButtonProps, Button as ReakitButton } from 'reakit/Button';
import styled from 'reshadow';

import type { ComponentStyle } from '@cloudbeaver/core-theming';
Expand All @@ -20,6 +20,7 @@ import { useS } from './useS';
import { useStyles } from './useStyles';

interface Props {
tag?: 'button' | 'a' | 'div';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this? I believe it is better to divide functionality. For example if we want a button to behave like a link it should be something like this:

<Link>
    <Button>Button Text></Button>
</Link>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

according to HTML, it's a really bad idea to place tags like this: <a><button></button></a>
the main idea here is that sometimes we want to display button looking component as a link or div so we can place it in another component like a button or input label

(it's really rare cases usually)

name: string;
img?: boolean;
viewBox?: string;
Expand All @@ -28,9 +29,11 @@ interface Props {

export type IconButtonProps = Props & ButtonProps;

export const IconButton: React.FC<IconButtonProps> = observer(function IconButton({ name, img, viewBox, style, className, ...rest }) {
export const IconButton: React.FC<IconButtonProps> = observer(function IconButton({ tag, name, img, viewBox, style, className, ...rest }) {
const styles = useS(IconButtonStyles);

const Button = tag ?? ReakitButton;

return styled(useStyles(style))(
<Button {...rest} className={s(styles, { iconButton: true }, className)}>
{img && <StaticImage className={s(styles, { staticImage: true })} icon={name} />}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function useSQLCodeEditorPanel(data: ISQLEditorData, editor: IEditor) {
}
},
onQueryChange(query: string) {
this.data.setQuery(query);
this.data.setScript(query);
},
onUpdate(update: ViewUpdate) {
const transactions = update.transactions.filter(t => t.selection !== undefined);
Expand Down
26 changes: 3 additions & 23 deletions webapp/packages/plugin-sql-editor/src/SQLParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export interface ISQLScriptLine {

export class SQLParser {
get scripts(): ISQLScriptSegment[] {
this.update();
return this._scripts;
}

Expand All @@ -39,30 +38,25 @@ export class SQLParser {

private _scripts: ISQLScriptSegment[];
private script: string;
private parsedScript: string | null;

constructor() {
this._scripts = [];
this.script = '';
this.parsedScript = null;

makeObservable<this, '_scripts' | 'script' | 'parsedScript' | 'update'>(this, {
makeObservable<this, '_scripts' | 'script'>(this, {
actualScript: computed,
_scripts: observable.ref,
script: observable.ref,
parsedScript: observable.ref,
getScriptSegment: action,
getSegment: action,
getQueryAtPos: action,
setScript: action,
parse: action,
setQueries: action,
update: action,
});
}

getScriptSegment(): ISQLScriptSegment {
const script = this.parsedScript || '';
const script = this.script || '';

return {
query: script,
Expand All @@ -80,10 +74,8 @@ export class SQLParser {
end = begin;
}

this.update();

return {
query: (this.parsedScript || '').substring(begin, end),
query: (this.script || '').substring(begin, end),
begin,
end,
};
Expand All @@ -107,15 +99,9 @@ export class SQLParser {

setScript(script: string): void {
this.script = script;
this.update();
}

parse(script: string): void {
this.parsedScript = script;
}

setQueries(queries: IQueryInfo[]): this {
this.update();
this._scripts = queries.map<ISQLScriptSegment>(query => ({
query: this.script.substring(query.start, query.end),
begin: query.start,
Expand All @@ -124,10 +110,4 @@ export class SQLParser {

return this;
}

private update() {
if (this.parsedScript !== this.script) {
this.parse(this.script);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { action, computed, makeObservable, observable, toJS } from 'mobx';

import type { IConnectionExecutionContextInfo } from '@cloudbeaver/core-connections';
import { ISyncExecutor, SyncExecutor } from '@cloudbeaver/core-executor';
import { isContainsException, isObjectsEqual, isValuesEqual, staticImplements } from '@cloudbeaver/core-utils';
import { isContainsException, isValuesEqual, staticImplements } from '@cloudbeaver/core-utils';
import type { IDatabaseDataModel, IDatabaseResultSet } from '@cloudbeaver/plugin-data-viewer';

import type { IDataQueryOptions } from '../QueryDataSource';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ export interface ISegmentExecutionData {
}

export interface ICursor {
begin: number;
end: number;
readonly begin: number;
readonly end: number;
}

export interface ISQLEditorData {
readonly cursor: ICursor;
readonly activeSegmentMode: ISQLEditorMode;
activeSegmentMode: ISQLEditorMode;
readonly parser: SQLParser;
readonly dialect: SqlDialectInfo | undefined;
readonly activeSegment: ISQLScriptSegment | undefined;
Expand All @@ -48,7 +48,7 @@ export interface ISQLEditorData {
readonly hintsLimitIsMet: boolean;

updateParserScriptsThrottle(): Promise<void>;
setQuery(query: string): void;
setScript(query: string): void;
init(): void;
destruct(): void;
setCursor(begin: number, end?: number): void;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2023 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/

.container {
composes: theme-border-color-background from global;
display: flex;
flex-direction: column;
justify-content: space-between;
overflow: auto;
border-right: solid 1px;
}

.actions {
display: flex;
flex-direction: column;
align-items: center;
user-select: none;

&:empty {
width: initial;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2023 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import { observer } from 'mobx-react-lite';

import { ActionIconButton, getComputed, preventFocusHandler, s, useS, useTranslate } from '@cloudbeaver/core-blocks';

import type { ISqlEditorTabState } from '../ISqlEditorTabState';
import { ESqlDataSourceFeatures } from '../SqlDataSource/ESqlDataSourceFeatures';
import type { ISQLEditorData } from './ISQLEditorData';
import style from './SQLEditorActions.m.css';
import { SqlEditorActionsMenu } from './SqlEditorActionsMenu';
import { SqlEditorTools } from './SqlEditorTools';

interface Props {
data: ISQLEditorData;
state: ISqlEditorTabState;
className?: string;
}

export const SQLEditorActions = observer<Props>(function SQLEditorActions({ data, state }) {
const styles = useS(style);
const translate = useTranslate();
const isActiveSegmentMode = getComputed(() => data.activeSegmentMode.activeSegmentMode);
const disabled = getComputed(() => data.isLineScriptEmpty || data.isDisabled);
const isQuery = data.dataSource?.hasFeature(ESqlDataSourceFeatures.query);
const isExecutable = data.dataSource?.hasFeature(ESqlDataSourceFeatures.executable);

return (
<div className={s(styles, { container: true })}>
<div className={s(styles, { actions: true })} onMouseDown={preventFocusHandler}>
{isExecutable && (
<>
{isQuery && (
<>
<ActionIconButton
name="/icons/sql_exec.svg"
disabled={disabled}
title={translate('sql_editor_sql_execution_button_tooltip')}
img
onClick={data.executeQuery}
/>
<ActionIconButton
name="/icons/sql_exec_new.svg"
disabled={disabled}
title={translate('sql_editor_sql_execution_new_tab_button_tooltip')}
img
onClick={data.executeQueryNewTab}
/>
</>
)}
<ActionIconButton
name="/icons/sql_script_exec.svg"
disabled={disabled}
hidden={isActiveSegmentMode}
title={translate('sql_editor_sql_execution_script_button_tooltip')}
img
onClick={data.executeScript}
/>
{isQuery && data.dialect?.supportsExplainExecutionPlan && (
<ActionIconButton
name="/icons/sql_execution_plan.svg"
disabled={disabled}
hidden={isActiveSegmentMode}
title={translate('sql_editor_execution_plan_button_tooltip')}
img
onClick={data.showExecutionPlan}
/>
)}
</>
)}
<SqlEditorActionsMenu state={state} />
</div>
<SqlEditorTools data={data} state={state} />
</div>
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export interface ISQLEditorMode {

export const SQLEditorModeContext: ISyncContextLoader<ISQLEditorMode, ISQLEditorData> = function SQLEditorModeContext(context, data) {
return {
activeSegment: data.parser.getSegment(data.cursor.begin, data.cursor.end),
get activeSegment() {
return data.parser.getSegment(data.cursor.begin, data.cursor.end);
},
activeSegmentMode: false,
};
};
Loading
Loading