sql tree - before refactor

This commit is contained in:
Jan Prochazka 2020-03-05 11:42:16 +01:00
parent 0507e84457
commit bf607fcb06
12 changed files with 136 additions and 18 deletions

View File

@ -0,0 +1,4 @@
export default interface GridConfig {
hiddenColumns: number[];
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -18,7 +18,10 @@ const driver = {
port,
user,
password,
database
database,
options: {
enableArithAbort: true
}
});
pool._nativeModules = nativeModules;
return pool;

View File

@ -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;

View 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;
}

View File

@ -0,0 +1,4 @@
export abstract class Expression {
}

View File

@ -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;

View 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[] = [];
}

View File

@ -1,2 +1,2 @@
export { default as Select } from "./Select";
export { default as Command } from "./Command";
export { Select } from "./Select";
export { Command } from "./Command";

View File

@ -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);
}

View File

@ -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}`}