Skip to content

Commit

Permalink
Releases development changes (#306)
Browse files Browse the repository at this point in the history
* Update PetaPoco.Core.ttinclude

Added an ExcludePrefix option to easily suppress blocks of tables from generation by a prefix.  This is useful when connecting to CMS and other systems so a user doesn't have to manually remove many tables.

* Update Database.tt

* Removed extra CR.

* Pynej patch 2 (#298)

* Update PetaPoco.Generator.ttinclude

Added a ExplicitColumns option.

* Update PetaPoco.Generator.ttinclude

Revert incidental changes.

* Update PetaPoco.Core.ttinclude

Added variable.

* Update Database.tt

Added variable.

* Update PetaPoco.Generator.ttinclude

Retore inadvertent deletion.

* Removes extra line breaks at end of Database.tt file

* Cleans up Sql.cs (SQL builder class). Adds unit test for reported bug #106

* Fixes #255 Page query is very slow when use a long sql statement

    Special thanks to @kuangyunsheng for the fix

* Update PetaPoco.Core.ttinclude (#304)

Fix auto-increment for Guid key when set in SQL server as newsequentialid() or newid().
  • Loading branch information
pleb committed Jul 20, 2016
1 parent 622945b commit 2b0810e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 21 deletions.
16 changes: 16 additions & 0 deletions PetaPoco.Tests.Unit/Core/SqlTests.cs
Expand Up @@ -350,5 +350,21 @@ public void Append_GivenMultipleAppends_ShouldBeValid()
.ShouldBe(
@"UPDATE [Resource] SET [ResourceName] = @0,[ResourceDescription] = @1,[ResourceContent] = @2,[ResourceData] = @3,[ResourceGUID] = @4,[LaunchPath] = @5,[ResourceType] = @6,[ContentType] = @7,[SchoolID] = @8,[DistrictID] = @9,[IsActive] = @10,[UpdatedBy] = @11,[UpdatedDate] = @12,[Extension] = @13 WHERE ResourceID=@14");
}

[Fact]
[Description("Investigation of reported bug #106")]
public void Sql_CacheShouldBeResetAfterAdditionalChanges_ShouldBeValid()
{
_sql.Select("field");
var sqlCapture1 = _sql.SQL;
_sql.From("myTable");
var sqlCapture2 = _sql.SQL;
_sql.Where("id = @0", 1);
var sqlCapture3 = _sql.SQL;

sqlCapture1.Replace("\n", " ").ShouldBe("SELECT field");
sqlCapture2.Replace("\n", " ").ShouldBe("SELECT field FROM myTable");
sqlCapture3.Replace("\n", " ").ShouldBe("SELECT field FROM myTable WHERE (id = @0)");
}
}
}
1 change: 1 addition & 0 deletions PetaPoco.sln.DotSettings
Expand Up @@ -156,6 +156,7 @@
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_CONSTRUCTOR_INITIALIZER_ON_SAME_LINE/@EntryValue">False</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DB/@EntryIndexedValue">DB</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=MS/@EntryIndexedValue">MS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAddAccessorOwnerDeclarationBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAlwaysTreatStructAsNotReorderableMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
Expand Down
26 changes: 12 additions & 14 deletions PetaPoco/Core/Sql.cs
Expand Up @@ -2,7 +2,7 @@
// Apache License, Version 2.0 https://github.com/CollaboratingPlatypus/PetaPoco/blob/master/LICENSE.txt
// </copyright>
// <author>PetaPoco - CollaboratingPlatypus</author>
// <date>2015/12/13</date>
// <date>2016/07/16</date>

using System;
using System.Collections.Generic;
Expand All @@ -26,7 +26,7 @@ public class Sql

/// <summary>
/// Instantiate a new SQL Builder object. Weirdly implemented as a property but makes
/// for more elegantly readble fluent style construction of SQL Statements
/// for more elegantly readable fluent style construction of SQL Statements
/// eg: db.Query(Sql.Builder.Append(....))
/// </summary>
public static Sql Builder
Expand Down Expand Up @@ -107,7 +107,7 @@ public Sql Append(Sql sql)
}

/// <summary>
/// Append an SQL fragement to the right-hand-side of this SQL builder
/// Append an SQL fragment to the right-hand-side of this SQL builder
/// </summary>
/// <param name="sql">The SQL statement or fragment</param>
/// <param name="args">Arguments to any parameters embedded in the SQL</param>
Expand All @@ -124,7 +124,7 @@ private static bool Is(Sql sql, string sqltype)

