Skip to content

Commit

Permalink
Merge pull request #19 from catcherwong/bb-sqlserver
Browse files Browse the repository at this point in the history
feat: bb support sqlserver
  • Loading branch information
catcherwong authored Feb 5, 2022
2 parents 37ce796 + 1293dae commit 96ba6cd
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Dtmcli.Tests/DbSpecialTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DtmcliException>(() => sqlserver.GetXaSQL("", ""));
}
}
}
2 changes: 2 additions & 0 deletions src/Dtmcli/Constant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
59 changes: 59 additions & 0 deletions src/Dtmcli/DtmImp/IDbSpecial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,64 @@ public string GetXaSQL(string command, string xid)
}
}

public class SqlServerDBSpecial : IDbSpecial
{
private SqlServerDBSpecial()
{ }

private static readonly Lazy<SqlServerDBSpecial> Instancelock =
new Lazy<SqlServerDBSpecial>(() => 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()
Expand All @@ -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;

Expand Down

0 comments on commit 96ba6cd

Please sign in to comment.