Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support views of HANA #347

Merged
merged 2 commits into from Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions packages/core/dialect/saphana/index.ts
Expand Up @@ -153,7 +153,7 @@ export default class SAPHana extends GenericDialect<HanaConnection> implements C
}

public getTables(): Promise<DatabaseInterface.Table[]> {
return this.query(this.queries.fetchTables, [this.schema])
return this.query(this.queries.fetchTables, [this.schema, this.schema])
.then(([queryRes]) => {
return queryRes.results
.reduce((prev, curr) => prev.concat(curr), [])
Expand All @@ -172,7 +172,7 @@ export default class SAPHana extends GenericDialect<HanaConnection> implements C
}

public getColumns(): Promise<DatabaseInterface.TableColumn[]> {
return this.query(this.queries.fetchColumns, [this.schema])
return this.query(this.queries.fetchColumns, [this.schema, this.schema])
.then(([queryRes]) => {
return queryRes.results
.reduce((prev, curr) => prev.concat(curr), [])
Expand Down Expand Up @@ -200,6 +200,15 @@ export default class SAPHana extends GenericDialect<HanaConnection> implements C
}

public describeTable(prefixedTable: string) {
return this.query(this.queries.describeTable, [this.schema, prefixedTable]);
return new Promise(resolve => {
this.query(this.queries.describeTable, [this.schema, prefixedTable]).then(queryRes => {
if (queryRes[0].results.length == 0) {
this.query(this.queries.describeView, [this.schema, prefixedTable]).then(res => resolve(res));
} else {
resolve(queryRes);
}
})
});
}

}
70 changes: 65 additions & 5 deletions packages/core/dialect/saphana/queries.ts
Expand Up @@ -8,6 +8,12 @@ export default {
TABLE_COLUMNS C
WHERE
C.SCHEMA_NAME = ? and C.TABLE_NAME = ?`,
describeView: `
SELECT *
FROM
VIEW_COLUMNS C
WHERE
C.SCHEMA_NAME = ? and C.VIEW_NAME = ?`,
fetchColumns: `
SELECT
C.TABLE_NAME AS tableName,
Expand All @@ -19,9 +25,9 @@ SELECT
'' as dbName,
C.DEFAULT_VALUE as defaultValue,
C.IS_NULLABLE as isNullable,
'table' as constraintType,
'Table' as constraintType,
CONCAT( CONCAT( CONCAT(
'tables${TREE_SEP}',
'Tables${TREE_SEP}',
C.TABLE_NAME),
'${TREE_SEP}'),
C.COLUMN_NAME)
Expand All @@ -30,11 +36,65 @@ FROM
TABLE_COLUMNS C
WHERE
C.SCHEMA_NAME = ?
ORDER BY
C.TABLE_NAME`,
UNION
SELECT
D.VIEW_NAME AS tableName,
D.COLUMN_NAME AS columnName,
D.CS_DATA_TYPE_NAME AS type,
D.LENGTH AS size,
'' as tableSchema,
'' AS tableCatalog,
'' as dbName,
D.DEFAULT_VALUE as defaultValue,
D.IS_NULLABLE as isNullable,
'View' as constraintType,
CONCAT( CONCAT( CONCAT(
'Views${TREE_SEP}',
D.VIEW_NAME),
'${TREE_SEP}'),
D.COLUMN_NAME)
AS tree
FROM
VIEW_COLUMNS D
WHERE
D.SCHEMA_NAME = ?`,

fetchRecords: 'SELECT TOP :limit * FROM :table',
fetchTables: `

SELECT
A.TABLE_NAME AS tableName,
'' AS tableSchema,
'' AS tableCatalog,
0 AS isView,
'' AS dbName,
COUNT(1) AS numberOfColumns,
CONCAT('Tables${TREE_SEP}', A.TABLE_NAME) AS tree
FROM
TABLE_COLUMNS A
WHERE
A.SCHEMA_NAME = ?
GROUP BY
A.TABLE_NAME
UNION
SELECT
B.VIEW_NAME AS tableName,
'' AS tableSchema,
'' AS tableCatalog,
1 AS isView,
'' AS dbName,
COUNT(1) AS numberOfColumns,
CONCAT('Views${TREE_SEP}', B.VIEW_NAME) AS tree
FROM
VIEW_COLUMNS B
WHERE
B.SCHEMA_NAME = ?
GROUP BY
B.VIEW_NAME

`

/* `
SELECT
A.TABLE_NAME AS tableName,
'' AS tableSchema,
Expand All @@ -51,5 +111,5 @@ FROM
WHERE
A.SCHEMA_NAME = ?
GROUP BY
A.TABLE_NAME`
A.TABLE_NAME`*/
} as DialectQueries;