2020-03-12 11:46:07 +00:00
|
|
|
import _ from 'lodash';
|
2022-11-03 15:40:54 +00:00
|
|
|
import type { SqlDumper } from 'dbgate-types';
|
2020-03-05 12:32:42 +00:00
|
|
|
import { Expression, ColumnRefExpression } from './types';
|
|
|
|
import { dumpSqlSourceRef } from './dumpSqlSource';
|
2020-03-05 11:23:07 +00:00
|
|
|
|
2020-03-05 12:19:25 +00:00
|
|
|
export function dumpSqlExpression(dmp: SqlDumper, expr: Expression) {
|
2020-03-05 11:23:07 +00:00
|
|
|
switch (expr.exprType) {
|
2020-03-05 12:32:42 +00:00
|
|
|
case 'column':
|
2020-03-05 12:19:25 +00:00
|
|
|
{
|
|
|
|
if (expr.source) {
|
|
|
|
if (dumpSqlSourceRef(dmp, expr.source)) {
|
2020-03-05 12:32:42 +00:00
|
|
|
dmp.put('.');
|
2020-03-05 12:19:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-05 12:32:42 +00:00
|
|
|
dmp.put('%i', expr.columnName);
|
2020-03-05 12:19:25 +00:00
|
|
|
}
|
2020-03-05 11:23:07 +00:00
|
|
|
break;
|
2020-03-12 11:46:07 +00:00
|
|
|
|
|
|
|
case 'placeholder':
|
|
|
|
dmp.putRaw('{PLACEHOLDER}');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'value':
|
|
|
|
dmp.put('%v', expr.value);
|
|
|
|
break;
|
2020-04-14 17:37:31 +00:00
|
|
|
|
|
|
|
case 'raw':
|
|
|
|
dmp.put('%s', expr.sql);
|
|
|
|
break;
|
2020-06-21 08:36:43 +00:00
|
|
|
|
2024-07-25 08:59:43 +00:00
|
|
|
case 'unaryRaw':
|
|
|
|
if (expr.beforeSql) dmp.putRaw(expr.beforeSql);
|
|
|
|
dumpSqlExpression(dmp, expr.expr);
|
|
|
|
if (expr.afterSql) dmp.putRaw(expr.afterSql);
|
|
|
|
break;
|
|
|
|
|
2020-06-21 08:36:43 +00:00
|
|
|
case 'call':
|
2020-12-29 14:07:32 +00:00
|
|
|
dmp.put('%s(', expr.func);
|
|
|
|
if (expr.argsPrefix) dmp.put('%s ', expr.argsPrefix);
|
2021-01-23 06:24:46 +00:00
|
|
|
dmp.putCollection(',', expr.args, x => dumpSqlExpression(dmp, x));
|
2020-06-21 08:36:43 +00:00
|
|
|
dmp.put(')');
|
|
|
|
break;
|
2020-06-21 19:44:54 +00:00
|
|
|
|
2022-06-11 17:19:50 +00:00
|
|
|
case 'methodCall':
|
2024-07-25 08:59:43 +00:00
|
|
|
dumpSqlExpression(dmp, expr.thisObject);
|
2022-06-11 17:19:50 +00:00
|
|
|
dmp.put('.%s(', expr.method);
|
|
|
|
dmp.putCollection(',', expr.args, x => dumpSqlExpression(dmp, x));
|
|
|
|
dmp.put(')');
|
|
|
|
break;
|
|
|
|
|
2020-06-21 19:44:54 +00:00
|
|
|
case 'transform':
|
|
|
|
dmp.transform(expr.transform, () => dumpSqlExpression(dmp, expr.expr));
|
|
|
|
break;
|
2021-04-25 09:48:23 +00:00
|
|
|
|
|
|
|
case 'rowNumber':
|
2022-06-11 17:19:50 +00:00
|
|
|
dmp.put(' ^row_number() ^over (^order ^by ');
|
2021-04-25 09:48:23 +00:00
|
|
|
dmp.putCollection(', ', expr.orderBy, x => {
|
|
|
|
dumpSqlExpression(dmp, x);
|
|
|
|
dmp.put(' %k', x.direction);
|
|
|
|
});
|
2022-06-11 17:19:50 +00:00
|
|
|
dmp.put(')');
|
2021-04-25 09:48:23 +00:00
|
|
|
break;
|
2020-03-05 11:23:07 +00:00
|
|
|
}
|
|
|
|
}
|