2021-06-27 00:09:46 +02:00
|
|
|
import { SqlQuery } from "../definitions/datasource"
|
2021-06-16 17:27:33 +02:00
|
|
|
const { DocumentTypes, SEPARATOR } = require("../db/utils")
|
2021-06-18 13:29:25 +02:00
|
|
|
const { FieldTypes } = require("../constants")
|
2021-06-16 17:27:33 +02:00
|
|
|
|
2021-06-17 15:42:30 +02:00
|
|
|
const DOUBLE_SEPARATOR = `${SEPARATOR}${SEPARATOR}`
|
|
|
|
|
2021-06-25 14:46:02 +02:00
|
|
|
export function isExternalTable(tableId: string) {
|
2021-06-16 17:27:33 +02:00
|
|
|
return tableId.includes(DocumentTypes.DATASOURCE)
|
|
|
|
}
|
|
|
|
|
2021-06-25 14:46:02 +02:00
|
|
|
export function buildExternalTableId(datasourceId: string, tableName: string) {
|
2021-06-17 15:42:30 +02:00
|
|
|
return `${datasourceId}${DOUBLE_SEPARATOR}${tableName}`
|
2021-06-16 17:27:33 +02:00
|
|
|
}
|
|
|
|
|
2021-06-25 14:46:02 +02:00
|
|
|
export function breakExternalTableId(tableId: string) {
|
2021-06-17 15:42:30 +02:00
|
|
|
const parts = tableId.split(DOUBLE_SEPARATOR)
|
2021-06-16 17:27:33 +02:00
|
|
|
let tableName = parts.pop()
|
2021-06-17 15:42:30 +02:00
|
|
|
// if they need joined
|
|
|
|
let datasourceId = parts.join(DOUBLE_SEPARATOR)
|
2021-06-16 17:27:33 +02:00
|
|
|
return { datasourceId, tableName }
|
|
|
|
}
|
2021-06-16 19:38:00 +02:00
|
|
|
|
2021-06-25 14:46:02 +02:00
|
|
|
export function generateRowIdField(keyProps: any[] = []) {
|
2021-06-16 19:38:00 +02:00
|
|
|
if (!Array.isArray(keyProps)) {
|
|
|
|
keyProps = [keyProps]
|
|
|
|
}
|
|
|
|
// this conserves order and types
|
2021-06-24 00:35:53 +02:00
|
|
|
return encodeURIComponent(JSON.stringify(keyProps).replace(/"/g, "'"))
|
2021-06-16 19:38:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// should always return an array
|
2021-06-25 14:46:02 +02:00
|
|
|
export function breakRowIdField(_id: string) {
|
2021-07-01 19:23:15 +02:00
|
|
|
const decoded = decodeURIComponent(_id)
|
|
|
|
const parsed = JSON.parse(decoded)
|
|
|
|
return Array.isArray(parsed) ? parsed : [parsed]
|
2021-06-16 19:38:00 +02:00
|
|
|
}
|
2021-06-18 13:29:25 +02:00
|
|
|
|
2021-06-25 14:46:02 +02:00
|
|
|
export function convertType(type: string, map: { [key: string]: any }) {
|
2021-06-18 13:29:25 +02:00
|
|
|
for (let [external, internal] of Object.entries(map)) {
|
|
|
|
if (type.toLowerCase().includes(external)) {
|
|
|
|
return internal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FieldTypes.STRING
|
|
|
|
}
|
2021-06-25 14:46:02 +02:00
|
|
|
|
|
|
|
export function getSqlQuery(query: SqlQuery | string): SqlQuery {
|
|
|
|
if (typeof query === "string") {
|
|
|
|
return { sql: query }
|
|
|
|
} else {
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
}
|