2021-11-04 15:54:35 +01:00
|
|
|
import {
|
|
|
|
Integration,
|
|
|
|
DatasourceFieldTypes,
|
|
|
|
QueryTypes,
|
|
|
|
SqlQuery,
|
|
|
|
} from "../definitions/datasource"
|
|
|
|
import { Table } from "../definitions/common"
|
|
|
|
import { getSqlQuery } from "./utils"
|
2021-11-08 23:08:47 +01:00
|
|
|
import oracledb, { ExecuteOptions, Result } from "oracledb"
|
|
|
|
import { Connection, ConnectionAttributes } from "oracledb"
|
2021-11-05 14:56:54 +01:00
|
|
|
import Sql from "./base/sql"
|
2021-11-04 15:54:35 +01:00
|
|
|
module OracleModule {
|
2021-11-05 14:56:54 +01:00
|
|
|
|
|
|
|
oracledb.outFormat = oracledb.OUT_FORMAT_OBJECT;
|
2021-11-04 15:54:35 +01:00
|
|
|
interface OracleConfig {
|
2021-11-05 14:56:54 +01:00
|
|
|
host: string
|
|
|
|
port: number
|
|
|
|
database: string
|
|
|
|
user: string
|
|
|
|
password: string
|
|
|
|
// ssl?: boolean
|
|
|
|
// ca?: string
|
|
|
|
// rejectUnauthorized?: boolean
|
2021-11-04 15:54:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const SCHEMA: Integration = {
|
2021-11-08 23:08:47 +01:00
|
|
|
docs: "https://github.com/oracle/node-oracledb",
|
2021-11-04 15:54:35 +01:00
|
|
|
friendlyName: "Oracle",
|
2021-11-08 23:08:47 +01:00
|
|
|
description: "Oracle Database is an object-relational database management system developed by Oracle Corporation",
|
2021-11-04 15:54:35 +01:00
|
|
|
datasource: {
|
2021-11-05 14:56:54 +01:00
|
|
|
host: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
default: "localhost",
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
port: {
|
|
|
|
type: DatasourceFieldTypes.NUMBER,
|
|
|
|
required: true,
|
|
|
|
default: 1521,
|
|
|
|
},
|
|
|
|
database: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
user: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
password: {
|
|
|
|
type: DatasourceFieldTypes.PASSWORD,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
// ssl: {
|
|
|
|
// type: DatasourceFieldTypes.BOOLEAN,
|
|
|
|
// default: false,
|
|
|
|
// required: false,
|
|
|
|
// },
|
|
|
|
// rejectUnauthorized: {
|
|
|
|
// type: DatasourceFieldTypes.BOOLEAN,
|
|
|
|
// default: false,
|
|
|
|
// required: false,
|
|
|
|
// },
|
|
|
|
// ca: {
|
|
|
|
// type: DatasourceFieldTypes.LONGFORM,
|
|
|
|
// default: false,
|
|
|
|
// required: false,
|
|
|
|
// },
|
2021-11-04 15:54:35 +01:00
|
|
|
},
|
|
|
|
query: {
|
2021-11-05 14:56:54 +01:00
|
|
|
create: {
|
|
|
|
type: QueryTypes.SQL,
|
|
|
|
},
|
|
|
|
read: {
|
|
|
|
type: QueryTypes.SQL,
|
|
|
|
},
|
|
|
|
update: {
|
|
|
|
type: QueryTypes.SQL,
|
|
|
|
},
|
|
|
|
delete: {
|
|
|
|
type: QueryTypes.SQL,
|
|
|
|
},
|
2021-11-04 15:54:35 +01:00
|
|
|
},
|
|
|
|
}
|
2021-11-08 23:08:47 +01:00
|
|
|
class OracleIntegration extends Sql {
|
2021-11-04 15:54:35 +01:00
|
|
|
private readonly config: OracleConfig
|
|
|
|
public tables: Record<string, Table> = {}
|
|
|
|
public schemaErrors: Record<string, string> = {}
|
|
|
|
|
|
|
|
constructor(config: OracleConfig) {
|
|
|
|
super("oracle")
|
2021-11-05 14:56:54 +01:00
|
|
|
this.config = config
|
|
|
|
}
|
|
|
|
|
2021-11-08 23:08:47 +01:00
|
|
|
private query = async (query: SqlQuery): Promise<Result<any>> => {
|
|
|
|
let connection
|
|
|
|
try {
|
|
|
|
connection = await this.getConnection()
|
|
|
|
|
|
|
|
const options : ExecuteOptions = { autoCommit: true }
|
|
|
|
const result: Result<any> = await connection.execute(query.sql, [], options)
|
|
|
|
|
|
|
|
return result
|
|
|
|
} finally {
|
|
|
|
if (connection) {
|
|
|
|
try {
|
|
|
|
await connection.close();
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private getConnection = async (): Promise<Connection> => {
|
2021-11-05 14:56:54 +01:00
|
|
|
//connectString : "(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))(CONNECT_DATA =(SID= ORCL)))"
|
|
|
|
const connectString = `${this.config.host}:${this.config.port || 1521}/${this.config.database}`
|
2021-11-08 23:08:47 +01:00
|
|
|
const attributes: ConnectionAttributes = {
|
2021-11-05 14:56:54 +01:00
|
|
|
user: this.config.user,
|
|
|
|
password: this.config.user,
|
2021-11-08 23:08:47 +01:00
|
|
|
connectString,
|
2021-11-05 14:56:54 +01:00
|
|
|
}
|
2021-11-08 23:08:47 +01:00
|
|
|
return oracledb.getConnection(attributes);
|
2021-11-04 15:54:35 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 23:08:47 +01:00
|
|
|
async create(query: SqlQuery | string) {
|
|
|
|
const response = await this.query(getSqlQuery(query))
|
|
|
|
return response.rows && response.rows.length ? response.rows : [{ created: true }]
|
2021-11-04 15:54:35 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 23:08:47 +01:00
|
|
|
async read(query: SqlQuery | string) {
|
|
|
|
const response = await this.query(getSqlQuery(query))
|
|
|
|
return response.rows
|
|
|
|
}
|
2021-11-04 15:54:35 +01:00
|
|
|
|
2021-11-08 23:08:47 +01:00
|
|
|
async update(query: SqlQuery | string) {
|
|
|
|
const response = await this.query(getSqlQuery(query))
|
|
|
|
return response.rows && response.rows.length ? response.rows : [{ updated: true }]
|
|
|
|
}
|
2021-11-04 15:54:35 +01:00
|
|
|
|
2021-11-08 23:08:47 +01:00
|
|
|
async delete(query: SqlQuery | string) {
|
|
|
|
const response = await this.query(getSqlQuery(query))
|
|
|
|
return response.rows && response.rows.length ? response.rows : [{ deleted: true }]
|
2021-11-04 15:54:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
schema: SCHEMA,
|
|
|
|
integration: OracleIntegration,
|
|
|
|
}
|
|
|
|
}
|