Skip to content

Commit

Permalink
Merge branch 'main' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
joacoc committed Dec 8, 2023
2 parents 932af5f + ccb98ae commit 5710915
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 78 deletions.
4 changes: 2 additions & 2 deletions src/clients/lsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ export default class LspClient {
documentSelector: [{ scheme: "file", language: "mzsql"}],
initializationOptions: {
formattingWidth,
schema: this.schema,
// schema: this.schema,
}
};

Expand Down Expand Up @@ -364,7 +364,7 @@ export default class LspClient {
command: "optionsUpdate",
arguments: [{
formattingWidth: this.getFormattingWidth(),
schema: this.schema,
// schema: this.schema,
}]}) as ExecuteCommandParseResponse;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/context/asyncContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export default class AsyncContext extends Context {
this.clients = {
...this.clients,
admin: adminClient,
cloud: new CloudClient(adminClient, profile["cloud-endpoint"])
cloud: new CloudClient(adminClient, this.config.getCloudEndpoint())
};

await this.loadEnvironment(init);
Expand Down
28 changes: 25 additions & 3 deletions src/context/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,16 +354,38 @@ export class Config {
}
}

/**
* @returns a clean profile cloud endpoint.
*/
getCloudEndpoint(): string | undefined {
return this.profile && this.cleanEndpoint(this.profile["cloud-endpoint"]);
}

/**
* Removes unwanted characters from the endpoint URLs.
* @param endpoint
* @returns the clean endpoint.
*/
cleanEndpoint(endpoint: string | undefined): string | undefined {
if (endpoint) {
const trimmedEndpoint = endpoint.trim();
return trimmedEndpoint.endsWith("/") ? trimmedEndpoint.substring(0, trimmedEndpoint.length - 1) : trimmedEndpoint
}
}

/**
* @returns the admin endpoint of the current profile
*/
getAdminEndpoint(): string | undefined {
if (this.profile) {
const cloudEndpoint = this.getCloudEndpoint();
if (this.profile["admin-endpoint"]) {
return this.profile["admin-endpoint"];
} else if (this.profile["cloud-endpoint"]) {
const cloudUrl = new URL(this.profile["cloud-endpoint"]);
return this.cleanEndpoint(this.profile["admin-endpoint"]);
} else if (cloudEndpoint) {
const cloudUrl = new URL(cloudEndpoint);
console.log("[Config]", "After endpoint: ", cloudUrl.toString());
const { hostname } = cloudUrl;

if (hostname.startsWith("api.")) {
cloudUrl.hostname = "admin." + hostname.slice(4);
return cloudUrl.toString();
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function activate(vsContext: vscode.ExtensionContext) {

// Focus the query results panel.
const sqlCommand = buildRunSQLCommand(context);
vscode.commands.executeCommand('queryResults.focus').then(sqlCommand);
sqlCommand()
});

const copyDisposable = vscode.commands.registerCommand('materialize.copy', async ({ tooltip }) => {
Expand Down
140 changes: 69 additions & 71 deletions src/providers/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,91 +23,89 @@ export const buildRunSQLCommand = (context: AsyncContext) => {
}

// Focus the query results panel.
vscode.commands.executeCommand('queryResults.focus').then(async () => {
const document = activeEditor.document;
const selection = activeEditor.selection;
const textSelected = activeEditor.document.getText(selection).trim();
const query = textSelected ? textSelected : document.getText();
const document = activeEditor.document;
const selection = activeEditor.selection;
const textSelected = activeEditor.document.getText(selection).trim();
const query = textSelected ? textSelected : document.getText();

// Identify the query to not overlap results.
// When a user press many times the run query button
// the results from one query can overlap the results
// from another. We only want to display the last results.
const id = randomUUID();
// Identify the query to not overlap results.
// When a user press many times the run query button
// the results from one query can overlap the results
// from another. We only want to display the last results.
const id = randomUUID();
try {
resultsProvider.setQueryId(id);
try {
resultsProvider.setQueryId(id);
try {
const statements = await context.parseSql(query);
console.log("[RunSQLCommand]", "Running statements: ", statements);
const lastStatement = statements[statements.length - 1];
const statements = await context.parseSql(query);
console.log("[RunSQLCommand]", "Running statements: ", statements);
const lastStatement = statements[statements.length - 1];

for (const statement of statements) {
console.log("[RunSQLCommand]", "Running statement: ", statement);
for (const statement of statements) {
console.log("[RunSQLCommand]", "Running statement: ", statement);

// Benchmark
const startTime = Date.now();
try {
const results = await context.privateQuery(statement.sql);
const endTime = Date.now();
const elapsedTime = endTime - startTime;
// Benchmark
const startTime = Date.now();
try {
const results = await context.privateQuery(statement.sql);
const endTime = Date.now();
const elapsedTime = endTime - startTime;

console.log("[RunSQLCommand]", "Results: ", results);
console.log("[RunSQLCommand]", "Emitting results.");
console.log("[RunSQLCommand]", "Results: ", results);
console.log("[RunSQLCommand]", "Emitting results.");

// Only display the results from the last statement.
if (lastStatement === statement) {
if (Array.isArray(results)) {
resultsProvider.setResults(id, { ...results[0], elapsedTime, id });
} else {
resultsProvider.setResults(id, { ...results, elapsedTime, id });
}
// Only display the results from the last statement.
if (lastStatement === statement) {
if (Array.isArray(results)) {
resultsProvider.setResults(id, { ...results[0], elapsedTime, id });
} else {
resultsProvider.setResults(id, { ...results, elapsedTime, id });
}
}

activityProvider.addLog({
status: "success",
latency: elapsedTime, // assuming elapsedTime holds the time taken for the query to execute
sql: statement.sql
});
} catch (error: any) {
console.log("[RunSQLCommand]", JSON.stringify(error));
const endTime = Date.now();
const elapsedTime = endTime - startTime;
activityProvider.addLog({
status: "success",
latency: elapsedTime, // assuming elapsedTime holds the time taken for the query to execute
sql: statement.sql
});
} catch (error: any) {
console.log("[RunSQLCommand]", JSON.stringify(error));
const endTime = Date.now();
const elapsedTime = endTime - startTime;

activityProvider.addLog({
status: "failure",
latency: elapsedTime, // assuming elapsedTime holds the time taken before the error was caught
sql: statement.sql
});
activityProvider.addLog({
status: "failure",
latency: elapsedTime, // assuming elapsedTime holds the time taken before the error was caught
sql: statement.sql
});

resultsProvider.setResults(id,
undefined,
{
message: error.toString(),
position: error.position,
query,
});
resultsProvider.setResults(id,
undefined,
{
message: error.toString(),
position: error.position,
query,
});

// Break for-loop.
break;
}
// Break for-loop.
break;
}
} catch (err) {
console.error("[RunSQLCommand]", err);
resultsProvider.setResults(id,
undefined,
{
message: err instanceof Error ? err.message : "Error processing your request.",
position: undefined,
query,
}
);

console.error("[RunSQLCommand]", "Error running statement: ", err);
}
} finally {
resultsProvider._view?.show();
} catch (err) {
console.error("[RunSQLCommand]", err);
resultsProvider.setResults(id,
undefined,
{
message: err instanceof Error ? err.message : "Error processing your request.",
position: undefined,
query,
}
);

console.error("[RunSQLCommand]", "Error running statement: ", err);
}
});
} finally {
resultsProvider._view?.show(true);
}
};

return sqlCommand;
Expand Down

0 comments on commit 5710915

Please sign in to comment.