From a40f87eb3699670eee3181e3a1373b462e1a2ec4 Mon Sep 17 00:00:00 2001 From: Jeremy Pyne Date: Thu, 14 Jul 2016 17:01:51 -0400 Subject: [PATCH 1/2] Fixed an issues with user generated PetaPoco's would not work if they included schema's. Fixed an issue where strong typed helper methods would not generate valid SQL for column names if the poco didn't explicitly set a [Column(Name="")] attribute. The PetaPoco format allows for this though and will default to the property name. Again this would cause issues for developers trying to use the DatabaseContext.Database class with their own Poco's. Both the above causes would happen for example if using the PetaPoco T4 templates to automatically generate Poco's. --- .../Persistence/PetaPocoSqlExtensions.cs | 16 +++++++++++----- .../SqlSyntax/MicrosoftSqlSyntaxProviderBase.cs | 6 +++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/Umbraco.Core/Persistence/PetaPocoSqlExtensions.cs b/src/Umbraco.Core/Persistence/PetaPocoSqlExtensions.cs index 3cbd70803d..72fb03c498 100644 --- a/src/Umbraco.Core/Persistence/PetaPocoSqlExtensions.cs +++ b/src/Umbraco.Core/Persistence/PetaPocoSqlExtensions.cs @@ -51,7 +51,8 @@ namespace Umbraco.Core.Persistence public static Sql OrderBy(this Sql sql, Expression> columnMember, ISqlSyntaxProvider sqlSyntax) { var column = ExpressionHelper.FindProperty(columnMember) as PropertyInfo; - var columnName = column.FirstAttribute().Name; + var columnAttribute = column.FirstAttribute(); + var columnName = columnAttribute == null || string.IsNullOrEmpty(columnAttribute.Name) ? column.Name : columnAttribute.Name; var type = typeof(TColumn); var tableNameAttribute = type.FirstAttribute(); @@ -74,7 +75,8 @@ namespace Umbraco.Core.Persistence public static Sql OrderByDescending(this Sql sql, Expression> columnMember, ISqlSyntaxProvider sqlSyntax) { var column = ExpressionHelper.FindProperty(columnMember) as PropertyInfo; - var columnName = column.FirstAttribute().Name; + var columnAttribute = column.FirstAttribute(); + var columnName = columnAttribute == null || string.IsNullOrEmpty(columnAttribute.Name) ? column.Name : columnAttribute.Name; var type = typeof(TColumn); var tableNameAttribute = type.FirstAttribute(); @@ -96,7 +98,8 @@ namespace Umbraco.Core.Persistence public static Sql GroupBy(this Sql sql, Expression> columnMember, ISqlSyntaxProvider sqlProvider) { var column = ExpressionHelper.FindProperty(columnMember) as PropertyInfo; - var columnName = column.FirstAttribute().Name; + var columnAttribute = column.FirstAttribute(); + var columnName = columnAttribute == null || string.IsNullOrEmpty(columnAttribute.Name) ? column.Name : columnAttribute.Name; return sql.GroupBy(sqlProvider.GetQuotedColumnName(columnName)); } @@ -178,8 +181,11 @@ namespace Umbraco.Core.Persistence var left = ExpressionHelper.FindProperty(leftMember) as PropertyInfo; var right = ExpressionHelper.FindProperty(rightMember) as PropertyInfo; - var leftColumnName = left.FirstAttribute().Name; - var rightColumnName = right.FirstAttribute().Name; + + var leftColumnAttribute = left.FirstAttribute(); + var leftColumnName = leftColumnAttribute == null || string.IsNullOrEmpty(leftColumnAttribute.Name) ? left.Name : leftColumnAttribute.Name; + var rightColumnAttribute = right.FirstAttribute(); + var rightColumnName = rightColumnAttribute == null || string.IsNullOrEmpty(rightColumnAttribute.Name) ? right.Name : rightColumnAttribute.Name; string onClause = string.Format("{0}.{1} = {2}.{3}", sqlSyntax.GetQuotedTableName(leftTableName), diff --git a/src/Umbraco.Core/Persistence/SqlSyntax/MicrosoftSqlSyntaxProviderBase.cs b/src/Umbraco.Core/Persistence/SqlSyntax/MicrosoftSqlSyntaxProviderBase.cs index a6d1d690ba..2b8afcd3e8 100644 --- a/src/Umbraco.Core/Persistence/SqlSyntax/MicrosoftSqlSyntaxProviderBase.cs +++ b/src/Umbraco.Core/Persistence/SqlSyntax/MicrosoftSqlSyntaxProviderBase.cs @@ -29,7 +29,11 @@ namespace Umbraco.Core.Persistence.SqlSyntax public override string GetQuotedTableName(string tableName) { - return string.Format("[{0}]", tableName); + if (tableName.Contains(".")) { + var tableNameParts = tableName.Split(new char[] { '.' }, 2); + return string.Format("[{0}].[{1}]", tableNameParts[0], tableNameParts[1]); + } else + return string.Format("[{0}]", tableName); } public override string GetQuotedColumnName(string columnName) From 2e24923cc400ec63df4dbb8afa5f21518d8cc34d Mon Sep 17 00:00:00 2001 From: Jeremy Pyne Date: Mon, 18 Jul 2016 09:00:23 -0400 Subject: [PATCH 2/2] Update PetaPoco.cs Sql() class should reset build cache when new Append operations are called. If not then stepping through with the debugger or doing internal logging can cause invalid SQL and unexpected results. --- src/Umbraco.Core/Persistence/PetaPoco.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Umbraco.Core/Persistence/PetaPoco.cs b/src/Umbraco.Core/Persistence/PetaPoco.cs index 88f90639d1..32f9024d49 100644 --- a/src/Umbraco.Core/Persistence/PetaPoco.cs +++ b/src/Umbraco.Core/Persistence/PetaPoco.cs @@ -2417,6 +2417,7 @@ namespace Umbraco.Core.Persistence else _rhs = sql; + _sqlFinal = null; return this; }