mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-28 06:06:00 +00:00
63 lines
1.7 KiB
TypeScript
63 lines
1.7 KiB
TypeScript
import { SqlDumper } from '@dbgate/types';
|
|
import { Command, Select } from './types';
|
|
import { dumpSqlExpression } from './dumpSqlExpression';
|
|
import { dumpSqlFromDefinition } from './dumpSqlSource';
|
|
import { dumpSqlCondition } from './dumpSqlCondition';
|
|
|
|
export function dumpSqlSelect(dmp: SqlDumper, select: Select) {
|
|
dmp.put('^select ');
|
|
if (select.topRecords) {
|
|
dmp.put('^top %s ', select.topRecords);
|
|
}
|
|
if (select.distinct) {
|
|
dmp.put('^distinct ');
|
|
}
|
|
if (select.selectAll) {
|
|
dmp.put('* ');
|
|
}
|
|
if (select.columns) {
|
|
if (select.selectAll) dmp.put('&n,');
|
|
dmp.put('&>&n');
|
|
dmp.putCollection(',&n', select.columns, fld => {
|
|
dumpSqlExpression(dmp, fld);
|
|
if (fld.alias) dmp.put(' ^as %i', fld.alias);
|
|
});
|
|
dmp.put('&n&<');
|
|
}
|
|
dmp.put('^from ');
|
|
dumpSqlFromDefinition(dmp, select.from);
|
|
if (select.where) {
|
|
dmp.put('&n^where ');
|
|
dumpSqlCondition(dmp, select.where);
|
|
dmp.put('&n');
|
|
}
|
|
if (select.groupBy) {
|
|
dmp.put('&n^group ^by ');
|
|
dmp.putCollection(', ', select.groupBy, expr => dumpSqlExpression(dmp, expr));
|
|
dmp.put('&n');
|
|
}
|
|
if (select.orderBy) {
|
|
dmp.put('&n^order ^by ');
|
|
dmp.putCollection(', ', select.orderBy, expr => {
|
|
dumpSqlExpression(dmp, expr);
|
|
dmp.put(' %k', expr.direction);
|
|
});
|
|
dmp.put('&n');
|
|
}
|
|
if (select.range) {
|
|
if (dmp.dialect.offsetFetchRangeSyntax) {
|
|
dmp.put('^offset %s ^rows ^fetch ^next %s ^rows ^only', select.range.offset, select.range.limit);
|
|
} else {
|
|
dmp.put('^limit %s ^offset %s ', select.range.limit, select.range.offset);
|
|
}
|
|
}
|
|
}
|
|
|
|
export function dumpSqlCommand(dmp: SqlDumper, command: Command) {
|
|
switch (command.commandType) {
|
|
case 'select':
|
|
dumpSqlSelect(dmp, command);
|
|
break;
|
|
}
|
|
}
|