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

Commit 1f4e1fc

Browse files
authored
Support query tags,like efcore (#659)
* add ToString format paramater support for MySql DateTime * Add tests for DateTime.ToString() format parameter in Mysql * Improve the implementation of FloatConverter to fix the conversion failure problem in some cases. * Enhance Insert/InsertAsync, and support manual-specified value of auto-incrementing primary key of generic type. * Adjust ModelDefinition<T>.Definition.IsPrimaryKey To ModelDefinition<T>.Definition.FieldDefinitions.FirstOrDefault(f => f.IsPrimaryKey) to avoid System.InvalidOperationException on models without a primary key field. * Support query tags,like efcore see:https://docs.microsoft.com/en-us/ef/core/querying/tags
1 parent 246b179 commit 1f4e1fc

11 files changed

Lines changed: 186 additions & 37 deletions

File tree

src/ServiceStack.OrmLite.Firebird/FirebirdOrmLiteDialectProvider.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -793,11 +793,13 @@ public override string ToSelectStatement(QueryType queryType, ModelDefinition mo
793793
string bodyExpression,
794794
string orderByExpression = null,
795795
int? offset = null,
796-
int? rows = null)
796+
int? rows = null,
797+
ISet<string> tags=null)
797798
{
799+
var sb = StringBuilderCache.Allocate();
800+
ApplyTags(sb,tags);
798801

799-
var sb = StringBuilderCache.Allocate()
800-
.Append(selectExpression)
802+
sb.Append(selectExpression)
801803
.Append(bodyExpression);
802804

803805
if (!string.IsNullOrEmpty(orderByExpression))

src/ServiceStack.OrmLite.Oracle/OracleOrmLiteDialectProvider.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,10 +1065,13 @@ public override string ToSelectStatement(QueryType queryType, ModelDefinition mo
10651065
string bodyExpression,
10661066
string orderByExpression = null,
10671067
int? offset = null,
1068-
int? rows = null)
1068+
int? rows = null,
1069+
ISet<string> tags=null)
10691070
{
1070-
var sbInner = StringBuilderCache.Allocate()
1071-
.Append(selectExpression);
1071+
var sbInner = StringBuilderCache.Allocate();
1072+
ApplyTags(sbInner, tags);
1073+
1074+
sbInner.Append(selectExpression);
10721075
if (!bodyExpression.StartsWith(" ") && !bodyExpression.StartsWith("\n")
10731076
&& !selectExpression.EndsWith(" ") && !selectExpression.EndsWith("\n"))
10741077
{

src/ServiceStack.OrmLite.SqlServer/SqlServer2012OrmLiteDialectProvider.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,14 @@ public override string ToSelectStatement(QueryType queryType, ModelDefinition mo
8080
string bodyExpression,
8181
string orderByExpression = null,
8282
int? offset = null,
83-
int? rows = null)
83+
int? rows = null,
84+
ISet<string> tags=null)
8485
{
85-
var sb = StringBuilderCache.Allocate()
86-
.Append(selectExpression)
87-
.Append(bodyExpression);
86+
var sb = StringBuilderCache.Allocate();
87+
ApplyTags(sb, tags);
88+
89+
sb.Append(selectExpression)
90+
.Append(bodyExpression);
8891

8992
if (!string.IsNullOrEmpty(orderByExpression))
9093
sb.Append(orderByExpression);

src/ServiceStack.OrmLite.SqlServer/SqlServerOrmLiteDialectProvider.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -562,12 +562,15 @@ public override string ToSelectStatement(QueryType queryType, ModelDefinition mo
562562
string bodyExpression,
563563
string orderByExpression = null,
564564
int? offset = null,
565-
int? rows = null)
565+
int? rows = null,
566+
ISet<string> tags=null)
566567
{
567-
var sb = StringBuilderCache.Allocate()
568-
.Append(selectExpression)
569-
.Append(bodyExpression);
570-
568+
var sb = StringBuilderCache.Allocate();
569+
ApplyTags(sb, tags);
570+
571+
sb.Append(selectExpression)
572+
.Append(bodyExpression);
573+
571574
if (!offset.HasValue && !rows.HasValue || (queryType != QueryType.Select && rows != 1))
572575
return StringBuilderCache.ReturnAndFree(sb) + orderByExpression;
573576

src/ServiceStack.OrmLite.VistaDB/VistaDbDialectProvider.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,11 +390,13 @@ public override string ToChangeColumnNameStatement(Type modelType, FieldDefiniti
390390

391391
/// Limit/Offset paging logic needs to be implemented here:
392392
public override string ToSelectStatement(QueryType queryType, ModelDefinition modelDef, string selectExpression,
393-
string bodyExpression, string orderByExpression = null, int? offset = null, int? rows = null)
393+
string bodyExpression, string orderByExpression = null, int? offset = null, int? rows = null,ISet<string> tags=null)
394394
{
395-
var sb = StringBuilderCache.Allocate()
396-
.Append(selectExpression)
397-
.Append(bodyExpression);
395+
var sb = StringBuilderCache.Allocate();
396+
ApplyTags(sb, tags);
397+
398+
sb.Append(selectExpression)
399+
.Append(bodyExpression);
398400

399401
var hasOrderBy = !string.IsNullOrWhiteSpace(orderByExpression);
400402

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Collections.ObjectModel;
55
using System.Data;
6+
using System.Diagnostics;
67
using System.Linq;
78
using System.Reflection;
89
using System.Text;
@@ -42,6 +43,7 @@ public abstract partial class SqlExpression<T> : ISqlExpression, IHasUntypedSqlE
4243
public bool PrefixFieldWithTableName { get; set; }
4344
public bool UseSelectPropertiesAsAliases { get; set; }
4445
public bool WhereStatementWithoutWhereString { get; set; }
46+
public ISet<string> Tags { get; } = new HashSet<string>();
4547

4648
protected bool CustomSelect { get; set; }
4749
protected bool useFieldName = false;
@@ -76,6 +78,12 @@ public SqlExpression<T> Clone()
7678
return CopyTo(DialectProvider.SqlExpression<T>());
7779
}
7880

81+
public virtual void AddTag(string tag)
82+
{
83+
Debug.Assert(!string.IsNullOrEmpty(tag));
84+
Tags.Add(tag);
85+
}
86+
7987
protected virtual SqlExpression<T> CopyTo(SqlExpression<T> to)
8088
{
8189
to.modelDef = modelDef;
@@ -1476,7 +1484,7 @@ public virtual string ToSelectStatement(QueryType forType)
14761484
OrmLiteConfig.SqlExpressionSelectFilter?.Invoke(GetUntyped());
14771485

14781486
var sql = DialectProvider
1479-
.ToSelectStatement(forType, modelDef, SelectExpression, BodyExpression, OrderByExpression, offset: Offset, rows: Rows);
1487+
.ToSelectStatement(forType, modelDef, SelectExpression, BodyExpression, OrderByExpression, offset: Offset, rows: Rows,Tags);
14801488

14811489
return SqlFilter != null
14821490
? SqlFilter(sql)

src/ServiceStack.OrmLite/IOrmLiteDialectProvider.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,15 @@ public interface IOrmLiteDialectProvider
118118

119119
string ToSelectStatement(Type tableType, string sqlFilter, params object[] filterParams);
120120

121-
string ToSelectStatement(QueryType queryType, ModelDefinition modelDef, string selectExpression,
122-
string bodyExpression, string orderByExpression = null, int? offset = null, int? rows = null);
121+
string ToSelectStatement(
122+
QueryType queryType,
123+
ModelDefinition modelDef,
124+
string selectExpression,
125+
string bodyExpression,
126+
string orderByExpression = null,
127+
int? offset = null,
128+
int? rows = null,
129+
ISet<string> tags=null);
123130

124131
string ToInsertRowStatement(IDbCommand cmd, object objWithProperties, ICollection<string> insertFields = null);
125132

@@ -265,5 +272,12 @@ string ToCreateIndexStatement<T>(Expression<Func<T, object>> field,
265272
string SqlLimit(int? offset = null, int? rows = null);
266273
string SqlCast(object fieldOrValue, string castAs);
267274
string SqlRandom { get; }
275+
276+
/// <summary>
277+
/// Generates a SQL comment.
278+
/// </summary>
279+
/// <param name="text">The comment text.</param>
280+
/// <returns>The generated SQL.</returns>
281+
string GenerateComment(in string text);
268282
}
269283
}

src/ServiceStack.OrmLite/OrmLiteCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace ServiceStack.OrmLite
55
{
66
public class OrmLiteCommand : IDbCommand, IHasDbCommand, IHasDialectProvider
77
{
8-
private OrmLiteConnection dbConn;
8+
private readonly OrmLiteConnection dbConn;
99
private readonly IDbCommand dbCmd;
1010
public IOrmLiteDialectProvider DialectProvider { get; set; }
1111
public bool IsDisposed { get; private set; }

src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,15 +513,32 @@ public virtual string ToSelectStatement(Type tableType, string sqlFilter, params
513513
return StringBuilderCache.ReturnAndFree(sql);
514514
}
515515

516-
public virtual string ToSelectStatement(QueryType queryType, ModelDefinition modelDef,
516+
protected virtual void ApplyTags(StringBuilder sqlBuilder, ISet<string> tags)
517+
{
518+
if (tags != null && tags.Count > 0)
519+
{
520+
foreach (var tag in tags)
521+
{
522+
sqlBuilder.AppendLine(GenerateComment(tag));
523+
}
524+
sqlBuilder.Append("\n");
525+
}
526+
}
527+
528+
public virtual string ToSelectStatement(
529+
QueryType queryType,
530+
ModelDefinition modelDef,
517531
string selectExpression,
518532
string bodyExpression,
519533
string orderByExpression = null,
520534
int? offset = null,
521-
int? rows = null)
535+
int? rows = null,
536+
ISet<string> tags = null)
522537
{
523-
524538
var sb = StringBuilderCache.Allocate();
539+
540+
ApplyTags(sb, tags);
541+
525542
sb.Append(selectExpression);
526543
sb.Append(bodyExpression);
527544
if (!string.IsNullOrEmpty(orderByExpression))
@@ -538,6 +555,11 @@ public virtual string ToSelectStatement(QueryType queryType, ModelDefinition mod
538555
return StringBuilderCache.ReturnAndFree(sb);
539556
}
540557

558+
public virtual string GenerateComment(in string text)
559+
{
560+
return $"-- {text}";
561+
}
562+
541563
public virtual SelectItem GetRowVersionSelectColumn(FieldDefinition field, string tablePrefix = null)
542564
{
543565
return new SelectItemColumn(this, field.FieldName, null, tablePrefix);

src/ServiceStack.OrmLite/OrmLiteReadExpressionsApi.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Data;
55
using System.Linq;
66
using System.Linq.Expressions;
7+
using System.Runtime.CompilerServices;
78
using System.Threading;
89
using System.Threading.Tasks;
910
using ServiceStack.Text;
@@ -90,6 +91,20 @@ public static SqlExpression<T> From<T>(this IDbConnection dbConn, TableOptions t
9091
return expr;
9192
}
9293

94+
public static SqlExpression<T> TagWith<T>(this SqlExpression<T> expression,string tag)
95+
{
96+
expression.AddTag(tag);
97+
return expression;
98+
}
99+
100+
public static SqlExpression<T> TagWithCallSite<T>(this SqlExpression<T> expression,
101+
[CallerFilePath] string filePath = null,
102+
[CallerLineNumber] int lineNumber = 0)
103+
{
104+
expression.AddTag($"File: {filePath}:{lineNumber.ToString()}");
105+
return expression;
106+
}
107+
93108
public static SqlExpression<T> From<T>(this IDbConnection dbConn, TableOptions tableOptions,
94109
Action<SqlExpression<T>> options)
95110
{

0 commit comments

Comments
 (0)