Fix operator handling and date functions for EF #287

Merged
merged 6 commits into from Jul 20, 2014

Projects

None yet

2 participants

@Emill
Member
Emill commented Jul 20, 2014

Hi. This fixes several issues:

  1. Handling of binary and unary operators hasn't been good. Parentheses are added more than needed and sometimes they are missing, which results in random bugs such as http://stackoverflow.com/questions/21908464/wrong-query-generated-by-postgresql-provider-to-entity-framework-for-contains-an, #282 and a lot more places I've noticed.
    This fix is a complete rewrite of how operators work. Now there is a table of all operators, their precedence, associativity and if they are postfix, prefix or binary. The table and helper classes are used to build OperatorExpressions, a new subclass of VisitedExpression. When they are written out as SQL, the parentheses are added only if needed, according to PostgreSQL's parsing rules/operator precedence table at the bottom of http://www.postgresql.org/docs/9.3/static/sql-syntax-lexical.html. This should improve the readability of the SQL queries. Smart negation is also implemented like before, so a double NOT expression will be removed for example. NOT a < b is mapped as a >= b etc.
  2. The DateDiff functions didn't work at all before. Now I've implemented them to match the behaviour of SQL Server as much as possible. Note that using DateTime variables in LINQ won't work yet since they are parameterized, and the parameter handling in Npgsql doesn't cast the time string to a timestamp. See https://groups.google.com/forum/#!topic/npgsql-dev/Fn08-RA-OHw. Inline "new DateTime(...)" works though, or using column names.
  3. When you imported a table with interval data type in Visual Studio's model designer, you got an error message telling the precision 65535 is wrong (must be between 0 and 6). This was due to an error in an SQL query in the manifest that read out the precision from pg_attribute table. I also added support for timetz.
  4. Literal TimeSpan objects in LINQ (created by new TimeSpan(...)) is now written as interval data type instead of time, to support time spans of more than 24 hours. EF Migration maps TimeSpan to interval so I think this is the correct thing to do.
  5. Add support for DateTimeOffset/timestamptz casting (for example from NULL).
  6. Add support for literal bytes and sbytes (cast to int2).
Emill added some commits Jul 18, 2014
@Emill Emill EF: Implement an operator table with precedences, instead of adding p…
…arentheses in an ad-hoc way thas sometimes generates wrong SQL.
e4735b5
@Emill Emill Merge branch 'master' of https://github.com/npgsql/Npgsql into operat…
…or-precedence-ef
608092b
@Emill Emill Implement the DateDiff functions. The old implementation wasn't even …
…close to do the right thing. Also add some operator tests.
1e4094e
@Emill Emill Add timetz mapping for EF in provider manifest.
Fix precision extraction for interval type in NpgsqlSchema.ssdl.
Add support for DateTimeOffset/timestamptz casting in EF.
Add support for literal byte values (cast to int2) in EF.
Change literal time values to interval values in EF.
Add tests for operators, date functions and data types.
49267ab
@Emill Emill merged commit 4c3fa80 into npgsql:master Jul 20, 2014

1 check passed

default Finished TeamCity Build Npgsql :: All : Running
Details
@franciscojunior franciscojunior added this to the 2.2 milestone Jul 21, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment