Dapper icon indicating copy to clipboard operation
Dapper copied to clipboard

Enclosing column names in square brackets breaks Dapper.Contrib in PostgresSQL

Open oliverw opened this issue 11 years ago • 35 comments

Enclosing column names in square brackets completely breaks Dapper.Contrib in PostgresSQL.

insert into products([Title], [ManufacturerId],) values((('Vibe P1')::text), ((1)::int4)) RETURNING Id

oliverw avatar Sep 08 '15 15:09 oliverw

@oliverw Could you please explain a bit more what you are trying to do. The dapper.contrib insert extension has been tested quite a lot I believe.

johandanforth avatar Sep 08 '15 18:09 johandanforth

Well, the queries above, and the one below have been generated by Dapper.Contrib (latest version from NuGet). When running the query below through NpgSql 3.0 (though happens on 2.x as well) it results in the following error (sorry for the German error message, I have yet to figure out how to force the PGSQL error log to be in English):

2015-09-16 12:24:17 CEST    ANWEISUNG   update rss_product_feeds set [Url] = $1, [Region] = $2, [Marketplace] = $3, [Name] = $4, [Created] = $5, [LastProcessed] = $6, [TimesProcessed] = $7 where [Id] = $8    
2015-09-16 12:24:17 CEST    FEHLER  Syntaxfehler bei '[' bei Zeichen 30

If I patch SqlMapperExtensions.cs to omit generating the square brackets, everything works as expected.

oliverw avatar Sep 16 '15 10:09 oliverw

@oliverw sanity check here: I see an extra comma in your column list - you sure that's not a contributing factor here...or is that a mis-paste?

NickCraver avatar Oct 10 '15 13:10 NickCraver

See mrjoltcola's explanation for a detailed understanding of this issue.

This square bracket issue causes Dapper.Contrib to not work with MySQL, and as oliverw noted, PostgresSQL.

As a solution, I suggest augmenting the ISqlAdapter interface with a columnList creation method, such that the column names are quoted with appropriate delimiters for each SQL provider where appropriate.

AndrewSmart avatar Oct 12 '15 19:10 AndrewSmart

Just for history, #60 and #298 introduced the non-universal escaping. Something similar almost happened with Dapper.Rainbow in #167 too, but @mgravell reverted after I pointed out it broke compatibility. Whatever solution is decided on here should probably be applied to that code as well, since it currently doesn't allow escaping.

tms avatar Oct 12 '15 19:10 tms

Thanks for the pointers @tms So double quote seems to be the right way of escaping column names on most database platforms but I'll spend some time today and dig into it. This must be solved.

johandanforth avatar Oct 13 '15 07:10 johandanforth

Seems that the SQL-99 standard specifies that double quote (") is used to delimit identifiers, but at least MySQL has to be in ANSI mode and SQL Server only supports it when QUOTED_IDENTIFIER is ON. The contrib extensions for Insert, Update and Delete all use brackets, not just the Insert. So the question is, should we replace the brackets with double quotes everywhere and refer to the standard (and risk breaking working code out there)? Or perhaps add some other way for devs to supply a escape character, with a delegate or other method?

johandanforth avatar Oct 13 '15 10:10 johandanforth

Introducing delimiter of a specific dialect was probably a mistake, so forcing another, even if standard, would probably be a mistake as well. Specifying delimiters in the ISqlAdapter implementation as suggested by @AndrewSmart could be the way to go as brackets may be used in sql server regardless of the QUOTED_IDENTIFIER setting. Don't know about MySql ANSI mode though. I guess the ansi mode setting could be read (SELECT @@SESSION.sql_mode; ?) in a specific MySql ISqlAdapter adapter and eventually be cached on the connection instance. The impact of breaking working code may be somewhat limited as the current state of affairs seem to have broken some code already. Brackets would be the default as it is now (as SqlServerAdapter is default) and the other adapters could use double quote (except the specific MySql adapter, then?).

Tewr avatar Oct 13 '15 12:10 Tewr

@Tewr In MySQL backticks as the identifier delimiter will always work regardless of its ANSI_QUOTES setting.

AndrewSmart avatar Oct 13 '15 17:10 AndrewSmart

Yes @Tewr and @AndrewSmart , specifying delimiters in ISqlAdapter implementations feels right to me too. The sql server adapter could keep using brackets as you wrote. Not sure at all about the proper way to handle mysql, but from what I can google, http://dev.mysql.com/doc/refman/5.6/en/identifiers.html says a "backtick" should be used. And for sqllite and postgressql use double quotes. How bout that?

I'll take a look at how to implement this in all parts needed, Insert(), Update() and Delete() and their corresponding async methods.

johandanforth avatar Oct 13 '15 17:10 johandanforth

@johandanforth Agreed, that seems like the right approach. I was going to go ahead and implement something yesterday, but I wanted to confirm first with you, @NickCraver, and @mgravell there was no concerns with changing that interface. If you have the free time though I'll defer to you :)

tms avatar Oct 13 '15 17:10 tms

@johandanforth Sounds good to me. Though thinking back to what @tms said in #167, I think it would be optimal to not escape every identifier in pursuit of Dapper's maximal performance goal. I'd suggest to have users either not use reserved keywords, or use another data annotation such as [Table("Tablename", EscapeIdentifiers=true)]. That data annotation could drive the escaping as implemented in ISqlAdapter implementations.

AndrewSmart avatar Oct 13 '15 17:10 AndrewSmart

@AndrewSmart hmmm, yeah, it will impact performance. Not much, but still. I tend to agree with you, but if the default would be to not escape names, that would affect some existing code out there which depend on the existing brackets around column names.

johandanforth avatar Oct 13 '15 18:10 johandanforth

The difference is probably negligible anyway, since there's already string concatenation being done to build the queries (and AFAIK all escaping methods would just be wrapping the identifiers, so nothing more complex). Could determine which types have parameters needing escaping the first time they're encountered and cache the result, but that seems like it's probably overkill.

On the flip side, if it actually is an issue I'm sure someone will complain about it after the implementation is in place, so assuming no one proactively profiles it, it can always be handled when it comes up.

tms avatar Oct 13 '15 18:10 tms

@tms Well, the string concatenation would be pertinent to Dapper itself, but their are also performance effects in the SQL engine such as logging. On a large project in the past w/ MySQL I did see a useful performance benefit after taking universal quoting out (millions of inserts, hundreds of columns). Though then I was naive and not using prepared statements... That's what struck up my thought :)

But you're right too about the flip-side. So thank you for working on this!

AndrewSmart avatar Oct 13 '15 18:10 AndrewSmart

PR #352 now also contains fixes for this issue

johandanforth avatar Oct 14 '15 12:10 johandanforth

We would greatly appreciate a release of Dapper.Contrib with the fix for PostgreSQL.

rusio avatar Oct 19 '15 14:10 rusio

Is there a chance that Dapper.Contrib is released on NuGet in the near future? This is a blocker for anyone using Dapper.Contrib with PostgreSQL.

rusio avatar Nov 11 '15 11:11 rusio

I have same ploblem on MySQL. Please update package on Nuget. Thanks

hoquangthaiholy avatar Nov 15 '15 08:11 hoquangthaiholy

I compiled from latest code for who want to use right now. Download this image and rename extension to zip. (Because dithub don't support upload zip file) dapper contrib 1 44

hoquangthaiholy avatar Nov 15 '15 09:11 hoquangthaiholy