mirror of
https://github.com/dbgate/dbgate
synced 2024-11-07 20:26:23 +00:00
execute query from shell
This commit is contained in:
parent
e0c91214fd
commit
f8ee3b92cf
19
packages/api/src/shell/executeQuery.js
Normal file
19
packages/api/src/shell/executeQuery.js
Normal file
@ -0,0 +1,19 @@
|
||||
const goSplit = require('../utility/goSplit');
|
||||
const requireEngineDriver = require('../utility/requireEngineDriver');
|
||||
|
||||
async function executeQuery({ connection, sql }) {
|
||||
console.log(`Execute query ${sql}`);
|
||||
|
||||
const driver = requireEngineDriver(connection);
|
||||
const pool = await driver.connect(connection);
|
||||
console.log(`Connected.`);
|
||||
|
||||
for (const sqlItem of goSplit(sql)) {
|
||||
console.log('Executing query', sqlItem);
|
||||
await driver.query(pool, sqlItem);
|
||||
}
|
||||
|
||||
console.log(`Query finished`);
|
||||
}
|
||||
|
||||
module.exports = executeQuery;
|
@ -15,6 +15,8 @@ const finalizer = require('./finalizer');
|
||||
const registerPlugins = require('./registerPlugins');
|
||||
const requirePlugin = require('./requirePlugin');
|
||||
const download = require('./download');
|
||||
const executeQuery = require('./executeQuery');
|
||||
const loadFile = require('./loadFile');
|
||||
|
||||
const dbgateApi = {
|
||||
queryReader,
|
||||
@ -32,7 +34,8 @@ const dbgateApi = {
|
||||
collectorWriter,
|
||||
finalizer,
|
||||
registerPlugins,
|
||||
download,
|
||||
executeQuery,
|
||||
loadFile,
|
||||
};
|
||||
|
||||
requirePlugin.initialize(dbgateApi);
|
||||
|
10
packages/api/src/shell/loadFile.js
Normal file
10
packages/api/src/shell/loadFile.js
Normal file
@ -0,0 +1,10 @@
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const { filesdir } = require('../utility/directories');
|
||||
|
||||
async function loadFile(file) {
|
||||
const text = await fs.readFile(path.join(filesdir(), file), { encoding: 'utf-8' });
|
||||
return text;
|
||||
}
|
||||
|
||||
module.exports = loadFile;
|
@ -18,6 +18,15 @@ export function extractShellApiPlugins(functionName, props): string[] {
|
||||
return res;
|
||||
}
|
||||
|
||||
export function extractPackageName(name): string {
|
||||
if (!name) return null;
|
||||
const nsMatch = name.match(/^([^@]+)@([^@]+)/);
|
||||
if (nsMatch) {
|
||||
return nsMatch[2];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function extractShellApiFunctionName(functionName) {
|
||||
const nsMatch = functionName.match(/^([^@]+)@([^@]+)/);
|
||||
if (nsMatch) {
|
||||
|
@ -5,20 +5,23 @@ import { DropDownMenuItem } from '../modals/DropDownMenu';
|
||||
import { AppObjectCore } from './AppObjectCore';
|
||||
import useNewQuery from '../query/useNewQuery';
|
||||
import { openNewTab } from '../utility/common';
|
||||
import { useSetOpenedTabs } from '../utility/globalState';
|
||||
import { useCurrentDatabase, useSetOpenedTabs } from '../utility/globalState';
|
||||
import ScriptWriter from '../impexp/ScriptWriter';
|
||||
import { extractPackageName } from 'dbgate-tools';
|
||||
|
||||
function Menu({ data }) {
|
||||
function Menu({ data, menuExt = null }) {
|
||||
const handleDelete = () => {
|
||||
axios.post('files/delete', data);
|
||||
};
|
||||
return (
|
||||
<>
|
||||
<DropDownMenuItem onClick={handleDelete}>Delete</DropDownMenuItem>
|
||||
{menuExt}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export function SavedFileAppObjectBase({ data, commonProps, format, icon, onLoad }) {
|
||||
export function SavedFileAppObjectBase({ data, commonProps, format, icon, onLoad, menuExt = null }) {
|
||||
const { file, folder } = data;
|
||||
|
||||
const onClick = async () => {
|
||||
@ -26,12 +29,48 @@ export function SavedFileAppObjectBase({ data, commonProps, format, icon, onLoad
|
||||
onLoad(resp.data);
|
||||
};
|
||||
|
||||
return <AppObjectCore {...commonProps} data={data} title={file} icon={icon} onClick={onClick} Menu={Menu} />;
|
||||
return (
|
||||
<AppObjectCore
|
||||
{...commonProps}
|
||||
data={data}
|
||||
title={file}
|
||||
icon={icon}
|
||||
onClick={onClick}
|
||||
Menu={menuExt ? (props) => <Menu {...props} menuExt={menuExt} /> : Menu}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export function SavedSqlFileAppObject({ data, commonProps }) {
|
||||
const { file, folder } = data;
|
||||
const newQuery = useNewQuery();
|
||||
const currentDatabase = useCurrentDatabase();
|
||||
const setOpenedTabs = useSetOpenedTabs();
|
||||
|
||||
const connection = _.get(currentDatabase, 'connection');
|
||||
const database = _.get(currentDatabase, 'name');
|
||||
|
||||
const handleGenerateExecute = () => {
|
||||
const script = new ScriptWriter();
|
||||
const conn = {
|
||||
..._.omit(connection, ['displayName', '_id']),
|
||||
database,
|
||||
};
|
||||
script.put(`const sql = await dbgateApi.loadFile('${folder}/${file}');`)
|
||||
script.put(`await dbgateApi.executeQuery({ sql, connection: ${JSON.stringify(conn)} });`)
|
||||
// @ts-ignore
|
||||
script.requirePackage(extractPackageName(conn.engine));
|
||||
|
||||
openNewTab(
|
||||
setOpenedTabs,
|
||||
{
|
||||
title: 'Shell',
|
||||
icon: 'img shell',
|
||||
tabComponent: 'ShellTab',
|
||||
},
|
||||
script.getScript()
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<SavedFileAppObjectBase
|
||||
@ -39,6 +78,11 @@ export function SavedSqlFileAppObject({ data, commonProps }) {
|
||||
commonProps={commonProps}
|
||||
format="text"
|
||||
icon="img sql-file"
|
||||
menuExt={
|
||||
connection && database ? (
|
||||
<DropDownMenuItem onClick={handleGenerateExecute}>Generate shell execute</DropDownMenuItem>
|
||||
) : null
|
||||
}
|
||||
onLoad={(data) => {
|
||||
newQuery({
|
||||
title: file,
|
||||
|
@ -2,7 +2,7 @@ import _ from 'lodash';
|
||||
import { extractShellApiFunctionName, extractShellApiPlugins } from 'dbgate-tools';
|
||||
|
||||
export default class ScriptWriter {
|
||||
constructor(varCount) {
|
||||
constructor(varCount = '0') {
|
||||
this.s = '';
|
||||
this.packageNames = [];
|
||||
// this.engines = [];
|
||||
@ -24,6 +24,10 @@ export default class ScriptWriter {
|
||||
this.packageNames.push(...extractShellApiPlugins(functionName, props));
|
||||
}
|
||||
|
||||
requirePackage(packageName) {
|
||||
this.packageNames.push(packageName);
|
||||
}
|
||||
|
||||
copyStream(sourceVar, targetVar) {
|
||||
this.put(`await dbgateApi.copyStream(${sourceVar}, ${targetVar});`);
|
||||
}
|
||||
@ -32,7 +36,7 @@ export default class ScriptWriter {
|
||||
this.put(`// ${s}`);
|
||||
}
|
||||
|
||||
getScript(extensions, schedule) {
|
||||
getScript(schedule = null) {
|
||||
const packageNames = this.packageNames;
|
||||
let prefix = _.uniq(packageNames)
|
||||
.map((packageName) => `// @require ${packageName}\n`)
|
||||
|
@ -182,7 +182,7 @@ export default async function createImpExpScript(extensions, values, addEditorIn
|
||||
script.comment('@ImportExportConfigurator');
|
||||
script.comment(JSON.stringify(values));
|
||||
}
|
||||
return script.getScript(extensions, values.schedule);
|
||||
return script.getScript(values.schedule);
|
||||
}
|
||||
|
||||
export function getActionOptions(extensions, source, values, targetDbinfo) {
|
||||
|
Loading…
Reference in New Issue
Block a user