Skip to content

Commit

Permalink
- 增加 DuckDB 数据库支持;
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Aug 17, 2024
1 parent 867e28f commit a1013a3
Show file tree
Hide file tree
Showing 53 changed files with 11,771 additions and 182 deletions.
33 changes: 31 additions & 2 deletions FreeSql-lite.sln
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Provider.Xugu", "Pr
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Extensions.ZeroEntity", "Extensions\FreeSql.Extensions.ZeroEntity\FreeSql.Extensions.ZeroEntity.csproj", "{4367B7AC-604F-4503-A1D4-643ADBFCF703}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Provider.Duckdb", "Providers\FreeSql.Provider.Duckdb\FreeSql.Provider.Duckdb.csproj", "{02CFB50A-D8C4-470D-AC93-5540D6029430}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Tests.Provider.Duckdb", "FreeSql.Tests\FreeSql.Tests.Provider.Duckdb\FreeSql.Tests.Provider.Duckdb.csproj", "{B9787A81-D537-45ED-B413-61BF03C8FEBE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -509,6 +513,30 @@ Global
{4367B7AC-604F-4503-A1D4-643ADBFCF703}.Release|x64.Build.0 = Release|Any CPU
{4367B7AC-604F-4503-A1D4-643ADBFCF703}.Release|x86.ActiveCfg = Release|Any CPU
{4367B7AC-604F-4503-A1D4-643ADBFCF703}.Release|x86.Build.0 = Release|Any CPU
{02CFB50A-D8C4-470D-AC93-5540D6029430}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{02CFB50A-D8C4-470D-AC93-5540D6029430}.Debug|Any CPU.Build.0 = Debug|Any CPU
{02CFB50A-D8C4-470D-AC93-5540D6029430}.Debug|x64.ActiveCfg = Debug|Any CPU
{02CFB50A-D8C4-470D-AC93-5540D6029430}.Debug|x64.Build.0 = Debug|Any CPU
{02CFB50A-D8C4-470D-AC93-5540D6029430}.Debug|x86.ActiveCfg = Debug|Any CPU
{02CFB50A-D8C4-470D-AC93-5540D6029430}.Debug|x86.Build.0 = Debug|Any CPU
{02CFB50A-D8C4-470D-AC93-5540D6029430}.Release|Any CPU.ActiveCfg = Release|Any CPU
{02CFB50A-D8C4-470D-AC93-5540D6029430}.Release|Any CPU.Build.0 = Release|Any CPU
{02CFB50A-D8C4-470D-AC93-5540D6029430}.Release|x64.ActiveCfg = Release|Any CPU
{02CFB50A-D8C4-470D-AC93-5540D6029430}.Release|x64.Build.0 = Release|Any CPU
{02CFB50A-D8C4-470D-AC93-5540D6029430}.Release|x86.ActiveCfg = Release|Any CPU
{02CFB50A-D8C4-470D-AC93-5540D6029430}.Release|x86.Build.0 = Release|Any CPU
{B9787A81-D537-45ED-B413-61BF03C8FEBE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B9787A81-D537-45ED-B413-61BF03C8FEBE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B9787A81-D537-45ED-B413-61BF03C8FEBE}.Debug|x64.ActiveCfg = Debug|Any CPU
{B9787A81-D537-45ED-B413-61BF03C8FEBE}.Debug|x64.Build.0 = Debug|Any CPU
{B9787A81-D537-45ED-B413-61BF03C8FEBE}.Debug|x86.ActiveCfg = Debug|Any CPU
{B9787A81-D537-45ED-B413-61BF03C8FEBE}.Debug|x86.Build.0 = Debug|Any CPU
{B9787A81-D537-45ED-B413-61BF03C8FEBE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B9787A81-D537-45ED-B413-61BF03C8FEBE}.Release|Any CPU.Build.0 = Release|Any CPU
{B9787A81-D537-45ED-B413-61BF03C8FEBE}.Release|x64.ActiveCfg = Release|Any CPU
{B9787A81-D537-45ED-B413-61BF03C8FEBE}.Release|x64.Build.0 = Release|Any CPU
{B9787A81-D537-45ED-B413-61BF03C8FEBE}.Release|x86.ActiveCfg = Release|Any CPU
{B9787A81-D537-45ED-B413-61BF03C8FEBE}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -544,10 +572,11 @@ Global
{FEE501EB-60D1-4370-BC65-F939BCA7F32E} = {2A381C57-2697-427B-9F10-55DA11FD02E4}
{353F3732-0704-40F2-972B-036E9CC01881} = {2A381C57-2697-427B-9F10-55DA11FD02E4}
{4367B7AC-604F-4503-A1D4-643ADBFCF703} = {4A92E8A6-9A6D-41A1-9CDA-DE10899648AA}
{02CFB50A-D8C4-470D-AC93-5540D6029430} = {2A381C57-2697-427B-9F10-55DA11FD02E4}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
RESX_NeutralResourcesLanguage = en-US
RESX_PrefixTranslations = True
SolutionGuid = {089687FA-5D21-40AC-BA8A-AA0D1E1H7F98}
RESX_PrefixTranslations = True
RESX_NeutralResourcesLanguage = en-US
EndGlobalSection
EndGlobal
12 changes: 3 additions & 9 deletions FreeSql.DbContext/UnitOfWork/UnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ public DbTransaction GetOrBeginTransaction(bool isCreate = true)
catch (Exception ex)
{
_fsql?.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(_tranBefore, "失败", ex));
#pragma warning disable CA2200 // 再次引发以保留堆栈详细信息
throw ex;
#pragma warning restore CA2200 // 再次引发以保留堆栈详细信息
throw;
}
return _tran;
}
Expand All @@ -125,9 +123,7 @@ public void Commit()
{
if (isCommited == false)
_fsql?.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(_tranBefore, "提交失败", ex));
#pragma warning disable CA2200 // 再次引发以保留堆栈详细信息
throw ex;
#pragma warning restore CA2200 // 再次引发以保留堆栈详细信息
throw;
}
finally
{
Expand All @@ -151,9 +147,7 @@ public void Rollback()
{
if (isRollbacked == false)
_fsql?.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(_tranBefore, "回滚失败", ex));
#pragma warning disable CA2200 // 再次引发以保留堆栈详细信息
throw ex;
#pragma warning restore CA2200 // 再次引发以保留堆栈详细信息
throw;
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace FreeSql.Tests.Duckdb
{
public class DuckdbDeleteTest
{
IFreeSql fsql => g.duckdb;
IDelete<Topic> delete => fsql.Delete<Topic>();

[Table(Name = "tb_topic_del")]
class Topic
{
[Column(IsIdentity = true, IsPrimary = true)]
public int Id { get; set; }
public int Clicks { get; set; }
public string Title { get; set; }
public DateTime CreateTime { get; set; }
}

[Fact]
public void Dywhere()
{
Assert.Null(fsql.Delete<Topic>().ToSql());
var sql = fsql.Delete<Topic>(new[] { 1, 2 }).ToSql();
Assert.Equal("DELETE FROM \"tb_topic_del\" WHERE (\"id\" IN (1,2))", sql);

sql = fsql.Delete<Topic>(new Topic { Id = 1, Title = "test" }).ToSql();
Assert.Equal("DELETE FROM \"tb_topic_del\" WHERE (\"id\" = 1)", sql);

sql = fsql.Delete<Topic>(new[] { new Topic { Id = 1, Title = "test" }, new Topic { Id = 2, Title = "test" } }).ToSql();
Assert.Equal("DELETE FROM \"tb_topic_del\" WHERE (\"id\" IN (1,2))", sql);

sql = fsql.Delete<Topic>(new { id = 1 }).ToSql();
Assert.Equal("DELETE FROM \"tb_topic_del\" WHERE (\"id\" = 1)", sql);

sql = fsql.Delete<MultiPkTopic>(new[] { new { Id1 = 1, Id2 = 10 }, new { Id1 = 2, Id2 = 20 } }).ToSql();
Assert.Equal("DELETE FROM \"multipktopic\" WHERE (\"id1\" = 1 AND \"id2\" = 10 OR \"id1\" = 2 AND \"id2\" = 20)", sql);
}
class MultiPkTopic
{
[Column(IsPrimary = true)]
public int Id1 { get; set; }
[Column(IsPrimary = true)]
public int Id2 { get; set; }
public int Clicks { get; set; }
public string Title { get; set; }
public DateTime CreateTime { get; set; }
}

[Fact]
public void Where()
{
var sql = delete.Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
Assert.Equal("DELETE FROM \"tb_topic_del\" WHERE (\"id\" = 1)", sql);

sql = delete.Where("id = @id", new { id = 1 }).ToSql().Replace("\r\n", "");
Assert.Equal("DELETE FROM \"tb_topic_del\" 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_del\" 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_del\" WHERE (\"id\" IN (1,2,3,4,5,6,7,8,9,10))", sql);
}
[Fact]
public void ExecuteAffrows()
{

var id = fsql.Insert<Topic>(new Topic { Title = "xxxx" }).ExecuteIdentity();
Assert.Equal(1, delete.Where(a => a.Id == id).ExecuteAffrows());
}

[Fact]
public void AsTable()
{
Assert.Null(fsql.Delete<Topic>().ToSql());
var sql = fsql.Delete<Topic>(new[] { 1, 2 }).AsTable(a => "TopicAsTable").ToSql();
Assert.Equal("DELETE FROM \"topicastable\" WHERE (\"id\" IN (1,2))", sql);

sql = fsql.Delete<Topic>(new Topic { Id = 1, Title = "test" }).AsTable(a => "TopicAsTable").ToSql();
Assert.Equal("DELETE FROM \"topicastable\" WHERE (\"id\" = 1)", sql);

sql = fsql.Delete<Topic>(new[] { new Topic { Id = 1, Title = "test" }, new Topic { Id = 2, Title = "test" } }).AsTable(a => "TopicAsTable").ToSql();
Assert.Equal("DELETE FROM \"topicastable\" WHERE (\"id\" IN (1,2))", sql);

sql = fsql.Delete<Topic>(new { id = 1 }).AsTable(a => "TopicAsTable").ToSql();
Assert.Equal("DELETE FROM \"topicastable\" WHERE (\"id\" = 1)", sql);
}
}
}
Loading

0 comments on commit a1013a3

Please sign in to comment.