You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Npgsql currently supports unmapped enums as simple strings. This removes the need to define a CLR enum and can help in some dynamic scenarios where the database schema isn't known ahead of time.
Try to implement the same for composite types, reading and writing them as type dynamic.
The main blocker here is that unlike all other types, composite definitions are loaded lazily by Npgsql, rather than at first-connection-time. This was done since a composite type is created for each table, leading to a quick explosion of composite types and an unreasonable start time (see #1126). However, it's easy to distinguish real composite types from table types by joining the pg_type entry with pg_class and checking relkind (see https://www.postgresql.org/docs/current/static/catalog-pg-class.html). So we could switch back to eager loading for composites and then return dynamic.
The only casualty here would be mapping tables as composite types (#990), since we'd stop loading type definitions for those. We could leave lazy/late type loading just for this case, but it a little too rare/exotic and it seems better to simplify Npgsql to be eager-loading only.
One issue here would be how to write unmapped composites. In the general case, Npgsql either infers the PostgreSQL type from the provided CLR type - but with a dynamic object obviously that isn't possible - or allow the user to specify the PostgreSQL type via NpgsqlDbType and SpecificType (for enums and composites). However, the whole point here is to avoid having to define a CLR type, so there's nothing to set SpecificType to.
We could add a simple string property to NpgsqlParameter (e.g. TypeName) which Npgsql would use. That's not amazing - a whole property just for writing unmapped enum/composite - but it's possible.
More in general, a simple string to specify the database type seems much more appropriate than the ADO.NET standard DbType/NpgsqlDbType enums, which are closed and do not work for more sophisticated types (enums/composites). Will open an issue about this general problem in ADO.NET.
This even makes general sense over NpgsqlDbType, allowing extension to support new types without getting stuck on NpgsqlDbType which, being an enum, isn't extensible.
We could leave lazy/late type loading just for this case, but it a little too rare/exotic and it seems better to simplify Npgsql to be eager-loading only.
Please think about this twice.
IMO the ability to map tables as composite types is a widely unknown feature of Npgsql that provides very slim and easy ORM functionality.
I could demostrate you quite a few cases where this is actually really useful.
We could leave lazy/late type loading just for this case, but it a little too rare/exotic and it seems better to simplify Npgsql to be eager-loading only.
Please think about this twice.
IMO the ability to map tables as composite types is a widely unknown feature of Npgsql that provides very slim and easy ORM functionality.
I could demostrate you quite a few cases where this is actually really useful.
I agree it's a pretty cool feature, it's just that it really seems really, really rarely used.
To be frank, changing the type loading system to be 100% eager would also simplify things inside Npgsql - composites are now the only types loaded lazily, and it's kind of a wart on the system. And the only reason composites are loaded lazily is because there can be so many of them, because a composite type exists per table.
One last option here, which in general I try to avoid as possible, is to simply add a parameter, Load All Composites, which would be false by default. Thus, everything will always be loaded eagerly, and if you happen to want to use the table-as-composite feature, you'd have to add this parameter.
In any case, I couldn't resist and have started working on this issue, and it's going quite well - I already have tests parsing unmapped composites as dynamic ExpandoObjects. It's a real pleasure to see and to use.
One last option here, which in general I try to avoid as possible, is to simply add a parameter, Load All Composites, which would be false by default. Thus, everything will always be loaded eagerly, and if you happen to want to use the table-as-composite feature, you'd have to add this parameter.
👍
In any case, I couldn't resist and have started working on this issue, and it's going quite well - I already have tests parsing unmapped composites as dynamic ExpandoObjects. It's a real pleasure to see and to use.
Looks good.
As I mentioned before SqlClient solves #1746 (comment) via read-write UdtTypeName property. Yes, maybe it makes standardized it on ADO.NET level.
Only unfortunate thing that it will be confusing to have all: EnumType, SpecificType, DbType, PostgresType and TypeName (or UdtTypeName) on NpgsqlParameter. :(
I know that EnumType is deprecated but still adds to perception complexity.
@dmitry-a-morozov - ah, thanks for the added information: I didn't know about UdtTypeName...
I think if we implement UdtTypeName (or TypeName) we can safely obsolete SpecificType as well. I hope that will at least reduce the confusion around those two (which we can also remove in a later release). Then there's the question of PostgresType, which we proposed to add to NpgsqlParameter only for derivation (so as an "output property"), although we could also allow it to be set by the user to determine the outgoing parameter name as well.
Yeah, we definitely will have to add some clear documentation on all this.
I think UdtTypeName is more like DataTypeName e.g. full data type name as in database including schema. Where is SpecificType is CLR mapped type. They are complementary not redundant, correct?
Having only UdtTypeName is sufficient for my purposes, no need for PostgresType.
@dmitry-a-morozov yes, UdtTypeName is a string property with a full database-specific type name, where SpecificType is an CLR type that has already been mapped to the type name. But at the end of the day, what we need is a simple way for users to tell Npgsql which database type to send. If there's a simple, direct string-based way to do so (i.e. UdtTypeName), then SpecificType really provides no additional value. It's just another layer of indirection, since in any case the CLR type has to have already been mapped to... the string name of the database type...
So I'm leaning towards obsoleting SpecificType in favor of UdtTypeName (or whatever we choose to call it).
Thanks for the code snipped from SqlClient. I'm thinking to do thinks a little bit different, and to allow specifying the type as a string for all database types, not just UDT ones (which seem to correspond PostgreSQL composites). I don't see any reason to restrict this mechanism only to composites, and it could actually be useful for other types added by extensions, and which aren't present in the closed NpgsqlDbType enum. This also means that the property should be named something like TypeName rather than UdtTypeName, because it isn't necessarily related to user-defined types.
@dmitry-a-morozov definitely not - that would break all existing Npgsql code. The idea would be for TypeName to be (yet) another way for users to specify which type they want to write. Npgsql would look in the following order:
If TypeName is set, use that
Otherwise, if NpgsqlDbType is set, use that. If it's set to composite or enum, also look at SpecificType (although this would be obsoleted)
Otherwise, if DbType is set, use that (for database-independent programming)
Otherwise, infer from the CLR type of Value (this actually works quite well for mapped composites and enums, so SpecificType is actually kinda useless).
In PostgreSQL, array names are simply the type name with an underscore prepended to it. So an array of text is _text, and an array of your custom enum my_enum is _my_enum.
FYI as we're adding the PostgresType property to NpgsqlParameter, I'm considering allowing that to be used as well to specify the type of the outgoing parameter. If we do that, then TypeName would be a thin wrapper which simply looks up the PostgresType by name and sets it.
The bulk of the work has been merged - along with some other changes to the type mapping system.
Composites can be read and written as any CLR type without mapping them first.
Mapping is still optional to make the CLR type the default when reading, and also to allow omitting specifying DataTypeName on the parameter when writing.
Support added for reading and writing unmapped composites as ExpandoObjects, i.e. when NpgsqlDataReader.GetValue() is called on an unmapped composites.
Composite type definitions are now eagerly loaded, just like all other types.
By default, only "free-standing" composite types are loaded; composite types corresponding to PostgreSQL tables are only loaded if the Load All Composites parameter is set.
Leaving open this issue for the following remaining work:
Remaining work: add two-way name translation to INpgsqlNameTranslator, necessary for reading unmapped composites dynamically. Right now dynamically-read composites don't have their names translated.
Note that reading arrays of unmapped composites doesn't work, and would require significant changes in ArrayHandler. Will wait to see if people actually request this.
Activity
roji commentedon Dec 13, 2017
One issue here would be how to write unmapped composites. In the general case, Npgsql either infers the PostgreSQL type from the provided CLR type - but with a dynamic object obviously that isn't possible - or allow the user to specify the PostgreSQL type via NpgsqlDbType and SpecificType (for enums and composites). However, the whole point here is to avoid having to define a CLR type, so there's nothing to set SpecificType to.
We could add a simple string property to NpgsqlParameter (e.g.
TypeName) which Npgsql would use. That's not amazing - a whole property just for writing unmapped enum/composite - but it's possible.More in general, a simple string to specify the database type seems much more appropriate than the ADO.NET standard DbType/NpgsqlDbType enums, which are closed and do not work for more sophisticated types (enums/composites). Will open an issue about this general problem in ADO.NET.
roji commentedon Dec 13, 2017
General ADO.NET issue for adding string-based alternative to NpgsqlDbType: https://github.com/dotnet/corefx/issues/25891. We can still implement this as an Npgsql-specific feature without waiting for ADO.NET adoption.
This even makes general sense over NpgsqlDbType, allowing extension to support new types without getting stuck on NpgsqlDbType which, being an enum, isn't extensible.
Brar commentedon Dec 14, 2017
Please think about this twice.
IMO the ability to map tables as composite types is a widely unknown feature of Npgsql that provides very slim and easy ORM functionality.
I could demostrate you quite a few cases where this is actually really useful.
roji commentedon Dec 14, 2017
I agree it's a pretty cool feature, it's just that it really seems really, really rarely used.
To be frank, changing the type loading system to be 100% eager would also simplify things inside Npgsql - composites are now the only types loaded lazily, and it's kind of a wart on the system. And the only reason composites are loaded lazily is because there can be so many of them, because a composite type exists per table.
One last option here, which in general I try to avoid as possible, is to simply add a parameter,
Load All Composites, which would be false by default. Thus, everything will always be loaded eagerly, and if you happen to want to use the table-as-composite feature, you'd have to add this parameter.In any case, I couldn't resist and have started working on this issue, and it's going quite well - I already have tests parsing unmapped composites as dynamic ExpandoObjects. It's a real pleasure to see and to use.
Brar commentedon Dec 14, 2017
👍
Great to hear :)
dmitry-a-morozov commentedon Dec 15, 2017
Looks good.
As I mentioned before SqlClient solves #1746 (comment) via read-write
UdtTypeNameproperty. Yes, maybe it makes standardized it on ADO.NET level.Only unfortunate thing that it will be confusing to have all: EnumType, SpecificType, DbType, PostgresType and TypeName (or UdtTypeName) on NpgsqlParameter. :(
I know that EnumType is deprecated but still adds to perception complexity.
roji commentedon Dec 15, 2017
@dmitry-a-morozov - ah, thanks for the added information: I didn't know about UdtTypeName...
I think if we implement
UdtTypeName(orTypeName) we can safely obsolete SpecificType as well. I hope that will at least reduce the confusion around those two (which we can also remove in a later release). Then there's the question of PostgresType, which we proposed to add to NpgsqlParameter only for derivation (so as an "output property"), although we could also allow it to be set by the user to determine the outgoing parameter name as well.Yeah, we definitely will have to add some clear documentation on all this.
dmitry-a-morozov commentedon Dec 15, 2017
I think
UdtTypeNameis more likeDataTypeNamee.g. full data type name as in database including schema. Where isSpecificTypeis CLR mapped type. They are complementary not redundant, correct?Having only
UdtTypeNameis sufficient for my purposes, no need forPostgresType.Interesting code snippet related to the discussion.
http://referencesource.microsoft.com/#System.Data/System/Data/SqlClient/SqlCommandBuilder.cs,160
roji commentedon Dec 15, 2017
@dmitry-a-morozov yes,
UdtTypeNameis a string property with a full database-specific type name, whereSpecificTypeis an CLR type that has already been mapped to the type name. But at the end of the day, what we need is a simple way for users to tell Npgsql which database type to send. If there's a simple, direct string-based way to do so (i.e.UdtTypeName), thenSpecificTypereally provides no additional value. It's just another layer of indirection, since in any case the CLR type has to have already been mapped to... the string name of the database type...So I'm leaning towards obsoleting
SpecificTypein favor ofUdtTypeName(or whatever we choose to call it).roji commentedon Dec 15, 2017
Thanks for the code snipped from SqlClient. I'm thinking to do thinks a little bit different, and to allow specifying the type as a string for all database types, not just UDT ones (which seem to correspond PostgreSQL composites). I don't see any reason to restrict this mechanism only to composites, and it could actually be useful for other types added by extensions, and which aren't present in the closed NpgsqlDbType enum. This also means that the property should be named something like
TypeNamerather thanUdtTypeName, because it isn't necessarily related to user-defined types.dmitry-a-morozov commentedon Dec 15, 2017
Makes sense. Will
TypeNamebe mandatory for parameters of simple types like int, decimal, string etc?roji commentedon Dec 16, 2017
@dmitry-a-morozov definitely not - that would break all existing Npgsql code. The idea would be for
TypeNameto be (yet) another way for users to specify which type they want to write. Npgsql would look in the following order:TypeNameis set, use thatNpgsqlDbTypeis set, use that. If it's set to composite or enum, also look atSpecificType(although this would be obsoleted)DbTypeis set, use that (for database-independent programming)Value(this actually works quite well for mapped composites and enums, soSpecificTypeis actually kinda useless).1 remaining item
roji commentedon Dec 17, 2017
In PostgreSQL, array names are simply the type name with an underscore prepended to it. So an array of text is
_text, and an array of your custom enum my_enum is_my_enum.FYI as we're adding the
PostgresTypeproperty toNpgsqlParameter, I'm considering allowing that to be used as well to specify the type of the outgoing parameter. If we do that, thenTypeNamewould be a thin wrapper which simply looks up thePostgresTypeby name and sets it.Support for unmapped composites
roji commentedon Jan 18, 2018
The bulk of the work has been merged - along with some other changes to the type mapping system.
Load All Compositesparameter is set.Leaving open this issue for the following remaining work:
Expose rich information about composite and enum types from PostgresType (Expose rich information about composite and enum types from PostgresType #1779).Rebase and adapt to the recent commits in context with npgsql#1746
roji commentedon May 12, 2018
Split off the remaining name translation work to #1929