forked from dbgate/dbgate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
430 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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});`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React from 'react'; | ||
import styled from 'styled-components'; | ||
import AceEditor from 'react-ace'; | ||
import useDimensions from '../utility/useDimensions'; | ||
|
||
const Wrapper = styled.div` | ||
position: absolute; | ||
left: 0; | ||
top: 0; | ||
right: 0; | ||
bottom: 0; | ||
`; | ||
|
||
export default function JavaScriptEditor({ | ||
value = undefined, | ||
readOnly = false, | ||
onChange = undefined, | ||
tabVisible = false, | ||
onKeyDown = undefined, | ||
editorRef = undefined, | ||
focusOnCreate = false, | ||
}) { | ||
const [containerRef, { height, width }] = useDimensions(); | ||
const ownEditorRef = React.useRef(null); | ||
|
||
const currentEditorRef = editorRef || ownEditorRef; | ||
|
||
React.useEffect(() => { | ||
if ((tabVisible || focusOnCreate) && currentEditorRef.current && currentEditorRef.current.editor) | ||
currentEditorRef.current.editor.focus(); | ||
}, [tabVisible, focusOnCreate]); | ||
|
||
const handleKeyDown = React.useCallback( | ||
async (data, hash, keyString, keyCode, event) => { | ||
if (onKeyDown) onKeyDown(data, hash, keyString, keyCode, event); | ||
}, | ||
[onKeyDown] | ||
); | ||
|
||
React.useEffect(() => { | ||
if ((onKeyDown || !readOnly) && currentEditorRef.current) { | ||
currentEditorRef.current.editor.keyBinding.addKeyboardHandler(handleKeyDown); | ||
} | ||
return () => { | ||
currentEditorRef.current.editor.keyBinding.removeKeyboardHandler(handleKeyDown); | ||
}; | ||
}, [handleKeyDown]); | ||
|
||
return ( | ||
<Wrapper ref={containerRef}> | ||
<AceEditor | ||
ref={currentEditorRef} | ||
mode="javascript" | ||
theme="github" | ||
onChange={onChange} | ||
name="UNIQUE_ID_OF_DIV" | ||
editorProps={{ $blockScrolling: true }} | ||
setOptions={{ | ||
showPrintMargin: false, | ||
}} | ||
value={value} | ||
readOnly={readOnly} | ||
fontSize="11pt" | ||
width={`${width}px`} | ||
height={`${height}px`} | ||
/> | ||
</Wrapper> | ||
); | ||
} |
Oops, something went wrong.