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

Commit 24e1c00

Browse files
committed
Add support for multiple [Pre/Post/Create/Drop/Table] attributes
1 parent 94a2270 commit 24e1c00

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

src/ServiceStack.OrmLite/OrmLiteConfigExtensions.cs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Threading;
1818
using ServiceStack.DataAnnotations;
1919
using ServiceStack.OrmLite.Converters;
20+
using ServiceStack.Text;
2021

2122
namespace ServiceStack.OrmLite
2223
{
@@ -51,21 +52,36 @@ internal static ModelDefinition GetModelDefinition(this Type modelType)
5152
var modelAliasAttr = modelType.FirstAttribute<AliasAttribute>();
5253
var schemaAttr = modelType.FirstAttribute<SchemaAttribute>();
5354

54-
var preCreate = modelType.FirstAttribute<PreCreateTableAttribute>();
55-
var postCreate = modelType.FirstAttribute<PostCreateTableAttribute>();
56-
var preDrop = modelType.FirstAttribute<PreDropTableAttribute>();
57-
var postDrop = modelType.FirstAttribute<PostDropTableAttribute>();
55+
var preCreates = modelType.AllAttributes<PreCreateTableAttribute>();
56+
var postCreates = modelType.AllAttributes<PostCreateTableAttribute>();
57+
var preDrops = modelType.AllAttributes<PreDropTableAttribute>();
58+
var postDrops = modelType.AllAttributes<PostDropTableAttribute>();
59+
60+
string JoinSql(List<string> statements)
61+
{
62+
if (statements.Count == 0)
63+
return null;
64+
var sb = StringBuilderCache.Allocate();
65+
foreach (var sql in statements)
66+
{
67+
if (sb.Length > 0)
68+
sb.AppendLine(";");
69+
sb.Append(sql);
70+
}
71+
var to = StringBuilderCache.ReturnAndFree(sb);
72+
return to;
73+
}
5874

5975
modelDef = new ModelDefinition
6076
{
6177
ModelType = modelType,
6278
Name = modelType.Name,
6379
Alias = modelAliasAttr?.Name,
6480
Schema = schemaAttr?.Name,
65-
PreCreateTableSql = preCreate?.Sql,
66-
PostCreateTableSql = postCreate?.Sql,
67-
PreDropTableSql = preDrop?.Sql,
68-
PostDropTableSql = postDrop?.Sql,
81+
PreCreateTableSql = JoinSql(preCreates.Map(x => x.Sql)),
82+
PostCreateTableSql = JoinSql(postCreates.Map(x => x.Sql)),
83+
PreDropTableSql = JoinSql(preDrops.Map(x => x.Sql)),
84+
PostDropTableSql = JoinSql(postDrops.Map(x => x.Sql)),
6985
};
7086

7187
modelDef.CompositeIndexes.AddRange(

tests/ServiceStack.OrmLite.Tests/CustomSqlTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ public class ModelWithSeedDataSql
4444
public string Name { get; set; }
4545
}
4646

47+
[PostCreateTable("INSERT INTO ModelWithSeedDataSqlMulti (Name) VALUES ('Foo')"),
48+
PostCreateTable("INSERT INTO ModelWithSeedDataSqlMulti (Name) VALUES ('Bar')")]
49+
public class ModelWithSeedDataSqlMulti
50+
{
51+
[AutoIncrement]
52+
public int Id { get; set; }
53+
54+
public string Name { get; set; }
55+
}
56+
4757
public class DynamicAttributeSeedData
4858
{
4959
[AutoIncrement]
@@ -126,6 +136,18 @@ public void Does_execute_CustomSql_after_table_created()
126136
Assert.That(seedDataNames, Is.EquivalentTo(new[] {"Foo", "Bar"}));
127137
}
128138

139+
[Test]
140+
[IgnoreDialect(Dialect.AnyOracle | Dialect.AnyPostgreSql, "multiple SQL statements need to be wrapped in an anonymous block")]
141+
public void Does_execute_multi_CustomSql_after_table_created()
142+
{
143+
using var db = OpenDbConnection();
144+
db.DropAndCreateTable<ModelWithSeedDataSqlMulti>();
145+
146+
var seedDataNames = db.Select<ModelWithSeedDataSqlMulti>().ConvertAll(x => x.Name);
147+
148+
Assert.That(seedDataNames, Is.EquivalentTo(new[] {"Foo", "Bar"}));
149+
}
150+
129151
[Test]
130152
[IgnoreDialect(Dialect.AnyOracle | Dialect.AnyPostgreSql, "multiple SQL statements need to be wrapped in an anonymous block")]
131153
public void Does_execute_CustomSql_after_table_created_using_dynamic_attribute()

0 commit comments

Comments
 (0)