dbgate/packages/web/src/perspectives/PerspectiveView.svelte

130 lines
3.6 KiB
Svelte
Raw Normal View History

2022-06-12 17:30:54 +00:00
<script lang="ts">
2022-06-20 20:14:48 +00:00
import {
getTableChildPerspectiveNodes,
PerspectiveDataLoadProps,
PerspectiveTableColumnNode,
PerspectiveTableNode,
} from 'dbgate-datalib';
2022-06-16 15:05:42 +00:00
2022-06-12 17:30:54 +00:00
import _ from 'lodash';
import HorizontalSplitter from '../elements/HorizontalSplitter.svelte';
2022-06-17 20:30:10 +00:00
import { useDatabaseInfo, useTableInfo, useViewInfo } from '../utility/metadataLoaders';
2022-06-12 17:30:54 +00:00
import { getLocalStorage, setLocalStorage } from '../utility/storageCache';
import WidgetColumnBar from '../widgets/WidgetColumnBar.svelte';
import WidgetColumnBarItem from '../widgets/WidgetColumnBarItem.svelte';
2022-06-18 06:46:40 +00:00
import PerspectiveTree from './PerspectiveTree.svelte';
2022-06-20 20:14:48 +00:00
import PerspectiveTable from './PerspectiveTable.svelte';
import { apiCall } from '../utility/api';
import { Select } from 'dbgate-sqltree';
2022-06-12 17:30:54 +00:00
export let conid;
export let database;
export let schemaName;
export let pureName;
2022-06-16 15:05:42 +00:00
export let config;
export let setConfig;
2022-06-12 17:30:54 +00:00
let managerSize;
$: if (managerSize) setLocalStorage('perspectiveManagerWidth', managerSize);
function getInitialManagerSize() {
const width = getLocalStorage('perspectiveManagerWidth');
if (_.isNumber(width) && width > 30 && width < 500) {
return `${width}px`;
}
return '300px';
}
2022-06-17 20:30:10 +00:00
const dbInfo = useDatabaseInfo({ conid, database });
2022-06-12 17:30:54 +00:00
const tableInfo = useTableInfo({ conid, database, schemaName, pureName });
const viewInfo = useViewInfo({ conid, database, schemaName, pureName });
2022-06-16 15:05:42 +00:00
// $: console.log('tableInfo', $tableInfo);
// $: console.log('viewInfo', $viewInfo);
2022-06-20 20:14:48 +00:00
// function getTableNodes(table, dbInfo, config, setConfig) {
// return getTableChildPerspectiveNodes(table, dbInfo, config, setConfig, null);
// }
// function getViewNodes(view, dbInfo, config, setConfig) {
// return [];
// }
// // $: console.log('CFG', config);
// $: nodes = $tableInfo
// ? getTableNodes($tableInfo, $dbInfo, config, setConfig)
// : $viewInfo
// ? getViewNodes($viewInfo, $dbInfo, config, setConfig)
// : null;
async function loader(props: PerspectiveDataLoadProps) {
2022-06-23 14:04:05 +00:00
const { schemaName, pureName, bindingColumns, bindingValues, dataColumns } = props;
2022-06-20 20:14:48 +00:00
const select: Select = {
commandType: 'select',
from: {
name: { schemaName, pureName },
},
2022-06-23 14:04:05 +00:00
columns: dataColumns?.map(columnName => ({
exprType: 'column',
columnName,
source: {
name: { schemaName, pureName },
},
})),
selectAll: !dataColumns,
2022-06-20 20:14:48 +00:00
};
if (bindingColumns?.length == 1) {
select.where = {
conditionType: 'in',
expr: {
exprType: 'column',
columnName: bindingColumns[0],
source: {
name: { schemaName, pureName },
},
},
values: bindingValues,
};
}
const response = await apiCall('database-connections/sql-select', {
conid,
database,
select,
});
if (response.errorMessage) return response;
return response.rows;
2022-06-16 15:05:42 +00:00
}
2022-06-20 20:14:48 +00:00
$: root = $tableInfo ? new PerspectiveTableNode($tableInfo, $dbInfo, config, setConfig, loader as any, null) : null;
2022-06-12 17:30:54 +00:00
</script>
<HorizontalSplitter initialValue={getInitialManagerSize()} bind:size={managerSize}>
<div class="left" slot="1">
<WidgetColumnBar>
2022-06-18 06:46:40 +00:00
<WidgetColumnBarItem title="Choose data" name="perspectiveTree" height="45%">
2022-06-20 20:14:48 +00:00
{#if root}
<PerspectiveTree {root} />
2022-06-16 15:05:42 +00:00
{/if}
2022-06-12 17:30:54 +00:00
</WidgetColumnBarItem>
</WidgetColumnBar>
</div>
<svelte:fragment slot="2">
2022-06-20 20:14:48 +00:00
<PerspectiveTable {root} />
2022-06-12 17:30:54 +00:00
</svelte:fragment>
</HorizontalSplitter>
<style>
.left {
display: flex;
flex: 1;
background-color: var(--theme-bg-0);
}
</style>