-
Notifications
You must be signed in to change notification settings - Fork 455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix ~string.IsNullOrEmpty~ string.Length mapping for SQL CE/MSSQL #4177
Open
MaceWindu
wants to merge
5
commits into
master
Choose a base branch
from
issue/4138
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8f3d93a
add test
MaceWindu 704eb27
fix build
MaceWindu 20baf75
add more tests and SQL CE/MSSQL fix for string.Length mapping
MaceWindu fed52d2
Merge branch 'master' into issue/4138
MaceWindu b5c4aae
update tests for oracle/sybase misbehavior
MaceWindu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data.Linq; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
|
||
using JetBrains.Annotations; | ||
|
||
using PN = LinqToDB.ProviderName; | ||
|
||
// ReSharper disable CheckNamespace | ||
// ReSharper disable RedundantNameQualifier | ||
|
||
namespace LinqToDB | ||
{ | ||
using Mapping; | ||
using Expressions; | ||
using Linq; | ||
using SqlQuery; | ||
using LinqToDB.Common; | ||
using Mapping; | ||
using SqlQuery; | ||
|
||
[PublicAPI] | ||
public static partial class Sql | ||
|
@@ -269,7 +271,7 @@ public static Guid NewGuid() | |
|
||
[CLSCompliant(false)] | ||
[Function("Convert", 0, 1, ServerSideOnly = true, IsPure = true, IsNullable = IsNullableType.SameAsSecondParameter)] | ||
[Function(PseudoFunctions.CONVERT, 2, 3, 1, ServerSideOnly = true, IsPure = true, IsNullable = IsNullableType.SameAsSecondParameter, Configuration = ProviderName.ClickHouse)] | ||
[Function(PseudoFunctions.CONVERT, 2, 3, 1, ServerSideOnly = true, IsPure = true, IsNullable = IsNullableType.SameAsSecondParameter, Configuration = PN.ClickHouse)] | ||
public static TTo Convert<TTo,TFrom>(TTo to, TFrom from) | ||
{ | ||
return Common.ConvertTo<TTo>.From(from); | ||
|
@@ -285,7 +287,7 @@ public static TTo Convert<TTo, TFrom>(TTo to, TFrom from, int format) | |
// TODO: v5 remove. bltoolkit legacy which duplicates Convert function above (without ServerSideOnly, but it shouldn't matter) | ||
[CLSCompliant(false)] | ||
[Function("Convert", 0, 1, IsPure = true, IsNullable = IsNullableType.SameAsSecondParameter)] | ||
[Function(PseudoFunctions.CONVERT, 2, 3, 1, ServerSideOnly = true, IsPure = true, IsNullable = IsNullableType.SameAsSecondParameter, Configuration = ProviderName.ClickHouse)] | ||
[Function(PseudoFunctions.CONVERT, 2, 3, 1, ServerSideOnly = true, IsPure = true, IsNullable = IsNullableType.SameAsSecondParameter, Configuration = PN.ClickHouse)] | ||
public static TTo Convert2<TTo,TFrom>(TTo to, TFrom from) | ||
{ | ||
return Common.ConvertTo<TTo>.From(from); | ||
|
@@ -383,6 +385,14 @@ public static TTo From<TFrom>(TFrom obj) | |
|
||
#region String Functions | ||
|
||
/// <summary> | ||
/// Returns string length in characters by calling corresponding database function as-is. | ||
/// Could result in unexpected results due to non-standard behavior of some databases. | ||
/// E.g. see LEN function notes for SQL CE or MSSQL databases. | ||
/// For more standard behavior look at <see cref="StringLength(string?)"/> function. | ||
/// </summary> | ||
/// <param name="str">String value.</param> | ||
/// <returns>String length or <c>null</c> for NULL input.</returns> | ||
[Function ( PreferServerSide = true, IsNullable = IsNullableType.SameAsFirstParameter)] | ||
[Function (PN.Access, "Len", PreferServerSide = true, IsNullable = IsNullableType.SameAsFirstParameter)] | ||
[Function (PN.Firebird, "Char_Length", PreferServerSide = true, IsNullable = IsNullableType.SameAsFirstParameter)] | ||
|
@@ -393,11 +403,41 @@ public static TTo From<TFrom>(TFrom obj) | |
[Function (PN.Informix, "CHAR_LENGTH", PreferServerSide = true, IsNullable = IsNullableType.SameAsFirstParameter)] | ||
[Function (PN.ClickHouse, "CHAR_LENGTH", PreferServerSide = true, IsNullable = IsNullableType.SameAsFirstParameter)] | ||
[Expression(PN.DB2LUW, "CHARACTER_LENGTH({0},CODEUNITS32)", PreferServerSide = true, IsNullable = IsNullableType.SameAsFirstParameter)] | ||
[return: NotNullIfNotNull(nameof(str))] | ||
public static int? Length(string? str) | ||
{ | ||
return str?.Length; | ||
} | ||
|
||
/// <summary> | ||
/// Returns string length in characters by calling corresponding database function. | ||
/// Compared to <see cref="Length(string?)"/> function, it tries to calculate length in same way as <see cref="string.Length"/> property | ||
/// for databases with non-standard length functions. | ||
/// E.g. see LEN function notes for SQL CE or MSSQL databases. | ||
/// Known issues with empty string: | ||
/// <list type="bullet"> | ||
/// <item>Oracle treats '' as NULL and returns NULL instead of 1</item> | ||
/// <item>Sybase ASE treats '' as ' ' and returns 1 instead of 1</item> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "instead of 0" |
||
/// </list> | ||
/// </summary> | ||
/// <param name="str">String value.</param> | ||
/// <returns>String length or <c>null</c> for NULL input.</returns> | ||
[Function ( "Length", PreferServerSide = true, IsNullable = IsNullableType.SameAsFirstParameter)] | ||
[Function (PN.Access, "Len", PreferServerSide = true, IsNullable = IsNullableType.SameAsFirstParameter)] | ||
[Function (PN.Firebird, "Char_Length", PreferServerSide = true, IsNullable = IsNullableType.SameAsFirstParameter)] | ||
[Expression(PN.SqlServer, "LEN(REPLACE({0},' ','.'))", PreferServerSide = true, IsNullable = IsNullableType.SameAsFirstParameter)] | ||
[Expression(PN.SqlCe, "LEN(REPLACE({0},' ','.'))", PreferServerSide = true, IsNullable = IsNullableType.SameAsFirstParameter)] | ||
[Function (PN.Sybase, "Len", PreferServerSide = true, IsNullable = IsNullableType.SameAsFirstParameter)] | ||
[Function (PN.MySql, "Char_Length", PreferServerSide = true, IsNullable = IsNullableType.SameAsFirstParameter)] | ||
[Function (PN.Informix, "CHAR_LENGTH", PreferServerSide = true, IsNullable = IsNullableType.SameAsFirstParameter)] | ||
[Function (PN.ClickHouse, "CHAR_LENGTH", PreferServerSide = true, IsNullable = IsNullableType.SameAsFirstParameter)] | ||
[Expression(PN.DB2LUW, "CHARACTER_LENGTH({0},CODEUNITS32)", PreferServerSide = true, IsNullable = IsNullableType.SameAsFirstParameter)] | ||
[return: NotNullIfNotNull(nameof(str))] | ||
public static int? StringLength(string? str) | ||
{ | ||
return str?.Length; | ||
} | ||
|
||
[Function ( PreferServerSide = true, IsNullable = IsNullableType.IfAnyParameterNullable)] | ||
[Function (PN.Access, "Mid", PreferServerSide = true, IsNullable = IsNullableType.IfAnyParameterNullable)] | ||
[Function (PN.DB2, "Substr", PreferServerSide = true, IsNullable = IsNullableType.IfAnyParameterNullable)] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"instead of 0"