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

Commit 5127c45

Browse files
committed
Add support for new [CustomSelect] attribute
1 parent 2f657c6 commit 5127c45

File tree

8 files changed

+164
-8
lines changed

8 files changed

+164
-8
lines changed

lib/ServiceStack.Interfaces.dll

512 Bytes
Binary file not shown.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
using Microsoft.SqlServer.Types;
3+
using NUnit.Framework;
4+
using ServiceStack.DataAnnotations;
5+
using ServiceStack.Text;
6+
7+
namespace ServiceStack.OrmLite.SqlServerTests.Converters
8+
{
9+
[TestFixture]
10+
public class FileStreamTests : SqlServerConvertersOrmLiteTestBase
11+
{
12+
[Explicit("Requires FileGroups enabled in DB")]
13+
[Test]
14+
public void Can_select_from_FileStream()
15+
{
16+
using (var db = OpenDbConnection())
17+
{
18+
db.DropAndCreateTable<FileStream>();
19+
20+
db.Insert(new FileStream
21+
{
22+
ID = Guid.NewGuid(),
23+
Name = "file.txt",
24+
Path = SqlHierarchyId.Parse("/1/2/3/"),
25+
ParentPath = SqlHierarchyId.Parse("/1/2/"),
26+
FileContent = "contents".ToUtf8Bytes(),
27+
FileType = MimeTypes.PlainText,
28+
});
29+
30+
//db.Select<FileStream>().PrintDump();
31+
32+
var q = db.From<FileStream>();
33+
db.Select(q);
34+
}
35+
}
36+
}
37+
38+
39+
public class FileStream
40+
{
41+
[PrimaryKey]
42+
[Alias("stream_id")]
43+
public Guid ID { get; set; }
44+
45+
//[CustomField("varbinary(max) FILESTREAM")]
46+
[Alias("file_stream")]
47+
//[DataAnnotations.Ignore]
48+
public byte[] FileContent { get; set; }
49+
50+
[Alias("name")]
51+
[StringLength(255)]
52+
public string Name { get; set; }
53+
54+
[Alias("path_locator")]
55+
public SqlHierarchyId Path { get; set; }
56+
57+
//[ForeignKey(typeof(FileStream))]
58+
[Alias("parent_path_locator")]
59+
public SqlHierarchyId? ParentPath { get; set; }
60+
61+
[Alias("file_type")]
62+
[Compute]
63+
[StringLength(255)]
64+
public string FileType { get; set; }
65+
66+
[Alias("cached_file_size")]
67+
[Compute]
68+
public long? FileSize { get; set; }
69+
70+
[Alias("creation_time")]
71+
public DateTimeOffset CreationDateTime { get; set; }
72+
73+
[Alias("last_write_time")]
74+
public DateTimeOffset LastWriteDateTime { get; set; }
75+
76+
[Alias("last_access_time")]
77+
public DateTimeOffset? LastAccessDateTime { get; set; }
78+
79+
[Alias("is_directory")]
80+
public bool IsDirectory { get; set; }
81+
82+
[Alias("is_offline")]
83+
public bool IsOffline { get; set; }
84+
85+
[Alias("is_hidden")]
86+
public bool IsHidden { get; set; }
87+
88+
[Alias("is_readonly")]
89+
public bool IsReadOnly { get; set; }
90+
91+
[Alias("is_archive")]
92+
public bool IsArchive { get; set; }
93+
94+
[Alias("is_system")]
95+
public bool IsSystem { get; set; }
96+
97+
[Alias("is_temporary")]
98+
public bool IsTemporary { get; set; }
99+
100+
[CustomSelect("file_stream.GetFileNamespacePath() + (CASE WHEN is_directory = 1 THEN '\' ELSE '' END)")]
101+
public string FullPath { get; set; }
102+
}
103+
}

src/ServiceStack.OrmLite.SqlServerTests/ServiceStack.OrmLite.SqlServerTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
<Compile Include="..\GlobalAssemblyInfo.cs">
8181
<Link>Properties\GlobalAssemblyInfo.cs</Link>
8282
</Compile>
83+
<Compile Include="Converters\FileStreamTests.cs" />
8384
<Compile Include="CustomSqlTests.cs" />
8485
<Compile Include="Datetime2Tests.cs" />
8586
<Compile Include="DateTimeOffsetTests.cs" />

src/ServiceStack.OrmLite/Expressions/SqlExpression.Join.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,16 @@ public string SelectInto<TModel>()
189189
if (sbSelect.Length > 0)
190190
sbSelect.Append(", ");
191191

192-
sbSelect.AppendFormat("{0} as {1}",
192+
if (fieldDef.CustomSelect == null)
193+
{
194+
sbSelect.AppendFormat("{0} as {1}",
193195
DialectProvider.GetQuotedColumnName(tableDef, matchingField),
194196
SqlColumn(fieldDef.Name));
197+
}
198+
else
199+
{
200+
sbSelect.Append(fieldDef.CustomSelect + " AS " + fieldDef.FieldName);
201+
}
195202

196203
continue;
197204
}
@@ -211,12 +218,19 @@ public string SelectInto<TModel>()
211218
if (sbSelect.Length > 0)
212219
sbSelect.Append(", ");
213220

