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() {
const select: Select = {
commandType: "select",
from: this.table,
selectAll: true
from: {
source: { name: this.table }
},
columns: this.table.columns.map(col => ({
expr: {
exprType: "column",
columnName: col.columnName
}
}))
};
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 { Command, Select } from "./types";
import { dumpSqlExpression } from "./dumpSqlExpression";
import { dumpSqlFromDefinition } from "./dumpSqlSource";
export function dumpSqlSelect(dmp: SqlDumper, select: Select) {
dmp.put("^select ");
@ -11,10 +13,18 @@ export function dumpSqlSelect(dmp: SqlDumper, select: Select) {
}
if (select.selectAll) {
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) {
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) {
switch (command.commandType) {
case "select":
dumpSqlSelect(dmp, command as Select);
dumpSqlSelect(dmp, command);
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 { dumpSqlSourceRef } from "./dumpSqlSource";
function dumpSqlColumnRef(dumper: SqlDumper, expr: ColumnRefExpression) {
if (expr.source) {
if (dumpSqlSourceRef(dumper, expr.source)) {
dumper.put(".");
}
}
dumper.put("%i", expr.columnName);
}
export function dumpSqlExpression(dumper: SqlDumper, expr: Expression) {
export function dumpSqlExpression(dmp: SqlDumper, expr: Expression) {
switch (expr.exprType) {
case "column":
dumpSqlColumnRef(dumper, expr as ColumnRefExpression);
{
if (expr.source) {
if (dumpSqlSourceRef(dmp, expr.source)) {
dmp.put(".");
}
}
dmp.put("%i", expr.columnName);
}
break;
}
}

View File

@ -1,6 +1,7 @@
import { Source } from "./types";
import { Source, FromDefinition, Relation } from "./types";
import { SqlDumper } from "@dbgate/types";
import { dumpSqlSelect } from "./dumpSqlCommand";
import { dumpSqlCondition } from "./dumpSqlCondition";
export function dumpSqlSourceDef(dmp: SqlDumper, source: Source) {
let sources = 0;
@ -38,3 +39,20 @@ export function dumpSqlSourceRef(dmp: SqlDumper, source: Source) {
}
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 "./dumpSqlCommand";
export * from "./treeToSql";
export * from "./dumpSqlSource";
export * from "./dumpSqlCondition";

View File

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