Permalink
Browse files

FirebirdDialect : 1. Added missing feature OnDelete|OnUpdate clause t…

…o foreingkey (when creating tables). 2. Fixed DefaultValueFormat
  • Loading branch information...
1 parent 86bbacb commit 8a05411c514fa544d72a1c20ec439d067c7c20ca @angelcolmenares angelcolmenares committed Mar 23, 2013
@@ -40,6 +40,7 @@ public FirebirdOrmLiteDialectProvider(bool compactGuid)
base.RealColumnDefinition= "FLOAT";
base.DefaultStringLength=128;
base.InitColumnTypeMap();
+ DefaultValueFormat = " DEFAULT '{0}'";
}
public override IDbConnection CreateConnection(string connectionString, Dictionary<string, string> options)
@@ -340,6 +341,9 @@ public override string ToCreateTableStatement(Type tableType)
GetQuotedColumnName(fieldDef.FieldName),
GetQuotedTableName(refModelDef),
GetQuotedColumnName(refModelDef.PrimaryKey.FieldName));
+
+ sbConstraints.Append(GetForeignKeyOnDeleteClause(fieldDef.ForeignKey));
+ sbConstraints.Append(GetForeignKeyOnUpdateClause(fieldDef.ForeignKey));
}
if (sbPk.Length !=0) sbColumns.AppendFormat(", \n PRIMARY KEY({0})", sbPk);
@@ -394,16 +398,16 @@ public override string ToCreateTableStatement(Type tableType)
var sql = new StringBuilder();
sql.AppendFormat("{0} {1}", GetQuotedColumnName(fieldName), fieldDefinition);
-
+
+ if (!string.IsNullOrEmpty(defaultValue))
+ {
+ sql.AppendFormat(DefaultValueFormat, defaultValue);
+ }
+
if (!isNullable)
{
sql.Append(" NOT NULL");
- }
-
- if (!string.IsNullOrEmpty(defaultValue))
- {
- sql.AppendFormat(DefaultValueFormat, defaultValue);
- }
+ }
return sql.ToString();
}
@@ -727,6 +731,16 @@ public override bool DoesTableExist(IDbCommand dbCmd, string tableName)
}
+ public override string GetForeignKeyOnDeleteClause(ForeignKeyConstraint foreignKey)
+ {
+ return (!string.IsNullOrEmpty(foreignKey.OnDelete) && foreignKey.OnDelete.ToUpper()!="RESTRICT" )? " ON DELETE " + foreignKey.OnDelete : "";
+ }
+
+ public override string GetForeignKeyOnUpdateClause(ForeignKeyConstraint foreignKey)
+ {
+ return (!string.IsNullOrEmpty(foreignKey.OnUpdate) && foreignKey.OnUpdate.ToUpper()!="RESTRICT" )? " ON UPDATE " + foreignKey.OnUpdate : "";
+ }
+
#region DDL
public override string ToAddColumnStatement(Type modelType, FieldDefinition fieldDef){
@@ -0,0 +1,194 @@
+using NUnit.Framework;
+using ServiceStack.DataAnnotations;
+
+namespace ServiceStack.OrmLite.FirebirdTests
+{
+ [TestFixture]
+ public class ForeignKeyAttributeTests : OrmLiteTestBase
+ {
+ [TestFixtureSetUp]
+ public void Setup()
+ {
+ using (var db = ConnectionString.OpenDbConnection())
+ {
+ db.CreateTable<ReferencedType>(true);
+ }
+ }
+
+ [Test]
+ public void CanCreateSimpleForeignKey()
+ {
+ using (var db = ConnectionString.OpenDbConnection())
+ {
+ db.CreateTable<TypeWithSimpleForeignKey>(true);
+ }
+ }
+
+ [Test]
+ public void CanCreateForeignWithOnDeleteCascade()
+ {
+ using (var db = ConnectionString.OpenDbConnection())
+ {
+ db.CreateTable<TypeWithOnDeleteCascade>(true);
+ }
+ }
+
+ [Test]
+ public void CascadesOnDelete()
+ {
+ using (var db = ConnectionString.OpenDbConnection())
+ {
+ db.CreateTable<TypeWithOnDeleteCascade>(true);
+
+ db.Save(new ReferencedType { Id = 1 });
+ db.Save(new TypeWithOnDeleteCascade { RefId = 1 });
+
+ Assert.AreEqual(1, db.Select<ReferencedType>().Count);
+ Assert.AreEqual(1, db.Select<TypeWithOnDeleteCascade>().Count);
+
+ db.Delete<ReferencedType>(r => r.Id == 1);
+
+ Assert.AreEqual(0, db.Select<ReferencedType>().Count);
+ Assert.AreEqual(0, db.Select<TypeWithOnDeleteCascade>().Count);
+ }
+ }
+
+ [Test]
+ public void CanCreateForeignWithOnDeleteCascadeAndOnUpdateCascade()
+ {
+ using (var db = ConnectionString.OpenDbConnection())
+ {
+ db.CreateTable<TypeWithOnDeleteAndUpdateCascade>(true);
+ }
+ }
+
+ [Test]
+ public void CanCreateForeignWithOnDeleteNoAction()
+ {
+ using (var db = ConnectionString.OpenDbConnection())
+ {
+ db.CreateTable<TypeWithOnDeleteNoAction>(true);
+ }
+ }
+
+ [Test]
+ public void CanCreateForeignWithOnDeleteRestrict()
+ {
+ using (var db = ConnectionString.OpenDbConnection())
+ {
+ db.CreateTable<TypeWithOnDeleteRestrict>(true);
+ }
+ }
+
+
+ [Test]
+ public void CanCreateForeignWithOnDeleteSetDefault()
+ {
+ using (var db = ConnectionString.OpenDbConnection())
+ {
+ db.CreateTable<TypeWithOnDeleteSetDefault>(true);
+ }
+ }
+
+ [Test]
+ public void CanCreateForeignWithOnDeleteSetNull()
+ {
+ using (var db = ConnectionString.OpenDbConnection())
+ {
+ db.CreateTable<TypeWithOnDeleteSetNull>(true);
+ }
+ }
+
+ [TestFixtureTearDown]
+ public void TearDwon()
+ {
+ using (var db = ConnectionString.OpenDbConnection())
+ {
+ db.DropTable<TypeWithOnDeleteAndUpdateCascade>();
+ db.DropTable<TypeWithOnDeleteSetNull>();
+ db.DropTable<TypeWithOnDeleteSetDefault>();
+ db.DropTable<TypeWithOnDeleteRestrict>();
+ db.DropTable<TypeWithOnDeleteNoAction>();
+ db.DropTable<TypeWithOnDeleteCascade>();
+ db.DropTable<TypeWithSimpleForeignKey>();
+ db.DropTable<ReferencedType>();
+ }
+ }
+ }
+
+ public class ReferencedType
+ {
+ public int Id { get; set; }
+ }
+
+
+ [Alias("TWSKF")]
+ public class TypeWithSimpleForeignKey
+ {
+ [AutoIncrement]
+ public int Id { get; set; }
+ [References(typeof(ReferencedType))]
+ public int RefId { get; set; }
+ }
+
+ [Alias("TWODC")]
+ public class TypeWithOnDeleteCascade
+ {
+ [AutoIncrement]
+ public int Id { get; set; }
+
+ [ForeignKey(typeof(ReferencedType), OnDelete = "CASCADE", ForeignKeyName="FK_DC")]
+ public int? RefId { get; set; }
+ }
+
+ [Alias("TWODUC")]
+ public class TypeWithOnDeleteAndUpdateCascade
+ {
+ [AutoIncrement]
+ public int Id { get; set; }
+
+ [ForeignKey(typeof(ReferencedType), OnDelete = "CASCADE", OnUpdate = "CASCADE", ForeignKeyName="FK_DC_UC")]
+ public int? RefId { get; set; }
+ }
+
+ [Alias("TWODNA")]
+ public class TypeWithOnDeleteNoAction
+ {
+ [AutoIncrement]
+ public int Id { get; set; }
+
+ [ForeignKey(typeof(ReferencedType), OnDelete = "NO ACTION", ForeignKeyName="FK_DNA")]
+ public int? RefId { get; set; }
+ }
+
+ [Alias("TWODNR")]
+ public class TypeWithOnDeleteRestrict
+ {
+ [AutoIncrement]
+ public int Id { get; set; }
+
+ [ForeignKey(typeof(ReferencedType), OnDelete = "RESTRICT", ForeignKeyName="FK_DR")]
+ public int? RefId { get; set; }
+ }
+
+ [Alias("TWODDF")]
+ public class TypeWithOnDeleteSetDefault
+ {
+ [AutoIncrement]
+ public int Id { get; set; }
+
+ [Default(typeof(int), "17")]
+ [ForeignKey(typeof(ReferencedType), OnDelete = "SET DEFAULT", ForeignKeyName="FK_DDF")]
+ public int RefId { get; set; }
+ }
+
+ [Alias("TWODSN")]
+ public class TypeWithOnDeleteSetNull
+ {
+ [AutoIncrement]
+ public int Id { get; set; }
+
+ [ForeignKey(typeof(ReferencedType), OnDelete = "SET NULL", ForeignKeyName="FKSN")]
+ public int? RefId { get; set; }
+ }
+}
@@ -14,7 +14,8 @@ public class OrmLiteTestBase
protected string GetFileConnectionString()
{
- return "User=SYSDBA;Password=masterkey;Database=D:\\ormlite-tests.fdb;DataSource=localhost;Dialect=3;charset=ISO8859_1;MinPoolSize=0;MaxPoolSize=100";
+ // add ormlite-tests.fdb = D:\\ormlite-tests.fdb to your firebird alias.conf
+ return "User=SYSDBA;Password=masterkey;Database=ormlite-tests.fdb;DataSource=localhost;Dialect=3;charset=ISO8859_1;MinPoolSize=0;MaxPoolSize=100";
}
protected void CreateNewDatabase()
@@ -11,22 +11,22 @@
<AssemblyName>ServiceStack.OrmLite.FirebirdTests</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
- <DebugSymbols>true</DebugSymbols>
+ <DebugSymbols>True</DebugSymbols>
<DebugType>full</DebugType>
- <Optimize>false</Optimize>
+ <Optimize>False</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
- <ConsolePause>false</ConsolePause>
+ <ConsolePause>False</ConsolePause>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
- <Optimize>false</Optimize>
+ <Optimize>False</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
- <ConsolePause>false</ConsolePause>
+ <ConsolePause>False</ConsolePause>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
@@ -119,6 +119,7 @@
<Compile Include="OrmLiteGetScalarTests.cs" />
<Compile Include="DateTimeColumnTest.cs" />
<Compile Include="OrmLiteCreateTableWithNamigStrategyTests.cs" />
+ <Compile Include="ForeignKeyAttributeTests.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>

0 comments on commit 8a05411

Please sign in to comment.