Skip to content

Commit

Permalink
b
Browse files Browse the repository at this point in the history
  • Loading branch information
polterguy committed Dec 20, 2023
1 parent b0cb52f commit 034b8e9
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 85 deletions.
131 changes: 63 additions & 68 deletions backend/files/system/sql/databases.get.hl
Original file line number Diff line number Diff line change
Expand Up @@ -77,90 +77,85 @@ cache.try-get:x:@strings.concat

// Iterating through databases.
for-each:x:@.result/*
try

// Retrieving tables.
// Retrieving tables.
strings.concat
.:magic.db.
get-value:x:@.databaseType
.:.tables
unwrap:x:+/*
signal:x:@strings.concat
connection-string:x:@.connection-string
database:x:@.dp/#/*/name
for-each:x:@signal/*

// Retrieving columns.
unwrap:x:+/*/*/*
add:x:@for-each/@.dp/#/*/tables
.
.
name:x:@.dp/#/*/table
columns
strings.concat
.:magic.db.
get-value:x:@.databaseType
.:.tables
.:.columns
unwrap:x:+/*
signal:x:@strings.concat
connection-string:x:@.connection-string
database:x:@.dp/#/*/name
database:x:@for-each/@.dp/#/*/name
table:x:@.dp/#/*/table
for-each:x:@signal/*

// Retrieving columns.
unwrap:x:+/*/*/*
add:x:@for-each/@.dp/#/*/tables
add:x:@for-each/@for-each/@.dp/#/*/tables/0/-/*/columns
.
.
name:x:@.dp/#/*/table
columns
strings.concat
.:magic.db.
get-value:x:@.databaseType
.:.columns
unwrap:x:+/*
signal:x:@strings.concat
connection-string:x:@.connection-string
database:x:@for-each/@.dp/#/*/name
table:x:@.dp/#/*/table
for-each:x:@signal/*
unwrap:x:+/*/*/*
add:x:@for-each/@for-each/@.dp/#/*/tables/0/-/*/columns
.
.
name:x:@.dp/#/*/name
db:x:@.dp/#/*/db
nullable:x:@.dp/#/*/nullable
primary:x:@.dp/#/*/primary
automatic:x:@.dp/#/*/automatic
hl:x:@.dp/#/*/hl

// Retrieving foreign keys.
add:x:@for-each/@for-each/@.dp/#/*/tables/0/-
name:x:@.dp/#/*/name
db:x:@.dp/#/*/db
nullable:x:@.dp/#/*/nullable
primary:x:@.dp/#/*/primary
automatic:x:@.dp/#/*/automatic
hl:x:@.dp/#/*/hl

// Retrieving foreign keys.
add:x:@for-each/@for-each/@.dp/#/*/tables/0/-
.
foreign_keys
strings.concat
.:magic.db.
get-value:x:@.databaseType
.:.foreign_keys
unwrap:x:+/*
signal:x:@strings.concat
connection-string:x:@.connection-string
database:x:@for-each/@for-each/@.dp/#/*/name
table:x:@.dp/#/*/table
for-each:x:@signal/*
add:x:+/*/*
get-nodes:x:@.dp/#/*
add:x:@for-each/@for-each/@for-each/@.dp/#/*/tables/0/-/*/foreign_keys
.
foreign_keys
strings.concat
.:magic.db.
get-value:x:@.databaseType
.:.foreign_keys
unwrap:x:+/*
signal:x:@strings.concat
connection-string:x:@.connection-string
database:x:@for-each/@for-each/@.dp/#/*/name
table:x:@.dp/#/*/table
for-each:x:@signal/*
add:x:+/*/*
get-nodes:x:@.dp/#/*
add:x:@for-each/@for-each/@for-each/@.dp/#/*/tables/0/-/*/foreign_keys
.
.

// Retrieving indexes.
add:x:@for-each/@for-each/@for-each/@.dp/#/*/tables/0/-
// Retrieving indexes.
add:x:@for-each/@for-each/@for-each/@.dp/#/*/tables/0/-
.
indexes
strings.concat
.:magic.db.
get-value:x:@.databaseType
.:.indexes
unwrap:x:+/*
signal:x:@strings.concat
connection-string:x:@.connection-string
database:x:@for-each/@for-each/@for-each/@.dp/#/*/name
table:x:@.dp/#/*/table
for-each:x:@signal/*
add:x:+/*/*
get-nodes:x:@.dp/#/*
add:x:@for-each/@for-each/@for-each/@for-each/@.dp/#/*/tables/0/-/*/indexes
.
indexes
strings.concat
.:magic.db.
get-value:x:@.databaseType
.:.indexes
unwrap:x:+/*
signal:x:@strings.concat
connection-string:x:@.connection-string
database:x:@for-each/@for-each/@for-each/@.dp/#/*/name
table:x:@.dp/#/*/table
for-each:x:@signal/*
add:x:+/*/*
get-nodes:x:@.dp/#/*
add:x:@for-each/@for-each/@for-each/@for-each/@.dp/#/*/tables/0/-/*/indexes
.
.
.catch

