Skip to content
Permalink
Browse files

- 解决 表名名称包含点,无法进行 CRUD 的问题,由于测试的复杂性,此类情况仅支持 MySql/Sqlite CodeFirst 自动迁移;

> 注意:尽量不要使用带点的表名,只有 MySql/Sqlite 对此类表名支持 CodeFirst。但是它不影响 CRUD 功能,使用 [Table(Name = "`sys.config`")] 解决
  • Loading branch information
28810 28810
28810 authored and 28810 committed Jan 10, 2020
1 parent 3fe4c54 commit 994cc475c214d0e376c5e78416f751d7ce9e33db
Showing with 459 additions and 206 deletions.
  1. +6 −2 Extensions/FreeSql.Generator/RazorModel.cs
  2. +27 −0 FreeSql.Tests/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/MySqlCodeFirstTest.cs
  3. +27 −0 FreeSql.Tests/FreeSql.Tests.Provider.Odbc/MySql/MySqlCodeFirstTest.cs
  4. +78 −0 FreeSql.Tests/FreeSql.Tests/Internal/CommonUtilsTest.cs
  5. +28 −0 FreeSql.Tests/FreeSql.Tests/MySql/MySqlCodeFirstTest.cs
  6. +28 −0 FreeSql.Tests/FreeSql.Tests/Sqlite/SqliteCodeFirstTest.cs
  7. +16 −1 FreeSql/Internal/CommonUtils.cs
  8. +1 −1 Providers/FreeSql.Provider.MsAccess/Curd/MsAccessDelete.cs
  9. +12 −8 Providers/FreeSql.Provider.MsAccess/MsAccessUtils.cs
  10. +15 −19 Providers/FreeSql.Provider.MySql/MySqlCodeFirst.cs
  11. +12 −9 Providers/FreeSql.Provider.MySql/MySqlUtils.cs
  12. +12 −9 Providers/FreeSql.Provider.MySqlConnector/MySqlConnectorUtils.cs
  13. +4 −3 Providers/FreeSql.Provider.Odbc/Dameng/OdbcDamengCodeFirst.cs
  14. +12 −7 Providers/FreeSql.Provider.Odbc/Dameng/OdbcDamengUtils.cs
  15. +12 −10 Providers/FreeSql.Provider.Odbc/Default/OdbcUtils.cs
  16. +4 −3 Providers/FreeSql.Provider.Odbc/GBase/__OdbcGBaseCodeFirst.cs
  17. +14 −12 Providers/FreeSql.Provider.Odbc/GBase/__OdbcGBaseUtils.cs
  18. +14 −14 Providers/FreeSql.Provider.Odbc/MySql/OdbcMySqlCodeFirst.cs
  19. +12 −9 Providers/FreeSql.Provider.Odbc/MySql/OdbcMySqlUtils.cs
  20. +4 −3 Providers/FreeSql.Provider.Odbc/Oracle/OdbcOracleCodeFirst.cs
  21. +12 −7 Providers/FreeSql.Provider.Odbc/Oracle/OdbcOracleUtils.cs
  22. +4 −3 Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLCodeFirst.cs
  23. +14 −12 Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLUtils.cs
  24. +4 −3 Providers/FreeSql.Provider.Odbc/SqlServer/OdbcSqlServerCodeFirst.cs
  25. +12 −8 Providers/FreeSql.Provider.Odbc/SqlServer/OdbcSqlServerUtils.cs
  26. +4 −3 Providers/FreeSql.Provider.Oracle/OracleCodeFirst.cs
  27. +12 −7 Providers/FreeSql.Provider.Oracle/OracleUtils.cs
  28. +5 −7 Providers/FreeSql.Provider.PostgreSQL/PostgreSQLCodeFirst.cs
  29. +14 −9 Providers/FreeSql.Provider.PostgreSQL/PostgreSQLUtils.cs
  30. +5 −8 Providers/FreeSql.Provider.SqlServer/SqlServerCodeFirst.cs
  31. +12 −7 Providers/FreeSql.Provider.SqlServer/SqlServerUtils.cs
  32. +11 −15 Providers/FreeSql.Provider.Sqlite/SqliteCodeFirst.cs
  33. +12 −7 Providers/FreeSql.Provider.Sqlite/SqliteUtils.cs
@@ -4,7 +4,6 @@
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

public class RazorModel {
public RazorModel(IFreeSql fsql, string nameSpace, bool[] NameOptions, List<DbTableInfo> tables, DbTableInfo table) {
@@ -55,7 +54,12 @@ public class RazorModel {
var sb = new List<string>();

if (GetCsName(this.FullTableName) != this.FullTableName)
sb.Add("Name = \"" + this.FullTableName + "\"");
{
if (this.FullTableName.IndexOf('.') == -1)
sb.Add("Name = \"" + this.FullTableName + "\"");
else
sb.Add("Name = \"" + this.FullTableName + "\""); //Todo: QuoteSqlName
}

if (sb.Any() == false) return null;
return "[Table(" + string.Join(", ", sb) + ")]";
@@ -10,6 +10,33 @@ namespace FreeSql.Tests.MySqlConnector
{
public class MySqlCodeFirstTest
{
[Fact]
public void 表名中有点()
{
var item = new tbdot01 { name = "insert" };
g.mysql.Insert(item).ExecuteAffrows();

var find = g.mysql.Select<tbdot01>().Where(a => a.id == item.id).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Assert.Equal("insert", find.name);

Assert.Equal(1, g.mysql.Update<tbdot01>().Set(a => a.name == "update").Where(a => a.id == item.id).ExecuteAffrows());
find = g.mysql.Select<tbdot01>().Where(a => a.id == item.id).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Assert.Equal("update", find.name);

Assert.Equal(1, g.mysql.Delete<tbdot01>().Where(a => a.id == item.id).ExecuteAffrows());
find = g.mysql.Select<tbdot01>().Where(a => a.id == item.id).First();
Assert.Null(find);
}
[Table(Name = "`sys.tbdot01`")]
class tbdot01
{
public Guid id { get; set; }
public string name { get; set; }
}

[Fact]
public void 中文表_字段()
@@ -10,6 +10,33 @@ namespace FreeSql.Tests.Odbc.MySql
{
public class MySqlCodeFirstTest
{
[Fact]
public void 表名中有点()
{
var item = new tbdot01 { name = "insert" };
g.mysql.Insert(item).ExecuteAffrows();

var find = g.mysql.Select<tbdot01>().Where(a => a.id == item.id).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Assert.Equal("insert", find.name);

Assert.Equal(1, g.mysql.Update<tbdot01>().Set(a => a.name == "update").Where(a => a.id == item.id).ExecuteAffrows());
find = g.mysql.Select<tbdot01>().Where(a => a.id == item.id).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Assert.Equal("update", find.name);

Assert.Equal(1, g.mysql.Delete<tbdot01>().Where(a => a.id == item.id).ExecuteAffrows());
find = g.mysql.Select<tbdot01>().Where(a => a.id == item.id).First();
Assert.Null(find);
}
[Table(Name = "`sys.tbdot01`")]
class tbdot01
{
public Guid id { get; set; }
public string name { get; set; }
}

[Fact]
public void 中文表_字段()
@@ -0,0 +1,78 @@
using FreeSql.DataAnnotations;
using FreeSql;
using System;
using System.Collections.Generic;
using Xunit;
using System.Linq;
using Newtonsoft.Json.Linq;
using NpgsqlTypes;
using Npgsql.LegacyPostgis;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.Threading;
using System.Data.SqlClient;
using kwlib;
using System.Diagnostics;
using System.IO;
using System.Text;
using FreeSql.Internal;

namespace FreeSql.InternalTests
{
public class CommonUtilsTest
{

[Fact]
public void GetSplitTableNames()
{
var tbname = CommonUtils.GetSplitTableNames("table1", '`', '`', 2);
Assert.Equal("table1", tbname[0]);

tbname = CommonUtils.GetSplitTableNames("table1", '"', '"', 2);
Assert.Equal("table1", tbname[0]);

tbname = CommonUtils.GetSplitTableNames("table1", '[', ']', 2);
Assert.Equal("table1", tbname[0]);

//---

tbname = CommonUtils.GetSplitTableNames("schema1.table1", '`', '`', 2);
Assert.Equal("schema1", tbname[0]);
Assert.Equal("table1", tbname[1]);

tbname = CommonUtils.GetSplitTableNames("schema1.table1", '"', '"', 2);
Assert.Equal("schema1", tbname[0]);
Assert.Equal("table1", tbname[1]);

tbname = CommonUtils.GetSplitTableNames("schema1.table1", '[', ']', 2);
Assert.Equal("schema1", tbname[0]);
Assert.Equal("table1", tbname[1]);

//---

tbname = CommonUtils.GetSplitTableNames("`sys.table1`", '`', '`', 2);
Assert.Equal("sys.table1", tbname[0]);

tbname = CommonUtils.GetSplitTableNames("\"sys.table1\"", '"', '"', 2);
Assert.Equal("sys.table1", tbname[0]);

tbname = CommonUtils.GetSplitTableNames("[sys.table1]", '[', ']', 2);
Assert.Equal("sys.table1", tbname[0]);

//---

tbname = CommonUtils.GetSplitTableNames("`schema1`.`sys.table1`", '`', '`', 2);
Assert.Equal("schema1", tbname[0]);
Assert.Equal("sys.table1", tbname[1]);

tbname = CommonUtils.GetSplitTableNames("\"schema1\".\"sys.table1\"", '"', '"', 2);
Assert.Equal("schema1", tbname[0]);
Assert.Equal("sys.table1", tbname[1]);

tbname = CommonUtils.GetSplitTableNames("[schema1].[sys.table1]", '[', ']', 2);
Assert.Equal("schema1", tbname[0]);
Assert.Equal("sys.table1", tbname[1]);
}
}
}
@@ -11,6 +11,34 @@ namespace FreeSql.Tests.MySql
public class MySqlCodeFirstTest
{

[Fact]
public void 表名中有点()
{
var item = new tbdot01 { name = "insert" };
g.mysql.Insert(item).ExecuteAffrows();

var find = g.mysql.Select<tbdot01>().Where(a => a.id == item.id).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Assert.Equal("insert", find.name);

Assert.Equal(1, g.mysql.Update<tbdot01>().Set(a => a.name == "update").Where(a => a.id == item.id).ExecuteAffrows());
find = g.mysql.Select<tbdot01>().Where(a => a.id == item.id).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Assert.Equal("update", find.name);

Assert.Equal(1, g.mysql.Delete<tbdot01>().Where(a => a.id == item.id).ExecuteAffrows());
find = g.mysql.Select<tbdot01>().Where(a => a.id == item.id).First();
Assert.Null(find);
}
[Table(Name = "`sys.tbdot01`")]
class tbdot01
{
public Guid id { get; set; }
public string name { get; set; }
}

[Fact]
public void 中文表_字段()
{
@@ -11,6 +11,34 @@ namespace FreeSql.Tests.Sqlite
public class SqliteCodeFirstTest
{

[Fact]
public void 表名中有点()
{
var item = new tbdot01 { name = "insert" };
g.sqlite.Insert(item).ExecuteAffrows();

var find = g.sqlite.Select<tbdot01>().Where(a => a.id == item.id).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Assert.Equal("insert", find.name);

Assert.Equal(1, g.sqlite.Update<tbdot01>().Set(a => a.name == "update").Where(a => a.id == item.id).ExecuteAffrows());
find = g.sqlite.Select<tbdot01>().Where(a => a.id == item.id).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Assert.Equal("update", find.name);

Assert.Equal(1, g.sqlite.Delete<tbdot01>().Where(a => a.id == item.id).ExecuteAffrows());
find = g.sqlite.Select<tbdot01>().Where(a => a.id == item.id).First();
Assert.Null(find);
}
[Table(Name = "\"sys.tbdot01\"")]
class tbdot01
{
public Guid id { get; set; }
public string name { get; set; }
}

[Fact]
public void 中文表_字段()
{
@@ -27,8 +27,23 @@ public abstract class CommonUtils
public abstract DbParameter AppendParamter(List<DbParameter> _params, string parameterName, ColumnInfo col, Type type, object value);
public abstract DbParameter[] GetDbParamtersByObject(string sql, object obj);
public abstract string FormatSql(string sql, params object[] args);
public abstract string QuoteSqlName(string name);
public abstract string QuoteSqlName(params string[] name);
public abstract string TrimQuoteSqlName(string name);
public abstract string[] SplitTableName(string name);
public static string[] GetSplitTableNames(string name, char leftQuote, char rightQuote, int size)
{
if (string.IsNullOrEmpty(name)) return null;
if (name.IndexOf(leftQuote) == -1) return name.Split(new[] { '.' }, size);
name = Regex.Replace(name,
(leftQuote == '[' ? "\\" : "") +
leftQuote + @"([^" + (rightQuote == ']' ? "\\" : "") + rightQuote + @"]+)" +
(rightQuote == ']' ? "\\" : "") +
rightQuote, m => m.Groups[1].Value.Replace('.', '?'));
var ret = name.Split(new[] { '.' }, size);
for (var a = 0; a < ret.Length; a++)
ret[a] = ret[a].Replace('?', '.');
return ret;
}
public abstract string QuoteParamterName(string name);
public abstract string IsNull(string sql, object value);
public abstract string StringConcat(string[] objs, Type[] types);
@@ -22,7 +22,7 @@ public override List<T1> ExecuteDeleted()

#if net40
#else
async public override Task<List<T1>> ExecuteDeletedAsync()
public override Task<List<T1>> ExecuteDeletedAsync()
{
throw new NotImplementedException();
}
@@ -2,11 +2,8 @@
using FreeSql.Internal.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace FreeSql.MsAccess
@@ -40,12 +37,18 @@ public override DbParameter AppendParamter(List<DbParameter> _params, string par
});

public override string FormatSql(string sql, params object[] args) => sql?.FormatAccess(args);
public override string QuoteSqlName(string name)
public override string QuoteSqlName(params string[] name)
{
var nametrim = name.Trim();
if (nametrim.StartsWith("(") && nametrim.EndsWith(")"))
return nametrim; //原生SQL
return $"[{nametrim.TrimStart('[').TrimEnd(']').Replace(".", "].[")}]";
if (name.Length == 1)
{
var nametrim = name[0].Trim();
if (nametrim.StartsWith("(") && nametrim.EndsWith(")"))
return nametrim; //原生SQL
if (nametrim.StartsWith("[") && nametrim.EndsWith("]"))
return nametrim;
return $"[{nametrim.Replace(".", "].[")}]";
}
return $"[{string.Join("].[", name)}]";
}
public override string TrimQuoteSqlName(string name)
{
@@ -54,6 +57,7 @@ public override string TrimQuoteSqlName(string name)
return nametrim; //原生SQL
return $"{nametrim.TrimStart('[').TrimEnd(']').Replace("].[", ".").Replace(".[", ".")}";
}
public override string[] SplitTableName(string name) => GetSplitTableNames(name, '[', ']', 2);
public override string QuoteParamterName(string name) => $"@{(_orm.CodeFirst.IsSyncStructureToLower ? name.ToLower() : name)}";
public override string IsNull(string sql, object value) => $"iif(isnull({sql}), {value}, {sql})";
public override string StringConcat(string[] objs, Type[] types)

0 comments on commit 994cc47

Please sign in to comment.
You can’t perform that action at this time.