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

feat: ability to edit table column #123

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/drivers/common/MySQLCommonInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ export default class MySQLCommonInterface extends SQLCommonInterface {
table_schema: database,
table_name: table,
})
.orderBy('ORDINAL_POSITION')
.toRawSQL(),
},
],
Expand Down
13 changes: 13 additions & 0 deletions src/libs/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface QueryStates {
where: QueryWhere[];
select: string[];
limit?: number;
orderBy?: [string, 'ASC' | 'DESC'];
}

abstract class QueryDialect {
Expand Down Expand Up @@ -98,6 +99,12 @@ export class QueryBuilder {
return this;
}

orderBy(field: string, by: 'ASC' | 'DESC' = 'ASC') {
if (!['ASC', 'DESC'].includes(by)) throw 'Order by must be DESC or ASC';

Choose a reason for hiding this comment

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

should allow 'asc' and 'desc' (lowercase)?

this.states.orderBy = [field, by];
return this;
}

select(...columns: string[]) {
this.states.select = this.states.select.concat(columns);
return this;
Expand Down Expand Up @@ -195,6 +202,12 @@ export class QueryBuilder {
'FROM',
this.dialect.escapeIdentifier(this.states.table),
whereSql ? 'WHERE ' + whereSql : whereSql,
this.states.orderBy
? 'ORDER BY ' +
this.dialect.escapeIdentifier(this.states.orderBy[0]) +
' ' +
this.states.orderBy[1]
: null,
this.states.limit ? `LIMIT ?` : null,
]
.filter(Boolean)
Expand Down
9 changes: 9 additions & 0 deletions src/renderer/components/OptimizeTable/CellCheckInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import styles from './cells.module.scss';

export default function CellCheckInput() {
return (
<div className={styles.checkContainer}>
<input type="checkbox" checked />
</div>
);
}
81 changes: 81 additions & 0 deletions src/renderer/components/OptimizeTable/CellGroupSelectInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { useState, useEffect, useRef } from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import styles from './cells.module.scss';
import { faChevronDown } from '@fortawesome/free-solid-svg-icons';
import AvoidOffscreen from '../AvoidOffscreen';
import DropContainer from '../DropContainer';
import OptionList from '../OptionList';
import OptionListGroup from '../OptionList/OptionListGroup';
import OptionListItem from '../OptionList/OptionListItem';
import TextField from '../TextField';

interface CellGroupSelectInputProps {
items: { name: string; items: { value: string; text: string }[] }[];
value?: string;
}

export default function CellGroupSelectInput({
items,
value,
}: CellGroupSelectInputProps) {
const [show, setShow] = useState(false);
const ref = useRef<HTMLDivElement>(null);

useEffect(() => {
if (ref.current && show) {
const onDocumentClick = (e: MouseEvent) => {
if (ref.current && !ref.current.contains(e.target as Node)) {
setShow(false);
}
};

document.addEventListener('click', onDocumentClick);
return () => document.removeEventListener('click', onDocumentClick);
}
}, [ref, show, setShow]);

Choose a reason for hiding this comment

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

it's safe to remove setShow from deps

Choose a reason for hiding this comment

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

and so does ref


return (
<div ref={ref} style={{ position: 'relative' }}>
<div
className={styles.dropdownContainer}
onClick={() => {
setShow(true);
}}
>
<div className={styles.dropdownContent}>{value}</div>
<div className={styles.dropdownArrow}>
<FontAwesomeIcon icon={faChevronDown} />
</div>
</div>
{show && (
<div style={{ position: 'absolute', zIndex: 100 }}>
<AvoidOffscreen>
<DropContainer>
<div style={{ padding: 5 }}>
<TextField autoFocus placeholder="Search type" />
</div>
<div
style={{ height: 300, width: 250, overflowY: 'auto' }}
className="scroll"
>
<OptionList>
{items.map((group) => {
return (
<OptionListGroup key={group.name} text={group.name}>
{group.items.map((sub) => {
return (
<OptionListItem key={sub.value} text={sub.text} />
);
})}
</OptionListGroup>
);
})}
</OptionList>
</div>
</DropContainer>
</AvoidOffscreen>
</div>
)}
</div>
);
}
44 changes: 44 additions & 0 deletions src/renderer/components/OptimizeTable/CellSingleLineInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import styles from './cells.module.scss';

interface CellInputProps {
onLostFocus?: (value: string | null | undefined) => void;
readOnly?: boolean;
value?: string | null;
alignRight?: boolean;
onChange?: (value: string) => void;
}

export default function CellSingleLineInput({
onLostFocus,
readOnly,
value,
onChange,
alignRight,
}: CellInputProps) {
return (
<div className={styles.inputContainer}>
<input
onKeyPress={(e) => {
if (e.key === 'Enter') {
if (onLostFocus) {
onLostFocus(value);
}
}
}}
spellCheck="false"
autoFocus
type="text"
readOnly={readOnly}
className={styles.input}
style={alignRight ? { textAlign: 'right' } : undefined}
onBlur={() => {
if (onLostFocus) onLostFocus(value);
}}
onChange={(e) => {
if (onChange) onChange(e.currentTarget.value);
}}
value={value ?? ''}
/>
</div>
);
}
46 changes: 46 additions & 0 deletions src/renderer/components/OptimizeTable/cells.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
.checkContainer {
display: flex;
height: 100%;
width: 100%;
padding: 0px 10px;
user-select: none;
justify-content: center;

input {
transform: scale(1.2)
}
}

.inputContainer {
display: flex;
width: 100%;
height: 100%;
}

.input {
padding: 5px 10px;
border: 1px solid var(--color-surface);
background: var(--color-surface);
color: var(--color-text);
box-sizing: border-box;
width: 100%;
height: 100%;
outline: none;
font-size: 1rem;
}

.dropdownContainer {
display: flex;
padding: 0px 10px;

.dropdownContent, .dropdownArrow {
line-height: 32px;
}

.dropdownContent {
flex-grow: 1;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
}
15 changes: 13 additions & 2 deletions src/renderer/components/OptionList/OptionListGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
export default function OptionListGroup() {
return <div></div>;
import { PropsWithChildren } from 'react';
import styles from './styles.module.scss';

export default function OptionListGroup({
children,
text,
}: PropsWithChildren<{ text: string }>) {
return (
<div>
<div className={styles.groupLabel}>{text}</div>
<div>{children}</div>
</div>
);
}
6 changes: 6 additions & 0 deletions src/renderer/components/OptionList/styles.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@

}

.groupLabel {
font-weight: bold;
padding: 5px 10px;
padding-left: 15px;
}

.item {
display: flex;
cursor: pointer;
Expand Down
4 changes: 0 additions & 4 deletions src/renderer/components/ResizableTable/styles.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@
border-right: 1px solid var(--color-table-grid);
}

td {
overflow: hidden;
}

th {
padding: 5px 10px;
position: sticky;
Expand Down
69 changes: 69 additions & 0 deletions src/renderer/components/SchemaEditor/TableColumnsAlterDiff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { TableColumnSchema } from 'types/SqlSchema';

interface TableColumnsAlterDiffNode {
index: number;
original: TableColumnSchema | null;
changed: Partial<TableColumnSchema> | null;
}

interface ColumnNode extends TableColumnsAlterDiffNode {
index: number;
original: TableColumnSchema | null;
changed: Partial<TableColumnSchema> | null;
next: ColumnNode | null;
prev: ColumnNode | null;
}

export class TableColumnsAlterDiff {
protected root: ColumnNode | null = null;

/**
* Initial from original columns
*
* @param columns
* @returns
*/
constructor(columns: TableColumnSchema[]) {
const root: ColumnNode = {
next: null,
prev: null,
index: 0,
original: columns[0],
changed: {},
};

let currentNode = root;
for (let i = 1; i < columns.length; i++) {
const newNode: ColumnNode = {
next: null,
prev: currentNode,
index: i,
original: columns[i],
changed: {},
};

currentNode.next = newNode;
currentNode = newNode;
}

this.root = root;
}

toArray() {
if (!this.root) return [];

const arr: TableColumnsAlterDiffNode[] = [];
let ptr: ColumnNode | null = this.root;

while (ptr) {
arr.push({
index: ptr.index,
original: ptr.original ? Object.freeze({ ...ptr.original }) : null,
changed: Object.freeze({ ...ptr.changed }),
});
ptr = ptr.next;
}

return arr;
}
}