Added "Select" and "AndSelect" to "PetaPocoSqlExtensions" to be able to select columns based on predicated instead of string values.
This commit is contained in:
@@ -2494,9 +2494,9 @@ namespace Umbraco.Core.Persistence
|
||||
// Now do rhs
|
||||
if (_rhs != null)
|
||||
_rhs.Build(sb, args, this);
|
||||
}
|
||||
}
|
||||
|
||||
public Sql Where(string sql, params object[] args)
|
||||
public Sql Where(string sql, params object[] args)
|
||||
{
|
||||
return Append(new Sql("WHERE (" + sql + ")", args));
|
||||
}
|
||||
@@ -2509,9 +2509,14 @@ namespace Umbraco.Core.Persistence
|
||||
public Sql Select(params object[] columns)
|
||||
{
|
||||
return Append(new Sql("SELECT " + String.Join(", ", (from x in columns select x.ToString()).ToArray())));
|
||||
}
|
||||
}
|
||||
|
||||
public Sql From(params object[] tables)
|
||||
public Sql AndSelect(params object[] columns)
|
||||
{
|
||||
return Append(new Sql(", " + String.Join(", ", (from x in columns select x.ToString()).ToArray())));
|
||||
}
|
||||
|
||||
public Sql From(params object[] tables)
|
||||
{
|
||||
return Append(new Sql("FROM " + String.Join(", ", (from x in tables select x.ToString()).ToArray())));
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
@@ -15,6 +14,57 @@ namespace Umbraco.Core.Persistence
|
||||
/// </summary>
|
||||
public static class PetaPocoSqlExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the column to select in the generated SQL query
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="sql">Sql object</param>
|
||||
/// <param name="fields">Columns to select</param>
|
||||
/// <returns></returns>
|
||||
[Obsolete("Use the overload specifying ISqlSyntaxProvider instead")]
|
||||
public static Sql Select<T>(this Sql sql, params Expression<Func<T, object>>[] fields)
|
||||
{
|
||||
return sql.Select(GetColumnsList(SqlSyntaxContext.SqlSyntaxProvider, fields));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines the column to select in the generated SQL query
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="sql">Sql object</param>
|
||||
/// <param name="sqlSyntax">Sql syntax</param>
|
||||
/// <param name="fields">Columns to select</param>
|
||||
/// <returns></returns>
|
||||
public static Sql Select<T>(this Sql sql, ISqlSyntaxProvider sqlSyntax, params Expression<Func<T, object>>[] fields)
|
||||
{
|
||||
return sql.Select(GetColumnsList(sqlSyntax, fields));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds another set of field to select. This method must be used with "Select" when fecthing fields from different tables.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="sql">Sql object</param>
|
||||
/// <param name="fields">Additional columns to select</param>
|
||||
/// <returns></returns>
|
||||
public static Sql AndSelect<T>(this Sql sql, params Expression<Func<T, object>>[] fields)
|
||||
{
|
||||
return sql.AndSelect(GetColumnsList(SqlSyntaxContext.SqlSyntaxProvider, fields));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds another set of field to select. This method must be used with "Select" when fecthing fields from different tables.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="sql">Sql object</param>
|
||||
/// <param name="sqlSyntax">Sql syntax</param>
|
||||
/// <param name="fields">Additional columns to select</param>
|
||||
/// <returns></returns>
|
||||
public static Sql AndSelect<T>(this Sql sql, ISqlSyntaxProvider sqlSyntax, params Expression<Func<T, object>>[] fields)
|
||||
{
|
||||
return sql.AndSelect(GetColumnsList(sqlSyntax, fields));
|
||||
}
|
||||
|
||||
[Obsolete("Use the overload specifying ISqlSyntaxProvider instead")]
|
||||
public static Sql From<T>(this Sql sql)
|
||||
{
|
||||
@@ -239,5 +289,23 @@ namespace Umbraco.Core.Persistence
|
||||
var attr = column.FirstAttribute<ColumnAttribute>();
|
||||
return attr == null || string.IsNullOrWhiteSpace(attr.Name) ? column.Name : attr.Name;
|
||||
}
|
||||
|
||||
private static string[] GetColumnsList<T>(ISqlSyntaxProvider sqlSyntax, params Expression<Func<T, object>>[] fields)
|
||||
{
|
||||
string tableName = sqlSyntax.GetQuotedTableName(GetTableName(typeof(T)));
|
||||
|
||||
if (fields.Length == 0)
|
||||
{
|
||||
return new[] { string.Format("{0}.*", tableName) };
|
||||
}
|
||||
|
||||
return fields.Select(field =>
|
||||
{
|
||||
var column = ExpressionHelper.FindProperty(field) as PropertyInfo;
|
||||
var columnName = GetColumnName(column);
|
||||
|
||||
return string.Format("{0}.{1}", tableName, sqlSyntax.GetQuotedColumnName(columnName));
|
||||
}).ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ using Umbraco.Core.Persistence;
|
||||
using Umbraco.Core.Persistence.Repositories;
|
||||
using Umbraco.Tests.TestHelpers;
|
||||
using Umbraco.Core.Persistence.Querying;
|
||||
using Umbraco.Core.Persistence.SqlSyntax;
|
||||
|
||||
namespace Umbraco.Tests.Persistence.Querying
|
||||
{
|
||||
@@ -25,13 +26,13 @@ namespace Umbraco.Tests.Persistence.Querying
|
||||
|
||||
Assert.AreEqual("SELECT * FROM [umbracoNode] WHERE (upper([umbracoNode].[path]) LIKE upper(@0))", sql.SQL.Replace("\n", " "));
|
||||
Assert.AreEqual(1, sql.Arguments.Length);
|
||||
Assert.AreEqual(content.Path + "%", sql.Arguments[0]);
|
||||
Assert.AreEqual(content.Path + "%", sql.Arguments[0]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Where_Clause_With_Starts_With_By_Variable()
|
||||
{
|
||||
var content = new NodeDto() {NodeId = 123, Path = "-1,123"};
|
||||
var content = new NodeDto() { NodeId = 123, Path = "-1,123" };
|
||||
var sql = new Sql("SELECT *").From<NodeDto>().Where<NodeDto>(x => x.Path.StartsWith(content.Path) && x.NodeId != content.NodeId);
|
||||
|
||||
Assert.AreEqual("SELECT * FROM [umbracoNode] WHERE ((upper([umbracoNode].[path]) LIKE upper(@0) AND ([umbracoNode].[id] <> @1)))", sql.SQL.Replace("\n", " "));
|
||||
@@ -260,5 +261,74 @@ namespace Umbraco.Tests.Persistence.Querying
|
||||
|
||||
Debug.Print(sql.SQL);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Can_Use_Select_With_Star_And_Predicate()
|
||||
{
|
||||
var expected = new Sql();
|
||||
expected.Select("[cmsContent].*")
|
||||
.From("[cmsContent]");
|
||||
|
||||
var sql = new Sql();
|
||||
sql.Select<ContentDto>()
|
||||
.From<ContentDto>();
|
||||
|
||||
Assert.That(sql.SQL, Is.EqualTo(expected.SQL));
|
||||
|
||||
Debug.Print(sql.SQL);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Can_Use_Select_With_One_Column_And_Predicate()
|
||||
{
|
||||
var expected = new Sql();
|
||||
expected.Select("[cmsContent].[nodeId]")
|
||||
.From("[cmsContent]");
|
||||
|
||||
var sql = new Sql();
|
||||
sql.Select<ContentDto>(c => c.NodeId)
|
||||
.From<ContentDto>();
|
||||
|
||||
Assert.That(sql.SQL, Is.EqualTo(expected.SQL));
|
||||
|
||||
Debug.Print(sql.SQL);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Can_Use_Select_With_Multiple_Column_And_Predicate()
|
||||
{
|
||||
var expected = new Sql();
|
||||
expected.Select("[cmsContent].[nodeId]", "[cmsContent].[contentType]", "[cmsContent].[pk]")
|
||||
.From("[cmsContent]");
|
||||
|
||||
var sql = new Sql();
|
||||
sql.Select<ContentDto>(c => c.NodeId, c => c.ContentTypeId, c => c.PrimaryKey)
|
||||
.From<ContentDto>();
|
||||
|
||||
Assert.That(sql.SQL, Is.EqualTo(expected.SQL));
|
||||
|
||||
Debug.Print(sql.SQL);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Can_InnerJoin_With_Select_And_AndSelect()
|
||||
{
|
||||
var expected = new Sql();
|
||||
expected.Select("[cmsDocument].[nodeId], [cmsDocument].[published]\n, [cmsContentVersion].[id]")
|
||||
.From("[cmsDocument]")
|
||||
.InnerJoin("[cmsContentVersion]")
|
||||
.On("[cmsDocument].[versionId] = [cmsContentVersion].[VersionId]");
|
||||
|
||||
var sql = new Sql();
|
||||
sql.Select<DocumentDto>(d => d.NodeId, d => d.Published)
|
||||
.AndSelect<ContentVersionDto>(cv => cv.Id)
|
||||
.From<DocumentDto>()
|
||||
.InnerJoin<ContentVersionDto>()
|
||||
.On<DocumentDto, ContentVersionDto>(left => left.VersionId, right => right.VersionId);
|
||||
|
||||
Assert.That(sql.SQL, Is.EqualTo(expected.SQL));
|
||||
|
||||
Debug.Print(sql.SQL);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
8
src/Umbraco.Web.UI.Client/vwd.webinfo
Normal file
8
src/Umbraco.Web.UI.Client/vwd.webinfo
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0"?>
|
||||
<!--
|
||||
Visual Studio global web project settings.
|
||||
-->
|
||||
<VisualWebDeveloper>
|
||||
|
||||
<iisExpressSettings windowsAuthentication="enabled" anonymousAuthentication="disabled" useClassicPipelineMode="false"/>
|
||||
</VisualWebDeveloper>
|
||||
Reference in New Issue
Block a user