-
Notifications
You must be signed in to change notification settings - Fork 240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a simple way to handle composite data type? #355
Comments
There's nothing ready-made, though I imagine it shouldn't be too hard to extract the array-related parsing code for converting from the text format to your custom type on the C++ side. |
I had another look, and I think I'll try and build some support for this. But it does raise the question: should the string conversion functions be encoding-aware? So far I've been able to get away with supporting only ASCII (plus basically anything you like in strings). That would still mostly work for arrays and composite types, but not entirely I think. |
For composite data type, PostgreSQL support ('member1','member2','member3') format |
Yes, it's documented. The hard part is dealing with different encodings. For example, when you see a quote in that text, it could be a quote, or it could be just one byte in a completely different Japanese character. |
We can only support utf8 encoding and refuse input when a quote is not quote. |
BTW, does libpqxx can support PostgreSQL array of composite data type of by std::vector ? |
Yes, you can convert a The other direction is more complicated, but it is supported: see the |
Do you meam: libpqxx can use array_parser convert std::vector to array of postgresql composite data type automatically ? Should I write string_traits::to_buf function for composite data type, such as (‘StringOfFiled1’,‘StringOfFiled2’,‘StringOfFiled3') format string ?
|
Since psql can print composite data type in text format when we run select command in console, I believe there is a way to package the whole struct automatically when each field's string_traits is provied. By use each member field's from_string and to_buf functions, maybe we don't need consider encodings. Only consider format of composite data type, such as single
|
For now, you'll have to write a full Of course you can use I have released features in the past that only work for encodings where this does not happen. But of course I'd prefer to do it right. This stuff can be hard to retrofit later. |
I'm work on a project which has many new basic data type and composite data type, all types are ASCII encoding. If there is a feature supports define string_traits for composite data type and array automatically, it will save much time greatly and reduce code errors. |
Right now I'm thinking along the lines of a function that parses an SQL composite-type value into a |
std::tuple is not bad. How about use reflect, such as: https://github.com/EOSIO/fc/blob/master/include/fc/reflect/reflect.hpp ? |
Unlikely. I'd rather not add dependencies, but also, I'd need better documentation to be sure that I understood the library correctly. |
@gzliudan I just pushed experimental support for parsing the string representation of a value of a composite type into a series of C++ variables. See current Once we're happy with how this works, I can build it into And separately from that, I imagine it would be nice to have a way to convert a C++ value to a composite-type string representation as well. |
I checked composite.hxx. It seems only parse output of postgresql's composite data type. I'm uploading data of composite types to PostgreSQL now. So I will test parse function later when I need to decode data from PostgreSQL. Will you support nested array or composite data types recursively? |
Nested arrays are already supported. I haven't figured out yet how nested composite types are represented. |
Of course I did. If you see something that describes nesting of composite types, please quote that specific part. |
We can use ARRAY and ROW construct some nested composite types, then use select command print it in psql to check the text format of output string. Like this article: https://stackoverflow.com/questions/7363516/how-to-formulate-an-array-literal-of-a-composite-type-containing-arrays |
In that case the nesting is just not any kind of special case. Should work as-is. |
Here's an nested composite type example: CREATE TYPE key_weight AS
(
weight int2,
key text
);
CREATE TYPE authority AS
(
threshold int4,
keys key_weight[]
);
CREATE TYPE producer AS
(
name text,
auth authority[]
);
CREATE TABLE schedules
(
id serial PRIMARY KEY,
version int4 NOT NULL,
producers producer[] NOT NULL DEFAULT '{}'
); |
Thanks. I should incorporate this into a test. I had some code for converting composite-type values to strings sketched out, but it'll need some more work. The idea is that you'll say "here are my fields, here's the buffer, go convert." It would be convenient for use in writing your own |
Work on this has been progressing. You can now read a
|
Oh, and you can convert a series of values to a string representing a composite value. But I still have to come to grips with exactly how you would use this, with possible quoting and escaping etc. |
Why isn't that |
Yeah. That's because it's a variant of the |
Barring any catastrophic bugs or great new ideas, I intend to release 7.2 soon, and I'll be closing this bug as well. @KayEss, if you've got anything to add on the naming, this is your chance. :-) |
I have no objection to closing this issus. Thank you very much. |
Given the Postgres term for this is "composite" it seems a little awkward to use that on the C++ side too. Wouldn't something like |
@KayEss okay... how about Of course at some point I'll be wanting to have something very similar on |
Actually, I now see that I did not call those functions Next proposal: |
How about as_composite or as_tuple ? |
Genius! |
@gzliudan I don't think we could do I wouldn't want to use "tuple" here because (and this has caused me trouble before) "tuple" already means something on the C++ side and something different on the SQL side. |
Support is in the latest release... I'll close this ticket now, and then if there are problems with what I released, we can have a new one. |
If a composite data type is created in PostgreSQL, such as below:
Then is there a simple way to define it in libpqxx? assume all data type of each field has been defined in libpqxx already.
The text was updated successfully, but these errors were encountered: