mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
Merge branch 'master' of https://github.com/2881099/FreeSql
This commit is contained in:
commit
77faf6e982
@ -1,18 +1,10 @@
|
|||||||
# CodeFirst
|
# CodeFirst
|
||||||
|
|
||||||
### 支持的类型
|
| 数据库 | 支持的类型类型 |
|
||||||
|
| - | - |
|
||||||
|
| MySql | bool, sbyte, short, int, long, byte, ushort, uint, ulong, double, float, decimal, Guid, TimeSpan, DateTime<br>bool?, sbyte?, short?, int?, long?, byte?, ushort?, uint?, ulong?, double?, float?, decimal?, Guid?, TimeSpan?, DateTime?<br>byte[], string, Enum & FlagsEnum<br>MygisPoint, MygisLineString, MygisPolygon, MygisMultiPoint, MygisMultiLineString, MygisMultiPolygon |
|
||||||
|
| SqlServer | bool, sbyte, short, int, long, byte, ushort, uint, ulong, double, float, decimal, Guid, TimeSpan, DateTime, DateTimeOffset<br>bool?, sbyte?, short?, int?, long?, byte?, ushort?, uint?, ulong?, double?, float?, decimal?, Guid?, TimeSpan?, DateTime?, DateTimeOffset?<br>byte[], string, Enum & FlagsEnum |
|
||||||
|
|
||||||
bool, byte, short, int, long, byte, ushort, uint, ulong, double, float, decimal, int, Guid
|
|
||||||
|
|
||||||
bool?, byte?, short?, int?, long?, byte?, ushort?, uint?, ulong?, double?, float?, decimal?, int?, Guid?
|
|
||||||
|
|
||||||
TimeSpan, DateTime
|
|
||||||
|
|
||||||
TimeSpan?, DateTime?
|
|
||||||
|
|
||||||
byte[], string
|
|
||||||
|
|
||||||
MygisPoint, MygisLineString, MygisPolygon, MygisMultiPoint, MygisMultiLineString, MygisMultiPolygon
|
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
|
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
|
||||||
|
@ -1,126 +1 @@
|
|||||||
|
|
||||||
//using System;
|
|
||||||
//using System.Collections;
|
|
||||||
//using System.Collections.Generic;
|
|
||||||
//using System.Linq;
|
|
||||||
//using System.Text;
|
|
||||||
//using System.Text.RegularExpressions;
|
|
||||||
//using FreeSql;
|
|
||||||
//using FreeSql.DatabaseModel;
|
|
||||||
|
|
||||||
////namespace TplDynamicCodeGenerate {
|
|
||||||
//public class TplDynamicCodeGenerate_view1 : FreeSql.Generator.TemplateEngin.ITemplateOutput {
|
|
||||||
// public FreeSql.Generator.TemplateEngin.TemplateReturnInfo OuTpUt(StringBuilder tOuTpUt, IDictionary oPtIoNs, string rEfErErFiLeNaMe, FreeSql.Generator.TemplateEngin tEmPlAtEsEnDeR) {
|
|
||||||
// FreeSql.Generator.TemplateEngin.TemplateReturnInfo rTn = tOuTpUt == null ?
|
|
||||||
// new FreeSql.Generator.TemplateEngin.TemplateReturnInfo { Sb = (tOuTpUt = new StringBuilder()), Blocks = new Dictionary<string, int[]>() } :
|
|
||||||
// new FreeSql.Generator.TemplateEngin.TemplateReturnInfo { Sb = tOuTpUt, Blocks = new Dictionary<string, int[]>() };
|
|
||||||
// Dictionary<string, int[]> TPL__blocks = rTn.Blocks;
|
|
||||||
// Stack<int[]> TPL__blocks_stack = new Stack<int[]>();
|
|
||||||
// int[] TPL__blocks_stack_peek;
|
|
||||||
// List<IDictionary> TPL__forc = new List<IDictionary>();
|
|
||||||
// Func<IDictionary> pRoCeSsOpTiOnS = new Func<IDictionary>(delegate () {
|
|
||||||
// IDictionary nEwoPtIoNs = new Hashtable();
|
|
||||||
// foreach (DictionaryEntry oPtIoNs_dE in oPtIoNs)
|
|
||||||
// nEwoPtIoNs[oPtIoNs_dE.Key] = oPtIoNs_dE.Value;
|
|
||||||
// foreach (IDictionary TPL__forc_dIc in TPL__forc)
|
|
||||||
// foreach (DictionaryEntry TPL__forc_dIc_dE in TPL__forc_dIc)
|
|
||||||
// nEwoPtIoNs[TPL__forc_dIc_dE.Key] = TPL__forc_dIc_dE.Value;
|
|
||||||
// return nEwoPtIoNs;
|
|
||||||
// });
|
|
||||||
// FreeSql.Generator.TemplateEngin.TemplateIf tPlIf = delegate (object exp) {
|
|
||||||
// if (exp is bool) return (bool)exp;
|
|
||||||
// if (exp == null) return false;
|
|
||||||
// if (exp is int && (int)exp == 0) return false;
|
|
||||||
// if (exp is string && (string)exp == string.Empty) return false;
|
|
||||||
// if (exp is long && (long)exp == 0) return false;
|
|
||||||
// if (exp is short && (short)exp == 0) return false;
|
|
||||||
// if (exp is byte && (byte)exp == 0) return false;
|
|
||||||
// if (exp is double && (double)exp == 0) return false;
|
|
||||||
// if (exp is float && (float)exp == 0) return false;
|
|
||||||
// if (exp is decimal && (decimal)exp == 0) return false;
|
|
||||||
// return true;
|
|
||||||
// };
|
|
||||||
// FreeSql.Generator.TemplateEngin.TemplatePrint print = delegate (object[] pArMs) {
|
|
||||||
// if (pArMs == null || pArMs.Length == 0) return;
|
|
||||||
// foreach (object pArMs_A in pArMs) if (pArMs_A != null) tOuTpUt.Append(pArMs_A);
|
|
||||||
// };
|
|
||||||
// FreeSql.Generator.TemplateEngin.TemplatePrint Print = prin
|
|
||||||
// dynamic index = oPtIoNs["index"];
|
|
||||||
// dynamic col = oPtIoNs["col"];
|
|
||||||
// dynamic table = oPtIoNs["table"];
|
|
||||||
// dynamic dbfirst = oPtIoNs["dbfirst"]; t;
|
|
||||||
// tOuTpUt.Append("using System;\r\nusing System.Collections;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Reflection;\r\nusing System.Threading.Tasks;\r\nusing Newtonsoft.Json;\r\nusing FreeSql.DataAnnotations;\r\n");
|
|
||||||
|
|
||||||
// var dbf = dbfirst as FreeSql.IDbFirst;
|
|
||||||
// var cols = (table.Columns as List<DbColumnInfo>);
|
|
||||||
|
|
||||||
// Func<string, string> UString = stra => stra.Substring(0, 1).ToUpper() + stra.Substring(1);
|
|
||||||
// Func<DbColumnInfo, string> GetCsType = cola3 => {
|
|
||||||
// if (cola3.DbType == (int)MySql.Data.MySqlClient.MySqlDbType.Enum || cola3.DbType == (int)MySql.Data.MySqlClient.MySqlDbType.Set) {
|
|
||||||
// return $"{UString(cola3.Table.Name)}{cola3.Name.ToUpper()}{(cola3.IsNullable ? "?" : "")}";
|
|
||||||
// }
|
|
||||||
// return dbf.GetCsType(cola3);
|
|
||||||
// };
|
|
||||||
|
|
||||||
// tOuTpUt.Append("\r\nnamespace test.Model {\r\n\r\n [JsonObject(MemberSerialization.OptIn), Table(Name = \"");
|
|
||||||
// Print(!string.IsNullOrEmpty(table.Schema) ? table.Schema + "." : "");
|
|
||||||
// tOuTpUt.Append("");
|
|
||||||
// Print(table.Name);
|
|
||||||
// tOuTpUt.Append("\"");
|
|
||||||
// if (tPlIf(cols.Where(cola003 => cola003.Name.ToLower() == "is_deleted" || cola003.Name.ToLower() == "isdeleted").Any())) {
|
|
||||||
// tOuTpUt.Append(", SelectFilter = \"a.IsDeleted = 1\"");
|
|
||||||
// }
|
|
||||||
// tOuTpUt.Append(")]\r\n public partial class ");
|
|
||||||
// Print(UString(table.Name));
|
|
||||||
// tOuTpUt.Append(" {");
|
|
||||||
// //new Action(delegate () {
|
|
||||||
// IDictionary TPL__tmp1 = new Hashtable();
|
|
||||||
// TPL__forc.Add(TPL__tmp1);
|
|
||||||
// var TPL__tmp2 = table.Columns;
|
|
||||||
// var TPL__tmp3 = col;
|
|
||||||
// var TPL__tmp4 = index;
|
|
||||||
// index = 0;
|
|
||||||
// if (TPL__tmp2 != null)
|
|
||||||
// foreach (var TPL__tmp5 in TPL__tmp2) {
|
|
||||||
// TPL__tmp1["index"] = ++index;
|
|
||||||
// TPL__tmp1["col"] = TPL__tmp5;
|
|
||||||
// col = TPL__tmp5;
|
|
||||||
// tOuTpUt.Append("\r\n ");
|
|
||||||
// if (tPlIf(string.IsNullOrEmpty(col.Coment) == false)) {
|
|
||||||
// tOuTpUt.Append("/// <summary>\r\n /// ");
|
|
||||||
// Print(col.Coment.Replace("\r\n", "\n").Replace("\n", "\r\n /// "));
|
|
||||||
// tOuTpUt.Append("\r\n /// </summary>");
|
|
||||||
// }
|
|
||||||
// tOuTpUt.Append("\r\n [JsonProperty, Column(Name = \"");
|
|
||||||
// Print(col.Name);
|
|
||||||
// tOuTpUt.Append("\", DbType = \"");
|
|
||||||
// Print(col.DbTypeTextFull);
|
|
||||||
// tOuTpUt.Append("\"");
|
|
||||||
// if (tPlIf(col.IsPrimary == true)) {
|
|
||||||
// tOuTpUt.Append(", IsPrimary = true");
|
|
||||||
// }
|
|
||||||
// tOuTpUt.Append("");
|
|
||||||
// if (tPlIf(col.IsIdentity == true)) {
|
|
||||||
// tOuTpUt.Append(", IsIdentity = true");
|
|
||||||
// }
|
|
||||||
// tOuTpUt.Append("");
|
|
||||||
// if (tPlIf(col.IsNullable == true)) {
|
|
||||||
// tOuTpUt.Append(", IsNullable = true");
|
|
||||||
// }
|
|
||||||
// tOuTpUt.Append(")]\r\n public ");
|
|
||||||
// Print(GetCsType(col));
|
|
||||||
// tOuTpUt.Append(" ");
|
|
||||||
// Print(UString(col.Name));
|
|
||||||
// tOuTpUt.Append(" { get; set; }\r\n ");
|
|
||||||
// }
|
|
||||||
// col = TPL__tmp3;
|
|
||||||
// index = TPL__tmp4;
|
|
||||||
// TPL__forc.RemoveAt(TPL__forc.Count - 1);
|
|
||||||
// //})();
|
|
||||||
// tOuTpUt.Append("\r\n }\r\n");
|
|
||||||
// tEmPlAtEsEnDeR.RenderFile2(tOuTpUt, pRoCeSsOpTiOnS(), "../../include/enumtype.tpl", rEfErErFiLeNaMe);
|
|
||||||
// tOuTpUt.Append("\r\n}");
|
|
||||||
// return rTn;
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
////}
|
|
27
FreeSql.Tests/Generator/SqlServerTemplateGeneratorTest.cs
Normal file
27
FreeSql.Tests/Generator/SqlServerTemplateGeneratorTest.cs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
using FreeSql.DataAnnotations;
|
||||||
|
using FreeSql.Generator;
|
||||||
|
using System;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace FreeSql.Tests.Generator {
|
||||||
|
public class SqlServerTemplateGeneratorTest {
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void BuildSimpleEntity() {
|
||||||
|
var gen = new TemplateGenerator();
|
||||||
|
gen.Build(g.sqlserver.DbFirst, @"C:\Users\28810\Desktop\github\FreeSql\Templates\SqlServer\simple-entity", @"C:\Users\28810\Desktop\新建文件夹 (9)", "shop");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void BuildSimpleEntityNavigationObject () {
|
||||||
|
var gen = new TemplateGenerator();
|
||||||
|
gen.Build(g.sqlserver.DbFirst, @"C:\Users\28810\Desktop\github\FreeSql\Templates\SqlServer\simple-entity-navigation-object", @"C:\Users\28810\Desktop\新建文件夹 (9)", "shop");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void BuildRichEntityNavigationObject() {
|
||||||
|
var gen = new TemplateGenerator();
|
||||||
|
gen.Build(g.sqlserver.DbFirst, @"C:\Users\28810\Desktop\github\FreeSql\Templates\SqlServer\rich-entity-navigation-object", @"C:\Users\28810\Desktop\新建文件夹 (9)", "shop");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -343,9 +343,9 @@ namespace FreeSql.Tests.MySql {
|
|||||||
query2.ToList();
|
query2.ToList();
|
||||||
|
|
||||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD>㲻<EFBFBD><E3B2BB>
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD>㲻<EFBFBD><E3B2BB>
|
||||||
query = select.Where("a.clicks > 100 && a.id = ?id", new { id = 10 });
|
query = select.Where("a.clicks > 100 and a.id = ?id", new { id = 10 });
|
||||||
sql = query.ToSql().Replace("\r\n", "");
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
Assert.Equal("SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 FROM `tb_topic` a WHERE (a.clicks > 100 && a.id = ?id)", sql);
|
Assert.Equal("SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 FROM `tb_topic` a WHERE (a.clicks > 100 and a.id = ?id)", sql);
|
||||||
query.ToList();
|
query.ToList();
|
||||||
}
|
}
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -390,9 +390,9 @@ namespace FreeSql.Tests.MySql {
|
|||||||
query2.ToList();
|
query2.ToList();
|
||||||
|
|
||||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD>㲻<EFBFBD><E3B2BB>
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD>㲻<EFBFBD><E3B2BB>
|
||||||
query = select.WhereIf(true, "a.clicks > 100 && a.id = ?id", new { id = 10 });
|
query = select.WhereIf(true, "a.clicks > 100 and a.id = ?id", new { id = 10 });
|
||||||
sql = query.ToSql().Replace("\r\n", "");
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
Assert.Equal("SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 FROM `tb_topic` a WHERE (a.clicks > 100 && a.id = ?id)", sql);
|
Assert.Equal("SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 FROM `tb_topic` a WHERE (a.clicks > 100 and a.id = ?id)", sql);
|
||||||
query.ToList();
|
query.ToList();
|
||||||
|
|
||||||
// ==========================================WhereIf(false)
|
// ==========================================WhereIf(false)
|
||||||
@ -437,7 +437,7 @@ namespace FreeSql.Tests.MySql {
|
|||||||
query2.ToList();
|
query2.ToList();
|
||||||
|
|
||||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD>㲻<EFBFBD><E3B2BB>
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD>㲻<EFBFBD><E3B2BB>
|
||||||
query = select.WhereIf(false, "a.clicks > 100 && a.id = ?id", new { id = 10 });
|
query = select.WhereIf(false, "a.clicks > 100 and a.id = ?id", new { id = 10 });
|
||||||
sql = query.ToSql().Replace("\r\n", "");
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
Assert.Equal("SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 FROM `tb_topic` a", sql);
|
Assert.Equal("SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 FROM `tb_topic` a", sql);
|
||||||
query.ToList();
|
query.ToList();
|
||||||
|
115
FreeSql.Tests/MySql/Expression/DateTimeTest.cs
Normal file
115
FreeSql.Tests/MySql/Expression/DateTimeTest.cs
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
using FreeSql.DataAnnotations;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace FreeSql.Tests.MySql.Expression {
|
||||||
|
public class DateTimeTest {
|
||||||
|
|
||||||
|
ISelect<Topic> select => g.mysql.Select<Topic>();
|
||||||
|
|
||||||
|
[Table(Name = "tb_topic111333")]
|
||||||
|
class Topic {
|
||||||
|
[Column(IsIdentity = true, IsPrimary = true)]
|
||||||
|
public int Id { get; set; }
|
||||||
|
public int Clicks { get; set; }
|
||||||
|
public int TestTypeInfoGuid { get; set; }
|
||||||
|
public TestTypeInfo Type { get; set; }
|
||||||
|
public string Title { get; set; }
|
||||||
|
public DateTime CreateTime { get; set; }
|
||||||
|
}
|
||||||
|
[Table(Name = "TestTypeInfo333")]
|
||||||
|
class TestTypeInfo {
|
||||||
|
public int Guid { get; set; }
|
||||||
|
public int ParentId { get; set; }
|
||||||
|
public TestTypeParentInfo Parent { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
public DateTime Time { get; set; }
|
||||||
|
}
|
||||||
|
[Table(Name = "TestTypeParentInfo23123")]
|
||||||
|
class TestTypeParentInfo {
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public List<TestTypeInfo> Types { get; set; }
|
||||||
|
public DateTime Time2 { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DayOfWeek() {
|
||||||
|
var data = new List<object>();
|
||||||
|
data.Add(select.Where(a => a.CreateTime.DayOfWeek > DateTime.Now.DayOfWeek).ToSql());
|
||||||
|
data.Add(select.Where(a => a.Type.Time.DayOfWeek > DateTime.Now.DayOfWeek).ToSql());
|
||||||
|
data.Add(select.Where(a => a.Type.Parent.Time2.DayOfWeek > DateTime.Now.DayOfWeek).ToSql());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic111333` a
|
||||||
|
//WHERE ((dayofweek(a.`CreateTime`) - 1) > (dayofweek(now()) - 1));
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9
|
||||||
|
//FROM `tb_topic111333` a, `TestTypeInfo333` a__Type
|
||||||
|
//WHERE ((dayofweek(a__Type.`Time`) - 1) > (dayofweek(now()) - 1));
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9
|
||||||
|
//FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent
|
||||||
|
//WHERE ((dayofweek(a__Type__Parent.`Time2`) - 1) > (dayofweek(now()) - 1))
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Day() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void DayOfYear() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Month() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Year() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Hour() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Minute() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Second() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Millisecond() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Ticks() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Add() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void AddDays() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void AddHours() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void AddMilliseconds() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void AddMinutes() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void AddMonths() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void AddSeconds() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void AddTicks() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void AddYears() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Subtract() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
95
FreeSql.Tests/MySql/Expression/MathTest.cs
Normal file
95
FreeSql.Tests/MySql/Expression/MathTest.cs
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
using FreeSql.DataAnnotations;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace FreeSql.Tests.MySql.Expression {
|
||||||
|
public class MathTest {
|
||||||
|
|
||||||
|
ISelect<Topic> select => g.mysql.Select<Topic>();
|
||||||
|
|
||||||
|
[Table(Name = "tb_topic")]
|
||||||
|
class Topic {
|
||||||
|
[Column(IsIdentity = true, IsPrimary = true)]
|
||||||
|
public int Id { get; set; }
|
||||||
|
public int Clicks { get; set; }
|
||||||
|
public int TestTypeInfoGuid { get; set; }
|
||||||
|
public TestTypeInfo Type { get; set; }
|
||||||
|
public string Title { get; set; }
|
||||||
|
public DateTime CreateTime { get; set; }
|
||||||
|
}
|
||||||
|
class TestTypeInfo {
|
||||||
|
public int Guid { get; set; }
|
||||||
|
public int ParentId { get; set; }
|
||||||
|
public TestTypeParentInfo Parent { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
}
|
||||||
|
class TestTypeParentInfo {
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public List<TestTypeInfo> Types { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void PI() {
|
||||||
|
var data = new List<object>();
|
||||||
|
data.Add(select.Where(a => Math.PI + a.Clicks > 0).ToSql());
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Abs() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Sign() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Floor() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Ceiling() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Round() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Exp() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Log() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Log10() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Pow() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Sqrt() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Cos() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Sin() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Tan() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Acos() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Asin() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Atan() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Atan2() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Truncate() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
663
FreeSql.Tests/MySql/Expression/StringTest.cs
Normal file
663
FreeSql.Tests/MySql/Expression/StringTest.cs
Normal file
@ -0,0 +1,663 @@
|
|||||||
|
using FreeSql.DataAnnotations;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace FreeSql.Tests.MySql.Expression {
|
||||||
|
public class StringTest {
|
||||||
|
|
||||||
|
ISelect<Topic> select => g.mysql.Select<Topic>();
|
||||||
|
|
||||||
|
[Table(Name = "tb_topic")]
|
||||||
|
class Topic {
|
||||||
|
[Column(IsIdentity = true, IsPrimary = true)]
|
||||||
|
public int Id { get; set; }
|
||||||
|
public int Clicks { get; set; }
|
||||||
|
public int TestTypeInfoGuid { get; set; }
|
||||||
|
public TestTypeInfo Type { get; set; }
|
||||||
|
public string Title { get; set; }
|
||||||
|
public DateTime CreateTime { get; set; }
|
||||||
|
}
|
||||||
|
class TestTypeInfo {
|
||||||
|
public int Guid { get; set; }
|
||||||
|
public int ParentId { get; set; }
|
||||||
|
public TestTypeParentInfo Parent { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
}
|
||||||
|
class TestTypeParentInfo {
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public List<TestTypeInfo> Types { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void StartsWith() {
|
||||||
|
var list = select.Where(a => a.Title.StartsWith("aaa")).ToList();
|
||||||
|
list = select.Where(a => a.Title.StartsWith(a.Title)).ToList();
|
||||||
|
list = select.Where(a => a.Title.StartsWith(a.Title + 1)).ToList();
|
||||||
|
list = select.Where(a => a.Title.StartsWith(a.Type.Name)).ToList();
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE((a.`Title`) LIKE '%aaa')
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE((a.`Title`) LIKE concat('%', a.`Title`))
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE((a.`Title`) LIKE concat('%', concat(a.`Title`, 1)))
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE((a.`Title`) LIKE concat('%', a__Type.`Name`))
|
||||||
|
list = select.Where(a => (a.Title + "aaa").StartsWith("aaa")).ToList();
|
||||||
|
list = select.Where(a => (a.Title + "aaa").StartsWith(a.Title)).ToList();
|
||||||
|
list = select.Where(a => (a.Title + "aaa").StartsWith(a.Title + 1)).ToList();
|
||||||
|
list = select.Where(a => (a.Title + "aaa").StartsWith(a.Type.Name)).ToList();
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE((concat(a.`Title`, 'aaa')) LIKE '%aaa')
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE((concat(a.`Title`, 'aaa')) LIKE concat('%', a.`Title`))
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE((concat(a.`Title`, 'aaa')) LIKE concat('%', concat(a.`Title`, 1)))
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE((concat(a.`Title`, 'aaa')) LIKE concat('%', a__Type.`Name`))
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void EndsWith() {
|
||||||
|
var list = select.Where(a => a.Title.EndsWith("aaa")).ToList();
|
||||||
|
list = select.Where(a => a.Title.EndsWith(a.Title)).ToList();
|
||||||
|
list = select.Where(a => a.Title.EndsWith(a.Title + 1)).ToList();
|
||||||
|
list = select.Where(a => a.Title.EndsWith(a.Type.Name)).ToList();
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE((a.`Title`) LIKE 'aaa%')
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE((a.`Title`) LIKE concat(a.`Title`, '%'))
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE((a.`Title`) LIKE concat(concat(a.`Title`, 1), '%'))
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE((a.`Title`) LIKE concat(a__Type.`Name`, '%'))
|
||||||
|
list = select.Where(a => (a.Title + "aaa").EndsWith("aaa")).ToList();
|
||||||
|
list = select.Where(a => (a.Title + "aaa").EndsWith(a.Title)).ToList();
|
||||||
|
list = select.Where(a => (a.Title + "aaa").EndsWith(a.Title + 1)).ToList();
|
||||||
|
list = select.Where(a => (a.Title + "aaa").EndsWith(a.Type.Name)).ToList();
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE((concat(a.`Title`, 'aaa')) LIKE 'aaa%')
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE((concat(a.`Title`, 'aaa')) LIKE concat(a.`Title`, '%'))
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE((concat(a.`Title`, 'aaa')) LIKE concat(concat(a.`Title`, 1), '%'))
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE((concat(a.`Title`, 'aaa')) LIKE concat(a__Type.`Name`, '%'))
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Contains() {
|
||||||
|
var ToList = select.Where(a => a.Title.Contains("aaa")).ToList();
|
||||||
|
ToList = select.Where(a => a.Title.Contains(a.Title)).ToList();
|
||||||
|
ToList = select.Where(a => a.Title.Contains(a.Title + 1)).ToList();
|
||||||
|
ToList = select.Where(a => a.Title.Contains(a.Type.Name)).ToList();
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE((a.`Title`) LIKE '%aaa%')
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE((a.`Title`) LIKE concat('%', a.`Title`, '%'))
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE((a.`Title`) LIKE concat('%', a.`Title` +1, '%'))
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE((a.`Title`) LIKE concat('%', a__Type.`Name`, '%'))
|
||||||
|
ToList = select.Where(a => (a.Title + "aaa").Contains("aaa")).ToList();
|
||||||
|
ToList = select.Where(a => (a.Title + "aaa").Contains(a.Title)).ToList();
|
||||||
|
ToList = select.Where(a => (a.Title + "aaa").Contains(a.Title + 1)).ToList();
|
||||||
|
ToList = select.Where(a => (a.Title + "aaa").Contains(a.Type.Name)).ToList();
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE((concat(a.`Title`, 'aaa')) LIKE '%aaa%')
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE((concat(a.`Title`, 'aaa')) LIKE concat('%', a.`Title`, '%'))
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE((concat(a.`Title`, 'aaa')) LIKE concat('%', concat(a.`Title`, 1), '%'))
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE((concat(a.`Title`, 'aaa')) LIKE concat('%', a__Type.`Name`, '%'))
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void ToLower() {
|
||||||
|
var data = new List<object>();
|
||||||
|
data.Add(select.Where(a => a.Title.ToLower() == "aaa").ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.ToLower() == a.Title).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.ToLower() == (a.Title + 1)).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.ToLower() == a.Type.Name).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE(lower(a.`Title`) = 'aaa');
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE(lower(a.`Title`) = a.`Title`);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE(lower(a.`Title`) = concat(a.`Title`, 1));
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE(lower(a.`Title`) = a__Type.`Name`);
|
||||||
|
data.Add(select.Where(a => (a.Title.ToLower() + "aaa").ToLower() == "aaa").ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.ToLower() + "aaa").ToLower() == a.Title).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.ToLower() + "aaa").ToLower() == (a.Title + 1)).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.ToLower() + "aaa").ToLower() == a.Type.Name).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE(lower(concat(lower(a.`Title`), 'aaa')) = 'aaa');
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE(lower(concat(lower(a.`Title`), 'aaa')) = a.`Title`);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE(lower(concat(lower(a.`Title`), 'aaa')) = concat(a.`Title`, 1));
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE(lower(concat(lower(a.`Title`), 'aaa')) = a__Type.`Name`)
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void ToUpper() {
|
||||||
|
var data = new List<object>();
|
||||||
|
data.Add(select.Where(a => a.Title.ToUpper() == "aaa").ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.ToUpper() == a.Title).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.ToUpper() == (a.Title + 1)).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.ToUpper() == a.Type.Name).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (upper(a.`Title`) = 'aaa');
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (upper(a.`Title`) = a.`Title`);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (upper(a.`Title`) = concat(a.`Title`, 1));
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE (upper(a.`Title`) = a__Type.`Name`);
|
||||||
|
data.Add(select.Where(a => (a.Title.ToUpper() + "aaa").ToUpper() == "aaa").ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.ToUpper() + "aaa").ToUpper() == a.Title).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.ToUpper() + "aaa").ToUpper() == (a.Title + 1)).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.ToUpper() + "aaa").ToUpper() == a.Type.Name).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (upper(concat(upper(a.`Title`), 'aaa')) = 'aaa');
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (upper(concat(upper(a.`Title`), 'aaa')) = a.`Title`);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (upper(concat(upper(a.`Title`), 'aaa')) = concat(a.`Title`, 1));
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE (upper(concat(upper(a.`Title`), 'aaa')) = a__Type.`Name`)
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Substring() {
|
||||||
|
var data = new List<object>();
|
||||||
|
data.Add(select.Where(a => a.Title.Substring(0) == "aaa").ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.Substring(0) == a.Title).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.Substring(0) == (a.Title + 1)).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.Substring(0) == a.Type.Name).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (substr(a.`Title`, 1) = 'aaa');
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (substr(a.`Title`, 1) = a.`Title`);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (substr(a.`Title`, 1) = concat(a.`Title`, 1));
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE (substr(a.`Title`, 1) = a__Type.`Name`);
|
||||||
|
data.Add(select.Where(a => (a.Title.Substring(0) + "aaa").Substring(a.Title.Length) == "aaa").ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.Substring(0) + "aaa").Substring(0, a.Title.Length) == a.Title).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.Substring(0) + "aaa").Substring(0, 3) == (a.Title + 1)).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.Substring(0) + "aaa").Substring(1, 2) == a.Type.Name).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (substr(concat(substr(a.`Title`, 1), 'aaa'), char_length(a.`Title`) + 1) = 'aaa');
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (substr(concat(substr(a.`Title`, 1), 'aaa'), 1, char_length(a.`Title`)) = a.`Title`);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (substr(concat(substr(a.`Title`, 1), 'aaa'), 1, 3) = concat(a.`Title`, 1));
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE (substr(concat(substr(a.`Title`, 1), 'aaa'), 2, 2) = a__Type.`Name`)
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Length() {
|
||||||
|
var data = new List<object>();
|
||||||
|
data.Add(select.Where(a => a.Title.Length == 0).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.Length == 1).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.Length == a.Title.Length + 1).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.Length == a.Type.Name.Length).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (char_length(a.`Title`) = 0);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (char_length(a.`Title`) = 1);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (char_length(a.`Title`) = char_length(a.`Title`) + 1);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE (char_length(a.`Title`) = char_length(a__Type.`Name`));
|
||||||
|
data.Add(select.Where(a => (a.Title + "aaa").Length == 0).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title + "aaa").Length == 1).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title + "aaa").Length == a.Title.Length + 1).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title + "aaa").Length == a.Type.Name.Length).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (char_length(concat(a.`Title`, 'aaa')) = 0);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (char_length(concat(a.`Title`, 'aaa')) = 1);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (char_length(concat(a.`Title`, 'aaa')) = char_length(a.`Title`) + 1);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE (char_length(concat(a.`Title`, 'aaa')) = char_length(a__Type.`Name`))
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void IndexOf() {
|
||||||
|
var data = new List<object>();
|
||||||
|
data.Add(select.Where(a => a.Title.IndexOf("aaa") == -1).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.IndexOf("aaa", 2) == -1).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.IndexOf("aaa", 2) == (a.Title.Length + 1)).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.IndexOf("aaa", 2) == a.Type.Name.Length + 1).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE ((locate(a.`Title`, 'aaa') - 1) = -1);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE ((locate(a.`Title`, 'aaa', 3) - 1) = -1);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE ((locate(a.`Title`, 'aaa', 3) - 1) = char_length(a.`Title`) + 1);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE ((locate(a.`Title`, 'aaa', 3) - 1) = char_length(a__Type.`Name`) + 1);
|
||||||
|
data.Add(select.Where(a => (a.Title + "aaa").IndexOf("aaa") == -1).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title + "aaa").IndexOf("aaa", 2) == -1).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title + "aaa").IndexOf("aaa", 2) == (a.Title.Length + 1)).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title + "aaa").IndexOf("aaa", 2) == a.Type.Name.Length + 1).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE ((locate(concat(a.`Title`, 'aaa'), 'aaa') - 1) = -1);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE ((locate(concat(a.`Title`, 'aaa'), 'aaa', 3) - 1) = -1);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE ((locate(concat(a.`Title`, 'aaa'), 'aaa', 3) - 1) = char_length(a.`Title`) + 1);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE ((locate(concat(a.`Title`, 'aaa'), 'aaa', 3) - 1) = char_length(a__Type.`Name`) + 1)
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void PadLeft() {
|
||||||
|
var data = new List<object>();
|
||||||
|
data.Add(select.Where(a => a.Title.PadLeft(10, 'a') == "aaa").ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.PadLeft(10, 'a') == a.Title).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.PadLeft(10, 'a') == (a.Title + 1)).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.PadLeft(10, 'a') == a.Type.Name).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (lpad(a.`Title`, 10, 'a') = 'aaa');
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (lpad(a.`Title`, 10, 'a') = a.`Title`);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (lpad(a.`Title`, 10, 'a') = concat(a.`Title`, 1));
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE (lpad(a.`Title`, 10, 'a') = a__Type.`Name`);
|
||||||
|
data.Add(select.Where(a => (a.Title.PadLeft(10, 'a') + "aaa").PadLeft(20, 'b') == "aaa").ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.PadLeft(10, 'a') + "aaa").PadLeft(20, 'b') == a.Title).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.PadLeft(10, 'a') + "aaa").PadLeft(20, 'b') == (a.Title + 1)).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.PadLeft(10, 'a') + "aaa").PadLeft(20, 'b') == a.Type.Name).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (lpad(concat(lpad(a.`Title`, 10, 'a'), 'aaa'), 20, 'b') = 'aaa');
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (lpad(concat(lpad(a.`Title`, 10, 'a'), 'aaa'), 20, 'b') = a.`Title`);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (lpad(concat(lpad(a.`Title`, 10, 'a'), 'aaa'), 20, 'b') = concat(a.`Title`, 1));
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE (lpad(concat(lpad(a.`Title`, 10, 'a'), 'aaa'), 20, 'b') = a__Type.`Name`)
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void PadRight() {
|
||||||
|
var data = new List<object>();
|
||||||
|
data.Add(select.Where(a => a.Title.PadRight(10, 'a') == "aaa").ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.PadRight(10, 'a') == a.Title).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.PadRight(10, 'a') == (a.Title + 1)).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.PadRight(10, 'a') == a.Type.Name).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (rpad(a.`Title`, 10, 'a') = 'aaa');
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (rpad(a.`Title`, 10, 'a') = a.`Title`);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (rpad(a.`Title`, 10, 'a') = concat(a.`Title`, 1));
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE (rpad(a.`Title`, 10, 'a') = a__Type.`Name`);
|
||||||
|
data.Add(select.Where(a => (a.Title.PadRight(10, 'a') + "aaa").PadRight(20, 'b') == "aaa").ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.PadRight(10, 'a') + "aaa").PadRight(20, 'b') == a.Title).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.PadRight(10, 'a') + "aaa").PadRight(20, 'b') == (a.Title + 1)).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.PadRight(10, 'a') + "aaa").PadRight(20, 'b') == a.Type.Name).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (rpad(concat(rpad(a.`Title`, 10, 'a'), 'aaa'), 20, 'b') = 'aaa');
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (rpad(concat(rpad(a.`Title`, 10, 'a'), 'aaa'), 20, 'b') = a.`Title`);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (rpad(concat(rpad(a.`Title`, 10, 'a'), 'aaa'), 20, 'b') = concat(a.`Title`, 1));
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE (rpad(concat(rpad(a.`Title`, 10, 'a'), 'aaa'), 20, 'b') = a__Type.`Name`)
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Trim() {
|
||||||
|
var data = new List<object>();
|
||||||
|
data.Add(select.Where(a => a.Title.Trim() == "aaa").ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.Trim('a') == a.Title).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.Trim('a', 'b') == (a.Title + 1)).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.Trim('a', 'b', 'c') == a.Type.Name).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (trim(a.`Title`) = 'aaa');
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (trim('a' from a.`Title`) = a.`Title`);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (trim('b' from trim('a' from a.`Title`)) = concat(a.`Title`, 1));
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE (trim('c' from trim('b' from trim('a' from a.`Title`))) = a__Type.`Name`);
|
||||||
|
data.Add(select.Where(a => (a.Title.Trim() + "aaa").Trim() == "aaa").ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.Trim('a') + "aaa").Trim('a') == a.Title).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.Trim('a', 'b') + "aaa").Trim('a', 'b') == (a.Title + 1)).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.Trim('a', 'b', 'c') + "aaa").Trim('a', 'b', 'c') == a.Type.Name).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (trim(concat(trim(a.`Title`), 'aaa')) = 'aaa');
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (trim('a' from concat(trim('a' from a.`Title`), 'aaa')) = a.`Title`);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (trim('b' from trim('a' from concat(trim('b' from trim('a' from a.`Title`)), 'aaa'))) = concat(a.`Title`, 1));
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE (trim('c' from trim('b' from trim('a' from concat(trim('c' from trim('b' from trim('a' from a.`Title`))), 'aaa')))) = a__Type.`Name`)
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void TrimStart() {
|
||||||
|
var data = new List<object>();
|
||||||
|
data.Add(select.Where(a => a.Title.TrimStart() == "aaa").ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.TrimStart('a') == a.Title).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.TrimStart('a', 'b') == (a.Title + 1)).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.TrimStart('a', 'b', 'c') == a.Type.Name).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (ltrim(a.`Title`) = 'aaa');
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (trim(trailing 'a' from trim(leading 'a' from a.`Title`)) = a.`Title`);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (trim(trailing 'b' from trim(leading 'b' from trim(trailing 'a' from trim(leading 'a' from a.`Title`)))) = concat(a.`Title`, 1));
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE (trim(trailing 'c' from trim(leading 'c' from trim(trailing 'b' from trim(leading 'b' from trim(trailing 'a' from trim(leading 'a' from a.`Title`)))))) = a__Type.`Name`);
|
||||||
|
data.Add(select.Where(a => (a.Title.TrimStart() + "aaa").TrimStart() == "aaa").ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.TrimStart('a') + "aaa").TrimStart('a') == a.Title).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.TrimStart('a', 'b') + "aaa").TrimStart('a', 'b') == (a.Title + 1)).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.TrimStart('a', 'b', 'c') + "aaa").TrimStart('a', 'b', 'c') == a.Type.Name).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (ltrim(concat(ltrim(a.`Title`), 'aaa')) = 'aaa');
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (trim(trailing 'a' from trim(leading 'a' from concat(trim(trailing 'a' from trim(leading 'a' from a.`Title`)), 'aaa'))) = a.`Title`);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (trim(trailing 'b' from trim(leading 'b' from trim(trailing 'a' from trim(leading 'a' from concat(trim(trailing 'b' from trim(leading 'b' from trim(trailing 'a' from trim(leading 'a' from a.`Title`)))), 'aaa'))))) = concat(a.`Title`, 1));
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE (trim(trailing 'c' from trim(leading 'c' from trim(trailing 'b' from trim(leading 'b' from trim(trailing 'a' from trim(leading 'a' from concat(trim(trailing 'c' from trim(leading 'c' from trim(trailing 'b' from trim(leading 'b' from trim(trailing 'a' from trim(leading 'a' from a.`Title`)))))), 'aaa'))))))) = a__Type.`Name`)
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void TrimEnd() {
|
||||||
|
var data = new List<object>();
|
||||||
|
data.Add(select.Where(a => a.Title.TrimEnd() == "aaa").ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.TrimEnd('a') == a.Title).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.TrimEnd('a', 'b') == (a.Title + 1)).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.TrimEnd('a', 'b', 'c') == a.Type.Name).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (rtrim(a.`Title`) = 'aaa');
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (trim(trailing 'a' from a.`Title`) = a.`Title`);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (trim(trailing 'b' from trim(trailing 'a' from a.`Title`)) = concat(a.`Title`, 1));
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE (trim(trailing 'c' from trim(trailing 'b' from trim(trailing 'a' from a.`Title`))) = a__Type.`Name`);
|
||||||
|
data.Add(select.Where(a => (a.Title.TrimEnd() + "aaa").TrimEnd() == "aaa").ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.TrimEnd('a') + "aaa").TrimEnd('a') == a.Title).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.TrimEnd('a', 'b') + "aaa").TrimEnd('a', 'b') == (a.Title + 1)).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.TrimEnd('a', 'b', 'c') + "aaa").TrimEnd('a', 'b', 'c') == a.Type.Name).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (rtrim(concat(rtrim(a.`Title`), 'aaa')) = 'aaa');
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (trim(trailing 'a' from concat(trim(trailing 'a' from a.`Title`), 'aaa')) = a.`Title`);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (trim(trailing 'b' from trim(trailing 'a' from concat(trim(trailing 'b' from trim(trailing 'a' from a.`Title`)), 'aaa'))) = concat(a.`Title`, 1));
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE (trim(trailing 'c' from trim(trailing 'b' from trim(trailing 'a' from concat(trim(trailing 'c' from trim(trailing 'b' from trim(trailing 'a' from a.`Title`))), 'aaa')))) = a__Type.`Name`)
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Replace() {
|
||||||
|
var data = new List<object>();
|
||||||
|
data.Add(select.Where(a => a.Title.Replace("a", "b") == "aaa").ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.Replace("a", "b").Replace("b", "c") == a.Title).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.Replace("a", "b").Replace("b", "c").Replace("c", "a") == (a.Title + 1)).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.Replace("a", "b").Replace("b", "c").Replace(a.Type.Name, "a") == a.Type.Name).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (replace(a.`Title`, 'a', 'b') = 'aaa');
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (replace(replace(a.`Title`, 'a', 'b'), 'b', 'c') = a.`Title`);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (replace(replace(replace(a.`Title`, 'a', 'b'), 'b', 'c'), 'c', 'a') = concat(a.`Title`, 1));
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE (replace(replace(replace(a.`Title`, 'a', 'b'), 'b', 'c'), a__Type.`Name`, 'a') = a__Type.`Name`);
|
||||||
|
data.Add(select.Where(a => (a.Title.Replace("a", "b") + "aaa").TrimEnd() == "aaa").ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.Replace("a", "b").Replace("b", "c") + "aaa").TrimEnd('a') == a.Title).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.Replace("a", "b").Replace("b", "c").Replace("c", "a") + "aaa").TrimEnd('a', 'b') == (a.Title + 1)).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title.Replace("a", "b").Replace("b", "c").Replace(a.Type.Name, "a") + "aaa").TrimEnd('a', 'b', 'c') == a.Type.Name).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (concat(replace(a.`Title`, 'a', 'b'), 'aaa') = 'aaa');
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (concat(replace(replace(a.`Title`, 'a', 'b'), 'b', 'c'), 'aaa') = a.`Title`);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (concat(replace(replace(replace(a.`Title`, 'a', 'b'), 'b', 'c'), 'c', 'a'), 'aaa') = concat(a.`Title`, 1));
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE (concat(replace(replace(replace(a.`Title`, 'a', 'b'), 'b', 'c'), a__Type.`Name`, 'a'), 'aaa') = a__Type.`Name`)
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void CompareTo() {
|
||||||
|
var data = new List<object>();
|
||||||
|
data.Add(select.Where(a => a.Title.CompareTo(a.Title) == 0).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.CompareTo(a.Title) > 0).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.CompareTo(a.Title + 1) == 0).ToList());
|
||||||
|
data.Add(select.Where(a => a.Title.CompareTo(a.Title + a.Type.Name) == 0).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (strcmp(a.`Title`, a.`Title`) = 0);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (strcmp(a.`Title`, a.`Title`) > 0);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (strcmp(a.`Title`, concat(a.`Title`, 1)) = 0);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE (strcmp(a.`Title`, concat(a.`Title`, a__Type.`Name`)) = 0);
|
||||||
|
data.Add(select.Where(a => (a.Title + "aaa").CompareTo("aaa") == 0).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title + "aaa").CompareTo(a.Title) > 0).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title + "aaa").CompareTo(a.Title + 1) == 0).ToList());
|
||||||
|
data.Add(select.Where(a => (a.Title + "aaa").CompareTo(a.Type.Name) == 0).ToList());
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (strcmp(concat(a.`Title`, 'aaa'), 'aaa') = 0);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (strcmp(concat(a.`Title`, 'aaa'), a.`Title`) > 0);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
|
||||||
|
//FROM `tb_topic` a
|
||||||
|
//WHERE (strcmp(concat(a.`Title`, 'aaa'), concat(a.`Title`, 1)) = 0);
|
||||||
|
|
||||||
|
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8
|
||||||
|
//FROM `tb_topic` a, `TestTypeInfo` a__Type
|
||||||
|
//WHERE (strcmp(concat(a.`Title`, 'aaa'), a__Type.`Name`) = 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -4,8 +4,8 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace FreeSql.Tests.MySql {
|
namespace FreeSql.Tests.MySql.Expression {
|
||||||
public class MySqlExpressionTest {
|
public class TimeSpanTest {
|
||||||
|
|
||||||
ISelect<Topic> select => g.mysql.Select<Topic>();
|
ISelect<Topic> select => g.mysql.Select<Topic>();
|
||||||
|
|
||||||
@ -33,50 +33,43 @@ namespace FreeSql.Tests.MySql {
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void StartsWith() {
|
public void Days() {
|
||||||
}
|
}
|
||||||
[Fact]
|
[Fact]
|
||||||
public void EndsWith() {
|
public void Hours() {
|
||||||
}
|
}
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Contains() {
|
public void Milliseconds() {
|
||||||
}
|
}
|
||||||
[Fact]
|
[Fact]
|
||||||
public void ToLower() {
|
public void Minutes() {
|
||||||
}
|
}
|
||||||
[Fact]
|
[Fact]
|
||||||
public void ToUpper() {
|
public void Seconds() {
|
||||||
|
|
||||||
}
|
}
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Substring() {
|
public void Ticks() {
|
||||||
}
|
}
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Length() {
|
public void TotalDays() {
|
||||||
}
|
}
|
||||||
[Fact]
|
[Fact]
|
||||||
public void IndexOf() {
|
public void TotalHours() {
|
||||||
}
|
}
|
||||||
[Fact]
|
[Fact]
|
||||||
public void PadLeft() {
|
public void TotalMilliseconds() {
|
||||||
}
|
}
|
||||||
[Fact]
|
[Fact]
|
||||||
public void PadRight() {
|
public void TotalMinutes() {
|
||||||
}
|
}
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Trim() {
|
public void TotalSeconds() {
|
||||||
}
|
}
|
||||||
[Fact]
|
[Fact]
|
||||||
public void TrimStart() {
|
public void Add() {
|
||||||
}
|
}
|
||||||
[Fact]
|
[Fact]
|
||||||
public void TrimEnd() {
|
public void Subtract() {
|
||||||
}
|
|
||||||
[Fact]
|
|
||||||
public void Replace() {
|
|
||||||
}
|
|
||||||
[Fact]
|
|
||||||
public void CompareTo() {
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
73
FreeSql.Tests/SqlServer/Curd/SqlServerDeleteTest.cs
Normal file
73
FreeSql.Tests/SqlServer/Curd/SqlServerDeleteTest.cs
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
using FreeSql.DataAnnotations;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace FreeSql.Tests.SqlServer {
|
||||||
|
public class SqlServerDeleteTest {
|
||||||
|
|
||||||
|
IDelete<Topic> delete => g.sqlserver.Delete<Topic>(); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
|
||||||
|
[Table(Name = "tb_topic")]
|
||||||
|
class Topic {
|
||||||
|
[Column(IsIdentity = true, IsPrimary = true)]
|
||||||
|
public int Id { get; set; }
|
||||||
|
public int Clicks { get; set; }
|
||||||
|
public TestTypeInfo Type { get; set; }
|
||||||
|
public string Title { get; set; }
|
||||||
|
public DateTime CreateTime { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Dywhere() {
|
||||||
|
Assert.Null(g.sqlserver.Delete<Topic>().ToSql());
|
||||||
|
var sql = g.sqlserver.Delete<Topic>(new[] { 1, 2 }).ToSql();
|
||||||
|
Assert.Equal("DELETE FROM [tb_topic] WHERE ([Id] = 1 OR [Id] = 2)", sql);
|
||||||
|
|
||||||
|
sql = g.sqlserver.Delete<Topic>(new Topic { Id = 1, Title = "test" }).ToSql();
|
||||||
|
Assert.Equal("DELETE FROM [tb_topic] WHERE ([Id] = 1)", sql);
|
||||||
|
|
||||||
|
sql = g.sqlserver.Delete<Topic>(new[] { new Topic { Id = 1, Title = "test" }, new Topic { Id = 2, Title = "test" } }).ToSql();
|
||||||
|
Assert.Equal("DELETE FROM [tb_topic] WHERE ([Id] = 1 OR [Id] = 2)", sql);
|
||||||
|
|
||||||
|
sql = g.sqlserver.Delete<Topic>(new { id = 1 }).ToSql();
|
||||||
|
Assert.Equal("DELETE FROM [tb_topic] WHERE ([Id] = 1)", sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Where() {
|
||||||
|
var sql = delete.Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("DELETE FROM [tb_topic] WHERE ([Id] = 1)", sql);
|
||||||
|
|
||||||
|
sql = delete.Where("id = ?id", new { id = 1 }).ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("DELETE FROM [tb_topic] WHERE (id = ?id)", sql);
|
||||||
|
|
||||||
|
var item = new Topic { Id = 1, Title = "newtitle" };
|
||||||
|
sql = delete.Where(item).ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("DELETE FROM [tb_topic] WHERE ([Id] = 1)", sql);
|
||||||
|
|
||||||
|
var items = new List<Topic>();
|
||||||
|
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
|
||||||
|
|
||||||
|
sql = delete.Where(items).ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("DELETE FROM [tb_topic] WHERE ([Id] IN (1,2,3,4,5,6,7,8,9,10))", sql);
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void WhereExists() {
|
||||||
|
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void ExecuteAffrows() {
|
||||||
|
|
||||||
|
var id = g.sqlserver.Insert<Topic>(new Topic { Title = "xxxx", CreateTime = DateTime.Now }).ExecuteIdentity();
|
||||||
|
Assert.Equal(1, delete.Where(a => a.Id == id).ExecuteAffrows());
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void ExecuteDeleted() {
|
||||||
|
|
||||||
|
var item = g.sqlserver.Insert<Topic>(new Topic { Title = "xxxx", CreateTime = DateTime.Now }).ExecuteInserted();
|
||||||
|
Assert.Equal(item[0].Id, delete.Where(a => a.Id == item[0].Id).ExecuteDeleted()[0].Id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
85
FreeSql.Tests/SqlServer/Curd/SqlServerInsertTest.cs
Normal file
85
FreeSql.Tests/SqlServer/Curd/SqlServerInsertTest.cs
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
using FreeSql.DataAnnotations;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace FreeSql.Tests.SqlServer {
|
||||||
|
public class SqlServerInsertTest {
|
||||||
|
|
||||||
|
IInsert<Topic> insert => g.sqlserver.Insert<Topic>(); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
|
||||||
|
[Table(Name = "tb_topic")]
|
||||||
|
class Topic {
|
||||||
|
[Column(IsIdentity = true, IsPrimary = true)]
|
||||||
|
public int Id { get; set; }
|
||||||
|
public int Clicks { get; set; }
|
||||||
|
public TestTypeInfo Type { get; set; }
|
||||||
|
public string Title { get; set; }
|
||||||
|
public DateTime CreateTime { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void AppendData() {
|
||||||
|
var items = new List<Topic>();
|
||||||
|
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100, CreateTime = DateTime.Now });
|
||||||
|
|
||||||
|
var sql = insert.AppendData(items.First()).ToSql();
|
||||||
|
Assert.Equal("INSERT INTO [tb_topic]([Clicks], [Title], [CreateTime]) VALUES(@Clicks0, @Title0, @CreateTime0)", sql);
|
||||||
|
|
||||||
|
sql = insert.AppendData(items).ToSql();
|
||||||
|
Assert.Equal("INSERT INTO [tb_topic]([Clicks], [Title], [CreateTime]) VALUES(@Clicks0, @Title0, @CreateTime0), (@Clicks1, @Title1, @CreateTime1), (@Clicks2, @Title2, @CreateTime2), (@Clicks3, @Title3, @CreateTime3), (@Clicks4, @Title4, @CreateTime4), (@Clicks5, @Title5, @CreateTime5), (@Clicks6, @Title6, @CreateTime6), (@Clicks7, @Title7, @CreateTime7), (@Clicks8, @Title8, @CreateTime8), (@Clicks9, @Title9, @CreateTime9)", sql);
|
||||||
|
|
||||||
|
sql = insert.AppendData(items).InsertColumns(a => a.Title).ToSql();
|
||||||
|
Assert.Equal("INSERT INTO [tb_topic]([Title]) VALUES(@Title0), (@Title1), (@Title2), (@Title3), (@Title4), (@Title5), (@Title6), (@Title7), (@Title8), (@Title9)", sql);
|
||||||
|
|
||||||
|
sql = insert.AppendData(items).IgnoreColumns(a => a.CreateTime).ToSql();
|
||||||
|
Assert.Equal("INSERT INTO [tb_topic]([Clicks], [Title]) VALUES(@Clicks0, @Title0), (@Clicks1, @Title1), (@Clicks2, @Title2), (@Clicks3, @Title3), (@Clicks4, @Title4), (@Clicks5, @Title5), (@Clicks6, @Title6), (@Clicks7, @Title7), (@Clicks8, @Title8), (@Clicks9, @Title9)", sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void InsertColumns() {
|
||||||
|
var items = new List<Topic>();
|
||||||
|
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100, CreateTime = DateTime.Now });
|
||||||
|
|
||||||
|
var sql = insert.AppendData(items).InsertColumns(a => a.Title).ToSql();
|
||||||
|
Assert.Equal("INSERT INTO [tb_topic]([Title]) VALUES(@Title0), (@Title1), (@Title2), (@Title3), (@Title4), (@Title5), (@Title6), (@Title7), (@Title8), (@Title9)", sql);
|
||||||
|
|
||||||
|
sql = insert.AppendData(items).InsertColumns(a =>new { a.Title, a.Clicks }).ToSql();
|
||||||
|
Assert.Equal("INSERT INTO [tb_topic]([Clicks], [Title]) VALUES(@Clicks0, @Title0), (@Clicks1, @Title1), (@Clicks2, @Title2), (@Clicks3, @Title3), (@Clicks4, @Title4), (@Clicks5, @Title5), (@Clicks6, @Title6), (@Clicks7, @Title7), (@Clicks8, @Title8), (@Clicks9, @Title9)", sql);
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void IgnoreColumns() {
|
||||||
|
var items = new List<Topic>();
|
||||||
|
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100, CreateTime = DateTime.Now });
|
||||||
|
|
||||||
|
var sql = insert.AppendData(items).IgnoreColumns(a => a.CreateTime).ToSql();
|
||||||
|
Assert.Equal("INSERT INTO [tb_topic]([Clicks], [Title]) VALUES(@Clicks0, @Title0), (@Clicks1, @Title1), (@Clicks2, @Title2), (@Clicks3, @Title3), (@Clicks4, @Title4), (@Clicks5, @Title5), (@Clicks6, @Title6), (@Clicks7, @Title7), (@Clicks8, @Title8), (@Clicks9, @Title9)", sql);
|
||||||
|
|
||||||
|
sql = insert.AppendData(items).IgnoreColumns(a => new { a.Title, a.CreateTime }).ToSql();
|
||||||
|
Assert.Equal("INSERT INTO [tb_topic]([Clicks]) VALUES(@Clicks0), (@Clicks1), (@Clicks2), (@Clicks3), (@Clicks4), (@Clicks5), (@Clicks6), (@Clicks7), (@Clicks8), (@Clicks9)", sql);
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void ExecuteAffrows() {
|
||||||
|
var items = new List<Topic>();
|
||||||
|
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100, CreateTime = DateTime.Now });
|
||||||
|
|
||||||
|
Assert.Equal(1, insert.AppendData(items.First()).ExecuteAffrows());
|
||||||
|
Assert.Equal(10, insert.AppendData(items).ExecuteAffrows());
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void ExecuteIdentity() {
|
||||||
|
var items = new List<Topic>();
|
||||||
|
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100, CreateTime = DateTime.Now });
|
||||||
|
|
||||||
|
Assert.NotEqual(0, insert.AppendData(items.First()).ExecuteIdentity());
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void ExecuteInserted() {
|
||||||
|
var items = new List<Topic>();
|
||||||
|
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100, CreateTime = DateTime.Now });
|
||||||
|
|
||||||
|
var items2 = insert.AppendData(items).ExecuteInserted();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
501
FreeSql.Tests/SqlServer/Curd/SqlServerSelectTest.cs
Normal file
501
FreeSql.Tests/SqlServer/Curd/SqlServerSelectTest.cs
Normal file
@ -0,0 +1,501 @@
|
|||||||
|
using FreeSql.DataAnnotations;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace FreeSql.Tests.SqlServer {
|
||||||
|
public class SqlServerSelectTest {
|
||||||
|
|
||||||
|
ISelect<Topic> select => g.sqlserver.Select<Topic>();
|
||||||
|
|
||||||
|
[Table(Name = "tb_topic22")]
|
||||||
|
class Topic {
|
||||||
|
[Column(IsIdentity = true, IsPrimary = true)]
|
||||||
|
public int Id { get; set; }
|
||||||
|
public int Clicks { get; set; }
|
||||||
|
public int TestTypeInfoGuid { get; set; }
|
||||||
|
public TestTypeInfo Type { get; set; }
|
||||||
|
public string Title { get; set; }
|
||||||
|
public DateTime CreateTime { get; set; }
|
||||||
|
}
|
||||||
|
class TestTypeInfo {
|
||||||
|
public int Guid { get; set; }
|
||||||
|
public int ParentId { get; set; }
|
||||||
|
public TestTypeParentInfo Parent { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
}
|
||||||
|
class TestTypeParentInfo {
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public List<TestTypeInfo> Types { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ToList() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void ToOne() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void ToSql() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Any() {
|
||||||
|
var count = select.Where(a => 1 == 1).Count();
|
||||||
|
Assert.False(select.Where(a => 1 == 2).Any());
|
||||||
|
Assert.Equal(count > 0, select.Where(a => 1 == 1).Any());
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Count() {
|
||||||
|
var count = select.Where(a => 1 == 1).Count();
|
||||||
|
select.Where(a => 1 == 1).Count(out var count2);
|
||||||
|
Assert.Equal(count, count2);
|
||||||
|
Assert.Equal(0, select.Where(a => 1 == 2).Count());
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Master() {
|
||||||
|
Assert.StartsWith(" SELECT", select.Master().Where(a => 1 == 1).ToSql());
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Caching() {
|
||||||
|
var result1 = select.Where(a => 1 == 1).Caching(20, "testcaching").ToList();
|
||||||
|
var testcaching1 = g.sqlserver.Cache.Get("testcaching");
|
||||||
|
Assert.NotNull(testcaching1);
|
||||||
|
var result2 = select.Where(a => 1 == 1).Caching(20, "testcaching").ToList();
|
||||||
|
var testcaching2 = g.sqlserver.Cache.Get("testcaching");
|
||||||
|
Assert.NotNull(testcaching2);
|
||||||
|
Assert.Equal(result1.Count, result1.Count);
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void From() {
|
||||||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
var query2 = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
|
||||||
|
.LeftJoin(a => a.TestTypeInfoGuid == b.Guid)
|
||||||
|
.LeftJoin(a => b.ParentId == c.Id)
|
||||||
|
);
|
||||||
|
var sql = query2.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, b.[Guid] as4, b.[ParentId] as5, b.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a LEFT JOIN [TestTypeInfo] b ON a.[TestTypeInfoGuid] = b.[Guid] LEFT JOIN [TestTypeParentInfo] c ON b.[ParentId] = c.[Id]", sql);
|
||||||
|
query2.ToList();
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void LeftJoin() {
|
||||||
|
//<2F><><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>a.Type<70><65>a.Type.Parent <20><><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
var query = select.LeftJoin(a => a.Type.Guid == a.TestTypeInfoGuid);
|
||||||
|
var sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a__Type.[Guid] as4, a__Type.[ParentId] as5, a__Type.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a LEFT JOIN [TestTypeInfo] a__Type ON a__Type.[Guid] = a.[TestTypeInfoGuid]", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.LeftJoin(a => a.Type.Guid == a.TestTypeInfoGuid && a.Type.Name == "xxx");
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a__Type.[Guid] as4, a__Type.[ParentId] as5, a__Type.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a LEFT JOIN [TestTypeInfo] a__Type ON a__Type.[Guid] = a.[TestTypeInfoGuid] AND a__Type.[Name] = 'xxx'", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.LeftJoin(a => a.Type.Guid == a.TestTypeInfoGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a__Type.[Guid] as4, a__Type.[ParentId] as5, a__Type.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a LEFT JOIN [TestTypeParentInfo] a__Type__Parent ON 1 = 1 LEFT JOIN [TestTypeInfo] a__Type ON a__Type.[Guid] = a.[TestTypeInfoGuid] AND a__Type.[Name] = 'xxx' WHERE (a__Type__Parent.[Id] = 10)", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TestTypeInfoGuid);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, b.[Guid] as4, b.[ParentId] as5, b.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a LEFT JOIN [TestTypeInfo] b ON b.[Guid] = a.[TestTypeInfoGuid]", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TestTypeInfoGuid && b.Name == "xxx");
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, b.[Guid] as4, b.[ParentId] as5, b.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a LEFT JOIN [TestTypeInfo] b ON b.[Guid] = a.[TestTypeInfoGuid] AND b.[Name] = 'xxx'", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TestTypeInfoGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, b.[Guid] as4, b.[ParentId] as5, b.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a LEFT JOIN [TestTypeParentInfo] b__Parent ON 1 = 1 LEFT JOIN [TestTypeInfo] b ON b.[Guid] = a.[TestTypeInfoGuid] AND b.[Name] = 'xxx' WHERE (b__Parent.[Id] = 10)", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
query = select
|
||||||
|
.LeftJoin(a => a.Type.Guid == a.TestTypeInfoGuid)
|
||||||
|
.LeftJoin(a => a.Type.Parent.Id == a.Type.ParentId);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a__Type.[Guid] as4, a__Type.[ParentId] as5, a__Type.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a LEFT JOIN [TestTypeInfo] a__Type ON a__Type.[Guid] = a.[TestTypeInfoGuid] LEFT JOIN [TestTypeParentInfo] a__Type__Parent ON a__Type__Parent.[Id] = a__Type.[ParentId]", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select
|
||||||
|
.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TestTypeInfoGuid)
|
||||||
|
.LeftJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, b.[Guid] as4, b.[ParentId] as5, b.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a LEFT JOIN [TestTypeInfo] b ON b.[Guid] = a.[TestTypeInfoGuid] LEFT JOIN [TestTypeParentInfo] c ON c.[Id] = b.[ParentId]", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ
|
||||||
|
var query2 = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
|
||||||
|
.LeftJoin(a => a.TestTypeInfoGuid == b.Guid)
|
||||||
|
.LeftJoin(a => b.ParentId == c.Id));
|
||||||
|
sql = query2.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, b.[Guid] as4, b.[ParentId] as5, b.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a LEFT JOIN [TestTypeInfo] b ON a.[TestTypeInfoGuid] = b.[Guid] LEFT JOIN [TestTypeParentInfo] c ON b.[ParentId] = c.[Id]", sql);
|
||||||
|
query2.ToList();
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD>㲻<EFBFBD><E3B2BB>
|
||||||
|
query = select.LeftJoin("TestTypeInfo b on b.Guid = a.TestTypeInfoGuid");
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a LEFT JOIN TestTypeInfo b on b.Guid = a.TestTypeInfoGuid", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.LeftJoin("TestTypeInfo b on b.Guid = a.TestTypeInfoGuid and b.Name = @bname", new { bname = "xxx" });
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a LEFT JOIN TestTypeInfo b on b.Guid = a.TestTypeInfoGuid and b.Name = @bname", sql);
|
||||||
|
query.ToList();
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void InnerJoin() {
|
||||||
|
//<2F><><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>a.Type<70><65>a.Type.Parent <20><><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
var query = select.InnerJoin(a => a.Type.Guid == a.TestTypeInfoGuid);
|
||||||
|
var sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a__Type.[Guid] as4, a__Type.[ParentId] as5, a__Type.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a INNER JOIN [TestTypeInfo] a__Type ON a__Type.[Guid] = a.[TestTypeInfoGuid]", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.InnerJoin(a => a.Type.Guid == a.TestTypeInfoGuid && a.Type.Name == "xxx");
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a__Type.[Guid] as4, a__Type.[ParentId] as5, a__Type.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a INNER JOIN [TestTypeInfo] a__Type ON a__Type.[Guid] = a.[TestTypeInfoGuid] AND a__Type.[Name] = 'xxx'", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.InnerJoin(a => a.Type.Guid == a.TestTypeInfoGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a__Type.[Guid] as4, a__Type.[ParentId] as5, a__Type.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a LEFT JOIN [TestTypeParentInfo] a__Type__Parent ON 1 = 1 INNER JOIN [TestTypeInfo] a__Type ON a__Type.[Guid] = a.[TestTypeInfoGuid] AND a__Type.[Name] = 'xxx' WHERE (a__Type__Parent.[Id] = 10)", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
query = select.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TestTypeInfoGuid);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, b.[Guid] as4, b.[ParentId] as5, b.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a INNER JOIN [TestTypeInfo] b ON b.[Guid] = a.[TestTypeInfoGuid]", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TestTypeInfoGuid && b.Name == "xxx");
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, b.[Guid] as4, b.[ParentId] as5, b.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a INNER JOIN [TestTypeInfo] b ON b.[Guid] = a.[TestTypeInfoGuid] AND b.[Name] = 'xxx'", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TestTypeInfoGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, b.[Guid] as4, b.[ParentId] as5, b.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a LEFT JOIN [TestTypeParentInfo] b__Parent ON 1 = 1 INNER JOIN [TestTypeInfo] b ON b.[Guid] = a.[TestTypeInfoGuid] AND b.[Name] = 'xxx' WHERE (b__Parent.[Id] = 10)", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
query = select
|
||||||
|
.InnerJoin(a => a.Type.Guid == a.TestTypeInfoGuid)
|
||||||
|
.InnerJoin(a => a.Type.Parent.Id == a.Type.ParentId);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a__Type.[Guid] as4, a__Type.[ParentId] as5, a__Type.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a INNER JOIN [TestTypeInfo] a__Type ON a__Type.[Guid] = a.[TestTypeInfoGuid] INNER JOIN [TestTypeParentInfo] a__Type__Parent ON a__Type__Parent.[Id] = a__Type.[ParentId]", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select
|
||||||
|
.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TestTypeInfoGuid)
|
||||||
|
.InnerJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, b.[Guid] as4, b.[ParentId] as5, b.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a INNER JOIN [TestTypeInfo] b ON b.[Guid] = a.[TestTypeInfoGuid] INNER JOIN [TestTypeParentInfo] c ON c.[Id] = b.[ParentId]", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ
|
||||||
|
var query2 = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
|
||||||
|
.InnerJoin(a => a.TestTypeInfoGuid == b.Guid)
|
||||||
|
.InnerJoin(a => b.ParentId == c.Id));
|
||||||
|
sql = query2.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, b.[Guid] as4, b.[ParentId] as5, b.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a INNER JOIN [TestTypeInfo] b ON a.[TestTypeInfoGuid] = b.[Guid] INNER JOIN [TestTypeParentInfo] c ON b.[ParentId] = c.[Id]", sql);
|
||||||
|
query2.ToList();
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD>㲻<EFBFBD><E3B2BB>
|
||||||
|
query = select.InnerJoin("TestTypeInfo b on b.Guid = a.TestTypeInfoGuid");
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a INNER JOIN TestTypeInfo b on b.Guid = a.TestTypeInfoGuid", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.InnerJoin("TestTypeInfo b on b.Guid = a.TestTypeInfoGuid and b.Name = @bname", new { bname = "xxx" });
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a INNER JOIN TestTypeInfo b on b.Guid = a.TestTypeInfoGuid and b.Name = @bname", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void RightJoin() {
|
||||||
|
//<2F><><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>a.Type<70><65>a.Type.Parent <20><><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
var query = select.RightJoin(a => a.Type.Guid == a.TestTypeInfoGuid);
|
||||||
|
var sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a__Type.[Guid] as4, a__Type.[ParentId] as5, a__Type.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a RIGHT JOIN [TestTypeInfo] a__Type ON a__Type.[Guid] = a.[TestTypeInfoGuid]", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.RightJoin(a => a.Type.Guid == a.TestTypeInfoGuid && a.Type.Name == "xxx");
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a__Type.[Guid] as4, a__Type.[ParentId] as5, a__Type.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a RIGHT JOIN [TestTypeInfo] a__Type ON a__Type.[Guid] = a.[TestTypeInfoGuid] AND a__Type.[Name] = 'xxx'", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.RightJoin(a => a.Type.Guid == a.TestTypeInfoGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a__Type.[Guid] as4, a__Type.[ParentId] as5, a__Type.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a LEFT JOIN [TestTypeParentInfo] a__Type__Parent ON 1 = 1 RIGHT JOIN [TestTypeInfo] a__Type ON a__Type.[Guid] = a.[TestTypeInfoGuid] AND a__Type.[Name] = 'xxx' WHERE (a__Type__Parent.[Id] = 10)", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
query = select.RightJoin<TestTypeInfo>((a, b) => b.Guid == a.TestTypeInfoGuid);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, b.[Guid] as4, b.[ParentId] as5, b.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a RIGHT JOIN [TestTypeInfo] b ON b.[Guid] = a.[TestTypeInfoGuid]", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.RightJoin<TestTypeInfo>((a, b) => b.Guid == a.TestTypeInfoGuid && b.Name == "xxx");
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, b.[Guid] as4, b.[ParentId] as5, b.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a RIGHT JOIN [TestTypeInfo] b ON b.[Guid] = a.[TestTypeInfoGuid] AND b.[Name] = 'xxx'", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.RightJoin<TestTypeInfo>((a, b) => b.Guid == a.TestTypeInfoGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, b.[Guid] as4, b.[ParentId] as5, b.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a LEFT JOIN [TestTypeParentInfo] b__Parent ON 1 = 1 RIGHT JOIN [TestTypeInfo] b ON b.[Guid] = a.[TestTypeInfoGuid] AND b.[Name] = 'xxx' WHERE (b__Parent.[Id] = 10)", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
query = select
|
||||||
|
.RightJoin(a => a.Type.Guid == a.TestTypeInfoGuid)
|
||||||
|
.RightJoin(a => a.Type.Parent.Id == a.Type.ParentId);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a__Type.[Guid] as4, a__Type.[ParentId] as5, a__Type.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a RIGHT JOIN [TestTypeInfo] a__Type ON a__Type.[Guid] = a.[TestTypeInfoGuid] RIGHT JOIN [TestTypeParentInfo] a__Type__Parent ON a__Type__Parent.[Id] = a__Type.[ParentId]", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select
|
||||||
|
.RightJoin<TestTypeInfo>((a, b) => b.Guid == a.TestTypeInfoGuid)
|
||||||
|
.RightJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, b.[Guid] as4, b.[ParentId] as5, b.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a RIGHT JOIN [TestTypeInfo] b ON b.[Guid] = a.[TestTypeInfoGuid] RIGHT JOIN [TestTypeParentInfo] c ON c.[Id] = b.[ParentId]", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ
|
||||||
|
var query2 = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
|
||||||
|
.RightJoin(a => a.TestTypeInfoGuid == b.Guid)
|
||||||
|
.RightJoin(a => b.ParentId == c.Id));
|
||||||
|
sql = query2.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, b.[Guid] as4, b.[ParentId] as5, b.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a RIGHT JOIN [TestTypeInfo] b ON a.[TestTypeInfoGuid] = b.[Guid] RIGHT JOIN [TestTypeParentInfo] c ON b.[ParentId] = c.[Id]", sql);
|
||||||
|
query2.ToList();
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD>㲻<EFBFBD><E3B2BB>
|
||||||
|
query = select.RightJoin("TestTypeInfo b on b.Guid = a.TestTypeInfoGuid");
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a RIGHT JOIN TestTypeInfo b on b.Guid = a.TestTypeInfoGuid", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.RightJoin("TestTypeInfo b on b.Guid = a.TestTypeInfoGuid and b.Name = @bname", new { bname = "xxx" });
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a RIGHT JOIN TestTypeInfo b on b.Guid = a.TestTypeInfoGuid and b.Name = @bname", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Where() {
|
||||||
|
//<2F><><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>a.Type<70><65>a.Type.Parent <20><><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
var query = select.Where(a => a.Id == 10);
|
||||||
|
var sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a WHERE (a.[Id] = 10)", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.Where(a => a.Id == 10 && a.Id > 10 || a.Clicks > 100);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a WHERE (a.[Id] = 10 AND a.[Id] > 10 OR a.[Clicks] > 100)", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.Where(a => a.Id == 10).Where(a => a.Clicks > 100);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a WHERE (a.[Id] = 10) AND (a.[Clicks] > 100)", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.Where(a => a.Type.Name == "typeTitle");
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a__Type.[Guid] as4, a__Type.[ParentId] as5, a__Type.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a, [TestTypeInfo] a__Type WHERE (a__Type.[Name] = 'typeTitle')", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.Where(a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TestTypeInfoGuid);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a__Type.[Guid] as4, a__Type.[ParentId] as5, a__Type.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a, [TestTypeInfo] a__Type WHERE (a__Type.[Name] = 'typeTitle' AND a__Type.[Guid] = a.[TestTypeInfoGuid])", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.Where(a => a.Type.Parent.Name == "tparent");
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a__Type.[Guid] as4, a__Type.[ParentId] as5, a__Type.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a, [TestTypeInfo] a__Type, [TestTypeParentInfo] a__Type__Parent WHERE (a__Type__Parent.[Name] = 'tparent')", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD><D4A3><EFBFBD><F2B5A5B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
query = select.Where<TestTypeInfo>((a, b) => b.Guid == a.TestTypeInfoGuid && b.Name == "typeTitle");
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, b.[Guid] as4, b.[ParentId] as5, b.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a, [TestTypeInfo] b WHERE (b.[Guid] = a.[TestTypeInfoGuid] AND b.[Name] = 'typeTitle')", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.Where<TestTypeInfo>((a, b) => b.Name == "typeTitle" && b.Guid == a.TestTypeInfoGuid);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, b.[Guid] as4, b.[ParentId] as5, b.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a, [TestTypeInfo] b WHERE (b.[Name] = 'typeTitle' AND b.[Guid] = a.[TestTypeInfoGuid])", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.Where<TestTypeInfo, TestTypeParentInfo>((a, b, c) => c.Name == "tparent");
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a, [TestTypeParentInfo] c WHERE (c.[Name] = 'tparent')", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> From <20><>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
var query2 = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
|
||||||
|
.Where(a => a.Id == 10 && c.Name == "xxx")
|
||||||
|
.Where(a => b.ParentId == 20));
|
||||||
|
sql = query2.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, b.[Guid] as4, b.[ParentId] as5, b.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a, [TestTypeParentInfo] c, [TestTypeInfo] b WHERE (a.[Id] = 10 AND c.[Name] = 'xxx') AND (b.[ParentId] = 20)", sql);
|
||||||
|
query2.ToList();
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD>㲻<EFBFBD><E3B2BB>
|
||||||
|
query = select.Where("a.clicks > 100 and a.id = @id", new { id = 10 });
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a WHERE (a.clicks > 100 and a.id = @id)", sql);
|
||||||
|
query.ToList();
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void WhereIf() {
|
||||||
|
//<2F><><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>a.Type<70><65>a.Type.Parent <20><><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
var query = select.WhereIf(true, a => a.Id == 10);
|
||||||
|
var sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a WHERE (a.[Id] = 10)", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.WhereIf(true, a => a.Id == 10 && a.Id > 10 || a.Clicks > 100);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a WHERE (a.[Id] = 10 AND a.[Id] > 10 OR a.[Clicks] > 100)", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.WhereIf(true, a => a.Id == 10).WhereIf(true, a => a.Clicks > 100);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a WHERE (a.[Id] = 10) AND (a.[Clicks] > 100)", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.WhereIf(true, a => a.Type.Name == "typeTitle");
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a__Type.[Guid] as4, a__Type.[ParentId] as5, a__Type.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a, [TestTypeInfo] a__Type WHERE (a__Type.[Name] = 'typeTitle')", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.WhereIf(true, a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TestTypeInfoGuid);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a__Type.[Guid] as4, a__Type.[ParentId] as5, a__Type.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a, [TestTypeInfo] a__Type WHERE (a__Type.[Name] = 'typeTitle' AND a__Type.[Guid] = a.[TestTypeInfoGuid])", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.WhereIf(true, a => a.Type.Parent.Name == "tparent");
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a__Type.[Guid] as4, a__Type.[ParentId] as5, a__Type.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a, [TestTypeInfo] a__Type, [TestTypeParentInfo] a__Type__Parent WHERE (a__Type__Parent.[Name] = 'tparent')", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> From <20><>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
var query2 = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
|
||||||
|
.WhereIf(true, a => a.Id == 10 && c.Name == "xxx")
|
||||||
|
.WhereIf(true, a => b.ParentId == 20));
|
||||||
|
sql = query2.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, b.[Guid] as4, b.[ParentId] as5, b.[Name] as6, a.[Title] as7, a.[CreateTime] as8 FROM [tb_topic22] a, [TestTypeParentInfo] c, [TestTypeInfo] b WHERE (a.[Id] = 10 AND c.[Name] = 'xxx') AND (b.[ParentId] = 20)", sql);
|
||||||
|
query2.ToList();
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD>㲻<EFBFBD><E3B2BB>
|
||||||
|
query = select.WhereIf(true, "a.clicks > 100 and a.id = @id", new { id = 10 });
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a WHERE (a.clicks > 100 and a.id = @id)", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
// ==========================================WhereIf(false)
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>a.Type<70><65>a.Type.Parent <20><><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
query = select.WhereIf(false, a => a.Id == 10);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.WhereIf(false, a => a.Id == 10 && a.Id > 10 || a.Clicks > 100);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.WhereIf(false, a => a.Id == 10).WhereIf(false, a => a.Clicks > 100);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.WhereIf(false, a => a.Type.Name == "typeTitle");
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.WhereIf(false, a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TestTypeInfoGuid);
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
query = select.WhereIf(false, a => a.Type.Parent.Name == "tparent");
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a", sql);
|
||||||
|
query.ToList();
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> From <20><>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
query2 = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
|
||||||
|
.WhereIf(false, a => a.Id == 10 && c.Name == "xxx")
|
||||||
|
.WhereIf(false, a => b.ParentId == 20));
|
||||||
|
sql = query2.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a", sql);
|
||||||
|
query2.ToList();
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD>㲻<EFBFBD><E3B2BB>
|
||||||
|
query = select.WhereIf(false, "a.clicks > 100 and a.id = @id", new { id = 10 });
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("SELECT a.[Id] as1, a.[Clicks] as2, a.[TestTypeInfoGuid] as3, a.[Title] as4, a.[CreateTime] as5 FROM [tb_topic22] a", sql);
|
||||||
|
query.ToList();
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void WhereLike() {
|
||||||
|
//ģ<><C4A3><EFBFBD><EFBFBD>ѯ<EFBFBD><D1AF>WhereLike(a => a.Title, "%sql")
|
||||||
|
var query = select.Where(a => a.Title.StartsWith("ss")).Where(a => a.Type.Name.Contains("sss"));
|
||||||
|
var sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
|
||||||
|
query = select.Where(a => a.Title.EndsWith("ss"));
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
|
||||||
|
query = select.Where(a => a.Title.Contains("ss"));
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
|
||||||
|
query = select.WhereLike(a => a.Title, "%ss");
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
|
||||||
|
query = select.WhereLike(a => a.Title, "%ss").WhereLike(a => a.Title, "%aa");
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
|
||||||
|
//ģ<><C4A3><EFBFBD><EFBFBD>ѯ<EFBFBD><D1AF>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD> OR<4F><52>WhereLike(a => new[] { a.Title, a.Content }, "%sql%")
|
||||||
|
query = select.WhereLike(a => new[] { a.Title, a.Type.Name, a.Type.Parent.Name }, "%aa");
|
||||||
|
sql = query.ToSql().Replace("\r\n", "");
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void GroupBy() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Having() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void OrderBy() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Skip_Offset() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Take_Limit() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Page() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Sum() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Min() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Max() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Avg() {
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void As() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
107
FreeSql.Tests/SqlServer/Curd/SqlServerUpdateTest.cs
Normal file
107
FreeSql.Tests/SqlServer/Curd/SqlServerUpdateTest.cs
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
using FreeSql.DataAnnotations;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace FreeSql.Tests.SqlServer {
|
||||||
|
public class SqlServerUpdateTest {
|
||||||
|
IUpdate<Topic> update => g.sqlserver.Update<Topic>();
|
||||||
|
|
||||||
|
[Table(Name = "tb_topic")]
|
||||||
|
class Topic {
|
||||||
|
[Column(IsIdentity = true, IsPrimary = true)]
|
||||||
|
public int Id { get; set; }
|
||||||
|
public int? Clicks { get; set; }
|
||||||
|
public TestTypeInfo Type { get; set; }
|
||||||
|
public string Title { get; set; }
|
||||||
|
public DateTime CreateTime { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Dywhere() {
|
||||||
|
Assert.Null(g.sqlserver.Update<Topic>().ToSql());
|
||||||
|
Assert.Equal("UPDATE [tb_topic] SET title='test' \r\nWHERE ([Id] = 1 OR [Id] = 2)", g.sqlserver.Update<Topic>(new[] { 1, 2 }).SetRaw("title='test'").ToSql());
|
||||||
|
Assert.Equal("UPDATE [tb_topic] SET title='test1' \r\nWHERE ([Id] = 1)", g.sqlserver.Update<Topic>(new Topic { Id = 1, Title = "test" }).SetRaw("title='test1'").ToSql());
|
||||||
|
Assert.Equal("UPDATE [tb_topic] SET title='test1' \r\nWHERE ([Id] = 1 OR [Id] = 2)", g.sqlserver.Update<Topic>(new[] { new Topic { Id = 1, Title = "test" }, new Topic { Id = 2, Title = "test" } }).SetRaw("title='test1'").ToSql());
|
||||||
|
Assert.Equal("UPDATE [tb_topic] SET title='test1' \r\nWHERE ([Id] = 1)", g.sqlserver.Update<Topic>(new { id = 1 }).SetRaw("title='test1'").ToSql());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SetSource() {
|
||||||
|
var sql = update.SetSource(new Topic { Id = 1, Title = "newtitle" }).ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("UPDATE [tb_topic] SET [Clicks] = @p_0, [Title] = @p_1, [CreateTime] = @p_2 WHERE ([Id] = 1)", sql);
|
||||||
|
|
||||||
|
var items = new List<Topic>();
|
||||||
|
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
|
||||||
|
|
||||||
|
sql = update.SetSource(items).ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("UPDATE [tb_topic] SET [Clicks] = CASE [Id] WHEN 1 THEN @p_0 WHEN 2 THEN @p_1 WHEN 3 THEN @p_2 WHEN 4 THEN @p_3 WHEN 5 THEN @p_4 WHEN 6 THEN @p_5 WHEN 7 THEN @p_6 WHEN 8 THEN @p_7 WHEN 9 THEN @p_8 WHEN 10 THEN @p_9 END, [Title] = CASE [Id] WHEN 1 THEN @p_10 WHEN 2 THEN @p_11 WHEN 3 THEN @p_12 WHEN 4 THEN @p_13 WHEN 5 THEN @p_14 WHEN 6 THEN @p_15 WHEN 7 THEN @p_16 WHEN 8 THEN @p_17 WHEN 9 THEN @p_18 WHEN 10 THEN @p_19 END, [CreateTime] = CASE [Id] WHEN 1 THEN @p_20 WHEN 2 THEN @p_21 WHEN 3 THEN @p_22 WHEN 4 THEN @p_23 WHEN 5 THEN @p_24 WHEN 6 THEN @p_25 WHEN 7 THEN @p_26 WHEN 8 THEN @p_27 WHEN 9 THEN @p_28 WHEN 10 THEN @p_29 END WHERE ([Id] IN (1,2,3,4,5,6,7,8,9,10))", sql);
|
||||||
|
|
||||||
|
sql = update.SetSource(items).IgnoreColumns(a => new { a.Clicks, a.CreateTime }).ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("UPDATE [tb_topic] SET [Title] = CASE [Id] WHEN 1 THEN @p_0 WHEN 2 THEN @p_1 WHEN 3 THEN @p_2 WHEN 4 THEN @p_3 WHEN 5 THEN @p_4 WHEN 6 THEN @p_5 WHEN 7 THEN @p_6 WHEN 8 THEN @p_7 WHEN 9 THEN @p_8 WHEN 10 THEN @p_9 END WHERE ([Id] IN (1,2,3,4,5,6,7,8,9,10))", sql);
|
||||||
|
|
||||||
|
sql = update.SetSource(items).Set(a => a.CreateTime, new DateTime(2020,1,1)).ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("UPDATE [tb_topic] SET [CreateTime] = @p_0 WHERE ([Id] IN (1,2,3,4,5,6,7,8,9,10))", sql);
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void IgnoreColumns() {
|
||||||
|
var sql = update.SetSource(new Topic { Id = 1, Title = "newtitle" }).IgnoreColumns(a => new { a.Clicks, a.CreateTime }).ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("UPDATE [tb_topic] SET [Title] = @p_0 WHERE ([Id] = 1)", sql);
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Set() {
|
||||||
|
var sql = update.Where(a => a.Id == 1).Set(a => a.Title, "newtitle").ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("UPDATE [tb_topic] SET [Title] = @p_0 WHERE ([Id] = 1)", sql);
|
||||||
|
|
||||||
|
sql = update.Where(a => a.Id == 1).Set(a => a.Title, "newtitle").Set(a => a.CreateTime, new DateTime(2020, 1, 1)).ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("UPDATE [tb_topic] SET [Title] = @p_0, [CreateTime] = @p_1 WHERE ([Id] = 1)", sql);
|
||||||
|
|
||||||
|
sql = update.Set(a => a.Clicks * 10 / 1).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("UPDATE [tb_topic] SET [Clicks] = isnull([Clicks], 0) * 10 / 1 WHERE ([Id] = 1)", sql);
|
||||||
|
|
||||||
|
sql = update.Set(a => a.Id - 10).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("UPDATE [tb_topic] SET [Id] = [Id] - 10 WHERE ([Id] = 1)", sql);
|
||||||
|
|
||||||
|
int incrv = 10;
|
||||||
|
sql = update.Set(a => a.Clicks * incrv / 1).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("UPDATE [tb_topic] SET [Clicks] = isnull([Clicks], 0) * 10 / 1 WHERE ([Id] = 1)", sql);
|
||||||
|
|
||||||
|
sql = update.Set(a => a.Id - incrv).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("UPDATE [tb_topic] SET [Id] = [Id] - 10 WHERE ([Id] = 1)", sql);
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void SetRaw() {
|
||||||
|
var sql = update.Where(a => a.Id == 1).SetRaw("clicks = clicks + @incrClick", new { incrClick = 1 }).ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("UPDATE [tb_topic] SET clicks = clicks + @incrClick WHERE ([Id] = 1)", sql);
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Where() {
|
||||||
|
var sql = update.Where(a => a.Id == 1).SetRaw("title='newtitle'").ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("UPDATE [tb_topic] SET title='newtitle' WHERE ([Id] = 1)", sql);
|
||||||
|
|
||||||
|
sql = update.Where("id = @id", new { id = 1 }).SetRaw("title='newtitle'").ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("UPDATE [tb_topic] SET title='newtitle' WHERE (id = @id)", sql);
|
||||||
|
|
||||||
|
var item = new Topic { Id = 1, Title = "newtitle" };
|
||||||
|
sql = update.Where(item).SetRaw("title='newtitle'").ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("UPDATE [tb_topic] SET title='newtitle' WHERE ([Id] = 1)", sql);
|
||||||
|
|
||||||
|
var items = new List<Topic>();
|
||||||
|
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
|
||||||
|
sql = update.Where(items).SetRaw("title='newtitle'").ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("UPDATE [tb_topic] SET title='newtitle' WHERE ([Id] IN (1,2,3,4,5,6,7,8,9,10))", sql);
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void WhereExists() {
|
||||||
|
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void ExecuteAffrows() {
|
||||||
|
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void ExecuteUpdated() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
55
FreeSql.Tests/SqlServer/SqlServerAdo/SqlServerAdoTest.cs
Normal file
55
FreeSql.Tests/SqlServer/SqlServerAdo/SqlServerAdoTest.cs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
using FreeSql.DataAnnotations;
|
||||||
|
using System;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace FreeSql.Tests.SqlServer {
|
||||||
|
public class SqlServerAdoTest {
|
||||||
|
[Fact]
|
||||||
|
public void Pool() {
|
||||||
|
var t1 = g.sqlserver.Ado.MasterPool.StatisticsFullily;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SlavePools() {
|
||||||
|
var t2 = g.sqlserver.Ado.SlavePools.Count;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void IsTracePerformance() {
|
||||||
|
Assert.True(g.sqlserver.Ado.IsTracePerformance);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ExecuteReader() {
|
||||||
|
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void ExecuteArray() {
|
||||||
|
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void ExecuteNonQuery() {
|
||||||
|
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void ExecuteScalar() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Query() {
|
||||||
|
var t3 = g.sqlserver.Ado.Query<xxx>("select * from song");
|
||||||
|
|
||||||
|
var t4 = g.sqlserver.Ado.Query<(int, string, string, DateTime)>("select * from song");
|
||||||
|
|
||||||
|
var t5 = g.sqlserver.Ado.Query<dynamic>("select * from song");
|
||||||
|
}
|
||||||
|
|
||||||
|
class xxx {
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string Title { get; set; }
|
||||||
|
public string Url { get; set; }
|
||||||
|
public DateTime Create_time { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
228
FreeSql.Tests/SqlServer/SqlServerCodeFirstTest.cs
Normal file
228
FreeSql.Tests/SqlServer/SqlServerCodeFirstTest.cs
Normal file
@ -0,0 +1,228 @@
|
|||||||
|
using FreeSql.DataAnnotations;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace FreeSql.Tests.SqlServer {
|
||||||
|
public class SqlServerCodeFirstTest {
|
||||||
|
[Fact]
|
||||||
|
public void GetComparisonDDLStatements() {
|
||||||
|
|
||||||
|
var sql = g.sqlserver.CodeFirst.GetComparisonDDLStatements<TableAllType>();
|
||||||
|
|
||||||
|
sql = g.sqlserver.CodeFirst.GetComparisonDDLStatements<Tb_alltype>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[JsonObject(MemberSerialization.OptIn), Table(Name = "dbo.tb_alltype")]
|
||||||
|
public partial class Tb_alltype {
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "Id", DbType = "int", IsPrimary = true, IsIdentity = true)]
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldBool1111", DbType = "bit")]
|
||||||
|
public bool TestFieldBool1111 { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldBoolNullable", DbType = "bit", IsNullable = true)]
|
||||||
|
public bool? TestFieldBoolNullable { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldByte", DbType = "tinyint")]
|
||||||
|
public sbyte TestFieldByte { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldByteNullable", DbType = "tinyint", IsNullable = true)]
|
||||||
|
public sbyte? TestFieldByteNullable { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldBytes", DbType = "varbinary(255)", IsNullable = true)]
|
||||||
|
public byte[] TestFieldBytes { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldDateTime", DbType = "datetime")]
|
||||||
|
public DateTime TestFieldDateTime { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldDateTimeNullable", DbType = "datetime", IsNullable = true)]
|
||||||
|
public DateTime? TestFieldDateTimeNullable { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldDateTimeNullableOffset", DbType = "datetimeoffset", IsNullable = true)]
|
||||||
|
public DateTime? TestFieldDateTimeNullableOffset { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldDateTimeOffset", DbType = "datetimeoffset")]
|
||||||
|
public DateTime TestFieldDateTimeOffset { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldDecimal", DbType = "decimal(10,2)")]
|
||||||
|
public decimal TestFieldDecimal { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldDecimalNullable", DbType = "decimal(10,2)", IsNullable = true)]
|
||||||
|
public decimal? TestFieldDecimalNullable { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldDouble", DbType = "float")]
|
||||||
|
public double TestFieldDouble { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldDoubleNullable", DbType = "float", IsNullable = true)]
|
||||||
|
public double? TestFieldDoubleNullable { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldEnum1", DbType = "int")]
|
||||||
|
public int TestFieldEnum1 { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldEnum1Nullable", DbType = "int", IsNullable = true)]
|
||||||
|
public int? TestFieldEnum1Nullable { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldEnum2", DbType = "bigint")]
|
||||||
|
public long TestFieldEnum2 { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldEnum2Nullable", DbType = "bigint", IsNullable = true)]
|
||||||
|
public long? TestFieldEnum2Nullable { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldFloat", DbType = "real")]
|
||||||
|
public float TestFieldFloat { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldFloatNullable", DbType = "real", IsNullable = true)]
|
||||||
|
public float? TestFieldFloatNullable { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldGuid", DbType = "uniqueidentifier")]
|
||||||
|
public Guid TestFieldGuid { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldGuidNullable", DbType = "uniqueidentifier", IsNullable = true)]
|
||||||
|
public Guid? TestFieldGuidNullable { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldInt", DbType = "int")]
|
||||||
|
public int TestFieldInt { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldIntNullable", DbType = "int", IsNullable = true)]
|
||||||
|
public int? TestFieldIntNullable { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldLong", DbType = "bigint")]
|
||||||
|
public long TestFieldLong { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldSByte", DbType = "tinyint")]
|
||||||
|
public sbyte TestFieldSByte { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldSByteNullable", DbType = "tinyint", IsNullable = true)]
|
||||||
|
public sbyte? TestFieldSByteNullable { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldShort", DbType = "smallint")]
|
||||||
|
public short TestFieldShort { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldShortNullable", DbType = "smallint", IsNullable = true)]
|
||||||
|
public short? TestFieldShortNullable { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldString", DbType = "nvarchar(255)", IsNullable = true)]
|
||||||
|
public string TestFieldString { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldTimeSpan", DbType = "time")]
|
||||||
|
public TimeSpan TestFieldTimeSpan { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldTimeSpanNullable", DbType = "time", IsNullable = true)]
|
||||||
|
public TimeSpan? TestFieldTimeSpanNullable { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldUInt", DbType = "int")]
|
||||||
|
public int TestFieldUInt { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldUIntNullable", DbType = "int", IsNullable = true)]
|
||||||
|
public int? TestFieldUIntNullable { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldULong", DbType = "bigint")]
|
||||||
|
public long TestFieldULong { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldULongNullable", DbType = "bigint", IsNullable = true)]
|
||||||
|
public long? TestFieldULongNullable { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldUShort", DbType = "smallint")]
|
||||||
|
public short TestFieldUShort { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFieldUShortNullable", DbType = "smallint", IsNullable = true)]
|
||||||
|
public short? TestFieldUShortNullable { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[JsonProperty, Column(Name = "testFielLongNullable", DbType = "bigint", IsNullable = true)]
|
||||||
|
public long? TestFielLongNullable { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Table(Name = "tb_alltype")]
|
||||||
|
class TableAllType {
|
||||||
|
[Column(IsIdentity = true, IsPrimary = true)]
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
[Column(Name = "testFieldBool1111")]
|
||||||
|
public bool testFieldBool { get; set; }
|
||||||
|
public sbyte testFieldSByte { get; set; }
|
||||||
|
public short testFieldShort { get; set; }
|
||||||
|
public int testFieldInt { get; set; }
|
||||||
|
public long testFieldLong { get; set; }
|
||||||
|
public byte testFieldByte { get; set; }
|
||||||
|
public ushort testFieldUShort { get; set; }
|
||||||
|
public uint testFieldUInt { get; set; }
|
||||||
|
public ulong testFieldULong { get; set; }
|
||||||
|
public double testFieldDouble { get; set; }
|
||||||
|
public float testFieldFloat { get; set; }
|
||||||
|
public decimal testFieldDecimal { get; set; }
|
||||||
|
public TimeSpan testFieldTimeSpan { get; set; }
|
||||||
|
public DateTime testFieldDateTime { get; set; }
|
||||||
|
public DateTimeOffset testFieldDateTimeOffset { get; set; }
|
||||||
|
public byte[] testFieldBytes { get; set; }
|
||||||
|
public string testFieldString { get; set; }
|
||||||
|
public Guid testFieldGuid { get; set; }
|
||||||
|
|
||||||
|
public bool? testFieldBoolNullable { get; set; }
|
||||||
|
public sbyte? testFieldSByteNullable { get; set; }
|
||||||
|
public short? testFieldShortNullable { get; set; }
|
||||||
|
public int? testFieldIntNullable { get; set; }
|
||||||
|
public long? testFielLongNullable { get; set; }
|
||||||
|
public byte? testFieldByteNullable { get; set; }
|
||||||
|
public ushort? testFieldUShortNullable { get; set; }
|
||||||
|
public uint? testFieldUIntNullable { get; set; }
|
||||||
|
public ulong? testFieldULongNullable { get; set; }
|
||||||
|
public double? testFieldDoubleNullable { get; set; }
|
||||||
|
public float? testFieldFloatNullable { get; set; }
|
||||||
|
public decimal? testFieldDecimalNullable { get; set; }
|
||||||
|
public TimeSpan? testFieldTimeSpanNullable { get; set; }
|
||||||
|
public DateTime? testFieldDateTimeNullable { get; set; }
|
||||||
|
public DateTimeOffset? testFieldDateTimeNullableOffset { get; set; }
|
||||||
|
public Guid? testFieldGuidNullable { get; set; }
|
||||||
|
|
||||||
|
public TableAllTypeEnumType1 testFieldEnum1 { get; set; }
|
||||||
|
public TableAllTypeEnumType1? testFieldEnum1Nullable { get; set; }
|
||||||
|
public TableAllTypeEnumType2 testFieldEnum2 { get; set; }
|
||||||
|
public TableAllTypeEnumType2? testFieldEnum2Nullable { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum TableAllTypeEnumType1 { e1, e2, e3, e5 }
|
||||||
|
[Flags] public enum TableAllTypeEnumType2 { f1, f2, f3 }
|
||||||
|
}
|
||||||
|
}
|
21
FreeSql.Tests/SqlServer/SqlServerDbFirstTest.cs
Normal file
21
FreeSql.Tests/SqlServer/SqlServerDbFirstTest.cs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
using FreeSql.DataAnnotations;
|
||||||
|
using System;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace FreeSql.Tests.SqlServer {
|
||||||
|
public class SqlServerDbFirstTest {
|
||||||
|
[Fact]
|
||||||
|
public void GetDatabases() {
|
||||||
|
|
||||||
|
var t1 = g.sqlserver.DbFirst.GetDatabases();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetTablesByDatabase() {
|
||||||
|
|
||||||
|
var t2 = g.sqlserver.DbFirst.GetTablesByDatabase(g.sqlserver.DbFirst.GetDatabases()[0]);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -10,6 +10,6 @@ public class g {
|
|||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
public static IFreeSql sqlserver = new FreeSql.FreeSqlBuilder()
|
public static IFreeSql sqlserver = new FreeSql.FreeSqlBuilder()
|
||||||
.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=shop;Pooling=true;Max Pool Size=10")
|
.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=cms;Pooling=true;Max Pool Size=10")
|
||||||
.Build();
|
.Build();
|
||||||
}
|
}
|
||||||
|
18
FreeSql/DatabaseModel/DbEnumInfo.cs
Normal file
18
FreeSql/DatabaseModel/DbEnumInfo.cs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace FreeSql.DatabaseModel {
|
||||||
|
public class DbEnumInfo {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 枚举类型标识
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 枚举项
|
||||||
|
/// </summary>
|
||||||
|
public Dictionary<string, string> Labels { get; set; }
|
||||||
|
}
|
||||||
|
}
|
18
FreeSql/DatabaseModel/DbTypeInfo.cs
Normal file
18
FreeSql/DatabaseModel/DbTypeInfo.cs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace FreeSql.DatabaseModel {
|
||||||
|
public class DbTypeInfo {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 类型标识
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 枚举项
|
||||||
|
/// </summary>
|
||||||
|
public List<(string label, string value)> Labels { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -34,6 +34,7 @@ namespace FreeSql.Generator {
|
|||||||
if (fi.Name.StartsWith("for-table.")) {
|
if (fi.Name.StartsWith("for-table.")) {
|
||||||
foreach (var table in tables) {
|
foreach (var table in tables) {
|
||||||
var result = tpl.RenderFile(file, new Dictionary<string, object>() { { "table", table }, { "dbfirst", dbfirst } });
|
var result = tpl.RenderFile(file, new Dictionary<string, object>() { { "table", table }, { "dbfirst", dbfirst } });
|
||||||
|
if (result.EndsWith("return;")) continue;
|
||||||
var outputName = table.Name + outputExtension;
|
var outputName = table.Name + outputExtension;
|
||||||
var mcls = Regex.Match(result, @"\s+class\s+(\w+)");
|
var mcls = Regex.Match(result, @"\s+class\s+(\w+)");
|
||||||
if (mcls.Success) outputName = mcls.Groups[1].Value + outputExtension;
|
if (mcls.Success) outputName = mcls.Groups[1].Value + outputExtension;
|
||||||
|
@ -80,6 +80,15 @@ namespace FreeSql {
|
|||||||
/// <param name="cmdParms"></param>
|
/// <param name="cmdParms"></param>
|
||||||
object ExecuteScalar(CommandType cmdType, string cmdText, params DbParameter[] cmdParms);
|
object ExecuteScalar(CommandType cmdType, string cmdText, params DbParameter[] cmdParms);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 执行SQL返回对象集合,Query<User>("select * from user where age > @age", new SqlParameter { ParameterName = "age", Value = 25 })
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="cmdType"></param>
|
||||||
|
/// <param name="cmdText"></param>
|
||||||
|
/// <param name="cmdParms"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
List<T> Query<T>(CommandType cmdType, string cmdText, params DbParameter[] cmdParms);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 执行SQL返回对象集合,Query<User>("select * from user where age > @age", new { age = 25 })
|
/// 执行SQL返回对象集合,Query<User>("select * from user where age > @age", new { age = 25 })
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -126,7 +135,16 @@ namespace FreeSql {
|
|||||||
Task<object> ExecuteScalarAsync(CommandType cmdType, string cmdText, params DbParameter[] cmdParms);
|
Task<object> ExecuteScalarAsync(CommandType cmdType, string cmdText, params DbParameter[] cmdParms);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 执行SQL返回对象集合,Query<User>("select * from user where age > @age", new { age = 25 })
|
/// 执行SQL返回对象集合,QueryAsync<User>("select * from user where age > @age", new SqlParameter { ParameterName = "age", Value = 25 })
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="cmdType"></param>
|
||||||
|
/// <param name="cmdText"></param>
|
||||||
|
/// <param name="cmdParms"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<List<T>> QueryAsync<T>(CommandType cmdType, string cmdText, params DbParameter[] cmdParms);
|
||||||
|
/// <summary>
|
||||||
|
/// 执行SQL返回对象集合,QueryAsync<User>("select * from user where age > @age", new { age = 25 })
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
/// <param name="cmdText"></param>
|
/// <param name="cmdText"></param>
|
||||||
|
@ -66,5 +66,12 @@ namespace FreeSql {
|
|||||||
/// <param name="column"></param>
|
/// <param name="column"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
string GetCsParse(DbColumnInfo column);
|
string GetCsParse(DbColumnInfo column);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取数据库枚举类型,适用 PostgreSQL
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="database"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
List<DbEnumInfo> GetEnumsByDatabase(params string[] database);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -113,15 +113,36 @@ namespace FreeSql.Internal {
|
|||||||
{ ExpressionType.Equal, "=" },
|
{ ExpressionType.Equal, "=" },
|
||||||
};
|
};
|
||||||
internal string ExpressionWhereLambdaNoneForeignObject(List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, Expression exp) {
|
internal string ExpressionWhereLambdaNoneForeignObject(List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, Expression exp) {
|
||||||
return ExpressionLambdaToSql(exp, _tables, _selectColumnMap, SelectTableInfoType.From, true);
|
var sql = ExpressionLambdaToSql(exp, _tables, _selectColumnMap, SelectTableInfoType.From, true);
|
||||||
|
switch(sql) {
|
||||||
|
case "1":
|
||||||
|
case "'t'": return "1=1";
|
||||||
|
case "0":
|
||||||
|
case "'f'": return "1=2";
|
||||||
|
default:return sql;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal string ExpressionWhereLambda(List<SelectTableInfo> _tables, Expression exp) {
|
internal string ExpressionWhereLambda(List<SelectTableInfo> _tables, Expression exp) {
|
||||||
return ExpressionLambdaToSql(exp, _tables, null, SelectTableInfoType.From, true);
|
var sql = ExpressionLambdaToSql(exp, _tables, null, SelectTableInfoType.From, true);
|
||||||
|
switch (sql) {
|
||||||
|
case "1":
|
||||||
|
case "'t'": return "1=1";
|
||||||
|
case "0":
|
||||||
|
case "'f'": return "1=2";
|
||||||
|
default: return sql;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
internal void ExpressionJoinLambda(List<SelectTableInfo> _tables, SelectTableInfoType tbtype, Expression exp) {
|
internal void ExpressionJoinLambda(List<SelectTableInfo> _tables, SelectTableInfoType tbtype, Expression exp) {
|
||||||
var tbidx = _tables.Count;
|
var tbidx = _tables.Count;
|
||||||
var filter = ExpressionLambdaToSql(exp, _tables, null, tbtype, true);
|
var filter = ExpressionLambdaToSql(exp, _tables, null, tbtype, true);
|
||||||
|
switch (filter) {
|
||||||
|
case "1":
|
||||||
|
case "'t'": filter = "1=1"; break;
|
||||||
|
case "0":
|
||||||
|
case "'f'": filter = "1=2"; break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
if (_tables.Count > tbidx) {
|
if (_tables.Count > tbidx) {
|
||||||
_tables[tbidx].Type = tbtype;
|
_tables[tbidx].Type = tbtype;
|
||||||
_tables[tbidx].On = filter;
|
_tables[tbidx].On = filter;
|
||||||
@ -139,11 +160,27 @@ namespace FreeSql.Internal {
|
|||||||
case ExpressionType.Lambda: return ExpressionLambdaToSql((exp as LambdaExpression)?.Body, _tables, _selectColumnMap, tbtype, isQuoteName);
|
case ExpressionType.Lambda: return ExpressionLambdaToSql((exp as LambdaExpression)?.Body, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
case ExpressionType.Convert: return ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, _tables, _selectColumnMap, tbtype, isQuoteName);
|
case ExpressionType.Convert: return ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
case ExpressionType.Constant: return _common.FormatSql("{0}", (exp as ConstantExpression)?.Value);
|
case ExpressionType.Constant: return _common.FormatSql("{0}", (exp as ConstantExpression)?.Value);
|
||||||
|
case ExpressionType.Call:
|
||||||
|
var exp3 = exp as MethodCallExpression;
|
||||||
|
if (exp3.Object.Type.FullName == "System.String") return ExpressionLambdaToSqlCallString(exp3, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
if (exp3.Object.Type.FullName == "System.Math") return ExpressionLambdaToSqlCallMath(exp3, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
if (exp3.Object.Type.FullName == "System.DateTime" || exp3.Object.Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.DateTime") return ExpressionLambdaToSqlCallDateTime(exp3, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
if (exp3.Object.Type.FullName == "System.TimeSpan" || exp3.Object.Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.TimeSpan") return ExpressionLambdaToSqlCallTimeSpan(exp3, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
throw new Exception($"MySqlExpression 未现实函数表达式 {exp3} 解析");
|
||||||
case ExpressionType.MemberAccess:
|
case ExpressionType.MemberAccess:
|
||||||
|
var exp4 = exp as MemberExpression;
|
||||||
|
var extRet = "";
|
||||||
|
if (exp4.Expression == null && exp4.Type.FullName == "System.DateTime" && exp4.Member.Name == "Now") return ExpressionLambdaToSqlMemberAccessDateTime(exp4, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
if (exp4.Expression.Type.FullName == "System.String") extRet = ExpressionLambdaToSqlMemberAccessString(exp4, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
else if (exp4.Expression.Type.FullName == "System.Math") extRet = ExpressionLambdaToSqlMemberAccessMath(exp4, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
else if (exp4.Expression.Type.FullName == "System.DateTime" || exp4.Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.DateTime") extRet = ExpressionLambdaToSqlMemberAccessDateTime(exp4, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
else if (exp4.Expression.Type.FullName == "System.TimeSpan" || exp4.Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.TimeSpan") extRet = ExpressionLambdaToSqlMemberAccessTimeSpan(exp4, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
if (string.IsNullOrEmpty(extRet) == false) return extRet;
|
||||||
|
|
||||||
var expStack = new Stack<Expression>();
|
var expStack = new Stack<Expression>();
|
||||||
expStack.Push(exp);
|
expStack.Push(exp);
|
||||||
MethodCallExpression callExp = null;
|
MethodCallExpression callExp = null;
|
||||||
var exp2 = (exp as MemberExpression).Expression;
|
var exp2 = exp4.Expression;
|
||||||
while (true) {
|
while (true) {
|
||||||
switch(exp2.NodeType) {
|
switch(exp2.NodeType) {
|
||||||
case ExpressionType.Constant:
|
case ExpressionType.Constant:
|
||||||
@ -167,7 +204,7 @@ namespace FreeSql.Internal {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (expStack.First().NodeType != ExpressionType.Parameter) return _common.FormatSql("{0}", Expression.Lambda(exp).Compile().DynamicInvoke());
|
if (expStack.First().NodeType != ExpressionType.Parameter) return _common.FormatSql("{0}", Expression.Lambda(exp).Compile().DynamicInvoke());
|
||||||
if (callExp != null) return ExpressionLambdaToSqlCall(callExp, _tables, _selectColumnMap, tbtype, isQuoteName);
|
if (callExp != null) return ExpressionLambdaToSql(callExp, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
if (_tables == null) {
|
if (_tables == null) {
|
||||||
var pp = expStack.Pop() as ParameterExpression;
|
var pp = expStack.Pop() as ParameterExpression;
|
||||||
var memberExp = expStack.Pop() as MemberExpression;
|
var memberExp = expStack.Pop() as MemberExpression;
|
||||||
@ -234,7 +271,6 @@ namespace FreeSql.Internal {
|
|||||||
}
|
}
|
||||||
if (isQuoteName) name2 = _common.QuoteSqlName(name2);
|
if (isQuoteName) name2 = _common.QuoteSqlName(name2);
|
||||||
return $"{alias2}.{name2}";
|
return $"{alias2}.{name2}";
|
||||||
case ExpressionType.Call: return ExpressionLambdaToSqlCall(exp as MethodCallExpression, _tables, _selectColumnMap, tbtype, isQuoteName);
|
|
||||||
}
|
}
|
||||||
if (dicExpressionOperator.TryGetValue(exp.NodeType, out var tryoper) == false) return "";
|
if (dicExpressionOperator.TryGetValue(exp.NodeType, out var tryoper) == false) return "";
|
||||||
var expBinary = exp as BinaryExpression;
|
var expBinary = exp as BinaryExpression;
|
||||||
@ -247,10 +283,17 @@ namespace FreeSql.Internal {
|
|||||||
left = tmp;
|
left = tmp;
|
||||||
}
|
}
|
||||||
if (right == "NULL") tryoper = tryoper == "=" ? " IS " : " IS NOT ";
|
if (right == "NULL") tryoper = tryoper == "=" ? " IS " : " IS NOT ";
|
||||||
|
if (tryoper == "+" && (expBinary.Left.Type.FullName == "System.String" || expBinary.Right.Type.FullName == "System.String")) return _common.StringConcat(left, right, expBinary.Left.Type, expBinary.Right.Type);
|
||||||
return $"{left} {tryoper} {right}";
|
return $"{left} {tryoper} {right}";
|
||||||
}
|
}
|
||||||
|
|
||||||
internal abstract string ExpressionLambdaToSqlCall(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName);
|
internal abstract string ExpressionLambdaToSqlMemberAccessString(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName);
|
||||||
|
internal abstract string ExpressionLambdaToSqlMemberAccessMath(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName);
|
||||||
|
internal abstract string ExpressionLambdaToSqlMemberAccessDateTime(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName);
|
||||||
|
internal abstract string ExpressionLambdaToSqlMemberAccessTimeSpan(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName);
|
||||||
|
internal abstract string ExpressionLambdaToSqlCallString(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName);
|
||||||
|
internal abstract string ExpressionLambdaToSqlCallMath(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName);
|
||||||
|
internal abstract string ExpressionLambdaToSqlCallDateTime(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName);
|
||||||
|
internal abstract string ExpressionLambdaToSqlCallTimeSpan(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@ namespace FreeSql.Internal.CommonProvider {
|
|||||||
foreach (var col in _table.Columns.Values)
|
foreach (var col in _table.Columns.Values)
|
||||||
if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.Attribute.Name) == false) {
|
if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.Attribute.Name) == false) {
|
||||||
if (colidx2 > 0) sb.Append(", ");
|
if (colidx2 > 0) sb.Append(", ");
|
||||||
sb.Append("?").Append(col.CsName).Append(didx);
|
sb.Append(_commonUtils.QuoteParamterName(col.CsName)).Append(didx);
|
||||||
_params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}{didx}", _table.Properties.TryGetValue(col.CsName, out var tryp) ? tryp.GetValue(d) : DBNull.Value);
|
_params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}{didx}", _table.Properties.TryGetValue(col.CsName, out var tryp) ? tryp.GetValue(d) : DBNull.Value);
|
||||||
++colidx2;
|
++colidx2;
|
||||||
}
|
}
|
||||||
|
@ -54,7 +54,7 @@ namespace FreeSql.Internal.CommonProvider {
|
|||||||
public IUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> column, TMember value) {
|
public IUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> column, TMember value) {
|
||||||
var col = _commonExpression.ExpressionSelectColumn_MemberAccess(null, null, SelectTableInfoType.From, column?.Body, true);
|
var col = _commonExpression.ExpressionSelectColumn_MemberAccess(null, null, SelectTableInfoType.From, column?.Body, true);
|
||||||
if (string.IsNullOrEmpty(col)) return this;
|
if (string.IsNullOrEmpty(col)) return this;
|
||||||
_set.Append(", ").Append(col).Append(" = ?p_").Append(_params.Count);
|
_set.Append(", ").Append(col).Append(" = ").Append(_commonUtils.QuoteParamterName("p_")).Append(_params.Count);
|
||||||
_commonUtils.AppendParamter(_params, null, value);
|
_commonUtils.AppendParamter(_params, null, value);
|
||||||
//foreach (var t in _source) Utils.FillPropertyValue(t, tryf.CsName, value);
|
//foreach (var t in _source) Utils.FillPropertyValue(t, tryf.CsName, value);
|
||||||
return this;
|
return this;
|
||||||
@ -135,7 +135,7 @@ namespace FreeSql.Internal.CommonProvider {
|
|||||||
// ++pkidx;
|
// ++pkidx;
|
||||||
//}
|
//}
|
||||||
//if (_table.Primarys.Length > 1) caseWhen.Append(")");
|
//if (_table.Primarys.Length > 1) caseWhen.Append(")");
|
||||||
var cw = caseWhen.Append(" ").ToString();
|
var cw = caseWhen.ToString();
|
||||||
|
|
||||||
_paramsSource.Clear();
|
_paramsSource.Clear();
|
||||||
var colidx = 0;
|
var colidx = 0;
|
||||||
|
@ -16,6 +16,7 @@ namespace FreeSql.Internal {
|
|||||||
internal abstract string QuoteSqlName(string name);
|
internal abstract string QuoteSqlName(string name);
|
||||||
internal abstract string QuoteParamterName(string name);
|
internal abstract string QuoteParamterName(string name);
|
||||||
internal abstract string IsNull(string sql, object value);
|
internal abstract string IsNull(string sql, object value);
|
||||||
|
internal abstract string StringConcat(string left, string right, Type leftType, Type rightType);
|
||||||
|
|
||||||
internal ICodeFirst CodeFirst { get; set; }
|
internal ICodeFirst CodeFirst { get; set; }
|
||||||
internal TableInfo GetTableByEntity(Type entity) => Utils.GetTableByEntity(entity, this);
|
internal TableInfo GetTableByEntity(Type entity) => Utils.GetTableByEntity(entity, this);
|
||||||
|
@ -27,17 +27,20 @@ namespace FreeSql.Internal {
|
|||||||
trytb.SelectFilter = tbattr?.SelectFilter;
|
trytb.SelectFilter = tbattr?.SelectFilter;
|
||||||
foreach (var p in trytb.Properties.Values) {
|
foreach (var p in trytb.Properties.Values) {
|
||||||
var tp = common.CodeFirst.GetDbInfo(p.PropertyType);
|
var tp = common.CodeFirst.GetDbInfo(p.PropertyType);
|
||||||
if (tp == null) continue;
|
//if (tp == null) continue;
|
||||||
var colattr = p.GetCustomAttributes(typeof(ColumnAttribute), false).LastOrDefault() as ColumnAttribute ?? new ColumnAttribute {
|
var colattr = p.GetCustomAttributes(typeof(ColumnAttribute), false).LastOrDefault() as ColumnAttribute;
|
||||||
Name = p.Name,
|
if (tp == null && colattr == null) continue;
|
||||||
DbType = tp.Value.dbtypeFull,
|
if (colattr == null)
|
||||||
IsIdentity = false,
|
colattr = new ColumnAttribute {
|
||||||
IsNullable = tp.Value.isnullable ?? false,
|
Name = p.Name,
|
||||||
IsPrimary = false,
|
DbType = tp.Value.dbtypeFull,
|
||||||
};
|
IsIdentity = false,
|
||||||
|
IsNullable = tp.Value.isnullable ?? false,
|
||||||
|
IsPrimary = false,
|
||||||
|
};
|
||||||
if (string.IsNullOrEmpty(colattr.Name)) colattr.Name = p.Name;
|
if (string.IsNullOrEmpty(colattr.Name)) colattr.Name = p.Name;
|
||||||
if (string.IsNullOrEmpty(colattr.DbType)) colattr.DbType = tp.Value.dbtypeFull;
|
if (string.IsNullOrEmpty(colattr.DbType)) colattr.DbType = tp?.dbtypeFull ?? "varchar(255)";
|
||||||
if (colattr.DbType.IndexOf("NOT NULL") == -1 && tp.Value.isnullable == false) colattr.DbType += " NOT NULL";
|
if (colattr.DbType.IndexOf("NOT NULL") == -1 && tp?.isnullable == false) colattr.DbType += " NOT NULL";
|
||||||
|
|
||||||
var col = new ColumnInfo {
|
var col = new ColumnInfo {
|
||||||
Table = trytb,
|
Table = trytb,
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using FreeSql.Internal;
|
using FreeSql.Internal;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace FreeSql.MySql.Curd {
|
namespace FreeSql.MySql.Curd {
|
||||||
@ -19,7 +20,7 @@ namespace FreeSql.MySql.Curd {
|
|||||||
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
||||||
++colidx;
|
++colidx;
|
||||||
}
|
}
|
||||||
return _orm.Ado.Query<T1>(sb.ToString());
|
return _orm.Ado.Query<T1>(CommandType.Text, sb.ToString(), _params.ToArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ namespace FreeSql.MySql.Curd {
|
|||||||
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
||||||
++colidx;
|
++colidx;
|
||||||
}
|
}
|
||||||
return _orm.Ado.Query<T1>(sb.ToString());
|
return _orm.Ado.Query<T1>(CommandType.Text, sb.ToString(), _params);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,10 @@ namespace FreeSql.MySql.Curd {
|
|||||||
|
|
||||||
class MySqlSelect<T1> : FreeSql.Internal.CommonProvider.Select1Provider<T1> where T1 : class {
|
class MySqlSelect<T1> : FreeSql.Internal.CommonProvider.Select1Provider<T1> where T1 : class {
|
||||||
|
|
||||||
internal static string ToSqlStatic(CommonUtils _commonUtils, string _select, string field, StringBuilder _join, StringBuilder _where, string _groupby, string _having, string _orderby, int _skip, int _limit, List<SelectTableInfo> _tables) {
|
internal static string ToSqlStatic(CommonUtils _commonUtils, string _select, string field, StringBuilder _join, StringBuilder _where, string _groupby, string _having, string _orderby, int _skip, int _limit, List<SelectTableInfo> _tables, IFreeSql _orm) {
|
||||||
|
if (_orm.CodeFirst.IsAutoSyncStructure)
|
||||||
|
_orm.CodeFirst.SyncStructure(_tables.Select(a => a.Table.Type).ToArray());
|
||||||
|
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
sb.Append(_select).Append(field).Append(" \r\nFROM ");
|
sb.Append(_select).Append(field).Append(" \r\nFROM ");
|
||||||
var tbsjoin = _tables.Where(a => a.Type != SelectTableInfoType.From).ToArray();
|
var tbsjoin = _tables.Where(a => a.Type != SelectTableInfoType.From).ToArray();
|
||||||
@ -73,42 +76,42 @@ namespace FreeSql.MySql.Curd {
|
|||||||
public override ISelect<T1, T2, T3, T4, T5, T6, T7, T8> From<T2, T3, T4, T5, T6, T7, T8>(Expression<Func<ISelectFromExpression<T1>, T2, T3, T4, T5, T6, T7, T8, ISelectFromExpression<T1>>> exp) { this.InternalFrom(exp?.Body); var ret = new MySqlSelect<T1, T2, T3, T4, T5, T6, T7, T8>(_orm, _commonUtils, _commonExpression, null); MySqlSelect<T1>.CopyData(this, ret); return ret; }
|
public override ISelect<T1, T2, T3, T4, T5, T6, T7, T8> From<T2, T3, T4, T5, T6, T7, T8>(Expression<Func<ISelectFromExpression<T1>, T2, T3, T4, T5, T6, T7, T8, ISelectFromExpression<T1>>> exp) { this.InternalFrom(exp?.Body); var ret = new MySqlSelect<T1, T2, T3, T4, T5, T6, T7, T8>(_orm, _commonUtils, _commonExpression, null); MySqlSelect<T1>.CopyData(this, ret); return ret; }
|
||||||
public override ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9> From<T2, T3, T4, T5, T6, T7, T8, T9>(Expression<Func<ISelectFromExpression<T1>, T2, T3, T4, T5, T6, T7, T8, T9, ISelectFromExpression<T1>>> exp) { this.InternalFrom(exp?.Body); var ret = new MySqlSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9>(_orm, _commonUtils, _commonExpression, null); MySqlSelect<T1>.CopyData(this, ret); return ret; }
|
public override ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9> From<T2, T3, T4, T5, T6, T7, T8, T9>(Expression<Func<ISelectFromExpression<T1>, T2, T3, T4, T5, T6, T7, T8, T9, ISelectFromExpression<T1>>> exp) { this.InternalFrom(exp?.Body); var ret = new MySqlSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9>(_orm, _commonUtils, _commonExpression, null); MySqlSelect<T1>.CopyData(this, ret); return ret; }
|
||||||
public override ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> From<T2, T3, T4, T5, T6, T7, T8, T9, T10>(Expression<Func<ISelectFromExpression<T1>, T2, T3, T4, T5, T6, T7, T8, T9, T10, ISelectFromExpression<T1>>> exp) { this.InternalFrom(exp?.Body); var ret = new MySqlSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(_orm, _commonUtils, _commonExpression, null); MySqlSelect<T1>.CopyData(this, ret); return ret; }
|
public override ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> From<T2, T3, T4, T5, T6, T7, T8, T9, T10>(Expression<Func<ISelectFromExpression<T1>, T2, T3, T4, T5, T6, T7, T8, T9, T10, ISelectFromExpression<T1>>> exp) { this.InternalFrom(exp?.Body); var ret = new MySqlSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(_orm, _commonUtils, _commonExpression, null); MySqlSelect<T1>.CopyData(this, ret); return ret; }
|
||||||
public override string ToSql(string field = null) => ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class MySqlSelect<T1, T2> : FreeSql.Internal.CommonProvider.Select2Provider<T1, T2> where T1 : class where T2 : class {
|
class MySqlSelect<T1, T2> : FreeSql.Internal.CommonProvider.Select2Provider<T1, T2> where T1 : class where T2 : class {
|
||||||
public MySqlSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public MySqlSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => MySqlSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => MySqlSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class MySqlSelect<T1, T2, T3> : FreeSql.Internal.CommonProvider.Select3Provider<T1, T2, T3> where T1 : class where T2 : class where T3 : class {
|
class MySqlSelect<T1, T2, T3> : FreeSql.Internal.CommonProvider.Select3Provider<T1, T2, T3> where T1 : class where T2 : class where T3 : class {
|
||||||
public MySqlSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public MySqlSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => MySqlSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => MySqlSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class MySqlSelect<T1, T2, T3, T4> : FreeSql.Internal.CommonProvider.Select4Provider<T1, T2, T3, T4> where T1 : class where T2 : class where T3 : class where T4 : class {
|
class MySqlSelect<T1, T2, T3, T4> : FreeSql.Internal.CommonProvider.Select4Provider<T1, T2, T3, T4> where T1 : class where T2 : class where T3 : class where T4 : class {
|
||||||
public MySqlSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public MySqlSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => MySqlSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => MySqlSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class MySqlSelect<T1, T2, T3, T4, T5> : FreeSql.Internal.CommonProvider.Select5Provider<T1, T2, T3, T4, T5> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class {
|
class MySqlSelect<T1, T2, T3, T4, T5> : FreeSql.Internal.CommonProvider.Select5Provider<T1, T2, T3, T4, T5> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class {
|
||||||
public MySqlSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public MySqlSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => MySqlSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => MySqlSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class MySqlSelect<T1, T2, T3, T4, T5, T6> : FreeSql.Internal.CommonProvider.Select6Provider<T1, T2, T3, T4, T5, T6> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class {
|
class MySqlSelect<T1, T2, T3, T4, T5, T6> : FreeSql.Internal.CommonProvider.Select6Provider<T1, T2, T3, T4, T5, T6> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class {
|
||||||
public MySqlSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public MySqlSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => MySqlSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => MySqlSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class MySqlSelect<T1, T2, T3, T4, T5, T6, T7> : FreeSql.Internal.CommonProvider.Select7Provider<T1, T2, T3, T4, T5, T6, T7> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class {
|
class MySqlSelect<T1, T2, T3, T4, T5, T6, T7> : FreeSql.Internal.CommonProvider.Select7Provider<T1, T2, T3, T4, T5, T6, T7> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class {
|
||||||
public MySqlSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public MySqlSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => MySqlSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => MySqlSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class MySqlSelect<T1, T2, T3, T4, T5, T6, T7, T8> : FreeSql.Internal.CommonProvider.Select8Provider<T1, T2, T3, T4, T5, T6, T7, T8> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class {
|
class MySqlSelect<T1, T2, T3, T4, T5, T6, T7, T8> : FreeSql.Internal.CommonProvider.Select8Provider<T1, T2, T3, T4, T5, T6, T7, T8> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class {
|
||||||
public MySqlSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public MySqlSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => MySqlSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => MySqlSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class MySqlSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9> : FreeSql.Internal.CommonProvider.Select9Provider<T1, T2, T3, T4, T5, T6, T7, T8, T9> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class {
|
class MySqlSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9> : FreeSql.Internal.CommonProvider.Select9Provider<T1, T2, T3, T4, T5, T6, T7, T8, T9> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class {
|
||||||
public MySqlSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public MySqlSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => MySqlSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => MySqlSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class MySqlSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> : FreeSql.Internal.CommonProvider.Select10Provider<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class where T10 : class {
|
class MySqlSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> : FreeSql.Internal.CommonProvider.Select10Provider<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class where T10 : class {
|
||||||
public MySqlSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public MySqlSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => MySqlSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => MySqlSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
using FreeSql.Internal;
|
using FreeSql.Internal;
|
||||||
using FreeSql.Internal.Model;
|
using FreeSql.Internal.Model;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace FreeSql.MySql.Curd {
|
namespace FreeSql.MySql.Curd {
|
||||||
@ -21,29 +23,37 @@ namespace FreeSql.MySql.Curd {
|
|||||||
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
||||||
++colidx;
|
++colidx;
|
||||||
}
|
}
|
||||||
return _orm.Ado.Query<T1>(sb.ToString());
|
return _orm.Ado.Query<T1>(CommandType.Text, sb.ToString(), _params.Concat(_paramsSource).ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void ToSqlCase(StringBuilder caseWhen, ColumnInfo[] primarys) {
|
protected override void ToSqlCase(StringBuilder caseWhen, ColumnInfo[] primarys) {
|
||||||
if (_table.Primarys.Length > 1) caseWhen.Append("CONCAT(");
|
if (_table.Primarys.Length == 1) {
|
||||||
|
caseWhen.Append(_commonUtils.QuoteSqlName(_table.Primarys.First().Attribute.Name));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
caseWhen.Append("CONCAT(");
|
||||||
var pkidx = 0;
|
var pkidx = 0;
|
||||||
foreach (var pk in _table.Primarys) {
|
foreach (var pk in _table.Primarys) {
|
||||||
if (pkidx > 0) caseWhen.Append(", ");
|
if (pkidx > 0) caseWhen.Append(", ");
|
||||||
caseWhen.Append(_commonUtils.QuoteSqlName(pk.Attribute.Name));
|
caseWhen.Append(_commonUtils.QuoteSqlName(pk.Attribute.Name));
|
||||||
++pkidx;
|
++pkidx;
|
||||||
}
|
}
|
||||||
if (_table.Primarys.Length > 1) caseWhen.Append(")");
|
caseWhen.Append(")");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void ToSqlWhen(StringBuilder sb, ColumnInfo[] primarys, object d) {
|
protected override void ToSqlWhen(StringBuilder sb, ColumnInfo[] primarys, object d) {
|
||||||
if (_table.Primarys.Length > 1) sb.Append("CONCAT(");
|
if (_table.Primarys.Length == 1) {
|
||||||
|
sb.Append(_commonUtils.FormatSql("{0}", _table.Properties.TryGetValue(_table.Primarys.First().CsName, out var tryp2) ? tryp2.GetValue(d) : null));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sb.Append("CONCAT(");
|
||||||
var pkidx = 0;
|
var pkidx = 0;
|
||||||
foreach (var pk in _table.Primarys) {
|
foreach (var pk in _table.Primarys) {
|
||||||
if (pkidx > 0) sb.Append(", ");
|
if (pkidx > 0) sb.Append(", ");
|
||||||
sb.Append(_commonUtils.FormatSql("{0}", _table.Properties.TryGetValue(pk.CsName, out var tryp2) ? tryp2.GetValue(d) : null));
|
sb.Append(_commonUtils.FormatSql("{0}", _table.Properties.TryGetValue(pk.CsName, out var tryp2) ? tryp2.GetValue(d) : null));
|
||||||
++pkidx;
|
++pkidx;
|
||||||
}
|
}
|
||||||
if (_table.Primarys.Length > 1) sb.Append(")");
|
sb.Append(")");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ namespace FreeSql.MySql {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
static DateTime dt1970 = new DateTime(1970, 1, 1);
|
||||||
public override object AddslashesProcessParam(object param) {
|
public override object AddslashesProcessParam(object param) {
|
||||||
if (param == null) return "NULL";
|
if (param == null) return "NULL";
|
||||||
if (param is bool || param is bool?)
|
if (param is bool || param is bool?)
|
||||||
@ -33,21 +34,24 @@ namespace FreeSql.MySql {
|
|||||||
return ((Enum)param).ToInt64();
|
return ((Enum)param).ToInt64();
|
||||||
else if (decimal.TryParse(string.Concat(param), out var trydec))
|
else if (decimal.TryParse(string.Concat(param), out var trydec))
|
||||||
return param;
|
return param;
|
||||||
else if (param is DateTime) {
|
else if (param is DateTime)
|
||||||
DateTime dt = (DateTime)param;
|
return string.Concat("'", ((DateTime)param).ToString("yyyy-MM-dd HH:mm:ss"), "'");
|
||||||
return string.Concat("'", dt.ToString("yyyy-MM-dd HH:mm:ss"), "'");
|
else if (param is DateTime?)
|
||||||
} else if (param is DateTime?) {
|
return string.Concat("'", (param as DateTime?).Value.ToString("yyyy-MM-dd HH:mm:ss"), "'");
|
||||||
DateTime? dt = param as DateTime?;
|
else if (param is TimeSpan) {
|
||||||
return string.Concat("'", dt.Value.ToString("yyyy-MM-dd HH:mm:ss"), "'");
|
var ts = (TimeSpan)param;
|
||||||
|
return string.Concat("'", ts.Ticks > 0 ? "" : "-", ts.TotalHours, dt1970.AddTicks(Math.Abs(ts.Ticks)).ToString(":mm:ss.fff"), "'");
|
||||||
|
} else if (param is TimeSpan) {
|
||||||
|
var ts = (param as TimeSpan?).Value;
|
||||||
|
return string.Concat("'", ts.Ticks > 0 ? "" : "-", ts.TotalHours, dt1970.AddTicks(Math.Abs(ts.Ticks)).ToString(":mm:ss.fff"), "'");
|
||||||
} else if (param is IEnumerable) {
|
} else if (param is IEnumerable) {
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
var ie = param as IEnumerable;
|
var ie = param as IEnumerable;
|
||||||
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z));
|
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z));
|
||||||
return sb.Length == 0 ? "(NULL)" : sb.Remove(0, 1).Insert(0, "(").Append(")").ToString();
|
return sb.Length == 0 ? "(NULL)" : sb.Remove(0, 1).Insert(0, "(").Append(")").ToString();
|
||||||
} else {
|
|
||||||
return string.Concat("'", param.ToString().Replace("'", "''"), "'");
|
|
||||||
//if (param is string) return string.Concat('N', nparms[a]);
|
|
||||||
}
|
}
|
||||||
|
return string.Concat("'", param.ToString().Replace("'", "''"), "'");
|
||||||
|
//if (param is string) return string.Concat('N', nparms[a]);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override DbCommand CreateCommand() {
|
protected override DbCommand CreateCommand() {
|
||||||
|
@ -169,12 +169,21 @@ where a.table_schema in ({0}) and a.table_name in ({1})".FormatMySql(isRenameTab
|
|||||||
return sb.Length == 0 ? null : sb.ToString();
|
return sb.Length == 0 ? null : sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Dictionary<string, bool> dicSyced = new Dictionary<string, bool>();
|
||||||
public bool SyncStructure<TEntity>() => this.SyncStructure(typeof(TEntity));
|
public bool SyncStructure<TEntity>() => this.SyncStructure(typeof(TEntity));
|
||||||
public bool SyncStructure(params Type[] entityTypes) {
|
public bool SyncStructure(params Type[] entityTypes) {
|
||||||
var ddl = this.GetComparisonDDLStatements(entityTypes);
|
if (entityTypes == null) return true;
|
||||||
if (string.IsNullOrEmpty(ddl)) return true;
|
var syncTypes = entityTypes.Where(a => dicSyced.ContainsKey(a.FullName) == false).ToArray();
|
||||||
|
if (syncTypes.Any() == false) return true;
|
||||||
|
var ddl = this.GetComparisonDDLStatements(syncTypes);
|
||||||
|
if (string.IsNullOrEmpty(ddl)) {
|
||||||
|
foreach (var syncType in syncTypes) dicSyced.Add(syncType.FullName, true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
return _orm.Ado.ExecuteNonQuery(CommandType.Text, ddl) > 0;
|
var affrows = _orm.Ado.ExecuteNonQuery(CommandType.Text, ddl);
|
||||||
|
foreach (var syncType in syncTypes) dicSyced.Add(syncType.FullName, true);
|
||||||
|
return affrows > 0;
|
||||||
} catch {
|
} catch {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -358,10 +358,24 @@ where a.constraint_schema in ({1}) and a.table_name in ({0}) and not isnull(posi
|
|||||||
if (ret == 0) ret = t1.Name.CompareTo(t2.Name);
|
if (ret == 0) ret = t1.Name.CompareTo(t2.Name);
|
||||||
return ret;
|
return ret;
|
||||||
});
|
});
|
||||||
|
foreach(var loc4 in loc1) {
|
||||||
|
var dicUniques = new Dictionary<string, List<DbColumnInfo>>();
|
||||||
|
if (loc4.Primarys.Count > 0) dicUniques.Add(string.Join(",", loc4.Primarys.Select(a => a.Name)), loc4.Primarys);
|
||||||
|
foreach(var loc5 in loc4.Uniques) {
|
||||||
|
var dickey = string.Join(",", loc5.Select(a => a.Name));
|
||||||
|
if (dicUniques.ContainsKey(dickey)) continue;
|
||||||
|
dicUniques.Add(dickey, loc5);
|
||||||
|
}
|
||||||
|
loc4.Uniques = dicUniques.Values.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
loc2.Clear();
|
loc2.Clear();
|
||||||
loc3.Clear();
|
loc3.Clear();
|
||||||
return loc1;
|
return loc1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<DbEnumInfo> GetEnumsByDatabase(params string[] database) {
|
||||||
|
return new List<DbEnumInfo>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,6 +2,7 @@
|
|||||||
using FreeSql.Internal.Model;
|
using FreeSql.Internal.Model;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
namespace FreeSql.MySql {
|
namespace FreeSql.MySql {
|
||||||
@ -9,150 +10,174 @@ namespace FreeSql.MySql {
|
|||||||
|
|
||||||
public MySqlExpression(CommonUtils common) : base(common) { }
|
public MySqlExpression(CommonUtils common) : base(common) { }
|
||||||
|
|
||||||
internal override string ExpressionLambdaToSqlCall(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
internal override string ExpressionLambdaToSqlMemberAccessString(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
if (exp.Object.Type.FullName == "System.String") {
|
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName);
|
switch (exp.Member.Name) {
|
||||||
switch (exp.Method.Name) {
|
case "Length": return $"char_length({left})";
|
||||||
case "StartsWith":
|
}
|
||||||
case "EndsWith":
|
return null;
|
||||||
case "Contains":
|
}
|
||||||
var args0Value = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
|
|
||||||
if (args0Value == "NULL") return $"({left}) IS NULL";
|
internal override string ExpressionLambdaToSqlMemberAccessMath(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
if (exp.Method.Name == "StartsWith") return $"({left}) LIKE {(args0Value.StartsWith("'") ? args0Value.Insert(1, "%") : $"concat('%', {args0Value})")}";
|
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
if (exp.Method.Name == "EndsWith") return $"({left}) LIKE {(args0Value.EndsWith("'") ? args0Value.Insert(args0Value.Length - 1, "%") : $"concat({args0Value}, '%')")}";
|
switch (exp.Member.Name) {
|
||||||
if (args0Value.StartsWith("'") && args0Value.EndsWith("'")) return $"({left}) LIKE {args0Value.Insert(1, "%").Insert(args0Value.Length, "%")}";
|
case "PI": return $"pi()";
|
||||||
return $"({left}) like concat('%', {args0Value}, '%')";
|
}
|
||||||
case "ToLower": return $"lower({left})";
|
return null;
|
||||||
case "ToUpper": return $"upper({left})";
|
}
|
||||||
case "Substring": return $"substr({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} + 1, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Length": return $"char_length({left})";
|
internal override string ExpressionLambdaToSqlMemberAccessDateTime(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
case "IndexOf":
|
if (exp.Expression == null && exp.Member.Name == "Now") return "now()";
|
||||||
var indexOfFindStr = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
|
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
if (exp.Arguments.Count > 1 && exp.Arguments[1].Type.FullName == "System.Int32") return $"(locate({left}, {indexOfFindStr}, ParseLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName) + 1) - 1)";
|
switch (exp.Member.Name) {
|
||||||
return $"(locate({left}, {indexOfFindStr}) - 1)";
|
case "DayOfWeek": return $"(dayofweek({left}) - 1)";
|
||||||
case "PadLeft": return $"lpad({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
case "Day": return $"dayofmonth({left})";
|
||||||
case "PadRight": return $"rpad({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
case "DayOfYear": return $"dayofyear({left})";
|
||||||
case "Trim":
|
case "Month": return $"month({left})";
|
||||||
case "TrimStart":
|
case "Year": return $"year({left})";
|
||||||
case "TrimEnd":
|
case "Hour": return $"hour({left})";
|
||||||
if (exp.Arguments.Count == 0) {
|
case "Minute": return $"minute({left})";
|
||||||
if (exp.Method.Name == "Trim") return $"trim({left})";
|
case "Second": return $"second({left})";
|
||||||
if (exp.Method.Name == "TrimStart") return $"ltrim({left})";
|
case "Millisecond": return $"floor(microsecond({left}) / 1000)";
|
||||||
if (exp.Method.Name == "TrimStart") return $"rtrim({left})";
|
case "Ticks": return $"(time_to_sec({left}) * 10000000 + 621355968000000000)";
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override string ExpressionLambdaToSqlMemberAccessTimeSpan(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
|
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
switch (exp.Member.Name) {
|
||||||
|
case "Days": return $"floor(time_to_sec({left}) / {60 * 60 * 24})";
|
||||||
|
case "Hours": return $"extract(hour from {left})";
|
||||||
|
case "Milliseconds": return $"floor(extract(microsecond from {left}) / 1000)";
|
||||||
|
case "Minutes": return $"extract(minute from {left})";
|
||||||
|
case "Seconds": return $"extract(second from {left})";
|
||||||
|
case "Ticks": return $"(time_to_sec({left}) * 10000000 + extract(microsecond from {left}) * 10)";
|
||||||
|
case "TotalDays": return $"floor(extract(hour from {left}) / 24)";
|
||||||
|
case "TotalHours": return $"floor(time_to_sec({left}) / {60 * 60})";
|
||||||
|
case "TotalMilliseconds": return $"(time_to_sec({left}) * 1000 + floor(extract(microsecond from {left}) / 1000))";
|
||||||
|
case "TotalMinutes": return $"floor(time_to_sec({left}) / 60)";
|
||||||
|
case "TotalSeconds": return $"time_to_sec({left})";
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
internal override string ExpressionLambdaToSqlCallString(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
|
var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
switch (exp.Method.Name) {
|
||||||
|
case "StartsWith":
|
||||||
|
case "EndsWith":
|
||||||
|
case "Contains":
|
||||||
|
var args0Value = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
if (args0Value == "NULL") return $"({left}) IS NULL";
|
||||||
|
if (exp.Method.Name == "StartsWith") return $"({left}) LIKE {(args0Value.StartsWith("'") ? args0Value.Insert(1, "%") : $"concat('%', {args0Value})")}";
|
||||||
|
if (exp.Method.Name == "EndsWith") return $"({left}) LIKE {(args0Value.EndsWith("'") ? args0Value.Insert(args0Value.Length - 1, "%") : $"concat({args0Value}, '%')")}";
|
||||||
|
if (args0Value.StartsWith("'") && args0Value.EndsWith("'")) return $"({left}) LIKE {args0Value.Insert(1, "%").Insert(args0Value.Length, "%")}";
|
||||||
|
return $"({left}) LIKE concat('%', {args0Value}, '%')";
|
||||||
|
case "ToLower": return $"lower({left})";
|
||||||
|
case "ToUpper": return $"upper({left})";
|
||||||
|
case "Substring":
|
||||||
|
var substrArgs1 = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
if (long.TryParse(substrArgs1, out var testtrylng1)) substrArgs1 = (testtrylng1 + 1).ToString();
|
||||||
|
else substrArgs1 += " + 1";
|
||||||
|
if (exp.Arguments.Count == 1) return $"substr({left}, {substrArgs1})";
|
||||||
|
return $"substr({left}, {substrArgs1}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "IndexOf":
|
||||||
|
var indexOfFindStr = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
if (exp.Arguments.Count > 1 && exp.Arguments[1].Type.FullName == "System.Int32") {
|
||||||
|
var locateArgs1 = ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
if (long.TryParse(locateArgs1, out var testtrylng2)) locateArgs1 = (testtrylng2 + 1).ToString();
|
||||||
|
else locateArgs1 += " + 1";
|
||||||
|
return $"(locate({left}, {indexOfFindStr}, {locateArgs1}) - 1)";
|
||||||
|
}
|
||||||
|
return $"(locate({left}, {indexOfFindStr}) - 1)";
|
||||||
|
case "PadLeft":
|
||||||
|
if (exp.Arguments.Count == 1) return $"lpad({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
return $"lpad({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "PadRight":
|
||||||
|
if (exp.Arguments.Count == 1) return $"rpad({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
return $"rpad({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Trim":
|
||||||
|
case "TrimStart":
|
||||||
|
case "TrimEnd":
|
||||||
|
if (exp.Arguments.Count == 0) {
|
||||||
|
if (exp.Method.Name == "Trim") return $"trim({left})";
|
||||||
|
if (exp.Method.Name == "TrimStart") return $"ltrim({left})";
|
||||||
|
if (exp.Method.Name == "TrimEnd") return $"rtrim({left})";
|
||||||
|
}
|
||||||
|
foreach (var argsTrim02 in exp.Arguments) {
|
||||||
|
var argsTrim01s = new[] { argsTrim02 };
|
||||||
|
if (argsTrim02.NodeType == ExpressionType.NewArrayInit) {
|
||||||
|
var arritem = argsTrim02 as NewArrayExpression;
|
||||||
|
argsTrim01s = arritem.Expressions.ToArray();
|
||||||
}
|
}
|
||||||
foreach (var argsTrim01 in exp.Arguments) {
|
foreach (var argsTrim01 in argsTrim01s) {
|
||||||
if (exp.Method.Name == "Trim") left = $"trim({ExpressionLambdaToSql(argsTrim01, _tables, _selectColumnMap, tbtype, isQuoteName)} from {left})";
|
if (exp.Method.Name == "Trim") left = $"trim({ExpressionLambdaToSql(argsTrim01, _tables, _selectColumnMap, tbtype, isQuoteName)} from {left})";
|
||||||
if (exp.Method.Name == "TrimStart") left = $"trim(leading {ExpressionLambdaToSql(argsTrim01, _tables, _selectColumnMap, tbtype, isQuoteName)} from {left})";
|
if (exp.Method.Name == "TrimStart") left = $"trim(leading {ExpressionLambdaToSql(argsTrim01, _tables, _selectColumnMap, tbtype, isQuoteName)} from {left})";
|
||||||
if (exp.Method.Name == "TrimStart") left = $"trim(trailing {ExpressionLambdaToSql(argsTrim01, _tables, _selectColumnMap, tbtype, isQuoteName)} from {left})";
|
if (exp.Method.Name == "TrimEnd") left = $"trim(trailing {ExpressionLambdaToSql(argsTrim01, _tables, _selectColumnMap, tbtype, isQuoteName)} from {left})";
|
||||||
}
|
}
|
||||||
return left;
|
}
|
||||||
case "Replace": return $"replace({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
return left;
|
||||||
case "CompareTo": return $"strcmp({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
case "Replace": return $"replace({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
}
|
case "CompareTo": return $"strcmp({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
}
|
}
|
||||||
|
throw new Exception($"MySqlExpression 未现实函数表达式 {exp} 解析");
|
||||||
|
}
|
||||||
|
|
||||||
if (exp.Object.Type.FullName == "System.Math") {
|
internal override string ExpressionLambdaToSqlCallMath(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
switch (exp.Method.Name) {
|
switch (exp.Method.Name) {
|
||||||
case "Abs": return $"abs({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
case "Abs": return $"abs({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
case "Sign": return $"sign({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
case "Sign": return $"sign({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
case "Floor": return $"floor({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
case "Floor": return $"floor({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
case "Round":
|
case "Ceiling": return $"ceiling({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
if (exp.Arguments.Count > 1 && exp.Arguments[1].Type.FullName == "System.Int32") return $"round({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
case "Round":
|
||||||
return $"round({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
if (exp.Arguments.Count > 1 && exp.Arguments[1].Type.FullName == "System.Int32") return $"round({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
case "Exp": return $"exp({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
return $"round({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
case "Log": return $"log({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
case "Exp": return $"exp({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
case "Log10": return $"log10({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
case "Log": return $"log({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
case "Pow": return $"pow({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
case "Log10": return $"log10({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
case "Sqrt": return $"sqrt({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
case "Pow": return $"pow({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
case "PI": return $"pi()";
|
case "Sqrt": return $"sqrt({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
case "Cos": return $"cos({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
case "Cos": return $"cos({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
case "Sin": return $"sin({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
case "Sin": return $"sin({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
case "Tan": return $"tan({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
case "Tan": return $"tan({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
case "Acos": return $"acos({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
case "Acos": return $"acos({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
case "Asin": return $"asin({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
case "Asin": return $"asin({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
case "Atan": return $"atan({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
case "Atan": return $"atan({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
case "Atan2": return $"atan2({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
case "Atan2": return $"atan2({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
case "Truncate": return $"truncate({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, 0)";
|
case "Truncate": return $"truncate({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, 0)";
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
throw new Exception($"MySqlExpression 未现实函数表达式 {exp} 解析");
|
||||||
|
}
|
||||||
|
|
||||||
//dayofweek = DayOfWeek
|
internal override string ExpressionLambdaToSqlCallDateTime(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
//dayofmonth = Day
|
var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
//dayofyear = DayOfYear
|
var args1 = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
//month = Month
|
switch (exp.Method.Name) {
|
||||||
//year = Year
|
case "Add": return $"date_add({left}, interval (time_to_sec({args1}) * 1000000 + extract(microsecond from {args1})) microsecond)";
|
||||||
//hour = Hour
|
case "AddDays": return $"date_add({left}, interval {args1} day)";
|
||||||
//minute = Minute
|
case "AddHours": return $"date_add({left}, interval {args1} hour)";
|
||||||
//second = Second
|
case "AddMilliseconds": return $"date_add({left}, interval {args1} microsecond)";
|
||||||
/*
|
case "AddMinutes": return $"date_add({left}, interval {args1} minute)";
|
||||||
* date_add(date,interval expr type)
|
case "AddMonths": return $"date_add({left}, interval {args1} month)";
|
||||||
date_sub(date,interval expr type)
|
case "AddSeconds": return $"date_add({left}, interval {args1} second)";
|
||||||
adddate(date,interval expr type)
|
case "AddTicks": return $"date_add({left}, interval ({args1}) / 10 microsecond)";
|
||||||
subdate(date,interval expr type)
|
case "AddYears": return $"date_add({left}, interval {args1} year)";
|
||||||
对日期时间进行加减法运算
|
case "Subtract":
|
||||||
(adddate()和subdate()是date_add()和date_sub()的同义词,也
|
if (exp.Arguments[0].Type.FullName == "System.DateTime" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.DateTime")
|
||||||
可以用运算符+和-而不是函数
|
return $"({left} - {args1})";
|
||||||
date是一个datetime或date值,expr对date进行加减法的一个表
|
if (exp.Arguments[0].Type.FullName == "System.TimeSpan" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.TimeSpan")
|
||||||
达式字符串type指明表达式expr应该如何被解释
|
return $"date_sub({left}, interval (time_to_sec({args1}) * 1000000 + extract(microsecond from {args1})) microsecond)";
|
||||||
[type值 含义 期望的expr格式]:
|
break;
|
||||||
second 秒 seconds
|
}
|
||||||
minute 分钟 minutes
|
throw new Exception($"MySqlExpression 未现实函数表达式 {exp} 解析");
|
||||||
hour 时间 hours
|
}
|
||||||
day 天 days
|
internal override string ExpressionLambdaToSqlCallTimeSpan(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
month 月 months
|
var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
year 年 years
|
var args1 = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
minute_second 分钟和秒 "minutes:seconds"
|
switch (exp.Method.Name) {
|
||||||
hour_minute 小时和分钟 "hours:minutes"
|
case "Add": return $"(date_add('1970-1-1', interval (time_to_sec({left}) * 1000000 + extract(microsecond from {left}) + time_to_sec({args1}) * 1000000 + extract(microsecond from {args1})) microsecond)) microsecond) - date_add('1970-1-1', interval 0 microsecond) second))";
|
||||||
day_hour 天和小时 "days hours"
|
case "Subtract": return $"(date_add('1970-1-1', interval (time_to_sec({left}) * 1000000 + extract(microsecond from {left}) - time_to_sec({args1}) * 1000000 - extract(microsecond from {args1})) microsecond) - date_add('1970-1-1', interval 0 second))";
|
||||||
year_month 年和月 "years-months"
|
}
|
||||||
hour_second 小时, 分钟, "hours:minutes:seconds"
|
|
||||||
day_minute 天, 小时, 分钟 "days hours:minutes"
|
|
||||||
day_second 天, 小时, 分钟, 秒 "days
|
|
||||||
hours:minutes:seconds"
|
|
||||||
expr中允许任何标点做分隔符,如果所有是date值时结果是一个
|
|
||||||
date值,否则结果是一个datetime值)
|
|
||||||
如果type关键词不完整,则mysql从右端取值,day_second因为缺
|
|
||||||
少小时分钟等于minute_second)
|
|
||||||
如果增加month、year_month或year,天数大于结果月份的最大天
|
|
||||||
数则使用最大天数)
|
|
||||||
mysql> select "1997-12-31 23:59:59" + interval 1 second;
|
|
||||||
|
|
||||||
-> 1998-01-01 00:00:00
|
|
||||||
mysql> select interval 1 day + "1997-12-31";
|
|
||||||
-> 1998-01-01
|
|
||||||
mysql> select "1998-01-01" - interval 1 second;
|
|
||||||
-> 1997-12-31 23:59:59
|
|
||||||
mysql> select date_add("1997-12-31 23:59:59",interval 1
|
|
||||||
second);
|
|
||||||
-> 1998-01-01 00:00:00
|
|
||||||
mysql> select date_add("1997-12-31 23:59:59",interval 1
|
|
||||||
day);
|
|
||||||
-> 1998-01-01 23:59:59
|
|
||||||
mysql> select date_add("1997-12-31 23:59:59",interval
|
|
||||||
"1:1" minute_second);
|
|
||||||
-> 1998-01-01 00:01:00
|
|
||||||
mysql> select date_sub("1998-01-01 00:00:00",interval "1
|
|
||||||
1:1:1" day_second);
|
|
||||||
-> 1997-12-30 22:58:59
|
|
||||||
mysql> select date_add("1998-01-01 00:00:00", interval "-1
|
|
||||||
10" day_hour);
|
|
||||||
-> 1997-12-30 14:00:00
|
|
||||||
mysql> select date_sub("1998-01-02", interval 31 day);
|
|
||||||
-> 1997-12-02
|
|
||||||
mysql> select extract(year from "1999-07-02");
|
|
||||||
-> 1999
|
|
||||||
mysql> select extract(year_month from "1999-07-02
|
|
||||||
01:02:03");
|
|
||||||
-> 199907
|
|
||||||
mysql> select extract(day_minute from "1999-07-02
|
|
||||||
01:02:03");
|
|
||||||
-> 20102
|
|
||||||
*/
|
|
||||||
|
|
||||||
//convert
|
|
||||||
var xxx = DateTime.Now.ToString("");
|
|
||||||
|
|
||||||
|
|
||||||
throw new Exception($"MySqlExpression 未现实函数表达式 {exp} 解析");
|
throw new Exception($"MySqlExpression 未现实函数表达式 {exp} 解析");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,5 +44,6 @@ namespace FreeSql.MySql {
|
|||||||
internal override string QuoteSqlName(string name) => $"`{name.Trim('`').Replace(".", "`.`")}`";
|
internal override string QuoteSqlName(string name) => $"`{name.Trim('`').Replace(".", "`.`")}`";
|
||||||
internal override string QuoteParamterName(string name) => $"?{name}";
|
internal override string QuoteParamterName(string name) => $"?{name}";
|
||||||
internal override string IsNull(string sql, object value) => $"ifnull({sql}, {value})";
|
internal override string IsNull(string sql, object value) => $"ifnull({sql}, {value})";
|
||||||
|
internal override string StringConcat(string left, string right, Type leftType, Type rightType) => $"concat({left}, {right})";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using FreeSql.Internal;
|
using FreeSql.Internal;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace FreeSql.PostgreSQL.Curd {
|
namespace FreeSql.PostgreSQL.Curd {
|
||||||
@ -19,7 +20,7 @@ namespace FreeSql.PostgreSQL.Curd {
|
|||||||
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
||||||
++colidx;
|
++colidx;
|
||||||
}
|
}
|
||||||
return _orm.Ado.Query<T1>(sb.ToString());
|
return _orm.Ado.Query<T1>(CommandType.Text, sb.ToString(), _params.ToArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ namespace FreeSql.PostgreSQL.Curd {
|
|||||||
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
||||||
++colidx;
|
++colidx;
|
||||||
}
|
}
|
||||||
return _orm.Ado.Query<T1>(sb.ToString());
|
return _orm.Ado.Query<T1>(CommandType.Text, sb.ToString(), _params);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,10 @@ namespace FreeSql.PostgreSQL.Curd {
|
|||||||
|
|
||||||
class PostgreSQLSelect<T1> : FreeSql.Internal.CommonProvider.Select1Provider<T1> where T1 : class {
|
class PostgreSQLSelect<T1> : FreeSql.Internal.CommonProvider.Select1Provider<T1> where T1 : class {
|
||||||
|
|
||||||
internal static string ToSqlStatic(CommonUtils _commonUtils, string _select, string field, StringBuilder _join, StringBuilder _where, string _groupby, string _having, string _orderby, int _skip, int _limit, List<SelectTableInfo> _tables) {
|
internal static string ToSqlStatic(CommonUtils _commonUtils, string _select, string field, StringBuilder _join, StringBuilder _where, string _groupby, string _having, string _orderby, int _skip, int _limit, List<SelectTableInfo> _tables, IFreeSql _orm) {
|
||||||
|
if (_orm.CodeFirst.IsAutoSyncStructure)
|
||||||
|
_orm.CodeFirst.SyncStructure(_tables.Select(a => a.Table.Type).ToArray());
|
||||||
|
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
sb.Append(_select).Append(field).Append(" \r\nFROM ");
|
sb.Append(_select).Append(field).Append(" \r\nFROM ");
|
||||||
var tbsjoin = _tables.Where(a => a.Type != SelectTableInfoType.From).ToArray();
|
var tbsjoin = _tables.Where(a => a.Type != SelectTableInfoType.From).ToArray();
|
||||||
@ -75,42 +78,42 @@ namespace FreeSql.PostgreSQL.Curd {
|
|||||||
public override ISelect<T1, T2, T3, T4, T5, T6, T7, T8> From<T2, T3, T4, T5, T6, T7, T8>(Expression<Func<ISelectFromExpression<T1>, T2, T3, T4, T5, T6, T7, T8, ISelectFromExpression<T1>>> exp) { this.InternalFrom(exp?.Body); var ret = new PostgreSQLSelect<T1, T2, T3, T4, T5, T6, T7, T8>(_orm, _commonUtils, _commonExpression, null); PostgreSQLSelect<T1>.CopyData(this, ret); return ret; }
|
public override ISelect<T1, T2, T3, T4, T5, T6, T7, T8> From<T2, T3, T4, T5, T6, T7, T8>(Expression<Func<ISelectFromExpression<T1>, T2, T3, T4, T5, T6, T7, T8, ISelectFromExpression<T1>>> exp) { this.InternalFrom(exp?.Body); var ret = new PostgreSQLSelect<T1, T2, T3, T4, T5, T6, T7, T8>(_orm, _commonUtils, _commonExpression, null); PostgreSQLSelect<T1>.CopyData(this, ret); return ret; }
|
||||||
public override ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9> From<T2, T3, T4, T5, T6, T7, T8, T9>(Expression<Func<ISelectFromExpression<T1>, T2, T3, T4, T5, T6, T7, T8, T9, ISelectFromExpression<T1>>> exp) { this.InternalFrom(exp?.Body); var ret = new PostgreSQLSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9>(_orm, _commonUtils, _commonExpression, null); PostgreSQLSelect<T1>.CopyData(this, ret); return ret; }
|
public override ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9> From<T2, T3, T4, T5, T6, T7, T8, T9>(Expression<Func<ISelectFromExpression<T1>, T2, T3, T4, T5, T6, T7, T8, T9, ISelectFromExpression<T1>>> exp) { this.InternalFrom(exp?.Body); var ret = new PostgreSQLSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9>(_orm, _commonUtils, _commonExpression, null); PostgreSQLSelect<T1>.CopyData(this, ret); return ret; }
|
||||||
public override ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> From<T2, T3, T4, T5, T6, T7, T8, T9, T10>(Expression<Func<ISelectFromExpression<T1>, T2, T3, T4, T5, T6, T7, T8, T9, T10, ISelectFromExpression<T1>>> exp) { this.InternalFrom(exp?.Body); var ret = new PostgreSQLSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(_orm, _commonUtils, _commonExpression, null); PostgreSQLSelect<T1>.CopyData(this, ret); return ret; }
|
public override ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> From<T2, T3, T4, T5, T6, T7, T8, T9, T10>(Expression<Func<ISelectFromExpression<T1>, T2, T3, T4, T5, T6, T7, T8, T9, T10, ISelectFromExpression<T1>>> exp) { this.InternalFrom(exp?.Body); var ret = new PostgreSQLSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(_orm, _commonUtils, _commonExpression, null); PostgreSQLSelect<T1>.CopyData(this, ret); return ret; }
|
||||||
public override string ToSql(string field = null) => ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class PostgreSQLSelect<T1, T2> : FreeSql.Internal.CommonProvider.Select2Provider<T1, T2> where T1 : class where T2 : class {
|
class PostgreSQLSelect<T1, T2> : FreeSql.Internal.CommonProvider.Select2Provider<T1, T2> where T1 : class where T2 : class {
|
||||||
public PostgreSQLSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public PostgreSQLSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => PostgreSQLSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => PostgreSQLSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class PostgreSQLSelect<T1, T2, T3> : FreeSql.Internal.CommonProvider.Select3Provider<T1, T2, T3> where T1 : class where T2 : class where T3 : class {
|
class PostgreSQLSelect<T1, T2, T3> : FreeSql.Internal.CommonProvider.Select3Provider<T1, T2, T3> where T1 : class where T2 : class where T3 : class {
|
||||||
public PostgreSQLSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public PostgreSQLSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => PostgreSQLSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => PostgreSQLSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class PostgreSQLSelect<T1, T2, T3, T4> : FreeSql.Internal.CommonProvider.Select4Provider<T1, T2, T3, T4> where T1 : class where T2 : class where T3 : class where T4 : class {
|
class PostgreSQLSelect<T1, T2, T3, T4> : FreeSql.Internal.CommonProvider.Select4Provider<T1, T2, T3, T4> where T1 : class where T2 : class where T3 : class where T4 : class {
|
||||||
public PostgreSQLSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public PostgreSQLSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => PostgreSQLSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => PostgreSQLSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class PostgreSQLSelect<T1, T2, T3, T4, T5> : FreeSql.Internal.CommonProvider.Select5Provider<T1, T2, T3, T4, T5> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class {
|
class PostgreSQLSelect<T1, T2, T3, T4, T5> : FreeSql.Internal.CommonProvider.Select5Provider<T1, T2, T3, T4, T5> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class {
|
||||||
public PostgreSQLSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public PostgreSQLSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => PostgreSQLSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => PostgreSQLSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class PostgreSQLSelect<T1, T2, T3, T4, T5, T6> : FreeSql.Internal.CommonProvider.Select6Provider<T1, T2, T3, T4, T5, T6> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class {
|
class PostgreSQLSelect<T1, T2, T3, T4, T5, T6> : FreeSql.Internal.CommonProvider.Select6Provider<T1, T2, T3, T4, T5, T6> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class {
|
||||||
public PostgreSQLSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public PostgreSQLSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => PostgreSQLSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => PostgreSQLSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class PostgreSQLSelect<T1, T2, T3, T4, T5, T6, T7> : FreeSql.Internal.CommonProvider.Select7Provider<T1, T2, T3, T4, T5, T6, T7> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class {
|
class PostgreSQLSelect<T1, T2, T3, T4, T5, T6, T7> : FreeSql.Internal.CommonProvider.Select7Provider<T1, T2, T3, T4, T5, T6, T7> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class {
|
||||||
public PostgreSQLSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public PostgreSQLSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => PostgreSQLSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => PostgreSQLSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class PostgreSQLSelect<T1, T2, T3, T4, T5, T6, T7, T8> : FreeSql.Internal.CommonProvider.Select8Provider<T1, T2, T3, T4, T5, T6, T7, T8> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class {
|
class PostgreSQLSelect<T1, T2, T3, T4, T5, T6, T7, T8> : FreeSql.Internal.CommonProvider.Select8Provider<T1, T2, T3, T4, T5, T6, T7, T8> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class {
|
||||||
public PostgreSQLSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public PostgreSQLSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => PostgreSQLSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => PostgreSQLSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class PostgreSQLSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9> : FreeSql.Internal.CommonProvider.Select9Provider<T1, T2, T3, T4, T5, T6, T7, T8, T9> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class {
|
class PostgreSQLSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9> : FreeSql.Internal.CommonProvider.Select9Provider<T1, T2, T3, T4, T5, T6, T7, T8, T9> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class {
|
||||||
public PostgreSQLSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public PostgreSQLSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => PostgreSQLSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => PostgreSQLSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class PostgreSQLSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> : FreeSql.Internal.CommonProvider.Select10Provider<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class where T10 : class {
|
class PostgreSQLSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> : FreeSql.Internal.CommonProvider.Select10Provider<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class where T10 : class {
|
||||||
public PostgreSQLSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public PostgreSQLSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => PostgreSQLSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => PostgreSQLSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
using FreeSql.Internal;
|
using FreeSql.Internal;
|
||||||
using FreeSql.Internal.Model;
|
using FreeSql.Internal.Model;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace FreeSql.PostgreSQL.Curd {
|
namespace FreeSql.PostgreSQL.Curd {
|
||||||
@ -21,29 +23,37 @@ namespace FreeSql.PostgreSQL.Curd {
|
|||||||
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
||||||
++colidx;
|
++colidx;
|
||||||
}
|
}
|
||||||
return _orm.Ado.Query<T1>(sb.ToString());
|
return _orm.Ado.Query<T1>(CommandType.Text, sb.ToString(), _params.Concat(_paramsSource).ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void ToSqlCase(StringBuilder caseWhen, ColumnInfo[] primarys) {
|
protected override void ToSqlCase(StringBuilder caseWhen, ColumnInfo[] primarys) {
|
||||||
if (_table.Primarys.Length > 1) caseWhen.Append("(");
|
if (_table.Primarys.Length == 1) {
|
||||||
|
caseWhen.Append(_commonUtils.QuoteSqlName(_table.Primarys.First().Attribute.Name));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
caseWhen.Append("(");
|
||||||
var pkidx = 0;
|
var pkidx = 0;
|
||||||
foreach (var pk in _table.Primarys) {
|
foreach (var pk in _table.Primarys) {
|
||||||
if (pkidx > 0) caseWhen.Append(", ");
|
if (pkidx > 0) caseWhen.Append(" || ");
|
||||||
caseWhen.Append(_commonUtils.QuoteSqlName(pk.Attribute.Name)).Append("::varchar");
|
caseWhen.Append(_commonUtils.QuoteSqlName(pk.Attribute.Name)).Append("::varchar");
|
||||||
++pkidx;
|
++pkidx;
|
||||||
}
|
}
|
||||||
if (_table.Primarys.Length > 1) caseWhen.Append(")");
|
caseWhen.Append(")");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void ToSqlWhen(StringBuilder sb, ColumnInfo[] primarys, object d) {
|
protected override void ToSqlWhen(StringBuilder sb, ColumnInfo[] primarys, object d) {
|
||||||
if (_table.Primarys.Length > 1) sb.Append("(");
|
if (_table.Primarys.Length == 1) {
|
||||||
|
sb.Append(_commonUtils.FormatSql("{0}", _table.Properties.TryGetValue(_table.Primarys.First().CsName, out var tryp2) ? tryp2.GetValue(d) : null));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sb.Append("(");
|
||||||
var pkidx = 0;
|
var pkidx = 0;
|
||||||
foreach (var pk in _table.Primarys) {
|
foreach (var pk in _table.Primarys) {
|
||||||
if (pkidx > 0) sb.Append(", ");
|
if (pkidx > 0) sb.Append(" || ");
|
||||||
sb.Append(_commonUtils.FormatSql("{0}", _table.Properties.TryGetValue(pk.CsName, out var tryp2) ? tryp2.GetValue(d) : null)).Append("::varchar");
|
sb.Append(_commonUtils.FormatSql("{0}", _table.Properties.TryGetValue(pk.CsName, out var tryp2) ? tryp2.GetValue(d) : null)).Append("::varchar");
|
||||||
++pkidx;
|
++pkidx;
|
||||||
}
|
}
|
||||||
if (_table.Primarys.Length > 1) sb.Append(")");
|
sb.Append(")");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ namespace FreeSql.PostgreSQL {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
static DateTime dt1970 = new DateTime(1970, 1, 1);
|
||||||
public override object AddslashesProcessParam(object param) {
|
public override object AddslashesProcessParam(object param) {
|
||||||
if (param == null) return "NULL";
|
if (param == null) return "NULL";
|
||||||
if (param is bool || param is bool?)
|
if (param is bool || param is bool?)
|
||||||
@ -31,12 +32,16 @@ namespace FreeSql.PostgreSQL {
|
|||||||
return string.Concat("'", param.ToString().Replace("'", "''"), "'");
|
return string.Concat("'", param.ToString().Replace("'", "''"), "'");
|
||||||
else if (decimal.TryParse(string.Concat(param), out var trydec))
|
else if (decimal.TryParse(string.Concat(param), out var trydec))
|
||||||
return param;
|
return param;
|
||||||
else if (param is DateTime) {
|
else if (param is DateTime)
|
||||||
DateTime dt = (DateTime)param;
|
return string.Concat("'", ((DateTime)param).ToString("yyyy-MM-dd HH:mm:ss.ffffff"), "'");
|
||||||
return string.Concat("'", dt.ToString("yyyy-MM-dd HH:mm:ss.ffffff"), "'");
|
else if (param is DateTime?)
|
||||||
} else if (param is DateTime?) {
|
return string.Concat("'", (param as DateTime?).Value.ToString("yyyy-MM-dd HH:mm:ss.ffffff"), "'");
|
||||||
DateTime? dt = param as DateTime?;
|
else if (param is TimeSpan) {
|
||||||
return string.Concat("'", dt.Value.ToString("yyyy-MM-dd HH:mm:ss.ffffff"), "'");
|
var ts = (TimeSpan)param;
|
||||||
|
return string.Concat("'", ts.Ticks > 0 ? "" : "-", ts.TotalHours, dt1970.AddTicks(Math.Abs(ts.Ticks)).ToString(":mm:ss.ffffff"), "'");
|
||||||
|
} else if (param is TimeSpan) {
|
||||||
|
var ts = (param as TimeSpan?).Value;
|
||||||
|
return string.Concat("'", ts.Ticks > 0 ? "" : "-", ts.TotalHours, dt1970.AddTicks(Math.Abs(ts.Ticks)).ToString(":mm:ss.ffffff"), "'");
|
||||||
} else if (param is IEnumerable) {
|
} else if (param is IEnumerable) {
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
var ie = param as IEnumerable;
|
var ie = param as IEnumerable;
|
||||||
|
@ -1,11 +1,16 @@
|
|||||||
using FreeSql.DatabaseModel;
|
using FreeSql.DatabaseModel;
|
||||||
using FreeSql.Internal;
|
using FreeSql.Internal;
|
||||||
using FreeSql.Internal.Model;
|
using FreeSql.Internal.Model;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using Npgsql.LegacyPostgis;
|
||||||
using NpgsqlTypes;
|
using NpgsqlTypes;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.NetworkInformation;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace FreeSql.PostgreSQL {
|
namespace FreeSql.PostgreSQL {
|
||||||
@ -22,63 +27,96 @@ namespace FreeSql.PostgreSQL {
|
|||||||
|
|
||||||
public bool IsAutoSyncStructure { get; set; } = true;
|
public bool IsAutoSyncStructure { get; set; } = true;
|
||||||
|
|
||||||
static readonly Dictionary<string, (NpgsqlDbType type, string dbtype, string dbtypeFull, bool? isUnsigned, bool? isnullable)> _dicCsToDb = new Dictionary<string, (NpgsqlDbType type, string dbtype, string dbtypeFull, bool? isUnsigned, bool? isnullable)>() {
|
static object _dicCsToDbLock = new object();
|
||||||
|
static Dictionary<string, (NpgsqlDbType type, string dbtype, string dbtypeFull, bool? isUnsigned, bool? isnullable)> _dicCsToDb = new Dictionary<string, (NpgsqlDbType type, string dbtype, string dbtypeFull, bool? isUnsigned, bool? isnullable)>() {
|
||||||
|
|
||||||
{ "System.Int16", (NpgsqlDbType.Smallint, "int2","int2 NOT NULL", false, false) },{ "System.Nullable`1[System.Int16]", (NpgsqlDbType.Smallint, "int2", "int2", false, true) },
|
{ typeof(sbyte).FullName, (NpgsqlDbType.Smallint, "int2","int2 NOT NULL", false, false) },{ typeof(sbyte?).FullName, (NpgsqlDbType.Smallint, "int2", "int2", false, true) },
|
||||||
{ "System.Int32", (NpgsqlDbType.Integer, "int4","int4 NOT NULL", false, false) },{ "System.Nullable`1[System.Int32]", (NpgsqlDbType.Integer, "int4", "int4", false, true) },
|
{ typeof(short).FullName, (NpgsqlDbType.Smallint, "int2","int2 NOT NULL", false, false) },{ typeof(short?).FullName, (NpgsqlDbType.Smallint, "int2", "int2", false, true) },
|
||||||
{ "System.Int64", (NpgsqlDbType.Bigint, "int8","int8 NOT NULL", false, false) },{ "System.Nullable`1[System.Int64]", (NpgsqlDbType.Bigint, "int8", "int8", false, true) },
|
{ typeof(int).FullName, (NpgsqlDbType.Integer, "int4","int4 NOT NULL", false, false) },{ typeof(int?).FullName, (NpgsqlDbType.Integer, "int4", "int4", false, true) },
|
||||||
|
{ typeof(long).FullName, (NpgsqlDbType.Bigint, "int8","int8 NOT NULL", false, false) },{ typeof(long?).FullName, (NpgsqlDbType.Bigint, "int8", "int8", false, true) },
|
||||||
|
|
||||||
{ "System.Single", (NpgsqlDbType.Real, "float4","float4 NOT NULL", false, false) },{ "System.Nullable`1[System.Single]", (NpgsqlDbType.Real, "float4", "float4", false, true) },
|
{ typeof(byte).FullName, (NpgsqlDbType.Smallint, "int2","int2 NOT NULL", false, false) },{ typeof(byte?).FullName, (NpgsqlDbType.Smallint, "int2", "int2", false, true) },
|
||||||
{ "System.Double", (NpgsqlDbType.Double, "float8","float8 NOT NULL", false, false) },{ "System.Nullable`1[System.Double]", (NpgsqlDbType.Double, "float8", "float8", false, true) },
|
{ typeof(ushort).FullName, (NpgsqlDbType.Smallint, "int2","int2 NOT NULL", false, false) },{ typeof(ushort?).FullName, (NpgsqlDbType.Smallint, "int2", "int2", false, true) },
|
||||||
{ "System.Decimal", (NpgsqlDbType.Numeric, "numeric", "numeric(10,2) NOT NULL", false, false) },{ "System.Nullable`1[System.Decimal]", (NpgsqlDbType.Numeric, "numeric", "numeric(10,2)", false, true) },
|
{ typeof(uint).FullName, (NpgsqlDbType.Integer, "int4","int4 NOT NULL", false, false) },{ typeof(uint?).FullName, (NpgsqlDbType.Integer, "int4", "int4", false, true) },
|
||||||
|
{ typeof(ulong).FullName, (NpgsqlDbType.Bigint, "int8","int8 NOT NULL", false, false) },{ typeof(ulong?).FullName, (NpgsqlDbType.Bigint, "int8", "int8", false, true) },
|
||||||
|
|
||||||
{ "System.String", (NpgsqlDbType.Varchar, "varchar", "varchar(255)", false, null) },
|
{ typeof(float).FullName, (NpgsqlDbType.Real, "float4","float4 NOT NULL", false, false) },{ typeof(float?).FullName, (NpgsqlDbType.Real, "float4", "float4", false, true) },
|
||||||
|
{ typeof(double).FullName, (NpgsqlDbType.Double, "float8","float8 NOT NULL", false, false) },{ typeof(double?).FullName, (NpgsqlDbType.Double, "float8", "float8", false, true) },
|
||||||
|
{ typeof(decimal).FullName, (NpgsqlDbType.Numeric, "numeric", "numeric(10,2) NOT NULL", false, false) },{ typeof(decimal?).FullName, (NpgsqlDbType.Numeric, "numeric", "numeric(10,2)", false, true) },
|
||||||
|
|
||||||
{ "System.TimeSpan", (NpgsqlDbType.Time, "time","time NOT NULL", false, false) },{ "System.Nullable`1[System.TimeSpan]", (NpgsqlDbType.Time, "time", "time",false, true) },
|
{ typeof(string).FullName, (NpgsqlDbType.Varchar, "varchar", "varchar(255)", false, null) },
|
||||||
{ "System.DateTime", (NpgsqlDbType.Timestamp, "timestamp", "timestamp NOT NULL", false, false) },{ "System.Nullable`1[System.DateTime]", (NpgsqlDbType.Timestamp, "timestamp", "timestamp", false, true) },
|
|
||||||
|
|
||||||
{ "System.Boolean", (NpgsqlDbType.Boolean, "bool","bool NOT NULL", null, false) },{ "System.Nullable`1[System.Boolean]", (NpgsqlDbType.Bit, "bool","bool", null, true) },
|
{ typeof(TimeSpan).FullName, (NpgsqlDbType.Time, "time","time NOT NULL", false, false) },{ typeof(TimeSpan?).FullName, (NpgsqlDbType.Time, "time", "time",false, true) },
|
||||||
{ "System.Byte[]", (NpgsqlDbType.Bytea, "bytea", "bytea", false, null) },
|
{ typeof(DateTime).FullName, (NpgsqlDbType.Timestamp, "timestamp", "timestamp NOT NULL", false, false) },{ typeof(DateTime?).FullName, (NpgsqlDbType.Timestamp, "timestamp", "timestamp", false, true) },
|
||||||
{ "System.BitArray", (NpgsqlDbType.Varbit, "varbit", "varbit(255)", false, null) },
|
|
||||||
|
|
||||||
{ "NpgsqlTypes.NpgsqlPoint", (NpgsqlDbType.Point, "point", "point", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlPoint]", (NpgsqlDbType.Point, "point", "point", false, true) },
|
{ typeof(bool).FullName, (NpgsqlDbType.Boolean, "bool","bool NOT NULL", null, false) },{ typeof(bool?).FullName, (NpgsqlDbType.Bit, "bool","bool", null, true) },
|
||||||
{ "NpgsqlTypes.NpgsqlLine", (NpgsqlDbType.Line, "line", "line", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlLine]", (NpgsqlDbType.Line, "line", "line", false, true) },
|
{ typeof(Byte[]).FullName, (NpgsqlDbType.Bytea, "bytea", "bytea", false, null) },
|
||||||
{ "NpgsqlTypes.NpgsqlLSeg", (NpgsqlDbType.LSeg, "lseg", "lseg", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlLSeg]", (NpgsqlDbType.LSeg, "lseg", "lseg", false, true) },
|
{ typeof(BitArray).FullName, (NpgsqlDbType.Varbit, "varbit", "varbit(64)", false, null) },
|
||||||
{ "NpgsqlTypes.NpgsqlBox", (NpgsqlDbType.Box, "box", "box", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlBox]", (NpgsqlDbType.Box, "box", "box", false, true) },
|
|
||||||
{ "NpgsqlTypes.NpgsqlPath", (NpgsqlDbType.Path, "path", "path", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlPath]", (NpgsqlDbType.Path, "path", "path", false, true) },
|
|
||||||
{ "NpgsqlTypes.NpgsqlPolygon", (NpgsqlDbType.Polygon, "polygon", "polygon", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlPolygon]", (NpgsqlDbType.Polygon, "polygon", "polygon", false, true) },
|
|
||||||
{ "NpgsqlTypes.NpgsqlCircle", (NpgsqlDbType.Circle, "circle", "circle", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlCircle]", (NpgsqlDbType.Circle, "circle", "circle", false, true) },
|
|
||||||
|
|
||||||
{ "System.ValueTuple`2[[System.Net.IPAddress, System.Int32]]", (NpgsqlDbType.Cidr, "cidr", "cidr", false, false) },{ "System.Nullable`1[System.ValueTuple`2[[System.Net.IPAddress, System.Int32]]]", (NpgsqlDbType.Cidr, "cidr", "cidr", false, true) },
|
{ typeof(NpgsqlPoint).FullName, (NpgsqlDbType.Point, "point", "point NOT NULL", false, false) },{ typeof(NpgsqlPoint?).FullName, (NpgsqlDbType.Point, "point", "point", false, true) },
|
||||||
{ "System.Net.IPAddress", (NpgsqlDbType.Inet, "inet", "inet", false, null) },
|
{ typeof(NpgsqlLine).FullName, (NpgsqlDbType.Line, "line", "line NOT NULL", false, false) },{ typeof(NpgsqlLine?).FullName, (NpgsqlDbType.Line, "line", "line", false, true) },
|
||||||
{ "System.Net.NetworkInformation.PhysicalAddress", (NpgsqlDbType.MacAddr, "macaddr", "macaddr", false, null) },
|
{ typeof(NpgsqlLSeg).FullName, (NpgsqlDbType.LSeg, "lseg", "lseg NOT NULL", false, false) },{ typeof(NpgsqlLSeg?).FullName, (NpgsqlDbType.LSeg, "lseg", "lseg", false, true) },
|
||||||
|
{ typeof(NpgsqlBox).FullName, (NpgsqlDbType.Box, "box", "box NOT NULL", false, false) },{ typeof(NpgsqlBox?).FullName, (NpgsqlDbType.Box, "box", "box", false, true) },
|
||||||
|
{ typeof(NpgsqlPath).FullName, (NpgsqlDbType.Path, "path", "path NOT NULL", false, false) },{ typeof(NpgsqlPath?).FullName, (NpgsqlDbType.Path, "path", "path", false, true) },
|
||||||
|
{ typeof(NpgsqlPolygon).FullName, (NpgsqlDbType.Polygon, "polygon", "polygon NOT NULL", false, false) },{ typeof(NpgsqlPolygon?).FullName, (NpgsqlDbType.Polygon, "polygon", "polygon", false, true) },
|
||||||
|
{ typeof(NpgsqlCircle).FullName, (NpgsqlDbType.Circle, "circle", "circle NOT NULL", false, false) },{ typeof(NpgsqlCircle?).FullName, (NpgsqlDbType.Circle, "circle", "circle", false, true) },
|
||||||
|
|
||||||
{ "Newtonsoft.Json.Linq.JToken", (NpgsqlDbType.Jsonb, "jsonb", "jsonb", false, null) },
|
{ typeof((IPAddress Address, int Subnet)).FullName, (NpgsqlDbType.Cidr, "cidr", "cidr NOT NULL", false, false) },{ typeof((IPAddress Address, int Subnet)?).FullName, (NpgsqlDbType.Cidr, "cidr", "cidr", false, true) },
|
||||||
{ "Newtonsoft.Json.Linq.JObject", (NpgsqlDbType.Jsonb, "jsonb", "jsonb", false, null) },
|
{ typeof(IPAddress).FullName, (NpgsqlDbType.Inet, "inet", "inet", false, null) },
|
||||||
{ "Newtonsoft.Json.Linq.JArray", (NpgsqlDbType.Jsonb, "jsonb", "jsonb", false, null) },
|
{ typeof(PhysicalAddress).FullName, (NpgsqlDbType.MacAddr, "macaddr", "macaddr", false, null) },
|
||||||
{ "System.Guid", (NpgsqlDbType.Uuid, "uuid", "uuid", false, false) },{ "System.Nullable`1[System.Guid]", (NpgsqlDbType.Uuid, "uuid", "uuid", false, true) },
|
|
||||||
|
|
||||||
{ "NpgsqlTypes.NpgsqlRange<int>", (NpgsqlDbType.Range | NpgsqlDbType.Integer, "int4range", "int4range", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlRange<int>]", (NpgsqlDbType.Range | NpgsqlDbType.Integer, "int4range", "int4range", false, true) },
|
{ typeof(JToken).FullName, (NpgsqlDbType.Jsonb, "jsonb", "jsonb", false, null) },
|
||||||
{ "NpgsqlTypes.NpgsqlRange<long>", (NpgsqlDbType.Range | NpgsqlDbType.Bigint, "int8range", "int8range", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlRange<long>]", (NpgsqlDbType.Range | NpgsqlDbType.Bigint, "int8range", "int8range", false, true) },
|
{ typeof(JObject).FullName, (NpgsqlDbType.Jsonb, "jsonb", "jsonb", false, null) },
|
||||||
{ "NpgsqlTypes.NpgsqlRange<decimal>", (NpgsqlDbType.Range | NpgsqlDbType.Numeric, "numrange", "numrange", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlRange<decimal>]", (NpgsqlDbType.Range | NpgsqlDbType.Numeric, "numrange", "numrange", false, true) },
|
{ typeof(JArray).FullName, (NpgsqlDbType.Jsonb, "jsonb", "jsonb", false, null) },
|
||||||
{ "NpgsqlTypes.NpgsqlRange<DateTime>", (NpgsqlDbType.Range | NpgsqlDbType.Timestamp, "tsrange", "tsrange", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlRange<DateTime>]", (NpgsqlDbType.Range | NpgsqlDbType.Timestamp, "tsrange", "tsrange", false, true) },
|
{ typeof(Guid).FullName, (NpgsqlDbType.Uuid, "uuid", "uuid NOT NULL", false, false) },{ typeof(Guid?).FullName, (NpgsqlDbType.Uuid, "uuid", "uuid", false, true) },
|
||||||
|
|
||||||
{ "Dictionary<string, string>", (NpgsqlDbType.Hstore, "hstore", "hstore", false, null) },
|
{ typeof(NpgsqlRange<int>).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Integer, "int4range", "int4range NOT NULL", false, false) },{ typeof(NpgsqlRange<int>?).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Integer, "int4range", "int4range", false, true) },
|
||||||
{ "Npgsql.LegacyPostgis.PostgisGeometry", (NpgsqlDbType.Geometry, "geometry", "geometry", false, null) },
|
{ typeof(NpgsqlRange<long>).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Bigint, "int8range", "int8range NOT NULL", false, false) },{ typeof(NpgsqlRange<long>?).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Bigint, "int8range", "int8range", false, true) },
|
||||||
|
{ typeof(NpgsqlRange<decimal>).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Numeric, "numrange", "numrange NOT NULL", false, false) },{ typeof(NpgsqlRange<decimal>?).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Numeric, "numrange", "numrange", false, true) },
|
||||||
|
{ typeof(NpgsqlRange<DateTime>).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Timestamp, "tsrange", "tsrange NOT NULL", false, false) },{ typeof(NpgsqlRange<DateTime>?).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Timestamp, "tsrange", "tsrange", false, true) },
|
||||||
|
|
||||||
|
{ typeof(Dictionary<string, string>).FullName, (NpgsqlDbType.Hstore, "hstore", "hstore", false, null) },
|
||||||
|
{ typeof(PostgisPoint).FullName, (NpgsqlDbType.Geometry, "geometry", "geometry", false, null) },
|
||||||
|
{ typeof(PostgisLineString).FullName, (NpgsqlDbType.Geometry, "geometry", "geometry", false, null) },
|
||||||
|
{ typeof(PostgisPolygon).FullName, (NpgsqlDbType.Geometry, "geometry", "geometry", false, null) },
|
||||||
|
{ typeof(PostgisMultiPoint).FullName, (NpgsqlDbType.Geometry, "geometry", "geometry", false, null) },
|
||||||
|
{ typeof(PostgisMultiLineString).FullName, (NpgsqlDbType.Geometry, "geometry", "geometry", false, null) },
|
||||||
|
{ typeof(PostgisMultiPolygon).FullName, (NpgsqlDbType.Geometry, "geometry", "geometry", false, null) },
|
||||||
|
{ typeof(PostgisGeometry).FullName, (NpgsqlDbType.Geometry, "geometry", "geometry", false, null) },
|
||||||
|
{ typeof(PostgisGeometryCollection).FullName, (NpgsqlDbType.Geometry, "geometry", "geometry", false, null) },
|
||||||
};
|
};
|
||||||
|
|
||||||
public (int type, string dbtype, string dbtypeFull, bool? isnullable)? GetDbInfo(Type type) {
|
public (int type, string dbtype, string dbtypeFull, bool? isnullable)? GetDbInfo(Type type) {
|
||||||
|
var elementType = type.IsArray ? type.GetElementType() : type;
|
||||||
|
var info = GetDbInfoNoneArray(elementType);
|
||||||
|
if (info == null) return null;
|
||||||
|
if (type.IsArray == false) return ((int)info.Value.type, info.Value.dbtype, info.Value.dbtypeFull, info.Value.isnullable);
|
||||||
|
var dbype = $"{info.Value.dbtype}[]";
|
||||||
|
return ((int)(info.Value.type | NpgsqlDbType.Array), dbype, info.Value.dbtypeFull.Replace(info.Value.dbtype, dbype), info.Value.isnullable);
|
||||||
|
}
|
||||||
|
(NpgsqlDbType type, string dbtype, string dbtypeFull, bool? isnullable)? GetDbInfoNoneArray(Type type) {
|
||||||
|
if (_dicCsToDb.TryGetValue(type.FullName, out var trydc)) return new (NpgsqlDbType, string, string, bool?)?((trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable));
|
||||||
var enumType = type.IsEnum ? type : null;
|
var enumType = type.IsEnum ? type : null;
|
||||||
if (enumType == null && type.FullName.StartsWith("System.Nullable`1[") && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments.First().IsEnum) enumType = type.GenericTypeArguments.First();
|
if (enumType == null && type.FullName.StartsWith("System.Nullable`1[") && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments.First().IsEnum) enumType = type.GenericTypeArguments.First();
|
||||||
if (enumType != null) {
|
if (enumType != null) {
|
||||||
return ((int)NpgsqlDbType.Integer, "int4", $"int4{(type.IsEnum ? " NOT NULL" : "")}", type.IsEnum ? false : true);
|
var newItem = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ?
|
||||||
|
(NpgsqlDbType.Varchar, "varchar", $"varchar(32){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true) :
|
||||||
|
(NpgsqlDbType.Varchar, "varchar", $"varchar(32){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true);
|
||||||
|
if (_dicCsToDb.ContainsKey(type.FullName) == false) {
|
||||||
|
lock (_dicCsToDbLock) {
|
||||||
|
if (_dicCsToDb.ContainsKey(type.FullName) == false)
|
||||||
|
_dicCsToDb.Add(type.FullName, newItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (newItem.Item1, newItem.Item2, newItem.Item3, newItem.Item5);
|
||||||
}
|
}
|
||||||
return _dicCsToDb.TryGetValue(type.FullName, out var trydc) ? new (int, string, string, bool?)?(((int)trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable)) : null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetComparisonDDLStatements<TEntity>() => this.GetComparisonDDLStatements(typeof(TEntity));
|
public string GetComparisonDDLStatements<TEntity>() => this.GetComparisonDDLStatements(typeof(TEntity));
|
||||||
public string GetComparisonDDLStatements(params Type[] entityTypes) {
|
public string GetComparisonDDLStatements(params Type[] entityTypes) {
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
|
var seqcols = new List<(ColumnInfo, string[], bool)>(); //序列
|
||||||
foreach (var entityType in entityTypes) {
|
foreach (var entityType in entityTypes) {
|
||||||
if (sb.Length > 0) sb.Append("\r\n");
|
if (sb.Length > 0) sb.Append("\r\n");
|
||||||
var tb = _commonUtils.GetTableByEntity(entityType);
|
var tb = _commonUtils.GetTableByEntity(entityType);
|
||||||
@ -88,22 +126,19 @@ namespace FreeSql.PostgreSQL {
|
|||||||
var isRenameTable = false;
|
var isRenameTable = false;
|
||||||
var tbname = tb.DbName.Split(new[] { '.' }, 2);
|
var tbname = tb.DbName.Split(new[] { '.' }, 2);
|
||||||
if (tbname.Length == 1) tbname = new[] { "public", tbname[0] };
|
if (tbname.Length == 1) tbname = new[] { "public", tbname[0] };
|
||||||
if (_orm.Ado.ExecuteScalar(CommandType.Text, "select 1 from pg_tables a inner join pg_namespace b on b.nspname = a.schemaname where b.nspname || '.' || a.tablename = {0}.{1}".FormatMySql(tbname)) == null) { //表不存在
|
if (_orm.Ado.ExecuteScalar(CommandType.Text, "select 1 from pg_tables a inner join pg_namespace b on b.nspname = a.schemaname where b.nspname || '.' || a.tablename = {0}.{1}".FormatPostgreSQL(tbname)) == null) { //表不存在
|
||||||
|
|
||||||
if (tboldname != null && _orm.Ado.ExecuteScalar(CommandType.Text, "select 1 from pg_tables a inner join pg_namespace b on b.nspname = a.schemaname where b.nspname || '.' || a.tablename = {0}.{1}".FormatMySql(tboldname)) != null) { //旧表存在
|
if (tboldname != null && _orm.Ado.ExecuteScalar(CommandType.Text, "select 1 from pg_tables a inner join pg_namespace b on b.nspname = a.schemaname where b.nspname || '.' || a.tablename = {0}.{1}".FormatPostgreSQL(tboldname)) != null) { //旧表存在
|
||||||
//修改表名
|
//修改表名
|
||||||
sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tboldname[0]}.{tboldname[1]}")).Append(" RENAME TO ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(";\r\n");
|
sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tboldname[0]}.{tboldname[1]}")).Append(" RENAME TO ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(";\r\n");
|
||||||
isRenameTable = true;
|
isRenameTable = true;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
//创建表
|
//创建表
|
||||||
var seqcols = new List<ColumnInfo>();
|
|
||||||
sb.Append("CREATE TABLE IF NOT EXISTS ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" (");
|
sb.Append("CREATE TABLE IF NOT EXISTS ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" (");
|
||||||
foreach (var tbcol in tb.Columns.Values) {
|
foreach (var tbcol in tb.Columns.Values) {
|
||||||
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
|
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType.ToUpper()).Append(",");
|
||||||
sb.Append(tbcol.Attribute.DbType.ToUpper());
|
if (tbcol.Attribute.IsIdentity) seqcols.Add((tbcol, tbname, true));
|
||||||
if (tbcol.Attribute.IsIdentity && tbcol.Attribute.DbType.IndexOf("serial", StringComparison.CurrentCultureIgnoreCase) == -1) seqcols.Add(tbcol);
|
|
||||||
sb.Append(",");
|
|
||||||
}
|
}
|
||||||
if (tb.Primarys.Any() == false)
|
if (tb.Primarys.Any() == false)
|
||||||
sb.Remove(sb.Length - 1, 1);
|
sb.Remove(sb.Length - 1, 1);
|
||||||
@ -126,8 +161,9 @@ a.attname,
|
|||||||
t.typname,
|
t.typname,
|
||||||
case when a.atttypmod > 0 and a.atttypmod < 32767 then a.atttypmod - 4 else a.attlen end len,
|
case when a.atttypmod > 0 and a.atttypmod < 32767 then a.atttypmod - 4 else a.attlen end len,
|
||||||
case when t.typelem = 0 then t.typname else t2.typname end,
|
case when t.typelem = 0 then t.typname else t2.typname end,
|
||||||
case when a.attnotnull then 0 else 1 end as is_nullable,
|
case when a.attnotnull then '0' else '1' end as is_nullable,
|
||||||
e.adsrc as is_identity
|
case when e.adsrc = 1 then '1' else '0' end as is_identity,
|
||||||
|
a.attndims
|
||||||
from pg_class c
|
from pg_class c
|
||||||
inner join pg_attribute a on a.attnum > 0 and a.attrelid = c.oid
|
inner join pg_attribute a on a.attnum > 0 and a.attrelid = c.oid
|
||||||
inner join pg_type t on t.oid = a.atttypid
|
inner join pg_type t on t.oid = a.atttypid
|
||||||
@ -136,7 +172,7 @@ left join pg_description d on d.objoid = a.attrelid and d.objsubid = a.attnum
|
|||||||
left join pg_attrdef e on e.adrelid = a.attrelid and e.adnum = a.attnum
|
left join pg_attrdef e on e.adrelid = a.attrelid and e.adnum = a.attnum
|
||||||
inner join pg_namespace ns on ns.oid = c.relnamespace
|
inner join pg_namespace ns on ns.oid = c.relnamespace
|
||||||
inner join pg_namespace ns2 on ns2.oid = t.typnamespace
|
inner join pg_namespace ns2 on ns2.oid = t.typnamespace
|
||||||
where ns.nspname = {0} and c.relname = {1}".FormatMySql(isRenameTable ? tboldname : tbname);
|
where ns.nspname = {0} and c.relname = {1}".FormatPostgreSQL(isRenameTable ? tboldname : tbname);
|
||||||
var ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
|
var ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
|
||||||
foreach (var row in ds) {
|
foreach (var row in ds) {
|
||||||
string column = string.Concat(row[0]);
|
string column = string.Concat(row[0]);
|
||||||
@ -144,41 +180,59 @@ where ns.nspname = {0} and c.relname = {1}".FormatMySql(isRenameTable ? tboldnam
|
|||||||
long max_length = long.Parse(string.Concat(row[2]));
|
long max_length = long.Parse(string.Concat(row[2]));
|
||||||
bool is_nullable = string.Concat(row[4]) == "1";
|
bool is_nullable = string.Concat(row[4]) == "1";
|
||||||
bool is_identity = string.Concat(row[5]).StartsWith(@"nextval('") && string.Concat(row[6]).EndsWith(@"_seq'::regclass)");
|
bool is_identity = string.Concat(row[5]).StartsWith(@"nextval('") && string.Concat(row[6]).EndsWith(@"_seq'::regclass)");
|
||||||
|
var attndims = long.Parse(string.Concat(row[6]));
|
||||||
|
if (attndims > 0) sqlType += "[]";
|
||||||
|
|
||||||
if (addcols.TryGetValue(column, out var trycol)) {
|
if (addcols.TryGetValue(column, out var trycol)) {
|
||||||
if (trycol.Attribute.DbType.ToLower().StartsWith(sqlType.ToLower()) == false ||
|
if (trycol.Attribute.DbType.ToLower().StartsWith(sqlType.ToLower()) == false ||
|
||||||
(trycol.Attribute.DbType.IndexOf("NOT NULL") == -1) != is_nullable ||
|
(trycol.Attribute.DbType.IndexOf("NOT NULL") == -1) != is_nullable) {
|
||||||
trycol.Attribute.IsIdentity != is_identity) {
|
sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ALTER COLUMN ").Append(_commonUtils.QuoteSqlName(column)).Append(" TYPE ").Append(trycol.Attribute.DbType.ToUpper()).Append(";\r\n");
|
||||||
sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ALTER COLUMN ").Append(_commonUtils.QuoteSqlName(column)).Append(" TYPE ").Append(trycol.Attribute.DbType.ToUpper());
|
|
||||||
if (trycol.Attribute.IsIdentity) sb.Append(" AUTO_INCREMENT");
|
|
||||||
sb.Append(";\r\n");
|
|
||||||
}
|
}
|
||||||
|
if (trycol.Attribute.IsIdentity != is_identity) seqcols.Add((trycol, tbname, trycol.Attribute.IsIdentity));
|
||||||
addcols.Remove(column);
|
addcols.Remove(column);
|
||||||
} else
|
} else {
|
||||||
|
if (trycol.Attribute.IsIdentity != is_identity) seqcols.Add((trycol, tbname, trycol.Attribute.IsIdentity));
|
||||||
surplus.Add(column, true); //记录剩余字段
|
surplus.Add(column, true); //记录剩余字段
|
||||||
|
}
|
||||||
}
|
}
|
||||||
foreach (var addcol in addcols.Values) {
|
foreach (var addcol in addcols.Values) {
|
||||||
if (string.IsNullOrEmpty(addcol.Attribute.OldName) == false && surplus.ContainsKey(addcol.Attribute.OldName)) { //修改列名
|
if (string.IsNullOrEmpty(addcol.Attribute.OldName) == false && surplus.ContainsKey(addcol.Attribute.OldName)) { //修改列名
|
||||||
sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" RENAME COLUMN ").Append(_commonUtils.QuoteSqlName(addcol.Attribute.OldName)).Append(" TO ").Append(_commonUtils.QuoteSqlName(addcol.Attribute.Name)).Append(";\r\n");
|
sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" RENAME COLUMN ").Append(_commonUtils.QuoteSqlName(addcol.Attribute.OldName)).Append(" TO ").Append(_commonUtils.QuoteSqlName(addcol.Attribute.Name)).Append(";\r\n");
|
||||||
if (addcol.Attribute.IsIdentity) sb.Append(" AUTO_INCREMENT");
|
|
||||||
sb.Append(";\r\n");
|
|
||||||
|
|
||||||
} else { //添加列
|
} else { //添加列
|
||||||
sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ADD COLUMN ").Append(_commonUtils.QuoteSqlName(addcol.Attribute.Name)).Append(" ").Append(addcol.Attribute.DbType.ToUpper());
|
sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ADD COLUMN ").Append(_commonUtils.QuoteSqlName(addcol.Attribute.Name)).Append(" ").Append(addcol.Attribute.DbType.ToUpper()).Append(";\r\n");
|
||||||
if (addcol.Attribute.IsIdentity) sb.Append(" AUTO_INCREMENT");
|
|
||||||
sb.Append(";\r\n");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
foreach(var seqcol in seqcols) {
|
||||||
|
var tbname = seqcol.Item2;
|
||||||
|
var seqname = Utils.GetCsName($"{tbname[0]}.{tbname[1]}_{seqcol.Item1.Attribute.Name}_sequence_name");
|
||||||
|
var tbname2 = _commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}");
|
||||||
|
var colname2 = _commonUtils.QuoteSqlName(seqcol.Item1.Attribute.Name);
|
||||||
|
sb.Append("ALTER TABLE ").Append(tbname2).Append(" ALTER COLUMN ").Append(colname2).Append(" SET DEFAULT null;");
|
||||||
|
sb.Append("DROP SEQUENCE IF EXISTS ").Append(seqname).Append(";");
|
||||||
|
if (seqcol.Item3) {
|
||||||
|
sb.Append("CREATE SEQUENCE ").Append(seqname).Append(" START WITH (select coalesce(max(").Append(colname2).Append("),1) from ").Append(tbname2).Append(");");
|
||||||
|
sb.Append("ALTER TABLE ").Append(tbname2).Append(" ALTER COLUMN ").Append(colname2).Append(" SET DEFAULT nextval('").Append(seqname).Append("'::regclass);");
|
||||||
|
}
|
||||||
|
}
|
||||||
return sb.Length == 0 ? null : sb.ToString();
|
return sb.Length == 0 ? null : sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Dictionary<string, bool> dicSyced = new Dictionary<string, bool>();
|
||||||
public bool SyncStructure<TEntity>() => this.SyncStructure(typeof(TEntity));
|
public bool SyncStructure<TEntity>() => this.SyncStructure(typeof(TEntity));
|
||||||
public bool SyncStructure(params Type[] entityTypes) {
|
public bool SyncStructure(params Type[] entityTypes) {
|
||||||
var ddl = this.GetComparisonDDLStatements(entityTypes);
|
if (entityTypes == null) return true;
|
||||||
if (string.IsNullOrEmpty(ddl)) return true;
|
var syncTypes = entityTypes.Where(a => dicSyced.ContainsKey(a.FullName) == false).ToArray();
|
||||||
|
if (syncTypes.Any() == false) return true;
|
||||||
|
var ddl = this.GetComparisonDDLStatements(syncTypes);
|
||||||
|
if (string.IsNullOrEmpty(ddl)) {
|
||||||
|
foreach (var syncType in syncTypes) dicSyced.Add(syncType.FullName, true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
return _orm.Ado.ExecuteNonQuery(CommandType.Text, ddl) > 0;
|
var affrows = _orm.Ado.ExecuteNonQuery(CommandType.Text, ddl);
|
||||||
|
foreach (var syncType in syncTypes) dicSyced.Add(syncType.FullName, true);
|
||||||
|
return affrows > 0;
|
||||||
} catch {
|
} catch {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -357,10 +357,40 @@ where a.constraint_schema in ({1}) and a.table_name in ({0}) and not isnull(posi
|
|||||||
if (ret == 0) ret = t1.Name.CompareTo(t2.Name);
|
if (ret == 0) ret = t1.Name.CompareTo(t2.Name);
|
||||||
return ret;
|
return ret;
|
||||||
});
|
});
|
||||||
|
foreach (var loc4 in loc1) {
|
||||||
|
var dicUniques = new Dictionary<string, List<DbColumnInfo>>();
|
||||||
|
if (loc4.Primarys.Count > 0) dicUniques.Add(string.Join(",", loc4.Primarys.Select(a => a.Name)), loc4.Primarys);
|
||||||
|
foreach (var loc5 in loc4.Uniques) {
|
||||||
|
var dickey = string.Join(",", loc5.Select(a => a.Name));
|
||||||
|
if (dicUniques.ContainsKey(dickey)) continue;
|
||||||
|
dicUniques.Add(dickey, loc5);
|
||||||
|
}
|
||||||
|
loc4.Uniques = dicUniques.Values.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
loc2.Clear();
|
loc2.Clear();
|
||||||
loc3.Clear();
|
loc3.Clear();
|
||||||
return loc1;
|
return loc1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<DbEnumInfo> GetEnumsByDatabase(params string[] database) {
|
||||||
|
if (database == null || database.Length == 0) return new List<DbEnumInfo>();
|
||||||
|
var drs = _orm.Ado.Query<(string name, string label)>(CommandType.Text, @"select
|
||||||
|
ns.nspname || '.' || a.typname,
|
||||||
|
b.enumlabel
|
||||||
|
from pg_type a
|
||||||
|
inner join pg_enum b on b.enumtypid = a.oid
|
||||||
|
inner join pg_namespace ns on ns.oid = a.typnamespace
|
||||||
|
where a.typtype = 'e' and ns.nspname in (SELECT ""schema_name"" FROM information_schema.schemata where catalog_name in {0})".FormatPostgreSQL(database));
|
||||||
|
var ret = new Dictionary<string, Dictionary<string, string>>();
|
||||||
|
foreach (var dr in drs) {
|
||||||
|
if (ret.TryGetValue(dr.name, out var labels) == false) ret.Add(dr.name, labels = new Dictionary<string, string>());
|
||||||
|
var key = dr.label;
|
||||||
|
if (Regex.IsMatch(key, @"^[\u0391-\uFFE5a-zA-Z_\$][\u0391-\uFFE5a-zA-Z_\$\d]*$") == false)
|
||||||
|
key = $"Unkown{ret[dr.name].Count + 1}";
|
||||||
|
if (labels.ContainsKey(key) == false) labels.Add(key, dr.label);
|
||||||
|
}
|
||||||
|
return ret.Select(a => new DbEnumInfo { Name = a.Key, Labels = a.Value }).ToList();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,6 +2,7 @@
|
|||||||
using FreeSql.Internal.Model;
|
using FreeSql.Internal.Model;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
namespace FreeSql.PostgreSQL {
|
namespace FreeSql.PostgreSQL {
|
||||||
@ -9,151 +10,176 @@ namespace FreeSql.PostgreSQL {
|
|||||||
|
|
||||||
public PostgreSQLExpression(CommonUtils common) : base(common) { }
|
public PostgreSQLExpression(CommonUtils common) : base(common) { }
|
||||||
|
|
||||||
internal override string ExpressionLambdaToSqlCall(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
internal override string ExpressionLambdaToSqlMemberAccessString(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
if (exp.Object.Type.FullName == "System.String") {
|
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName);
|
switch (exp.Member.Name) {
|
||||||
switch (exp.Method.Name) {
|
case "Length": return $"char_length({left})";
|
||||||
case "StartsWith":
|
|
||||||
case "EndsWith":
|
|
||||||
case "Contains":
|
|
||||||
var args0Value = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
|
|
||||||
if (args0Value == "NULL") return $"({left}) IS NULL";
|
|
||||||
if (exp.Method.Name == "StartsWith") return $"({left}) LIKE {(args0Value.StartsWith("'") ? args0Value.Insert(1, "%") : $"concat('%', {args0Value})")}";
|
|
||||||
if (exp.Method.Name == "EndsWith") return $"({left}) LIKE {(args0Value.EndsWith("'") ? args0Value.Insert(args0Value.Length - 1, "%") : $"concat({args0Value}, '%')")}";
|
|
||||||
if (args0Value.StartsWith("'") && args0Value.EndsWith("'")) return $"({left}) LIKE {args0Value.Insert(1, "%").Insert(args0Value.Length, "%")}";
|
|
||||||
return $"({left}) like concat('%', {args0Value}, '%')";
|
|
||||||
case "ToLower": return $"lower({left})";
|
|
||||||
case "ToUpper": return $"upper({left})";
|
|
||||||
case "Substring": return $"substr({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} + 1, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Length": return $"char_length({left})";
|
|
||||||
case "IndexOf":
|
|
||||||
var indexOfFindStr = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
|
|
||||||
if (exp.Arguments.Count > 1 && exp.Arguments[1].Type.FullName == "System.Int32") return $"(locate({left}, {indexOfFindStr}, ParseLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName) + 1) - 1)";
|
|
||||||
return $"(locate({left}, {indexOfFindStr}) - 1)";
|
|
||||||
case "PadLeft": return $"lpad({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "PadRight": return $"rpad({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Trim":
|
|
||||||
case "TrimStart":
|
|
||||||
case "TrimEnd":
|
|
||||||
if (exp.Arguments.Count == 0) {
|
|
||||||
if (exp.Method.Name == "Trim") return $"trim({left})";
|
|
||||||
if (exp.Method.Name == "TrimStart") return $"ltrim({left})";
|
|
||||||
if (exp.Method.Name == "TrimStart") return $"rtrim({left})";
|
|
||||||
}
|
|
||||||
foreach (var argsTrim01 in exp.Arguments) {
|
|
||||||
if (exp.Method.Name == "Trim") left = $"trim({ExpressionLambdaToSql(argsTrim01, _tables, _selectColumnMap, tbtype, isQuoteName)} from {left})";
|
|
||||||
if (exp.Method.Name == "TrimStart") left = $"trim(leading {ExpressionLambdaToSql(argsTrim01, _tables, _selectColumnMap, tbtype, isQuoteName)} from {left})";
|
|
||||||
if (exp.Method.Name == "TrimStart") left = $"trim(trailing {ExpressionLambdaToSql(argsTrim01, _tables, _selectColumnMap, tbtype, isQuoteName)} from {left})";
|
|
||||||
}
|
|
||||||
return left;
|
|
||||||
case "Replace": return $"replace({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "CompareTo": return $"strcmp({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
throw new Exception($"PostgreSQLExpression 未现实函数表达式 {exp} 解析");
|
||||||
|
}
|
||||||
|
|
||||||
if (exp.Object.Type.FullName == "System.Math") {
|
internal override string ExpressionLambdaToSqlMemberAccessMath(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
switch (exp.Method.Name) {
|
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
case "Abs": return $"abs({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
switch (exp.Member.Name) {
|
||||||
case "Sign": return $"sign({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
case "PI": return $"pi()";
|
||||||
case "Floor": return $"floor({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Round":
|
|
||||||
if (exp.Arguments.Count > 1 && exp.Arguments[1].Type.FullName == "System.Int32") return $"round({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
return $"round({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Exp": return $"exp({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Log": return $"log({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Log10": return $"log10({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Pow": return $"pow({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Sqrt": return $"sqrt({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "PI": return $"pi()";
|
|
||||||
case "Cos": return $"cos({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Sin": return $"sin({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Tan": return $"tan({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Acos": return $"acos({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Asin": return $"asin({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Atan": return $"atan({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Atan2": return $"atan2({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Truncate": return $"truncate({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, 0)";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
throw new Exception($"PostgreSQLExpression 未现实函数表达式 {exp} 解析");
|
||||||
|
}
|
||||||
|
|
||||||
//dayofweek = DayOfWeek
|
internal override string ExpressionLambdaToSqlMemberAccessDateTime(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
//dayofmonth = Day
|
if (exp.Expression == null && exp.Member.Name == "Now") return "current_timestamp";
|
||||||
//dayofyear = DayOfYear
|
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
//month = Month
|
switch (exp.Member.Name) {
|
||||||
//year = Year
|
case "DayOfWeek": return $"extract(dow from ({left})::timestamp)";
|
||||||
//hour = Hour
|
case "Day": return $"extract(day from ({left})::timestamp)";
|
||||||
//minute = Minute
|
case "DayOfYear": return $"extract(doy from ({left})::timestamp)";
|
||||||
//second = Second
|
case "Month": return $"extract(month from ({left})::timestamp)";
|
||||||
/*
|
case "Year": return $"extract(year from ({left})::timestamp)";
|
||||||
* date_add(date,interval expr type)
|
case "Hour": return $"extract(hour from ({left})::timestamp)";
|
||||||
date_sub(date,interval expr type)
|
case "Minute": return $"extract(minute from ({left})::timestamp)";
|
||||||
adddate(date,interval expr type)
|
case "Second": return $"extract(second from ({left})::timestamp)";
|
||||||
subdate(date,interval expr type)
|
case "Millisecond": return $"(extract(milliseconds from ({left})::timestamp) - extract(second from ({left})::timestamp) * 1000)";
|
||||||
对日期时间进行加减法运算
|
case "Ticks": return $"(extract(epoch from ({left})::timestamp) * 10000000 + 621355968000000000)";
|
||||||
(adddate()和subdate()是date_add()和date_sub()的同义词,也
|
}
|
||||||
可以用运算符+和-而不是函数
|
throw new Exception($"PostgreSQLExpression 未现实函数表达式 {exp} 解析");
|
||||||
date是一个datetime或date值,expr对date进行加减法的一个表
|
}
|
||||||
达式字符串type指明表达式expr应该如何被解释
|
|
||||||
[type值 含义 期望的expr格式]:
|
|
||||||
second 秒 seconds
|
|
||||||
minute 分钟 minutes
|
|
||||||
hour 时间 hours
|
|
||||||
day 天 days
|
|
||||||
month 月 months
|
|
||||||
year 年 years
|
|
||||||
minute_second 分钟和秒 "minutes:seconds"
|
|
||||||
hour_minute 小时和分钟 "hours:minutes"
|
|
||||||
day_hour 天和小时 "days hours"
|
|
||||||
year_month 年和月 "years-months"
|
|
||||||
hour_second 小时, 分钟, "hours:minutes:seconds"
|
|
||||||
day_minute 天, 小时, 分钟 "days hours:minutes"
|
|
||||||
day_second 天, 小时, 分钟, 秒 "days
|
|
||||||
hours:minutes:seconds"
|
|
||||||
expr中允许任何标点做分隔符,如果所有是date值时结果是一个
|
|
||||||
date值,否则结果是一个datetime值)
|
|
||||||
如果type关键词不完整,则mysql从右端取值,day_second因为缺
|
|
||||||
少小时分钟等于minute_second)
|
|
||||||
如果增加month、year_month或year,天数大于结果月份的最大天
|
|
||||||
数则使用最大天数)
|
|
||||||
mysql> select "1997-12-31 23:59:59" + interval 1 second;
|
|
||||||
|
|
||||||
-> 1998-01-01 00:00:00
|
internal override string ExpressionLambdaToSqlMemberAccessTimeSpan(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
mysql> select interval 1 day + "1997-12-31";
|
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
-> 1998-01-01
|
switch (exp.Member.Name) {
|
||||||
mysql> select "1998-01-01" - interval 1 second;
|
case "Days": return $"floor(extract(epoch from ({left})::interval) / {60 * 60 * 24})";
|
||||||
-> 1997-12-31 23:59:59
|
case "Hours": return $"extract(hour from ({left})::interval)";
|
||||||
mysql> select date_add("1997-12-31 23:59:59",interval 1
|
case "Milliseconds": return $"(extract(milliseconds from ({left})::interval) - extract(second from ({left})::interval) * 1000)";
|
||||||
second);
|
case "Minutes": return $"extract(minute from ({left})::interval)";
|
||||||
-> 1998-01-01 00:00:00
|
case "Seconds": return $"extract(second from ({left})::interval)";
|
||||||
mysql> select date_add("1997-12-31 23:59:59",interval 1
|
case "Ticks": return $"(extract(epoch from ({left})::interval) * 10000000)";
|
||||||
day);
|
case "TotalDays": return $"floor(extract(epoch from ({left})::interval) / {60 * 60 * 24})";
|
||||||
-> 1998-01-01 23:59:59
|
case "TotalHours": return $"floor(extract(epoch from ({left})::interval) / {60 * 60})";
|
||||||
mysql> select date_add("1997-12-31 23:59:59",interval
|
case "TotalMilliseconds": return $"(epoch from ({left})::interval + extract(milliseconds from ({left})::interval) - extract(second from ({left})::interval) * 1000)";
|
||||||
"1:1" minute_second);
|
case "TotalMinutes": return $"floor(extract(epoch from ({left})::interval) / 60)";
|
||||||
-> 1998-01-01 00:01:00
|
case "TotalSeconds": return $"extract(epoch from ({left})::interval)";
|
||||||
mysql> select date_sub("1998-01-01 00:00:00",interval "1
|
}
|
||||||
1:1:1" day_second);
|
throw new Exception($"PostgreSQLExpression 未现实函数表达式 {exp} 解析");
|
||||||
-> 1997-12-30 22:58:59
|
}
|
||||||
mysql> select date_add("1998-01-01 00:00:00", interval "-1
|
internal override string ExpressionLambdaToSqlCallString(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
10" day_hour);
|
var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
-> 1997-12-30 14:00:00
|
switch (exp.Method.Name) {
|
||||||
mysql> select date_sub("1998-01-02", interval 31 day);
|
case "StartsWith":
|
||||||
-> 1997-12-02
|
case "EndsWith":
|
||||||
mysql> select extract(year from "1999-07-02");
|
case "Contains":
|
||||||
-> 1999
|
var args0Value = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
mysql> select extract(year_month from "1999-07-02
|
if (args0Value == "NULL") return $"({left}) IS NULL";
|
||||||
01:02:03");
|
var likeOpt = "LIKE";
|
||||||
-> 199907
|
if (exp.Arguments.Count > 1) {
|
||||||
mysql> select extract(day_minute from "1999-07-02
|
if (exp.Arguments[1].Type == typeof(bool) ||
|
||||||
01:02:03");
|
exp.Arguments[1].Type == typeof(StringComparison) && ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName).Contains("IgnoreCase")) likeOpt = "ILIKE";
|
||||||
-> 20102
|
}
|
||||||
*/
|
if (exp.Method.Name == "StartsWith") return $"({left}) {likeOpt} {(args0Value.StartsWith("'") ? args0Value.Insert(1, "%") : $"('%' || ({args0Value})::varchar)")}";
|
||||||
|
if (exp.Method.Name == "EndsWith") return $"({left}) {likeOpt} {(args0Value.EndsWith("'") ? args0Value.Insert(args0Value.Length - 1, "%") : $"(({args0Value})::varchar || '%')")}";
|
||||||
|
if (args0Value.StartsWith("'") && args0Value.EndsWith("'")) return $"({left}) {likeOpt} {args0Value.Insert(1, "%").Insert(args0Value.Length, "%")}";
|
||||||
|
return $"({left}) {likeOpt} ('%' || ({args0Value})::varchar || '%')";
|
||||||
|
case "ToLower": return $"lower({left})";
|
||||||
|
case "ToUpper": return $"upper({left})";
|
||||||
|
case "Substring":
|
||||||
|
var substrArgs1 = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
if (long.TryParse(substrArgs1, out var testtrylng1)) substrArgs1 = (testtrylng1 + 1).ToString();
|
||||||
|
else substrArgs1 += " + 1";
|
||||||
|
if (exp.Arguments.Count == 1) return $"substr({left}, {substrArgs1})";
|
||||||
|
return $"substr({left}, {substrArgs1}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "IndexOf": return $"(strpos({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}) - 1)";
|
||||||
|
case "PadLeft":
|
||||||
|
if (exp.Arguments.Count == 1) return $"lpad({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
return $"lpad({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "PadRight":
|
||||||
|
if (exp.Arguments.Count == 1) return $"rpad({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
return $"rpad({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Trim":
|
||||||
|
case "TrimStart":
|
||||||
|
case "TrimEnd":
|
||||||
|
if (exp.Arguments.Count == 0) {
|
||||||
|
if (exp.Method.Name == "Trim") return $"trim({left})";
|
||||||
|
if (exp.Method.Name == "TrimStart") return $"ltrim({left})";
|
||||||
|
if (exp.Method.Name == "TrimEnd") return $"rtrim({left})";
|
||||||
|
}
|
||||||
|
var trimArg = "";
|
||||||
|
foreach (var argsTrim02 in exp.Arguments) {
|
||||||
|
var argsTrim01s = new[] { argsTrim02 };
|
||||||
|
if (argsTrim02.NodeType == ExpressionType.NewArrayInit) {
|
||||||
|
var arritem = argsTrim02 as NewArrayExpression;
|
||||||
|
argsTrim01s = arritem.Expressions.ToArray();
|
||||||
|
}
|
||||||
|
foreach (var argsTrim01 in argsTrim01s) {
|
||||||
|
var trimChr = ExpressionLambdaToSql(argsTrim01, _tables, _selectColumnMap, tbtype, isQuoteName).Trim('"');
|
||||||
|
if (trimChr.Length == 1) trimArg += trimChr;
|
||||||
|
else trimArg += $" || ({trimArg})";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (exp.Method.Name == "Trim") left = $"trim({left}, {trimArg})";
|
||||||
|
if (exp.Method.Name == "TrimStart") left = $"ltrim({left}, {trimArg})";
|
||||||
|
if (exp.Method.Name == "TrimEnd") left = $"rtrim({left}, {trimArg})";
|
||||||
|
return left;
|
||||||
|
case "Replace": return $"replace({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "CompareTo": return $"strcmp({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
}
|
||||||
|
throw new Exception($"PostgreSQLExpression 未现实函数表达式 {exp} 解析");
|
||||||
|
}
|
||||||
|
|
||||||
//convert
|
internal override string ExpressionLambdaToSqlCallMath(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
var xxx = DateTime.Now.ToString("");
|
switch (exp.Method.Name) {
|
||||||
|
case "Abs": return $"abs({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Sign": return $"sign({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Floor": return $"floor({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Ceiling": return $"ceiling({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Round":
|
||||||
|
if (exp.Arguments.Count > 1 && exp.Arguments[1].Type.FullName == "System.Int32") return $"round({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
return $"round({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Exp": return $"exp({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Log": return $"log({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Log10": return $"log10({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Pow": return $"pow({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Sqrt": return $"sqrt({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Cos": return $"cos({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Sin": return $"sin({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Tan": return $"tan({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Acos": return $"acos({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Asin": return $"asin({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Atan": return $"atan({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Atan2": return $"atan2({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Truncate": return $"trunc({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, 0)";
|
||||||
|
}
|
||||||
|
throw new Exception($"PostgreSQLExpression 未现实函数表达式 {exp} 解析");
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override string ExpressionLambdaToSqlCallDateTime(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
throw new Exception($"MySqlExpression 未现实函数表达式 {exp} 解析");
|
var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
var args1 = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
switch (exp.Method.Name) {
|
||||||
|
case "Add": return $"(({left})::timestamp + ({args1})::interval)";
|
||||||
|
case "AddDays": return $"(({left})::timestamp + (({args1}) || ' day')::interval)";
|
||||||
|
case "AddHours": return $"(({left})::timestamp + (({args1}) || ' hour')::interval)";
|
||||||
|
case "AddMilliseconds": return $"(({left})::timestamp + (({args1}) || ' milliseconds')::interval)";
|
||||||
|
case "AddMinutes": return $"(({left})::timestamp + (({args1}) || ' minute')::interval)";
|
||||||
|
case "AddMonths": return $"(({left})::timestamp + (({args1}) || ' month')::interval)";
|
||||||
|
case "AddSeconds": return $"(({left})::timestamp + (({args1}) || ' second')::interval)";
|
||||||
|
case "AddTicks": return $"(({left})::timestamp + (({args1}) / 10 || ' microseconds')::interval)";
|
||||||
|
case "AddYears": return $"(({left})::timestamp + (({args1}) || ' year')::interval)";
|
||||||
|
case "Subtract":
|
||||||
|
if (exp.Arguments[0].Type.FullName == "System.DateTime" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.DateTime")
|
||||||
|
return $"(({left})::timestamp - ({args1})::timestamp)";
|
||||||
|
if (exp.Arguments[0].Type.FullName == "System.TimeSpan" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.TimeSpan")
|
||||||
|
return $"(({left})::timestamp - ({args1})::interval)";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
throw new Exception($"PostgreSQLExpression 未现实函数表达式 {exp} 解析");
|
||||||
|
}
|
||||||
|
internal override string ExpressionLambdaToSqlCallTimeSpan(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
|
var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
var args1 = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
switch (exp.Method.Name) {
|
||||||
|
case "Add": return $"(({left})::interval + ({args1})::interval)";
|
||||||
|
case "Subtract": return $"(({left})::interval - ({args1})::interval)";
|
||||||
|
}
|
||||||
|
throw new Exception($"PostgreSQLExpression 未现实函数表达式 {exp} 解析");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
using FreeSql.Internal;
|
using FreeSql.Internal;
|
||||||
using MySql.Data.MySqlClient;
|
using Npgsql;
|
||||||
|
using NpgsqlTypes;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data.Common;
|
using System.Data.Common;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace FreeSql.PostgreSQL {
|
namespace FreeSql.PostgreSQL {
|
||||||
|
|
||||||
@ -14,35 +16,44 @@ namespace FreeSql.PostgreSQL {
|
|||||||
|
|
||||||
internal override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, object value) {
|
internal override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, object value) {
|
||||||
if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}";
|
if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}";
|
||||||
MySqlParameter ret = null;
|
NpgsqlParameter ret = null;
|
||||||
if (value == null) ret = new MySqlParameter { ParameterName = $"{parameterName}", Value = DBNull.Value };
|
if (value == null) ret = new NpgsqlParameter { ParameterName = $"{parameterName}", Value = DBNull.Value };
|
||||||
else {
|
else {
|
||||||
var type = value.GetType();
|
var type = value.GetType();
|
||||||
ret = new MySqlParameter {
|
ret = new NpgsqlParameter {
|
||||||
ParameterName = parameterName,
|
ParameterName = parameterName,
|
||||||
Value = value
|
Value = value
|
||||||
};
|
};
|
||||||
var tp = _orm.CodeFirst.GetDbInfo(type)?.type;
|
if (value.GetType().IsEnum || value.GetType().GenericTypeArguments.FirstOrDefault()?.IsEnum == true) {
|
||||||
if (tp != null) ret.MySqlDbType = (MySqlDbType)tp.Value;
|
ret.DataTypeName = "";
|
||||||
|
} else {
|
||||||
|
var tp = _orm.CodeFirst.GetDbInfo(type)?.type;
|
||||||
|
if (tp != null) ret.NpgsqlDbType = (NpgsqlDbType)tp.Value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_params?.Add(ret);
|
_params?.Add(ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal override DbParameter[] GetDbParamtersByObject(string sql, object obj) =>
|
internal override DbParameter[] GetDbParamtersByObject(string sql, object obj) =>
|
||||||
Utils.GetDbParamtersByObject<MySqlParameter>(sql, obj, "?", (name, type, value) => {
|
Utils.GetDbParamtersByObject<NpgsqlParameter>(sql, obj, "@", (name, type, value) => {
|
||||||
var cp = new MySqlParameter {
|
var ret = new NpgsqlParameter {
|
||||||
ParameterName = name,
|
ParameterName = name,
|
||||||
Value = value ?? DBNull.Value
|
Value = value ?? DBNull.Value
|
||||||
};
|
};
|
||||||
var tp = _orm.CodeFirst.GetDbInfo(type)?.type;
|
if (value.GetType().IsEnum || value.GetType().GenericTypeArguments.FirstOrDefault()?.IsEnum == true) {
|
||||||
if (tp != null) cp.MySqlDbType = (MySqlDbType)tp.Value;
|
ret.DataTypeName = "";
|
||||||
return cp;
|
} else {
|
||||||
|
var tp = _orm.CodeFirst.GetDbInfo(type)?.type;
|
||||||
|
if (tp != null) ret.NpgsqlDbType = (NpgsqlDbType)tp.Value;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
});
|
});
|
||||||
|
|
||||||
internal override string FormatSql(string sql, params object[] args) => sql?.FormatMySql(args);
|
internal override string FormatSql(string sql, params object[] args) => sql?.FormatMySql(args);
|
||||||
internal override string QuoteSqlName(string name) => $"`{name.Trim('`').Replace(".", "`.`")}`";
|
internal override string QuoteSqlName(string name) => $"\"{name.Trim('"').Replace(".", "\".\"")}\"";
|
||||||
internal override string QuoteParamterName(string name) => $"?{name}";
|
internal override string QuoteParamterName(string name) => $"@{name}";
|
||||||
internal override string IsNull(string sql, object value) => $"ifnull({sql}, {value})";
|
internal override string IsNull(string sql, object value) => $"coalesce({sql}, {value})";
|
||||||
|
internal override string StringConcat(string left, string right, Type leftType, Type rightType) => $"{left} || {right}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
using FreeSql.Internal;
|
using FreeSql.Internal;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace FreeSql.SqlServer.Curd {
|
namespace FreeSql.SqlServer.Curd {
|
||||||
@ -28,7 +29,7 @@ namespace FreeSql.SqlServer.Curd {
|
|||||||
sb.Insert(0, sql.Substring(0, validx));
|
sb.Insert(0, sql.Substring(0, validx));
|
||||||
sb.Append(sql.Substring(validx));
|
sb.Append(sql.Substring(validx));
|
||||||
|
|
||||||
return _orm.Ado.Query<T1>(sb.ToString());
|
return _orm.Ado.Query<T1>(CommandType.Text, sb.ToString(), _params.ToArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,10 +28,10 @@ namespace FreeSql.SqlServer.Curd {
|
|||||||
|
|
||||||
var validx = sql.IndexOf(") VALUES");
|
var validx = sql.IndexOf(") VALUES");
|
||||||
if (validx == -1) throw new ArgumentException("找不到 VALUES");
|
if (validx == -1) throw new ArgumentException("找不到 VALUES");
|
||||||
sb.Insert(0, sql.Substring(0, validx)).Insert(0, ")");
|
sb.Insert(0, sql.Substring(0, validx + 1));
|
||||||
sb.Append(sql.Substring(validx + 1));
|
sb.Append(sql.Substring(validx + 1));
|
||||||
|
|
||||||
return _orm.Ado.Query<T1>(sb.ToString());
|
return _orm.Ado.Query<T1>(CommandType.Text, sb.ToString(), _params);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,10 @@ namespace FreeSql.SqlServer.Curd {
|
|||||||
|
|
||||||
class SqlServerSelect<T1> : FreeSql.Internal.CommonProvider.Select1Provider<T1> where T1 : class {
|
class SqlServerSelect<T1> : FreeSql.Internal.CommonProvider.Select1Provider<T1> where T1 : class {
|
||||||
|
|
||||||
internal static string ToSqlStatic(CommonUtils _commonUtils, string _select, string field, StringBuilder _join, StringBuilder _where, string _groupby, string _having, string _orderby, int _skip, int _limit, List<SelectTableInfo> _tables) {
|
internal static string ToSqlStatic(CommonUtils _commonUtils, string _select, string field, StringBuilder _join, StringBuilder _where, string _groupby, string _having, string _orderby, int _skip, int _limit, List<SelectTableInfo> _tables, IFreeSql _orm) {
|
||||||
|
if (_orm.CodeFirst.IsAutoSyncStructure)
|
||||||
|
_orm.CodeFirst.SyncStructure(_tables.Select(a => a.Table.Type).ToArray());
|
||||||
|
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
sb.Append(_select);
|
sb.Append(_select);
|
||||||
if (_limit > 0) sb.Append("TOP ").Append(_skip + _limit).Append(" ");
|
if (_limit > 0) sb.Append("TOP ").Append(_skip + _limit).Append(" ");
|
||||||
@ -85,42 +88,42 @@ namespace FreeSql.SqlServer.Curd {
|
|||||||
public override ISelect<T1, T2, T3, T4, T5, T6, T7, T8> From<T2, T3, T4, T5, T6, T7, T8>(Expression<Func<ISelectFromExpression<T1>, T2, T3, T4, T5, T6, T7, T8, ISelectFromExpression<T1>>> exp) { this.InternalFrom(exp?.Body); var ret = new SqlServerSelect<T1, T2, T3, T4, T5, T6, T7, T8>(_orm, _commonUtils, _commonExpression, null); SqlServerSelect<T1>.CopyData(this, ret); return ret; }
|
public override ISelect<T1, T2, T3, T4, T5, T6, T7, T8> From<T2, T3, T4, T5, T6, T7, T8>(Expression<Func<ISelectFromExpression<T1>, T2, T3, T4, T5, T6, T7, T8, ISelectFromExpression<T1>>> exp) { this.InternalFrom(exp?.Body); var ret = new SqlServerSelect<T1, T2, T3, T4, T5, T6, T7, T8>(_orm, _commonUtils, _commonExpression, null); SqlServerSelect<T1>.CopyData(this, ret); return ret; }
|
||||||
public override ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9> From<T2, T3, T4, T5, T6, T7, T8, T9>(Expression<Func<ISelectFromExpression<T1>, T2, T3, T4, T5, T6, T7, T8, T9, ISelectFromExpression<T1>>> exp) { this.InternalFrom(exp?.Body); var ret = new SqlServerSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9>(_orm, _commonUtils, _commonExpression, null); SqlServerSelect<T1>.CopyData(this, ret); return ret; }
|
public override ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9> From<T2, T3, T4, T5, T6, T7, T8, T9>(Expression<Func<ISelectFromExpression<T1>, T2, T3, T4, T5, T6, T7, T8, T9, ISelectFromExpression<T1>>> exp) { this.InternalFrom(exp?.Body); var ret = new SqlServerSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9>(_orm, _commonUtils, _commonExpression, null); SqlServerSelect<T1>.CopyData(this, ret); return ret; }
|
||||||
public override ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> From<T2, T3, T4, T5, T6, T7, T8, T9, T10>(Expression<Func<ISelectFromExpression<T1>, T2, T3, T4, T5, T6, T7, T8, T9, T10, ISelectFromExpression<T1>>> exp) { this.InternalFrom(exp?.Body); var ret = new SqlServerSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(_orm, _commonUtils, _commonExpression, null); SqlServerSelect<T1>.CopyData(this, ret); return ret; }
|
public override ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> From<T2, T3, T4, T5, T6, T7, T8, T9, T10>(Expression<Func<ISelectFromExpression<T1>, T2, T3, T4, T5, T6, T7, T8, T9, T10, ISelectFromExpression<T1>>> exp) { this.InternalFrom(exp?.Body); var ret = new SqlServerSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(_orm, _commonUtils, _commonExpression, null); SqlServerSelect<T1>.CopyData(this, ret); return ret; }
|
||||||
public override string ToSql(string field = null) => ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class SqlServerSelect<T1, T2> : FreeSql.Internal.CommonProvider.Select2Provider<T1, T2> where T1 : class where T2 : class {
|
class SqlServerSelect<T1, T2> : FreeSql.Internal.CommonProvider.Select2Provider<T1, T2> where T1 : class where T2 : class {
|
||||||
public SqlServerSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public SqlServerSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => SqlServerSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => SqlServerSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class SqlServerSelect<T1, T2, T3> : FreeSql.Internal.CommonProvider.Select3Provider<T1, T2, T3> where T1 : class where T2 : class where T3 : class {
|
class SqlServerSelect<T1, T2, T3> : FreeSql.Internal.CommonProvider.Select3Provider<T1, T2, T3> where T1 : class where T2 : class where T3 : class {
|
||||||
public SqlServerSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public SqlServerSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => SqlServerSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => SqlServerSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class SqlServerSelect<T1, T2, T3, T4> : FreeSql.Internal.CommonProvider.Select4Provider<T1, T2, T3, T4> where T1 : class where T2 : class where T3 : class where T4 : class {
|
class SqlServerSelect<T1, T2, T3, T4> : FreeSql.Internal.CommonProvider.Select4Provider<T1, T2, T3, T4> where T1 : class where T2 : class where T3 : class where T4 : class {
|
||||||
public SqlServerSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public SqlServerSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => SqlServerSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => SqlServerSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class SqlServerSelect<T1, T2, T3, T4, T5> : FreeSql.Internal.CommonProvider.Select5Provider<T1, T2, T3, T4, T5> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class {
|
class SqlServerSelect<T1, T2, T3, T4, T5> : FreeSql.Internal.CommonProvider.Select5Provider<T1, T2, T3, T4, T5> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class {
|
||||||
public SqlServerSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public SqlServerSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => SqlServerSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => SqlServerSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class SqlServerSelect<T1, T2, T3, T4, T5, T6> : FreeSql.Internal.CommonProvider.Select6Provider<T1, T2, T3, T4, T5, T6> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class {
|
class SqlServerSelect<T1, T2, T3, T4, T5, T6> : FreeSql.Internal.CommonProvider.Select6Provider<T1, T2, T3, T4, T5, T6> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class {
|
||||||
public SqlServerSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public SqlServerSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => SqlServerSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => SqlServerSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class SqlServerSelect<T1, T2, T3, T4, T5, T6, T7> : FreeSql.Internal.CommonProvider.Select7Provider<T1, T2, T3, T4, T5, T6, T7> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class {
|
class SqlServerSelect<T1, T2, T3, T4, T5, T6, T7> : FreeSql.Internal.CommonProvider.Select7Provider<T1, T2, T3, T4, T5, T6, T7> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class {
|
||||||
public SqlServerSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public SqlServerSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => SqlServerSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => SqlServerSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class SqlServerSelect<T1, T2, T3, T4, T5, T6, T7, T8> : FreeSql.Internal.CommonProvider.Select8Provider<T1, T2, T3, T4, T5, T6, T7, T8> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class {
|
class SqlServerSelect<T1, T2, T3, T4, T5, T6, T7, T8> : FreeSql.Internal.CommonProvider.Select8Provider<T1, T2, T3, T4, T5, T6, T7, T8> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class {
|
||||||
public SqlServerSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public SqlServerSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => SqlServerSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => SqlServerSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class SqlServerSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9> : FreeSql.Internal.CommonProvider.Select9Provider<T1, T2, T3, T4, T5, T6, T7, T8, T9> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class {
|
class SqlServerSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9> : FreeSql.Internal.CommonProvider.Select9Provider<T1, T2, T3, T4, T5, T6, T7, T8, T9> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class {
|
||||||
public SqlServerSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public SqlServerSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => SqlServerSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => SqlServerSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
class SqlServerSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> : FreeSql.Internal.CommonProvider.Select10Provider<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class where T10 : class {
|
class SqlServerSelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> : FreeSql.Internal.CommonProvider.Select10Provider<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class where T10 : class {
|
||||||
public SqlServerSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
public SqlServerSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
|
||||||
public override string ToSql(string field = null) => SqlServerSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables);
|
public override string ToSql(string field = null) => SqlServerSelect<T1>.ToSqlStatic(_commonUtils, _select, field ?? this.GetAllField().field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, _orm);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
using FreeSql.Internal.Model;
|
using FreeSql.Internal.Model;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace FreeSql.SqlServer.Curd {
|
namespace FreeSql.SqlServer.Curd {
|
||||||
@ -30,29 +32,35 @@ namespace FreeSql.SqlServer.Curd {
|
|||||||
sb.Insert(0, sql.Substring(0, validx));
|
sb.Insert(0, sql.Substring(0, validx));
|
||||||
sb.Append(sql.Substring(validx));
|
sb.Append(sql.Substring(validx));
|
||||||
|
|
||||||
return _orm.Ado.Query<T1>(sb.ToString());
|
return _orm.Ado.Query<T1>(CommandType.Text, sb.ToString(), _params.Concat(_paramsSource).ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void ToSqlCase(StringBuilder caseWhen, ColumnInfo[] primarys) {
|
protected override void ToSqlCase(StringBuilder caseWhen, ColumnInfo[] primarys) {
|
||||||
if (_table.Primarys.Length > 1) caseWhen.Append("(");
|
if (_table.Primarys.Length == 1) {
|
||||||
|
caseWhen.Append(_commonUtils.QuoteSqlName(_table.Primarys.First().Attribute.Name));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
caseWhen.Append("(");
|
||||||
var pkidx = 0;
|
var pkidx = 0;
|
||||||
foreach (var pk in _table.Primarys) {
|
foreach (var pk in _table.Primarys) {
|
||||||
if (pkidx > 0) caseWhen.Append(", ");
|
if (pkidx > 0) caseWhen.Append(", ");
|
||||||
caseWhen.Append("cast(").Append(_commonUtils.QuoteSqlName(pk.Attribute.Name)).Append(" as varchar)");
|
caseWhen.Append("cast(").Append(_commonUtils.QuoteSqlName(pk.Attribute.Name)).Append(" as varchar)");
|
||||||
++pkidx;
|
++pkidx;
|
||||||
}
|
}
|
||||||
if (_table.Primarys.Length > 1) caseWhen.Append(")");
|
caseWhen.Append(")");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void ToSqlWhen(StringBuilder sb, ColumnInfo[] primarys, object d) {
|
protected override void ToSqlWhen(StringBuilder sb, ColumnInfo[] primarys, object d) {
|
||||||
if (_table.Primarys.Length > 1) sb.Append("(");
|
if (_table.Primarys.Length == 1) {
|
||||||
|
sb.Append(_commonUtils.FormatSql("{0}", _table.Properties.TryGetValue(_table.Primarys.First().CsName, out var tryp2) ? tryp2.GetValue(d) : null));
|
||||||
|
return;
|
||||||
|
}
|
||||||
var pkidx = 0;
|
var pkidx = 0;
|
||||||
foreach (var pk in _table.Primarys) {
|
foreach (var pk in _table.Primarys) {
|
||||||
if (pkidx > 0) sb.Append(", ");
|
if (pkidx > 0) sb.Append(", ");
|
||||||
sb.Append("cast(").Append(_commonUtils.FormatSql("{0}", _table.Properties.TryGetValue(pk.CsName, out var tryp2) ? tryp2.GetValue(d) : null)).Append(" as varchar)");
|
sb.Append("cast(").Append(_commonUtils.FormatSql("{0}", _table.Properties.TryGetValue(pk.CsName, out var tryp2) ? tryp2.GetValue(d) : null)).Append(" as varchar)");
|
||||||
++pkidx;
|
++pkidx;
|
||||||
}
|
}
|
||||||
if (_table.Primarys.Length > 1) sb.Append(")");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ namespace FreeSql.SqlServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
static DateTime dt1970 = new DateTime(1970, 1, 1);
|
||||||
public override object AddslashesProcessParam(object param) {
|
public override object AddslashesProcessParam(object param) {
|
||||||
if (param == null) return "NULL";
|
if (param == null) return "NULL";
|
||||||
if (param is bool || param is bool?)
|
if (param is bool || param is bool?)
|
||||||
@ -31,12 +32,16 @@ namespace FreeSql.SqlServer {
|
|||||||
return string.Concat("'", param.ToString().Replace("'", "''"), "'");
|
return string.Concat("'", param.ToString().Replace("'", "''"), "'");
|
||||||
else if (decimal.TryParse(string.Concat(param), out var trydec))
|
else if (decimal.TryParse(string.Concat(param), out var trydec))
|
||||||
return param;
|
return param;
|
||||||
else if (param is DateTime) {
|
else if (param is DateTime)
|
||||||
DateTime dt = (DateTime)param;
|
return string.Concat("'", ((DateTime)param).ToString("yyyy-MM-dd HH:mm:ss.fff"), "'");
|
||||||
return string.Concat("'", dt.ToString("yyyy-MM-dd HH:mm:ss.ffffff"), "'");
|
else if (param is DateTime?)
|
||||||
} else if (param is DateTime?) {
|
return string.Concat("'", (param as DateTime?).Value.ToString("yyyy-MM-dd HH:mm:ss.fff"), "'");
|
||||||
DateTime? dt = param as DateTime?;
|
else if (param is TimeSpan) {
|
||||||
return string.Concat("'", dt.Value.ToString("yyyy-MM-dd HH:mm:ss.ffffff"), "'");
|
var ts = (TimeSpan)param;
|
||||||
|
return string.Concat("'", dt1970.Add(ts).ToString("yyyy-MM-dd HH:mm:ss.fff"), "'");
|
||||||
|
} else if (param is TimeSpan) {
|
||||||
|
var ts = (param as TimeSpan?).Value;
|
||||||
|
return string.Concat("'", dt1970.Add(ts).ToString("yyyy-MM-dd HH:mm:ss.fff"), "'");
|
||||||
} else if (param is IEnumerable) {
|
} else if (param is IEnumerable) {
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
var ie = param as IEnumerable;
|
var ie = param as IEnumerable;
|
||||||
|
@ -22,40 +22,51 @@ namespace FreeSql.SqlServer {
|
|||||||
|
|
||||||
public bool IsAutoSyncStructure { get; set; } = true;
|
public bool IsAutoSyncStructure { get; set; } = true;
|
||||||
|
|
||||||
static readonly Dictionary<string, (SqlDbType type, string dbtype, string dbtypeFull, bool? isUnsigned, bool? isnullable)> _dicCsToDb = new Dictionary<string, (SqlDbType type, string dbtype, string dbtypeFull, bool? isUnsigned, bool? isnullable)>() {
|
static object _dicCsToDbLock = new object();
|
||||||
{ "System.Boolean", (SqlDbType.Bit, "bit","bit NOT NULL", null, false) },{ "System.Nullable`1[System.Boolean]", (SqlDbType.Bit, "bit","bit", null, true) },
|
static Dictionary<string, (SqlDbType type, string dbtype, string dbtypeFull, bool? isUnsigned, bool? isnullable)> _dicCsToDb = new Dictionary<string, (SqlDbType type, string dbtype, string dbtypeFull, bool? isUnsigned, bool? isnullable)>() {
|
||||||
|
{ typeof(bool).FullName, (SqlDbType.Bit, "bit","bit NOT NULL", null, false) },{ typeof(bool?).FullName, (SqlDbType.Bit, "bit","bit", null, true) },
|
||||||
|
|
||||||
{ "System.SByte", (SqlDbType.TinyInt, "tinyint", "tinyint NOT NULL", false, false) },{ "System.Nullable`1[System.SByte]", (SqlDbType.TinyInt, "tinyint", "tinyint", false, true) },
|
{ typeof(sbyte).FullName, (SqlDbType.TinyInt, "tinyint", "tinyint NOT NULL", false, false) },{ typeof(sbyte?).FullName, (SqlDbType.TinyInt, "tinyint", "tinyint", false, true) },
|
||||||
{ "System.Int16", (SqlDbType.SmallInt, "smallint","smallint NOT NULL", false, false) },{ "System.Nullable`1[System.Int16]", (SqlDbType.SmallInt, "smallint", "smallint", false, true) },
|
{ typeof(short).FullName, (SqlDbType.SmallInt, "smallint","smallint NOT NULL", false, false) },{ typeof(short?).FullName, (SqlDbType.SmallInt, "smallint", "smallint", false, true) },
|
||||||
{ "System.Int32", (SqlDbType.Int, "int", "int NOT NULL", false, false) },{ "System.Nullable`1[System.Int32]", (SqlDbType.Int, "int", "int", false, true) },
|
{ typeof(int).FullName, (SqlDbType.Int, "int", "int NOT NULL", false, false) },{ typeof(int?).FullName, (SqlDbType.Int, "int", "int", false, true) },
|
||||||
{ "System.Int64", (SqlDbType.BigInt, "bigint","bigint NOT NULL", false, false) },{ "System.Nullable`1[System.Int64]", (SqlDbType.BigInt, "bigint","bigint", false, true) },
|
{ typeof(long).FullName, (SqlDbType.BigInt, "bigint","bigint NOT NULL", false, false) },{ typeof(long?).FullName, (SqlDbType.BigInt, "bigint","bigint", false, true) },
|
||||||
|
|
||||||
{ "System.Byte", (SqlDbType.TinyInt, "tinyint","tinyint NOT NULL", true, false) },{ "System.Nullable`1[System.Byte]", (SqlDbType.TinyInt, "tinyint","tinyint", true, true) },
|
{ typeof(byte).FullName, (SqlDbType.TinyInt, "tinyint","tinyint NOT NULL", true, false) },{ typeof(byte?).FullName, (SqlDbType.TinyInt, "tinyint","tinyint", true, true) },
|
||||||
{ "System.UInt16", (SqlDbType.SmallInt, "smallint","smallint NOT NULL", true, false) },{ "System.Nullable`1[System.UInt16]", (SqlDbType.SmallInt, "smallint", "smallint", true, true) },
|
{ typeof(ushort).FullName, (SqlDbType.SmallInt, "smallint","smallint NOT NULL", true, false) },{ typeof(ushort?).FullName, (SqlDbType.SmallInt, "smallint", "smallint", true, true) },
|
||||||
{ "System.UInt32", (SqlDbType.Int, "int", "int NOT NULL", true, false) },{ "System.Nullable`1[System.UInt32]", (SqlDbType.Int, "int", "int", true, true) },
|
{ typeof(uint).FullName, (SqlDbType.Int, "int", "int NOT NULL", true, false) },{ typeof(uint?).FullName, (SqlDbType.Int, "int", "int", true, true) },
|
||||||
{ "System.UInt64", (SqlDbType.BigInt, "bigint", "bigint NOT NULL", true, false) },{ "System.Nullable`1[System.UInt64]", (SqlDbType.BigInt, "bigint", "bigint", true, true) },
|
{ typeof(ulong).FullName, (SqlDbType.BigInt, "bigint", "bigint NOT NULL", true, false) },{ typeof(ulong?).FullName, (SqlDbType.BigInt, "bigint", "bigint", true, true) },
|
||||||
|
|
||||||
{ "System.Double", (SqlDbType.Float, "double", "double NOT NULL", false, false) },{ "System.Nullable`1[System.Double]", (SqlDbType.Float, "double", "double", false, true) },
|
{ typeof(double).FullName, (SqlDbType.Float, "float", "float NOT NULL", false, false) },{ typeof(double?).FullName, (SqlDbType.Float, "float", "float", false, true) },
|
||||||
{ "System.Single", (SqlDbType.Real, "float","float NOT NULL", false, false) },{ "System.Nullable`1[System.Single]", (SqlDbType.Real, "float","float", false, true) },
|
{ typeof(float).FullName, (SqlDbType.Real, "real","real NOT NULL", false, false) },{ typeof(float?).FullName, (SqlDbType.Real, "real","real", false, true) },
|
||||||
{ "System.Decimal", (SqlDbType.Decimal, "decimal", "decimal(10,2) NOT NULL", false, false) },{ "System.Nullable`1[System.Decimal]", (SqlDbType.Decimal, "decimal", "decimal(10,2)", false, true) },
|
{ typeof(decimal).FullName, (SqlDbType.Decimal, "decimal", "decimal(10,2) NOT NULL", false, false) },{ typeof(decimal?).FullName, (SqlDbType.Decimal, "decimal", "decimal(10,2)", false, true) },
|
||||||
|
|
||||||
{ "System.TimeSpan", (SqlDbType.Time, "time","time NOT NULL", false, false) },{ "System.Nullable`1[System.TimeSpan]", (SqlDbType.Time, "time", "time",false, true) },
|
{ typeof(TimeSpan).FullName, (SqlDbType.Time, "time","time NOT NULL", false, false) },{ typeof(TimeSpan?).FullName, (SqlDbType.Time, "time", "time",false, true) },
|
||||||
{ "System.DateTime", (SqlDbType.DateTime, "datetime", "datetime NOT NULL", false, false) },{ "System.Nullable`1[System.DateTime]", (SqlDbType.DateTime, "datetime", "datetime", false, true) },
|
{ typeof(DateTime).FullName, (SqlDbType.DateTime, "datetime", "datetime NOT NULL", false, false) },{ typeof(DateTime?).FullName, (SqlDbType.DateTime, "datetime", "datetime", false, true) },
|
||||||
{ "System.DateTimeOffset", (SqlDbType.DateTimeOffset, "datetimeoffset", "datetimeoffset NOT NULL", false, false) },{ "System.Nullable`1[System.DateTime]", (SqlDbType.DateTimeOffset, "datetimeoffset", "datetimeoffset", false, true) },
|
{ typeof(DateTimeOffset).FullName, (SqlDbType.DateTimeOffset, "datetimeoffset", "datetimeoffset NOT NULL", false, false) },{ typeof(DateTimeOffset?).FullName, (SqlDbType.DateTimeOffset, "datetimeoffset", "datetimeoffset", false, true) },
|
||||||
|
|
||||||
{ "System.Byte[]", (SqlDbType.VarBinary, "varbinary", "varbinary(255)", false, null) },
|
{ typeof(byte[]).FullName, (SqlDbType.VarBinary, "varbinary", "varbinary(255)", false, null) },
|
||||||
{ "System.String", (SqlDbType.NVarChar, "nvarchar", "nvarchar(255)", false, null) },
|
{ typeof(string).FullName, (SqlDbType.NVarChar, "nvarchar", "nvarchar(255)", false, null) },
|
||||||
|
|
||||||
{ "System.Guid", (SqlDbType.UniqueIdentifier, "uniqueidentifier", "uniqueidentifier", false, false) },{ "System.Guid", (SqlDbType.UniqueIdentifier, "uniqueidentifier", "uniqueidentifier", false, true) },
|
{ typeof(Guid).FullName, (SqlDbType.UniqueIdentifier, "uniqueidentifier", "uniqueidentifier NOT NULL", false, false) },{ typeof(Guid?).FullName, (SqlDbType.UniqueIdentifier, "uniqueidentifier", "uniqueidentifier", false, true) },
|
||||||
};
|
};
|
||||||
|
|
||||||
public (int type, string dbtype, string dbtypeFull, bool? isnullable)? GetDbInfo(Type type) {
|
public (int type, string dbtype, string dbtypeFull, bool? isnullable)? GetDbInfo(Type type) {
|
||||||
|
if (_dicCsToDb.TryGetValue(type.FullName, out var trydc)) return new (int, string, string, bool?)?(((int)trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable));
|
||||||
var enumType = type.IsEnum ? type : null;
|
var enumType = type.IsEnum ? type : null;
|
||||||
if (enumType == null && type.FullName.StartsWith("System.Nullable`1[") && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments.First().IsEnum) enumType = type.GenericTypeArguments.First();
|
if (enumType == null && type.FullName.StartsWith("System.Nullable`1[") && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments.First().IsEnum) enumType = type.GenericTypeArguments.First();
|
||||||
if (enumType != null) {
|
if (enumType != null) {
|
||||||
return ((int)SqlDbType.Int, "int", "int", type.IsEnum ? false : true);
|
var newItem = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ?
|
||||||
|
(SqlDbType.BigInt, "bigint", $"bigint{(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true) :
|
||||||
|
(SqlDbType.Int, "int", $"int{(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true);
|
||||||
|
if (_dicCsToDb.ContainsKey(type.FullName) == false) {
|
||||||
|
lock (_dicCsToDbLock) {
|
||||||
|
if (_dicCsToDb.ContainsKey(type.FullName) == false)
|
||||||
|
_dicCsToDb.Add(type.FullName, newItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ((int)newItem.Item1, newItem.Item2, newItem.Item3, newItem.Item5);
|
||||||
}
|
}
|
||||||
return _dicCsToDb.TryGetValue(type.FullName, out var trydc) ? new (int, string, string, bool?)?(((int)trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable)) : null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetComparisonDDLStatements<TEntity>() => this.GetComparisonDDLStatements(typeof(TEntity));
|
public string GetComparisonDDLStatements<TEntity>() => this.GetComparisonDDLStatements(typeof(TEntity));
|
||||||
@ -70,11 +81,11 @@ namespace FreeSql.SqlServer {
|
|||||||
var isRenameTable = false;
|
var isRenameTable = false;
|
||||||
var tbname = tb.DbName.Split(new[] { '.' }, 2);
|
var tbname = tb.DbName.Split(new[] { '.' }, 2);
|
||||||
if (tbname.Length == 1) tbname = new[] { "dbo", tbname[0] };
|
if (tbname.Length == 1) tbname = new[] { "dbo", tbname[0] };
|
||||||
if (_orm.Ado.ExecuteScalar(CommandType.Text, "select 1 from dbo.sysobjects where id = object_id(N'[{0}].[{1}]') and OBJECTPROPERTY(id, N'IsUserTable')".FormatMySql(tbname)) == null) { //表不存在
|
if (_orm.Ado.ExecuteScalar(CommandType.Text, string.Format("select 1 from dbo.sysobjects where id = object_id(N'[{0}].[{1}]') and OBJECTPROPERTY(id, N'IsUserTable') = 1", tbname)) == null) { //表不存在
|
||||||
|
|
||||||
if (tboldname != null && _orm.Ado.ExecuteScalar(CommandType.Text, "select 1 from dbo.sysobjects where id = object_id(N'[{0}].[{1}]') and OBJECTPROPERTY(id, N'IsUserTable')".FormatMySql(tboldname)) != null) { //旧表存在
|
if (tboldname != null && _orm.Ado.ExecuteScalar(CommandType.Text, string.Format("select 1 from dbo.sysobjects where id = object_id(N'[{0}].[{1}]') and OBJECTPROPERTY(id, N'IsUserTable') = 1", tboldname)) != null) { //旧表存在
|
||||||
//修改表名
|
//修改表名
|
||||||
sb.Append(_commonUtils.FormatSql("EXEC sp_rename {0}, {1} GO \r\n", _commonUtils.QuoteSqlName($"{tboldname[0]}.{tboldname[1]}"), _commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")));
|
sb.Append(_commonUtils.FormatSql("EXEC sp_rename {0}, {1};\r\n", _commonUtils.QuoteSqlName($"{tboldname[0]}.{tboldname[1]}"), _commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")));
|
||||||
isRenameTable = true;
|
isRenameTable = true;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@ -87,7 +98,7 @@ namespace FreeSql.SqlServer {
|
|||||||
if (tbcol.Attribute.IsPrimary) sb.Append(" primary key");
|
if (tbcol.Attribute.IsPrimary) sb.Append(" primary key");
|
||||||
sb.Append(",");
|
sb.Append(",");
|
||||||
}
|
}
|
||||||
sb.Remove(sb.Length - 1, 1).Append("\r\n) GO \r\n");
|
sb.Remove(sb.Length - 1, 1).Append("\r\n);\r\n");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -96,7 +107,7 @@ namespace FreeSql.SqlServer {
|
|||||||
foreach (var tbcol in tb.Columns) addcols.Add(tbcol.Value.Attribute.Name, tbcol.Value);
|
foreach (var tbcol in tb.Columns) addcols.Add(tbcol.Value.Attribute.Name, tbcol.Value);
|
||||||
var surplus = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
|
var surplus = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
|
||||||
var dbcols = new List<DbColumnInfo>();
|
var dbcols = new List<DbColumnInfo>();
|
||||||
var sql = @"select
|
var sql = string.Format(@"select
|
||||||
a.name 'Column'
|
a.name 'Column'
|
||||||
,b.name + case
|
,b.name + case
|
||||||
when b.name in ('Char', 'VarChar', 'NChar', 'NVarChar', 'Binary', 'VarBinary') then '(' +
|
when b.name in ('Char', 'VarChar', 'NChar', 'NVarChar', 'Binary', 'VarBinary') then '(' +
|
||||||
@ -105,14 +116,14 @@ a.name 'Column'
|
|||||||
else cast(a.max_length as varchar) end + ')'
|
else cast(a.max_length as varchar) end + ')'
|
||||||
when b.name in ('Numeric', 'Decimal') then '(' + cast(a.precision as varchar) + ',' + cast(a.scale as varchar) + ')'
|
when b.name in ('Numeric', 'Decimal') then '(' + cast(a.precision as varchar) + ',' + cast(a.scale as varchar) + ')'
|
||||||
else '' end as 'SqlType'
|
else '' end as 'SqlType'
|
||||||
,a.is_nullable 'IsNullable'
|
,case when a.is_nullable = 1 then '1' else '0' end 'IsNullable'
|
||||||
,a.is_identity 'IsIdentity'
|
,case when a.is_identity = 1 then '1' else '0' end 'IsIdentity'
|
||||||
from sys.columns a
|
from sys.columns a
|
||||||
inner join sys.types b on b.user_type_id = a.user_type_id
|
inner join sys.types b on b.user_type_id = a.user_type_id
|
||||||
left join sys.extended_properties AS c ON c.major_id = a.object_id AND c.minor_id = a.column_id
|
left join sys.extended_properties AS c ON c.major_id = a.object_id AND c.minor_id = a.column_id
|
||||||
left join sys.tables d on d.object_id = a.object_id
|
left join sys.tables d on d.object_id = a.object_id
|
||||||
left join sys.schemas e on e.schema_id = d.schema_id
|
left join sys.schemas e on e.schema_id = d.schema_id
|
||||||
where a.object_id in (object_id(N'[{0}].[{1}]'))".FormatMySql(isRenameTable ? tboldname : tbname);
|
where a.object_id in (object_id(N'[{0}].[{1}]'))", isRenameTable ? tboldname : tbname);
|
||||||
var ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
|
var ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
|
||||||
foreach (var row in ds) {
|
foreach (var row in ds) {
|
||||||
string column = string.Concat(row[0]);
|
string column = string.Concat(row[0]);
|
||||||
@ -126,7 +137,7 @@ where a.object_id in (object_id(N'[{0}].[{1}]'))".FormatMySql(isRenameTable ? tb
|
|||||||
trycol.Attribute.IsIdentity != is_identity) {
|
trycol.Attribute.IsIdentity != is_identity) {
|
||||||
sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ALTER COLUMN ").Append(_commonUtils.QuoteSqlName(column)).Append(" ").Append(trycol.Attribute.DbType.ToUpper());
|
sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ALTER COLUMN ").Append(_commonUtils.QuoteSqlName(column)).Append(" ").Append(trycol.Attribute.DbType.ToUpper());
|
||||||
if (trycol.Attribute.IsIdentity && trycol.Attribute.DbType.IndexOf("identity", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" identity(1,1)");
|
if (trycol.Attribute.IsIdentity && trycol.Attribute.DbType.IndexOf("identity", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" identity(1,1)");
|
||||||
sb.Append(" GO \r\n");
|
sb.Append(";\r\n");
|
||||||
}
|
}
|
||||||
addcols.Remove(column);
|
addcols.Remove(column);
|
||||||
} else
|
} else
|
||||||
@ -134,27 +145,36 @@ where a.object_id in (object_id(N'[{0}].[{1}]'))".FormatMySql(isRenameTable ? tb
|
|||||||
}
|
}
|
||||||
foreach (var addcol in addcols.Values) {
|
foreach (var addcol in addcols.Values) {
|
||||||
if (string.IsNullOrEmpty(addcol.Attribute.OldName) == false && surplus.ContainsKey(addcol.Attribute.OldName)) { //修改列名
|
if (string.IsNullOrEmpty(addcol.Attribute.OldName) == false && surplus.ContainsKey(addcol.Attribute.OldName)) { //修改列名
|
||||||
sb.Append(_commonUtils.FormatSql("EXEC sp_rename {0}, {1}, 'COLUMN' GO \r\n", _commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}.{addcol.Attribute.OldName}"), _commonUtils.QuoteSqlName(addcol.Attribute.Name)));
|
sb.Append(_commonUtils.FormatSql("EXEC sp_rename {0}, {1}, 'COLUMN';\r\n", $"{tbname[0]}.{tbname[1]}.{addcol.Attribute.OldName}", addcol.Attribute.Name));
|
||||||
sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ALTER COLUMN ").Append(_commonUtils.QuoteSqlName(addcol.Attribute.Name)).Append(" ").Append(addcol.Attribute.DbType.ToUpper());
|
sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ALTER COLUMN ").Append(_commonUtils.QuoteSqlName(addcol.Attribute.Name)).Append(" ").Append(addcol.Attribute.DbType.ToUpper());
|
||||||
if (addcol.Attribute.IsIdentity && addcol.Attribute.DbType.IndexOf("identity", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" identity(1,1)");
|
if (addcol.Attribute.IsIdentity && addcol.Attribute.DbType.IndexOf("identity", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" identity(1,1)");
|
||||||
sb.Append(" GO \r\n");
|
sb.Append(";\r\n");
|
||||||
|
|
||||||
} else { //添加列
|
} else { //添加列
|
||||||
sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ADD ").Append(_commonUtils.QuoteSqlName(addcol.Attribute.Name)).Append(" ").Append(addcol.Attribute.DbType.ToUpper());
|
sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ADD ").Append(_commonUtils.QuoteSqlName(addcol.Attribute.Name)).Append(" ").Append(addcol.Attribute.DbType.ToUpper());
|
||||||
if (addcol.Attribute.IsIdentity && addcol.Attribute.DbType.IndexOf("identity", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" identity(1,1)");
|
if (addcol.Attribute.IsIdentity && addcol.Attribute.DbType.IndexOf("identity", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" identity(1,1)");
|
||||||
sb.Append(" GO \r\n");
|
sb.Append(";\r\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sb.Length == 0 ? null : sb.ToString();
|
return sb.Length == 0 ? null : sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Dictionary<string, bool> dicSyced = new Dictionary<string, bool>();
|
||||||
public bool SyncStructure<TEntity>() => this.SyncStructure(typeof(TEntity));
|
public bool SyncStructure<TEntity>() => this.SyncStructure(typeof(TEntity));
|
||||||
public bool SyncStructure(params Type[] entityTypes) {
|
public bool SyncStructure(params Type[] entityTypes) {
|
||||||
var ddl = this.GetComparisonDDLStatements(entityTypes);
|
if (entityTypes == null) return true;
|
||||||
if (string.IsNullOrEmpty(ddl)) return true;
|
var syncTypes = entityTypes.Where(a => dicSyced.ContainsKey(a.FullName) == false).ToArray();
|
||||||
|
if (syncTypes.Any() == false) return true;
|
||||||
|
var ddl = this.GetComparisonDDLStatements(syncTypes);
|
||||||
|
if (string.IsNullOrEmpty(ddl)) {
|
||||||
|
foreach (var syncType in syncTypes) dicSyced.Add(syncType.FullName, true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
return _orm.Ado.ExecuteNonQuery(CommandType.Text, ddl) > 0;
|
var affrows = _orm.Ado.ExecuteNonQuery(CommandType.Text, ddl);
|
||||||
|
foreach (var syncType in syncTypes) dicSyced.Add(syncType.FullName, true);
|
||||||
|
return affrows > 0;
|
||||||
} catch {
|
} catch {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -60,7 +60,6 @@ namespace FreeSql.SqlServer {
|
|||||||
{ (int)SqlDbType.Int, ("(int?)", "int.Parse({0})", "{0}.ToString()", "int?", typeof(int), typeof(int?), "{0}.Value", "GetInt32") },
|
{ (int)SqlDbType.Int, ("(int?)", "int.Parse({0})", "{0}.ToString()", "int?", typeof(int), typeof(int?), "{0}.Value", "GetInt32") },
|
||||||
{ (int)SqlDbType.BigInt, ("(long?)", "long.Parse({0})", "{0}.ToString()", "long?", typeof(long), typeof(long?), "{0}.Value", "GetInt64") },
|
{ (int)SqlDbType.BigInt, ("(long?)", "long.Parse({0})", "{0}.ToString()", "long?", typeof(long), typeof(long?), "{0}.Value", "GetInt64") },
|
||||||
|
|
||||||
{ (int)SqlDbType.Decimal, ("(decimal?)", "decimal.Parse({0})", "{0}.ToString()", "decimal?", typeof(decimal), typeof(decimal?), "{0}.Value", "GetDecimal") },
|
|
||||||
{ (int)SqlDbType.SmallMoney, ("(decimal?)", "decimal.Parse({0})", "{0}.ToString()", "decimal?", typeof(decimal), typeof(decimal?), "{0}.Value", "GetDecimal") },
|
{ (int)SqlDbType.SmallMoney, ("(decimal?)", "decimal.Parse({0})", "{0}.ToString()", "decimal?", typeof(decimal), typeof(decimal?), "{0}.Value", "GetDecimal") },
|
||||||
{ (int)SqlDbType.Money, ("(decimal?)", "decimal.Parse({0})", "{0}.ToString()", "decimal?", typeof(decimal), typeof(decimal?), "{0}.Value", "GetDecimal") },
|
{ (int)SqlDbType.Money, ("(decimal?)", "decimal.Parse({0})", "{0}.ToString()", "decimal?", typeof(decimal), typeof(decimal?), "{0}.Value", "GetDecimal") },
|
||||||
{ (int)SqlDbType.Decimal, ("(decimal?)", "decimal.Parse({0})", "{0}.ToString()", "decimal?", typeof(decimal), typeof(decimal?), "{0}.Value", "GetDecimal") },
|
{ (int)SqlDbType.Decimal, ("(decimal?)", "decimal.Parse({0})", "{0}.ToString()", "decimal?", typeof(decimal), typeof(decimal?), "{0}.Value", "GetDecimal") },
|
||||||
@ -86,7 +85,7 @@ namespace FreeSql.SqlServer {
|
|||||||
{ (int)SqlDbType.NVarChar, ("", "{0}.Replace(StringifySplit, \"|\")", "{0}.Replace(\"|\", StringifySplit)", "string", typeof(string), typeof(string), "{0}", "GetString") },
|
{ (int)SqlDbType.NVarChar, ("", "{0}.Replace(StringifySplit, \"|\")", "{0}.Replace(\"|\", StringifySplit)", "string", typeof(string), typeof(string), "{0}", "GetString") },
|
||||||
{ (int)SqlDbType.NText, ("", "{0}.Replace(StringifySplit, \"|\")", "{0}.Replace(\"|\", StringifySplit)", "string", typeof(string), typeof(string), "{0}", "GetString") },
|
{ (int)SqlDbType.NText, ("", "{0}.Replace(StringifySplit, \"|\")", "{0}.Replace(\"|\", StringifySplit)", "string", typeof(string), typeof(string), "{0}", "GetString") },
|
||||||
|
|
||||||
{ (int)SqlDbType.UniqueIdentifier, ("(Guid?)", "Guid.Parse({0})", "{0}.ToString()", "MygisGeometry", typeof(Guid), typeof(Guid?), "{0}.Value", "GetGuid") },
|
{ (int)SqlDbType.UniqueIdentifier, ("(Guid?)", "Guid.Parse({0})", "{0}.ToString()", "Guid?", typeof(Guid), typeof(Guid?), "{0}.Value", "GetGuid") },
|
||||||
};
|
};
|
||||||
|
|
||||||
public string GetCsConvert(DbColumnInfo column) => _dicDbToCs.TryGetValue(column.DbType, out var trydc) ? (column.IsNullable ? trydc.csConvert : trydc.csConvert.Replace("?", "")) : null;
|
public string GetCsConvert(DbColumnInfo column) => _dicDbToCs.TryGetValue(column.DbType, out var trydc) ? (column.IsNullable ? trydc.csConvert : trydc.csConvert.Replace("?", "")) : null;
|
||||||
@ -104,11 +103,22 @@ namespace FreeSql.SqlServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<DbTableInfo> GetTablesByDatabase(params string[] database) {
|
public List<DbTableInfo> GetTablesByDatabase(params string[] database) {
|
||||||
List<DbTableInfo> loc1 = null;
|
var olddatabase = "";
|
||||||
Dictionary<int, DbTableInfo> loc2 = new Dictionary<int, DbTableInfo>();
|
using (var conn = _orm.Ado.MasterPool.Get(TimeSpan.FromSeconds(5))) {
|
||||||
Dictionary<int, Dictionary<string, DbColumnInfo>> loc3 = new Dictionary<int, Dictionary<string, DbColumnInfo>>();
|
olddatabase = conn.Value.Database;
|
||||||
|
}
|
||||||
|
var dbs = database?.ToArray() ?? new[] { olddatabase };
|
||||||
|
var tables = new List<DbTableInfo>();
|
||||||
|
|
||||||
var sql = @"
|
foreach (var db in dbs) {
|
||||||
|
if (string.IsNullOrEmpty(db)) continue;
|
||||||
|
|
||||||
|
var loc1 = new List<DbTableInfo>();
|
||||||
|
var loc2 = new Dictionary<int, DbTableInfo>();
|
||||||
|
var loc3 = new Dictionary<int, Dictionary<string, DbColumnInfo>>();
|
||||||
|
|
||||||
|
var sql = $@"
|
||||||
|
use {db};
|
||||||
select
|
select
|
||||||
a.Object_id
|
a.Object_id
|
||||||
,b.name 'Owner'
|
,b.name 'Owner'
|
||||||
@ -135,34 +145,37 @@ from sys.procedures a
|
|||||||
inner join sys.schemas b on b.schema_id = a.schema_id
|
inner join sys.schemas b on b.schema_id = a.schema_id
|
||||||
where a.type = 'P' and charindex('$NPSP', a.name) = 0 and charindex('diagram', a.name) = 0
|
where a.type = 'P' and charindex('$NPSP', a.name) = 0 and charindex('diagram', a.name) = 0
|
||||||
order by type desc, b.name, a.name
|
order by type desc, b.name, a.name
|
||||||
|
;
|
||||||
|
use {olddatabase};
|
||||||
";
|
";
|
||||||
var ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
|
var ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
|
||||||
if (ds == null) return loc1;
|
if (ds == null) return loc1;
|
||||||
|
|
||||||
var loc6 = new List<int>();
|
var loc6 = new List<int>();
|
||||||
var loc66 = new List<int>();
|
var loc66 = new List<int>();
|
||||||
foreach (object[] row in ds) {
|
foreach (object[] row in ds) {
|
||||||
int object_id = int.Parse(string.Concat(row[0]));
|
int object_id = int.Parse(string.Concat(row[0]));
|
||||||
var owner = string.Concat(row[1]);
|
var owner = string.Concat(row[1]);
|
||||||
var table = string.Concat(row[2]);
|
var table = string.Concat(row[2]);
|
||||||
Enum.TryParse<DbTableType>(string.Concat(row[3]), out var type);
|
Enum.TryParse<DbTableType>(string.Concat(row[3]), out var type);
|
||||||
loc2.Add(object_id, new DbTableInfo { Id = object_id.ToString(), Schema = owner, Name = table, Type = type });
|
loc2.Add(object_id, new DbTableInfo { Id = object_id.ToString(), Schema = owner, Name = table, Type = type });
|
||||||
loc3.Add(object_id, new Dictionary<string, DbColumnInfo>());
|
loc3.Add(object_id, new Dictionary<string, DbColumnInfo>());
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case DbTableType.VIEW:
|
case DbTableType.VIEW:
|
||||||
case DbTableType.TABLE:
|
case DbTableType.TABLE:
|
||||||
loc6.Add(object_id);
|
loc6.Add(object_id);
|
||||||
break;
|
break;
|
||||||
case DbTableType.StoreProcedure:
|
case DbTableType.StoreProcedure:
|
||||||
loc66.Add(object_id);
|
loc66.Add(object_id);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
if (loc6.Count == 0) return loc1;
|
||||||
if (loc6.Count == 0) return loc1;
|
var loc8 = string.Join(",", loc6.Select(a => string.Concat(a)));
|
||||||
var loc8 = string.Join(",", loc6.Select(a => string.Concat(a)));
|
var loc88 = string.Join(",", loc66.Select(a => string.Concat(a)));
|
||||||
var loc88 = string.Join(",", loc66.Select(a => string.Concat(a)));
|
|
||||||
|
var tsql_place = @"
|
||||||
|
|
||||||
var tsql_place = @"
|
|
||||||
select
|
select
|
||||||
isnull(e.name,'') + '.' + isnull(d.name,'')
|
isnull(e.name,'') + '.' + isnull(d.name,'')
|
||||||
,a.Object_id
|
,a.Object_id
|
||||||
@ -187,50 +200,52 @@ left join sys.tables d on d.object_id = a.object_id
|
|||||||
left join sys.schemas e on e.schema_id = d.schema_id
|
left join sys.schemas e on e.schema_id = d.schema_id
|
||||||
where a.object_id in ({1})
|
where a.object_id in ({1})
|
||||||
";
|
";
|
||||||
sql = string.Format(tsql_place, @"
|
sql = string.Format(tsql_place, @"
|
||||||
,a.is_nullable 'IsNullable'
|
,a.is_nullable 'IsNullable'
|
||||||
,a.is_identity 'IsIdentity'
|
,a.is_identity 'IsIdentity'
|
||||||
from sys.columns", loc8);
|
from sys.columns", loc8);
|
||||||
if (loc88.Length > 0) {
|
if (loc88.Length > 0) {
|
||||||
sql += "union all" +
|
sql += "union all" +
|
||||||
string.Format(tsql_place.Replace(
|
string.Format(tsql_place.Replace(
|
||||||
"left join sys.extended_properties AS c ON c.major_id = a.object_id AND c.minor_id = a.column_id",
|
"left join sys.extended_properties AS c ON c.major_id = a.object_id AND c.minor_id = a.column_id",
|
||||||
"left join sys.extended_properties AS c ON c.major_id = a.object_id AND c.minor_id = a.parameter_id"), @"
|
"left join sys.extended_properties AS c ON c.major_id = a.object_id AND c.minor_id = a.parameter_id"), @"
|
||||||
,cast(0 as bit) 'IsNullable'
|
,cast(0 as bit) 'IsNullable'
|
||||||
,a.is_output 'IsIdentity'
|
,a.is_output 'IsIdentity'
|
||||||
from sys.parameters", loc88);
|
from sys.parameters", loc88);
|
||||||
}
|
}
|
||||||
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
|
sql = $"use {db};{sql};use {olddatabase}; ";
|
||||||
if (ds == null) return loc1;
|
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
|
||||||
|
if (ds == null) return loc1;
|
||||||
|
|
||||||
foreach (object[] row in ds) {
|
foreach (object[] row in ds) {
|
||||||
var table_id = string.Concat(row[0]);
|
var table_id = string.Concat(row[0]);
|
||||||
var object_id = int.Parse(string.Concat(row[1]));
|
var object_id = int.Parse(string.Concat(row[1]));
|
||||||
var column = string.Concat(row[2]);
|
var column = string.Concat(row[2]);
|
||||||
var type = string.Concat(row[3]);
|
var type = string.Concat(row[3]);
|
||||||
var max_length = int.Parse(string.Concat(row[4]));
|
var max_length = int.Parse(string.Concat(row[4]));
|
||||||
var sqlType = string.Concat(row[5]);
|
var sqlType = string.Concat(row[5]);
|
||||||
var comment = string.Concat(row[6]);
|
var comment = string.Concat(row[6]);
|
||||||
var is_nullable = bool.Parse(string.Concat(row[7]));
|
var is_nullable = bool.Parse(string.Concat(row[7]));
|
||||||
var is_identity = bool.Parse(string.Concat(row[8]));
|
var is_identity = bool.Parse(string.Concat(row[8]));
|
||||||
if (max_length == 0) max_length = -1;
|
if (max_length == 0) max_length = -1;
|
||||||
|
|
||||||
loc3[object_id].Add(column, new DbColumnInfo {
|
loc3[object_id].Add(column, new DbColumnInfo {
|
||||||
Name = column,
|
Name = column,
|
||||||
MaxLength = max_length,
|
MaxLength = max_length,
|
||||||
IsIdentity = is_identity,
|
IsIdentity = is_identity,
|
||||||
IsNullable = is_nullable,
|
IsNullable = is_nullable,
|
||||||
IsPrimary = false,
|
IsPrimary = false,
|
||||||
DbTypeText = type,
|
DbTypeText = type,
|
||||||
DbTypeTextFull = sqlType,
|
DbTypeTextFull = sqlType,
|
||||||
Table = loc2[object_id],
|
Table = loc2[object_id],
|
||||||
Coment = comment
|
Coment = comment
|
||||||
});
|
});
|
||||||
loc3[object_id][column].DbType = this.GetDbType(loc3[object_id][column]);
|
loc3[object_id][column].DbType = this.GetDbType(loc3[object_id][column]);
|
||||||
loc3[object_id][column].CsType = this.GetCsTypeInfo(loc3[object_id][column]);
|
loc3[object_id][column].CsType = this.GetCsTypeInfo(loc3[object_id][column]);
|
||||||
}
|
}
|
||||||
|
|
||||||
sql = string.Format(@"
|
sql = $@"
|
||||||
|
use {db};
|
||||||
select
|
select
|
||||||
a.object_id 'Object_id'
|
a.object_id 'Object_id'
|
||||||
,c.name 'Column'
|
,c.name 'Column'
|
||||||
@ -242,53 +257,55 @@ select
|
|||||||
from sys.index_columns a
|
from sys.index_columns a
|
||||||
inner join sys.indexes b on b.object_id = a.object_id and b.index_id = a.index_id
|
inner join sys.indexes b on b.object_id = a.object_id and b.index_id = a.index_id
|
||||||
left join sys.columns c on c.object_id = a.object_id and c.column_id = a.column_id
|
left join sys.columns c on c.object_id = a.object_id and c.column_id = a.column_id
|
||||||
where a.object_id in ({0})
|
where a.object_id in ({loc8})
|
||||||
", loc8);
|
;
|
||||||
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
|
use {olddatabase};
|
||||||
if (ds == null) return loc1;
|
";
|
||||||
|
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
|
||||||
|
if (ds == null) return loc1;
|
||||||
|
|
||||||
var indexColumns = new Dictionary<int, Dictionary<int, List<DbColumnInfo>>>();
|
var indexColumns = new Dictionary<int, Dictionary<int, List<DbColumnInfo>>>();
|
||||||
var uniqueColumns = new Dictionary<int, Dictionary<int, List<DbColumnInfo>>>();
|
var uniqueColumns = new Dictionary<int, Dictionary<int, List<DbColumnInfo>>>();
|
||||||
foreach (object[] row in ds) {
|
foreach (object[] row in ds) {
|
||||||
int object_id = int.Parse(string.Concat(row[0]));
|
int object_id = int.Parse(string.Concat(row[0]));
|
||||||
string column = string.Concat(row[1]);
|
string column = string.Concat(row[1]);
|
||||||
int index_id = int.Parse(string.Concat(row[2]));
|
int index_id = int.Parse(string.Concat(row[2]));
|
||||||
bool is_unique = bool.Parse(string.Concat(row[3]));
|
bool is_unique = bool.Parse(string.Concat(row[3]));
|
||||||
bool is_primary_key = bool.Parse(string.Concat(row[4]));
|
bool is_primary_key = bool.Parse(string.Concat(row[4]));
|
||||||
bool is_clustered = bool.Parse(string.Concat(row[5]));
|
bool is_clustered = bool.Parse(string.Concat(row[5]));
|
||||||
int is_desc = int.Parse(string.Concat(row[6]));
|
int is_desc = int.Parse(string.Concat(row[6]));
|
||||||
|
|
||||||
if (loc3.ContainsKey(object_id) == false || loc3[object_id].ContainsKey(column) == false) continue;
|
if (loc3.ContainsKey(object_id) == false || loc3[object_id].ContainsKey(column) == false) continue;
|
||||||
DbColumnInfo loc9 = loc3[object_id][column];
|
DbColumnInfo loc9 = loc3[object_id][column];
|
||||||
if (loc9.IsPrimary == false && is_primary_key) loc9.IsPrimary = is_primary_key;
|
if (loc9.IsPrimary == false && is_primary_key) loc9.IsPrimary = is_primary_key;
|
||||||
|
|
||||||
Dictionary<int, List<DbColumnInfo>> loc10 = null;
|
Dictionary<int, List<DbColumnInfo>> loc10 = null;
|
||||||
List<DbColumnInfo> loc11 = null;
|
List<DbColumnInfo> loc11 = null;
|
||||||
if (!indexColumns.TryGetValue(object_id, out loc10))
|
if (!indexColumns.TryGetValue(object_id, out loc10))
|
||||||
indexColumns.Add(object_id, loc10 = new Dictionary<int, List<DbColumnInfo>>());
|
indexColumns.Add(object_id, loc10 = new Dictionary<int, List<DbColumnInfo>>());
|
||||||
if (!loc10.TryGetValue(index_id, out loc11))
|
|
||||||
loc10.Add(index_id, loc11 = new List<DbColumnInfo>());
|
|
||||||
loc11.Add(loc9);
|
|
||||||
if (is_unique) {
|
|
||||||
if (!uniqueColumns.TryGetValue(object_id, out loc10))
|
|
||||||
uniqueColumns.Add(object_id, loc10 = new Dictionary<int, List<DbColumnInfo>>());
|
|
||||||
if (!loc10.TryGetValue(index_id, out loc11))
|
if (!loc10.TryGetValue(index_id, out loc11))
|
||||||
loc10.Add(index_id, loc11 = new List<DbColumnInfo>());
|
loc10.Add(index_id, loc11 = new List<DbColumnInfo>());
|
||||||
loc11.Add(loc9);
|
loc11.Add(loc9);
|
||||||
|
if (is_unique) {
|
||||||
|
if (!uniqueColumns.TryGetValue(object_id, out loc10))
|
||||||
|
uniqueColumns.Add(object_id, loc10 = new Dictionary<int, List<DbColumnInfo>>());
|
||||||
|
if (!loc10.TryGetValue(index_id, out loc11))
|
||||||
|
loc10.Add(index_id, loc11 = new List<DbColumnInfo>());
|
||||||
|
loc11.Add(loc9);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
foreach (var object_id in indexColumns.Keys) {
|
||||||
foreach (var object_id in indexColumns.Keys) {
|
foreach (List<DbColumnInfo> columns in indexColumns[object_id].Values)
|
||||||
foreach (List<DbColumnInfo> columns in indexColumns[object_id].Values)
|
loc2[object_id].Indexes.Add(columns);
|
||||||
loc2[object_id].Indexes.Add(columns);
|
}
|
||||||
}
|
foreach (var object_id in uniqueColumns.Keys) {
|
||||||
foreach (var object_id in uniqueColumns.Keys) {
|
foreach (var columns in uniqueColumns[object_id].Values) {
|
||||||
foreach (var columns in uniqueColumns[object_id].Values) {
|
columns.Sort((c1, c2) => c1.Name.CompareTo(c2.Name));
|
||||||
columns.Sort((c1, c2) => c1.Name.CompareTo(c2.Name));
|
loc2[object_id].Uniques.Add(columns);
|
||||||
loc2[object_id].Uniques.Add(columns);
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
sql = string.Format(@"
|
sql = $@"
|
||||||
select
|
select
|
||||||
b.object_id 'Object_id'
|
b.object_id 'Object_id'
|
||||||
,c.name 'Column'
|
,c.name 'Column'
|
||||||
@ -302,82 +319,100 @@ from sys.foreign_key_columns a
|
|||||||
inner join sys.tables b on b.object_id = a.parent_object_id
|
inner join sys.tables b on b.object_id = a.parent_object_id
|
||||||
inner join sys.columns c on c.object_id = a.parent_object_id and c.column_id = a.parent_column_id
|
inner join sys.columns c on c.object_id = a.parent_object_id and c.column_id = a.parent_column_id
|
||||||
inner join sys.columns d on d.object_id = a.referenced_object_id and d.column_id = a.referenced_column_id
|
inner join sys.columns d on d.object_id = a.referenced_object_id and d.column_id = a.referenced_column_id
|
||||||
where b.object_id in ({0})
|
where b.object_id in ({loc8})
|
||||||
", loc8);
|
;
|
||||||
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
|
use {olddatabase};
|
||||||
if (ds == null) return loc1;
|
";
|
||||||
|
ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
|
||||||
|
if (ds == null) return loc1;
|
||||||
|
|
||||||
var fkColumns = new Dictionary<int, Dictionary<int, DbForeignInfo>>();
|
var fkColumns = new Dictionary<int, Dictionary<int, DbForeignInfo>>();
|
||||||
foreach (object[] row in ds) {
|
foreach (object[] row in ds) {
|
||||||
int object_id, fk_id, referenced_object_id;
|
int object_id, fk_id, referenced_object_id;
|
||||||
int.TryParse(string.Concat(row[0]), out object_id);
|
int.TryParse(string.Concat(row[0]), out object_id);
|
||||||
var column = string.Concat(row[1]);
|
var column = string.Concat(row[1]);
|
||||||
int.TryParse(string.Concat(row[2]), out fk_id);
|
int.TryParse(string.Concat(row[2]), out fk_id);
|
||||||
int.TryParse(string.Concat(row[3]), out referenced_object_id);
|
int.TryParse(string.Concat(row[3]), out referenced_object_id);
|
||||||
var is_foreign_key = bool.Parse(string.Concat(row[4]));
|
var is_foreign_key = bool.Parse(string.Concat(row[4]));
|
||||||
var referenced_column = string.Concat(row[5]);
|
var referenced_column = string.Concat(row[5]);
|
||||||
var referenced_db = string.Concat(row[6]);
|
var referenced_db = string.Concat(row[6]);
|
||||||
var referenced_table = string.Concat(row[7]);
|
var referenced_table = string.Concat(row[7]);
|
||||||
DbColumnInfo loc9 = loc3[object_id][column];
|
DbColumnInfo loc9 = loc3[object_id][column];
|
||||||
DbTableInfo loc10 = null;
|
DbTableInfo loc10 = null;
|
||||||
DbColumnInfo loc11 = null;
|
DbColumnInfo loc11 = null;
|
||||||
bool isThisSln = referenced_object_id != 0;
|
bool isThisSln = referenced_object_id != 0;
|
||||||
|
|
||||||
if (isThisSln) {
|
if (isThisSln) {
|
||||||
loc10 = loc2[referenced_object_id];
|
loc10 = loc2[referenced_object_id];
|
||||||
loc11 = loc3[referenced_object_id][referenced_column];
|
loc11 = loc3[referenced_object_id][referenced_column];
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
}
|
||||||
|
Dictionary<int, DbForeignInfo> loc12 = null;
|
||||||
|
DbForeignInfo loc13 = null;
|
||||||
|
if (!fkColumns.TryGetValue(object_id, out loc12))
|
||||||
|
fkColumns.Add(object_id, loc12 = new Dictionary<int, DbForeignInfo>());
|
||||||
|
if (!loc12.TryGetValue(fk_id, out loc13))
|
||||||
|
loc12.Add(fk_id, loc13 = new DbForeignInfo { Table = loc2[object_id], ReferencedTable = loc10 });
|
||||||
|
loc13.Columns.Add(loc9);
|
||||||
|
loc13.ReferencedColumns.Add(loc11);
|
||||||
}
|
}
|
||||||
Dictionary<int, DbForeignInfo> loc12 = null;
|
foreach (var table_id in fkColumns.Keys)
|
||||||
DbForeignInfo loc13 = null;
|
foreach (var fk in fkColumns[table_id].Values)
|
||||||
if (!fkColumns.TryGetValue(object_id, out loc12))
|
loc2[table_id].Foreigns.Add(fk);
|
||||||
fkColumns.Add(object_id, loc12 = new Dictionary<int, DbForeignInfo>());
|
|
||||||
if (!loc12.TryGetValue(fk_id, out loc13))
|
|
||||||
loc12.Add(fk_id, new DbForeignInfo { Table = loc2[object_id], ReferencedTable = loc10 });
|
|
||||||
loc13.Columns.Add(loc9);
|
|
||||||
loc13.ReferencedColumns.Add(loc11);
|
|
||||||
}
|
|
||||||
foreach (var table_id in fkColumns.Keys)
|
|
||||||
foreach (var fk in fkColumns[table_id].Values)
|
|
||||||
loc2[table_id].Foreigns.Add(fk);
|
|
||||||
|
|
||||||
foreach (var table_id in loc3.Keys) {
|
foreach (var table_id in loc3.Keys) {
|
||||||
foreach (var loc5 in loc3[table_id].Values) {
|
foreach (var loc5 in loc3[table_id].Values) {
|
||||||
loc2[table_id].Columns.Add(loc5);
|
loc2[table_id].Columns.Add(loc5);
|
||||||
if (loc5.IsIdentity) loc2[table_id].Identitys.Add(loc5);
|
if (loc5.IsIdentity) loc2[table_id].Identitys.Add(loc5);
|
||||||
if (loc5.IsPrimary) loc2[table_id].Primarys.Add(loc5);
|
if (loc5.IsPrimary) loc2[table_id].Primarys.Add(loc5);
|
||||||
}
|
|
||||||
}
|
|
||||||
foreach (var loc4 in loc2.Values) {
|
|
||||||
if (loc4.Primarys.Count == 0 && loc4.Uniques.Count > 0) {
|
|
||||||
foreach (var loc5 in loc4.Uniques[0]) {
|
|
||||||
loc5.IsPrimary = true;
|
|
||||||
loc4.Primarys.Add(loc5);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
loc4.Primarys.Sort((c1, c2) => c1.Name.CompareTo(c2.Name));
|
foreach (var loc4 in loc2.Values) {
|
||||||
loc4.Columns.Sort((c1, c2) => {
|
if (loc4.Primarys.Count == 0 && loc4.Uniques.Count > 0) {
|
||||||
int compare = c2.IsPrimary.CompareTo(c1.IsPrimary);
|
foreach (var loc5 in loc4.Uniques[0]) {
|
||||||
if (compare == 0) {
|
loc5.IsPrimary = true;
|
||||||
bool b1 = loc4.Foreigns.Find(fk => fk.Columns.Find(c3 => c3.Name == c1.Name) != null) != null;
|
loc4.Primarys.Add(loc5);
|
||||||
bool b2 = loc4.Foreigns.Find(fk => fk.Columns.Find(c3 => c3.Name == c2.Name) != null) != null;
|
}
|
||||||
compare = b2.CompareTo(b1);
|
|
||||||
}
|
}
|
||||||
if (compare == 0) compare = c1.Name.CompareTo(c2.Name);
|
loc4.Primarys.Sort((c1, c2) => c1.Name.CompareTo(c2.Name));
|
||||||
return compare;
|
loc4.Columns.Sort((c1, c2) => {
|
||||||
|
int compare = c2.IsPrimary.CompareTo(c1.IsPrimary);
|
||||||
|
if (compare == 0) {
|
||||||
|
bool b1 = loc4.Foreigns.Find(fk => fk.Columns.Find(c3 => c3.Name == c1.Name) != null) != null;
|
||||||
|
bool b2 = loc4.Foreigns.Find(fk => fk.Columns.Find(c3 => c3.Name == c2.Name) != null) != null;
|
||||||
|
compare = b2.CompareTo(b1);
|
||||||
|
}
|
||||||
|
if (compare == 0) compare = c1.Name.CompareTo(c2.Name);
|
||||||
|
return compare;
|
||||||
|
});
|
||||||
|
loc1.Add(loc4);
|
||||||
|
}
|
||||||
|
loc1.Sort((t1, t2) => {
|
||||||
|
var ret = t1.Schema.CompareTo(t2.Schema);
|
||||||
|
if (ret == 0) ret = t1.Name.CompareTo(t2.Name);
|
||||||
|
return ret;
|
||||||
});
|
});
|
||||||
loc1.Add(loc4);
|
foreach (var loc4 in loc1) {
|
||||||
}
|
var dicUniques = new Dictionary<string, List<DbColumnInfo>>();
|
||||||
loc1.Sort((t1, t2) => {
|
if (loc4.Primarys.Count > 0) dicUniques.Add(string.Join(",", loc4.Primarys.Select(a => a.Name)), loc4.Primarys);
|
||||||
var ret = t1.Schema.CompareTo(t2.Schema);
|
foreach (var loc5 in loc4.Uniques) {
|
||||||
if (ret == 0) ret = t1.Name.CompareTo(t2.Name);
|
var dickey = string.Join(",", loc5.Select(a => a.Name));
|
||||||
return ret;
|
if (dicUniques.ContainsKey(dickey)) continue;
|
||||||
});
|
dicUniques.Add(dickey, loc5);
|
||||||
|
}
|
||||||
|
loc4.Uniques = dicUniques.Values.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
loc2.Clear();
|
loc2.Clear();
|
||||||
loc3.Clear();
|
loc3.Clear();
|
||||||
return loc1;
|
tables.AddRange(loc1);
|
||||||
|
}
|
||||||
|
return tables;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<DbEnumInfo> GetEnumsByDatabase(params string[] database) {
|
||||||
|
return new List<DbEnumInfo>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,6 +2,7 @@
|
|||||||
using FreeSql.Internal.Model;
|
using FreeSql.Internal.Model;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
namespace FreeSql.SqlServer {
|
namespace FreeSql.SqlServer {
|
||||||
@ -9,73 +10,146 @@ namespace FreeSql.SqlServer {
|
|||||||
|
|
||||||
public SqlServerExpression(CommonUtils common) : base(common) { }
|
public SqlServerExpression(CommonUtils common) : base(common) { }
|
||||||
|
|
||||||
internal override string ExpressionLambdaToSqlCall(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
internal override string ExpressionLambdaToSqlMemberAccessString(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
if (exp.Object.Type.FullName == "System.String") {
|
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName);
|
switch (exp.Member.Name) {
|
||||||
switch (exp.Method.Name) {
|
case "Length": return $"len({left})";
|
||||||
case "StartsWith":
|
|
||||||
case "EndsWith":
|
|
||||||
case "Contains":
|
|
||||||
var args0Value = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
|
|
||||||
if (args0Value == "NULL") return $"({left}) IS NULL";
|
|
||||||
if (exp.Method.Name == "StartsWith") return $"({left}) LIKE {(args0Value.StartsWith("'") ? args0Value.Insert(1, "%") : $"concat('%', {args0Value})")}";
|
|
||||||
if (exp.Method.Name == "EndsWith") return $"({left}) LIKE {(args0Value.EndsWith("'") ? args0Value.Insert(args0Value.Length - 1, "%") : $"concat({args0Value}, '%')")}";
|
|
||||||
if (args0Value.StartsWith("'") && args0Value.EndsWith("'")) return $"({left}) LIKE {args0Value.Insert(1, "%").Insert(args0Value.Length, "%")}";
|
|
||||||
return $"({left}) like concat('%', {args0Value}, '%')";
|
|
||||||
case "ToLower": return $"lower({left})";
|
|
||||||
case "ToUpper": return $"upper({left})";
|
|
||||||
case "Substring": return $"substr({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} + 1, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Length": return $"char_length({left})";
|
|
||||||
case "IndexOf":
|
|
||||||
var indexOfFindStr = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
|
|
||||||
if (exp.Arguments.Count > 1 && exp.Arguments[1].Type.FullName == "System.Int32") return $"(locate({left}, {indexOfFindStr}, ParseLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName) + 1) - 1)";
|
|
||||||
return $"(locate({left}, {indexOfFindStr}) - 1)";
|
|
||||||
case "PadLeft": return $"lpad({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "PadRight": return $"rpad({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Trim":
|
|
||||||
case "TrimStart":
|
|
||||||
case "TrimEnd":
|
|
||||||
if (exp.Arguments.Count == 0) {
|
|
||||||
if (exp.Method.Name == "Trim") return $"trim({left})";
|
|
||||||
if (exp.Method.Name == "TrimStart") return $"ltrim({left})";
|
|
||||||
if (exp.Method.Name == "TrimStart") return $"rtrim({left})";
|
|
||||||
}
|
|
||||||
foreach (var argsTrim01 in exp.Arguments) {
|
|
||||||
if (exp.Method.Name == "Trim") left = $"trim({ExpressionLambdaToSql(argsTrim01, _tables, _selectColumnMap, tbtype, isQuoteName)} from {left})";
|
|
||||||
if (exp.Method.Name == "TrimStart") left = $"trim(leading {ExpressionLambdaToSql(argsTrim01, _tables, _selectColumnMap, tbtype, isQuoteName)} from {left})";
|
|
||||||
if (exp.Method.Name == "TrimStart") left = $"trim(trailing {ExpressionLambdaToSql(argsTrim01, _tables, _selectColumnMap, tbtype, isQuoteName)} from {left})";
|
|
||||||
}
|
|
||||||
return left;
|
|
||||||
case "Replace": return $"replace({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "CompareTo": return $"strcmp({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析");
|
||||||
|
}
|
||||||
|
|
||||||
if (exp.Object.Type.FullName == "System.Math") {
|
internal override string ExpressionLambdaToSqlMemberAccessMath(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
switch (exp.Method.Name) {
|
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
case "Abs": return $"abs({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
switch (exp.Member.Name) {
|
||||||
case "Sign": return $"sign({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
case "PI": return $"pi()";
|
||||||
case "Floor": return $"floor({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Round":
|
|
||||||
if (exp.Arguments.Count > 1 && exp.Arguments[1].Type.FullName == "System.Int32") return $"round({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
return $"round({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Exp": return $"exp({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Log": return $"log({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Log10": return $"log10({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Pow": return $"pow({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Sqrt": return $"sqrt({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "PI": return $"pi()";
|
|
||||||
case "Cos": return $"cos({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Sin": return $"sin({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Tan": return $"tan({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Acos": return $"acos({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Asin": return $"asin({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Atan": return $"atan({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Atan2": return $"atan2({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
|
||||||
case "Truncate": return $"truncate({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, 0)";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析");
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override string ExpressionLambdaToSqlMemberAccessDateTime(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
|
if (exp.Expression == null && exp.Member.Name == "Now") return "getdate()";
|
||||||
|
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
switch (exp.Member.Name) {
|
||||||
|
case "DayOfWeek": return $"(datepart(weekday, {left}) - 1)";
|
||||||
|
case "Day": return $"datepart(day, {left})";
|
||||||
|
case "DayOfYear": return $"datepart(dayofyear, {left})";
|
||||||
|
case "Month": return $"datepart(month, {left})";
|
||||||
|
case "Year": return $"datepart(year, {left})";
|
||||||
|
case "Hour": return $"datepart(hour, {left})";
|
||||||
|
case "Minute": return $"datepart(minute, {left})";
|
||||||
|
case "Second": return $"datepart(second, {left})";
|
||||||
|
case "Millisecond": return $"datepart(millisecond, {left})";
|
||||||
|
case "Ticks": return $"(datediff(second, '1970-1-1', {left}) * 10000000 + 621355968000000000)";
|
||||||
|
}
|
||||||
|
throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析");
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override string ExpressionLambdaToSqlMemberAccessTimeSpan(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
|
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
switch (exp.Member.Name) {
|
||||||
|
case "Days": return $"datediff(day, '1970-1-1', {left})";
|
||||||
|
case "Hours": return $"datepart(hour, '1970-1-1', {left})";
|
||||||
|
case "Milliseconds": return $"datepart(millisecond, {left})";
|
||||||
|
case "Minutes": return $"datepart(minute, {left})";
|
||||||
|
case "Seconds": return $"datepart(second, {left})";
|
||||||
|
case "Ticks": return $"(datediff(millisecond, '1970-1-1', {left}) * 10000)";
|
||||||
|
case "TotalDays": return $"datediff(day, '1970-1-1', {left})";
|
||||||
|
case "TotalHours": return $"datediff(hour, '1970-1-1', {left})";
|
||||||
|
case "TotalMilliseconds": return $"datediff(millisecond, '1970-1-1', {left})";
|
||||||
|
case "TotalMinutes": return $"datediff(minute, '1970-1-1', {left})";
|
||||||
|
case "TotalSeconds": return $"datediff(second, '1970-1-1', {left})";
|
||||||
|
}
|
||||||
|
throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析");
|
||||||
|
}
|
||||||
|
internal override string ExpressionLambdaToSqlCallString(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
|
var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
switch (exp.Method.Name) {
|
||||||
|
case "StartsWith":
|
||||||
|
case "EndsWith":
|
||||||
|
case "Contains":
|
||||||
|
var args0Value = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
if (args0Value == "NULL") return $"({left}) IS NULL";
|
||||||
|
if (exp.Method.Name == "StartsWith") return $"({left}) LIKE {(args0Value.StartsWith("'") ? args0Value.Insert(1, "%") : $"('%' + cast({args0Value} as nvarchar))")}";
|
||||||
|
if (exp.Method.Name == "EndsWith") return $"({left}) LIKE {(args0Value.EndsWith("'") ? args0Value.Insert(args0Value.Length - 1, "%") : $"(cast({args0Value} as nvarchar) + '%')")}";
|
||||||
|
if (args0Value.StartsWith("'") && args0Value.EndsWith("'")) return $"({left}) LIKE {args0Value.Insert(1, "%").Insert(args0Value.Length, "%")}";
|
||||||
|
return $"({left}) LIKE ('%' + cast({args0Value} as nvarchar) + '%')";
|
||||||
|
case "ToLower": return $"lower({left})";
|
||||||
|
case "ToUpper": return $"upper({left})";
|
||||||
|
case "Substring":
|
||||||
|
var substrArgs1 = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
if (long.TryParse(substrArgs1, out var testtrylng1)) substrArgs1 = (testtrylng1 + 1).ToString();
|
||||||
|
else substrArgs1 += " + 1";
|
||||||
|
if (exp.Arguments.Count == 1) return $"substring({left}, {substrArgs1})";
|
||||||
|
return $"substring({left}, {substrArgs1}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "IndexOf":
|
||||||
|
var indexOfFindStr = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
if (exp.Arguments.Count > 1 && exp.Arguments[1].Type.FullName == "System.Int32") return $"(charindex({left}, {indexOfFindStr}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)} + 1) - 1)";
|
||||||
|
return $"(locate({left}, {indexOfFindStr}) - 1)";
|
||||||
|
case "PadLeft":
|
||||||
|
if (exp.Arguments.Count == 1) return $"lpad({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
return $"lpad({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "PadRight":
|
||||||
|
if (exp.Arguments.Count == 1) return $"rpad({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
return $"rpad({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Trim": return $"ltrim(rtrim({left}))";
|
||||||
|
case "TrimStart": return $"ltrim({left})";
|
||||||
|
case "TrimEnd": return $"rtrim({left})";
|
||||||
|
case "Replace": return $"replace({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "CompareTo": return $"strcmp({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
}
|
||||||
|
throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析");
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override string ExpressionLambdaToSqlCallMath(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
|
switch (exp.Method.Name) {
|
||||||
|
case "Abs": return $"abs({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Sign": return $"sign({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Floor": return $"floor({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Ceiling": return $"ceiling({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Round":
|
||||||
|
if (exp.Arguments.Count > 1 && exp.Arguments[1].Type.FullName == "System.Int32") return $"round({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
return $"round({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Exp": return $"exp({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Log": return $"log({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Log10": return $"log10({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Pow": return $"power({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Sqrt": return $"sqrt({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Cos": return $"cos({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Sin": return $"sin({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Tan": return $"tan({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Acos": return $"acos({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Asin": return $"asin({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Atan": return $"atan({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Atan2": return $"atan2({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
|
||||||
|
case "Truncate": return $"floor({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, 0)";
|
||||||
|
}
|
||||||
|
throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析");
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override string ExpressionLambdaToSqlCallDateTime(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
|
var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
var args1 = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
switch (exp.Method.Name) {
|
||||||
|
case "Add": return $"dateadd(millisecond, datediff(millisecond, '1970-1-1', {args1}), {left})";
|
||||||
|
case "AddDays": return $"dateadd(day, {args1}, {left})";
|
||||||
|
case "AddHours": return $"dateadd(hour, {args1}, {left})";
|
||||||
|
case "AddMilliseconds": return $"dateadd(millisecond, {args1}, {left})";
|
||||||
|
case "AddMinutes": return $"dateadd(minute, {args1}, {left})";
|
||||||
|
case "AddMonths": return $"dateadd(month, {args1}, {left})";
|
||||||
|
case "AddSeconds": return $"dateadd(second, {args1}, {left})";
|
||||||
|
case "AddTicks": return $"dateadd(millisecond, {args1} / 10000, {left})";
|
||||||
|
case "AddYears": return $"dateadd(year, {args1}, {left})";
|
||||||
|
case "Subtract": return $"dateadd(millisecond, -datediff(millisecond, '1970-1-1', {args1}), {left})";
|
||||||
|
}
|
||||||
|
throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析");
|
||||||
|
}
|
||||||
|
internal override string ExpressionLambdaToSqlCallTimeSpan(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||||
|
var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
var args1 = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
|
||||||
|
switch (exp.Method.Name) {
|
||||||
|
case "Add": return $"dateadd(millisecond, datediff(millisecond, '1970-1-1', {args1}), {left})";
|
||||||
|
case "Subtract": return $"dateadd(millisecond, -datediff(millisecond, '1970-1-1', {args1}), {left})";
|
||||||
|
}
|
||||||
throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析");
|
throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,5 +45,6 @@ namespace FreeSql.SqlServer {
|
|||||||
internal override string QuoteSqlName(string name) => $"[{name.TrimStart('[').TrimEnd(']').Replace(".", "].[")}]";
|
internal override string QuoteSqlName(string name) => $"[{name.TrimStart('[').TrimEnd(']').Replace(".", "].[")}]";
|
||||||
internal override string QuoteParamterName(string name) => $"@{name}";
|
internal override string QuoteParamterName(string name) => $"@{name}";
|
||||||
internal override string IsNull(string sql, object value) => $"isnull({sql}, {value})";
|
internal override string IsNull(string sql, object value) => $"isnull({sql}, {value})";
|
||||||
|
internal override string StringConcat(string left, string right, Type leftType, Type rightType) => $"{(leftType.FullName == "System.String" ? left : $"cast({left} as nvarchar)")} + {(rightType.FullName == "System.String" ? right : $"cast({right} as nvarchar)")}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,8 +57,8 @@ namespace test.Model {
|
|||||||
{/if}{/for}{for fk in fks}
|
{/if}{/for}{for fk in fks}
|
||||||
public {#UString(fk.ReferencedTable.Name)} {#GetFkObjectName(fk)} { get; set; }
|
public {#UString(fk.ReferencedTable.Name)} {#GetFkObjectName(fk)} { get; set; }
|
||||||
{/for}
|
{/for}
|
||||||
internal static IFreeSql mysql => Const.mysql;
|
internal static IFreeSql freesql => Const.mysql;
|
||||||
public static ISelect<{#UString(table.Name)}> Select => mysql.Select<{#UString(table.Name)}>();
|
public static ISelect<{#UString(table.Name)}> Select => freesql.Select<{#UString(table.Name)}>();
|
||||||
{if (table.Uniques.Count > 0)}
|
{if (table.Uniques.Count > 0)}
|
||||||
public static int ItemCacheTimeout = 180;{for uk001 in table.Uniques}{%
|
public static int ItemCacheTimeout = 180;{for uk001 in table.Uniques}{%
|
||||||
|
|
||||||
@ -79,7 +79,7 @@ namespace test.Model {
|
|||||||
parmsNodeTypeUpdateCacheRemove = parmsNodeTypeUpdateCacheRemove.Substring(0, parmsNodeTypeUpdateCacheRemove.Length - 9);
|
parmsNodeTypeUpdateCacheRemove = parmsNodeTypeUpdateCacheRemove.Substring(0, parmsNodeTypeUpdateCacheRemove.Length - 9);
|
||||||
%}
|
%}
|
||||||
public static {#UString(table.Name)} GetItem{#uk001[0].IsPrimary ? string.Empty : parmsBy}({#parms}) => Select.Where(a => {#parmsByWhereLambda}).Caching(ItemCacheTimeout, string.Concat("test:{#table.Name}{#uk001[0].IsPrimary ? string.Empty : parmsBy}:", {#parmsNodeTypeUpdateCacheRemove.Replace("item.", "")})).ToOne();{/for}
|
public static {#UString(table.Name)} GetItem{#uk001[0].IsPrimary ? string.Empty : parmsBy}({#parms}) => Select.Where(a => {#parmsByWhereLambda}).Caching(ItemCacheTimeout, string.Concat("test:{#table.Name}{#uk001[0].IsPrimary ? string.Empty : parmsBy}:", {#parmsNodeTypeUpdateCacheRemove.Replace("item.", "")})).ToOne();{/for}
|
||||||
{/if}{if (table.Uniques.Count > 0)}{for uk001 in table.Uniques}{%
|
{for uk001 in table.Uniques}{%
|
||||||
|
|
||||||
string parms = string.Empty;
|
string parms = string.Empty;
|
||||||
string parmsByWhereLambda = string.Empty;
|
string parmsByWhereLambda = string.Empty;
|
||||||
@ -101,12 +101,12 @@ namespace test.Model {
|
|||||||
parmsNoneType = parmsNoneType.Substring(0, parmsNoneType.Length - 2);
|
parmsNoneType = parmsNoneType.Substring(0, parmsNoneType.Length - 2);
|
||||||
%}
|
%}
|
||||||
public static long Delete{#uk001[0].IsPrimary ? string.Empty : parmsBy}({#parms}) {
|
public static long Delete{#uk001[0].IsPrimary ? string.Empty : parmsBy}({#parms}) {
|
||||||
var affrows = mysql.Delete<{#UString(table.Name)}>().Where(a => {#parmsByWhereLambda}).ExecuteAffrows();{if table.Uniques.Count > 1}
|
var affrows = freesql.Delete<{#UString(table.Name)}>().Where(a => {#parmsByWhereLambda}).ExecuteAffrows();{if table.Uniques.Count > 1}
|
||||||
if (ItemCacheTimeout > 0) RemoveCache(GetItem{#uk001[0].IsPrimary ? string.Empty : parmsBy}({#parmsNoneType}));{else}
|
if (ItemCacheTimeout > 0) RemoveCache(GetItem{#uk001[0].IsPrimary ? string.Empty : parmsBy}({#parmsNoneType}));{else}
|
||||||
if (ItemCacheTimeout > 0) RemoveCache(new {#UString(table.Name)} { {#parmsNewItem} });{/if}
|
if (ItemCacheTimeout > 0) RemoveCache(new {#UString(table.Name)} { {#parmsNewItem} });{/if}
|
||||||
return affrows;
|
return affrows;
|
||||||
}{/for}
|
}{/for}
|
||||||
{/if}{for fkcoldel in fks}{%
|
{for fkcoldel in fks}{%
|
||||||
string parms = string.Empty;
|
string parms = string.Empty;
|
||||||
string parmsBy = "By";
|
string parmsBy = "By";
|
||||||
string parmsByWhereLambda = string.Empty;
|
string parmsByWhereLambda = string.Empty;
|
||||||
@ -121,9 +121,9 @@ namespace test.Model {
|
|||||||
parmsByWhereLambda = parmsByWhereLambda.Substring(0, parmsByWhereLambda.Length - 4);
|
parmsByWhereLambda = parmsByWhereLambda.Substring(0, parmsByWhereLambda.Length - 4);
|
||||||
%}
|
%}
|
||||||
public static long DeleteBy{#parmsBy}({#parms}) {
|
public static long DeleteBy{#parmsBy}({#parms}) {
|
||||||
return mysql.Delete<{#UString(table.Name)}>().Where(a => {#parmsByWhereLambda}).ExecuteAffrows(); //删除缓存
|
return freesql.Delete<{#UString(table.Name)}>().Where(a => {#parmsByWhereLambda}).ExecuteAffrows(); //删除缓存
|
||||||
}
|
}
|
||||||
{/for}{if (table.Uniques.Count > 0)}{% string redisRemoveCode = string.Empty; %}{for uk001 in table.Uniques}{%
|
{/for}{% string redisRemoveCode = string.Empty; %}{for uk001 in table.Uniques}{%
|
||||||
|
|
||||||
string parmsBy = "By";
|
string parmsBy = "By";
|
||||||
string parmsNodeTypeUpdateCacheRemove = string.Empty;
|
string parmsNodeTypeUpdateCacheRemove = string.Empty;
|
||||||
@ -143,10 +143,10 @@ namespace test.Model {
|
|||||||
var keysIdx = 0;
|
var keysIdx = 0;
|
||||||
foreach (var item in items) {{#redisRemoveCode}
|
foreach (var item in items) {{#redisRemoveCode}
|
||||||
}
|
}
|
||||||
if (mysql.Ado.TransactionCurrentThread != null) mysql.Ado.TransactionPreRemoveCache(keys);
|
if (freesql.Ado.TransactionCurrentThread != null) freesql.Ado.TransactionPreRemoveCache(keys);
|
||||||
else mysql.Cache.Remove(keys);
|
else freesql.Cache.Remove(keys);
|
||||||
}
|
}
|
||||||
{/if}{if (table.Columns.Count < 100)}{%
|
{if (table.Columns.Count < 100)}{%
|
||||||
string CsParam3 = string.Empty;
|
string CsParam3 = string.Empty;
|
||||||
string CsParamNoType3 = string.Empty;
|
string CsParamNoType3 = string.Empty;
|
||||||
string parms = string.Empty;
|
string parms = string.Empty;
|
||||||
@ -186,54 +186,55 @@ namespace test.Model {
|
|||||||
if (CsParamNoType3.Length > 0) CsParamNoType3 = CsParamNoType3.Substring(0, CsParamNoType3.Length - 2);
|
if (CsParamNoType3.Length > 0) CsParamNoType3 = CsParamNoType3.Substring(0, CsParamNoType3.Length - 2);
|
||||||
%}{if idens > 0}
|
%}{if idens > 0}
|
||||||
public static {#GetCsType(idensCol)} Insert({#UString(table.Name)} item) {
|
public static {#GetCsType(idensCol)} Insert({#UString(table.Name)} item) {
|
||||||
item.{#UString(idensCol.Name)} = ({#GetCsType(idensCol)})mysql.Insert<{#UString(table.Name)}>().AppendData(item).ExecuteIdentity();
|
item.{#UString(idensCol.Name)} = ({#GetCsType(idensCol)})freesql.Insert<{#UString(table.Name)}>().AppendData(item).ExecuteIdentity();
|
||||||
if (ItemCacheTimeout > 0) RemoveCache(item);
|
if (ItemCacheTimeout > 0) RemoveCache(item);
|
||||||
return item.{#UString(idensCol.Name)};
|
return item.{#UString(idensCol.Name)};
|
||||||
}{else}
|
}{else}
|
||||||
public static void Insert({#UString(table.Name)} item) {
|
public static void Insert({#UString(table.Name)} item) {
|
||||||
if (ItemCacheTimeout > 0) RemoveCache(item);
|
if (ItemCacheTimeout > 0) RemoveCache(item);
|
||||||
mysql.Insert<{#UString(table.Name)}>().AppendData(item).ExecuteAffrows();
|
freesql.Insert<{#UString(table.Name)}>().AppendData(item).ExecuteAffrows();
|
||||||
}
|
}
|
||||||
public static long Insert(IEnumerable<{#UString(table.Name)}> items) {
|
public static long Insert(IEnumerable<{#UString(table.Name)}> items) {
|
||||||
if (ItemCacheTimeout > 0) RemoveCache(items);
|
if (ItemCacheTimeout > 0) RemoveCache(items);
|
||||||
return mysql.Insert<{#UString(table.Name)}>().AppendData(items).ExecuteAffrows();
|
return freesql.Insert<{#UString(table.Name)}>().AppendData(items).ExecuteAffrows();
|
||||||
}{/if}
|
}{/if}
|
||||||
{if CsParamNoType3.Split('=').Length <= 5}{if idens > 0}
|
{if CsParamNoType3.Split('=').Length <= 5}{if idens > 0}
|
||||||
public static {#GetCsType(idensCol)} Insert({#CsParam3}) {
|
public static {#GetCsType(idensCol)} Insert({#CsParam3}) {
|
||||||
var item = new {#UString(table.Name)} {{#CsParamNoType3}};
|
var item = new {#UString(table.Name)} {{#CsParamNoType3}};
|
||||||
item.{#UString(idensCol.Name)} = ({#GetCsType(idensCol)})mysql.Insert<{#UString(table.Name)}>().AppendData(new {#UString(table.Name)} {{#CsParamNoType3}}).ExecuteIdentity();
|
item.{#UString(idensCol.Name)} = ({#GetCsType(idensCol)})freesql.Insert<{#UString(table.Name)}>().AppendData(new {#UString(table.Name)} {{#CsParamNoType3}}).ExecuteIdentity();
|
||||||
if (ItemCacheTimeout > 0) RemoveCache(item);
|
if (ItemCacheTimeout > 0) RemoveCache(item);
|
||||||
return item.{#UString(idensCol.Name)};
|
return item.{#UString(idensCol.Name)};
|
||||||
}{else}
|
}{else}
|
||||||
public static {#UString(table.Name)} Insert({#CsParam3}) {
|
public static {#UString(table.Name)} Insert({#CsParam3}) {
|
||||||
var item = new {#UString(table.Name)} {{#CsParamNoType3}};
|
var item = new {#UString(table.Name)} {{#CsParamNoType3}};
|
||||||
mysql.Insert<{#UString(table.Name)}>().AppendData(item).ExecuteAffrows();
|
freesql.Insert<{#UString(table.Name)}>().AppendData(item).ExecuteAffrows();
|
||||||
if (ItemCacheTimeout > 0) RemoveCache(item);
|
if (ItemCacheTimeout > 0) RemoveCache(item);
|
||||||
return item;
|
return item;
|
||||||
}{/if}{/if}
|
}{/if}{/if}
|
||||||
public static long Update({#UString(table.Name)} item) {
|
public static long Update({#UString(table.Name)} item) {
|
||||||
if (ItemCacheTimeout > 0) RemoveCache(item);
|
if (ItemCacheTimeout > 0) RemoveCache(item);
|
||||||
return mysql.Update<{#UString(table.Name)}>().SetSource(item).ExecuteAffrows();
|
return freesql.Update<{#UString(table.Name)}>().SetSource(item).ExecuteAffrows();
|
||||||
}
|
}
|
||||||
public static long Update(IEnumerable<{#UString(table.Name)}> items) {
|
public static long Update(IEnumerable<{#UString(table.Name)}> items) {
|
||||||
if (ItemCacheTimeout > 0) RemoveCache(items);
|
if (ItemCacheTimeout > 0) RemoveCache(items);
|
||||||
return mysql.Update<{#UString(table.Name)}>().SetSource(items).ExecuteAffrows();
|
return freesql.Update<{#UString(table.Name)}>().SetSource(items).ExecuteAffrows();
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 指定字段更新
|
/// 指定字段更新
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IUpdate<{#UString(table.Name)}> UpdateDiy => mysql.Update<{#UString(table.Name)}>().Where(a => {#parmsByWhereLambda});
|
public IUpdate<{#UString(table.Name)}> UpdateDiy => freesql.Update<{#UString(table.Name)}>().Where(a => {#parmsByWhereLambda});
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// {if pks.Count > 0}保存或添加,如果主键有值则尝试 Update,如果影响的行为 0 则尝试 Insert{else}添加{/if}
|
/// {if pks.Count > 0}保存或添加,如果主键有值则尝试 Update,如果影响的行为 0 则尝试 Insert{else}添加{/if}
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Save() {{if pks.Count > 0}
|
public void Save() {{if pks.Count > 0}
|
||||||
if ({#string.Join(" && ", pks.Select(pkssa => "this." + UString(pkssa.Name) + " != default(" + GetCsType(pkssa) + ")"))}) {
|
if ({#string.Join(" && ", pks.Select(pkssa => "this." + UString(pkssa.Name) + " != default(" + GetCsType(pkssa) + ")"))}) {
|
||||||
var affrows = mysql.Update<{#UString(table.Name)}>().Where(a => {#parmsByWhereLambda}).ExecuteAffrows();
|
var affrows = freesql.Update<{#UString(table.Name)}>().Where(a => {#parmsByWhereLambda}).ExecuteAffrows();
|
||||||
if (affrows > 0) return;
|
if (affrows > 0) return;
|
||||||
}{/if}
|
}{/if}
|
||||||
{if pks.Count > 0}this.{#UString(idensCol.Name)} = {#GetCsType(idensCol).Replace("?", "") == "long" ? "" : ("(" + GetCsType(idensCol) + ")")}mysql.Insert<{#UString(table.Name)}>().AppendData(this).ExecuteIdentity();{else}mysql.Insert<{#UString(table.Name)}>().AppendData(this).ExecuteAffrows();{/if}
|
{if idens > 0}this.{#UString(idensCol.Name)} = {#GetCsType(idensCol).Replace("?", "") == "long" ? "" : ("(" + GetCsType(idensCol) + ")")}freesql.Insert<{#UString(table.Name)}>().AppendData(this).ExecuteIdentity();{else}
|
||||||
|
freesql.Insert<{#UString(table.Name)}>().AppendData(this).ExecuteAffrows();{/if}
|
||||||
}
|
}
|
||||||
{/if}
|
{/if}{/if}
|
||||||
}
|
}
|
||||||
{include ../../include/enumtype.tpl}
|
{include ../../include/enumtype.tpl}
|
||||||
}
|
}
|
@ -0,0 +1,94 @@
|
|||||||
|
# 生成器
|
||||||
|
|
||||||
|
生成器是基于 dbfirst 开发的辅助工具,适用老项目一键生成实体。生成器采用模板的方式,作者实现了三种生成模板:
|
||||||
|
|
||||||
|
| 模板名称 | 类型映射 | 外键导航属性 | 缓存管理 | 失血 | 贫血 | 充血 |
|
||||||
|
| ------------- | - | - |- | - |- | - |
|
||||||
|
| simple-entity | √ | X | X | √ | X | X |
|
||||||
|
| simple-entity-navigation-object | √ | √ | X | √ | X | X |
|
||||||
|
| rich-entity-navigation-object | √ | √ | √ | X | √ | X |
|
||||||
|
|
||||||
|
模板在项目目录:/Templates/MySql
|
||||||
|
|
||||||
|
> 更多模板逐步开发中。。。
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
//定义 mysql FreeSql
|
||||||
|
var mysql = new FreeSql.FreeSqlBuilder()
|
||||||
|
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
//创建模板生成类现实
|
||||||
|
var gen = new FreeSql.Generator.TemplateGenerator();
|
||||||
|
gen.Build(mysql.DbFirst,
|
||||||
|
@"C:\Users\28810\Desktop\github\FreeSql\Templates\MySql\simple-entity", //模板目录(事先下载)
|
||||||
|
@"C:\Users\28810\Desktop\新建文件夹 (9)", //生成后保存的目录
|
||||||
|
"cccddd" //数据库
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
## 模板语法
|
||||||
|
|
||||||
|
```html
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>{#title}</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<!--绑定表达式-->
|
||||||
|
{#表达式}
|
||||||
|
{##表达式} 当表达式可能发生runtime错误时使用,性能没有上面的高
|
||||||
|
|
||||||
|
<!--可嵌套使用,同一标签最多支持3个指令-->
|
||||||
|
{include ../header.html}
|
||||||
|
<div @for="i 1, 101">
|
||||||
|
<p @if="i === 50" @for="item,index in data">aaa</p>
|
||||||
|
<p @else="i % 3 === 0">bbb {#i}</p>
|
||||||
|
<p @else="">ccc {#i}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!--定义模块,可以将公共模块定义到一个文件中-->
|
||||||
|
{module module_name1 parms1, 2, 3...}
|
||||||
|
{/module}
|
||||||
|
{module module_name2 parms1, 2, 3...}
|
||||||
|
{/module}
|
||||||
|
|
||||||
|
<!--使用模块-->
|
||||||
|
{import ../module.html as myname}
|
||||||
|
{#myname.module_name(parms1, 2, 3...)}
|
||||||
|
|
||||||
|
<!--继承-->
|
||||||
|
{extends ../inc/layout.html}
|
||||||
|
{block body}{/block}
|
||||||
|
|
||||||
|
<!--嵌入代码块-->
|
||||||
|
{%
|
||||||
|
for (var a = 0; a < 100; a++)
|
||||||
|
print(a);
|
||||||
|
%}
|
||||||
|
|
||||||
|
<!--条件分支-->
|
||||||
|
{if i === 50}
|
||||||
|
{elseif i > 60}
|
||||||
|
{else}
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<!--三种循环-->
|
||||||
|
{for i 1,101} 可自定义名 {for index2 表达式1 in 表达式2}
|
||||||
|
|
||||||
|
{for item,index in items} 可选参数称 index
|
||||||
|
可自定义名 {for item2, index99 in 数组表达式}
|
||||||
|
|
||||||
|
{for key,item,index on json} 可选参数 item, index,
|
||||||
|
可自定义名 {for key2, item2, index99 in 对象表达式}
|
||||||
|
{/for}
|
||||||
|
|
||||||
|
<!--不被解析-->
|
||||||
|
{miss}
|
||||||
|
此块内容不被bmw.js解析
|
||||||
|
{/miss}
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
```
|
@ -0,0 +1,94 @@
|
|||||||
|
# 生成器
|
||||||
|
|
||||||
|
生成器是基于 dbfirst 开发的辅助工具,适用老项目一键生成实体。生成器采用模板的方式,作者实现了三种生成模板:
|
||||||
|
|
||||||
|
| 模板名称 | 类型映射 | 外键导航属性 | 缓存管理 | 失血 | 贫血 | 充血 |
|
||||||
|
| ------------- | - | - |- | - |- | - |
|
||||||
|
| simple-entity | √ | X | X | √ | X | X |
|
||||||
|
| simple-entity-navigation-object | √ | √ | X | √ | X | X |
|
||||||
|
| rich-entity-navigation-object | √ | √ | √ | X | √ | X |
|
||||||
|
|
||||||
|
模板在项目目录:/Templates/MySql
|
||||||
|
|
||||||
|
> 更多模板逐步开发中。。。
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
//定义 mysql FreeSql
|
||||||
|
var mysql = new FreeSql.FreeSqlBuilder()
|
||||||
|
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
//创建模板生成类现实
|
||||||
|
var gen = new FreeSql.Generator.TemplateGenerator();
|
||||||
|
gen.Build(mysql.DbFirst,
|
||||||
|
@"C:\Users\28810\Desktop\github\FreeSql\Templates\MySql\simple-entity", //模板目录(事先下载)
|
||||||
|
@"C:\Users\28810\Desktop\新建文件夹 (9)", //生成后保存的目录
|
||||||
|
"cccddd" //数据库
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
## 模板语法
|
||||||
|
|
||||||
|
```html
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>{#title}</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<!--绑定表达式-->
|
||||||
|
{#表达式}
|
||||||
|
{##表达式} 当表达式可能发生runtime错误时使用,性能没有上面的高
|
||||||
|
|
||||||
|
<!--可嵌套使用,同一标签最多支持3个指令-->
|
||||||
|
{include ../header.html}
|
||||||
|
<div @for="i 1, 101">
|
||||||
|
<p @if="i === 50" @for="item,index in data">aaa</p>
|
||||||
|
<p @else="i % 3 === 0">bbb {#i}</p>
|
||||||
|
<p @else="">ccc {#i}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!--定义模块,可以将公共模块定义到一个文件中-->
|
||||||
|
{module module_name1 parms1, 2, 3...}
|
||||||
|
{/module}
|
||||||
|
{module module_name2 parms1, 2, 3...}
|
||||||
|
{/module}
|
||||||
|
|
||||||
|
<!--使用模块-->
|
||||||
|
{import ../module.html as myname}
|
||||||
|
{#myname.module_name(parms1, 2, 3...)}
|
||||||
|
|
||||||
|
<!--继承-->
|
||||||
|
{extends ../inc/layout.html}
|
||||||
|
{block body}{/block}
|
||||||
|
|
||||||
|
<!--嵌入代码块-->
|
||||||
|
{%
|
||||||
|
for (var a = 0; a < 100; a++)
|
||||||
|
print(a);
|
||||||
|
%}
|
||||||
|
|
||||||
|
<!--条件分支-->
|
||||||
|
{if i === 50}
|
||||||
|
{elseif i > 60}
|
||||||
|
{else}
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<!--三种循环-->
|
||||||
|
{for i 1,101} 可自定义名 {for index2 表达式1 in 表达式2}
|
||||||
|
|
||||||
|
{for item,index in items} 可选参数称 index
|
||||||
|
可自定义名 {for item2, index99 in 数组表达式}
|
||||||
|
|
||||||
|
{for key,item,index on json} 可选参数 item, index,
|
||||||
|
可自定义名 {for key2, item2, index99 in 对象表达式}
|
||||||
|
{/for}
|
||||||
|
|
||||||
|
<!--不被解析-->
|
||||||
|
{miss}
|
||||||
|
此块内容不被bmw.js解析
|
||||||
|
{/miss}
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
```
|
@ -0,0 +1,94 @@
|
|||||||
|
# 生成器
|
||||||
|
|
||||||
|
生成器是基于 dbfirst 开发的辅助工具,适用老项目一键生成实体。生成器采用模板的方式,作者实现了三种生成模板:
|
||||||
|
|
||||||
|
| 模板名称 | 类型映射 | 外键导航属性 | 缓存管理 | 失血 | 贫血 | 充血 |
|
||||||
|
| ------------- | - | - |- | - |- | - |
|
||||||
|
| simple-entity | √ | X | X | √ | X | X |
|
||||||
|
| simple-entity-navigation-object | √ | √ | X | √ | X | X |
|
||||||
|
| rich-entity-navigation-object | √ | √ | √ | X | √ | X |
|
||||||
|
|
||||||
|
模板在项目目录:/Templates/MySql
|
||||||
|
|
||||||
|
> 更多模板逐步开发中。。。
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
//定义 mysql FreeSql
|
||||||
|
var mysql = new FreeSql.FreeSqlBuilder()
|
||||||
|
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
//创建模板生成类现实
|
||||||
|
var gen = new FreeSql.Generator.TemplateGenerator();
|
||||||
|
gen.Build(mysql.DbFirst,
|
||||||
|
@"C:\Users\28810\Desktop\github\FreeSql\Templates\MySql\simple-entity", //模板目录(事先下载)
|
||||||
|
@"C:\Users\28810\Desktop\新建文件夹 (9)", //生成后保存的目录
|
||||||
|
"cccddd" //数据库
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
## 模板语法
|
||||||
|
|
||||||
|
```html
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>{#title}</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<!--绑定表达式-->
|
||||||
|
{#表达式}
|
||||||
|
{##表达式} 当表达式可能发生runtime错误时使用,性能没有上面的高
|
||||||
|
|
||||||
|
<!--可嵌套使用,同一标签最多支持3个指令-->
|
||||||
|
{include ../header.html}
|
||||||
|
<div @for="i 1, 101">
|
||||||
|
<p @if="i === 50" @for="item,index in data">aaa</p>
|
||||||
|
<p @else="i % 3 === 0">bbb {#i}</p>
|
||||||
|
<p @else="">ccc {#i}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!--定义模块,可以将公共模块定义到一个文件中-->
|
||||||
|
{module module_name1 parms1, 2, 3...}
|
||||||
|
{/module}
|
||||||
|
{module module_name2 parms1, 2, 3...}
|
||||||
|
{/module}
|
||||||
|
|
||||||
|
<!--使用模块-->
|
||||||
|
{import ../module.html as myname}
|
||||||
|
{#myname.module_name(parms1, 2, 3...)}
|
||||||
|
|
||||||
|
<!--继承-->
|
||||||
|
{extends ../inc/layout.html}
|
||||||
|
{block body}{/block}
|
||||||
|
|
||||||
|
<!--嵌入代码块-->
|
||||||
|
{%
|
||||||
|
for (var a = 0; a < 100; a++)
|
||||||
|
print(a);
|
||||||
|
%}
|
||||||
|
|
||||||
|
<!--条件分支-->
|
||||||
|
{if i === 50}
|
||||||
|
{elseif i > 60}
|
||||||
|
{else}
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<!--三种循环-->
|
||||||
|
{for i 1,101} 可自定义名 {for index2 表达式1 in 表达式2}
|
||||||
|
|
||||||
|
{for item,index in items} 可选参数称 index
|
||||||
|
可自定义名 {for item2, index99 in 数组表达式}
|
||||||
|
|
||||||
|
{for key,item,index on json} 可选参数 item, index,
|
||||||
|
可自定义名 {for key2, item2, index99 in 对象表达式}
|
||||||
|
{/for}
|
||||||
|
|
||||||
|
<!--不被解析-->
|
||||||
|
{miss}
|
||||||
|
此块内容不被bmw.js解析
|
||||||
|
{/miss}
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
```
|
@ -0,0 +1,5 @@
|
|||||||
|
public static class Const {
|
||||||
|
public static IFreeSql sqlserver = new FreeSql.FreeSqlBuilder()
|
||||||
|
.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
|
||||||
|
.Build();
|
||||||
|
}
|
@ -0,0 +1,240 @@
|
|||||||
|
{%if (table.Type == DbTableType.StoreProcedure) {
|
||||||
|
print("return;");
|
||||||
|
return rTn;
|
||||||
|
}
|
||||||
|
%}using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using FreeSql;
|
||||||
|
using FreeSql.DataAnnotations;
|
||||||
|
{%
|
||||||
|
var dbf = dbfirst as FreeSql.IDbFirst;
|
||||||
|
var cols = (table.Columns as List<DbColumnInfo>);
|
||||||
|
var pks = (table.Primarys as List<DbColumnInfo>);
|
||||||
|
var fks = (table.Foreigns as List<DbForeignInfo>);
|
||||||
|
|
||||||
|
Func<string, string> UString = stra => stra.Substring(0, 1).ToUpper() + stra.Substring(1);
|
||||||
|
Func<DbColumnInfo, string> GetCsType = cola3 => {
|
||||||
|
return dbf.GetCsType(cola3);
|
||||||
|
};
|
||||||
|
Func<DbForeignInfo, string> GetFkObjectName = fkx => {
|
||||||
|
var eqfks = fks.Where(fk22a => fk22a.ReferencedTable.Name == fkx.ReferencedTable.Name);
|
||||||
|
if (eqfks.Count() == 1) return "Obj_" + fkx.ReferencedTable.Name;
|
||||||
|
var fkretname = fkx.Columns[0].Name;
|
||||||
|
if (fkretname.EndsWith(fkx.ReferencedColumns[0].Name, StringComparison.CurrentCultureIgnoreCase)) fkretname = fkretname.Substring(0, fkretname.Length - fkx.ReferencedColumns[0].Name.Length).TrimEnd('_');
|
||||||
|
if (fkretname.EndsWith(fkx.ReferencedTable.Name, StringComparison.CurrentCultureIgnoreCase)) fkretname = fkretname.Substring(0, fkretname.Length - fkx.ReferencedTable.Name.Length).TrimEnd('_');
|
||||||
|
if (fkretname.StartsWith(fkx.ReferencedTable.Name, StringComparison.CurrentCultureIgnoreCase)) fkretname = fkretname.Substring(fkx.ReferencedTable.Name.Length).TrimStart('_');
|
||||||
|
return "Obj_" + fkx.ReferencedTable.Name + (string.IsNullOrEmpty(fkretname) ? "" : ("_" + fkretname));
|
||||||
|
};
|
||||||
|
|
||||||
|
%}
|
||||||
|
namespace test.Model {
|
||||||
|
|
||||||
|
[JsonObject(MemberSerialization.OptIn), Table(Name = "{#!string.IsNullOrEmpty(table.Schema) ? table.Schema + "." : ""}{#table.Name}"{if cols.Where(cola003 => cola003.Name.ToLower() == "is_deleted" || cola003.Name.ToLower() == "isdeleted").Any()}, SelectFilter = "a.IsDeleted = 1"{/if})]
|
||||||
|
public partial class {#UString(table.Name)} {{for col,index in table.Columns}{%
|
||||||
|
var findfks = fks.Where(fkaa => fkaa.Columns.Where(fkaac1 => fkaac1.Name == col.Name).Any());
|
||||||
|
%}{if findfks.Any() == false}
|
||||||
|
{if string.IsNullOrEmpty(col.Coment) == false}/// <summary>
|
||||||
|
/// {#col.Coment.Replace("\r\n", "\n").Replace("\n", "\r\n /// ")}
|
||||||
|
/// </summary>{/if}
|
||||||
|
[JsonProperty, Column(Name = "{#col.Name}", DbType = "{#col.DbTypeTextFull}"{if col.IsPrimary == true}, IsPrimary = true{/if}{if col.IsIdentity == true}, IsIdentity = true{/if}{if col.IsNullable == true}, IsNullable = true{/if})]
|
||||||
|
public {#GetCsType(col)} {#UString(col.Name)} { get; set; }
|
||||||
|
{else}
|
||||||
|
private {#GetCsType(col)} _{#UString(col.Name)};
|
||||||
|
{if string.IsNullOrEmpty(col.Coment) == false}/// <summary>
|
||||||
|
/// {#col.Coment.Replace("\r\n", "\n").Replace("\n", "\r\n /// ")}
|
||||||
|
/// </summary>{/if}
|
||||||
|
[JsonProperty, Column(Name = "{#col.Name}", DbType = "{#col.DbTypeTextFull}"{if col.IsPrimary == true}, IsPrimary = true{/if}{if col.IsIdentity == true}, IsIdentity = true{/if}{if col.IsNullable == true}, IsNullable = true{/if})]
|
||||||
|
public {#GetCsType(col)} {#UString(col.Name)} { get => _{#UString(col.Name)}; set {
|
||||||
|
if (_{#UString(col.Name)} == value) return;
|
||||||
|
_{#UString(col.Name)} = value;{for fkcok2 in findfks}
|
||||||
|
{#GetFkObjectName(fkcok2)} = null;{/for}
|
||||||
|
} }
|
||||||
|
{/if}{/for}{for fk in fks}
|
||||||
|
public {#UString(fk.ReferencedTable.Name)} {#GetFkObjectName(fk)} { get; set; }
|
||||||
|
{/for}
|
||||||
|
internal static IFreeSql freesql => Const.sqlserver;
|
||||||
|
public static ISelect<{#UString(table.Name)}> Select => freesql.Select<{#UString(table.Name)}>();
|
||||||
|
{if (table.Uniques.Count > 0)}
|
||||||
|
public static int ItemCacheTimeout = 180;{for uk001 in table.Uniques}{%
|
||||||
|
|
||||||
|
string parms = string.Empty;
|
||||||
|
string parmsByWhereLambda = string.Empty;
|
||||||
|
string parmsBy = "By";
|
||||||
|
string parmsNodeTypeUpdateCacheRemove = string.Empty;
|
||||||
|
foreach (var uk001col in uk001) {
|
||||||
|
string getcstype = GetCsType(uk001col);
|
||||||
|
parms += getcstype.Replace("?", "") + " " + UString(uk001col.Name) + ", ";
|
||||||
|
parmsByWhereLambda += "a." + UString(uk001col.Name) + " == " + UString(uk001col.Name) + " && ";
|
||||||
|
parmsBy += UString(uk001col.Name) + "And";
|
||||||
|
parmsNodeTypeUpdateCacheRemove += "item." + UString(uk001col.Name) + ", \"_,_\", ";
|
||||||
|
}
|
||||||
|
parms = parms.Substring(0, parms.Length - 2);
|
||||||
|
parmsByWhereLambda = parmsByWhereLambda.Substring(0, parmsByWhereLambda.Length - 4);
|
||||||
|
parmsBy = parmsBy.Substring(0, parmsBy.Length - 3);
|
||||||
|
parmsNodeTypeUpdateCacheRemove = parmsNodeTypeUpdateCacheRemove.Substring(0, parmsNodeTypeUpdateCacheRemove.Length - 9);
|
||||||
|
%}
|
||||||
|
public static {#UString(table.Name)} GetItem{#uk001[0].IsPrimary ? string.Empty : parmsBy}({#parms}) => Select.Where(a => {#parmsByWhereLambda}).Caching(ItemCacheTimeout, string.Concat("test:{#table.Name}{#uk001[0].IsPrimary ? string.Empty : parmsBy}:", {#parmsNodeTypeUpdateCacheRemove.Replace("item.", "")})).ToOne();{/for}
|
||||||
|
{for uk001 in table.Uniques}{%
|
||||||
|
|
||||||
|
string parms = string.Empty;
|
||||||
|
string parmsByWhereLambda = string.Empty;
|
||||||
|
string parmsNewItem = string.Empty;
|
||||||
|
string parmsBy = "By";
|
||||||
|
string parmsNoneType = string.Empty;
|
||||||
|
foreach (var uk001col in uk001) {
|
||||||
|
string getcstype = GetCsType(uk001col);
|
||||||
|
parms += getcstype.Replace("?", "") + " " + UString(uk001col.Name) + ", ";
|
||||||
|
parmsByWhereLambda += "a." + UString(uk001col.Name) + " == " + UString(uk001col.Name) + " && ";
|
||||||
|
parmsNewItem += UString(uk001col.Name) + " = " + UString(uk001col.Name) + ", ";
|
||||||
|
parmsBy += UString(uk001col.Name) + "And";
|
||||||
|
parmsNoneType += UString(uk001col.Name) + ", ";
|
||||||
|
}
|
||||||
|
parms = parms.Substring(0, parms.Length - 2);
|
||||||
|
parmsByWhereLambda = parmsByWhereLambda.Substring(0, parmsByWhereLambda.Length - 4);
|
||||||
|
parmsNewItem = parmsNewItem.Substring(0, parmsNewItem.Length - 2);
|
||||||
|
parmsBy = parmsBy.Substring(0, parmsBy.Length - 3);
|
||||||
|
parmsNoneType = parmsNoneType.Substring(0, parmsNoneType.Length - 2);
|
||||||
|
%}
|
||||||
|
public static long Delete{#uk001[0].IsPrimary ? string.Empty : parmsBy}({#parms}) {
|
||||||
|
var affrows = freesql.Delete<{#UString(table.Name)}>().Where(a => {#parmsByWhereLambda}).ExecuteAffrows();{if table.Uniques.Count > 1}
|
||||||
|
if (ItemCacheTimeout > 0) RemoveCache(GetItem{#uk001[0].IsPrimary ? string.Empty : parmsBy}({#parmsNoneType}));{else}
|
||||||
|
if (ItemCacheTimeout > 0) RemoveCache(new {#UString(table.Name)} { {#parmsNewItem} });{/if}
|
||||||
|
return affrows;
|
||||||
|
}{/for}
|
||||||
|
{for fkcoldel in fks}{%
|
||||||
|
string parms = string.Empty;
|
||||||
|
string parmsBy = "By";
|
||||||
|
string parmsByWhereLambda = string.Empty;
|
||||||
|
foreach( var fkcoldelcol in fkcoldel.Columns) {
|
||||||
|
string getcstype = GetCsType(fkcoldelcol);
|
||||||
|
parms += getcstype.Replace("?", "") + " " + UString(fkcoldelcol.Name) + ", ";
|
||||||
|
parmsBy += UString(fkcoldelcol.Name) + "And";
|
||||||
|
parmsByWhereLambda += "a." + UString(fkcoldelcol.Name) + " == " + UString(fkcoldelcol.Name) + " && ";
|
||||||
|
}
|
||||||
|
parms = parms.Substring(0, parms.Length - 2);
|
||||||
|
parmsBy = parmsBy.Substring(0, parmsBy.Length - 3);
|
||||||
|
parmsByWhereLambda = parmsByWhereLambda.Substring(0, parmsByWhereLambda.Length - 4);
|
||||||
|
%}
|
||||||
|
public static long DeleteBy{#parmsBy}({#parms}) {
|
||||||
|
return freesql.Delete<{#UString(table.Name)}>().Where(a => {#parmsByWhereLambda}).ExecuteAffrows(); //删除缓存
|
||||||
|
}
|
||||||
|
{/for}{% string redisRemoveCode = string.Empty; %}{for uk001 in table.Uniques}{%
|
||||||
|
|
||||||
|
string parmsBy = "By";
|
||||||
|
string parmsNodeTypeUpdateCacheRemove = string.Empty;
|
||||||
|
foreach (var uk001col in uk001) {
|
||||||
|
parmsBy += UString(uk001col.Name) + "And";
|
||||||
|
parmsNodeTypeUpdateCacheRemove += "item." + UString(uk001col.Name) + ", \"_,_\", ";
|
||||||
|
}
|
||||||
|
parmsBy = parmsBy.Substring(0, parmsBy.Length - 3);
|
||||||
|
parmsNodeTypeUpdateCacheRemove = parmsNodeTypeUpdateCacheRemove.Substring(0, parmsNodeTypeUpdateCacheRemove.Length - 9);
|
||||||
|
redisRemoveCode += $@"
|
||||||
|
keys[keysIdx++] = string.Concat(""test:{table.Name}{(uk001[0].IsPrimary ? string.Empty : parmsBy)}:"", {parmsNodeTypeUpdateCacheRemove});";
|
||||||
|
%}{/for}
|
||||||
|
internal static void RemoveCache({#UString(table.Name)} item) => RemoveCache(item == null ? null : new [] { item });
|
||||||
|
internal static void RemoveCache(IEnumerable<{#UString(table.Name)}> items) {
|
||||||
|
if (ItemCacheTimeout <= 0 || items == null || items.Any() == false) return;
|
||||||
|
var keys = new string[items.Count() * {#table.Uniques.Count}];
|
||||||
|
var keysIdx = 0;
|
||||||
|
foreach (var item in items) {{#redisRemoveCode}
|
||||||
|
}
|
||||||
|
if (freesql.Ado.TransactionCurrentThread != null) freesql.Ado.TransactionPreRemoveCache(keys);
|
||||||
|
else freesql.Cache.Remove(keys);
|
||||||
|
}
|
||||||
|
{if (table.Columns.Count < 100)}{%
|
||||||
|
string CsParam3 = string.Empty;
|
||||||
|
string CsParamNoType3 = string.Empty;
|
||||||
|
string parms = string.Empty;
|
||||||
|
string parmsByWhereLambda = string.Empty;
|
||||||
|
string parmsNewItem = string.Empty;
|
||||||
|
string parmsBy = "By";
|
||||||
|
string parmsNoneType = string.Empty;
|
||||||
|
var idens = 0;
|
||||||
|
var idensCol = table.Columns[0];
|
||||||
|
foreach (var uk001col in table.Columns) {
|
||||||
|
string getcstype = GetCsType(uk001col);
|
||||||
|
parms += getcstype.Replace("?", "") + " " + UString(uk001col.Name) + ", ";
|
||||||
|
if (uk001col.IsPrimary)
|
||||||
|
parmsByWhereLambda += "a." + UString(uk001col.Name) + " == " + UString(uk001col.Name) + " && ";
|
||||||
|
parmsNewItem += UString(uk001col.Name) + " = " + UString(uk001col.Name) + ", ";
|
||||||
|
parmsBy += UString(uk001col.Name) + "And";
|
||||||
|
parmsNoneType += UString(uk001col.Name) + ", ";
|
||||||
|
if (uk001col.IsIdentity) {
|
||||||
|
idens++;
|
||||||
|
idensCol = uk001col;
|
||||||
|
} else {
|
||||||
|
if (getcstype.StartsWith("DateTime") && (uk001col.Name.ToLower() == "create_time" || uk001col.Name.ToLower() == "update_time" || uk001col.Name.ToLower() == "createtime" || uk001col.Name.ToLower() == "updatetime") ||
|
||||||
|
getcstype == "bool?" && (uk001col.Name.ToLower() == "is_deleted" || uk001col.Name.ToLower() == "isdeleted")) {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
CsParam3 += getcstype + " " + UString(uk001col.Name) + ", ";
|
||||||
|
CsParamNoType3 += string.Format("\r\n {0} = {0}, ", UString(uk001col.Name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
parms = parms.Substring(0, parms.Length - 2);
|
||||||
|
if (parmsByWhereLambda.Length > 0) parmsByWhereLambda = parmsByWhereLambda.Substring(0, parmsByWhereLambda.Length - 4);
|
||||||
|
parmsNewItem = parmsNewItem.Substring(0, parmsNewItem.Length - 2);
|
||||||
|
parmsBy = parmsBy.Substring(0, parmsBy.Length - 3);
|
||||||
|
parmsNoneType = parmsNoneType.Substring(0, parmsNoneType.Length - 2);
|
||||||
|
if (CsParam3.Length > 0) CsParam3 = CsParam3.Substring(0, CsParam3.Length - 2);
|
||||||
|
if (CsParamNoType3.Length > 0) CsParamNoType3 = CsParamNoType3.Substring(0, CsParamNoType3.Length - 2);
|
||||||
|
%}{if idens > 0}
|
||||||
|
public static {#GetCsType(idensCol)} Insert({#UString(table.Name)} item) {
|
||||||
|
item.{#UString(idensCol.Name)} = ({#GetCsType(idensCol)})freesql.Insert<{#UString(table.Name)}>().AppendData(item).ExecuteIdentity();
|
||||||
|
if (ItemCacheTimeout > 0) RemoveCache(item);
|
||||||
|
return item.{#UString(idensCol.Name)};
|
||||||
|
}{else}
|
||||||
|
public static void Insert({#UString(table.Name)} item) {
|
||||||
|
if (ItemCacheTimeout > 0) RemoveCache(item);
|
||||||
|
freesql.Insert<{#UString(table.Name)}>().AppendData(item).ExecuteAffrows();
|
||||||
|
}
|
||||||
|
public static long Insert(IEnumerable<{#UString(table.Name)}> items) {
|
||||||
|
if (ItemCacheTimeout > 0) RemoveCache(items);
|
||||||
|
return freesql.Insert<{#UString(table.Name)}>().AppendData(items).ExecuteAffrows();
|
||||||
|
}{/if}
|
||||||
|
{if CsParamNoType3.Split('=').Length <= 5}{if idens > 0}
|
||||||
|
public static {#GetCsType(idensCol)} Insert({#CsParam3}) {
|
||||||
|
var item = new {#UString(table.Name)} {{#CsParamNoType3}};
|
||||||
|
item.{#UString(idensCol.Name)} = ({#GetCsType(idensCol)})freesql.Insert<{#UString(table.Name)}>().AppendData(new {#UString(table.Name)} {{#CsParamNoType3}}).ExecuteIdentity();
|
||||||
|
if (ItemCacheTimeout > 0) RemoveCache(item);
|
||||||
|
return item.{#UString(idensCol.Name)};
|
||||||
|
}{else}
|
||||||
|
public static {#UString(table.Name)} Insert({#CsParam3}) {
|
||||||
|
var item = new {#UString(table.Name)} {{#CsParamNoType3}};
|
||||||
|
freesql.Insert<{#UString(table.Name)}>().AppendData(item).ExecuteAffrows();
|
||||||
|
if (ItemCacheTimeout > 0) RemoveCache(item);
|
||||||
|
return item;
|
||||||
|
}{/if}{/if}
|
||||||
|
public static long Update({#UString(table.Name)} item) {
|
||||||
|
if (ItemCacheTimeout > 0) RemoveCache(item);
|
||||||
|
return freesql.Update<{#UString(table.Name)}>().SetSource(item).ExecuteAffrows();
|
||||||
|
}
|
||||||
|
public static long Update(IEnumerable<{#UString(table.Name)}> items) {
|
||||||
|
if (ItemCacheTimeout > 0) RemoveCache(items);
|
||||||
|
return freesql.Update<{#UString(table.Name)}>().SetSource(items).ExecuteAffrows();
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 指定字段更新
|
||||||
|
/// </summary>
|
||||||
|
public IUpdate<{#UString(table.Name)}> UpdateDiy => freesql.Update<{#UString(table.Name)}>().Where(a => {#parmsByWhereLambda});
|
||||||
|
/// <summary>
|
||||||
|
/// {if pks.Count > 0}保存或添加,如果主键有值则尝试 Update,如果影响的行为 0 则尝试 Insert{else}添加{/if}
|
||||||
|
/// </summary>
|
||||||
|
public void Save() {{if pks.Count > 0}
|
||||||
|
if ({#string.Join(" && ", pks.Select(pkssa => "this." + UString(pkssa.Name) + " != default(" + GetCsType(pkssa) + ")"))}) {
|
||||||
|
var affrows = freesql.Update<{#UString(table.Name)}>().Where(a => {#parmsByWhereLambda}).ExecuteAffrows();
|
||||||
|
if (affrows > 0) return;
|
||||||
|
}{/if}
|
||||||
|
{if idens > 0}this.{#UString(idensCol.Name)} = {#GetCsType(idensCol).Replace("?", "") == "long" ? "" : ("(" + GetCsType(idensCol) + ")")}freesql.Insert<{#UString(table.Name)}>().AppendData(this).ExecuteIdentity();{else}
|
||||||
|
freesql.Insert<{#UString(table.Name)}>().AppendData(this).ExecuteAffrows();{/if}
|
||||||
|
}
|
||||||
|
{/if}{/if}
|
||||||
|
}
|
||||||
|
}
|
94
Templates/SqlServer/rich-entity-navigation-object/readme.md
Normal file
94
Templates/SqlServer/rich-entity-navigation-object/readme.md
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
# 生成器
|
||||||
|
|
||||||
|
生成器是基于 dbfirst 开发的辅助工具,适用老项目一键生成实体。生成器采用模板的方式,作者实现了三种生成模板:
|
||||||
|
|
||||||
|
| 模板名称 | 类型映射 | 外键导航属性 | 缓存管理 | 失血 | 贫血 | 充血 |
|
||||||
|
| ------------- | - | - |- | - |- | - |
|
||||||
|
| simple-entity | √ | X | X | √ | X | X |
|
||||||
|
| simple-entity-navigation-object | √ | √ | X | √ | X | X |
|
||||||
|
| rich-entity-navigation-object | √ | √ | √ | X | √ | X |
|
||||||
|
|
||||||
|
模板在项目目录:/Templates/MySql
|
||||||
|
|
||||||
|
> 更多模板逐步开发中。。。
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
//定义 mysql FreeSql
|
||||||
|
var mysql = new FreeSql.FreeSqlBuilder()
|
||||||
|
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
//创建模板生成类现实
|
||||||
|
var gen = new FreeSql.Generator.TemplateGenerator();
|
||||||
|
gen.Build(mysql.DbFirst,
|
||||||
|
@"C:\Users\28810\Desktop\github\FreeSql\Templates\MySql\simple-entity", //模板目录(事先下载)
|
||||||
|
@"C:\Users\28810\Desktop\新建文件夹 (9)", //生成后保存的目录
|
||||||
|
"cccddd" //数据库
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
## 模板语法
|
||||||
|
|
||||||
|
```html
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>{#title}</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<!--绑定表达式-->
|
||||||
|
{#表达式}
|
||||||
|
{##表达式} 当表达式可能发生runtime错误时使用,性能没有上面的高
|
||||||
|
|
||||||
|
<!--可嵌套使用,同一标签最多支持3个指令-->
|
||||||
|
{include ../header.html}
|
||||||
|
<div @for="i 1, 101">
|
||||||
|
<p @if="i === 50" @for="item,index in data">aaa</p>
|
||||||
|
<p @else="i % 3 === 0">bbb {#i}</p>
|
||||||
|
<p @else="">ccc {#i}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!--定义模块,可以将公共模块定义到一个文件中-->
|
||||||
|
{module module_name1 parms1, 2, 3...}
|
||||||
|
{/module}
|
||||||
|
{module module_name2 parms1, 2, 3...}
|
||||||
|
{/module}
|
||||||
|
|
||||||
|
<!--使用模块-->
|
||||||
|
{import ../module.html as myname}
|
||||||
|
{#myname.module_name(parms1, 2, 3...)}
|
||||||
|
|
||||||
|
<!--继承-->
|
||||||
|
{extends ../inc/layout.html}
|
||||||
|
{block body}{/block}
|
||||||
|
|
||||||
|
<!--嵌入代码块-->
|
||||||
|
{%
|
||||||
|
for (var a = 0; a < 100; a++)
|
||||||
|
print(a);
|
||||||
|
%}
|
||||||
|
|
||||||
|
<!--条件分支-->
|
||||||
|
{if i === 50}
|
||||||
|
{elseif i > 60}
|
||||||
|
{else}
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<!--三种循环-->
|
||||||
|
{for i 1,101} 可自定义名 {for index2 表达式1 in 表达式2}
|
||||||
|
|
||||||
|
{for item,index in items} 可选参数称 index
|
||||||
|
可自定义名 {for item2, index99 in 数组表达式}
|
||||||
|
|
||||||
|
{for key,item,index on json} 可选参数 item, index,
|
||||||
|
可自定义名 {for key2, item2, index99 in 对象表达式}
|
||||||
|
{/for}
|
||||||
|
|
||||||
|
<!--不被解析-->
|
||||||
|
{miss}
|
||||||
|
此块内容不被bmw.js解析
|
||||||
|
{/miss}
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
```
|
@ -0,0 +1,59 @@
|
|||||||
|
{%if (table.Type == DbTableType.StoreProcedure) {
|
||||||
|
print("return;");
|
||||||
|
return rTn;
|
||||||
|
}
|
||||||
|
%}using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using FreeSql.DataAnnotations;
|
||||||
|
{%
|
||||||
|
var dbf = dbfirst as FreeSql.IDbFirst;
|
||||||
|
var cols = (table.Columns as List<DbColumnInfo>);
|
||||||
|
var pks = (table.Primarys as List<DbColumnInfo>);
|
||||||
|
var fks = (table.Foreigns as List<DbForeignInfo>);
|
||||||
|
|
||||||
|
Func<string, string> UString = stra => stra.Substring(0, 1).ToUpper() + stra.Substring(1);
|
||||||
|
Func<DbColumnInfo, string> GetCsType = cola3 => {
|
||||||
|
return dbf.GetCsType(cola3);
|
||||||
|
};
|
||||||
|
Func<DbForeignInfo, string> GetFkObjectName = fkx => {
|
||||||
|
var eqfks = fks.Where(fk22a => fk22a.ReferencedTable.Name == fkx.ReferencedTable.Name);
|
||||||
|
if (eqfks.Count() == 1) return "Obj_" + fkx.ReferencedTable.Name;
|
||||||
|
var fkretname = fkx.Columns[0].Name;
|
||||||
|
if (fkretname.EndsWith(fkx.ReferencedColumns[0].Name, StringComparison.CurrentCultureIgnoreCase)) fkretname = fkretname.Substring(0, fkretname.Length - fkx.ReferencedColumns[0].Name.Length).TrimEnd('_');
|
||||||
|
if (fkretname.EndsWith(fkx.ReferencedTable.Name, StringComparison.CurrentCultureIgnoreCase)) fkretname = fkretname.Substring(0, fkretname.Length - fkx.ReferencedTable.Name.Length).TrimEnd('_');
|
||||||
|
if (fkretname.StartsWith(fkx.ReferencedTable.Name, StringComparison.CurrentCultureIgnoreCase)) fkretname = fkretname.Substring(fkx.ReferencedTable.Name.Length).TrimStart('_');
|
||||||
|
return "Obj_" + fkx.ReferencedTable.Name + (string.IsNullOrEmpty(fkretname) ? "" : ("_" + fkretname));
|
||||||
|
};
|
||||||
|
%}
|
||||||
|
namespace test.Model {
|
||||||
|
|
||||||
|
[JsonObject(MemberSerialization.OptIn), Table(Name = "{#!string.IsNullOrEmpty(table.Schema) ? table.Schema + "." : ""}{#table.Name}"{if cols.Where(cola003 => cola003.Name.ToLower() == "is_deleted" || cola003.Name.ToLower() == "isdeleted").Any()}, SelectFilter = "a.IsDeleted = 1"{/if})]
|
||||||
|
public partial class {#UString(table.Name)} {{for col,index in table.Columns}{%
|
||||||
|
var findfks = fks.Where(fkaa => fkaa.Columns.Where(fkaac1 => fkaac1.Name == col.Name).Any());
|
||||||
|
%}{if findfks.Any() == false}
|
||||||
|
{if string.IsNullOrEmpty(col.Coment) == false}/// <summary>
|
||||||
|
/// {#col.Coment.Replace("\r\n", "\n").Replace("\n", "\r\n /// ")}
|
||||||
|
/// </summary>{/if}
|
||||||
|
[JsonProperty, Column(Name = "{#col.Name}", DbType = "{#col.DbTypeTextFull}"{if col.IsPrimary == true}, IsPrimary = true{/if}{if col.IsIdentity == true}, IsIdentity = true{/if}{if col.IsNullable == true}, IsNullable = true{/if})]
|
||||||
|
public {#GetCsType(col)} {#UString(col.Name)} { get; set; }
|
||||||
|
{else}
|
||||||
|
private {#GetCsType(col)} _{#UString(col.Name)};
|
||||||
|
{if string.IsNullOrEmpty(col.Coment) == false}/// <summary>
|
||||||
|
/// {#col.Coment.Replace("\r\n", "\n").Replace("\n", "\r\n /// ")}
|
||||||
|
/// </summary>{/if}
|
||||||
|
[JsonProperty, Column(Name = "{#col.Name}", DbType = "{#col.DbTypeTextFull}"{if col.IsPrimary == true}, IsPrimary = true{/if}{if col.IsIdentity == true}, IsIdentity = true{/if}{if col.IsNullable == true}, IsNullable = true{/if})]
|
||||||
|
public {#GetCsType(col)} {#UString(col.Name)} { get => _{#UString(col.Name)}; set {
|
||||||
|
if (_{#UString(col.Name)} == value) return;
|
||||||
|
_{#UString(col.Name)} = value;{for fkcok2 in findfks}
|
||||||
|
{#GetFkObjectName(fkcok2)} = null;{/for}
|
||||||
|
} }
|
||||||
|
{/if}{/for}{for fk in fks}
|
||||||
|
public {#UString(fk.ReferencedTable.Name)} {#GetFkObjectName(fk)} { get; set; }
|
||||||
|
{/for}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,94 @@
|
|||||||
|
# 生成器
|
||||||
|
|
||||||
|
生成器是基于 dbfirst 开发的辅助工具,适用老项目一键生成实体。生成器采用模板的方式,作者实现了三种生成模板:
|
||||||
|
|
||||||
|
| 模板名称 | 类型映射 | 外键导航属性 | 缓存管理 | 失血 | 贫血 | 充血 |
|
||||||
|
| ------------- | - | - |- | - |- | - |
|
||||||
|
| simple-entity | √ | X | X | √ | X | X |
|
||||||
|
| simple-entity-navigation-object | √ | √ | X | √ | X | X |
|
||||||
|
| rich-entity-navigation-object | √ | √ | √ | X | √ | X |
|
||||||
|
|
||||||
|
模板在项目目录:/Templates/MySql
|
||||||
|
|
||||||
|
> 更多模板逐步开发中。。。
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
//定义 mysql FreeSql
|
||||||
|
var mysql = new FreeSql.FreeSqlBuilder()
|
||||||
|
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
//创建模板生成类现实
|
||||||
|
var gen = new FreeSql.Generator.TemplateGenerator();
|
||||||
|
gen.Build(mysql.DbFirst,
|
||||||
|
@"C:\Users\28810\Desktop\github\FreeSql\Templates\MySql\simple-entity", //模板目录(事先下载)
|
||||||
|
@"C:\Users\28810\Desktop\新建文件夹 (9)", //生成后保存的目录
|
||||||
|
"cccddd" //数据库
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
## 模板语法
|
||||||
|
|
||||||
|
```html
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>{#title}</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<!--绑定表达式-->
|
||||||
|
{#表达式}
|
||||||
|
{##表达式} 当表达式可能发生runtime错误时使用,性能没有上面的高
|
||||||
|
|
||||||
|
<!--可嵌套使用,同一标签最多支持3个指令-->
|
||||||
|
{include ../header.html}
|
||||||
|
<div @for="i 1, 101">
|
||||||
|
<p @if="i === 50" @for="item,index in data">aaa</p>
|
||||||
|
<p @else="i % 3 === 0">bbb {#i}</p>
|
||||||
|
<p @else="">ccc {#i}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!--定义模块,可以将公共模块定义到一个文件中-->
|
||||||
|
{module module_name1 parms1, 2, 3...}
|
||||||
|
{/module}
|
||||||
|
{module module_name2 parms1, 2, 3...}
|
||||||
|
{/module}
|
||||||
|
|
||||||
|
<!--使用模块-->
|
||||||
|
{import ../module.html as myname}
|
||||||
|
{#myname.module_name(parms1, 2, 3...)}
|
||||||
|
|
||||||
|
<!--继承-->
|
||||||
|
{extends ../inc/layout.html}
|
||||||
|
{block body}{/block}
|
||||||
|
|
||||||
|
<!--嵌入代码块-->
|
||||||
|
{%
|
||||||
|
for (var a = 0; a < 100; a++)
|
||||||
|
print(a);
|
||||||
|
%}
|
||||||
|
|
||||||
|
<!--条件分支-->
|
||||||
|
{if i === 50}
|
||||||
|
{elseif i > 60}
|
||||||
|
{else}
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<!--三种循环-->
|
||||||
|
{for i 1,101} 可自定义名 {for index2 表达式1 in 表达式2}
|
||||||
|
|
||||||
|
{for item,index in items} 可选参数称 index
|
||||||
|
可自定义名 {for item2, index99 in 数组表达式}
|
||||||
|
|
||||||
|
{for key,item,index on json} 可选参数 item, index,
|
||||||
|
可自定义名 {for key2, item2, index99 in 对象表达式}
|
||||||
|
{/for}
|
||||||
|
|
||||||
|
<!--不被解析-->
|
||||||
|
{miss}
|
||||||
|
此块内容不被bmw.js解析
|
||||||
|
{/miss}
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
```
|
33
Templates/SqlServer/simple-entity/Model/for-table.cs.freesql
Normal file
33
Templates/SqlServer/simple-entity/Model/for-table.cs.freesql
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
{%if (table.Type == DbTableType.StoreProcedure) {
|
||||||
|
print("return;");
|
||||||
|
return rTn;
|
||||||
|
}
|
||||||
|
%}using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using FreeSql.DataAnnotations;
|
||||||
|
{%
|
||||||
|
var dbf = dbfirst as FreeSql.IDbFirst;
|
||||||
|
var cols = (table.Columns as List<DbColumnInfo>);
|
||||||
|
|
||||||
|
Func<string, string> UString = stra => stra.Substring(0, 1).ToUpper() + stra.Substring(1);
|
||||||
|
Func<DbColumnInfo, string> GetCsType = cola3 => {
|
||||||
|
return dbf.GetCsType(cola3);
|
||||||
|
};
|
||||||
|
%}
|
||||||
|
namespace test.Model {
|
||||||
|
|
||||||
|
[JsonObject(MemberSerialization.OptIn), Table(Name = "{#!string.IsNullOrEmpty(table.Schema) ? table.Schema + "." : ""}{#table.Name}"{if cols.Where(cola003 => cola003.Name.ToLower() == "is_deleted" || cola003.Name.ToLower() == "isdeleted").Any()}, SelectFilter = "a.IsDeleted = 1"{/if})]
|
||||||
|
public partial class {#UString(table.Name)} {{for col,index in table.Columns}
|
||||||
|
{if string.IsNullOrEmpty(col.Coment) == false}/// <summary>
|
||||||
|
/// {#col.Coment.Replace("\r\n", "\n").Replace("\n", "\r\n /// ")}
|
||||||
|
/// </summary>{/if}
|
||||||
|
[JsonProperty, Column(Name = "{#col.Name}", DbType = "{#col.DbTypeTextFull}"{if col.IsPrimary == true}, IsPrimary = true{/if}{if col.IsIdentity == true}, IsIdentity = true{/if}{if col.IsNullable == true}, IsNullable = true{/if})]
|
||||||
|
public {#GetCsType(col)} {#UString(col.Name)} { get; set; }
|
||||||
|
{/for}
|
||||||
|
}
|
||||||
|
}
|
94
Templates/SqlServer/simple-entity/readme.md
Normal file
94
Templates/SqlServer/simple-entity/readme.md
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
# 生成器
|
||||||
|
|
||||||
|
生成器是基于 dbfirst 开发的辅助工具,适用老项目一键生成实体。生成器采用模板的方式,作者实现了三种生成模板:
|
||||||
|
|
||||||
|
| 模板名称 | 类型映射 | 外键导航属性 | 缓存管理 | 失血 | 贫血 | 充血 |
|
||||||
|
| ------------- | - | - |- | - |- | - |
|
||||||
|
| simple-entity | √ | X | X | √ | X | X |
|
||||||
|
| simple-entity-navigation-object | √ | √ | X | √ | X | X |
|
||||||
|
| rich-entity-navigation-object | √ | √ | √ | X | √ | X |
|
||||||
|
|
||||||
|
模板在项目目录:/Templates/MySql
|
||||||
|
|
||||||
|
> 更多模板逐步开发中。。。
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
//定义 mysql FreeSql
|
||||||
|
var mysql = new FreeSql.FreeSqlBuilder()
|
||||||
|
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
//创建模板生成类现实
|
||||||
|
var gen = new FreeSql.Generator.TemplateGenerator();
|
||||||
|
gen.Build(mysql.DbFirst,
|
||||||
|
@"C:\Users\28810\Desktop\github\FreeSql\Templates\MySql\simple-entity", //模板目录(事先下载)
|
||||||
|
@"C:\Users\28810\Desktop\新建文件夹 (9)", //生成后保存的目录
|
||||||
|
"cccddd" //数据库
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
## 模板语法
|
||||||
|
|
||||||
|
```html
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>{#title}</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<!--绑定表达式-->
|
||||||
|
{#表达式}
|
||||||
|
{##表达式} 当表达式可能发生runtime错误时使用,性能没有上面的高
|
||||||
|
|
||||||
|
<!--可嵌套使用,同一标签最多支持3个指令-->
|
||||||
|
{include ../header.html}
|
||||||
|
<div @for="i 1, 101">
|
||||||
|
<p @if="i === 50" @for="item,index in data">aaa</p>
|
||||||
|
<p @else="i % 3 === 0">bbb {#i}</p>
|
||||||
|
<p @else="">ccc {#i}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!--定义模块,可以将公共模块定义到一个文件中-->
|
||||||
|
{module module_name1 parms1, 2, 3...}
|
||||||
|
{/module}
|
||||||
|
{module module_name2 parms1, 2, 3...}
|
||||||
|
{/module}
|
||||||
|
|
||||||
|
<!--使用模块-->
|
||||||
|
{import ../module.html as myname}
|
||||||
|
{#myname.module_name(parms1, 2, 3...)}
|
||||||
|
|
||||||
|
<!--继承-->
|
||||||
|
{extends ../inc/layout.html}
|
||||||
|
{block body}{/block}
|
||||||
|
|
||||||
|
<!--嵌入代码块-->
|
||||||
|
{%
|
||||||
|
for (var a = 0; a < 100; a++)
|
||||||
|
print(a);
|
||||||
|
%}
|
||||||
|
|
||||||
|
<!--条件分支-->
|
||||||
|
{if i === 50}
|
||||||
|
{elseif i > 60}
|
||||||
|
{else}
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<!--三种循环-->
|
||||||
|
{for i 1,101} 可自定义名 {for index2 表达式1 in 表达式2}
|
||||||
|
|
||||||
|
{for item,index in items} 可选参数称 index
|
||||||
|
可自定义名 {for item2, index99 in 数组表达式}
|
||||||
|
|
||||||
|
{for key,item,index on json} 可选参数 item, index,
|
||||||
|
可自定义名 {for key2, item2, index99 in 对象表达式}
|
||||||
|
{/for}
|
||||||
|
|
||||||
|
<!--不被解析-->
|
||||||
|
{miss}
|
||||||
|
此块内容不被bmw.js解析
|
||||||
|
{/miss}
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
```
|
Loading…
x
Reference in New Issue
Block a user