Repository navigation
Conversation
|
Oh shit, I worked on this at the exact same time :( We really should coordinate more :( Have no time to look at this right now, but will look and compare with what I did... |
|
@Emill, you can see my work in this branch - it's not 100% complete but it does work. Some differences from your implementation:
Anyway, your work looks great, take a look at mine and tell me what you think. In any case we should probably start coordinating more, by self-assigning issues we want to work on and noting that we're starting to work... |
|
Haha, well it wasn't that much wasted time I spent on this... I like the idea of late registration of enums, and your code seems to have solved that. EnumType in the parameter type is needed if you want to send a typed null value. It is also probably good just for completeness. In your read handler, I'm not sure if it should throw an exception... Might that corrupt the buffer state? Maybe we can somehow merge both and take the best of both worlds? |
I just added a commit to my branch to test null... Even if you don't set NpgsqlDbValue, Npgsql sends parameter type "unrecognized" (OID 0), an as long as PG has enough context to understand that it's a null everything works. In other words, except for context-less cases (e.g. SELECT @p), it doesn't seem like NpgsqlDbValue.Enum is needed... Do you think still it's important to have it?
That's a good question... In general, exceptions thrown by read handlers should break the connector, since in sequential read mode they might have been thrown in the middle of the column and now we're no longer in sync. However you're right - this kind of exception is because of a "usage error" and not an I/O error or a bug in the handler, so we can do better. Here's an idea: the first time an enum is "activated" on a database (i.e. connection string), we query the pg_enum table and make sure everything corresponds, or throw an exception then. For late registration the exception would be thrown from the registration method (this is perfect), if you do global "pre-registration" the exception will be thrown on the first open - not so perfect but probably the best we can do. What do you think?
Sure. Tell me what you think about the above and I can merge things together. I'm assigning #441 (composite types) to you, I guess it can be implemented a bit similar to enums with registration etc. There's also numeric/decimal support with a provider-specific value (#448) if you want to work on that. |
|
Can't we just do a table join to get all the enum values as well from the database? But for backwards/forwards? compatibility, we shouldn't complain if the user misses some enum values in the .NET enum that are present in the database. What do you think of just using default(EnumType) if an unknown enum value is encountered. Or yet better, make it somehow possible to return an exception (for non-protocol errors) in the Read handler, that the caller can take care of. The Read handler should, if that's the case, skip everything in the buffer until the end (using the len parameter).
What do you other guys think? I agree that it is probably a bit useless in practice, but should be there for completeness... And it must be there to get a typed enum null value (without having to write a cast in the query). One could otherwise with the same argument argue that most NpgsqlDbTypes are worthless, such as int, float, double, bytea etc. |
+1
+1 for the default value.
I think we should think about the user in this case. I think that for completeness, as @Emill said, we should have it. Users would expect to set a NpgsqlDbType to Enum if they are using an Enum. Just like when using a custom type, I'd like to set an NpgsqlDbType which could specify that. The same way, when returning data from the database, I think it would be nice if the NpgsqlDbType could be filled by Npgsql for me. From msdn, I can see that sql server has the Udt sqldbtype. I think this has the same role as NpgsqlDbType.Composite, right? Now, regarding the EnumType property in the NpgsqlParameter, I need to check it better to give a proper opinion. By seeing the Composite NpgsqlDbType, I'm seeing this may be the start of the works to add support for registering datatypes and maybe some handlers for user types in the future? :) Please, let me know if I'm talking any nonsense. :) |
Yes. Udt is a kind of composite type, like a C struct.
Since there is no type in postgresql called "enum", only a type class, NpgsqlDbType.Enum doesn't really say what oid to use, since all user-defined enums have different oids. Therefore, to specify which enum type to use, NpgsqlDbType is not enough. But Npgsql is the owner of NpgsqlDbType, not the user, so a user cannot add custom types to that enum (NpgsqlDbType), so we need something else (like EnumType) to fully specify the type.
Not really... Composite types are a kind of special types that all use the same i/o (and can therefore use the same type handler). Composite types are just like a C struct, many fields in one single value.
Well, something similar, yes. |
Yep, that'll work. I'll do that.
Not sure about this, this could lead to very serious errors that are hard to debug - the user would get an incorrect value (whatever enum value happens to have the 0 value). Compare this with something like Entity Framework. If the model changes (i.e. you added some field to a class), and your database is no longer in sync, EF refuses to start and tells you to do a migration. Isn't it better to do the same in this case?
The problem with this is that reading the field is the type handler's responsibility, and an exception may be thrown anywhere during that process, so the framework wouldn't know how much of the field was already read. What could work, is if the type handler itself is written to always guarantee to consume the entire field if it emits an exception (actually maybe this is what you meant @Emill?). This makes sense to me, we could also use this in cases where a provider-specific value cannot be returned as a regular .NET value (e.g. out-of-range date field). Note that in the current architecture this works even better than before, because the type handler itself never performs any I/O. So any exceptions coming out of it are necessary actual conversion exceptions and never protocol or I/O errors... So we have two options: compare the enums on startup time and bomb if there's a discrepancy, or just do validation on send/receive and raise the exception there.
Well, I've found one argument against myself... When preparing a statement, you set up the parameters without actual values (because you're just preparing, there aren't any values to actually send to the database yet). This is a good case for using DbType/NpgsqlDbType rather than type inference, and so the EnumType makes sense there... I'll do it. However, one thing I think I'll change, is the value of NpgsqlDbType.Enum which is currently set to 0x40000000 as if it were a "combining" value (like Array or Range). Since you don't combine it with any other NpgsqlDbType value it can just be another NpgsqlDbType with a normal low value... @Emill, I'll merge in your work into my commit soon.
Yeah, I agree. |
Well, you can add a database field with a default value. Since EF only retrieves fields it know about, and upon insertion the database automatically inserts the default value if missing, it works very well to first add a field in the database, and then update the code. This is at least what I do in production...
Yes, note that I wrote returns an exception, or sets an out parameter of whatever, not throws an exception.
I didn't think of that, but you're correct ;)
In my PR, this is not the case anymore... |
Why is that important? I mean, why not simply allow the read handler throw an exception? As long as it internally consumes the entire column in the buffer everything should be fine... |
|
Ok, that probably works as well. |
|
OK great, I'll do the merge in the next few days. After that I was thinking of attacking multiqueries. |
I see. I didn't notice the enum oid need :) I think I'm inclined to handle Enum values as any other custom type the user could add to Npgsql by specifying all the information when registering the Enum. We would use the NpgsqlDbType.Enum to specify the enum and use the value provided by the user to check which object should be used.
+1
What you said changed my mind about the default value. |
I don't think C# allows you to extend enums...
The EnumType field would be totally optional when sending the enum, if the user leaves it unset it's still inferred from the Value (or NpgsqlValue) fields. It's just a way to let the user specify the enum type without loading an actual value - not terribly important but why not.
Well, we need to choose one of the options: either throw the exception on startup, or when it happens (on actual reading of a value). I guess that if we can throw exceptions from the read handler, it may be better to do as @Emill suggested and throw them on actual reading (although there are arguments for the other options as well...) |
|
I think the best way would be to throw an exception when a value is read, 2015-01-20 0:49 GMT+01:00 Shay Rojansky [email protected]:
|
Whooops, sorry about that. (Feeling ashamed for saying such thing... :( ) I think I had in mind the idea of having a custom type to hold the enum value which would have all the information (I think this is the role of the EnumType)
I see. I was worried this could add a very different way of interacting with the data provider. But I think, as usual, that I'm over thinking about it. :)
I see. Ok, +1 to throw the exception on actual reading. The advantage I saw doing the checking up front was that we wouldn't need to handle the problem later and the user would know there is a problem when registering the enum. On the other side, if we handle it in the actual reading, we don't need to make any check upfront and we would only report an error when it really occurs. (I mean, even if the enums are out of sync, we would only get a problem if the out of sync value is actually used.) |
|
One small comment... I'm wrapping up the enum implementation merge between @Emill's code and mine, and I wonder... Is it really worth loading the available enum labels from the database at all? They would serve to validate, client-side, that we're sending a valid label to the database. But why not just send the label (after going through any mapping via the attribute, of course), and let the backend raise the exception as an error? In general, I'm not sure there's really a benefit to checking something client-side which would be checked immediately after server-side (@Emill, this is also why I preferred removing the check for array sizes <= 6). Any comments? |
|
Server side check is enough, I think. We don't need to load valid values. |
|
Merged manually and added agreed changes. @Emill, I did this one a little quickly, if you have time to take a look and make sure I didn't do anything silly that would help... Note that on the way I implemented caching of the backend type definitions loaded from the database at connect-time. This cached list is also reused for enum late registration. Other numerous improvements and fixes as usual. |
|
Hi, It seems to me like it uses the last registered enum within the command (I'm registering the result enum after severity). BTW, I've noticed you've changed the logging system, how do I turn on Npgsql logging now, please? Thanks, |
|
@ManickaP, it's a bit difficult to understand exactly what's going on from your description: any way you can submit a code repro for this? Regarding logging, yes I've changed it. All you need to do is set Npgsql.Logging.NpgsqlLogManager's Provider property to a class implementing INpgsqlLoggingProvider before you do anything else with Npgsql, and logs will flow through your provider. A good example can be found in our unit test setup, which sets up an NLog adapter for Npgsql. Let me know if you need any further help with this. Once we release our first beta I'll seriously start to work on documenting all the new things... |
|
C# code: postgre sql: Exception text: |
|
Ok, try the latest commit. |
|
It works, thanks a lot. |
This adds support for user-defined enum types.
If a user has created an enum like this in the database:
and has defined a .NET type like this:
the user can register it for use in Npgsql like this:
Then it can be used in parameters in these different ways:
A data reader's GetValue() will return the value converted to the enum type the user has registered.
Some questions: