From 34496ced0ef23657bc4c99bbe31d50d917f4d1dc Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Sat, 11 Jun 2022 19:19:50 +0200 Subject: [PATCH] support for geograpghy view in mssql --- packages/sqltree/src/dumpSqlExpression.ts | 11 +++++++++-- packages/sqltree/src/evaluateExpression.ts | 3 +++ packages/sqltree/src/types.ts | 8 ++++++++ packages/tools/src/stringTools.ts | 12 +++--------- .../dbgate-plugin-mssql/src/frontend/driver.js | 17 +++++++++++++++++ 5 files changed, 40 insertions(+), 11 deletions(-) diff --git a/packages/sqltree/src/dumpSqlExpression.ts b/packages/sqltree/src/dumpSqlExpression.ts index 433bb740..7cd279a2 100644 --- a/packages/sqltree/src/dumpSqlExpression.ts +++ b/packages/sqltree/src/dumpSqlExpression.ts @@ -35,17 +35,24 @@ export function dumpSqlExpression(dmp: SqlDumper, expr: Expression) { dmp.put(')'); break; + case 'methodCall': + dumpSqlExpression(dmp, expr.thisObject) + dmp.put('.%s(', expr.method); + dmp.putCollection(',', expr.args, x => dumpSqlExpression(dmp, x)); + dmp.put(')'); + break; + case 'transform': dmp.transform(expr.transform, () => dumpSqlExpression(dmp, expr.expr)); break; case 'rowNumber': - dmp.put(" ^row_number() ^over (^order ^by "); + dmp.put(' ^row_number() ^over (^order ^by '); dmp.putCollection(', ', expr.orderBy, x => { dumpSqlExpression(dmp, x); dmp.put(' %k', x.direction); }); - dmp.put(")"); + dmp.put(')'); break; } } diff --git a/packages/sqltree/src/evaluateExpression.ts b/packages/sqltree/src/evaluateExpression.ts index 5be138ec..2f99943c 100644 --- a/packages/sqltree/src/evaluateExpression.ts +++ b/packages/sqltree/src/evaluateExpression.ts @@ -20,6 +20,9 @@ export function evaluateExpression(expr: Expression, values) { case 'call': return null; + case 'methodCall': + return null; + case 'transform': return null; } diff --git a/packages/sqltree/src/types.ts b/packages/sqltree/src/types.ts index 44f80b82..4f7996c2 100644 --- a/packages/sqltree/src/types.ts +++ b/packages/sqltree/src/types.ts @@ -155,6 +155,13 @@ export interface CallExpression { argsPrefix?: string; // DISTINCT in case of COUNT DISTINCT } +export interface MethodCallExpression { + exprType: 'methodCall'; + method: string; + args: Expression[]; + thisObject: Expression; +} + export interface TranformExpression { exprType: 'transform'; expr: Expression; @@ -172,6 +179,7 @@ export type Expression = | PlaceholderExpression | RawExpression | CallExpression + | MethodCallExpression | TranformExpression | RowNumberExpression; export type OrderByExpression = Expression & { direction: 'ASC' | 'DESC' }; diff --git a/packages/tools/src/stringTools.ts b/packages/tools/src/stringTools.ts index 9e3d2d0f..0b5f7bbf 100644 --- a/packages/tools/src/stringTools.ts +++ b/packages/tools/src/stringTools.ts @@ -88,14 +88,8 @@ export function getIconForRedisType(type) { export function isWktGeometry(s) { if (!_isString(s)) return false; - return ( - s.startsWith('POINT(') || - s.startsWith('LINESTRING(') || - s.startsWith('POLYGON(') || - s.startsWith('MULTIPOINT(') || - s.startsWith('MULTILINESTRING(') || - s.startsWith('MULTIPOLYGON(') || - s.startsWith('GEOMCOLLECTION(') || - s.startsWith('GEOMETRYCOLLECTION(') + // return !!s.match(/^POINT\s*\(|/) + return !!s.match( + /^POINT\s*\(|^LINESTRING\s*\(|^POLYGON\s*\(|^MULTIPOINT\s*\(|^MULTILINESTRING\s*\(|^MULTIPOLYGON\s*\(|^GEOMCOLLECTION\s*\(|^GEOMETRYCOLLECTION\s*\(/ ); } diff --git a/plugins/dbgate-plugin-mssql/src/frontend/driver.js b/plugins/dbgate-plugin-mssql/src/frontend/driver.js index 3acb4612..3f7c8b56 100644 --- a/plugins/dbgate-plugin-mssql/src/frontend/driver.js +++ b/plugins/dbgate-plugin-mssql/src/frontend/driver.js @@ -2,6 +2,8 @@ const { driverBase } = global.DBGATE_TOOLS; const MsSqlDumper = require('./MsSqlDumper'); const { mssqlSplitterOptions } = require('dbgate-query-splitter/lib/options'); +const spatialTypes = ['GEOGRAPHY']; + /** @type {import('dbgate-types').SqlDialect} */ const dialect = { limitSelect: true, @@ -70,6 +72,21 @@ const dialect = { 'image', 'xml', ], + + createColumnViewExpression(columnName, dataType, source, alias) { + if (dataType && spatialTypes.includes(dataType.toUpperCase())) { + return { + exprType: 'methodCall', + method: 'STAsText', + alias: alias || columnName, + thisObject: { + exprType: 'column', + columnName, + source, + }, + }; + } + }, }; /** @type {import('dbgate-types').EngineDriver} */