简体   繁体   English

Postgresql C如何将uint64_t转换为数字?

[英]Postgresql C How to convert uint64_t into numeric?

Postgres doesn't support uint64_t , the largest integer type is BIGINT which's range is equal to int64. Postgres不支持uint64_t,最大的整数类型是BIGINT,其范围等于int64。 I need to return an unit64_t var from C function, how can I convert it into numeric then I can return a numeric from my function? 我需要从C函数返回一个unit64_t var,如何将其转换为数字,然后才能从函数中返回数字?

C's uint64_t and int64_t are castable to each other without loss of information (in twos-complement arithmetic, but I doubt you're using a UNIVAC), so you could cast all uint64_t quantities to int64_t before writing them to the database and back to uint64_t on read. C的uint64_tint64_t可相互转换而不会丢失信息(采用二进制补码算法,但我怀疑您使用的是UNIVAC),因此您可以将所有uint64_t数量转换为int64_t然后再将其写入数据库并返回uint64_t阅读。

There are some potential problems, though. 但是,存在一些潜在的问题。 First off, do not do this if you ever need to store actual negative numbers in the relevant database columns, because you won't be able to tell whether they were negative numbers or numbers above 2 63 −1 when you read them back. 首先,如果您需要在相关的数据库列中存储实际的负数, 则不要执行此操作,因为当您读回它们时,您将无法分辨出它们是负数还是大于2 63 -1的数字。 Also, all SQL operations will treat all numbers greater than 2 63 −1 as negative. 同样,所有SQL操作会将大于2 63 -1的所有数字都视为负数。 This may or may not be a problem, depending on what you are using these large numbers for and how complicated the queries involving them tend to be. 这可能是问题,也可能不是问题,这取决于您将这些大量数字用于什么以及涉及它们的查询趋向于多么复杂。

EDIT: I just realized that by "numeric" you probably meant PostgreSQL's arbitrary-precision NUMERIC column type. 编辑:我刚刚意识到,“数字”可能意味着PostgreSQL的任意精度NUMERIC列类型。 Unfortunately I am not having any luck whatsoever finding documentation for how to work with this type below the level of textual SQL. 不幸的是,我在文本SQL级别以下找不到任何有关如何使用此类型的文档的运气。 The only thing I can suggest is for you to read the header files for writing server-side extensions in C and/or ask for help on the pg mailing lists. 我唯一可以建议的就是让您阅读用于在C语言中编写服务器端扩展的头文件和/或在pg邮件列表上寻求帮助。

I can't find an existing function which handles this conversion directly. 我找不到直接处理此转换的现有函数。 Worse than that, all of the structures and functions used to build NUMERIC values appear to be local to numeric.c , and there's not much of any use in the accompanying header , so I'm not sure how you're expected to build them yourself (looks like we are not the first to notice this ). 更糟糕的是,用于构建NUMERIC值的所有结构和函数似乎都numeric.c本地,并且附带的标头中并没有太多用处,因此我不确定您期望如何构建它们自己(看起来我们不是第一个注意到这一点的人 )。

Seems that this basically limits you to the type's SQL-level API (ie the built-in type casts and arithmetic operations). 似乎这基本上将您限制在类型的SQL级别的API(即内置的类型转换和算术运算)上。 I think the only other SQL type which is both castable to NUMERIC , and wide enough to hold a uint64 , is TEXT . 我认为TEXT是唯一可转换为NUMERIC且足够宽以容纳uint64其他SQL类型。 In other words, the simplest approach might be to convert your value to a string, and pass it into the NUMERIC parsing routine, eg: 换句话说,最简单的方法可能是将您的值转换为字符串,然后将其传递给NUMERIC解析例程,例如:

char uint64_string [21];
sprintf(uint64_string, "%" PRIu64, uint64_val);
PG_RETURN_DATUM(DirectFunctionCall3(numeric_in, CStringGetDatum(uint64_string), 0, -1));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM