mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
orm.pg: fix f32 and f64 endianness (#20412)
This commit is contained in:
parent
3d8425dafd
commit
1303c244e2
5 changed files with 82 additions and 2 deletions
|
@ -93,6 +93,7 @@ const skip_test_files = [
|
|||
'vlib/db/mysql/prepared_stmt_test.v', // mysql not installed
|
||||
'vlib/db/pg/pg_orm_test.v', // pg not installed
|
||||
'vlib/db/pg/pg_test.v', // pg not installed
|
||||
'vlib/db/pg/pg_double_test.v', // pg not installed
|
||||
]
|
||||
// These tests are too slow to be run in the CI on each PR/commit
|
||||
// in the sanitized modes:
|
||||
|
@ -368,6 +369,7 @@ fn main() {
|
|||
}
|
||||
testing.find_started_process('postgres') or {
|
||||
tsession.skip_files << 'vlib/db/pg/pg_orm_test.v'
|
||||
tsession.skip_files << 'vlib/db/pg/pg_double_test.v'
|
||||
}
|
||||
|
||||
$if windows {
|
||||
|
|
|
@ -138,6 +138,9 @@ fn (mut ctx Context) should_test(path string, backend string) ShouldTestStatus {
|
|||
if path.ends_with('pg_orm_test.v') {
|
||||
testing.find_started_process('postgres') or { return .skip }
|
||||
}
|
||||
if path.ends_with('pg_double_test.v') {
|
||||
testing.find_started_process('postgres') or { return .skip }
|
||||
}
|
||||
if path.ends_with('onecontext_test.v') {
|
||||
return .skip
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
39
vlib/db/pg/pg_double_test.v
Normal file
39
vlib/db/pg/pg_double_test.v
Normal 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)
|
||||
}
|
|
@ -1,5 +1,13 @@
|
|||
module conv
|
||||
|
||||
union ConversionUnion {
|
||||
mut:
|
||||
as_int64 u64
|
||||
as_int32 u32
|
||||
as_double64 f64
|
||||
as_double32 f32
|
||||
}
|
||||
|
||||
// htn64 - DON'T USE, use hton64 instead
|
||||
@[deprecated: 'use hton64() instead']
|
||||
@[deprecated_after: '2023-12-31']
|
||||
|
@ -7,6 +15,32 @@ pub fn htn64(host u64) u64 {
|
|||
return hton64(host)
|
||||
}
|
||||
|
||||
// htonf32 converts the 32 bit double `host` to the net format
|
||||
pub fn htonf32(host f32) f32 {
|
||||
$if little_endian {
|
||||
mut convert := ConversionUnion{
|
||||
as_double32: host
|
||||
}
|
||||
convert.as_int32 = unsafe { hton32(convert.as_int32) }
|
||||
return unsafe { convert.as_double32 }
|
||||
} $else {
|
||||
return host
|
||||
}
|
||||
}
|
||||
|
||||
// htonf64 converts the 64 bit double `host` to the net format
|
||||
pub fn htonf64(host f64) f64 {
|
||||
$if little_endian {
|
||||
mut convert := ConversionUnion{
|
||||
as_double64: host
|
||||
}
|
||||
convert.as_int64 = unsafe { hton64(convert.as_int64) }
|
||||
return unsafe { convert.as_double64 }
|
||||
} $else {
|
||||
return host
|
||||
}
|
||||
}
|
||||
|
||||
// hton64 converts the 64 bit value `host` to the net format (htonll)
|
||||
pub fn hton64(host u64) u64 {
|
||||
$if little_endian {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue