From 0d755fa8fca995475e5a78a0ca7e41afe7ce0972 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Thu, 18 Jun 2020 20:39:35 +0200 Subject: [PATCH] utility functions, trat view as table --- .../web/src/appobj/databaseObjectAppObject.js | 8 ++++++ .../src/impexp/ImportExportConfigurator.js | 27 ++++++++++++++++++- packages/web/src/tabs/QueryTab.js | 10 +++++-- packages/web/src/tabs/TableStructureTab.js | 6 ++--- packages/web/src/utility/metadataLoaders.js | 2 +- 5 files changed, 46 insertions(+), 7 deletions(-) diff --git a/packages/web/src/appobj/databaseObjectAppObject.js b/packages/web/src/appobj/databaseObjectAppObject.js index 143ee553..ee01852a 100644 --- a/packages/web/src/appobj/databaseObjectAppObject.js +++ b/packages/web/src/appobj/databaseObjectAppObject.js @@ -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: [ { diff --git a/packages/web/src/impexp/ImportExportConfigurator.js b/packages/web/src/impexp/ImportExportConfigurator.js index 7f1b3dca..4d75fea7 100644 --- a/packages/web/src/impexp/ImportExportConfigurator.js +++ b/packages/web/src/impexp/ImportExportConfigurator.js @@ -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 ( {direction == 'source' && } @@ -172,6 +174,29 @@ function SourceTargetConfig({ databaseName={databaseNameField} name={tablesField} /> +
+ + setFieldValue( + 'sourceList', + _.uniq([...(values.sourceList || []), ...(dbinfo && dbinfo.tables.map((x) => x.pureName))]) + ) + } + /> + + setFieldValue( + 'sourceList', + _.uniq([...(values.sourceList || []), ...(dbinfo && dbinfo.views.map((x) => x.pureName))]) + ) + } + /> + setFieldValue('sourceList', [])} /> +
)} diff --git a/packages/web/src/tabs/QueryTab.js b/packages/web/src/tabs/QueryTab.js index 706e501a..93a53639 100644 --- a/packages/web/src/tabs/QueryTab.js +++ b/packages/web/src/tabs/QueryTab.js @@ -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(); diff --git a/packages/web/src/tabs/TableStructureTab.js b/packages/web/src/tabs/TableStructureTab.js index dec18710..4109882e 100644 --- a/packages/web/src/tabs/TableStructureTab.js +++ b/packages/web/src/tabs/TableStructureTab.js @@ -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 ( diff --git a/packages/web/src/utility/metadataLoaders.js b/packages/web/src/utility/metadataLoaders.js index cccac194..5e8fa775 100644 --- a/packages/web/src/utility/metadataLoaders.js +++ b/packages/web/src/utility/metadataLoaders.js @@ -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(