117 lines
5.2 KiB
TypeScript
117 lines
5.2 KiB
TypeScript
import { Component, inject, signal } from '@angular/core';
|
|
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MAT_DIALOG_DATA, MatDialogModule, MatDialogRef } from '@angular/material/dialog';
|
|
import { MatFormFieldModule } from '@angular/material/form-field';
|
|
import { MatIconModule } from '@angular/material/icon';
|
|
import { MatInputModule } from '@angular/material/input';
|
|
import { MatProgressBarModule } from '@angular/material/progress-bar';
|
|
import { MatTableModule } from '@angular/material/table';
|
|
import { PreviewResponse, apiErrorMessage } from '../../api/api.types';
|
|
import { SourceGateway } from '../../api/source-gateway';
|
|
import { SqlConnection } from '../../domain/sql-source';
|
|
|
|
export interface SqlQueryResult {
|
|
name: string;
|
|
query: string;
|
|
}
|
|
|
|
/**
|
|
* Custom SELECT against a database connection (joins, filters, computed
|
|
* columns), added to the connection as one more table. "Probar" runs it
|
|
* (read-only, first rows only) before it's accepted.
|
|
*/
|
|
@Component({
|
|
selector: 'app-sql-query-dialog',
|
|
imports: [MatButtonModule, MatDialogModule, MatFormFieldModule, MatIconModule, MatInputModule, MatProgressBarModule, MatTableModule, ReactiveFormsModule],
|
|
template: `
|
|
<h2 mat-dialog-title>Consulta SQL · {{ connection.label }}</h2>
|
|
<mat-dialog-content>
|
|
<form [formGroup]="form" class="query-form">
|
|
<mat-form-field appearance="outline">
|
|
<mat-label>Nombre</mat-label>
|
|
<input matInput formControlName="name" />
|
|
</mat-form-field>
|
|
<mat-form-field appearance="outline" class="query-form__sql">
|
|
<mat-label>SELECT</mat-label>
|
|
<textarea matInput formControlName="query" rows="8" spellcheck="false"></textarea>
|
|
<mat-hint>Una sola sentencia SELECT (o WITH … SELECT). La conexión es de solo lectura.</mat-hint>
|
|
</mat-form-field>
|
|
</form>
|
|
@if (running()) {
|
|
<mat-progress-bar mode="indeterminate" aria-label="Ejecutando consulta"></mat-progress-bar>
|
|
}
|
|
@if (error(); as message) {
|
|
<p class="query-error" role="alert"><mat-icon aria-hidden="true">error</mat-icon>{{ message }}</p>
|
|
}
|
|
@if (preview(); as result) {
|
|
<p class="query-summary">{{ result.total_rows }} filas · {{ result.columns.length }} columnas</p>
|
|
<div class="query-preview">
|
|
<table mat-table [dataSource]="result.sample_rows.slice(0, 5)">
|
|
@for (column of result.columns; track column.name) {
|
|
<ng-container [matColumnDef]="column.name">
|
|
<th mat-header-cell *matHeaderCellDef>{{ column.name }}</th>
|
|
<td mat-cell *matCellDef="let row">{{ row[column.name] }}</td>
|
|
</ng-container>
|
|
}
|
|
<tr mat-header-row *matHeaderRowDef="columnNames()"></tr>
|
|
<tr mat-row *matRowDef="let row; columns: columnNames()"></tr>
|
|
</table>
|
|
</div>
|
|
}
|
|
</mat-dialog-content>
|
|
<mat-dialog-actions align="end">
|
|
<button mat-button mat-dialog-close type="button">Cancelar</button>
|
|
<button mat-stroked-button type="button" [disabled]="form.invalid || running()" (click)="run()">
|
|
<mat-icon aria-hidden="true">play_arrow</mat-icon>Probar
|
|
</button>
|
|
<button mat-flat-button type="button" [disabled]="form.invalid || !preview()" (click)="accept()">Agregar</button>
|
|
</mat-dialog-actions>
|
|
`,
|
|
styles: `
|
|
.query-form { display: grid; gap: 0; min-width: min(640px, 80vw); }
|
|
.query-form__sql textarea { font-family: 'Roboto Mono', ui-monospace, monospace; font-size: 13px; }
|
|
.query-error { align-items: center; color: var(--mat-sys-error); display: flex; gap: 8px; }
|
|
.query-summary { color: var(--mat-sys-on-surface-variant); font: var(--mat-sys-body-small); }
|
|
.query-preview { border: 1px solid var(--mat-sys-outline-variant); border-radius: 8px; max-width: 80vw; overflow: auto; }
|
|
`,
|
|
})
|
|
export class SqlQueryDialog {
|
|
readonly connection = inject<SqlConnection>(MAT_DIALOG_DATA);
|
|
private readonly dialogRef = inject<MatDialogRef<SqlQueryDialog, SqlQueryResult>>(MatDialogRef);
|
|
private readonly gateway = inject(SourceGateway);
|
|
|
|
readonly form = inject(FormBuilder).nonNullable.group({
|
|
name: [`Consulta ${this.connection.tables().filter((t) => t.removable).length + 1}`, Validators.required],
|
|
query: ['SELECT * FROM ', Validators.required],
|
|
});
|
|
readonly running = signal(false);
|
|
readonly error = signal<string | null>(null);
|
|
readonly preview = signal<PreviewResponse | null>(null);
|
|
readonly columnNames = signal<string[]>([]);
|
|
|
|
constructor() {
|
|
this.form.controls.query.valueChanges.subscribe(() => this.preview.set(null));
|
|
}
|
|
|
|
run(): void {
|
|
this.running.set(true);
|
|
this.error.set(null);
|
|
this.gateway.sql.preview(this.connection.ref, { query: this.form.getRawValue().query }, null).subscribe({
|
|
next: (preview) => {
|
|
this.running.set(false);
|
|
this.preview.set(preview);
|
|
this.columnNames.set(preview.columns.map((column) => column.name));
|
|
},
|
|
error: (error) => {
|
|
this.running.set(false);
|
|
this.error.set(apiErrorMessage(error, 'La consulta falló.'));
|
|
},
|
|
});
|
|
}
|
|
|
|
accept(): void {
|
|
this.dialogRef.close(this.form.getRawValue());
|
|
}
|
|
}
|