Merge pull request #13449 from Budibase/feature/sqs-prepared-statements
SQS prepared statement support
This commit is contained in:
commit
ab2396fa1f
|
@ -12,6 +12,7 @@ import {
|
||||||
isDocument,
|
isDocument,
|
||||||
RowResponse,
|
RowResponse,
|
||||||
RowValue,
|
RowValue,
|
||||||
|
SqlQueryBinding,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { getCouchInfo } from "./connections"
|
import { getCouchInfo } from "./connections"
|
||||||
import { directCouchUrlCall } from "./utils"
|
import { directCouchUrlCall } from "./utils"
|
||||||
|
@ -248,14 +249,20 @@ export class DatabaseImpl implements Database {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async sql<T extends Document>(sql: string): Promise<T[]> {
|
async sql<T extends Document>(
|
||||||
|
sql: string,
|
||||||
|
parameters?: SqlQueryBinding
|
||||||
|
): Promise<T[]> {
|
||||||
const dbName = this.name
|
const dbName = this.name
|
||||||
const url = `/${dbName}/${SQLITE_DESIGN_DOC_ID}`
|
const url = `/${dbName}/${SQLITE_DESIGN_DOC_ID}`
|
||||||
const response = await directCouchUrlCall({
|
const response = await directCouchUrlCall({
|
||||||
url: `${this.couchInfo.sqlUrl}/${url}`,
|
url: `${this.couchInfo.sqlUrl}/${url}`,
|
||||||
method: "POST",
|
method: "POST",
|
||||||
cookie: this.couchInfo.cookie,
|
cookie: this.couchInfo.cookie,
|
||||||
body: sql,
|
body: {
|
||||||
|
query: sql,
|
||||||
|
args: parameters,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
if (response.status > 300) {
|
if (response.status > 300) {
|
||||||
throw new Error(await response.text())
|
throw new Error(await response.text())
|
||||||
|
|
|
@ -13,6 +13,7 @@ import {
|
||||||
DatabaseQueryOpts,
|
DatabaseQueryOpts,
|
||||||
Document,
|
Document,
|
||||||
RowValue,
|
RowValue,
|
||||||
|
SqlQueryBinding,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import tracer from "dd-trace"
|
import tracer from "dd-trace"
|
||||||
import { Writable } from "stream"
|
import { Writable } from "stream"
|
||||||
|
@ -150,10 +151,13 @@ export class DDInstrumentedDatabase implements Database {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
sql<T extends Document>(sql: string): Promise<T[]> {
|
sql<T extends Document>(
|
||||||
|
sql: string,
|
||||||
|
parameters?: SqlQueryBinding
|
||||||
|
): Promise<T[]> {
|
||||||
return tracer.trace("db.sql", span => {
|
return tracer.trace("db.sql", span => {
|
||||||
span?.addTags({ db_name: this.name })
|
span?.addTags({ db_name: this.name })
|
||||||
return this.db.sql(sql)
|
return this.db.sql(sql, parameters)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -156,21 +156,21 @@ export async function search(
|
||||||
try {
|
try {
|
||||||
const query = builder._query(request, {
|
const query = builder._query(request, {
|
||||||
disableReturning: true,
|
disableReturning: true,
|
||||||
disableBindings: true,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
if (Array.isArray(query)) {
|
if (Array.isArray(query)) {
|
||||||
throw new Error("SQS cannot currently handle multiple queries")
|
throw new Error("SQS cannot currently handle multiple queries")
|
||||||
}
|
}
|
||||||
|
|
||||||
let sql = query.sql
|
let sql = query.sql,
|
||||||
|
bindings = query.bindings
|
||||||
|
|
||||||
// quick hack for docIds
|
// quick hack for docIds
|
||||||
sql = sql.replace(/`doc1`.`rowId`/g, "`doc1.rowId`")
|
sql = sql.replace(/`doc1`.`rowId`/g, "`doc1.rowId`")
|
||||||
sql = sql.replace(/`doc2`.`rowId`/g, "`doc2.rowId`")
|
sql = sql.replace(/`doc2`.`rowId`/g, "`doc2.rowId`")
|
||||||
|
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
const rows = await db.sql<Row>(sql)
|
const rows = await db.sql<Row>(sql, bindings)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
rows: await sqlOutputProcessing(
|
rows: await sqlOutputProcessing(
|
||||||
|
|
|
@ -4,6 +4,7 @@ import {
|
||||||
AnyDocument,
|
AnyDocument,
|
||||||
Document,
|
Document,
|
||||||
RowValue,
|
RowValue,
|
||||||
|
SqlQueryBinding,
|
||||||
ViewTemplateOpts,
|
ViewTemplateOpts,
|
||||||
} from "../"
|
} from "../"
|
||||||
import { Writable } from "stream"
|
import { Writable } from "stream"
|
||||||
|
@ -143,7 +144,10 @@ export interface Database {
|
||||||
opts?: DatabasePutOpts
|
opts?: DatabasePutOpts
|
||||||
): Promise<Nano.DocumentInsertResponse>
|
): Promise<Nano.DocumentInsertResponse>
|
||||||
bulkDocs(documents: AnyDocument[]): Promise<Nano.DocumentBulkResponse[]>
|
bulkDocs(documents: AnyDocument[]): Promise<Nano.DocumentBulkResponse[]>
|
||||||
sql<T extends Document>(sql: string): Promise<T[]>
|
sql<T extends Document>(
|
||||||
|
sql: string,
|
||||||
|
parameters?: SqlQueryBinding
|
||||||
|
): Promise<T[]>
|
||||||
allDocs<T extends Document | RowValue>(
|
allDocs<T extends Document | RowValue>(
|
||||||
params: DatabaseQueryOpts
|
params: DatabaseQueryOpts
|
||||||
): Promise<AllDocsResponse<T>>
|
): Promise<AllDocsResponse<T>>
|
||||||
|
|
Loading…
Reference in New Issue