Skip to content

Commit 707c83e

Browse files
committed
querySimple add support for cds profiles and db abstraction
1 parent 5c8b5cf commit 707c83e

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

bin/querySimple.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @ts-check
22
import * as base from '../utils/base.js'
3+
import dbClientClass from "../utils/database/index.js"
34

45
export const command = 'querySimple'
56
export const aliases = ['qs', "querysimple"]
@@ -28,6 +29,11 @@ export const builder = base.getBuilder({
2829
default: "table",
2930
type: 'string',
3031
desc: base.bundle.getText("outputTypeQuery")
32+
},
33+
profile: {
34+
alias: ['p', 'Profile'],
35+
type: 'string',
36+
desc: base.bundle.getText("profile")
3137
}
3238
})
3339

@@ -54,6 +60,12 @@ export let inputPrompts = {
5460
description: base.bundle.getText("outputTypeQuery"),
5561
type: 'string',
5662
required: true
63+
},
64+
profile: {
65+
description: base.bundle.getText("profile"),
66+
type: 'string',
67+
required: false,
68+
ask: () => { }
5769
}
5870
}
5971

@@ -89,10 +101,10 @@ export async function dbQuery(prompts) {
89101
// @ts-ignore
90102
const parser = new AsyncParser(opts, transformOpts, asyncOpts)
91103
try {
92-
base.setPrompts(prompts)
93-
const db = await base.createDBConnection()
104+
const dbClient = await dbClientClass.getNewClient(prompts)
105+
await dbClient.connect()
106+
let results = await dbClient.execSQL(prompts.query)
94107

95-
let results = await db.execSQL(prompts.query)
96108
if (!results[0]) {
97109
return base.error(base.bundle.getText("errNoResults"))
98110
}
@@ -122,27 +134,28 @@ export async function dbQuery(prompts) {
122134
}])
123135
await toFile(prompts.folder, prompts.filename, 'xlsx', excelOutput)
124136
} else {
125-
base.end()
126-
return base.error(base.bundle.getText("errExcel"))
137+
base.error(base.bundle.getText("errExcel"))
138+
dbClient.disconnect()
139+
return
127140
}
128141
break
129142
case 'json':
130143
if (prompts.filename) {
131144
await toFile(prompts.folder, prompts.filename, 'json', JSON.stringify(results, null, 2))
132145
} else {
133146
console.log(highlight(JSON.stringify(results, null, 2)))
134-
base.end()
147+
dbClient.disconnect()
135148
return JSON.stringify(results, null, 2)
136149
}
137150
break
138-
case 'csv':
151+
case 'csv':
139152
if (prompts.filename) {
140153
const csv = await parser.parse(results).promise()
141154
await toFile(prompts.folder, prompts.filename, 'csv', csv)
142155
} else {
143156
const csv = await parser.parse(results).promise()
144157
console.log(highlight(csv))
145-
base.end()
158+
dbClient.disconnect()
146159
return csv
147160
}
148161
break
@@ -154,7 +167,7 @@ export async function dbQuery(prompts) {
154167
}
155168
break
156169
}
157-
base.end()
170+
dbClient.disconnect()
158171
return results
159172
} catch (error) {
160173
base.error(error)

utils/database/hanaDirect.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,11 @@ export default class extends DBClientClass {
4242
return results
4343
}
4444

45+
async execSQL(query){
46+
base.debug(`execSQL for ${this.#clientType}`)
47+
const db = super.getDB()
48+
let results = await db.execSQL(query)
49+
return results
50+
}
51+
4552
}

utils/database/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ export default class dbClientClass {
2525
* @type {Object}
2626
*/
2727
#db
28+
/**
29+
* Database Client type/flavor
30+
* @type {String}
31+
*/
32+
#clientType = 'generic'
2833

2934
/**
3035
* Create an instance of the database client specific to the prompt profile
@@ -158,6 +163,17 @@ export default class dbClientClass {
158163
return
159164
}
160165

166+
/**
167+
* Execute single SQL Statement and directly return result set
168+
* @param {string} sql - SQL Statement
169+
* @returns {Promise<any>} - result set object
170+
*/
171+
async execSQL(query){
172+
base.debug(`execSQL for ${this.#clientType}`)
173+
let results = await this.#db.run(query)
174+
return results
175+
}
176+
161177
/**
162178
* Getter for Prompts Private Attribute
163179
* @returns {typeof import("prompt")} prompts - input prompts current value

0 commit comments

Comments
 (0)