109 lines
3.2 KiB
TypeScript
109 lines
3.2 KiB
TypeScript
import { Signal, signal } from '@angular/core';
|
|
import { Observable, map, switchMap, tap } from 'rxjs';
|
|
import { PreviewResponse, SourceLocator, SqlConnectionRef } from '../api/api.types';
|
|
import { SourceGateway } from '../api/source-gateway';
|
|
import { DataConnection, SourceTable } from './data-source';
|
|
|
|
/**
|
|
* A live database. Its tables are discovered from the server's catalog;
|
|
* the user can add custom SELECT queries as extra tables (a join, a
|
|
* filter, a computed column).
|
|
*
|
|
* The credentials travel with every request (the API is stateless) but
|
|
* are never shown: `label` is a credential-free display name.
|
|
*/
|
|
export class SqlConnection extends DataConnection {
|
|
readonly icon = 'storage';
|
|
private readonly _dialect = signal<string | null>(null);
|
|
readonly dialect: Signal<string | null> = this._dialect.asReadonly();
|
|
|
|
constructor(
|
|
readonly ref: SqlConnectionRef,
|
|
displayName: string,
|
|
readonly schema?: string,
|
|
) {
|
|
super(displayName);
|
|
}
|
|
|
|
get typeLabel(): string {
|
|
return SQL_DIALECT_LABELS[this._dialect() ?? ''] ?? 'Base de datos';
|
|
}
|
|
|
|
addQuery(name: string, query: string): SqlQueryTable {
|
|
const table = new SqlQueryTable(this, name, query);
|
|
this.addTable(table);
|
|
return table;
|
|
}
|
|
|
|
protected discover(gateway: SourceGateway): Observable<SourceTable[]> {
|
|
return gateway.sql.testConnection(this.ref).pipe(
|
|
tap((response) => this._dialect.set(response.dialect)),
|
|
switchMap(() => gateway.sql.tables(this.ref, this.schema)),
|
|
map((tables) => {
|
|
const queries = this.tables().filter((table) => table instanceof SqlQueryTable);
|
|
return [...tables.map((name) => new SqlTable(this, name)), ...queries];
|
|
}),
|
|
);
|
|
}
|
|
}
|
|
|
|
const SQL_DIALECT_LABELS: Record<string, string> = {
|
|
mysql: 'MySQL',
|
|
mariadb: 'MariaDB',
|
|
postgresql: 'PostgreSQL',
|
|
mssql: 'SQL Server',
|
|
sqlite: 'SQLite',
|
|
};
|
|
|
|
export class SqlTable extends SourceTable {
|
|
readonly icon = 'table_rows';
|
|
readonly kindLabel = 'Tabla';
|
|
|
|
constructor(
|
|
override readonly connection: SqlConnection,
|
|
name: string,
|
|
) {
|
|
super(connection, name);
|
|
}
|
|
|
|
preview(gateway: SourceGateway, target: string | null): Observable<PreviewResponse> {
|
|
return gateway.sql.preview(this.connection.ref, { table: this.name, schema: this.connection.schema }, target);
|
|
}
|
|
|
|
async locator(): Promise<SourceLocator> {
|
|
return {
|
|
source_type: 'sql',
|
|
payload: {
|
|
...this.connection.ref,
|
|
table: this.name,
|
|
...(this.connection.schema ? { schema: this.connection.schema } : {}),
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
export class SqlQueryTable extends SourceTable {
|
|
readonly icon = 'manage_search';
|
|
readonly kindLabel = 'Consulta SQL';
|
|
|
|
constructor(
|
|
override readonly connection: SqlConnection,
|
|
name: string,
|
|
readonly query: string,
|
|
) {
|
|
super(connection, name);
|
|
}
|
|
|
|
override get removable(): boolean {
|
|
return true;
|
|
}
|
|
|
|
preview(gateway: SourceGateway, target: string | null): Observable<PreviewResponse> {
|
|
return gateway.sql.preview(this.connection.ref, { query: this.query }, target);
|
|
}
|
|
|
|
async locator(): Promise<SourceLocator> {
|
|
return { source_type: 'sql', payload: { ...this.connection.ref, query: this.query } };
|
|
}
|
|
}
|