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

Commit 0994624

Browse files
committed
Add support for Check Constraints
1 parent de8a2f0 commit 0994624

File tree

6 files changed

+88
-0
lines changed

6 files changed

+88
-0
lines changed

lib/ServiceStack.Interfaces.dll

1 KB
Binary file not shown.

src/ServiceStack.OrmLite/FieldDefinition.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public class FieldDefinition
5454

5555
public string DefaultValue { get; set; }
5656

57+
public string CheckConstraint { get; set; }
58+
5759
public ForeignKeyConstraint ForeignKey { get; set; }
5860

5961
public PropertyGetterDelegate GetValueFn { get; set; }

src/ServiceStack.OrmLite/OrmLiteConfigExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ internal static ModelDefinition GetModelDefinition(this Type modelType)
126126
var referenceAttr = propertyInfo.FirstAttribute<ReferenceAttribute>();
127127
var fkAttr = propertyInfo.FirstAttribute<ForeignKeyAttribute>();
128128
var customFieldAttr = propertyInfo.FirstAttribute<CustomFieldAttribute>();
129+
var chkConstraintAttr = propertyInfo.FirstAttribute<CheckConstraintAttribute>();
129130

130131
var fieldDefinition = new FieldDefinition
131132
{
@@ -147,6 +148,7 @@ internal static ModelDefinition GetModelDefinition(this Type modelType)
147148
IsRowVersion = isRowVersion,
148149
FieldLength = stringLengthAttr?.MaximumLength,
149150
DefaultValue = defaultValueAttr?.DefaultValue,
151+
CheckConstraint = chkConstraintAttr?.Constraint,
150152
ForeignKey = fkAttr == null
151153
? referencesAttr != null ? new ForeignKeyConstraint(referencesAttr.Type) : null
152154
: new ForeignKeyConstraint(fkAttr.Type, fkAttr.OnDelete, fkAttr.OnUpdate, fkAttr.ForeignKeyName),

src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,12 @@ public virtual string ToCreateTableStatement(Type tableType)
12281228

12291229
sbColumns.Append(columnDefinition);
12301230

1231+
var sqlConstraint = GetCheckConstraint(fieldDef);
1232+
if (sqlConstraint != null)
1233+
{
1234+
sbConstraints.Append(",\n" + sqlConstraint);
1235+
}
1236+
12311237
if (fieldDef.ForeignKey == null || OrmLiteConfig.SkipForeignKeys)
12321238
continue;
12331239

@@ -1246,6 +1252,14 @@ public virtual string ToCreateTableStatement(Type tableType)
12461252
return sql;
12471253
}
12481254

1255+
public virtual string GetCheckConstraint(FieldDefinition fieldDef)
1256+
{
1257+
if (fieldDef.CheckConstraint == null)
1258+
return null;
1259+
1260+
return $"CONSTRAINT CHK_{fieldDef.FieldName} CHECK ({fieldDef.CheckConstraint})";
1261+
}
1262+
12491263
public virtual string ToPostCreateTableStatement(ModelDefinition modelDef)
12501264
{
12511265
return null;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using NUnit.Framework;
3+
using ServiceStack.DataAnnotations;
4+
using ServiceStack.Logging;
5+
using ServiceStack.Text;
6+
7+
namespace ServiceStack.OrmLite.Tests
8+
{
9+
public class CheckConstraintTest
10+
{
11+
[AutoIncrement]
12+
public int Id { get; set; }
13+
14+
[Required]
15+
[CheckConstraint("Age > 1")]
16+
public int Age { get; set; }
17+
18+
[CheckConstraint("Name IS NOT NULL")]
19+
public string Name { get; set; }
20+
}
21+
22+
public class CheckConstraintTests : OrmLiteTestBase
23+
{
24+
[Test]
25+
public void Does_create_table_with_CheckConstraints()
26+
{
27+
if (Dialect == Dialect.MySql) return; //parsed but not supported http://stackoverflow.com/a/2115641/85785
28+
29+
using (var db = OpenDbConnection())
30+
{
31+
db.DropAndCreateTable<CheckConstraintTest>();
32+
33+
try
34+
{
35+
db.Insert(new CheckConstraintTest { Age = 1 });
36+
Assert.Fail("Should fail");
37+
}
38+
catch (Exception ex)
39+
{
40+
Assert.That(ex.Message.ToLower(), Does.Contain("age"));
41+
Assert.That(ex.Message.ToLower(), Does.Contain("constraint"));
42+
}
43+
44+
try
45+
{
46+
db.Insert(new CheckConstraintTest { Age = 2 });
47+
Assert.Fail("Should fail");
48+
}
49+
catch (Exception ex)
50+
{
51+
ex.Message.Print();
52+
Assert.That(ex.Message.ToLower(), Does.Contain("name"));
53+
Assert.That(ex.Message.ToLower(), Does.Contain("constraint"));
54+
}
55+
}
56+
}
57+
58+
[Test]
59+
public void Can_insert_record_passing_check_constraints()
60+
{
61+
using (var db = OpenDbConnection())
62+
{
63+
db.DropAndCreateTable<CheckConstraintTest>();
64+
65+
db.Insert(new CheckConstraintTest { Age = 2, Name = "foo" });
66+
}
67+
}
68+
}
69+
}

tests/ServiceStack.OrmLite.Tests/ServiceStack.OrmLite.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
<Compile Include="Async\SqlExpressionTests.cs" />
137137
<Compile Include="Async\SqlServerProviderTestsAsync.cs" />
138138
<Compile Include="Async\UpdateAsyncTests.cs" />
139+
<Compile Include="CheckConstraintTests.cs" />
139140
<Compile Include="CustomSqlExpressionTests.cs" />
140141
<Compile Include="Expression\JoinAliasTests.cs" />
141142
<Compile Include="Expression\RestrictionTests.cs" />

0 commit comments

Comments
 (0)