Skip to content

Commit 4e28e91

Browse files
committed
build
1 parent fb73e93 commit 4e28e91

File tree

6 files changed

+18
-26
lines changed

6 files changed

+18
-26
lines changed

cjs/src/index.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ function Postgres(a, b) {
235235
const queries = Queue()
236236
let savepoints = 0
237237
, connection
238-
let transactionId = null
238+
, prepare = null
239239

240240
try {
241241
await sql.unsafe('begin ' + options.replace(/[^a-z ]/ig, ''), [], { onexecute }).execute()
@@ -247,7 +247,7 @@ function Postgres(a, b) {
247247
async function scope(c, fn, name) {
248248
const sql = Sql(handler)
249249
sql.savepoint = savepoint
250-
sql.prepare = prepare
250+
sql.prepare = x => prepare = x.replace(/[^a-z0-9$-_. ]/gi)
251251
let uncaughtError
252252
, result
253253

@@ -268,11 +268,12 @@ function Postgres(a, b) {
268268
throw e instanceof PostgresError && e.code === '25P02' && uncaughtError || e
269269
}
270270

271-
if (transactionId) {
272-
!name && await sql.unsafe(`prepare transaction '${transactionId}'`)
273-
}else{
274-
!name && await sql`commit`
271+
if (!name) {
272+
prepare
273+
? await sql`prepare transaction '${ sql.unsafe(prepare) }'`
274+
: await sql`commit`
275275
}
276+
276277
return result
277278

278279
function savepoint(name, fn) {
@@ -291,9 +292,6 @@ function Postgres(a, b) {
291292
}
292293
}
293294

294-
async function prepare(name) {
295-
transactionId = name
296-
}
297295
function onexecute(c) {
298296
connection = c
299297
move(c, reserved)
@@ -303,7 +301,6 @@ function Postgres(a, b) {
303301
}
304302
}
305303

306-
307304
function move(c, queue) {
308305
c.queue.remove(c)
309306
queue.push(c)
@@ -468,7 +465,7 @@ function parseOptions(a, b) {
468465
...Object.entries(defaults).reduce(
469466
(acc, [k, d]) => {
470467
const value = k in o ? o[k] : k in query
471-
? (query[k] === 'disable' || query[k] === 'false' ? false : query[k])
468+
? (query[k] === 'disable' || query[k] === 'false' ? false : query[k])
472469
: env['PG' + k.toUpperCase()] || d
473470
acc[k] = typeof value === 'string' && ints.includes(k)
474471
? +value

cjs/tests/bootstrap.js

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ exec('createdb', ['postgres_js_test'])
1414
exec('psql', ['-c', 'grant all on database postgres_js_test to postgres_js_test'])
1515
exec('psql', ['-c', 'alter database postgres_js_test owner to postgres_js_test'])
1616

17-
1817
module.exports.exec = exec;function exec(cmd, args) {
1918
const { stderr } = spawnSync(cmd, args, { stdio: 'pipe', encoding: 'utf8' })
2019
if (stderr && !stderr.includes('already exists') && !stderr.includes('does not exist'))

cjs/tests/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ t('Prepared transaction', async() => {
246246
await sql.prepare('tx1')
247247
})
248248

249-
await sql.unsafe("commit prepared 'tx1'")
249+
await sql`commit prepared 'tx1'`
250250

251251
return ['1', (await sql`select count(1) from test`)[0].count, await sql`drop table test`]
252252
})

deno/src/index.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ function Postgres(a, b) {
236236
const queries = Queue()
237237
let savepoints = 0
238238
, connection
239-
let transactionId = null
239+
, prepare = null
240240

241241
try {
242242
await sql.unsafe('begin ' + options.replace(/[^a-z ]/ig, ''), [], { onexecute }).execute()
@@ -248,7 +248,7 @@ function Postgres(a, b) {
248248
async function scope(c, fn, name) {
249249
const sql = Sql(handler)
250250
sql.savepoint = savepoint
251-
sql.prepare = prepare
251+
sql.prepare = x => prepare = x.replace(/[^a-z0-9$-_. ]/gi)
252252
let uncaughtError
253253
, result
254254

@@ -269,11 +269,12 @@ function Postgres(a, b) {
269269
throw e instanceof PostgresError && e.code === '25P02' && uncaughtError || e
270270
}
271271

272-
if (transactionId) {
273-
!name && await sql.unsafe(`prepare transaction '${transactionId}'`)
274-
}else{
275-
!name && await sql`commit`
272+
if (!name) {
273+
prepare
274+
? await sql`prepare transaction '${ sql.unsafe(prepare) }'`
275+
: await sql`commit`
276276
}
277+
277278
return result
278279

279280
function savepoint(name, fn) {
@@ -292,9 +293,6 @@ function Postgres(a, b) {
292293
}
293294
}
294295

295-
async function prepare(name) {
296-
transactionId = name
297-
}
298296
function onexecute(c) {
299297
connection = c
300298
move(c, reserved)
@@ -304,7 +302,6 @@ function Postgres(a, b) {
304302
}
305303
}
306304

307-
308305
function move(c, queue) {
309306
c.queue.remove(c)
310307
queue.push(c)
@@ -469,7 +466,7 @@ function parseOptions(a, b) {
469466
...Object.entries(defaults).reduce(
470467
(acc, [k, d]) => {
471468
const value = k in o ? o[k] : k in query
472-
? (query[k] === 'disable' || query[k] === 'false' ? false : query[k])
469+
? (query[k] === 'disable' || query[k] === 'false' ? false : query[k])
473470
: env['PG' + k.toUpperCase()] || d
474471
acc[k] = typeof value === 'string' && ints.includes(k)
475472
? +value

deno/tests/bootstrap.js

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ await exec('createdb', ['postgres_js_test'])
1414
await exec('psql', ['-c', 'grant all on database postgres_js_test to postgres_js_test'])
1515
await exec('psql', ['-c', 'alter database postgres_js_test owner to postgres_js_test'])
1616

17-
1817
function ignore(cmd, args) {
1918
const { stderr } = spawnSync(cmd, args, { stdio: 'pipe', encoding: 'utf8' })
2019
if (stderr && !stderr.includes('already exists') && !stderr.includes('does not exist'))

deno/tests/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ t('Prepared transaction', async() => {
248248
await sql.prepare('tx1')
249249
})
250250

251-
await sql.unsafe("commit prepared 'tx1'")
251+
await sql`commit prepared 'tx1'`
252252

253253
return ['1', (await sql`select count(1) from test`)[0].count, await sql`drop table test`]
254254
})

0 commit comments

Comments
 (0)