utility functions, trat view as table

This commit is contained in:
Jan Prochazka 2020-06-18 20:39:35 +02:00
parent 10d8a40d1c
commit 0d755fa8fc
5 changed files with 46 additions and 7 deletions

View File

@ -43,10 +43,18 @@ const menus = {
label: 'Show CREATE VIEW script',
sqlTemplate: 'CREATE OBJECT',
},
{
label: 'Show CREATE TABLE script',
sqlTemplate: 'CREATE TABLE',
},
{
label: 'Export',
isExport: true,
},
{
label: 'Open structure',
tab: 'TableStructureTab',
},
],
procedures: [
{

View File

@ -1,4 +1,5 @@
import React from 'react';
import _ from 'lodash';
import FormStyledButton from '../widgets/FormStyledButton';
import { useFormikContext } from 'formik';
import styled from 'styled-components';
@ -148,8 +149,9 @@ function SourceTargetConfig({
{ value: 'jsonl', label: 'JSON lines file(s)', directions: ['source', 'target'] },
{ value: 'excel', label: 'MS Excel file(s)', directions: ['source'] },
];
const { values } = useFormikContext();
const { values, setFieldValue } = useFormikContext();
const storageType = values[storageTypeField];
const dbinfo = useDatabaseInfo({ conid: values[connectionIdField], database: values[databaseNameField] });
return (
<Column>
{direction == 'source' && <Label>Source configuration</Label>}
@ -172,6 +174,29 @@ function SourceTargetConfig({
databaseName={databaseNameField}
name={tablesField}
/>
<div>
<FormStyledButton
type="button"
value="All tables"
onClick={() =>
setFieldValue(
'sourceList',
_.uniq([...(values.sourceList || []), ...(dbinfo && dbinfo.tables.map((x) => x.pureName))])
)
}
/>
<FormStyledButton
type="button"
value="All views"
onClick={() =>
setFieldValue(
'sourceList',
_.uniq([...(values.sourceList || []), ...(dbinfo && dbinfo.views.map((x) => x.pureName))])
)
}
/>
<FormStyledButton type="button" value="Remove all" onClick={() => setFieldValue('sourceList', [])} />
</div>
</>
)}
</>

View File

@ -4,7 +4,13 @@ import _ from 'lodash';
import axios from '../utility/axios';
import engines from '@dbgate/engines';
import { useConnectionInfo, getTableInfo, getConnectionInfo, getSqlObjectInfo } from '../utility/metadataLoaders';
import {
useConnectionInfo,
getTableInfo,
getDbCore,
getConnectionInfo,
getSqlObjectInfo,
} from '../utility/metadataLoaders';
import SqlEditor from '../sqleditor/SqlEditor';
import { useUpdateDatabaseForTab, useSetOpenedTabs, useOpenedTabs } from '../utility/globalState';
import QueryToolbar from '../query/QueryToolbar';
@ -24,7 +30,7 @@ function useSqlTemplate(sqlTemplate, props) {
async function loadTemplate() {
if (sqlTemplate == 'CREATE TABLE') {
const tableInfo = await getTableInfo(props);
const tableInfo = await getDbCore(props, props.objectTypeField || 'tables');
const connection = await getConnectionInfo(props);
const driver = engines(connection.engine);
const dmp = driver.createDumper();

View File

@ -5,7 +5,7 @@ import ObjectListControl from '../utility/ObjectListControl';
import { TableColumn } from '../utility/TableControl';
import columnAppObject from '../appobj/columnAppObject';
import constraintAppObject from '../appobj/constraintAppObject';
import { useTableInfo } from '../utility/metadataLoaders';
import { useTableInfo, useDbCore } from '../utility/metadataLoaders';
const WhitePage = styled.div`
position: absolute;
@ -16,8 +16,8 @@ const WhitePage = styled.div`
background-color: white;
`;
export default function TableStructureTab({ conid, database, schemaName, pureName }) {
const tableInfo = useTableInfo({ conid, database, schemaName, pureName });
export default function TableStructureTab({ conid, database, schemaName, pureName, objectTypeField = 'tables' }) {
const tableInfo = useDbCore({ conid, database, schemaName, pureName, objectTypeField });
if (!tableInfo) return null;
const { columns, primaryKey, foreignKeys, dependencies } = tableInfo;
return (

View File

@ -122,7 +122,7 @@ export function useDatabaseInfo(args) {
return useCore(databaseInfoLoader, args);
}
async function getDbCore(args, objectTypeField = undefined) {
export async function getDbCore(args, objectTypeField = undefined) {
const db = await getDatabaseInfo(args);
if (!db) return null;
return db[objectTypeField || args.objectTypeField].find(