- 兼容 SqlServer varchar/nvarchar 表达式解析,分别解析为:N'' 和 '';

This commit is contained in:
28810
2019-11-20 03:34:55 +08:00
parent b338efc8a2
commit efdc7c8c5d
27 changed files with 440 additions and 86 deletions

View File

@ -1,4 +1,5 @@
using FreeSql.Internal;
using FreeSql.Internal.Model;
using MySql.Data.MySqlClient;
using SafeObjectPool;
using System;
@ -28,7 +29,7 @@ namespace FreeSql.MySql
}
}
static DateTime dt1970 = new DateTime(1970, 1, 1);
public override object AddslashesProcessParam(object param, Type mapType)
public override object AddslashesProcessParam(object param, Type mapType, ColumnInfo mapColumn)
{
if (param == null) return "NULL";
if (mapType != null && mapType != param.GetType() && (param is IEnumerable == false || mapType.IsArrayOrList()))
@ -51,7 +52,7 @@ namespace FreeSql.MySql
{
var sb = new StringBuilder();
var ie = param as IEnumerable;
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z, mapType));
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z, mapType, mapColumn));
return sb.Length == 0 ? "(NULL)" : sb.Remove(0, 1).Insert(0, "(").Append(")").ToString();
}
return string.Concat("'", param.ToString().Replace("'", "''"), "'");

View File

@ -101,11 +101,11 @@ namespace FreeSql.MySql
if (objType == null) objType = callExp.Method.DeclaringType;
if (objType != null || objType.IsArrayOrList())
{
tsc?.SetMapTypeTmp(null);
tsc.SetMapColumnTmp(null);
var args1 = getExp(callExp.Arguments[argIndex]);
var oldMapType = tsc?.SetMapTypeReturnOld(tsc?.mapTypeTmp);
var oldMapType = tsc.SetMapTypeReturnOld(tsc.mapTypeTmp);
var left = objExp == null ? null : getExp(objExp);
tsc.SetMapTypeReturnOld(oldMapType);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
switch (callExp.Method.Name)
{
case "Contains":

View File

@ -1,4 +1,5 @@
using System;
using FreeSql.Internal.Model;
using System;
using System.Collections.Generic;
using System.Text;
@ -43,7 +44,7 @@ namespace FreeSql.Odbc.Default
public virtual char QuoteSqlNameRight => ']';
public virtual string FieldSql(Type type, string columnName) => columnName;
public virtual string UnicodeStringRawSql(object value) => value == null ? "NULL" : string.Concat("N'", value.ToString().Replace("'", "''"), "'");
public virtual string UnicodeStringRawSql(object value, ColumnInfo mapColumn) => value == null ? "NULL" : string.Concat("N'", value.ToString().Replace("'", "''"), "'");
public virtual string DateTimeRawSql(object value)
{
if (value == null) return "NULL";

View File

@ -1,4 +1,5 @@
using FreeSql.Internal;
using FreeSql.Internal.Model;
using SafeObjectPool;
using System;
using System.Collections;
@ -29,7 +30,7 @@ namespace FreeSql.Odbc.Default
OdbcAdapter Adapter => (_util == null ? FreeSqlOdbcGlobalExtensions.DefaultOdbcAdapter : _util._orm.GetOdbcAdapter());
static DateTime dt1970 = new DateTime(1970, 1, 1);
public override object AddslashesProcessParam(object param, Type mapType)
public override object AddslashesProcessParam(object param, Type mapType, ColumnInfo mapColumn)
{
if (param == null) return "NULL";
if (mapType != null && mapType != param.GetType() && (param is IEnumerable == false || mapType.IsArrayOrList()))
@ -37,7 +38,7 @@ namespace FreeSql.Odbc.Default
if (param is bool || param is bool?)
return (bool)param ? 1 : 0;
else if (param is string)
return Adapter.UnicodeStringRawSql(param);
return Adapter.UnicodeStringRawSql(param, mapColumn);
else if (param is char)
return string.Concat("'", param.ToString().Replace("'", "''"), "'");
else if (param is Enum)
@ -52,7 +53,7 @@ namespace FreeSql.Odbc.Default
{
var sb = new StringBuilder();
var ie = param as IEnumerable;
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z, mapType));
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z, mapType, mapColumn));
return sb.Length == 0 ? "(NULL)" : sb.Remove(0, 1).Insert(0, "(").Append(")").ToString();
}
return string.Concat("'", param.ToString().Replace("'", "''"), "'");

