@@ -224,15 +224,15 @@ fn res_to_rows(res voidptr) []Row {
224
224
}
225
225
226
226
// close frees the underlying resource allocated by the database connection
227
- pub fn (db DB) close () {
227
+ pub fn (db & DB) close () {
228
228
C.PQfinish (db.conn)
229
229
}
230
230
231
231
// q_int submit a command to the database server and
232
232
// returns an the first field in the first tuple
233
233
// converted to an int. If no row is found or on
234
234
// command failure, an error is returned
235
- pub fn (db DB) q_int (query string ) ! int {
235
+ pub fn (db & DB) q_int (query string ) ! int {
236
236
rows := db.exec (query)!
237
237
if rows.len == 0 {
238
238
return error ('q_int "${query} " not found' )
@@ -249,7 +249,7 @@ pub fn (db DB) q_int(query string) !int {
249
249
// returns an the first field in the first tuple
250
250
// as a string. If no row is found or on
251
251
// command failure, an error is returned
252
- pub fn (db DB) q_string (query string ) ! string {
252
+ pub fn (db & DB) q_string (query string ) ! string {
253
253
rows := db.exec (query)!
254
254
if rows.len == 0 {
255
255
return error ('q_string "${query} " not found' )
@@ -264,12 +264,12 @@ pub fn (db DB) q_string(query string) !string {
264
264
265
265
// q_strings submit a command to the database server and
266
266
// returns the resulting row set. Alias of `exec`
267
- pub fn (db DB) q_strings (query string ) ! []Row {
267
+ pub fn (db & DB) q_strings (query string ) ! []Row {
268
268
return db.exec (query)
269
269
}
270
270
271
271
// exec submits a command to the database server and wait for the result, returning an error on failure and a row set on success
272
- pub fn (db DB) exec (query string ) ! []Row {
272
+ pub fn (db & DB) exec (query string ) ! []Row {
273
273
res := C.PQexec (db.conn, & char (query.str))
274
274
return db.handle_error_or_result (res, 'exec' )
275
275
}
@@ -282,7 +282,7 @@ fn rows_first_or_empty(rows []Row) !Row {
282
282
}
283
283
284
284
// exec_one executes a query and returns its first row as a result, or an error on failure
285
- pub fn (db DB) exec_one (query string ) ! Row {
285
+ pub fn (db & DB) exec_one (query string ) ! Row {
286
286
res := C.PQexec (db.conn, & char (query.str))
287
287
e := unsafe { C.PQerrorMessage (db.conn).vstring () }
288
288
if e != '' {
@@ -293,7 +293,7 @@ pub fn (db DB) exec_one(query string) !Row {
293
293
}
294
294
295
295
// exec_param_many executes a query with the parameters provided as ($1), ($2), ($n)
296
- pub fn (db DB) exec_param_many (query string , params []string ) ! []Row {
296
+ pub fn (db & DB) exec_param_many (query string , params []string ) ! []Row {
297
297
unsafe {
298
298
mut param_vals := []& char{len: params.len}
299
299
for i in 0 .. params.len {
@@ -307,24 +307,24 @@ pub fn (db DB) exec_param_many(query string, params []string) ![]Row {
307
307
}
308
308
309
309
// exec_param executes a query with 1 parameter ($1), and returns either an error on failure, or the full result set on success
310
- pub fn (db DB) exec_param (query string , param string ) ! []Row {
310
+ pub fn (db & DB) exec_param (query string , param string ) ! []Row {
311
311
return db.exec_param_many (query, [param])
312
312
}
313
313
314
314
// exec_param2 executes a query with 2 parameters ($1) and ($2), and returns either an error on failure, or the full result set on success
315
- pub fn (db DB) exec_param2 (query string , param string , param2 string ) ! []Row {
315
+ pub fn (db & DB) exec_param2 (query string , param string , param2 string ) ! []Row {
316
316
return db.exec_param_many (query, [param, param2 ])
317
317
}
318
318
319
319
// prepare submits a request to create a prepared statement with the given parameters, and waits for completion. You must provide the number of parameters (`$1, $2, $3 ...`) used in the statement
320
- pub fn (db DB) prepare (name string , query string , num_params int ) ! {
320
+ pub fn (db & DB) prepare (name string , query string , num_params int ) ! {
321
321
res := C.PQprepare (db.conn, & char (name.str), & char (query.str), num_params, 0 ) // defining param types is optional
322
322
323
323
return db.handle_error (res, 'prepare' )
324
324
}
325
325
326
326
// exec_prepared sends a request to execute a prepared statement with given parameters, and waits for the result. The number of parameters must match with the parameters declared in the prepared statement.
327
- pub fn (db DB) exec_prepared (name string , params []string ) ! []Row {
327
+ pub fn (db & DB) exec_prepared (name string , params []string ) ! []Row {
328
328
unsafe {
329
329
mut param_vals := []& char{len: params.len}
330
330
for i in 0 .. params.len {
@@ -337,7 +337,7 @@ pub fn (db DB) exec_prepared(name string, params []string) ![]Row {
337
337
}
338
338
}
339
339
340
- fn (db DB) handle_error_or_result (res voidptr , elabel string ) ! []Row {
340
+ fn (db & DB) handle_error_or_result (res voidptr , elabel string ) ! []Row {
341
341
e := unsafe { C.PQerrorMessage (db.conn).vstring () }
342
342
if e != '' {
343
343
C.PQclear (res)
@@ -349,7 +349,7 @@ fn (db DB) handle_error_or_result(res voidptr, elabel string) ![]Row {
349
349
return res_to_rows (res)
350
350
}
351
351
352
- fn (db DB) handle_error (res voidptr , elabel string ) ! {
352
+ fn (db & DB) handle_error (res voidptr , elabel string ) ! {
353
353
e := unsafe { C.PQerrorMessage (db.conn).vstring () }
354
354
if e != '' {
355
355
C.PQclear (res)
@@ -362,7 +362,7 @@ fn (db DB) handle_error(res voidptr, elabel string) ! {
362
362
363
363
// copy_expert executes COPY command
364
364
// https://www.postgresql.org/docs/9.5/libpq-copy.html
365
- pub fn (db DB) copy_expert (query string , mut file io.ReaderWriter) ! int {
365
+ pub fn (db & DB) copy_expert (query string , mut file io.ReaderWriter) ! int {
366
366
mut res := C.PQexec (db.conn, & char (query.str))
367
367
status := unsafe { ExecStatusType (C.PQresultStatus (res)) }
368
368
defer {
@@ -423,7 +423,7 @@ pub fn (db DB) copy_expert(query string, mut file io.ReaderWriter) !int {
423
423
return 0
424
424
}
425
425
426
- fn pg_stmt_worker (db DB, query string , data orm.QueryData, where orm.QueryData) ! []Row {
426
+ fn pg_stmt_worker (db & DB, query string , data orm.QueryData, where orm.QueryData) ! []Row {
427
427
mut param_types := []u32 {}
428
428
mut param_vals := []& char{}
429
429
mut param_lens := []int {}
@@ -452,7 +452,7 @@ pub struct PQTransactionParam {
452
452
}
453
453
454
454
// begin begins a new transaction.
455
- pub fn (db DB) begin (param PQTransactionParam) ! {
455
+ pub fn (db & DB) begin (param PQTransactionParam) ! {
456
456
mut sql_stmt := 'BEGIN TRANSACTION ISOLATION LEVEL '
457
457
match param.transaction_level {
458
458
.read_uncommitted { sql_stmt + = 'READ UNCOMMITTED' }
@@ -468,7 +468,7 @@ pub fn (db DB) begin(param PQTransactionParam) ! {
468
468
}
469
469
470
470
// commit commits the current transaction.
471
- pub fn (db DB) commit () ! {
471
+ pub fn (db & DB) commit () ! {
472
472
_ := C.PQexec (db.conn, c 'COMMIT;' )
473
473
e := unsafe { C.PQerrorMessage (db.conn).vstring () }
474
474
if e != '' {
@@ -477,7 +477,7 @@ pub fn (db DB) commit() ! {
477
477
}
478
478
479
479
// rollback rollbacks the current transaction.
480
- pub fn (db DB) rollback () ! {
480
+ pub fn (db & DB) rollback () ! {
481
481
_ := C.PQexec (db.conn, c 'ROLLBACK;' )
482
482
e := unsafe { C.PQerrorMessage (db.conn).vstring () }
483
483
if e != '' {
@@ -486,7 +486,7 @@ pub fn (db DB) rollback() ! {
486
486
}
487
487
488
488
// rollback_to rollbacks to a specified savepoint.
489
- pub fn (db DB) rollback_to (savepoint string ) ! {
489
+ pub fn (db & DB) rollback_to (savepoint string ) ! {
490
490
if ! savepoint.is_identifier () {
491
491
return error ('savepoint should be a identifier string' )
492
492
}
@@ -499,7 +499,7 @@ pub fn (db DB) rollback_to(savepoint string) ! {
499
499
}
500
500
501
501
// savepoint create a new savepoint.
502
- pub fn (db DB) savepoint (savepoint string ) ! {
502
+ pub fn (db & DB) savepoint (savepoint string ) ! {
503
503
if ! savepoint.is_identifier () {
504
504
return error ('savepoint should be a identifier string' )
505
505
}
0 commit comments