Skip to content

Commit 96ba6cd

Browse files
authored
Merge pull request #19 from catcherwong/bb-sqlserver
feat: bb support sqlserver
2 parents 37ce796 + 1293dae commit 96ba6cd

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

src/Dtmcli.Tests/DbSpecialTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ public void TestDbSpecial()
1818
var postgres = DtmImp.DbSpecialDelegate.Instance.GetDBSpecial();
1919
Assert.Equal("begin", postgres.GetXaSQL("start", "xa1"));
2020
Assert.Equal("insert into a(f) values(@f) on conflict ON CONSTRAINT c do nothing", postgres.GetInsertIgnoreTemplate("a(f) values(@f)", "c"));
21+
22+
DtmImp.DbSpecialDelegate.Instance.SetCurrentDBType("sqlserver");
23+
24+
var sqlserver = DtmImp.DbSpecialDelegate.Instance.GetDBSpecial();
25+
Assert.Equal("insert into a(f) values(@f)", sqlserver.GetInsertIgnoreTemplate("a(f) values(@f)", "c"));
26+
Assert.Throws<DtmcliException>(() => sqlserver.GetXaSQL("", ""));
2127
}
2228
}
2329
}

src/Dtmcli/Constant.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ internal class Barrier
121121

122122
internal static readonly string DBTYPE_POSTGRES = "postgres";
123123

124+
internal static readonly string DBTYPE_SQLSERVER = "sqlserver";
125+
124126
internal static readonly string PG_CONSTRAINT = "uniq_barrier";
125127

126128
internal static readonly string MSG_BARRIER_REASON = "rollback";

src/Dtmcli/DtmImp/IDbSpecial.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,64 @@ public string GetXaSQL(string command, string xid)
6363
}
6464
}
6565

66+
public class SqlServerDBSpecial : IDbSpecial
67+
{
68+
private SqlServerDBSpecial()
69+
{ }
70+
71+
private static readonly Lazy<SqlServerDBSpecial> Instancelock =
72+
new Lazy<SqlServerDBSpecial>(() => new SqlServerDBSpecial());
73+
74+
public static SqlServerDBSpecial Instance => Instancelock.Value;
75+
76+
/*
77+
78+
IF NOT EXISTS(SELECT * FROM sys.databases WHERE name = 'dtm_barrier')
79+
BEGIN
80+
CREATE DATABASE dtm_barrier
81+
USE dtm_barrier
82+
END
83+
84+
GO
85+
86+
IF EXISTS (SELECT * FROM sysobjects WHERE id = object_id(N’[dbo].[barrier]’) and OBJECTPROPERTY(id, N’IsUserTable’) = 1)
87+
BEGIN
88+
DROP TABLE [dbo].[barrier]
89+
END
90+
91+
GO
92+
93+
CREATE TABLE [dbo].[barrier]
94+
(
95+
[id] bigint NOT NULL IDENTITY(1,1) PRIMARY KEY,
96+
[trans_type] varchar(45) NOT NULL DEFAULT(''),
97+
[gid] varchar(128) NOT NULL DEFAULT(''),
98+
[branch_id] varchar(128) NOT NULL DEFAULT(''),
99+
[op] varchar(45) NOT NULL DEFAULT(''),
100+
[barrier_id] varchar(45) NOT NULL DEFAULT(''),
101+
[reason] varchar(45) NOT NULL DEFAULT(''),
102+
[create_time] datetime NOT NULL DEFAULT(getdate()) ,
103+
[update_time] datetime NOT NULL DEFAULT(getdate())
104+
)
105+
106+
GO
107+
108+
CREATE UNIQUE INDEX[ix_uniq_barrier] ON[dbo].[barrier]
109+
([gid] ASC, [branch_id] ASC, [op] ASC, [barrier_id] ASC)
110+
WITH(IGNORE_DUP_KEY = ON)
111+
112+
GO
113+
*/
114+
public string GetInsertIgnoreTemplate(string tableAndValues, string pgConstraint)
115+
=> string.Format("insert into {0}", tableAndValues);
116+
117+
public string GetPlaceHoldSQL(string sql)
118+
=> sql;
119+
120+
public string GetXaSQL(string command, string xid)
121+
=> throw new DtmcliException("not support xa now!!!");
122+
}
123+
66124
public class DbSpecialDelegate
67125
{
68126
private DbSpecialDelegate()
@@ -77,6 +135,7 @@ private DbSpecialDelegate()
77135
{
78136
{ Constant.Barrier.DBTYPE_MYSQL, MysqlDBSpecial.Instance },
79137
{ Constant.Barrier.DBTYPE_POSTGRES, PostgresDBSpecial.Instance },
138+
{ Constant.Barrier.DBTYPE_SQLSERVER, SqlServerDBSpecial.Instance },
80139
};
81140
private string _currentDBType = Constant.Barrier.DBTYPE_MYSQL;
82141

0 commit comments

Comments
 (0)