View File

@ -108,11 +108,11 @@ namespace FreeSql.Odbc.Default
if (objType == null) objType = callExp.Method.DeclaringType;
if (objType != null || objType.IsArrayOrList())
{
tsc?.SetMapTypeTmp(null);
tsc.SetMapColumnTmp(null);
var args1 = getExp(callExp.Arguments[argIndex]);
var oldMapType = tsc?.SetMapTypeReturnOld(tsc?.mapTypeTmp);
var oldMapType = tsc.SetMapTypeReturnOld(tsc.mapTypeTmp);
var left = objExp == null ? null : getExp(objExp);
tsc.SetMapTypeReturnOld(oldMapType);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
switch (callExp.Method.Name)
{
case "Contains":

View File

@ -1,4 +1,5 @@
using FreeSql.Internal;
using FreeSql.Internal.Model;
using SafeObjectPool;
using System;
using System.Collections;
@ -28,7 +29,7 @@ namespace FreeSql.Odbc.MySql
}
}
static DateTime dt1970 = new DateTime(1970, 1, 1);
public override object AddslashesProcessParam(object param, Type mapType)
public override object AddslashesProcessParam(object param, Type mapType, ColumnInfo mapColumn)
{
if (param == null) return "NULL";
if (mapType != null && mapType != param.GetType() && (param is IEnumerable == false || mapType.IsArrayOrList()))
@ -49,7 +50,7 @@ namespace FreeSql.Odbc.MySql
{
var sb = new StringBuilder();
var ie = param as IEnumerable;
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z, mapType));
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z, mapType, mapColumn));
return sb.Length == 0 ? "(NULL)" : sb.Remove(0, 1).Insert(0, "(").Append(")").ToString();
}
return string.Concat("'", param.ToString().Replace("'", "''"), "'");

View File

@ -101,11 +101,11 @@ namespace FreeSql.Odbc.MySql
if (objType == null) objType = callExp.Method.DeclaringType;
if (objType != null || objType.IsArrayOrList())
{
tsc?.SetMapTypeTmp(null);
tsc.SetMapColumnTmp(null);
var args1 = getExp(callExp.Arguments[argIndex]);
var oldMapType = tsc?.SetMapTypeReturnOld(tsc?.mapTypeTmp);
var oldMapType = tsc.SetMapTypeReturnOld(tsc.mapTypeTmp);
var left = objExp == null ? null : getExp(objExp);
tsc.SetMapTypeReturnOld(oldMapType);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
switch (callExp.Method.Name)
{
case "Contains":

View File

@ -1,4 +1,5 @@
using FreeSql.Internal;
using FreeSql.Internal.Model;
using SafeObjectPool;
using System;
using System.Collections;
@ -27,7 +28,7 @@ namespace FreeSql.Odbc.Oracle
}
}
static DateTime dt1970 = new DateTime(1970, 1, 1);
public override object AddslashesProcessParam(object param, Type mapType)
public override object AddslashesProcessParam(object param, Type mapType, ColumnInfo mapColumn)
{
if (param == null) return "NULL";
if (mapType != null && mapType != param.GetType() && (param is IEnumerable == false || mapType.IsArrayOrList()))
@ -48,7 +49,7 @@ namespace FreeSql.Odbc.Oracle
{
var sb = new StringBuilder();
var ie = param as IEnumerable;
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z, mapType));
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z, mapType, mapColumn));
return sb.Length == 0 ? "(NULL)" : sb.Remove(0, 1).Insert(0, "(").Append(")").ToString();
}
return string.Concat("'", param.ToString().Replace("'", "''"), "'");

View File

