mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-05-02 14:53:58 +00:00
create table script
This commit is contained in:
@@ -1,30 +1,39 @@
|
|||||||
class SqlDumper {
|
class SqlDumper {
|
||||||
/** @param driver {import('@dbgate/types').EngineDriver} */
|
/** @param driver {import('@dbgate/types').EngineDriver} */
|
||||||
constructor(driver) {
|
constructor(driver) {
|
||||||
this.s = '';
|
this.s = "";
|
||||||
this.driver = driver;
|
this.driver = driver;
|
||||||
this.dialect = driver.dialect;
|
this.dialect = driver.dialect;
|
||||||
|
this.indentLevel = 0;
|
||||||
|
}
|
||||||
|
endCommand() {
|
||||||
|
this.putRaw(";\n");
|
||||||
}
|
}
|
||||||
putRaw(text) {
|
putRaw(text) {
|
||||||
this.s += text;
|
this.s += text;
|
||||||
}
|
}
|
||||||
putCmd(format, ...args) {
|
putCmd(format, ...args) {
|
||||||
this.put(format, ...args);
|
this.put(format, ...args);
|
||||||
this.putRaw(';\n');
|
this.endCommand();
|
||||||
}
|
}
|
||||||
putFormattedValue(c, value) {
|
putFormattedValue(c, value) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 's':
|
case "s":
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
this.putRaw(value.toString());
|
this.putRaw(value.toString());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'f':
|
case "i":
|
||||||
|
{
|
||||||
|
this.putRaw(this.dialect.quoteIdentifier(value));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "f":
|
||||||
{
|
{
|
||||||
const { schemaName, pureName } = value;
|
const { schemaName, pureName } = value;
|
||||||
if (schemaName) {
|
if (schemaName) {
|
||||||
this.putRaw(this.dialect.quoteIdentifier(schemaName));
|
this.putRaw(this.dialect.quoteIdentifier(schemaName));
|
||||||
this.putRaw('.');
|
this.putRaw(".");
|
||||||
}
|
}
|
||||||
this.putRaw(this.dialect.quoteIdentifier(pureName));
|
this.putRaw(this.dialect.quoteIdentifier(pureName));
|
||||||
}
|
}
|
||||||
@@ -40,18 +49,37 @@ class SqlDumper {
|
|||||||
let c = format[i];
|
let c = format[i];
|
||||||
i++;
|
i++;
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '^':
|
case "^":
|
||||||
while (i < length && format[i].match(/[a-z]/i)) {
|
while (i < length && format[i].match(/[a-z0-9_]/i)) {
|
||||||
this.putRaw(format[i].toUpperCase());
|
this.putRaw(format[i].toUpperCase());
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case '%':
|
case "%":
|
||||||
c = format[i];
|
c = format[i];
|
||||||
i++;
|
i++;
|
||||||
this.putFormattedValue(c, args[argIndex]);
|
this.putFormattedValue(c, args[argIndex]);
|
||||||
argIndex++;
|
argIndex++;
|
||||||
break;
|
break;
|
||||||
|
case "&":
|
||||||
|
c = format[i];
|
||||||
|
i++;
|
||||||
|
switch (c) {
|
||||||
|
case "&":
|
||||||
|
this.putRaw("&");
|
||||||
|
break;
|
||||||
|
case ">":
|
||||||
|
this.indentLevel++;
|
||||||
|
break;
|
||||||
|
case "<":
|
||||||
|
this.indentLevel--;
|
||||||
|
break;
|
||||||
|
case "n":
|
||||||
|
this.putRaw("\n");
|
||||||
|
this.putRaw(" ".repeat(2 * this.indentLevel));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
this.putRaw(c);
|
this.putRaw(c);
|
||||||
@@ -59,6 +87,123 @@ class SqlDumper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
autoIncrement() {
|
||||||
|
this.put(" ^auto_increment");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param column {import('@dbgate/types').ColumnInfo}
|
||||||
|
*/
|
||||||
|
columnDefinition(
|
||||||
|
column,
|
||||||
|
{
|
||||||
|
includeDefault = true,
|
||||||
|
includeNullable = true,
|
||||||
|
includeCollate = true
|
||||||
|
} = {}
|
||||||
|
) {
|
||||||
|
if (column.computedExpression) {
|
||||||
|
this.put("^as %s", column.computedExpression);
|
||||||
|
if (column.isPersisted) this.put(" ^persisted");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.put("%k", column.dataType);
|
||||||
|
if (column.autoIncrement) {
|
||||||
|
this.autoIncrement();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.putRaw(" ");
|
||||||
|
if (column.isSparse) {
|
||||||
|
this.put(" ^sparse ");
|
||||||
|
}
|
||||||
|
if (includeNullable) {
|
||||||
|
this.put(column.notNull ? "^not ^null" : "^null");
|
||||||
|
}
|
||||||
|
if (includeDefault && column.defaultValue != null) {
|
||||||
|
this.columnDefault(column);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param column {import('@dbgate/types').ColumnInfo}
|
||||||
|
*/
|
||||||
|
columnDefault(column) {
|
||||||
|
if (column.defaultConstraint != null) {
|
||||||
|
this.put(
|
||||||
|
" ^constraint %i ^default %s ",
|
||||||
|
column.defaultConstraint,
|
||||||
|
column.defaultValue
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
this.put(" ^default %s ", column.defaultValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @template T
|
||||||
|
* @param {string} delimiter
|
||||||
|
* @param {T[]} collection
|
||||||
|
* @param {(col: T) => void} lambda
|
||||||
|
*/
|
||||||
|
putCollection(delimiter, collection, lambda) {
|
||||||
|
let first = true;
|
||||||
|
for (const item of collection) {
|
||||||
|
if (!first) this.put(delimiter);
|
||||||
|
first = false;
|
||||||
|
lambda(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/** @param table {import('@dbgate/types').TableInfo} */
|
||||||
|
createTable(table) {
|
||||||
|
this.put("^create ^table %f ( &>&n", table);
|
||||||
|
this.putCollection(", &n", table.columns, col => {
|
||||||
|
this.put("%i", col.columnName);
|
||||||
|
this.columnDefinition(col);
|
||||||
|
});
|
||||||
|
// bool first = true;
|
||||||
|
// _primaryKeyWrittenInCreateTable = false;
|
||||||
|
// foreach (var col in table.Columns)
|
||||||
|
// {
|
||||||
|
// if (!first) Put(", &n");
|
||||||
|
// first = false;
|
||||||
|
// Put("%i ", col.Name);
|
||||||
|
// ColumnDefinition(col, true, true, true);
|
||||||
|
// }
|
||||||
|
// if (table.PrimaryKey != null && !_primaryKeyWrittenInCreateTable)
|
||||||
|
// {
|
||||||
|
// if (!first) Put(", &n");
|
||||||
|
// first = false;
|
||||||
|
// if (table.PrimaryKey.ConstraintName != null)
|
||||||
|
// {
|
||||||
|
// Put("^constraint %i", table.PrimaryKey.ConstraintName);
|
||||||
|
// }
|
||||||
|
// Put(" ^primary ^key (%,i)", table.PrimaryKey.Columns);
|
||||||
|
// }
|
||||||
|
// foreach (var cnt in table.ForeignKeys)
|
||||||
|
// {
|
||||||
|
// if (!first) Put(", &n");
|
||||||
|
// first = false;
|
||||||
|
// CreateForeignKeyCore(cnt);
|
||||||
|
// }
|
||||||
|
// foreach (var cnt in table.Uniques)
|
||||||
|
// {
|
||||||
|
// if (!first) Put(", &n");
|
||||||
|
// first = false;
|
||||||
|
// CreateUniqueCore(cnt);
|
||||||
|
// }
|
||||||
|
// foreach (var cnt in table.Checks)
|
||||||
|
// {
|
||||||
|
// if (!first) Put(", &n");
|
||||||
|
// first = false;
|
||||||
|
// CreateCheckCore(cnt);
|
||||||
|
// }
|
||||||
|
this.put("&<&n)");
|
||||||
|
this.endCommand();
|
||||||
|
// foreach (var ix in table.Indexes)
|
||||||
|
// {
|
||||||
|
// CreateIndex(ix);
|
||||||
|
// }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = SqlDumper;
|
module.exports = SqlDumper;
|
||||||
|
|||||||
4
packages/types/dumper.d.ts
vendored
4
packages/types/dumper.d.ts
vendored
@@ -1,5 +1,9 @@
|
|||||||
|
import { TableInfo } from "./dbinfo";
|
||||||
|
|
||||||
export interface SqlDumper {
|
export interface SqlDumper {
|
||||||
s: string;
|
s: string;
|
||||||
put(format: string, ...args);
|
put(format: string, ...args);
|
||||||
putCmd(format: string, ...args);
|
putCmd(format: string, ...args);
|
||||||
|
endCommand();
|
||||||
|
createTable(table: TableInfo);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import theme from '../theme';
|
|||||||
import AceEditor from 'react-ace';
|
import AceEditor from 'react-ace';
|
||||||
import useDimensions from '../utility/useDimensions';
|
import useDimensions from '../utility/useDimensions';
|
||||||
import engines from '@dbgate/engines';
|
import engines from '@dbgate/engines';
|
||||||
|
import useTableInfo from '../utility/useTableInfo';
|
||||||
|
|
||||||
const Wrapper = styled.div`
|
const Wrapper = styled.div`
|
||||||
position: absolute;
|
position: absolute;
|
||||||
@@ -15,19 +16,15 @@ const Wrapper = styled.div`
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
export default function TableCreateScriptTab({ conid, database, schemaName, pureName }) {
|
export default function TableCreateScriptTab({ conid, database, schemaName, pureName }) {
|
||||||
const sql = `SELECT * FROM MOJE`;
|
|
||||||
const [containerRef, { height, width }] = useDimensions();
|
const [containerRef, { height, width }] = useDimensions();
|
||||||
|
|
||||||
/** @type {import('@dbgate/types').TableInfo} */
|
const tableInfo = useTableInfo({ conid, database, schemaName, pureName });
|
||||||
const tableInfo = useFetch({
|
|
||||||
url: 'tables/table-info',
|
console.log(tableInfo);
|
||||||
params: { conid, database, schemaName, pureName },
|
|
||||||
});
|
|
||||||
|
|
||||||
/** @type {import('@dbgate/types').EngineDriver} */
|
|
||||||
const driver = engines('mssql');
|
const driver = engines('mssql');
|
||||||
const dumper = driver.createDumper();
|
const dumper = driver.createDumper();
|
||||||
dumper.putCmd('^select * ^from %f', { schemaName, pureName });
|
if (tableInfo) dumper.createTable(tableInfo);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Wrapper ref={containerRef}>
|
<Wrapper ref={containerRef}>
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import ObjectListControl from '../utility/ObjectListControl';
|
|||||||
import { TableColumn } from '../utility/TableControl';
|
import { TableColumn } from '../utility/TableControl';
|
||||||
import columnAppObject from '../appobj/columnAppObject';
|
import columnAppObject from '../appobj/columnAppObject';
|
||||||
import constraintAppObject from '../appobj/constraintAppObject';
|
import constraintAppObject from '../appobj/constraintAppObject';
|
||||||
|
import useTableInfo from '../utility/useTableInfo';
|
||||||
|
|
||||||
const WhitePage = styled.div`
|
const WhitePage = styled.div`
|
||||||
position: absolute;
|
position: absolute;
|
||||||
@@ -18,11 +19,7 @@ const WhitePage = styled.div`
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
export default function TableStructureTab({ conid, database, schemaName, pureName }) {
|
export default function TableStructureTab({ conid, database, schemaName, pureName }) {
|
||||||
/** @type {import('@dbgate/types').TableInfo} */
|
const tableInfo = useTableInfo({ conid, database, schemaName, pureName });
|
||||||
const tableInfo = useFetch({
|
|
||||||
url: 'tables/table-info',
|
|
||||||
params: { conid, database, schemaName, pureName },
|
|
||||||
});
|
|
||||||
if (!tableInfo) return null;
|
if (!tableInfo) return null;
|
||||||
const { columns, primaryKey, foreignKeys, dependencies } = tableInfo;
|
const { columns, primaryKey, foreignKeys, dependencies } = tableInfo;
|
||||||
return (
|
return (
|
||||||
|
|||||||
10
packages/web/src/utility/useTableInfo.js
Normal file
10
packages/web/src/utility/useTableInfo.js
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import useFetch from './useFetch';
|
||||||
|
|
||||||
|
export default function useTableInfo({ conid, database, schemaName, pureName }) {
|
||||||
|
/** @type {import('@dbgate/types').TableInfo} */
|
||||||
|
const tableInfo = useFetch({
|
||||||
|
url: 'tables/table-info',
|
||||||
|
params: { conid, database, schemaName, pureName },
|
||||||
|
});
|
||||||
|
return tableInfo;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user