Skip to content

Commit 190fb86

Browse files
authored
Adding ORM definitions to schema designer (#21981)
1 parent 2383529 commit 190fb86

16 files changed

Lines changed: 2644 additions & 42 deletions

extensions/mssql/l10n/bundle.l10n.json

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,14 @@
705705
"Definition": "Definition",
706706
"Show Definition": "Show Definition",
707707
"Hide Definition": "Hide Definition",
708+
"Definition type": "Definition type",
709+
"T-SQL": "T-SQL",
710+
"Prisma": "Prisma",
711+
"Sequelize": "Sequelize",
712+
"TypeORM": "TypeORM",
713+
"Drizzle": "Drizzle",
714+
"SQLAlchemy": "SQLAlchemy",
715+
"EF Core": "EF Core",
708716
"Delete Confirmation": "Delete Confirmation",
709717
"Are you sure you want to delete the selected items?": "Are you sure you want to delete the selected items?",
710718
"Undo": "Undo",
@@ -2144,6 +2152,15 @@
21442152
"SQL Plan Files": "SQL Plan Files",
21452153
"Script copied to clipboard": "Script copied to clipboard",
21462154
"Copied": "Copied",
2155+
"Failed to open text in editor: {0}/{0} is the error message": {
2156+
"message": "Failed to open text in editor: {0}",
2157+
"comment": ["{0} is the error message"]
2158+
},
2159+
"Failed to copy text to clipboard: {0}/{0} is the error message": {
2160+
"message": "Failed to copy text to clipboard: {0}",
2161+
"comment": ["{0} is the error message"]
2162+
},
2163+
"Schema designer details are not available.": "Schema designer details are not available.",
21472164
"Copying results...": "Copying results...",
21482165
"Results copied to clipboard": "Results copied to clipboard",
21492166
"Do you want to always display query results in a new tab instead of the query pane?": "Do you want to always display query results in a new tab instead of the query pane?",
@@ -3374,10 +3391,6 @@
33743391
"message": "File: {0}",
33753392
"comment": ["{0} is the file name"]
33763393
},
3377-
"Failed to open text in editor: {0}/{0} is the error message": {
3378-
"message": "Failed to open text in editor: {0}",
3379-
"comment": ["{0} is the error message"]
3380-
},
33813394
"Profiler events exported successfully to {0}/{0} is the file path": {
33823395
"message": "Profiler events exported successfully to {0}",
33833396
"comment": ["{0} is the file path"]

extensions/mssql/src/constants/locConstants.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,19 @@ export let executionPlan = l10n.t("Execution Plan");
780780
export let executionPlanFileFilter = l10n.t("SQL Plan Files");
781781
export let scriptCopiedToClipboard = l10n.t("Script copied to clipboard");
782782
export let copied = l10n.t("Copied");
783+
export let failedToOpenTextInEditor = (errorMessage: string) =>
784+
l10n.t({
785+
message: "Failed to open text in editor: {0}",
786+
args: [errorMessage],
787+
comment: ["{0} is the error message"],
788+
});
789+
export let failedToCopyTextToClipboard = (errorMessage: string) =>
790+
l10n.t({
791+
message: "Failed to copy text to clipboard: {0}",
792+
args: [errorMessage],
793+
comment: ["{0} is the error message"],
794+
});
795+
export let schemaDesignerDetailsUnavailable = l10n.t("Schema designer details are not available.");
783796
export let copyingResults = l10n.t("Copying results...");
784797
export let resultsCopiedToClipboard = l10n.t("Results copied to clipboard");
785798

extensions/mssql/src/schemaDesigner/schemaDesignerWebviewController.ts

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import { DabService } from "../services/dabService";
2323
import { Dab } from "../sharedInterfaces/dab";
2424
import { CopilotChat } from "../sharedInterfaces/copilotChat";
2525
import { addMcpServerToWorkspace } from "../copilot/copilotUtils";
26+
import {
27+
getSchemaDesignerDefinitionOutput,
28+
SchemaDesignerDefinitionOutput,
29+
} from "../sharedInterfaces/schemaDesignerDefinitionOutput";
2630

2731
function isExpandCollapseButtonsEnabled(): boolean {
2832
return vscode.workspace
@@ -327,19 +331,39 @@ export class SchemaDesignerWebviewController extends WebviewPanelController<
327331
});
328332

329333
this.onNotification(SchemaDesigner.CopyToClipboardNotification.type, async (params) => {
330-
await vscode.env.clipboard.writeText(params.text);
331-
await vscode.window.showInformationMessage(LocConstants.scriptCopiedToClipboard);
334+
try {
335+
const definition = await this.createDefinitionOutput(params);
336+
await vscode.env.clipboard.writeText(definition.text);
337+
await vscode.window.showInformationMessage(LocConstants.copied);
338+
} catch (error) {
339+
await vscode.window.showErrorMessage(
340+
LocConstants.failedToCopyTextToClipboard(getErrorMessage(error)),
341+
);
342+
}
332343
});
333344

