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

Commit 6fa5f22

Browse files
committed
Add UseSelectPropertiesAsAliases + options API to allow fluent config of SqlExpr
1 parent ffadd72 commit 6fa5f22

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public abstract partial class SqlExpression<T> : ISqlExpression, IHasUntypedSqlE
4949
public static Action<SqlExpression<T>> SelectFilter { get; set; }
5050
public int? Rows { get; set; }
5151
public int? Offset { get; set; }
52+
public bool UseSelectPropertiesAsAliases { get; set; }
5253

5354
protected string Sep => sep;
5455

@@ -98,6 +99,7 @@ protected virtual SqlExpression<T> CopyTo(SqlExpression<T> to)
9899
to.Offset = Offset;
99100
to.Rows = Rows;
100101
to.tableDefs = tableDefs;
102+
to.UseSelectPropertiesAsAliases = UseSelectPropertiesAsAliases;
101103
return to;
102104
}
103105

@@ -1922,7 +1924,8 @@ private object SetAnonTypePropertyNamesForSelectExpression(object expr, Expressi
19221924
// When selecting a column use the anon type property name, rather than the table property name, as the returned column name
19231925
if (arg is MemberExpression propExpr && IsLambdaArg(propExpr.Expression))
19241926
{
1925-
if (propExpr.Member.Name != member.Name || // Use anon property alias when names don't match
1927+
if (UseSelectPropertiesAsAliases || // Use anon property alias when explicitly requested
1928+
propExpr.Member.Name != member.Name || // or when names don't match
19261929
propExpr.Expression.Type != ModelDef.ModelType) // or when selecting a field from a different table
19271930
return new SelectItemExpression(DialectProvider, expr.ToString(), member.Name);
19281931

@@ -1975,7 +1978,9 @@ private object SetAnonTypePropertyNamesForSelectExpression(object expr, Expressi
19751978
return new PartialSqlString(strExpr + " AS " + member.Name);
19761979
}
19771980

1978-
return expr;
1981+
return UseSelectPropertiesAsAliases
1982+
? new SelectItemExpression(DialectProvider, expr.ToString(), member.Name)
1983+
: expr;
19791984
}
19801985

19811986
private static void StripAliases(SelectList selectList)

src/ServiceStack.OrmLite/OrmLiteReadExpressionsApi.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ public static SqlExpression<T> From<T>(this IDbConnection dbConn)
5353
return dbConn.GetExecFilter().SqlExpression<T>(dbConn);
5454
}
5555

56+
public static SqlExpression<T> From<T>(this IDbConnection dbConn, Action<SqlExpression<T>> options)
57+
{
58+
var q = dbConn.GetExecFilter().SqlExpression<T>(dbConn);
59+
options(q);
60+
return q;
61+
}
62+
5663
public static SqlExpression<T> From<T, JoinWith>(this IDbConnection dbConn, Expression<Func<T, JoinWith, bool>> joinExpr=null)
5764
{
5865
var sql = dbConn.GetExecFilter().SqlExpression<T>(dbConn);
@@ -70,16 +77,24 @@ public static SqlExpression<T> From<T>(this IDbConnection dbConn, string fromExp
7077
return expr;
7178
}
7279

73-
public static SqlExpression<T> From<T>(this IDbConnection dbConn, TableOptions options)
80+
public static SqlExpression<T> From<T>(this IDbConnection dbConn, TableOptions tableOptions)
7481
{
7582
var expr = dbConn.GetExecFilter().SqlExpression<T>(dbConn);
76-
if (!string.IsNullOrEmpty(options.Expression))
77-
expr.From(options.Expression);
78-
if (!string.IsNullOrEmpty(options.Alias))
79-
expr.SetTableAlias(options.Alias);
83+
if (!string.IsNullOrEmpty(tableOptions.Expression))
84+
expr.From(tableOptions.Expression);
85+
if (!string.IsNullOrEmpty(tableOptions.Alias))
86+
expr.SetTableAlias(tableOptions.Alias);
8087
return expr;
8188
}
8289

90+
public static SqlExpression<T> From<T>(this IDbConnection dbConn, TableOptions tableOptions,
91+
Action<SqlExpression<T>> options)
92+
{
93+
var q = dbConn.From<T>(tableOptions);
94+
options(q);
95+
return q;
96+
}
97+
8398
[Obsolete("Use TableAlias")]
8499
public static JoinFormatDelegate JoinAlias(this IDbConnection db, string alias) => OrmLiteUtils.JoinAlias(alias);
85100

tests/ServiceStack.OrmLite.Tests/Issues/LRAIssues.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ private static void DropTables(IDbConnection db)
215215

216216
private static void InitAliasTables(IDbConnection db)
217217
{
218-
OrmLiteUtils.PrintSql();
219218
db.DropTable<LRDProfiloAnalisi>();
220219
db.DropTable<LRDAnalisi>();
221220
db.DropTable<LRDContenitore>();
@@ -238,6 +237,7 @@ private static void InitAliasTables(IDbConnection db)
238237
ProfiloAnalisiId = null
239238
});
240239

240+
OrmLiteUtils.PrintSql();
241241
}
242242

243243
[Test]
@@ -247,7 +247,10 @@ public void Table_Alias()
247247
{
248248
InitAliasTables(db);
249249

250-
var q = db.From<LRDAnalisi>(db.TableAlias("dana"))
250+
var q = db.From<LRDAnalisi>(options => {
251+
options.SetTableAlias("dana");
252+
options.UseSelectPropertiesAsAliases = true;
253+
})
251254
.Join<LRDAnalisi, LRDContenitore>((dana, dcont) => dana.ContenitoreId == dcont.Id, db.TableAlias("c"))
252255
.Join<LRDAnalisi, LRDProfiloAnalisi>((dana, dprofana) => dana.Id == dprofana.AnalisiId, db.TableAlias("dprofana"))
253256
.Where<LRDProfiloAnalisi>(dprofana => Sql.TableAlias(dprofana.ProfiloAnalisiId, "dprofana") == null)
@@ -258,7 +261,7 @@ public void Table_Alias()
258261
AnalisiId = dana.Id,
259262
Codice = dana.Codice,
260263
Descrizione = dana.Descrizione,
261-
ContenitoreId = Sql.TableAlias(dcont.Id, "c"),
264+
ContenitoreId = dana.ContenitoreId,
262265
ContenitoreCodice = Sql.TableAlias(dcont.Codice, "c"),
263266
ContenitoreDescrizione = Sql.TableAlias(dcont.Descrizione, "c"),
264267
VersioneRecord = Sql.TableAlias(dprofana.VersioneRecord, "dprofana")

0 commit comments

Comments
 (0)