unified prettier

This commit is contained in:
Jan Prochazka
2020-03-05 13:32:42 +01:00
parent 9ef719ec95
commit ffe8f1027f
12 changed files with 88 additions and 100 deletions

View File

@@ -1,5 +1,10 @@
import GridDisplay from "./GridDisplay"; import GridDisplay from "./GridDisplay";
import { Select, treeToSql, dumpSqlSelect } from "@dbgate/sqltree"; import {
Select,
treeToSql,
dumpSqlSelect,
createColumnResultField
} from "@dbgate/sqltree";
import { TableInfo, EngineDriver } from "@dbgate/types"; import { TableInfo, EngineDriver } from "@dbgate/types";
import GridConfig from "./GridConfig"; import GridConfig from "./GridConfig";
@@ -19,12 +24,9 @@ export default class TableGridDisplay extends GridDisplay {
from: { from: {
source: { name: this.table } source: { name: this.table }
}, },
columns: this.table.columns.map(col => ({ columns: this.table.columns.map(col =>
expr: { createColumnResultField(col.columnName)
exprType: "column", )
columnName: col.columnName
}
}))
}; };
return select; return select;
} }

View File

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

View File

@@ -1,38 +1,38 @@
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 { dumpSqlExpression } from './dumpSqlExpression';
import { dumpSqlFromDefinition } from "./dumpSqlSource"; import { dumpSqlFromDefinition } from './dumpSqlSource';
export function dumpSqlSelect(dmp: SqlDumper, select: Select) { export function dumpSqlSelect(dmp: SqlDumper, select: Select) {
dmp.put("^select "); dmp.put('^select ');
if (select.topRecords) { if (select.topRecords) {
dmp.put("^top %s ", select.topRecords); dmp.put('^top %s ', select.topRecords);
} }
if (select.distinct) { if (select.distinct) {
dmp.put("^distinct "); dmp.put('^distinct ');
} }
if (select.selectAll) { if (select.selectAll) {
dmp.put("* "); dmp.put('* ');
} }
if (select.columns) { if (select.columns) {
if (select.selectAll) dmp.put("&n,"); if (select.selectAll) dmp.put('&n,');
dmp.put("&>&n"); dmp.put('&>&n');
dmp.putCollection(",&n", select.columns, fld => { dmp.putCollection(',&n', select.columns, fld => {
dumpSqlExpression(dmp, fld.expr); dumpSqlExpression(dmp, fld.expr);
if (fld.alias) dmp.put(" %i", fld.alias); if (fld.alias) dmp.put(' %i', fld.alias);
}); });
dmp.put("&n&<"); dmp.put('&n&<');
} }
dmp.put("^from "); dmp.put('^from ');
dumpSqlFromDefinition(dmp, select.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);
} }
} }
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); dumpSqlSelect(dmp, command);
break; break;
} }

View File

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

View File

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

View File

@@ -1,58 +1,55 @@
import { Source, FromDefinition, Relation } 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"; import { dumpSqlCondition } from './dumpSqlCondition';
export function dumpSqlSourceDef(dmp: SqlDumper, source: Source) { export function dumpSqlSourceDef(dmp: SqlDumper, source: Source) {
let sources = 0; let sources = 0;
if (source.name != null) sources++; if (source.name != null) sources++;
if (source.subQuery != null) sources++; if (source.subQuery != null) sources++;
if (source.subQueryString != null) sources++; if (source.subQueryString != null) sources++;
if (sources != 1) if (sources != 1) throw new Error('sqltree.Source should have exactly one source');
throw new Error("sqltree.Source should have exactly one source");
if (source.name != null) { if (source.name != null) {
dmp.put("%f", source.name); dmp.put('%f', source.name);
} }
if (source.subQuery) { if (source.subQuery) {
dmp.put("("); dmp.put('(');
dumpSqlSelect(dmp, source.subQuery); dumpSqlSelect(dmp, source.subQuery);
dmp.put(")"); dmp.put(')');
} }
if (source.subQueryString) { if (source.subQueryString) {
dmp.put("("); dmp.put('(');
dmp.putRaw(source.subQueryString); dmp.putRaw(source.subQueryString);
dmp.put(")"); dmp.put(')');
} }
if (source.alias) { if (source.alias) {
dmp.put(" %i", this.alias); dmp.put(' %i', this.alias);
} }
} }
export function dumpSqlSourceRef(dmp: SqlDumper, source: Source) { export function dumpSqlSourceRef(dmp: SqlDumper, source: Source) {
if (source.alias) { if (source.alias) {
dmp.put("%i", source.alias); dmp.put('%i', source.alias);
return true; return true;
} else if (source.name) { } else if (source.name) {
dmp.put("%f", source.name); dmp.put('%f', source.name);
return true; return true;
} }
return false; return false;
} }
export function dumpSqlRelation(dmp: SqlDumper, from: Relation) { export function dumpSqlRelation(dmp: SqlDumper, from: Relation) {
dmp.put("&n %k ", from.joinType); dmp.put('&n %k ', from.joinType);
dumpSqlSourceDef(dmp, from.source); dumpSqlSourceDef(dmp, from.source);
if (from.conditions) { if (from.conditions) {
dmp.put(" ^on "); dmp.put(' ^on ');
dmp.putCollection(" ^and ", from.conditions, cond => dmp.putCollection(' ^and ', from.conditions, cond => dumpSqlCondition(dmp, cond));
dumpSqlCondition(dmp, cond)
);
} }
} }
export function dumpSqlFromDefinition(dmp: SqlDumper, from: FromDefinition) { export function dumpSqlFromDefinition(dmp: SqlDumper, from: FromDefinition) {
dumpSqlSourceDef(dmp, from.source); dumpSqlSourceDef(dmp, from.source);
dmp.put(" "); dmp.put(' ');
if (from.relations) from.relations.forEach(rel => dumpSqlRelation(dmp, rel)); if (from.relations) from.relations.forEach(rel => dumpSqlRelation(dmp, rel));
} }

