Repository navigation
Conversation
…s bool and otherwise BitString is used. Casts are used as long as the type is not explicitly set to NpgsqlDbType.Unknown or DbType.Object. NpgsqlDbType.Unknown is now default for parameters instead of text. Unknown parameters are always sent as text on prepared queries. DbType.Time is now mapped to interval. Output parameters now work for prepared queries.
|
I'm not expert on the parameter side of things, but this looks like awesome work. @glenebob and probably @franciscojunior should have better background to comment. If I understand correctly, if the user doesn't explicit set a parameter type, you modify the query text and introduce casting to let the backend know the type. With prepared statements we also have the option of using OIDs for this, right? In this case shouldn't OIDs be prepared over casts? The bit/bitstring/bool logic seems good to me, I like that a Postgresql array is always mapped to a .NET array, and that everything is nice and static. I'd rename the DbType from Varbit to BitString though; the type isn't inherently variable-length or invariable-length. Postgresql supports both BIT(N) and VARYING BIT(N), and both are represented by the same DbType, so it shouldn't contain "var" in its name. Calling it BitString would also create a nice analogy with String. One weird thing I found out while playing around though... The current npgsql implementation returns string for CHAR(1), rather than the expected .NET char. This seems to be another thing we should correct, and would make the two systems completely consistent:
What do you think? |
|
Thanks for feedback. When sending parameter oid list, we can't specify size, for example bit(3). Therefore we have to use cast. If size is not specified, we could use oid list. I haven't measured performance what is best. It should be semantically equivalent. Char(1) would be a good idea to use Char instead of String. |
|
Is the size really an issue? I mean, when you're sending parameters, isn't the OID for bit enough? After all, you're sending one bit type (with whatever size) to go into a column with a bit type (that could be with another size I think)? You're right that there are two OIDs, one for bit and one for varbit, but as a parameter OID bit should always be fine, shouldn't it? In any case I don't understand why the size would be a problem when sending OIDs to the database... I'm don't know anything about the performance difference, but if it's possible to send OIDs we should try to do it, right? It's less query rewriting at the client and less parsing at the server... About char(1), do you want to do it as part of this PR? |
|
@Emill, I think I've merged all my work which touches on the same areas. Sorry about the conflicts you will get when rebasing this... but I think you can continue working. There are some questions above (use OIDs instead of casts, char(1)), it would be good to see this merged. |
+1 to change it to BitString
"There's a previous discussion for That" :) |
|
Anyone who knows how this is done in sql server and mysql? |
I think we can use the oid regardless of the size. From the documentation, the Parse message only contains support for the type oid. I think postgresql can figure out the sizes later by the parameter value itself. |
|
@franciscojunior, about the char/string thing - I didn't really understand the problem... the fact that there's a special Postgresql "char" prevents us in some way from mapping char(1) to .NET Char? If the efficiency argument is the important one (checking each time), it may still be worth looking into, it doesn't sound like serious impact... |
|
@Emill: good question, I don't know. It would also be good to check the Postgresql JDBC driver... |
|
The "right" way to check for char(1) or varchar(1) (as opposed to char(2) etc.) seems to be to check the The type modifier in the RowDescription, which corresponds to pg_attribute.atttypmod):
|
I think you are right. I'm reviewing the discussion and the problem is in fact with the type char itself and not char(1). One problem, though is how would you map the other way around: .Net char to char(1)? .Net char has a size of 16 bits. |
|
I think it's right to map .NET Char to char(1) - char(1) is unicode (2-byte) so there's no problem. It's just like mapping a string to char(N) The special 1-byte "char" type can simply be mapped to .NET byte, which is really what it is: according to the docs its an internal enum-like representation... |
Hmmmmm, I thought char(1) would be 8-bits. I think at the time of the ASCII encoding it really was. Now that we support exclusively UTF-8 database encoding, a char(1) may not necessarily map to a single byte value :) |
+1 to map char(1) to .Net Char and add the documentation about this possible breaking change. |
I also don't know :( |
Well, MS SQL's SqlClient returns a string for CHAR(1), so I've changed my mind and think we should do the same and keep how it is today. Their BIT type is always only one bit, never longer (i.e. there are no BIT(n) or BIT VARYING), therefore their BIT maps to bool. So I think it is good that our BIT(1) maps to bool but BIT VARYING and BIT(n>1) maps to BitString.
BIT and BIT VARYING are two different data types (and have different OIDs), so I think there should be two different NpgsqlDbTypes. Compare with Char and Varchar: there are an NpgsqlDbType.Char and an NpgsqlDbType.Varchar. One thing I'm wondering over, in NpgsqlTypesHelper.cs, is there a reason that DbType.StringFixedLength isn't used instead of DbType.String for bpchar? |
It makes sense to align with SqlClient I guess. But in that case, is there any data type that SqlClient maps to .NET Char?
Hmm... I think the interesting comparison is maybe between VARCHAR and TEXT (CHAR being the single-char type, which seems not to correspond to any Postgresql type currently). I agree that the bit types should behave more or less like the character types, but I'm not sure the importance is of making the (client-side) distinction between VARCHAR and TEXT, it seems to be a purely backend-related storage consideration. But in the interest of not breaking too much etc. I guess we can leave it that way and do the same with the bit types.
No idea here... |
|
Em sábado, 18 de outubro de 2014, Emill [email protected] escreveu:
+1 for this. By mirroring sqlclient behavior we get a more consistent
I see. Giving what you said, I changed my mind and I think the best name
I think there is no specific reason for that. As bpchar , if I'm not wrong, —
Regards, Francisco Figueiredo Jr. |
|
I think there's kind of a lot of confusion that there are both the The problem with the |
|
Em sábado, 18 de outubro de 2014, Emill [email protected] escreveu:
I'll have a look at it, as this mapping was done a long time ago. What you
Regards, Francisco Figueiredo Jr. |
|
I just noticed that if you set a SqlParameter's value to a .NET char, the inferred type will be DbType.String and SqlDbType.NVarChar. Also, if you then try to retrieve the value again, you get a string back and not a char ;) |
That really sucks. A part of me wants to "fix" that in Npgsql and have chars roundtrip correctly, although aligning with SqlClient is also an important goal. Thoughts? |
|
Hmm, according to the test suite, NpgsqlDbType.Char seems to be used to get the bpchar type, but in the type mappings, NpgsqlDbType.Char is actually mapped to the single byte "char" data type, with OID 18. To avoid breakage, I think we should add a new NpgsqlDbType and call it something like SingleChar that should map to "char". Then we can correctly handle this, which will probably help later in #370. By the way, does anyone know if the "char" datatype widely used? |
I really don't think so. The Postgresql docs say:
I think it's definitely OK to introduce breakage around this type in 3.0. |
I'm always in favor of aligning with SqlClient where possible. A lot of Npgsql users is used to work with SqlClient and I think we should mirror its behavior. I know this may sound like a little lack of personality but we would get a more consistent library as many would expect the behavior to be similar. |
I also think almost nobody uses the "char" datatype.
Agreed. |
Hi. I have created a fix for the parameter issues + BitString issues.
This fix make these behaviours:
The type of the parameter can either be explicitly set or automatically infered when a value is set.
If the type is explicitly set to NpgsqlDbType.Unknown or DbType.Object, no casting will be made. Instead, the value is written as a literal string. For prepared queries, the value is also sent as a literal string (.ToString() on the value), hoping the backend can take care of it.
Otherwise, casting will always be made.
Until a value has been set (or only null has been set before something else), the type is initially NpgsqlDbType.Unknown instead of text, as it was before.
The char datatype if used as parameter type doesn't truncate the string anymore to one character.
I added a "varbit" datatype which maps to BitString.
In the case the user uses "bit(1)", a bool will be returned from the database instead of BitString. Array of bit(1) means a bool[].
Before, BitString didn't work well. If used as parameter, the bit string was truncated to one bit. I've fixed this and also implemented binary encoding for use with prepared queries (didn't work before at all).
Tell me what you think :)
I think this fix makes the parameter handling and data type handling more reasonable.
It should fix #146, #335, #361
Commit log:
BitString works better now.
Bit(1) uses bool and otherwise BitString is used.
Casts are used as long as the type is not explicitly set to NpgsqlDbType.Unknown or DbType.Object. NpgsqlDbType.Unknown is now default for parameters instead of text.
Unknown parameters are always sent as text on prepared queries.
DbType.Time is now mapped to interval.
Output parameters now work for prepared queries.