mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-22 06:26:00 +00:00
generate shell script
This commit is contained in:
@@ -4,7 +4,14 @@ import { Formik, Form, useFormik, useFormikContext } from 'formik';
|
||||
import styled from 'styled-components';
|
||||
import Select from 'react-select';
|
||||
import { FontIcon } from '../icons';
|
||||
import { FormButtonRow, FormSubmit, FormReactSelect, FormConnectionSelect, FormDatabaseSelect } from '../utility/forms';
|
||||
import {
|
||||
FormButtonRow,
|
||||
FormSubmit,
|
||||
FormReactSelect,
|
||||
FormConnectionSelect,
|
||||
FormDatabaseSelect,
|
||||
FormTablesSelect,
|
||||
} from '../utility/forms';
|
||||
import { useConnectionList, useDatabaseList } from '../utility/metadataLoaders';
|
||||
|
||||
const Wrapper = styled.div`
|
||||
@@ -62,6 +69,7 @@ function SourceTargetConfig({
|
||||
storageTypeField,
|
||||
connectionIdField,
|
||||
databaseNameField,
|
||||
tablesField,
|
||||
}) {
|
||||
const types = [
|
||||
{ value: 'database', label: 'Database' },
|
||||
@@ -80,6 +88,8 @@ function SourceTargetConfig({
|
||||
<FormConnectionSelect name={connectionIdField} />
|
||||
<Label>Database</Label>
|
||||
<FormDatabaseSelect conidName={connectionIdField} name={databaseNameField} />
|
||||
<Label>Tables/views</Label>
|
||||
<FormTablesSelect conidName={connectionIdField} databaseName={databaseNameField} name={tablesField} />
|
||||
</>
|
||||
)}
|
||||
</Column>
|
||||
@@ -88,23 +98,21 @@ function SourceTargetConfig({
|
||||
|
||||
export default function ImportExportConfigurator() {
|
||||
return (
|
||||
<Formik onSubmit={null} initialValues={{ sourceStorageType: 'database', targetStorageType: 'csv' }}>
|
||||
<Form>
|
||||
<Wrapper>
|
||||
<SourceTargetConfig
|
||||
isSource
|
||||
storageTypeField="sourceStorageType"
|
||||
connectionIdField="sourceConnectionId"
|
||||
databaseNameField="sourceDatabaseName"
|
||||
/>
|
||||
<SourceTargetConfig
|
||||
isTarget
|
||||
storageTypeField="targetStorageType"
|
||||
connectionIdField="targetConnectionId"
|
||||
databaseNameField="targetDatabaseName"
|
||||
/>
|
||||
</Wrapper>
|
||||
</Form>
|
||||
</Formik>
|
||||
<Wrapper>
|
||||
<SourceTargetConfig
|
||||
isSource
|
||||
storageTypeField="sourceStorageType"
|
||||
connectionIdField="sourceConnectionId"
|
||||
databaseNameField="sourceDatabaseName"
|
||||
tablesField="sourceTables"
|
||||
/>
|
||||
<SourceTargetConfig
|
||||
isTarget
|
||||
storageTypeField="targetStorageType"
|
||||
connectionIdField="targetConnectionId"
|
||||
databaseNameField="targetDatabaseName"
|
||||
tablesField="targetTables"
|
||||
/>
|
||||
</Wrapper>
|
||||
);
|
||||
}
|
||||
|
||||
49
packages/web/src/impexp/ScriptCreator.js
Normal file
49
packages/web/src/impexp/ScriptCreator.js
Normal file
@@ -0,0 +1,49 @@
|
||||
import ScriptWriter from './ScriptWriter';
|
||||
|
||||
export default class ScriptCreator {
|
||||
constructor() {
|
||||
this.varCount = 0;
|
||||
this.commands = [];
|
||||
}
|
||||
allocVariable(prefix = 'var') {
|
||||
this.varCount += 1;
|
||||
return `${prefix}${this.varCount}`;
|
||||
}
|
||||
getCode() {
|
||||
const writer = new ScriptWriter();
|
||||
for (const command of this.commands) {
|
||||
const { type } = command;
|
||||
switch (type) {
|
||||
case 'assign':
|
||||
{
|
||||
const { variableName, functionName, props } = command;
|
||||
writer.assign(variableName, functionName, props);
|
||||
}
|
||||
break;
|
||||
case 'copyStream':
|
||||
{
|
||||
const { sourceVar, targetVar } = command;
|
||||
writer.copyStream(sourceVar, targetVar);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
writer.finish();
|
||||
return writer.s;
|
||||
}
|
||||
assign(variableName, functionName, props) {
|
||||
this.commands.push({
|
||||
type: 'assign',
|
||||
variableName,
|
||||
functionName,
|
||||
props,
|
||||
});
|
||||
}
|
||||
copyStream(sourceVar, targetVar) {
|
||||
this.commands.push({
|
||||
type: 'copyStream',
|
||||
sourceVar,
|
||||
targetVar,
|
||||
});
|
||||
}
|
||||
}
|
||||
26
packages/web/src/impexp/ScriptWriter.js
Normal file
26
packages/web/src/impexp/ScriptWriter.js
Normal file
@@ -0,0 +1,26 @@
|
||||
export default class ScriptWriter {
|
||||
constructor() {
|
||||
this.s = '';
|
||||
this.put('const dbgateApi = require("@dbgate/api");');
|
||||
this.put('async function run() {');
|
||||
}
|
||||
|
||||
put(s) {
|
||||
this.s += s;
|
||||
this.s += '\n';
|
||||
}
|
||||
|
||||
finish() {
|
||||
this.put('await dbgateApi.copyStream(queryReader, csvWriter);');
|
||||
this.put('dbgateApi.runScript(run);');
|
||||
this.put('}');
|
||||
}
|
||||
|
||||
assign(variableName, functionName, props) {
|
||||
this.put(`const ${variableName} = await dbgateApi.${functionName}(${JSON.stringify(props)});`);
|
||||
}
|
||||
|
||||
copyStream(sourceVar, targetVar) {
|
||||
this.put(`await dbgateApi.copyStream(${sourceVar}, ${targetVar});`);
|
||||
}
|
||||
}
|
||||
50
packages/web/src/impexp/createImpExpScript.js
Normal file
50
packages/web/src/impexp/createImpExpScript.js
Normal file
@@ -0,0 +1,50 @@
|
||||
import ScriptCreator from './ScriptCreator';
|
||||
import getAsArray from '../utility/getAsArray';
|
||||
import { getConnectionInfo } from '../utility/metadataLoaders';
|
||||
import engines from '@dbgate/engines';
|
||||
|
||||
function splitFullName(name) {
|
||||
const i = name.indexOf('.');
|
||||
if (i >= 0)
|
||||
return {
|
||||
schemaName: name.substr(0, i),
|
||||
pureName: name.substr(i + 1),
|
||||
};
|
||||
return {
|
||||
schemaName: null,
|
||||
pureName: name,
|
||||
};
|
||||
}
|
||||
function quoteFullName(dialect, { schemaName, pureName }) {
|
||||
if (schemaName) return `${dialect.quoteIdentifier(schemaName)}.${dialect.quoteIdentifier(pureName)}`;
|
||||
return `${dialect.quoteIdentifier(pureName)}`;
|
||||
}
|
||||
|
||||
export default async function createImpExpScript(values) {
|
||||
const script = new ScriptCreator();
|
||||
if (values.sourceStorageType == 'database') {
|
||||
const tables = getAsArray(values.sourceTables);
|
||||
for (const table of tables) {
|
||||
const sourceVar = script.allocVariable();
|
||||
const connection = await getConnectionInfo({ conid: values.sourceConnectionId });
|
||||
const driver = engines(connection);
|
||||
|
||||
const fullName = splitFullName(table);
|
||||
script.assign(sourceVar, 'queryReader', {
|
||||
connection: {
|
||||
...connection,
|
||||
database: values.sourceDatabaseName,
|
||||
},
|
||||
sql: `select * from ${quoteFullName(driver.dialect, fullName)}`,
|
||||
});
|
||||
|
||||
const targetVar = script.allocVariable();
|
||||
script.assign(targetVar, 'csvWriter', {
|
||||
fileName: `${fullName.pureName}.csv`,
|
||||
});
|
||||
|
||||
script.copyStream(sourceVar, targetVar);
|
||||
}
|
||||
}
|
||||
return script.getCode();
|
||||
}
|
||||
Reference in New Issue
Block a user