private void Build(StringBuilder sb, List<object> args, Sql lhs)
{
if (!String.IsNullOrEmpty(_sql))
if (!string.IsNullOrEmpty(_sql))
{
// Add SQL to the string
if (sb.Length > 0)
Expand Down Expand Up @@ -179,19 +179,17 @@ public Sql Where(string sql, params object[] args)
/// <returns>A reference to this builder, allowing for fluent style concatenation</returns>
public Sql OrderBy(params object[] columns)
{
return Append(new Sql("ORDER BY " + String.Join(", ", (from x in columns select x.ToString()).ToArray())));
return Append(new Sql("ORDER BY " + string.Join(", ", (from x in columns select x.ToString()).ToArray())));
}

/// <summary>
/// Appends an SQL SELECT clause to this SQL builder
/// </summary>
/// <param name="columns">
/// A collection of SQL column names to select
/// <param>
/// <returns>A reference to this builder, allowing for fluent style concatenation</returns>
/// <param name="columns">A collection of SQL column names to select</param>
/// <returns>A reference to this builder, allowing for fluent style concatenation</returns>
public Sql Select(params object[] columns)
{
return Append(new Sql("SELECT " + String.Join(", ", (from x in columns select x.ToString()).ToArray())));
return Append(new Sql("SELECT " + string.Join(", ", (from x in columns select x.ToString()).ToArray())));
}

/// <summary>
Expand All @@ -201,7 +199,7 @@ public Sql Select(params object[] columns)
/// <returns>A reference to this builder, allowing for fluent style concatenation</returns>
public Sql From(params object[] tables)
{
return Append(new Sql("FROM " + String.Join(", ", (from x in tables select x.ToString()).ToArray())));
return Append(new Sql("FROM " + string.Join(", ", (from x in tables select x.ToString()).ToArray())));
}

/// <summary>
Expand All @@ -211,12 +209,12 @@ public Sql From(params object[] tables)
/// <returns>A reference to this builder, allowing for fluent style concatenation</returns>
public Sql GroupBy(params object[] columns)
{
return Append(new Sql("GROUP BY " + String.Join(", ", (from x in columns select x.ToString()).ToArray())));
return Append(new Sql("GROUP BY " + string.Join(", ", (from x in columns select x.ToString()).ToArray())));
}

private SqlJoinClause Join(string JoinType, string table)
private SqlJoinClause Join(string joinType, string table)
{
return new SqlJoinClause(Append(new Sql(JoinType + table)));
return new SqlJoinClause(Append(new Sql(joinType + table)));
}

/// <summary>
Expand Down
17 changes: 11 additions & 6 deletions PetaPoco/Providers/SqlServerDatabaseProvider.cs
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Data.Common;
using System.Linq;
using System.Text.RegularExpressions;
using PetaPoco.Core;
using PetaPoco.Utilities;

Expand All @@ -19,18 +20,22 @@ public override DbProviderFactory GetFactory()
return GetFactory("System.Data.SqlClient.SqlClientFactory, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
}

private static readonly Regex simpleRegexOrderBy = new Regex(@"\bORDER\s+BY\s+", RegexOptions.RightToLeft | RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.Compiled);

public override string BuildPageQuery(long skip, long take, SQLParts parts, ref object[] args)
{
var helper = (PagingHelper) PagingUtility;
parts.SqlSelectRemoved = helper.RegexOrderBy.Replace(parts.SqlSelectRemoved, "", 1);
var helper = (PagingHelper)PagingUtility;
// when the query does not contain an "order by", it is very slow
if (simpleRegexOrderBy.IsMatch(parts.SqlSelectRemoved))
{
parts.SqlSelectRemoved = helper.RegexOrderBy.Replace(parts.SqlSelectRemoved, "", 1);
}
if (helper.RegexDistinct.IsMatch(parts.SqlSelectRemoved))
{
parts.SqlSelectRemoved = "peta_inner.* FROM (SELECT " + parts.SqlSelectRemoved + ") peta_inner";
}
var sqlPage = string.Format("SELECT * FROM (SELECT ROW_NUMBER() OVER ({0}) peta_rn, {1}) peta_paged WHERE peta_rn>@{2} AND peta_rn<=@{3}",
parts.SqlOrderBy == null ? "ORDER BY (SELECT NULL)" : parts.SqlOrderBy, parts.SqlSelectRemoved, args.Length, args.Length + 1);
args = args.Concat(new object[] {skip, skip + take}).ToArray();

var sqlPage = string.Format("SELECT * FROM (SELECT ROW_NUMBER() OVER ({0}) peta_rn, {1}) peta_paged WHERE peta_rn > @{2} AND peta_rn <= @{3}", parts.SqlOrderBy ?? "ORDER BY (SELECT NULL)", parts.SqlSelectRemoved, args.Length, args.Length + 1);
args = args.Concat(new object[] { skip, skip + take }).ToArray();
return sqlPage;
}

Expand Down
4 changes: 3 additions & 1 deletion PetaPoco/T4 Templates/PetaPoco.Core.ttinclude
Expand Up @@ -563,7 +563,9 @@ class SqlServerSchemaReader : SchemaReader
col.PropertyName=CleanUp(col.Name);
col.PropertyType=GetPropertyType(rdr["DataType"].ToString());
col.IsNullable=rdr["IsNullable"].ToString()=="YES";
col.IsAutoIncrement=((int)rdr["IsIdentity"])==1;
col.IsAutoIncrement=((int)rdr["IsIdentity"])==1 ||
(!DBNull.Value.Equals(rdr["DefaultSetting"]) && ((string)rdr["DefaultSetting"] == "(newsequentialid())" ||
(string)rdr["DefaultSetting"] == "(newid())"));
result.Add(col);
}
}
Expand Down

0 comments on commit 2b0810e

Please sign in to comment.