Skip to content

Commit

Permalink
fix: Change sql property to query for errors (#611)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobdenOs authored Apr 25, 2024
1 parent 05babcf commit 585577a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
4 changes: 2 additions & 2 deletions hana/lib/drivers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ const formatPrivilegeError = function (row) {
return `MISSING ${GRANT}${row.PRIVILEGE} ON ${row.SCHEMA_NAME}.${row.OBJECT_NAME}`
}

const enhanceError = function (err, stack, sql, message) {
const enhanceError = function (err, stack, query, message) {
return Object.assign(err, stack, {
sql,
query,
message: message ? message : err.message,
})
}
Expand Down
40 changes: 25 additions & 15 deletions postgres/lib/PostgresService.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,34 +144,45 @@ GROUP BY k
text: sql,
name: sha,
}

const enhanceError = (err, sql) => Object.assign(err, { query: sql + '\n' + new Array(err.position).fill(' ').join('') + '^' })

return {
run: async values => {
// REVISIT: SQLService provides empty values as {} for plain SQL statements - PostgreSQL driver expects array or nothing - see issue #78
let newQuery = this._prepareStreams(query, values)
if (typeof newQuery.then === 'function') newQuery = await newQuery
const result = await this.dbc.query(newQuery)
return { changes: result.rowCount }
try {
// REVISIT: SQLService provides empty values as {} for plain SQL statements - PostgreSQL driver expects array or nothing - see issue #78
let newQuery = this._prepareStreams(query, values)
if (typeof newQuery.then === 'function') newQuery = await newQuery
const result = await this.dbc.query(newQuery)
return { changes: result.rowCount }
} catch (e) {
throw enhanceError(e, sql)
}
},
get: async values => {
// REVISIT: SQLService provides empty values as {} for plain SQL statements - PostgreSQL driver expects array or nothing - see issue #78
const result = await this.dbc.query({ ...query, values: this._getValues(values) })
return result.rows[0]
try {
// REVISIT: SQLService provides empty values as {} for plain SQL statements - PostgreSQL driver expects array or nothing - see issue #78
const result = await this.dbc.query({ ...query, values: this._getValues(values) })
return result.rows[0]
} catch (e) {
throw enhanceError(e, sql)
}
},
all: async values => {
// REVISIT: SQLService provides empty values as {} for plain SQL statements - PostgreSQL driver expects array or nothing - see issue #78
try {
const result = await this.dbc.query({ ...query, values: this._getValues(values) })
return result.rows
} catch (e) {
throw Object.assign(e, { sql: sql + '\n' + new Array(e.position).fill(' ').join('') + '^' })
throw enhanceError(e, sql)
}
},
stream: async (values, one) => {
try {
const streamQuery = new QueryStream({ ...query, values: this._getValues(values) }, one)
return await this.dbc.query(streamQuery)
} catch (e) {
throw Object.assign(e, { sql: sql + '\n' + new Array(e.position).fill(' ').join('') + '^' })
throw enhanceError(e, sql)
}
},
}
Expand Down Expand Up @@ -206,8 +217,7 @@ GROUP BY k
sql = sql.replace(
new RegExp(`\\$${i + 1}`, 'g'),
// Don't ask about the dollar signs
`(SELECT ${isBinary ? `DECODE(PARAM,'base64')` : 'PARAM'} FROM "$$$$PARAMETER_BUFFER$$$$" WHERE NAME='${
query.name
`(SELECT ${isBinary ? `DECODE(PARAM,'base64')` : 'PARAM'} FROM "$$$$PARAMETER_BUFFER$$$$" WHERE NAME='${query.name
}' AND ID=$${i + 1})`,
)
return
Expand Down Expand Up @@ -565,7 +575,7 @@ GROUP BY k
// Create new schema using schema owner
await this.tx(async tx => {
await tx.run(`DROP SCHEMA IF EXISTS "${creds.schema}" CASCADE`)
if (!clean) await tx.run(`CREATE SCHEMA "${creds.schema}" AUTHORIZATION "${creds.user}"`).catch(() => {})
if (!clean) await tx.run(`CREATE SCHEMA "${creds.schema}" AUTHORIZATION "${creds.user}"`).catch(() => { })
})
} finally {
await this.disconnect()
Expand Down Expand Up @@ -593,7 +603,7 @@ class QueryStream extends Query {
})
this.connection.flush()
}
: () => {},
: () => { },
})
this.push = this.stream.push.bind(this.stream)

Expand Down Expand Up @@ -708,7 +718,7 @@ class ParameterStream extends Writable {
}

// Used by the client to handle timeouts
callback() {}
callback() { }

_write(chunk, enc, cb) {
return this.flush(chunk, cb)
Expand Down
2 changes: 1 addition & 1 deletion sqlite/lib/SQLiteService.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class SQLiteService extends SQLService {
stream: (..._) => this._stream(stmt, ..._),
}
} catch (e) {
e.message += ' in:\n' + (e.sql = sql)
e.message += ' in:\n' + (e.query = sql)
throw e
}
}
Expand Down

0 comments on commit 585577a

Please sign in to comment.