334-
this.onNotification(SchemaDesigner.OpenInEditorNotification.type, async () => {
335-
const definition = await this.schemaDesignerService.getDefinition({
336-
updatedSchema: this.schemaDesignerDetails!.schema,
337-
sessionId: this._sessionId,
338-
});
339-
await this.mainController.sqlDocumentService.newQuery({
340-
content: definition.script,
341-
connectionStrategy: ConnectionStrategy.DoNotConnect,
342-
});
345+
this.onNotification(SchemaDesigner.OpenInEditorNotification.type, async (params) => {
346+
try {
347+
const definition = await this.createDefinitionOutput(params);
348+
349+
if (definition.language === "sql") {
350+
await this.mainController.sqlDocumentService.newQuery({
351+
content: definition.text,
352+
connectionStrategy: ConnectionStrategy.DoNotConnect,
353+
});
354+
return;
355+
}
356+
357+
const document = await vscode.workspace.openTextDocument({
358+
content: definition.text,
359+
language: definition.language,
360+
});
361+
await vscode.window.showTextDocument(document, { preview: false });
362+
} catch (error) {
363+
await vscode.window.showErrorMessage(
364+
LocConstants.failedToOpenTextInEditor(getErrorMessage(error)),
365+
);
366+
}
343367
});
344368

345369
this.onNotification(SchemaDesigner.OpenInEditorWithConnectionNotification.type, () => {
@@ -578,6 +602,38 @@ export class SchemaDesignerWebviewController extends WebviewPanelController<
578602
});
579603
}
580604

605+
private async createDefinitionOutput(
606+
params?: SchemaDesigner.OpenInEditorOptions | SchemaDesigner.CopyToClipboardOptions,
607+
): Promise<SchemaDesignerDefinitionOutput> {
608+
if (params?.text !== undefined) {
609+
return {
610+
text: params.text,
611+
language: "language" in params ? (params.language ?? "sql") : "sql",
612+
};
613+
}
614+
615+
const updatedSchema = params?.updatedSchema ?? this.schemaDesignerDetails?.schema;
616+
if (!updatedSchema) {
617+
throw new Error(LocConstants.schemaDesignerDetailsUnavailable);
618+
}
619+
620+
this.updateCacheItem(updatedSchema);
621+
622+
const definitionKind = params?.definitionKind ?? SchemaDesigner.DefinitionKind.Sql;
623+
if (definitionKind === SchemaDesigner.DefinitionKind.Sql) {
624+
const definition = await this.schemaDesignerService.getDefinition({
625+
updatedSchema,
626+
sessionId: this._sessionId || this.schemaDesignerDetails?.sessionId || "",
627+
});
628+
return {
629+
text: definition.script,
630+
language: "sql",
631+
};
632+
}
633+
634+
return getSchemaDesignerDefinitionOutput(updatedSchema, definitionKind);
635+
}
636+
581637
private setupConfigurationListener() {
582638
const configChangeDisposable = vscode.workspace.onDidChangeConfiguration((e) => {
583639
if (e.affectsConfiguration(configSchemaDesignerEnableExpandCollapseButtons)) {
@@ -596,7 +652,19 @@ export class SchemaDesignerWebviewController extends WebviewPanelController<
596652
updatedSchema?: SchemaDesigner.Schema,
597653
isDirty?: boolean,
598654
): SchemaDesigner.SchemaDesignerCacheItem {
599-
let schemaDesignerCacheItem = this.schemaDesignerCache.get(this._key)!;
655+
let schemaDesignerCacheItem = this.schemaDesignerCache.get(this._key);
656+
if (!schemaDesignerCacheItem) {
657+
if (!this.schemaDesignerDetails) {
658+
throw new Error(LocConstants.schemaDesignerDetailsUnavailable);
659+
}
660+
661+
schemaDesignerCacheItem = {
662+
schemaDesignerDetails: this.schemaDesignerDetails,
663+
baselineSchema: this.baselineSchema ?? this.schemaDesignerDetails.schema,
664+
isDirty: false,
665+
};
666+
}
667+
this.schemaDesignerDetails ??= schemaDesignerCacheItem.schemaDesignerDetails;
600668
if (updatedSchema) {
601669
this.schemaDesignerDetails!.schema = updatedSchema;
602670
schemaDesignerCacheItem.schemaDesignerDetails.schema = updatedSchema;

extensions/mssql/src/sharedInterfaces/schemaDesigner.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ export namespace SchemaDesigner {
147147
Dab = "dab",
148148
}
149149

150+
export enum DefinitionKind {
151+
Sql = "sql",
152+
Prisma = "prisma",
153+
Sequelize = "sequelize",
154+
TypeOrm = "typeorm",
155+
Drizzle = "drizzle",
156+
SqlAlchemy = "sqlalchemy",
157+
EfCore = "efcore",
158+
}
159+
150160
/**
151161
* Schema designer model ready event
152162
* This event is sent when the schema designer model is ready
@@ -356,11 +366,16 @@ export namespace SchemaDesigner {
356366
}
357367

358368
export interface CopyToClipboardOptions {
359-
text: string;
369+
text?: string;
370+
updatedSchema?: Schema;
371+
definitionKind?: DefinitionKind;
360372
}
361373

362374
export interface OpenInEditorOptions {
363-
text: string;
375+
text?: string;
376+
language?: string;
377+
updatedSchema?: Schema;
378+
definitionKind?: DefinitionKind;
364379
}
365380

366381
export interface SchemaDesignerReducers {
@@ -399,10 +414,6 @@ export namespace SchemaDesigner {
399414
export namespace CloseSchemaDesignerNotification {
400415
export const type = new NotificationType<void>("closeDesigner");
401416
}
402-
export interface OpenInEditorParams {
403-
text: string;
404-
}
405-
406417
export namespace OpenInEditorWithConnectionNotification {
407418
export const type = new NotificationType<void>("openInEditorWithConnection");
408419
}
@@ -411,7 +422,7 @@ export namespace SchemaDesigner {
411422
}
412423

413424
export namespace CopyToClipboardNotification {
414-
export const type = new NotificationType<OpenInEditorParams>("copyToClipboard");
425+
export const type = new NotificationType<CopyToClipboardOptions>("copyToClipboard");
415426
}
416427

417428
export interface UpdatedSchemaParams {

0 commit comments

Comments
 (0)