Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 78703bf

Browse files
committed
Merge pull request #206 from angelcolmenares/master
FirebirdDialect : added missing features ...
2 parents 25a1933 + 8a05411 commit 78703bf

File tree

4 files changed

+223
-13
lines changed

4 files changed

+223
-13
lines changed

src/ServiceStack.OrmLite.Firebird/FirebirdOrmLiteDialectProvider.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public FirebirdOrmLiteDialectProvider(bool compactGuid)
4040
base.RealColumnDefinition= "FLOAT";
4141
base.DefaultStringLength=128;
4242
base.InitColumnTypeMap();
43+
DefaultValueFormat = " DEFAULT '{0}'";
4344
}
4445

4546
public override IDbConnection CreateConnection(string connectionString, Dictionary<string, string> options)
@@ -340,6 +341,9 @@ public override string ToCreateTableStatement(Type tableType)
340341
GetQuotedColumnName(fieldDef.FieldName),
341342
GetQuotedTableName(refModelDef),
342343
GetQuotedColumnName(refModelDef.PrimaryKey.FieldName));
344+
345+
sbConstraints.Append(GetForeignKeyOnDeleteClause(fieldDef.ForeignKey));
346+
sbConstraints.Append(GetForeignKeyOnUpdateClause(fieldDef.ForeignKey));
343347
}
344348

345349
if (sbPk.Length !=0) sbColumns.AppendFormat(", \n PRIMARY KEY({0})", sbPk);
@@ -394,16 +398,16 @@ public override string GetColumnDefinition (string fieldName, Type fieldType,
394398

395399
var sql = new StringBuilder();
396400
sql.AppendFormat("{0} {1}", GetQuotedColumnName(fieldName), fieldDefinition);
397-
401+
402+
if (!string.IsNullOrEmpty(defaultValue))
403+
{
404+
sql.AppendFormat(DefaultValueFormat, defaultValue);
405+
}
406+
398407
if (!isNullable)
399408
{
400409
sql.Append(" NOT NULL");
401-
}
402-
403-
if (!string.IsNullOrEmpty(defaultValue))
404-
{
405-
sql.AppendFormat(DefaultValueFormat, defaultValue);
406-
}
410+
}
407411

408412
return sql.ToString();
409413
}
@@ -727,6 +731,16 @@ public override bool DoesTableExist(IDbCommand dbCmd, string tableName)
727731
}
728732

729733

