orm.pg: fix f32 and f64 endianness (#20412)

This commit is contained in:
Felipe Pena 2024-01-06 17:27:10 -03:00 committed by GitHub
parent 3d8425dafd
commit 1303c244e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 82 additions and 2 deletions

View file

@ -143,13 +143,15 @@ fn pg_stmt_match(mut types []u32, mut vals []&char, mut lens []int, mut formats
}
f32 {
types << u32(Oid.t_float4)
vals << &char(&data)
num := conv.htonf32(f32(data))
vals << &char(&num)
lens << int(sizeof(f32))
formats << 1
}
f64 {
types << u32(Oid.t_float8)
vals << &char(&data)
num := conv.htonf64(f64(data))
vals << &char(&num)
lens << int(sizeof(f64))
formats << 1
}

View file

@ -0,0 +1,39 @@
module main
import db.pg
@[table: 'demo']
struct Demo {
id int @[primary; sql: serial]
number f64
number2 f32
}
fn test_float_field() {
conn := 'host=localhost user=test password=test' // insert own connection string
db := pg.connect_with_conninfo(conn)!
defer {
db.close()
}
sql db {
create table Demo
}!
demo := Demo{0, 9.58815, 9.58815}
sql db {
insert demo into Demo
}!
rows := sql db {
select from Demo
}!
assert rows[0].number == 9.58815
assert rows[0].number2 == 9.58815
sql db {
drop table Demo
}!
println(rows)
}