@ -101,11 +101,11 @@ namespace FreeSql.Odbc.Oracle
if (objType == null) objType = callExp.Method.DeclaringType;
if (objType != null || objType.IsArrayOrList())
{
tsc?.SetMapTypeTmp(null);
tsc.SetMapColumnTmp(null);
var args1 = getExp(callExp.Arguments[argIndex]);
var oldMapType = tsc?.SetMapTypeReturnOld(tsc?.mapTypeTmp);
var oldMapType = tsc.SetMapTypeReturnOld(tsc.mapTypeTmp);
var left = objExp == null ? null : getExp(objExp);
tsc.SetMapTypeReturnOld(oldMapType);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
switch (callExp.Method.Name)
{
case "Contains":

View File

@ -1,4 +1,5 @@
using FreeSql.Internal;
using FreeSql.Internal.Model;
using SafeObjectPool;
using System;
using System.Collections;
@ -29,7 +30,7 @@ namespace FreeSql.Odbc.PostgreSQL
}
static DateTime dt1970 = new DateTime(1970, 1, 1);
public override object AddslashesProcessParam(object param, Type mapType)
public override object AddslashesProcessParam(object param, Type mapType, ColumnInfo mapColumn)
{
if (param == null) return "NULL";
if (mapType != null && mapType != param.GetType() && (param is IEnumerable == false || mapType.IsArrayOrList()))
@ -50,7 +51,7 @@ namespace FreeSql.Odbc.PostgreSQL
{
var sb = new StringBuilder();
var ie = param as IEnumerable;
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z, mapType));
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z, mapType, mapColumn));
return sb.Length == 0 ? "(NULL)" : sb.Remove(0, 1).Insert(0, "(").Append(")").ToString();
}
return string.Concat("'", param.ToString().Replace("'", "''"), "'");

View File

@ -130,11 +130,11 @@ namespace FreeSql.Odbc.PostgreSQL
if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]";
return $"(case when {left} is null then 0 else array_length({left},1) end > 0)";
case "Contains":
tsc?.SetMapTypeTmp(null);
tsc.SetMapColumnTmp(null);
var args1 = getExp(callExp.Arguments[argIndex]);
var oldMapType = tsc?.SetMapTypeReturnOld(tsc?.mapTypeTmp);
var oldMapType = tsc.SetMapTypeReturnOld(tsc.mapTypeTmp);
left = objExp == null ? null : getExp(objExp);
tsc.SetMapTypeReturnOld(oldMapType);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
//判断 in 或 array @> array
if (left.StartsWith("array[") || left.EndsWith("]"))
return $"{args1} in ({left.Substring(6, left.Length - 7)})";

View File

@ -1,9 +1,11 @@
using FreeSql.Internal;
using FreeSql.Internal.Model;
using SafeObjectPool;
using System;
using System.Collections;
using System.Data.Common;
using System.Data.Odbc;
using System.Linq;
using System.Text;
using System.Threading;
@ -26,8 +28,10 @@ namespace FreeSql.Odbc.SqlServer
}
}
}
static DateTime dt1970 = new DateTime(1970, 1, 1);
public override object AddslashesProcessParam(object param, Type mapType)
string[] ncharDbTypes = new[] { "NVARCHAR", "NCHAR", "NTEXT" };
public override object AddslashesProcessParam(object param, Type mapType, ColumnInfo mapColumn)
{
if (param == null) return "NULL";
if (mapType != null && mapType != param.GetType() && (param is IEnumerable == false || mapType.IsArrayOrList()))
@ -35,7 +39,11 @@ namespace FreeSql.Odbc.SqlServer
if (param is bool || param is bool?)
return (bool)param ? 1 : 0;
else if (param is string)
{
if (mapColumn != null && mapColumn.CsType.NullableTypeOrThis() == typeof(string) && ncharDbTypes.Any(a => mapColumn.Attribute.DbType.Contains(a)) == false)
return string.Concat("'", param.ToString().Replace("'", "''"), "'");
return string.Concat("N'", param.ToString().Replace("'", "''"), "'");
}
else if (param is char)
return string.Concat("'", param.ToString().Replace("'", "''"), "'");
else if (param is Enum)
@ -58,7 +66,7 @@ namespace FreeSql.Odbc.SqlServer
{
var sb = new StringBuilder();
var ie = param as IEnumerable;
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z, mapType));
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z, mapType, mapColumn));
return sb.Length == 0 ? "(NULL)" : sb.Remove(0, 1).Insert(0, "(").Append(")").ToString();
}
return string.Concat("'", param.ToString().Replace("'", "''"), "'");

View File

