mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-26 09:56:00 +00:00
scriptWriterEval
This commit is contained in:
@@ -1,7 +1,21 @@
|
|||||||
import _uniq from 'lodash/uniq';
|
import _uniq from 'lodash/uniq';
|
||||||
import { extractShellApiFunctionName, extractShellApiPlugins } from './packageTools';
|
import { evalShellApiFunctionName, extractShellApiFunctionName, extractShellApiPlugins } from './packageTools';
|
||||||
|
|
||||||
export class ScriptWriterJavaScript {
|
export interface ScriptWriterGeneric {
|
||||||
|
allocVariable(prefix?: string);
|
||||||
|
endLine();
|
||||||
|
assign(variableName: string, functionName: string, props: any);
|
||||||
|
assignValue(variableName: string, jsonValue: any);
|
||||||
|
requirePackage(packageName: string);
|
||||||
|
copyStream(sourceVar: string, targetVar: string, colmapVar?: string, progressName?: string);
|
||||||
|
importDatabase(options: any);
|
||||||
|
dataReplicator(options: any);
|
||||||
|
comment(s: string);
|
||||||
|
zipDirectory(inputDirectory: string, outputFile: string);
|
||||||
|
getScript(schedule?: any): any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class ScriptWriterJavaScript implements ScriptWriterGeneric {
|
||||||
s = '';
|
s = '';
|
||||||
packageNames: string[] = [];
|
packageNames: string[] = [];
|
||||||
varCount = 0;
|
varCount = 0;
|
||||||
@@ -78,7 +92,7 @@ export class ScriptWriterJavaScript {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ScriptWriterJson {
|
export class ScriptWriterJson implements ScriptWriterGeneric {
|
||||||
s = '';
|
s = '';
|
||||||
packageNames: string[] = [];
|
packageNames: string[] = [];
|
||||||
varCount = 0;
|
varCount = 0;
|
||||||
@@ -110,6 +124,10 @@ export class ScriptWriterJson {
|
|||||||
this.packageNames.push(...extractShellApiPlugins(functionName, props));
|
this.packageNames.push(...extractShellApiPlugins(functionName, props));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
requirePackage(packageName) {
|
||||||
|
this.packageNames.push(packageName);
|
||||||
|
}
|
||||||
|
|
||||||
assignValue(variableName, jsonValue) {
|
assignValue(variableName, jsonValue) {
|
||||||
this.commands.push({
|
this.commands.push({
|
||||||
type: 'assignValue',
|
type: 'assignValue',
|
||||||
@@ -167,17 +185,66 @@ export class ScriptWriterJson {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function jsonScriptToJavascript(json) {
|
export class ScriptWriterEval implements ScriptWriterGeneric {
|
||||||
const { schedule, commands, packageNames } = json;
|
s = '';
|
||||||
const script = new ScriptWriterJavaScript();
|
varCount = 0;
|
||||||
for (const packageName of packageNames) {
|
commands = [];
|
||||||
if (!/^dbgate-plugin-.*$/.test(packageName)) {
|
dbgateApi: any;
|
||||||
throw new Error('Unallowed package name:' + packageName);
|
requirePlugin: (name: string) => any;
|
||||||
}
|
variables: { [name: string]: any } = {};
|
||||||
script.packageNames.push(packageName);
|
|
||||||
|
constructor(dbgateApi, requirePlugin, varCount = '0') {
|
||||||
|
this.varCount = parseInt(varCount) || 0;
|
||||||
|
this.dbgateApi = dbgateApi;
|
||||||
|
this.requirePlugin = requirePlugin;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const cmd of commands) {
|
allocVariable(prefix = 'var') {
|
||||||
|
this.varCount += 1;
|
||||||
|
return `${prefix}${this.varCount}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
endLine() {}
|
||||||
|
|
||||||
|
requirePackage(packageName) {}
|
||||||
|
|
||||||
|
async assign(variableName, functionName, props) {
|
||||||
|
const func = evalShellApiFunctionName(functionName, this.dbgateApi, this.requirePlugin);
|
||||||
|
this.variables[variableName] = await func(props);
|
||||||
|
}
|
||||||
|
|
||||||
|
assignValue(variableName, jsonValue) {
|
||||||
|
this.variables[variableName] = jsonValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
async copyStream(sourceVar, targetVar, colmapVar = null, progressName?: string) {
|
||||||
|
await this.dbgateApi.copyStream(this.variables[sourceVar], this.variables[targetVar], {
|
||||||
|
progressName,
|
||||||
|
columns: colmapVar ? this.variables[colmapVar] : null,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
comment(text) {}
|
||||||
|
|
||||||
|
async importDatabase(options) {
|
||||||
|
await this.dbgateApi.importDatabase(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
async dataReplicator(options) {
|
||||||
|
await this.dbgateApi.dataReplicator(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
async zipDirectory(inputDirectory, outputFile) {
|
||||||
|
await this.dbgateApi.zipDirectory(inputDirectory, outputFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
getScript(schedule?: any) {
|
||||||
|
throw new Error('Not implemented');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function playJsonScriptWriter(json, script) {
|
||||||
|
for (const cmd of json.commands) {
|
||||||
switch (cmd.type) {
|
switch (cmd.type) {
|
||||||
case 'assign':
|
case 'assign':
|
||||||
script.assignCore(cmd.variableName, cmd.functionName, cmd.props);
|
script.assignCore(cmd.variableName, cmd.functionName, cmd.props);
|
||||||
@@ -205,6 +272,19 @@ export function jsonScriptToJavascript(json) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function jsonScriptToJavascript(json) {
|
||||||
|
const { schedule, packageNames } = json;
|
||||||
|
const script = new ScriptWriterJavaScript();
|
||||||
|
for (const packageName of packageNames) {
|
||||||
|
if (!/^dbgate-plugin-.*$/.test(packageName)) {
|
||||||
|
throw new Error('Unallowed package name:' + packageName);
|
||||||
|
}
|
||||||
|
script.packageNames.push(packageName);
|
||||||
|
}
|
||||||
|
|
||||||
|
playJsonScriptWriter(json, script);
|
||||||
|
|
||||||
return script.getScript(schedule);
|
return script.getScript(schedule);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,14 @@ export function extractShellApiFunctionName(functionName) {
|
|||||||
return `dbgateApi.${functionName}`;
|
return `dbgateApi.${functionName}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function evalShellApiFunctionName(functionName, dbgateApi, requirePlugin) {
|
||||||
|
const nsMatch = functionName.match(/^([^@]+)@([^@]+)/);
|
||||||
|
if (nsMatch) {
|
||||||
|
return requirePlugin(nsMatch[2]).shellApi[nsMatch[1]];
|
||||||
|
}
|
||||||
|
return dbgateApi[functionName];
|
||||||
|
}
|
||||||
|
|
||||||
export function findEngineDriver(connection, extensions: ExtensionsDirectory): EngineDriver {
|
export function findEngineDriver(connection, extensions: ExtensionsDirectory): EngineDriver {
|
||||||
if (!extensions) {
|
if (!extensions) {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import { ScriptWriterJavaScript, ScriptWriterJson } from 'dbgate-tools';
|
import { ScriptWriterGeneric, ScriptWriterJavaScript, ScriptWriterJson } from 'dbgate-tools';
|
||||||
import getAsArray from '../utility/getAsArray';
|
import getAsArray from '../utility/getAsArray';
|
||||||
import { getConnectionInfo } from '../utility/metadataLoaders';
|
import { getConnectionInfo } from '../utility/metadataLoaders';
|
||||||
import { findEngineDriver, findObjectLike } from 'dbgate-tools';
|
import { findEngineDriver, findObjectLike } from 'dbgate-tools';
|
||||||
@@ -203,12 +203,12 @@ export function normalizeExportColumnMap(colmap) {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function createImpExpScript(extensions, values, forceScript = false) {
|
export default async function createImpExpScript(extensions, values, format = undefined) {
|
||||||
const config = getCurrentConfig();
|
const config = getCurrentConfig();
|
||||||
const script =
|
let script: ScriptWriterGeneric = new ScriptWriterJson(values.startVariableIndex || 0);
|
||||||
config.allowShellScripting || forceScript
|
if (format == 'script' && config.allowShellScripting) {
|
||||||
? new ScriptWriterJavaScript(values.startVariableIndex || 0)
|
script = new ScriptWriterJavaScript(values.startVariableIndex || 0);
|
||||||
: new ScriptWriterJson(values.startVariableIndex || 0);
|
}
|
||||||
|
|
||||||
const [sourceConnection, sourceDriver] = await getConnection(
|
const [sourceConnection, sourceDriver] = await getConnection(
|
||||||
extensions,
|
extensions,
|
||||||
|
|||||||
@@ -167,7 +167,7 @@
|
|||||||
|
|
||||||
const handleGenerateScript = async e => {
|
const handleGenerateScript = async e => {
|
||||||
const values = $formValues as any;
|
const values = $formValues as any;
|
||||||
const code = await createImpExpScript($extensions, values, true);
|
const code = await createImpExpScript($extensions, values, 'script');
|
||||||
openNewTab(
|
openNewTab(
|
||||||
{
|
{
|
||||||
title: 'Shell #',
|
title: 'Shell #',
|
||||||
@@ -183,7 +183,7 @@
|
|||||||
progressHolder = {};
|
progressHolder = {};
|
||||||
const values = $formValues as any;
|
const values = $formValues as any;
|
||||||
busy = true;
|
busy = true;
|
||||||
const script = await createImpExpScript($extensions, values);
|
const script = await createImpExpScript($extensions, values, 'json');
|
||||||
executeNumber += 1;
|
executeNumber += 1;
|
||||||
let runid = runnerId;
|
let runid = runnerId;
|
||||||
const resp = await apiCall('runners/start', { script });
|
const resp = await apiCall('runners/start', { script });
|
||||||
|
|||||||
Reference in New Issue
Block a user