mirror of
https://github.com/dbgate/dbgate
synced 2024-11-07 20:26:23 +00:00
sql tree - before refactor
This commit is contained in:
parent
0507e84457
commit
bf607fcb06
4
packages/datalib/src/GridConfig.ts
Normal file
4
packages/datalib/src/GridConfig.ts
Normal file
@ -0,0 +1,4 @@
|
||||
|
||||
export default interface GridConfig {
|
||||
hiddenColumns: number[];
|
||||
}
|
@ -1,5 +1,9 @@
|
||||
import {Select} from '@dbgate/sqltree'
|
||||
import GridConfig from "./GridConfig";
|
||||
|
||||
export default abstract class GridDisplay {
|
||||
abstract getPageQuery(offse: number, count: number): string;
|
||||
constructor(
|
||||
public config: GridConfig,
|
||||
protected setConfig: (configh: GridConfig) => void
|
||||
) {}
|
||||
abstract getPageQuery(offset: number, count: number): string;
|
||||
}
|
||||
|
@ -1,19 +1,30 @@
|
||||
import GridDisplay from "./GridDisplay";
|
||||
import { Select } from "@dbgate/sqltree";
|
||||
import { TableInfo, EngineDriver } from "@dbgate/types";
|
||||
import GridConfig from "./GridConfig";
|
||||
|
||||
export default class TableGridDisplay extends GridDisplay {
|
||||
constructor(public table: TableInfo, public driver: EngineDriver) {
|
||||
super();
|
||||
constructor(
|
||||
public table: TableInfo,
|
||||
public driver: EngineDriver,
|
||||
config: GridConfig,
|
||||
setConfig: (configh: GridConfig) => void
|
||||
) {
|
||||
super(config, setConfig);
|
||||
}
|
||||
|
||||
createSelect() {
|
||||
const select = new Select();
|
||||
select.from = this.table;
|
||||
select.selectAll = true;
|
||||
return select;
|
||||
}
|
||||
|
||||
getPageQuery(offset: number, count: number) {
|
||||
const select = new Select();
|
||||
const select = this.createSelect();
|
||||
if (this.driver.dialect.limitSelect) select.topRecords = count;
|
||||
if (this.driver.dialect.rangeSelect)
|
||||
select.range = { offset: offset, limit: count };
|
||||
select.from = this.table;
|
||||
select.selectAll = true;
|
||||
const sql = select.toSql(this.driver);
|
||||
return sql;
|
||||
}
|
||||
|
@ -18,7 +18,10 @@ const driver = {
|
||||
port,
|
||||
user,
|
||||
password,
|
||||
database
|
||||
database,
|
||||
options: {
|
||||
enableArithAbort: true
|
||||
}
|
||||
});
|
||||
pool._nativeModules = nativeModules;
|
||||
return pool;
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { EngineDriver, SqlDumper } from "@dbgate/types";
|
||||
|
||||
class Command {
|
||||
export class Command {
|
||||
toSql(driver: EngineDriver) {
|
||||
const dumper = driver.createDumper();
|
||||
this.dumpSql(dumper);
|
||||
@ -9,5 +9,3 @@ class Command {
|
||||
|
||||
dumpSql(dumper: SqlDumper) {}
|
||||
}
|
||||
|
||||
export default Command;
|
||||
|
9
packages/sqltree/src/Condition.ts
Normal file
9
packages/sqltree/src/Condition.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { SqlDumper } from "@dbgate/types";
|
||||
|
||||
export abstract class Condition {
|
||||
abstract dumpSql(dumper: SqlDumper) ;
|
||||
}
|
||||
|
||||
export abstract class UnaryCondition extends Condition {
|
||||
// expr: Expresssion;
|
||||
}
|
4
packages/sqltree/src/Expression.ts
Normal file
4
packages/sqltree/src/Expression.ts
Normal file
@ -0,0 +1,4 @@
|
||||
|
||||
export abstract class Expression {
|
||||
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
import Command from "./Command";
|
||||
import { Command } from "./Command";
|
||||
import { NamedObjectInfo, RangeDefinition, SqlDumper } from "@dbgate/types";
|
||||
|
||||
class Select extends Command {
|
||||
export class Select extends Command {
|
||||
topRecords: number;
|
||||
from: NamedObjectInfo;
|
||||
range: RangeDefinition;
|
||||
@ -27,5 +27,3 @@ class Select extends Command {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default Select;
|
||||
|
78
packages/sqltree/src/Source.ts
Normal file
78
packages/sqltree/src/Source.ts
Normal file
@ -0,0 +1,78 @@
|
||||
import { SqlDumper, NamedObjectInfo } from "@dbgate/types";
|
||||
import { Select } from "./Select";
|
||||
|
||||
export class Source {
|
||||
name: NamedObjectInfo;
|
||||
alias: string;
|
||||
subQuery: Select;
|
||||
subQueryString: string;
|
||||
|
||||
dumpSqlDef(dumper: SqlDumper) {
|
||||
let sources = 0;
|
||||
if (this.name != null) sources++;
|
||||
if (this.subQuery != null) sources++;
|
||||
if (this.subQueryString != null) sources++;
|
||||
if (sources != 1)
|
||||
throw new Error("sqltree.Source should have exactly one source");
|
||||
|
||||
if (this.name != null) {
|
||||
dumper.put("%f", this.name);
|
||||
}
|
||||
if (this.subQuery) {
|
||||
dumper.put("(");
|
||||
this.subQuery.dumpSql(dumper);
|
||||
dumper.put(")");
|
||||
}
|
||||
if (this.subQueryString) {
|
||||
dumper.put("(");
|
||||
dumper.putRaw(this.subQueryString);
|
||||
dumper.put(")");
|
||||
}
|
||||
if (this.alias) {
|
||||
dumper.put(" %i", this.alias);
|
||||
}
|
||||
}
|
||||
|
||||
dumpSqlRef(dumper: SqlDumper) {
|
||||
if (this.alias != null) {
|
||||
dumper.put("%i", this.alias);
|
||||
return true;
|
||||
} else if (this.name != null) {
|
||||
dumper.put("%f", this.name);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class Relation {
|
||||
source:Source;
|
||||
joinType: string;
|
||||
// conditions:
|
||||
|
||||
|
||||
// dumpSqlRef(dumper: SqlDumper) {
|
||||
|
||||
// dumper.put("&n");
|
||||
// dumper.putRaw(this.joinType);
|
||||
// dumper.put(" ");
|
||||
// this.source.dumpSqlDef(dumper)
|
||||
// if (Conditions.Any())
|
||||
// {
|
||||
// dumper.put(" ^on ");
|
||||
// bool was = false;
|
||||
// foreach (var cond in Conditions)
|
||||
// {
|
||||
// if (was) dumper.put(" ^and ");
|
||||
// cond.GenSql(dmp);
|
||||
// was = true;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
export class FromDefinition {
|
||||
source: Source;
|
||||
relations: Relation[] = [];
|
||||
}
|
@ -1,2 +1,2 @@
|
||||
export { default as Select } from "./Select";
|
||||
export { default as Command } from "./Command";
|
||||
export { Select } from "./Select";
|
||||
export { Command } from "./Command";
|
||||
|
8
packages/types/dumper.d.ts
vendored
8
packages/types/dumper.d.ts
vendored
@ -2,8 +2,16 @@ import { TableInfo } from "./dbinfo";
|
||||
|
||||
export interface SqlDumper {
|
||||
s: string;
|
||||
|
||||
putRaw(s: string);
|
||||
put(format: string, ...args);
|
||||
putCmd(format: string, ...args);
|
||||
putCollection<T>(
|
||||
delimiter: string,
|
||||
collection: T[],
|
||||
lambda: (item: T) => void
|
||||
);
|
||||
|
||||
endCommand();
|
||||
createTable(table: TableInfo);
|
||||
}
|
||||
|
@ -10,9 +10,10 @@ import engines from '@dbgate/engines';
|
||||
|
||||
export default function TableDataTab({ conid, database, schemaName, pureName }) {
|
||||
const tableInfo = useTableInfo({ conid, database, schemaName, pureName });
|
||||
const [config, setConfig] = React.useState({ hiddenColumns: [] });
|
||||
const connection = useConnectionInfo(conid);
|
||||
if (!tableInfo || !connection) return null;
|
||||
const display = new TableGridDisplay(tableInfo, engines(connection));
|
||||
const display = new TableGridDisplay(tableInfo, engines(connection), config, setConfig);
|
||||
return (
|
||||
<DataGrid
|
||||
// key={`${conid}, ${database}, ${schemaName}, ${pureName}`}
|
||||
|
Loading…
Reference in New Issue
Block a user