@ -105,11 +105,11 @@ namespace FreeSql.Odbc.SqlServer
if (objType == null) objType = callExp.Method.DeclaringType;
if (objType != null || objType.IsArrayOrList())
{
tsc?.SetMapTypeTmp(null);
tsc.SetMapColumnTmp(null);
var args1 = getExp(callExp.Arguments[argIndex]);
var oldMapType = tsc?.SetMapTypeReturnOld(tsc?.mapTypeTmp);
var oldMapType = tsc.SetMapTypeReturnOld(tsc.mapTypeTmp);
var left = objExp == null ? null : getExp(objExp);
tsc.SetMapTypeReturnOld(oldMapType);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
switch (callExp.Method.Name)
{
case "Contains":

View File

@ -1,4 +1,5 @@
using FreeSql.Internal;
using FreeSql.Internal.Model;
using Oracle.ManagedDataAccess.Client;
using SafeObjectPool;
using System;
@ -27,7 +28,7 @@ namespace FreeSql.Oracle
}
}
static DateTime dt1970 = new DateTime(1970, 1, 1);
public override object AddslashesProcessParam(object param, Type mapType)
public override object AddslashesProcessParam(object param, Type mapType, ColumnInfo mapColumn)
{
if (param == null) return "NULL";
if (mapType != null && mapType != param.GetType() && (param is IEnumerable == false || mapType.IsArrayOrList()))
@ -48,7 +49,7 @@ namespace FreeSql.Oracle
{
var sb = new StringBuilder();
var ie = param as IEnumerable;
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z, mapType));
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z, mapType, mapColumn));
return sb.Length == 0 ? "(NULL)" : sb.Remove(0, 1).Insert(0, "(").Append(")").ToString();
}
return string.Concat("'", param.ToString().Replace("'", "''"), "'");

View File

@ -101,11 +101,11 @@ namespace FreeSql.Oracle
if (objType == null) objType = callExp.Method.DeclaringType;
if (objType != null || objType.IsArrayOrList())
{
tsc?.SetMapTypeTmp(null);
tsc.SetMapColumnTmp(null);
var args1 = getExp(callExp.Arguments[argIndex]);
var oldMapType = tsc?.SetMapTypeReturnOld(tsc?.mapTypeTmp);
var oldMapType = tsc.SetMapTypeReturnOld(tsc.mapTypeTmp);
var left = objExp == null ? null : getExp(objExp);
tsc.SetMapTypeReturnOld(oldMapType);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
switch (callExp.Method.Name)
{
case "Contains":

View File

@ -1,4 +1,5 @@
using FreeSql.Internal;
using FreeSql.Internal.Model;
using Newtonsoft.Json.Linq;
using Npgsql;
using SafeObjectPool;
@ -30,7 +31,7 @@ namespace FreeSql.PostgreSQL
}
static DateTime dt1970 = new DateTime(1970, 1, 1);
public override object AddslashesProcessParam(object param, Type mapType)
public override object AddslashesProcessParam(object param, Type mapType, ColumnInfo mapColumn)
{
if (param == null) return "NULL";
if (mapType != null && mapType != param.GetType() && (param is IEnumerable == false || mapType.IsArrayOrList() || param is JToken || param is JObject || param is JArray))
@ -67,7 +68,7 @@ namespace FreeSql.PostgreSQL
{
var sb = new StringBuilder();
var ie = param as IEnumerable;
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z, mapType));
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z, mapType, mapColumn));
return sb.Length == 0 ? "(NULL)" : sb.Remove(0, 1).Insert(0, "(").Append(")").ToString();
}
return string.Concat("'", param.ToString().Replace("'", "''"), "'");

View File

@ -160,11 +160,11 @@ namespace FreeSql.PostgreSQL
if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]";
return $"(case when {left} is null then 0 else array_length({left},1) end > 0)";
case "Contains":
tsc?.SetMapTypeTmp(null);
tsc.SetMapColumnTmp(null);
var args1 = getExp(callExp.Arguments[argIndex]);
var oldMapType = tsc?.SetMapTypeReturnOld(tsc?.mapTypeTmp);
var oldMapType = tsc.SetMapTypeReturnOld(tsc.mapTypeTmp);
left = objExp == null ? null : getExp(objExp);
tsc.SetMapTypeReturnOld(oldMapType);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
//判断 in 或 array @> array
if (left.StartsWith("array[") || left.EndsWith("]"))
return $"{args1} in ({left.Substring(6, left.Length - 7)})";

View File

