This commit is contained in:
Jan Prochazka
2020-03-05 13:19:25 +01:00
parent c47711d346
commit 9ef719ec95
8 changed files with 115 additions and 36 deletions

View File

@@ -16,8 +16,15 @@ export default class TableGridDisplay extends GridDisplay {
createSelect() { createSelect() {
const select: Select = { const select: Select = {
commandType: "select", commandType: "select",
from: this.table, from: {
selectAll: true source: { name: this.table }
},
columns: this.table.columns.map(col => ({
expr: {
exprType: "column",
columnName: col.columnName
}
}))
}; };
return select; return select;
} }

View File

@@ -0,0 +1,10 @@
import { ResultField } from "./types";
export function createColumnResultField(columnName: string): ResultField {
return {
expr: {
exprType: "column",
columnName
}
};
}

View File

@@ -1,5 +1,7 @@
import { SqlDumper } from "@dbgate/types"; import { SqlDumper } from "@dbgate/types";
import { Command, Select } from "./types"; import { Command, Select } from "./types";
import { dumpSqlExpression } from "./dumpSqlExpression";
import { dumpSqlFromDefinition } from "./dumpSqlSource";
export function dumpSqlSelect(dmp: SqlDumper, select: Select) { export function dumpSqlSelect(dmp: SqlDumper, select: Select) {
dmp.put("^select "); dmp.put("^select ");
@@ -11,10 +13,18 @@ export function dumpSqlSelect(dmp: SqlDumper, select: Select) {
} }
if (select.selectAll) { if (select.selectAll) {
dmp.put("* "); dmp.put("* ");
} else {
// TODO
} }
dmp.put("^from %f ", select.from); if (select.columns) {
if (select.selectAll) dmp.put("&n,");
dmp.put("&>&n");
dmp.putCollection(",&n", select.columns, fld => {
dumpSqlExpression(dmp, fld.expr);
if (fld.alias) dmp.put(" %i", fld.alias);
});
dmp.put("&n&<");
}
dmp.put("^from ");
dumpSqlFromDefinition(dmp, select.from);
if (select.range) { if (select.range) {
dmp.put("^limit %s ^offset %s ", select.range.limit, select.range.offset); dmp.put("^limit %s ^offset %s ", select.range.limit, select.range.offset);
} }
@@ -23,7 +33,7 @@ export function dumpSqlSelect(dmp: SqlDumper, select: Select) {
export function dumpSqlCommand(dmp: SqlDumper, command: Command) { export function dumpSqlCommand(dmp: SqlDumper, command: Command) {
switch (command.commandType) { switch (command.commandType) {
case "select": case "select":
dumpSqlSelect(dmp, command as Select); dumpSqlSelect(dmp, command);
break; break;
} }
} }

View File

@@ -0,0 +1,15 @@
import { SqlDumper } from "@dbgate/types";
import { Condition, BinaryCondition } from "./types";
import { dumpSqlExpression } from "./dumpSqlExpression";
export function dumpSqlCondition(dmp: SqlDumper, condition: Condition) {
switch (condition.conditionType) {
case "binary":
dmp.put("(");
dumpSqlExpression(dmp, condition.left);
dmp.put(" %s ", condition.operator);
dumpSqlExpression(dmp, condition.right);
dmp.put(")");
break;
}
}

View File

@@ -2,19 +2,17 @@ import { SqlDumper } from "@dbgate/types";
import { Expression, ColumnRefExpression } from "./types"; import { Expression, ColumnRefExpression } from "./types";
import { dumpSqlSourceRef } from "./dumpSqlSource"; import { dumpSqlSourceRef } from "./dumpSqlSource";
function dumpSqlColumnRef(dumper: SqlDumper, expr: ColumnRefExpression) { export function dumpSqlExpression(dmp: SqlDumper, expr: Expression) {
if (expr.source) {
if (dumpSqlSourceRef(dumper, expr.source)) {
dumper.put(".");
}
}
dumper.put("%i", expr.columnName);
}
export function dumpSqlExpression(dumper: SqlDumper, expr: Expression) {
switch (expr.exprType) { switch (expr.exprType) {
case "column": case "column":
dumpSqlColumnRef(dumper, expr as ColumnRefExpression); {
if (expr.source) {
if (dumpSqlSourceRef(dmp, expr.source)) {
dmp.put(".");
}
}
dmp.put("%i", expr.columnName);
}
break; break;
} }
} }

View File

@@ -1,6 +1,7 @@
import { Source } from "./types"; import { Source, FromDefinition, Relation } from "./types";
import { SqlDumper } from "@dbgate/types"; import { SqlDumper } from "@dbgate/types";
import { dumpSqlSelect } from "./dumpSqlCommand"; import { dumpSqlSelect } from "./dumpSqlCommand";
import { dumpSqlCondition } from "./dumpSqlCondition";
export function dumpSqlSourceDef(dmp: SqlDumper, source: Source) { export function dumpSqlSourceDef(dmp: SqlDumper, source: Source) {
let sources = 0; let sources = 0;
@@ -38,3 +39,20 @@ export function dumpSqlSourceRef(dmp: SqlDumper, source: Source) {
} }
return false; return false;
} }
export function dumpSqlRelation(dmp: SqlDumper, from: Relation) {
dmp.put("&n %k ", from.joinType);
dumpSqlSourceDef(dmp, from.source);
if (from.conditions) {
dmp.put(" ^on ");
dmp.putCollection(" ^and ", from.conditions, cond =>
dumpSqlCondition(dmp, cond)
);
}
}
export function dumpSqlFromDefinition(dmp: SqlDumper, from: FromDefinition) {
dumpSqlSourceDef(dmp, from.source);
dmp.put(" ");
if (from.relations) from.relations.forEach(rel => dumpSqlRelation(dmp, rel));
}

View File

@@ -1,3 +1,5 @@
export * from "./types"; export * from "./types";
export * from "./dumpSqlCommand"; export * from "./dumpSqlCommand";
export * from "./treeToSql"; export * from "./treeToSql";
export * from "./dumpSqlSource";
export * from "./dumpSqlCondition";

View File

@@ -1,27 +1,33 @@
import { NamedObjectInfo, RangeDefinition } from "@dbgate/types"; import { NamedObjectInfo, RangeDefinition } from "@dbgate/types";
export interface Command { // export interface Command {
// }
export interface Select {
commandType: "select"; commandType: "select";
}
export interface Select extends Command { from: FromDefinition;
from: NamedObjectInfo;
columns?: ResultField[];
topRecords?: number; topRecords?: number;
range?: RangeDefinition; range?: RangeDefinition;
distinct?: boolean; distinct?: boolean;
selectAll?: boolean; selectAll?: boolean;
} }
export interface Condition { export type Command = Select;
conditionType: "eq" | "not";
}
export interface UnaryCondition extends Condition { // export interface Condition {
// conditionType: "eq" | "not" | "binary";
// }
export interface UnaryCondition {
expr: Expression; expr: Expression;
} }
export interface BinaryCondition extends Condition { export interface BinaryCondition {
operator: "=" | "!=" | "<" | ">" | ">=" | "<=";
conditionType: "binary";
left: Expression; left: Expression;
right: Expression; right: Expression;
} }
@@ -30,11 +36,13 @@ export interface NotCondition extends UnaryCondition {
conditionType: "not"; conditionType: "not";
} }
export type Condition = BinaryCondition | NotCondition;
export interface Source { export interface Source {
name: NamedObjectInfo; name?: NamedObjectInfo;
alias: string; alias?: string;
subQuery: Select; subQuery?: Select;
subQueryString: string; subQueryString?: string;
} }
export type JoinType = "LEFT JOIN" | "INNER JOIN" | "RIGHT JOIN"; export type JoinType = "LEFT JOIN" | "INNER JOIN" | "RIGHT JOIN";
@@ -45,18 +53,29 @@ export interface Relation {
joinType: JoinType; joinType: JoinType;
} }
export interface Expression { export interface FromDefinition {
exprType: "column" | "value" | "string" | "literal" | "count"; source: Source;
relations?: Relation[];
} }
export interface ColumnRefExpression extends Expression { // export interface Expression {
// exprType: "column" | "value" | "string" | "literal" | "count";
// }
export interface ColumnRefExpression {
exprType: "column"; exprType: "column";
columnName: string; columnName: string;
source: Source; source?: Source;
} }
export interface ValueExpression extends Expression { export interface ValueExpression {
exprType: "value"; exprType: "value";
value: any; value: any;
} }
export type Expression = ColumnRefExpression | ValueExpression;
export interface ResultField {
expr: ValueExpression | ColumnRefExpression;
alias?: string;
}