214-
sbSelect.AppendFormat("{0}.{1}",
215-
SqlTable(tableDef),
216-
tableFieldDef.GetQuotedName(DialectProvider));
221+
if (fieldDef.CustomSelect == null)
222+
{
223+
sbSelect.AppendFormat("{0}.{1}",
224+
SqlTable(tableDef),
225+
tableFieldDef.GetQuotedName(DialectProvider));
217226

218-
if (tableFieldDef.Alias != null)
219-
sbSelect.Append(" AS ").Append(SqlColumn(fieldDef.Name));
227+
if (tableFieldDef.Alias != null)
228+
sbSelect.Append(" AS ").Append(SqlColumn(fieldDef.Name));
229+
}
230+
else
231+
{
232+
sbSelect.Append(tableFieldDef.CustomSelect + " AS " + tableFieldDef.FieldName);
233+
}
220234

221235
found = true;
222236
break;

src/ServiceStack.OrmLite/FieldDefinition.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ public string GetQuotedValue(object fromInstance, IOrmLiteDialectProvider dialec
9090

9191
public string ComputeExpression { get; set; }
9292

93+
public string CustomSelect { get; set; }
94+
9395
public string BelongToModelName { get; set; }
9496

9597
public bool IsReference { get; set; }

src/ServiceStack.OrmLite/OrmLiteConfigExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ internal static ModelDefinition GetModelDefinition(this Type modelType)
9393

9494
var sequenceAttr = propertyInfo.FirstAttribute<SequenceAttribute>();
9595
var computeAttr = propertyInfo.FirstAttribute<ComputeAttribute>();
96+
var customSelectAttr = propertyInfo.FirstAttribute<CustomSelectAttribute>();
9697
var decimalAttribute = propertyInfo.FirstAttribute<DecimalLengthAttribute>();
9798
var belongToAttribute = propertyInfo.FirstAttribute<BelongToAttribute>();
9899
var isFirst = i++ == 0;
@@ -164,8 +165,9 @@ internal static ModelDefinition GetModelDefinition(this Type modelType)
164165
GetValueFn = propertyInfo.GetPropertyGetterFn(),
165166
SetValueFn = propertyInfo.GetPropertySetterFn(),
166167
Sequence = sequenceAttr != null ? sequenceAttr.Name : string.Empty,
167-
IsComputed = computeAttr != null,
168+
IsComputed = computeAttr != null || customSelectAttr != null,
168169
ComputeExpression = computeAttr != null ? computeAttr.Expression : string.Empty,
170+
CustomSelect = customSelectAttr != null ? customSelectAttr.Sql : null,
169171
Scale = decimalAttribute != null ? decimalAttribute.Scale : (int?)null,
170172
BelongToModelName = belongToAttribute != null ? belongToAttribute.BelongToTableType.GetModelDefinition().ModelName : null,
171173
CustomFieldDefinition = customFieldAttr != null ? customFieldAttr.Sql : null,

src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,14 @@ public virtual string GetColumnNames(ModelDefinition modelDef)
559559
if (sqlColumns.Length > 0)
560560
sqlColumns.Append(", ");
561561

562-
sqlColumns.Append(field.GetQuotedName(this));
562+
if (field.CustomSelect == null)
563+
{
564+
sqlColumns.Append(field.GetQuotedName(this));
565+
}
566+
else
567+
{
568+
sqlColumns.Append(field.CustomSelect + " AS " + field.FieldName);
569+
}
563570
}
564571

565572
return StringBuilderCache.ReturnAndFree(sqlColumns);
@@ -1106,6 +1113,9 @@ public virtual string ToCreateTableStatement(Type tableType)
11061113
var modelDef = tableType.GetModelDefinition();
11071114
foreach (var fieldDef in modelDef.FieldDefinitions)
11081115
{
1116+
if (fieldDef.CustomSelect != null)
1117+
continue;
1118+
11091119
var columnDefinition = GetColumnDefinition(
11101120
fieldDef.FieldName,
11111121
fieldDef.ColumnType,

tests/ServiceStack.OrmLite.Tests/CustomSqlTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,5 +179,29 @@ public void Does_execute_CustomSql_after_table_dropped()
179179
}
180180
}
181181

182+
public class CustomSelectTest
183+
{
184+
public int Id { get; set; }
185+
public int Width { get; set; }
186+
public int Height { get; set; }
187+
188+
[CustomSelect("Width * Height")]
189+
public int Area { get; set; }
190+
}
191+
192+
[Test]
193+
public void Can_select_custom_field_expressions()
194+
{
195+
using (var db = OpenDbConnection())
196+
{
197+
db.DropAndCreateTable<CustomSelectTest>();
198+
199+
db.Insert(new CustomSelectTest { Id = 1, Width = 10, Height = 5 });
200+
201+
var row = db.SingleById<CustomSelectTest>(1);
202+
203+
Assert.That(row.Area, Is.EqualTo(10 * 5));
204+
}
205+
}
182206
}
183207
}

0 commit comments

Comments
 (0)