734+
public override string GetForeignKeyOnDeleteClause(ForeignKeyConstraint foreignKey)
735+
{
736+
return (!string.IsNullOrEmpty(foreignKey.OnDelete) && foreignKey.OnDelete.ToUpper()!="RESTRICT" )? " ON DELETE " + foreignKey.OnDelete : "";
737+
}
738+
739+
public override string GetForeignKeyOnUpdateClause(ForeignKeyConstraint foreignKey)
740+
{
741+
return (!string.IsNullOrEmpty(foreignKey.OnUpdate) && foreignKey.OnUpdate.ToUpper()!="RESTRICT" )? " ON UPDATE " + foreignKey.OnUpdate : "";
742+
}
743+
730744
#region DDL
731745
public override string ToAddColumnStatement(Type modelType, FieldDefinition fieldDef){
732746

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
using NUnit.Framework;
2+
using ServiceStack.DataAnnotations;
3+
4+
namespace ServiceStack.OrmLite.FirebirdTests
5+
{
6+
[TestFixture]
7+
public class ForeignKeyAttributeTests : OrmLiteTestBase
8+
{
9+
[TestFixtureSetUp]
10+
public void Setup()
11+
{
12+
using (var db = ConnectionString.OpenDbConnection())
13+
{
14+
db.CreateTable<ReferencedType>(true);
15+
}
16+
}
17+
18+
[Test]
19+
public void CanCreateSimpleForeignKey()
20+
{
21+
using (var db = ConnectionString.OpenDbConnection())
22+
{
23+
db.CreateTable<TypeWithSimpleForeignKey>(true);
24+
}
25+
}
26+
27+
[Test]
28+
public void CanCreateForeignWithOnDeleteCascade()
29+
{
30+
using (var db = ConnectionString.OpenDbConnection())
31+
{
32+
db.CreateTable<TypeWithOnDeleteCascade>(true);
33+
}
34+
}
35+
36+
[Test]
37+
public void CascadesOnDelete()
38+
{
39+
using (var db = ConnectionString.OpenDbConnection())
40+
{
41+
db.CreateTable<TypeWithOnDeleteCascade>(true);
42+
43+
db.Save(new ReferencedType { Id = 1 });
44+
db.Save(new TypeWithOnDeleteCascade { RefId = 1 });
45+
46+
Assert.AreEqual(1, db.Select<ReferencedType>().Count);
47+
Assert.AreEqual(1, db.Select<TypeWithOnDeleteCascade>().Count);
48+
49+
db.Delete<ReferencedType>(r => r.Id == 1);
50+
51+
Assert.AreEqual(0, db.Select<ReferencedType>().Count);
52+
Assert.AreEqual(0, db.Select<TypeWithOnDeleteCascade>().Count);
53+
}
54+
}
55+
56+
[Test]
57+
public void CanCreateForeignWithOnDeleteCascadeAndOnUpdateCascade()
58+
{
59+
using (var db = ConnectionString.OpenDbConnection())
60+
{
61+
db.CreateTable<TypeWithOnDeleteAndUpdateCascade>(true);
62+
}
63+
}
64+
65+
[Test]
66+
public void CanCreateForeignWithOnDeleteNoAction()
67+
{
68+
using (var db = ConnectionString.OpenDbConnection())
69+
{
70+
db.CreateTable<TypeWithOnDeleteNoAction>(true);
71+
}
72+
}
73+
74+
[Test]
75+
public void CanCreateForeignWithOnDeleteRestrict()
76+
{
77+
using (var db = ConnectionString.OpenDbConnection())
78+
{
79+
db.CreateTable<TypeWithOnDeleteRestrict>(true);
80+
}
81+
}
82+
83+
84+
[Test]
85+
public void CanCreateForeignWithOnDeleteSetDefault()
86+
{
87+
using (var db = ConnectionString.OpenDbConnection())
88+
{
89+
db.CreateTable<TypeWithOnDeleteSetDefault>(true);
90+
}
91+
}
92+
93+
[Test]
94+
public void CanCreateForeignWithOnDeleteSetNull()
95+
{
96+
using (var db = ConnectionString.OpenDbConnection())
97+
{
98+
db.CreateTable<TypeWithOnDeleteSetNull>(true);
99+
}
100+
}
101+
102+
[TestFixtureTearDown]
103+
public void TearDwon()
104+
{
105+
using (var db = ConnectionString.OpenDbConnection())
106+
{
107+
db.DropTable<TypeWithOnDeleteAndUpdateCascade>();
108+
db.DropTable<TypeWithOnDeleteSetNull>();
109+
db.DropTable<TypeWithOnDeleteSetDefault>();
110+
db.DropTable<TypeWithOnDeleteRestrict>();
111+
db.DropTable<TypeWithOnDeleteNoAction>();
112+
db.DropTable<TypeWithOnDeleteCascade>();
113+
db.DropTable<TypeWithSimpleForeignKey>();
114+
db.DropTable<ReferencedType>();
115+
}
116+
}
117+
}
118+
119+
public class ReferencedType
120+
{
121+
public int Id { get; set; }
122+
}
123+
124+
125+
[Alias("TWSKF")]
126+
public class TypeWithSimpleForeignKey
127+
{
128+
[AutoIncrement]
129+
public int Id { get; set; }
130+
[References(typeof(ReferencedType))]
131+
public int RefId { get; set; }
132+
}
133+
134+
[Alias("TWODC")]
135+
public class TypeWithOnDeleteCascade
136+
{
137+
[AutoIncrement]
138+
public int Id { get; set; }
139+
140+
[ForeignKey(typeof(ReferencedType), OnDelete = "CASCADE", ForeignKeyName="FK_DC")]
141+
public int? RefId { get; set; }
142+
}
143+
144+
[Alias("TWODUC")]
145+
public class TypeWithOnDeleteAndUpdateCascade
146+
{
147+
[AutoIncrement]
148+
public int Id { get; set; }
149+
150+
[ForeignKey(typeof(ReferencedType), OnDelete = "CASCADE", OnUpdate = "CASCADE", ForeignKeyName="FK_DC_UC")]
151+
public int? RefId { get; set; }
152+
}
153+
154+
[Alias("TWODNA")]
155+
public class TypeWithOnDeleteNoAction
156+
{
157+
[AutoIncrement]
158+
public int Id { get; set; }
159+
160+
[ForeignKey(typeof(ReferencedType), OnDelete = "NO ACTION", ForeignKeyName="FK_DNA")]
161+
public int? RefId { get; set; }
162+
}
163+
164+
[Alias("TWODNR")]
165+
public class TypeWithOnDeleteRestrict
166+
{
167+
[AutoIncrement]
168+
public int Id { get; set; }
169+
170+
[ForeignKey(typeof(ReferencedType), OnDelete = "RESTRICT", ForeignKeyName="FK_DR")]
171+
public int? RefId { get; set; }
172+
}
173+
174+
[Alias("TWODDF")]
175+
public class TypeWithOnDeleteSetDefault
176+
{
177+
[AutoIncrement]
178+
public int Id { get; set; }
179+
180+
[Default(typeof(int), "17")]
181+
[ForeignKey(typeof(ReferencedType), OnDelete = "SET DEFAULT", ForeignKeyName="FK_DDF")]
182+
public int RefId { get; set; }
183+
}
184+
185+
[Alias("TWODSN")]
186+
public class TypeWithOnDeleteSetNull
187+
{
188+
[AutoIncrement]
189+
public int Id { get; set; }
190+
191+
[ForeignKey(typeof(ReferencedType), OnDelete = "SET NULL", ForeignKeyName="FKSN")]
192+
public int? RefId { get; set; }
193+
}
194+
}

tests/ServiceStack.OrmLite.FirebirdTests/OrmLiteTestBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public class OrmLiteTestBase
1414

1515
protected string GetFileConnectionString()
1616
{
17-
return "User=SYSDBA;Password=masterkey;Database=D:\\ormlite-tests.fdb;DataSource=localhost;Dialect=3;charset=ISO8859_1;MinPoolSize=0;MaxPoolSize=100";
17+
// add ormlite-tests.fdb = D:\\ormlite-tests.fdb to your firebird alias.conf
18+
return "User=SYSDBA;Password=masterkey;Database=ormlite-tests.fdb;DataSource=localhost;Dialect=3;charset=ISO8859_1;MinPoolSize=0;MaxPoolSize=100";
1819
}
1920

2021
protected void CreateNewDatabase()

tests/ServiceStack.OrmLite.FirebirdTests/ServiceStack.OrmLite.FirebirdTests.csproj

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@
1111
<AssemblyName>ServiceStack.OrmLite.FirebirdTests</AssemblyName>
1212
</PropertyGroup>
1313
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
14-
<DebugSymbols>true</DebugSymbols>
14+
<DebugSymbols>True</DebugSymbols>
1515
<DebugType>full</DebugType>
16-
<Optimize>false</Optimize>
16+
<Optimize>False</Optimize>
1717
<OutputPath>bin\Debug</OutputPath>
1818
<DefineConstants>DEBUG;</DefineConstants>
1919
<ErrorReport>prompt</ErrorReport>
2020
<WarningLevel>4</WarningLevel>
21-
<ConsolePause>false</ConsolePause>
21+
<ConsolePause>False</ConsolePause>
2222
</PropertyGroup>
2323
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2424
<DebugType>none</DebugType>
25-
<Optimize>false</Optimize>
25+
<Optimize>False</Optimize>
2626
<OutputPath>bin\Release</OutputPath>
2727
<ErrorReport>prompt</ErrorReport>
2828
<WarningLevel>4</WarningLevel>
29-
<ConsolePause>false</ConsolePause>
29+
<ConsolePause>False</ConsolePause>
3030
</PropertyGroup>
3131
<ItemGroup>
3232
<Reference Include="System" />
@@ -119,6 +119,7 @@
119119
<Compile Include="OrmLiteGetScalarTests.cs" />
120120
<Compile Include="DateTimeColumnTest.cs" />
121121
<Compile Include="OrmLiteCreateTableWithNamigStrategyTests.cs" />
122+
<Compile Include="ForeignKeyAttributeTests.cs" />
122123
</ItemGroup>
123124
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
124125
<ItemGroup>

0 commit comments

Comments
 (0)