Skip to content

Commit

Permalink
Correclty include new methods in tools namespace
Browse files Browse the repository at this point in the history
Signed-off-by: worksofliam <[email protected]>
  • Loading branch information
worksofliam committed Feb 16, 2024
1 parent 16b7564 commit c621f68
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 44 deletions.
85 changes: 43 additions & 42 deletions src/api/Tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,50 +259,51 @@ export namespace Tools {
}
}
}
}

/**
* We do this to find previously opened files with the same path, but different case OR readonly flags.
* Without this, it's possible for the same document to be opened twice simply due to the readonly flag.
*/
export function findExistingDocumentUri(uri: vscode.Uri) {
const bathUriString = uriStringWithoutFragment(uri);
const possibleDoc = vscode.workspace.textDocuments.find(document => uriStringWithoutFragment(document.uri) === bathUriString);
if (possibleDoc) {
return possibleDoc.uri;
}

return uri;
}

/**
* We convert member to lowercase as members are case insensitive.
*/
function uriStringWithoutFragment(uri: vscode.Uri) {
// To lowercase because the URI path is case-insensitive
const baseUri = uri.scheme + `:` + uri.path;
const isCaseSensitive = (uri.scheme === `streamfile` && uri.path.startsWith(`/QOpenSys/`));
return (isCaseSensitive ? baseUri : baseUri.toLowerCase());
}

/**
* Fixes an SQL statement to make it compatible with db2 CLI program QZDFMDB2.
* - Changes `@clCommand` statements into Call `QSYS2.QCMDEX('clCommand')` procedure calls
* - Makes sure each comment (`--`) starts on a new line
* @param statement the statement to fix
* @returns statement compatible with QZDFMDB2
*/
export function fixSQL(statement: string) {
return statement.split("\n").map(line => {
if (line.startsWith('@')) {
//- Escape all '
//- Remove any trailing ;
//- Put the command in a Call QSYS2.QCMDEXC statement
line = `Call QSYS2.QCMDEXC('${line.substring(1, line.endsWith(";") ? line.length - 1 : undefined).replaceAll("'", "''")}');`;

/**
* We do this to find previously opened files with the same path, but different case OR readonly flags.
* Without this, it's possible for the same document to be opened twice simply due to the readonly flag.
*/
export function findExistingDocumentUri(uri: vscode.Uri) {
const bathUriString = uriStringWithoutFragment(uri);
const possibleDoc = vscode.workspace.textDocuments.find(document => uriStringWithoutFragment(document.uri) === bathUriString);
if (possibleDoc) {
return possibleDoc.uri;
}

//Make each comment start on a new line
return line.replaceAll("--", "\n--");
return uri;
}

/**
* We convert member to lowercase as members are case insensitive.
*/
function uriStringWithoutFragment(uri: vscode.Uri) {
// To lowercase because the URI path is case-insensitive
const baseUri = uri.scheme + `:` + uri.path;
const isCaseSensitive = (uri.scheme === `streamfile` && uri.path.startsWith(`/QOpenSys/`));
return (isCaseSensitive ? baseUri : baseUri.toLowerCase());
}

/**
* Fixes an SQL statement to make it compatible with db2 CLI program QZDFMDB2.
* - Changes `@clCommand` statements into Call `QSYS2.QCMDEX('clCommand')` procedure calls
* - Makes sure each comment (`--`) starts on a new line
* @param statement the statement to fix
* @returns statement compatible with QZDFMDB2
*/
export function fixSQL(statement: string) {
return statement.split("\n").map(line => {
if (line.startsWith('@')) {
//- Escape all '
//- Remove any trailing ;
//- Put the command in a Call QSYS2.QCMDEXC statement
line = `Call QSYS2.QCMDEXC('${line.substring(1, line.endsWith(";") ? line.length - 1 : undefined).replaceAll("'", "''")}');`;
}

//Make each comment start on a new line
return line.replaceAll("--", "\n--");
}
).join("\n");
}
).join("\n");
}
4 changes: 2 additions & 2 deletions src/instantiate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tools, findExistingDocumentUri } from './api/Tools';
import { Tools } from './api/Tools';

import path, { dirname } from 'path';
import * as vscode from "vscode";
Expand Down Expand Up @@ -121,7 +121,7 @@ export async function loadAllofExtension(context: vscode.ExtensionContext) {

const uri = getUriFromPath(path, options);

const existingUri = findExistingDocumentUri(uri);
const existingUri = Tools.findExistingDocumentUri(uri);

if (existingUri) {
const existingOptions = parseFSOptions(existingUri);
Expand Down

0 comments on commit c621f68

Please sign in to comment.