@ -1,9 +1,11 @@
using FreeSql.Internal;
using FreeSql.Internal.Model;
using SafeObjectPool;
using System;
using System.Collections;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading;
@ -26,8 +28,10 @@ namespace FreeSql.SqlServer
}
}
}
static DateTime dt1970 = new DateTime(1970, 1, 1);
public override object AddslashesProcessParam(object param, Type mapType)
static string[] ncharDbTypes = new[] { "NVARCHAR", "NCHAR", "NTEXT" };
public override object AddslashesProcessParam(object param, Type mapType, ColumnInfo mapColumn)
{
if (param == null) return "NULL";
if (mapType != null && mapType != param.GetType() && (param is IEnumerable == false || mapType.IsArrayOrList()))
@ -35,7 +39,11 @@ namespace FreeSql.SqlServer
if (param is bool || param is bool?)
return (bool)param ? 1 : 0;
else if (param is string)
{
if (mapColumn != null && mapColumn.CsType.NullableTypeOrThis() == typeof(string) && ncharDbTypes.Any(a => mapColumn.Attribute.DbType.Contains(a)) == false)
return string.Concat("'", param.ToString().Replace("'", "''"), "'");
return string.Concat("N'", param.ToString().Replace("'", "''"), "'");
}
else if (param is char)
return string.Concat("'", param.ToString().Replace("'", "''"), "'");
else if (param is Enum)
@ -58,7 +66,7 @@ namespace FreeSql.SqlServer
{
var sb = new StringBuilder();
var ie = param as IEnumerable;
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z, mapType));
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z, mapType, mapColumn));
return sb.Length == 0 ? "(NULL)" : sb.Remove(0, 1).Insert(0, "(").Append(")").ToString();
}
return string.Concat("'", param.ToString().Replace("'", "''"), "'");

View File

@ -105,11 +105,11 @@ namespace FreeSql.SqlServer
if (objType == null) objType = callExp.Method.DeclaringType;
if (objType != null || objType.IsArrayOrList())
{
tsc?.SetMapTypeTmp(null);
tsc.SetMapColumnTmp(null);
var args1 = getExp(callExp.Arguments[argIndex]);
var oldMapType = tsc?.SetMapTypeReturnOld(tsc?.mapTypeTmp);
var oldMapType = tsc.SetMapTypeReturnOld(tsc.mapTypeTmp);
var left = objExp == null ? null : getExp(objExp);
tsc.SetMapTypeReturnOld(oldMapType);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
switch (callExp.Method.Name)
{
case "Contains":

View File

@ -1,4 +1,5 @@
using FreeSql.Internal;
using FreeSql.Internal.Model;
using SafeObjectPool;
using System;
using System.Collections;
@ -26,7 +27,7 @@ namespace FreeSql.Sqlite
}
}
static DateTime dt1970 = new DateTime(1970, 1, 1);
public override object AddslashesProcessParam(object param, Type mapType)
public override object AddslashesProcessParam(object param, Type mapType, ColumnInfo mapColumn)
{
if (param == null) return "NULL";
if (mapType != null && mapType != param.GetType() && (param is IEnumerable == false || mapType.IsArrayOrList()))
@ -47,7 +48,7 @@ namespace FreeSql.Sqlite
{
var sb = new StringBuilder();
var ie = param as IEnumerable;
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z, mapType));
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z, mapType, mapColumn));
return sb.Length == 0 ? "(NULL)" : sb.Remove(0, 1).Insert(0, "(").Append(")").ToString();
}
return string.Concat("'", param.ToString().Replace("'", "''"), "'");

View File

@ -101,11 +101,11 @@ namespace FreeSql.Sqlite
if (objType == null) objType = callExp.Method.DeclaringType;
if (objType != null || objType.IsArrayOrList())
{
tsc?.SetMapTypeTmp(null);
tsc.SetMapColumnTmp(null);
var args1 = getExp(callExp.Arguments[argIndex]);
var oldMapType = tsc?.SetMapTypeReturnOld(tsc?.mapTypeTmp);
var oldMapType = tsc.SetMapTypeReturnOld(tsc.mapTypeTmp);
var left = objExp == null ? null : getExp(objExp);
tsc.SetMapTypeReturnOld(oldMapType);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
switch (callExp.Method.Name)
{
case "Contains":