Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix linebreaks in generated SQL #326

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@
},
{
"command": "vscode-db2i.getObjectLocks",
"when": "viewItem == table || viewItem == view || viewItem == alias || viewItem == constraint || viewItem == function || viewItem == variable || viewItem == index || viewItem == procedure || viewItem == sequence || viewItem == package || viewItem == trigger || viewItem == type",
"when": "viewItem == table || viewItem == view || viewItem == alias || viewItem == function || viewItem == variable || viewItem == index || viewItem == procedure || viewItem == sequence || viewItem == package || viewItem == trigger || viewItem == type",
"group": "db2workWith@5"
},
{
Expand Down Expand Up @@ -1322,7 +1322,7 @@
},
"devDependencies": {
"@continuedev/core": "^1.0.13",
"@halcyontech/vscode-ibmi-types": "^2.14.0",
"@halcyontech/vscode-ibmi-types": "^2.14.5",
"@types/glob": "^7.1.3",
"@types/node": "18.x",
"@types/vscode": "^1.95.0",
Expand Down
30 changes: 22 additions & 8 deletions src/database/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import path from "path";
import { getInstance } from "../base";

import { JobManager } from "../config";

export type SQLType = "schemas" | "tables" | "views" | "aliases" | "constraints" | "functions" | "variables" | "indexes" | "procedures" | "sequences" | "packages" | "triggers" | "types" | "logicals";
Expand Down Expand Up @@ -220,13 +220,27 @@ export default class Schemas {
* @param object Not user input
*/
static async generateSQL(schema: string, object: string, internalType: string): Promise<string> {
const lines = await JobManager.runSQL<{ SRCDTA: string }>([
`CALL QSYS2.GENERATE_SQL(?, ?, ?, CREATE_OR_REPLACE_OPTION => '1', PRIVILEGES_OPTION => '0')`
].join(` `), { parameters: [object, schema, internalType] });

const generatedStatement = lines.map(line => line.SRCDTA).join(`\n`);

return generatedStatement;
const instance = getInstance();
const connection = instance.getConnection();

const result = await connection.withTempDirectory<string>(async (tempDir) => {
const tempFilePath = path.posix.join(tempDir, `generatedSql.sql`);
await JobManager.runSQL<{ SRCDTA: string }>([
`CALL QSYS2.GENERATE_SQL( DATABASE_OBJECT_NAME => ?, DATABASE_OBJECT_LIBRARY_NAME => ?, DATABASE_OBJECT_TYPE => ?
, CREATE_OR_REPLACE_OPTION => '1', PRIVILEGES_OPTION => '0'
, DATABASE_SOURCE_FILE_NAME => '*STMF'
, STATEMENT_FORMATTING_OPTION => '0'
, SOURCE_STREAM_FILE => '${tempFilePath}'
, SOURCE_STREAM_FILE_END_OF_LINE => 'LF'
, SOURCE_STREAM_FILE_CCSID => 1208 )`
].join(` `), { parameters: [object, schema, internalType] });

// TODO: eventually .content -> .getContent(), it's not available yet
const contents = (await connection.content.downloadStreamfileRaw(tempFilePath)).toString();
return contents;
})

return result;
}

static async deleteObject(schema: string, name: string, type: string): Promise<void> {
Expand Down