Skip to content

Commit ddb1564

Browse files
authored
db.pg: fix incompatible fn signature (#24549)
1 parent 299ebdf commit ddb1564

File tree

3 files changed

+31
-30
lines changed

3 files changed

+31
-30
lines changed

vlib/db/pg/pg.c.v

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,15 @@ fn res_to_rows(res voidptr) []Row {
224224
}
225225

226226
// close frees the underlying resource allocated by the database connection
227-
pub fn (db DB) close() {
227+
pub fn (db &DB) close() {
228228
C.PQfinish(db.conn)
229229
}
230230

231231
// q_int submit a command to the database server and
232232
// returns an the first field in the first tuple
233233
// converted to an int. If no row is found or on
234234
// 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 {
236236
rows := db.exec(query)!
237237
if rows.len == 0 {
238238
return error('q_int "${query}" not found')
@@ -249,7 +249,7 @@ pub fn (db DB) q_int(query string) !int {
249249
// returns an the first field in the first tuple
250250
// as a string. If no row is found or on
251251
// 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 {
253253
rows := db.exec(query)!
254254
if rows.len == 0 {
255255
return error('q_string "${query}" not found')
@@ -264,12 +264,12 @@ pub fn (db DB) q_string(query string) !string {
264264

265265
// q_strings submit a command to the database server and
266266
// 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 {
268268
return db.exec(query)
269269
}
270270

271271
// 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 {
273273
res := C.PQexec(db.conn, &char(query.str))
274274
return db.handle_error_or_result(res, 'exec')
275275
}
@@ -282,7 +282,7 @@ fn rows_first_or_empty(rows []Row) !Row {
282282
}
283283

284284
// 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 {
286286
res := C.PQexec(db.conn, &char(query.str))
287287
e := unsafe { C.PQerrorMessage(db.conn).vstring() }
288288
if e != '' {
@@ -293,7 +293,7 @@ pub fn (db DB) exec_one(query string) !Row {
293293
}
294294

295295
// 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 {
297297
unsafe {
298298
mut param_vals := []&char{len: params.len}
299299
for i in 0 .. params.len {
@@ -307,24 +307,24 @@ pub fn (db DB) exec_param_many(query string, params []string) ![]Row {
307307
}
308308

309309
// 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 {
311311
return db.exec_param_many(query, [param])
312312
}
313313

314314
// 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 {
316316
return db.exec_param_many(query, [param, param2])
317317
}
318318

319319
// 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) ! {
321321
res := C.PQprepare(db.conn, &char(name.str), &char(query.str), num_params, 0) // defining param types is optional
322322

323323
return db.handle_error(res, 'prepare')
324324
}
325325

326326
// 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 {
328328
unsafe {
329329
mut param_vals := []&char{len: params.len}
330330
for i in 0 .. params.len {
@@ -337,7 +337,7 @@ pub fn (db DB) exec_prepared(name string, params []string) ![]Row {
337337
}
338338
}
339339

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 {
341341
e := unsafe { C.PQerrorMessage(db.conn).vstring() }
342342
if e != '' {
343343
C.PQclear(res)
@@ -349,7 +349,7 @@ fn (db DB) handle_error_or_result(res voidptr, elabel string) ![]Row {
349349
return res_to_rows(res)
350350
}
351351

352-
fn (db DB) handle_error(res voidptr, elabel string) ! {
352+
fn (db &DB) handle_error(res voidptr, elabel string) ! {
353353
e := unsafe { C.PQerrorMessage(db.conn).vstring() }
354354
if e != '' {
355355
C.PQclear(res)
@@ -362,7 +362,7 @@ fn (db DB) handle_error(res voidptr, elabel string) ! {
362362

363363
// copy_expert executes COPY command
364364
// 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 {
366366
mut res := C.PQexec(db.conn, &char(query.str))
367367
status := unsafe { ExecStatusType(C.PQresultStatus(res)) }
368368
defer {
@@ -423,7 +423,7 @@ pub fn (db DB) copy_expert(query string, mut file io.ReaderWriter) !int {
423423
return 0
424424
}
425425

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 {
427427
mut param_types := []u32{}
428428
mut param_vals := []&char{}
429429
mut param_lens := []int{}
@@ -452,7 +452,7 @@ pub struct PQTransactionParam {
452452
}
453453

454454
// begin begins a new transaction.
455-
pub fn (db DB) begin(param PQTransactionParam) ! {
455+
pub fn (db &DB) begin(param PQTransactionParam) ! {
456456
mut sql_stmt := 'BEGIN TRANSACTION ISOLATION LEVEL '
457457
match param.transaction_level {
458458
.read_uncommitted { sql_stmt += 'READ UNCOMMITTED' }
@@ -468,7 +468,7 @@ pub fn (db DB) begin(param PQTransactionParam) ! {
468468
}
469469

470470
// commit commits the current transaction.
471-
pub fn (db DB) commit() ! {
471+
pub fn (db &DB) commit() ! {
472472
_ := C.PQexec(db.conn, c'COMMIT;')
473473
e := unsafe { C.PQerrorMessage(db.conn).vstring() }
474474
if e != '' {
@@ -477,7 +477,7 @@ pub fn (db DB) commit() ! {
477477
}
478478

479479
// rollback rollbacks the current transaction.
480-
pub fn (db DB) rollback() ! {
480+
pub fn (db &DB) rollback() ! {
481481
_ := C.PQexec(db.conn, c'ROLLBACK;')
482482
e := unsafe { C.PQerrorMessage(db.conn).vstring() }
483483
if e != '' {
@@ -486,7 +486,7 @@ pub fn (db DB) rollback() ! {
486486
}
487487

488488
// 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) ! {
490490
if !savepoint.is_identifier() {
491491
return error('savepoint should be a identifier string')
492492
}
@@ -499,7 +499,7 @@ pub fn (db DB) rollback_to(savepoint string) ! {
499499
}
500500

501501
// savepoint create a new savepoint.
502-
pub fn (db DB) savepoint(savepoint string) ! {
502+
pub fn (db &DB) savepoint(savepoint string) ! {
503503
if !savepoint.is_identifier() {
504504
return error('savepoint should be a identifier string')
505505
}

vlib/db/pg/pg_double_test.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn test_float_field() {
1616
eprintln('> This test requires a working postgres server running on localhost.')
1717
return
1818
}
19-
conn := 'host=localhost user=test password=test' // insert own connection string
19+
conn := 'host=localhost user=postgres password=12345678' // insert own connection string
2020
db := pg.connect_with_conninfo(conn)!
2121
defer {
2222
db.close()

vlib/db/pg/pg_orm_test.v

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ fn test_pg_orm() {
4545
mut db := pg.connect(
4646
host: 'localhost'
4747
user: 'postgres'
48-
password: 'password'
49-
dbname: 'postgres'
48+
password: '12345678'
49+
dbname: 'test'
5050
) or { panic(err) }
5151

5252
defer {
5353
db.close()
5454
}
55+
db.drop('Test')!
5556

5657
db.create('Test', [
5758
orm.TableField{
@@ -140,6 +141,9 @@ fn test_pg_orm() {
140141
/** test orm sql type
141142
* - verify if all type create by attribute sql_type has created
142143
*/
144+
sql db {
145+
drop table TestCustomSqlType
146+
}!
143147

144148
sql db {
145149
create table TestCustomSqlType
@@ -157,15 +161,12 @@ fn test_pg_orm() {
157161
mut information_schema_data_types_results := []string{}
158162
information_schema_custom_sql := ['integer', 'text', 'character varying',
159163
'timestamp without time zone', 'uuid']
164+
160165
for data_type in result_custom_sql {
161-
x := data_type.vals[0]!
166+
x := data_type.vals[0]
162167
information_schema_data_types_results << x?
163168
}
164169

165-
sql db {
166-
drop table TestCustomSqlType
167-
}!
168-
169170
assert information_schema_data_types_results == information_schema_custom_sql
170171

171172
/** test_orm_time_type
@@ -224,8 +225,8 @@ fn test_pg_orm() {
224225
mut information_schema_defaults_results := []string{}
225226

226227
for defaults in result_defaults {
227-
x := defaults.vals[0]!
228-
information_schema_defaults_results << x?
228+
x := defaults.vals[0]
229+
information_schema_defaults_results << x or { '' }
229230
}
230231
sql db {
231232
drop table TestDefaultAttribute

0 commit comments

Comments
 (0)