View File

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

View File

@@ -1,10 +1,6 @@
import { EngineDriver, SqlDumper } from "@dbgate/types"; import { EngineDriver, SqlDumper } from '@dbgate/types';
export function treeToSql<T>( export function treeToSql<T>(driver: EngineDriver, object: T, func: (dmp: SqlDumper, obj: T) => void) {
driver: EngineDriver,
object: T,
func: (dmp: SqlDumper, obj: T) => void
) {
const dmp = driver.createDumper(); const dmp = driver.createDumper();
func(dmp, object); func(dmp, object);
return dmp.s; return dmp.s;

View File

@@ -1,10 +1,10 @@
import { NamedObjectInfo, RangeDefinition } from "@dbgate/types"; import { NamedObjectInfo, RangeDefinition } from '@dbgate/types';
// export interface Command { // export interface Command {
// } // }
export interface Select { export interface Select {
commandType: "select"; commandType: 'select';
from: FromDefinition; from: FromDefinition;
@@ -26,14 +26,14 @@ export interface UnaryCondition {
} }
export interface BinaryCondition { export interface BinaryCondition {
operator: "=" | "!=" | "<" | ">" | ">=" | "<="; operator: '=' | '!=' | '<' | '>' | '>=' | '<=';
conditionType: "binary"; conditionType: 'binary';
left: Expression; left: Expression;
right: Expression; right: Expression;
} }
export interface NotCondition extends UnaryCondition { export interface NotCondition extends UnaryCondition {
conditionType: "not"; conditionType: 'not';
} }
export type Condition = BinaryCondition | NotCondition; export type Condition = BinaryCondition | NotCondition;
@@ -45,7 +45,7 @@ export interface Source {
subQueryString?: string; subQueryString?: string;
} }
export type JoinType = "LEFT JOIN" | "INNER JOIN" | "RIGHT JOIN"; export type JoinType = 'LEFT JOIN' | 'INNER JOIN' | 'RIGHT JOIN';
export interface Relation { export interface Relation {
source: Source; source: Source;
@@ -63,13 +63,13 @@ export interface FromDefinition {
// } // }
export interface ColumnRefExpression { export interface ColumnRefExpression {
exprType: "column"; exprType: 'column';
columnName: string; columnName: string;
source?: Source; source?: Source;
} }
export interface ValueExpression { export interface ValueExpression {
exprType: "value"; exprType: 'value';
value: any; value: any;
} }

View File

@@ -1,8 +0,0 @@
module.exports = {
trailingComma: 'es5',
tabWidth: 2,
semi: true,
singleQuote: true,
arrowParen: 'avoid',
printWidth: 120,
};

View File

@@ -1,17 +1,17 @@
import React from "react"; import React from 'react';
import ReactDOM from "react-dom"; import ReactDOM from 'react-dom';
import "./index.css"; import './index.css';
import "@fortawesome/fontawesome-free/css/all.css"; import '@fortawesome/fontawesome-free/css/all.css';
import App from "./App"; import App from './App';
import * as serviceWorker from "./serviceWorker"; import * as serviceWorker from './serviceWorker';
import "ace-builds/src-noconflict/mode-sql"; import 'ace-builds/src-noconflict/mode-sql';
import "ace-builds/src-noconflict/mode-mysql"; import 'ace-builds/src-noconflict/mode-mysql';
import "ace-builds/src-noconflict/mode-pgsql"; import 'ace-builds/src-noconflict/mode-pgsql';
import "ace-builds/src-noconflict/mode-sqlserver"; import 'ace-builds/src-noconflict/mode-sqlserver';
import "ace-builds/src-noconflict/theme-github"; import 'ace-builds/src-noconflict/theme-github';
ReactDOM.render(<App />, document.getElementById("root")); ReactDOM.render(<App />, document.getElementById('root'));
// If you want your app to work offline and load faster, you can change // If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls. // unregister() to register() below. Note this comes with some pitfalls.