Skip to content

Commit

Permalink
fixing all type error
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Oct 8, 2023
1 parent 70653c1 commit 1c29de3
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 69 deletions.
7 changes: 6 additions & 1 deletion src/drivers/base/NotImplementCommonInterface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { DatabaseSchemas, TableDefinitionSchema } from 'types/SqlSchema';
import SQLCommonInterface from './SQLCommonInterface';
import { SqlStatementResult } from 'libs/SqlRunnerManager';
import { QueryTypedResult } from 'types/SqlResult';
import { QueryResultHeader, QueryTypedResult } from 'types/SqlResult';
import BaseType from 'renderer/datatype/BaseType';

export default class NotImplementCommonInterface extends SQLCommonInterface {
public FLAG_USE_STATEMENT = false;
Expand All @@ -26,6 +27,10 @@ export default class NotImplementCommonInterface extends SQLCommonInterface {
throw new Error('Not implemented');
}

createTypeValue(): BaseType<unknown> {
throw new Error('Not implemented');
}

attachType(): SqlStatementResult<QueryTypedResult> {
throw new Error('Not implemented');
}
Expand Down
5 changes: 4 additions & 1 deletion src/drivers/base/SQLCommonInterface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SqlStatementResult } from 'libs/SqlRunnerManager';
import { QueryTypedResult } from 'types/SqlResult';
import BaseType from 'renderer/datatype/BaseType';
import { QueryResultHeader, QueryTypedResult } from 'types/SqlResult';
import { DatabaseSchemas, TableDefinitionSchema } from 'types/SqlSchema';

