How to handle the absence of named parameters in a data store
We are writing an Entity Provider for a database that does not support named parameters.
By overriding RelationalSqlGenerationHelper.GenerateParameterName() to simply append "?" to the StringBuilder we are able to execute simple queries. However there are many cases where an ExpressionTranslator changes an expression that has a single parameter to one that has 2 or 3 parameters.
For example myString.Trim(x) translates to LTrim(RTrim(object, ?), ?) AS "Trim"
When using named parameters that is not an issue since the same parameter is used for all instances. If however we do not have named parameters we must generate additional parameters to match the modified expression.
The obvious solution here is to clone the current parameter and add the new instance(s) to the DbCommand.Parameters collection immediately before, or after, the current parameter.
Is there a way to obtain the DbCommand.Parameters collection from within the Expression Translator and to identify which parameter the expression is related to?
If not, could you describe an alternate way to handle the lack of named parameters in this situation. It would also be helpful if you could let us know of any other areas of EF Core that may be effected by the lack of named parameters.
EF Core version: 2.1 Database Provider: Teradata.EntityFrameworkCore Operating system: Windows 10 IDE: Visual Studio 2017 15.7
@MikeDempseyFL PostgreSQL also doesn't support named parameters. I believe that the way it is handled there is at the ADO.NET level where the provider re-writes the named parameters at the last minute. @roji Can probably elaborate on this. We discussed that this is something that could be done by EF, probably again by re-writing the named parameters immediately before calling down to ADO.NET, but we'd like to hear @roji's opinion on that too.
It's true that PostgreSQL supports only positional parameters ($1, $2), and that Npgsql currently rewrites SQL (at the ADO driver level) to convert from named parameters (e.g. @p) to positional ones PostgreSQL understands ($1).
However, I don't think this is a good thing. The main reason for this state of affairs is backwards compatibility and an (in my opinion) mistaken view of portability. Before I started working on Npgsql, the decision was made to support named parameters (both@p and :p) in the belief that it's expected or required, and with the idea that it would help portability (e.g. programs written for SQL Server can run as-is on PostgreSQL).
I personally believe that a database driver should not ever need to parse or rewrite SQL given to it - it's complicated (SQL is hard to parse), brittle and bad for perf. https://github.com/dotnet/corefx/issues/27075 is about adding a portable API for generating parameter placeholders, and https://github.com/npgsql/npgsql/issues/1042 is about having a mode where Npgsql does parse/rewrite. If the latter is ever implemented, it will require the SQL to contain PostgreSQL positional parameters ($1, $2).
To sum it up, although Npgsql doesn't currently require support for positional arguments from EF Core, IMHO it's quite important for them to be supported. On the other hand, this issue seems to be about another kind of positional parameters: unlike PostgreSQL's parameter which have numbers and can therefore be repeated (you can specify $1 multiple times in the query SQL), a simple question mark is just a question mark...
Maybe calling them 'named' parameters was a bad idea. My definition of 'named' parameters would include 'numbered' parameters - if each one can be referenced multiple times.
In our case I am referring to 'positional' parameters. If there are 20 parameter markers in the SQL then you must define 20 parameters - even if they all refer to the same value. They are positional in that Parameters[0] is used for the first marker, Parameters[1] for the second, etc.
It rather sounds like post processing the SQL may be the only way to do this. Assign names within our EF provider but then replace each one with a ? and keep track of them as we go so that we can clone a duplicate when we hit it and add the clone into the Parameters collection at the 'current count' we have reached so far.
I hope that's not the only way to go since that would be a nightmare.
Maybe calling them 'named' parameters was a bad idea. My definition of 'named' parameters would include 'numbered' parameters - if each one can be referenced multiple times.
Yeah, I think I misunderstood your question a little. So you're saying that your database only recognizes single question marks as parameter placeholders, and does not support any notion of referencing a specific parameter in a list (either via a name or via an index)? If so, then the situation really is quite different from PostgreSQL, where placeholders do reference parameters (by index and not by name).
If this is indeed the situation, then I agree that you need to somehow generate an ADO parameter list that contains the same data twice (in two parameters). I'm not very familiar with the plumbing here, but you'll probably have to make the changes in your provider's QuerySqlGenerator - see the VisitParameter() method, which you will probably need to override. There are other places in the query generator which handle parameters and which you may need to override (e.g. see GenerateFromSql()).
The EF Core team can probably provide more/better guidance.
Thanks. I think that's the solution. I have overridden VisitParameter and VisitPropertyParameter in our QuerySqlGenerator so that they always generate a parameter. I don't think I need to override GenerateFromSql because that has no 'short cutting' of parameter generation to begin with.