// Silently ignoring, after logging, probably don't have access to database.
log.info:x:@.arguments/*/message

// Returning database to caller.
lambda2hyper:x:@.result/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { ChangeDetectorRef, Component, OnInit, ViewChild } from '@angular/core';
import { CodemirrorComponent } from '@ctrl/ngx-codemirror';
import { FieldType, FieldTypeConfig } from '@ngx-formly/core';
import { debounceTime, distinctUntilChanged, tap } from 'rxjs';
import { Databases } from 'src/app/models/databases.model';
import { CodemirrorActionsService } from 'src/app/services/codemirror-actions.service';
import { GeneralService } from 'src/app/services/general.service';
Expand All @@ -17,7 +18,7 @@ import { SqlService } from 'src/app/services/sql.service';
*/
@Component({
selector: 'app-codemirror-formly',
template: `<ngx-codemirror #editor class="sql-formly-editor" *ngIf="cmOptions" [options]="cmOptions" [(ngModel)]="model[field.key]"></ngx-codemirror>`,
template: `<mat-icon [matTooltip]="connected ? 'You have a valid database connection' : 'You do not have a valid database connection'" [class]="connected ? 'connected' : 'disconnected'">{{connected ? 'check_circle' : 'report_problem'}}</mat-icon><ngx-codemirror #editor class="sql-formly-editor" *ngIf="cmOptions" [options]="cmOptions" [(ngModel)]="model[field.key]"></ngx-codemirror>`,
styleUrls: ['./codemirror-sql-formly.scss']
})
export class CodemirrorSqlFormlyComponent extends FieldType<FieldTypeConfig> implements OnInit {
Expand All @@ -26,6 +27,7 @@ export class CodemirrorSqlFormlyComponent extends FieldType<FieldTypeConfig> imp
@ViewChild('editor') private editor: CodemirrorComponent;
cmOptions: any = null;
ready: boolean = false;
connected: boolean = false;

constructor(
private sqlService: SqlService,
Expand Down Expand Up @@ -72,16 +74,18 @@ export class CodemirrorSqlFormlyComponent extends FieldType<FieldTypeConfig> imp
this.getDatabaseTables();
}
});
this.form.controls['connection-string'].valueChanges.subscribe({
next : () => {
this.getDatabaseTables();
}
});
this.form.controls['database'].valueChanges.subscribe({
next : () => {
this.databaseChanged();
}
});
this.form.controls['connection-string'].valueChanges.pipe(
debounceTime(400),
distinctUntilChanged(),
tap(() => {
this.getDatabaseTables();
})).subscribe();
this.form.controls['database'].valueChanges.pipe(
debounceTime(400),
distinctUntilChanged(),
tap(() => {
this.databaseChanged();
})).subscribe();
}
}, 1);
}, 250);
Expand Down Expand Up @@ -109,23 +113,33 @@ export class CodemirrorSqlFormlyComponent extends FieldType<FieldTypeConfig> imp

this.generalService.hideLoading();
this.databases = result;
this.connected = true;
this.cdn.detectChanges();
this.databaseChanged();
},

error: () => {

this.generalService.hideLoading();
this.generalService.showFeedback('Not a valid database-type/connection-string combination', 'errorMessage');
this.connected = false;
this.cdn.detectChanges();
}
});
}

private databaseChanged() {

let hintTables = (this.databases.databases || []).find((db: any) => db.name === this.model['database'])?.tables || [];
const hints = Object.fromEntries(hintTables.map((x: any) => [x.name, x.columns.map((y: any) => y.name)]));
this.cmOptions.hintOptions = {
tables: hints,
};
}
if (hintTables.length === 0) {
this.connected = false;
this.cdn.detectChanges();
} else {
const hints = Object.fromEntries(hintTables.map((x: any) => [x.name, x.columns.map((y: any) => y.name)]));
this.cmOptions.hintOptions = {
tables: hints,
};
this.connected = true;
this.cdn.detectChanges();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,17 @@
::ng-deep .sql-formly-editor .CodeMirror {
max-height: 150px;
}
mat-icon {
position: absolute;
z-index: 100000;
position: absolute;
right: -5px;
top: 18px;
font-size: 1rem;
}
mat-icon.connected {
color: #a0ffa0;
}
mat-icon.disconnected {
color: #ffa0a0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

::ng-deep app-codemirror-formly {
position: relative;
}

0 comments on commit 034b8e9

Please sign in to comment.