export default abstract class SQLCommonInterface {
Expand All @@ -26,4 +27,6 @@ export default abstract class SQLCommonInterface {
abstract attachType(
statement: SqlStatementResult,
): SqlStatementResult<QueryTypedResult>;

abstract createTypeValue(header: QueryResultHeader, value: unknown): BaseType;
}
34 changes: 23 additions & 11 deletions src/drivers/mysql/MySQLCommonInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import StringType from 'renderer/datatype/StringType';
import NumberType from 'renderer/datatype/NumberType';
import DecimalType from 'renderer/datatype/DecimalType';
import JsonType from 'renderer/datatype/JsonType';
import BaseType from 'renderer/datatype/BaseType';

interface MySqlDatabase {
SCHEMA_NAME: string;
Expand Down Expand Up @@ -166,6 +167,8 @@ function mapDataType(header: QueryResultHeader): QueryResultHeader {
].includes(columnType)
) {
type = { type: 'string_datetime' };
} else if (header.columnDefinition?.dataType === 'enum') {
type = { type: 'enum', enumValues: header.columnDefinition?.enumValues };
}

return {
Expand Down Expand Up @@ -427,7 +430,6 @@ export default class MySQLCommonInterface extends SQLCommonInterface {
statements: SqlStatementResult[],
schema: DatabaseSchemas | undefined,
): SqlStatementResult<QueryTypedResult>[] {
console.log(statements);
if (!schema) return statements.map(this.attachType);
const databaseList = schema.getSchema();

Expand All @@ -444,21 +446,31 @@ export default class MySQLCommonInterface extends SQLCommonInterface {
});
}

protected getTypeClass(
header: QueryResultHeader,
):
| typeof NumberType
| typeof DecimalType
| typeof JsonType
| typeof StringType {
if (header.type.type === 'number') return NumberType;
if (header.type.type === 'decimal') return DecimalType;
if (header.type.type === 'json') return JsonType;
return StringType;
}

createTypeValue(header: QueryResultHeader, value: unknown): BaseType {
const typeClass = this.getTypeClass(header);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return new typeClass(value as any);
}

attachType(statements: SqlStatementResult) {
const headers = statements.result.headers;
const rows = statements.result.rows;

for (const header of headers) {
let typeClass:
| typeof StringType
| typeof NumberType
| typeof JsonType
| typeof DecimalType = StringType;

if (header.type.type === 'number') typeClass = NumberType;
if (header.type.type === 'decimal') typeClass = DecimalType;
if (header.type.type === 'json') typeClass = JsonType;

const typeClass = this.getTypeClass(header);
for (const row of rows) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
row[header.name] = new typeClass(row[header.name] as any);
Expand Down
32 changes: 22 additions & 10 deletions src/drivers/pg/PgCommonInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import SQLCommonInterface from './../base/SQLCommonInterface';
import { SqlRunnerManager, SqlStatementResult } from 'libs/SqlRunnerManager';
import {
QueryResult,
QueryResultHeader,
QueryResultHeaderType,
QueryTypedResult,
} from 'types/SqlResult';
Expand All @@ -16,6 +17,7 @@ import DecimalType from 'renderer/datatype/DecimalType';
import NumberType from 'renderer/datatype/NumberType';
import StringType from 'renderer/datatype/StringType';
import JsonType from 'renderer/datatype/JsonType';
import BaseType from 'renderer/datatype/BaseType';

interface PgColumn {
table_schema: string;
Expand Down Expand Up @@ -259,21 +261,31 @@ export default class PgCommonInterface extends SQLCommonInterface {
return result;
}

protected getTypeClass(
header: QueryResultHeader,
):
| typeof NumberType
| typeof DecimalType
| typeof JsonType
| typeof StringType {
if (header.type.type === 'number') return NumberType;
if (header.type.type === 'decimal') return DecimalType;
if (header.type.type === 'json') return JsonType;
return StringType;
}

createTypeValue(header: QueryResultHeader, value: unknown): BaseType {
const typeClass = this.getTypeClass(header);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return new typeClass(value as any);
}

attachType(statements: SqlStatementResult) {
const headers = statements.result.headers;
const rows = statements.result.rows;

for (const header of headers) {
let typeClass:
| typeof StringType
| typeof NumberType
| typeof JsonType
| typeof DecimalType = StringType;

if (header.type.type === 'number') typeClass = NumberType;
if (header.type.type === 'decimal') typeClass = DecimalType;
if (header.type.type === 'json') typeClass = JsonType;

const typeClass = this.getTypeClass(header);
for (const row of rows) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
row[header.name] = new typeClass(row[header.name] as any);
Expand Down
9 changes: 5 additions & 4 deletions src/renderer/contexts/EditableQueryResultProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
useContext,
useMemo,
} from 'react';
import BaseType from 'renderer/datatype/BaseType';

import { TableEditableCellHandler } from 'renderer/screens/DatabaseScreen/QueryResultViewer/TableCell/TableEditableCell';

Expand Down Expand Up @@ -67,7 +68,7 @@ export class TableCellManager {
}

const QueryResultChangeContext = createContext<{
setChange: (row: number, col: number, value: unknown) => void;
setChange: (row: number, col: number, value: BaseType) => void;
removeChange: (row: number, col: number) => void;
collector: ResultChangeCollector;
clearChange: () => void;
Expand All @@ -89,17 +90,17 @@ export function EditableQueryResultProvider({ children }: PropsWithChildren) {
const manager = useMemo(() => new TableCellManager(), []);

const setChange = useCallback(
(row: number, col: number, value: unknown) => {
(row: number, col: number, value: BaseType) => {
collector.addChange(row, col, value);
},
[collector]
[collector],
);

const removeChange = useCallback(
(row: number, col: number) => {
collector.removeChange(row, col);
},
[collector]
[collector],
);

const clearChange = useCallback(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import {
} from '@fortawesome/free-solid-svg-icons';
import { useDebounceEffect } from 'hooks/useDebounce';
import CommitChangeToolbarItem from './CommitChangeToolbarItem';
import BaseType from 'renderer/datatype/BaseType';

interface QueryResultActionProps {
result: QueryResult;
resultAfterFilter: { data: Record<string, unknown>; rowIndex: number }[];
result: QueryTypedResult;
resultAfterFilter: { data: Record<string, BaseType>; rowIndex: number }[];
onResultChange: React.Dispatch<React.SetStateAction<QueryTypedResult>>;
onSearchChange: (v: string) => void;
onRequestRefetch: () => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
faChevronUp,
} from '@fortawesome/free-solid-svg-icons';
import { useEditableResult } from 'renderer/contexts/EditableQueryResultProvider';
import BaseType from 'renderer/datatype/BaseType';

export interface QuertResultTableSortedHeader {
by: 'ASC' | 'DESC';
Expand All @@ -27,7 +28,7 @@ export interface QuertResultTableSortedHeader {

interface QueryResultTableProps {
headers: QueryResultHeader[];
result: QueryResultWithIndex[];
result: QueryResultWithIndex<BaseType>[];
onSortHeader?: (header: QuertResultTableSortedHeader) => void;
onSortReset?: () => void;
sortedHeader?: QuertResultTableSortedHeader;
Expand Down Expand Up @@ -71,7 +72,7 @@ function QueryResultTable({
setSelectedRowsIndex(selectedRows);
};

const data: { data: Record<string, unknown>; rowIndex: number }[] =
const data: { data: Record<string, BaseType>; rowIndex: number }[] =
useMemo(() => {
const newRows = new Array(newRowCount)
.fill(false)
Expand Down Expand Up @@ -125,7 +126,7 @@ function QueryResultTable({
const maxLength = Math.max(
...result.slice(0, 100).map(({ data: row }) => {
if (typeof row[header.name] === 'string')
return (row[header.name] as string).length;
return row[header.name].toString().length;
return 10;
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,9 @@ function getComponentFromHeader(
['string_date', 'string_time', 'string_datetime'].includes(header.type.type)
) {
return TableCellDateString;
} else if (header.type.type === 'enum') {
return TableCellEnum;
} else if (['string'].includes(header.type.type)) {
if (header.columnDefinition) {
if (header.columnDefinition.dataType === 'enum') {
return TableCellEnum;
}
}

return TableCellString;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function TableCellStringEditor({

return (
<TableCellSelect
items={[...(header.columnDefinition?.enumValues ?? [])]}
items={[...(header.type?.enumValues ?? [])]}
readOnly={readOnly}
onChange={setEditValue}
onLostFocus={onLostFocus}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface TableEditableContentProps<T = unknown> {
value: T;
}

interface TableEditableCellProps<T = unknown> {
interface TableEditableCellProps<T = BaseType> {
diff: (prev: unknown, current: unknown) => boolean;
editor?: React.FC<TableEditableEditorProps<T>>;
detactEditor?: boolean;
Expand All @@ -49,7 +49,7 @@ interface TableEditableCellProps<T = unknown> {
header: QueryResultHeader;
}

function TableEditableCellWithRef<T = unknown>(
function TableEditableCellWithRef<T = BaseType>(
{
diff,
detactEditor,
Expand All @@ -76,7 +76,7 @@ function TableEditableCellWithRef<T = unknown>(
const divRef = useRef<HTMLDivElement>(null);

const insertValueHandler = useCallback(
(newValue: unknown) => {
(newValue: BaseType) => {
if (readOnly) return;

setAfterValue(newValue);
Expand Down Expand Up @@ -142,7 +142,7 @@ function TableEditableCellWithRef<T = unknown>(
}, [setFocus, cellManager, onFocus, row, col]);

const onExitEditMode = useCallback(
(discard: boolean, newValue: unknown) => {
(discard: boolean, newValue: BaseType) => {
setOnEditMode(false);
if (!discard) {
setAfterValue(newValue);
Expand Down Expand Up @@ -196,7 +196,8 @@ function TableEditableCellWithRef<T = unknown>(
<Editor
header={header}
value={afterValue as T}
onExit={onExitEditMode}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
onExit={onExitEditMode as any}
readOnly={readOnly}
/>
<Content value={afterValue as T} header={header} />
Expand All @@ -205,7 +206,8 @@ function TableEditableCellWithRef<T = unknown>(
<Editor
header={header}
value={afterValue as T}
onExit={onExitEditMode}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
onExit={onExitEditMode as any}
readOnly={readOnly}
/>
)
Expand Down
Loading

0 comments on commit 1c29de3

Please sign in to comment.