From 1293daeb8825af2d8c312f8ae904c4c7fec7008e Mon Sep 17 00:00:00 2001 From: catcherwong Date: Thu, 3 Feb 2022 22:11:45 +0800 Subject: [PATCH] feat: bb support sqlserver --- src/Dtmcli.Tests/DbSpecialTests.cs | 6 +++ src/Dtmcli/Constant.cs | 2 + src/Dtmcli/DtmImp/IDbSpecial.cs | 59 ++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/src/Dtmcli.Tests/DbSpecialTests.cs b/src/Dtmcli.Tests/DbSpecialTests.cs index a019042..8edd590 100644 --- a/src/Dtmcli.Tests/DbSpecialTests.cs +++ b/src/Dtmcli.Tests/DbSpecialTests.cs @@ -18,6 +18,12 @@ public void TestDbSpecial() var postgres = DtmImp.DbSpecialDelegate.Instance.GetDBSpecial(); Assert.Equal("begin", postgres.GetXaSQL("start", "xa1")); Assert.Equal("insert into a(f) values(@f) on conflict ON CONSTRAINT c do nothing", postgres.GetInsertIgnoreTemplate("a(f) values(@f)", "c")); + + DtmImp.DbSpecialDelegate.Instance.SetCurrentDBType("sqlserver"); + + var sqlserver = DtmImp.DbSpecialDelegate.Instance.GetDBSpecial(); + Assert.Equal("insert into a(f) values(@f)", sqlserver.GetInsertIgnoreTemplate("a(f) values(@f)", "c")); + Assert.Throws(() => sqlserver.GetXaSQL("", "")); } } } \ No newline at end of file diff --git a/src/Dtmcli/Constant.cs b/src/Dtmcli/Constant.cs index 816531d..ccd0883 100644 --- a/src/Dtmcli/Constant.cs +++ b/src/Dtmcli/Constant.cs @@ -121,6 +121,8 @@ internal class Barrier internal static readonly string DBTYPE_POSTGRES = "postgres"; + internal static readonly string DBTYPE_SQLSERVER = "sqlserver"; + internal static readonly string PG_CONSTRAINT = "uniq_barrier"; internal static readonly string MSG_BARRIER_REASON = "rollback"; diff --git a/src/Dtmcli/DtmImp/IDbSpecial.cs b/src/Dtmcli/DtmImp/IDbSpecial.cs index 701a4df..daab353 100644 --- a/src/Dtmcli/DtmImp/IDbSpecial.cs +++ b/src/Dtmcli/DtmImp/IDbSpecial.cs @@ -63,6 +63,64 @@ public string GetXaSQL(string command, string xid) } } + public class SqlServerDBSpecial : IDbSpecial + { + private SqlServerDBSpecial() + { } + + private static readonly Lazy Instancelock = + new Lazy(() => new SqlServerDBSpecial()); + + public static SqlServerDBSpecial Instance => Instancelock.Value; + + /* + +IF NOT EXISTS(SELECT * FROM sys.databases WHERE name = 'dtm_barrier') +BEGIN + CREATE DATABASE dtm_barrier + USE dtm_barrier +END + +GO + +IF EXISTS (SELECT * FROM sysobjects WHERE id = object_id(N’[dbo].[barrier]’) and OBJECTPROPERTY(id, N’IsUserTable’) = 1) +BEGIN + DROP TABLE [dbo].[barrier] +END + +GO + +CREATE TABLE [dbo].[barrier] +( + [id] bigint NOT NULL IDENTITY(1,1) PRIMARY KEY, + [trans_type] varchar(45) NOT NULL DEFAULT(''), + [gid] varchar(128) NOT NULL DEFAULT(''), + [branch_id] varchar(128) NOT NULL DEFAULT(''), + [op] varchar(45) NOT NULL DEFAULT(''), + [barrier_id] varchar(45) NOT NULL DEFAULT(''), + [reason] varchar(45) NOT NULL DEFAULT(''), + [create_time] datetime NOT NULL DEFAULT(getdate()) , + [update_time] datetime NOT NULL DEFAULT(getdate()) +) + +GO + +CREATE UNIQUE INDEX[ix_uniq_barrier] ON[dbo].[barrier] + ([gid] ASC, [branch_id] ASC, [op] ASC, [barrier_id] ASC) +WITH(IGNORE_DUP_KEY = ON) + +GO + */ + public string GetInsertIgnoreTemplate(string tableAndValues, string pgConstraint) + => string.Format("insert into {0}", tableAndValues); + + public string GetPlaceHoldSQL(string sql) + => sql; + + public string GetXaSQL(string command, string xid) + => throw new DtmcliException("not support xa now!!!"); + } + public class DbSpecialDelegate { private DbSpecialDelegate() @@ -77,6 +135,7 @@ private DbSpecialDelegate() { { Constant.Barrier.DBTYPE_MYSQL, MysqlDBSpecial.Instance }, { Constant.Barrier.DBTYPE_POSTGRES, PostgresDBSpecial.Instance }, + { Constant.Barrier.DBTYPE_SQLSERVER, SqlServerDBSpecial.Instance }, }; private string _currentDBType = Constant.Barrier.DBTYPE_MYSQL;