mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-05-01 15:33:57 +00:00
insert rows
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import { Command, Insert, Update, Delete, UpdateField, Condition } from '@dbgate/sqltree';
|
import { Command, Insert, Update, Delete, UpdateField, Condition } from '@dbgate/sqltree';
|
||||||
|
import { NamedObjectInfo } from '@dbgate/types';
|
||||||
|
|
||||||
export interface ChangeSetItem {
|
export interface ChangeSetItem {
|
||||||
pureName: string;
|
pureName: string;
|
||||||
@@ -189,3 +190,31 @@ export function revertChangeSetRowChanges(changeSet: ChangeSet, definition: Chan
|
|||||||
};
|
};
|
||||||
return changeSet;
|
return changeSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getChangeSetInsertedRows(changeSet: ChangeSet, name?: NamedObjectInfo) {
|
||||||
|
if (!name) return [];
|
||||||
|
if (!changeSet) return [];
|
||||||
|
const rows = changeSet.inserts.filter(x => x.pureName == name.pureName && x.schemaName == name.schemaName);
|
||||||
|
const maxIndex = _.maxBy(rows, x => x.insertedRowIndex)?.insertedRowIndex;
|
||||||
|
if (maxIndex == null) return [];
|
||||||
|
const res = Array(maxIndex + 1).fill({});
|
||||||
|
for (const row of rows) {
|
||||||
|
res[row.insertedRowIndex] = row.fields;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function changeSetInsertNewRow(changeSet: ChangeSet, name?: NamedObjectInfo): ChangeSet {
|
||||||
|
const insertedRows = getChangeSetInsertedRows(changeSet, name);
|
||||||
|
return {
|
||||||
|
...changeSet,
|
||||||
|
inserts: [
|
||||||
|
...changeSet.inserts,
|
||||||
|
{
|
||||||
|
...name,
|
||||||
|
insertedRowIndex: insertedRows.length,
|
||||||
|
fields: {},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
@@ -365,7 +365,7 @@ export abstract class GridDisplay {
|
|||||||
return _.pick(row, this.changeSetKeyFields);
|
return _.pick(row, this.changeSetKeyFields);
|
||||||
}
|
}
|
||||||
|
|
||||||
getChangeSetField(row, uniqueName): ChangeSetFieldDefinition {
|
getChangeSetField(row, uniqueName, insertedRowIndex): ChangeSetFieldDefinition {
|
||||||
const col = this.columns.find(x => x.uniqueName == uniqueName);
|
const col = this.columns.find(x => x.uniqueName == uniqueName);
|
||||||
if (!col) return null;
|
if (!col) return null;
|
||||||
if (!this.baseTable) return null;
|
if (!this.baseTable) return null;
|
||||||
@@ -375,7 +375,8 @@ export abstract class GridDisplay {
|
|||||||
schemaName: col.schemaName,
|
schemaName: col.schemaName,
|
||||||
uniqueName: uniqueName,
|
uniqueName: uniqueName,
|
||||||
columnName: col.columnName,
|
columnName: col.columnName,
|
||||||
condition: this.getChangeSetCondition(row),
|
insertedRowIndex,
|
||||||
|
condition: insertedRowIndex == null ? this.getChangeSetCondition(row) : null,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,10 +36,7 @@ export interface Delete {
|
|||||||
export interface Insert {
|
export interface Insert {
|
||||||
commandType: 'insert';
|
commandType: 'insert';
|
||||||
fields: UpdateField[];
|
fields: UpdateField[];
|
||||||
targetTable: {
|
targetTable: NamedObjectInfo;
|
||||||
schemaName: string;
|
|
||||||
pureName: string;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Command = Select | Update | Delete | Insert;
|
export type Command = Select | Update | Delete | Insert;
|
||||||
|
|||||||
@@ -26,7 +26,13 @@ import DataGridRow from './DataGridRow';
|
|||||||
import { countColumnSizes, countVisibleRealColumns } from './gridutil';
|
import { countColumnSizes, countVisibleRealColumns } from './gridutil';
|
||||||
import useModalState from '../modals/useModalState';
|
import useModalState from '../modals/useModalState';
|
||||||
import ConfirmSqlModal from '../modals/ConfirmSqlModal';
|
import ConfirmSqlModal from '../modals/ConfirmSqlModal';
|
||||||
import { changeSetToSql, createChangeSet, revertChangeSetRowChanges } from '@dbgate/datalib';
|
import {
|
||||||
|
changeSetToSql,
|
||||||
|
createChangeSet,
|
||||||
|
revertChangeSetRowChanges,
|
||||||
|
getChangeSetInsertedRows,
|
||||||
|
changeSetInsertNewRow,
|
||||||
|
} from '@dbgate/datalib';
|
||||||
import { scriptToSql } from '@dbgate/sqltree';
|
import { scriptToSql } from '@dbgate/sqltree';
|
||||||
import { sleep } from '../utility/common';
|
import { sleep } from '../utility/common';
|
||||||
|
|
||||||
@@ -232,7 +238,11 @@ export default function DataGridCore(props) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (!isLoadedAll && firstVisibleRowScrollIndex + visibleRowCountUpperBound >= loadedRows.length) {
|
if (
|
||||||
|
!isLoadedAll &&
|
||||||
|
firstVisibleRowScrollIndex + visibleRowCountUpperBound >= loadedRows.length &&
|
||||||
|
insertedRows.length == 0
|
||||||
|
) {
|
||||||
const sql = display.getPageQuery(0, 1);
|
const sql = display.getPageQuery(0, 1);
|
||||||
// try to get SQL, if success, load page. If not, callbacks to load missing metadata are dispatched
|
// try to get SQL, if success, load page. If not, callbacks to load missing metadata are dispatched
|
||||||
if (sql) loadNextData();
|
if (sql) loadNextData();
|
||||||
@@ -284,7 +294,8 @@ export default function DataGridCore(props) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (!loadedRows || !columns) return null;
|
if (!loadedRows || !columns) return null;
|
||||||
const rowCountNewIncluded = loadedRows.length;
|
const insertedRows = getChangeSetInsertedRows(changeSet, display.baseTable);
|
||||||
|
const rowCountNewIncluded = loadedRows.length + insertedRows.length;
|
||||||
|
|
||||||
const handleRowScroll = value => {
|
const handleRowScroll = value => {
|
||||||
setFirstVisibleRowScrollIndex(value);
|
setFirstVisibleRowScrollIndex(value);
|
||||||
@@ -401,6 +412,18 @@ export default function DataGridCore(props) {
|
|||||||
// this.saveAndFocus();
|
// this.saveAndFocus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (event.keyCode == keycodes.insert) {
|
||||||
|
event.preventDefault();
|
||||||
|
if (display.baseTable) {
|
||||||
|
setChangeSet(changeSetInsertNewRow(changeSet, display.baseTable));
|
||||||
|
const cell = [rowCountNewIncluded, (currentCell && currentCell[1]) || 0];
|
||||||
|
// @ts-ignore
|
||||||
|
setCurrentCell(cell);
|
||||||
|
scrollIntoView(cell);
|
||||||
|
}
|
||||||
|
// this.saveAndFocus();
|
||||||
|
}
|
||||||
|
|
||||||
if (inplaceEditorState.cell) return;
|
if (inplaceEditorState.cell) return;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@@ -574,6 +597,8 @@ export default function DataGridCore(props) {
|
|||||||
// columnSizes.getVisibleScrollSizeSum()
|
// columnSizes.getVisibleScrollSizeSum()
|
||||||
// );
|
// );
|
||||||
|
|
||||||
|
const loadedAndInsertedRows = [...loadedRows, ...insertedRows];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<GridContainer ref={containerRef}>
|
<GridContainer ref={containerRef}>
|
||||||
<Table
|
<Table
|
||||||
@@ -629,7 +654,7 @@ export default function DataGridCore(props) {
|
|||||||
</TableHeaderRow>
|
</TableHeaderRow>
|
||||||
</TableHead>
|
</TableHead>
|
||||||
<TableBody ref={tableBodyRef}>
|
<TableBody ref={tableBodyRef}>
|
||||||
{loadedRows
|
{loadedAndInsertedRows
|
||||||
.slice(firstVisibleRowScrollIndex, firstVisibleRowScrollIndex + visibleRowCountUpperBound)
|
.slice(firstVisibleRowScrollIndex, firstVisibleRowScrollIndex + visibleRowCountUpperBound)
|
||||||
.map((row, index) => (
|
.map((row, index) => (
|
||||||
<DataGridRow
|
<DataGridRow
|
||||||
@@ -640,6 +665,11 @@ export default function DataGridCore(props) {
|
|||||||
inplaceEditorState={inplaceEditorState}
|
inplaceEditorState={inplaceEditorState}
|
||||||
dispatchInsplaceEditor={dispatchInsplaceEditor}
|
dispatchInsplaceEditor={dispatchInsplaceEditor}
|
||||||
cellIsSelected={cellIsSelected}
|
cellIsSelected={cellIsSelected}
|
||||||
|
insertedRowIndex={
|
||||||
|
firstVisibleRowScrollIndex + index >= loadedRows.length
|
||||||
|
? firstVisibleRowScrollIndex + index - loadedRows.length
|
||||||
|
: null
|
||||||
|
}
|
||||||
changeSet={changeSet}
|
changeSet={changeSet}
|
||||||
setChangeSet={setChangeSet}
|
setChangeSet={setChangeSet}
|
||||||
display={display}
|
display={display}
|
||||||
|
|||||||
@@ -31,15 +31,24 @@ const TableBodyCell = styled.td`
|
|||||||
color: white;`}
|
color: white;`}
|
||||||
${props =>
|
${props =>
|
||||||
props.isModifiedRow &&
|
props.isModifiedRow &&
|
||||||
|
!props.isInsertedRow &&
|
||||||
!props.isSelected &&
|
!props.isSelected &&
|
||||||
!props.isModifiedCell &&
|
!props.isModifiedCell &&
|
||||||
`
|
`
|
||||||
background-color: #FFFFDB;`}
|
background-color: #FFFFDB;`}
|
||||||
${props =>
|
${props =>
|
||||||
!props.isSelected &&
|
!props.isSelected &&
|
||||||
|
!props.isInsertedRow &&
|
||||||
props.isModifiedCell &&
|
props.isModifiedCell &&
|
||||||
`
|
`
|
||||||
background-color: bisque;`}
|
background-color: bisque;`}
|
||||||
|
|
||||||
|
${props =>
|
||||||
|
!props.isSelected &&
|
||||||
|
props.isInsertedRow &&
|
||||||
|
`
|
||||||
|
background-color: #DBFFDB;`}
|
||||||
|
|
||||||
`;
|
`;
|
||||||
const HintSpan = styled.span`
|
const HintSpan = styled.span`
|
||||||
color: gray;
|
color: gray;
|
||||||
@@ -86,6 +95,7 @@ export default function DataGridRow({
|
|||||||
display,
|
display,
|
||||||
changeSet,
|
changeSet,
|
||||||
setChangeSet,
|
setChangeSet,
|
||||||
|
insertedRowIndex,
|
||||||
}) {
|
}) {
|
||||||
// console.log('RENDER ROW', rowIndex);
|
// console.log('RENDER ROW', rowIndex);
|
||||||
const rowDefinition = display.getChangeSetRow(row);
|
const rowDefinition = display.getChangeSetRow(row);
|
||||||
@@ -110,8 +120,11 @@ export default function DataGridRow({
|
|||||||
isSelected={cellIsSelected(rowIndex, col.colIndex)}
|
isSelected={cellIsSelected(rowIndex, col.colIndex)}
|
||||||
isModifiedRow={!!matchedChangeSetItem}
|
isModifiedRow={!!matchedChangeSetItem}
|
||||||
isModifiedCell={matchedChangeSetItem && col.uniqueName in matchedChangeSetItem.fields}
|
isModifiedCell={matchedChangeSetItem && col.uniqueName in matchedChangeSetItem.fields}
|
||||||
|
isInsertedRow={insertedRowIndex != null}
|
||||||
>
|
>
|
||||||
{inplaceEditorState.cell && rowIndex == inplaceEditorState.cell[0] && col.colIndex == inplaceEditorState.cell[1] ? (
|
{inplaceEditorState.cell &&
|
||||||
|
rowIndex == inplaceEditorState.cell[0] &&
|
||||||
|
col.colIndex == inplaceEditorState.cell[1] ? (
|
||||||
<InplaceEditor
|
<InplaceEditor
|
||||||
widthPx={col.widthPx}
|
widthPx={col.widthPx}
|
||||||
inplaceEditorState={inplaceEditorState}
|
inplaceEditorState={inplaceEditorState}
|
||||||
@@ -119,8 +132,9 @@ export default function DataGridRow({
|
|||||||
cellValue={rowUpdated[col.uniqueName]}
|
cellValue={rowUpdated[col.uniqueName]}
|
||||||
changeSet={changeSet}
|
changeSet={changeSet}
|
||||||
setChangeSet={setChangeSet}
|
setChangeSet={setChangeSet}
|
||||||
definition={display.getChangeSetField(row, col.uniqueName)}
|
insertedRowIndex={insertedRowIndex}
|
||||||
/>
|
definition={display.getChangeSetField(row, col.uniqueName, insertedRowIndex)}
|
||||||
|
/>
|
||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
<CellFormattedValue value={rowUpdated[col.uniqueName]} />
|
<CellFormattedValue value={rowUpdated[col.uniqueName]} />
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ export default function InplaceEditor({
|
|||||||
cellValue,
|
cellValue,
|
||||||
inplaceEditorState,
|
inplaceEditorState,
|
||||||
dispatchInsplaceEditor,
|
dispatchInsplaceEditor,
|
||||||
|
isInsertedRow,
|
||||||
}) {
|
}) {
|
||||||
const editorRef = React.useRef();
|
const editorRef = React.useRef();
|
||||||
const isChangedRef = React.useRef(!!inplaceEditorState.text);
|
const isChangedRef = React.useRef(!!inplaceEditorState.text);
|
||||||
|
|||||||
Reference in New Issue
Block a user