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
{{ message }}
Repository navigation
Array slice expression upper bound misinterpreted as parameter #946
Since version 3 the following query has started failing for us:
context.Connection.Execute(@"
drop table if exists stuff;
create table stuff (a int primary key, b int[])");
context.Connection.Execute(@"insert into stuff values (1, array[1,2,3,4])");
var asdf = context.Connection
.Query(@"select b[2:3] as foo from stuff")
.Single();
Log.Debug("asdf = " + asdf.foo);
(using dapper)
...with the exception:
Exception: System.Exception: Parameter ':3' referenced in SQL but not found in parameter list
at Npgsql.SqlQueryParser.ParseRawQuery(String sql, Boolean standardConformantStrings, NpgsqlParameterCollection parameters, List`1 queries)
at Npgsql.NpgsqlCommand.ProcessRawQuery()
at Npgsql.NpgsqlCommand.CreateMessagesNonPrepared(CommandBehavior behavior)
at Npgsql.NpgsqlCommand.ExecuteDbDataReaderInternal(CommandBehavior behavior)
at Dapper.SqlMapper.<QueryImpl>d__11`1.MoveNext() in d:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1562
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in d:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1452
at Dapper.SqlMapper.Query(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in d:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1391
It looks like npgsql is parsing the query and misinterpreting the :3 part of the array slice expression as a parameter name.
Workaround:
Pass the upper bound in as a parameter using @, e.g.
var asdf = context.Connection
.Query(@"select b[2:@Three] as foo from stuff", new { Three = 3 })
.Single();
Previously parameter placeholders not found in the parameter collection were left as-is in the query, and therefore this issue did not happen. Now an exception is thrown instead. Let's make it illegal for a parameter name to start with a digit. That should probably be good for most people.
@Emill sounds like a good workaround for this particular case, but in general I really hate the fact that Npgsql parses the command text. I would be very happy if we could have a connection string parameter that turns off the parsing altogether. This would mean users have to use PostgreSQL placeholders ($1, $2), and they need some sort of alternative in order to do command batching (see https://github.com/dotnet/corefx/issues/3688 for a long-term view). This is the only good way forward IMHO, everything else is going to be a hack.
But in the short term yes, making it illegal to start parameter names with digits should be OK.
BTW I think I've just found another similar issue:
ALTER TABLE foo ADD COLUMN created_on timestamp(6) with time zone NOT NULL DEFAULT @Ts;
(assuming have passed it a valid Ts param)
Produces:
Unhandled Exception: Npgsql.NpgsqlException: 42P02: there is no parameter $1
at Npgsql.NpgsqlConnector.DoReadSingleMessage(DataRowLoadingMode dataRowLoadingMode, Boolean returnNullForAsyncMessage, Boolean isPrependedMessage)
at Npgsql.NpgsqlConnector.ReadSingleMessage(DataRowLoadingMode dataRowLoadingMode, Boolean returnNullForAsyncMessage)
at Npgsql.NpgsqlConnector.SkipUntil(BackendMessageCode stopAt1, BackendMessageCode stopAt2)
at Npgsql.NpgsqlDataReader.SkipUntil(BackendMessageCode stopAt1, BackendMessageCode stopAt2)
at Npgsql.NpgsqlDataReader.NextResultInternal()
at Npgsql.NpgsqlDataReader.NextResult()
at Npgsql.NpgsqlDataReader.Init()
at Npgsql.NpgsqlCommand.Execute(CommandBehavior behavior)
at Npgsql.NpgsqlCommand.ExecuteNonQueryInternal()
at Npgsql.NpgsqlCommand.ExecuteNonQuery()
at Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, CommandDefinition& command, Action`2 paramReader) in d:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 3321
at Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, CommandDefinition& command) in d:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1319
at Dapper.SqlMapper.Execute(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable`1 commandTimeout, Nullable`1 commandType) in d:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1194
I've had to resort to string replacement, which works, but is not ideal. This is a slightly less obscure scenario than the original, and may pose more of a problem.
This change breaks nearly all of my code as I'm used to generate numbered parameter names. Isn't a starting "@" or ":" in SQL command enough to detect a parameter?
Activity
Emill commentedon Jan 14, 2016
Previously parameter placeholders not found in the parameter collection were left as-is in the query, and therefore this issue did not happen. Now an exception is thrown instead. Let's make it illegal for a parameter name to start with a digit. That should probably be good for most people.
roji commentedon Jan 14, 2016
@Emill sounds like a good workaround for this particular case, but in general I really hate the fact that Npgsql parses the command text. I would be very happy if we could have a connection string parameter that turns off the parsing altogether. This would mean users have to use PostgreSQL placeholders ($1, $2), and they need some sort of alternative in order to do command batching (see https://github.com/dotnet/corefx/issues/3688 for a long-term view). This is the only good way forward IMHO, everything else is going to be a hack.
But in the short term yes, making it illegal to start parameter names with digits should be OK.
shanlun commentedon Feb 2, 2016
BTW I think I've just found another similar issue:
(assuming have passed it a valid Ts param)
Produces:
I've had to resort to string replacement, which works, but is not ideal. This is a slightly less obscure scenario than the original, and may pose more of a problem.
Parameter names can no longer start with digits
roji commentedon Apr 10, 2016
@shanlun your problem isn't really related to this issue - AFAIK PostgreSQL doesn't support parameters in DDL, see http://www.postgresql.org/message-id/[email protected]
shanlun commentedon Apr 10, 2016
Makes sense, thanks for the info.
Parameter names can no longer start with digits
dstarosta commentedon May 2, 2016
This change is going to break some ORM frameworks that automatically generate numbered parameters. Peta POCO and NPOCO are some of them.
roji commentedon May 2, 2016
@dstarosta, you know for a fact that these two ORMs generate parameter names that start with digits?
lerrand commentedon May 2, 2016
@roji NPoco does currently.
dstarosta commentedon May 3, 2016
@roji Yes, I verified that in their source code.
udoliess commentedon May 3, 2016
This change breaks nearly all of my code as I'm used to generate numbered parameter names. Isn't a starting "@" or ":" in SQL command enough to detect a parameter?
roji commentedon May 3, 2016
@udoliess don't worry, later today I'm publishing 3.0.7 which undoes this change, hang on.
roji commentedon May 3, 2016
FYI, 3.0.7 has been released and undoes this fix - parameter names can start with digits. #1056 has been applied instead.