Skip to content

Commit

Permalink
Clean up SQLite model comments PR
Browse files Browse the repository at this point in the history
  • Loading branch information
bricelam committed Aug 19, 2019
1 parent 00f03db commit 5e79127
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 79 deletions.
1 change: 0 additions & 1 deletion EFCore.Sqlite.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"src\\EFCore.Relational\\EFCore.Relational.csproj",
"src\\EFCore.Sqlite.Core\\EFCore.Sqlite.Core.csproj",
"src\\EFCore.Sqlite.NTS\\EFCore.Sqlite.NTS.csproj",
"src\\EFCore.Sqlite\\EFCore.Sqlite.csproj",
"src\\EFCore\\EFCore.csproj",
"src\\Microsoft.Data.Sqlite.Core\\Microsoft.Data.Sqlite.Core.csproj",
"test\\EFCore.Analyzers.Tests\\EFCore.Analyzers.Test.csproj",
Expand Down
20 changes: 3 additions & 17 deletions src/EFCore.Relational/Query/QuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
Expand Down Expand Up @@ -72,11 +70,6 @@ public virtual IRelationalCommand GetCommand(SelectExpression selectExpression)
/// </summary>
protected virtual string AliasSeparator { get; } = " AS ";

/// <summary>
/// The default single line comment prefix.
/// </summary>
protected virtual string SingleLineCommentToken { get; } = "--";

protected virtual IRelationalCommandBuilder Sql => _relationalCommandBuilder;

protected virtual void GenerateTagsHeaderComment(SelectExpression selectExpression)
Expand All @@ -85,16 +78,9 @@ protected virtual void GenerateTagsHeaderComment(SelectExpression selectExpressi
{
foreach (var tag in selectExpression.Tags)
{
using (var reader = new StringReader(tag))
{
string line;
while ((line = reader.ReadLine()) != null)
{
_relationalCommandBuilder.Append(SingleLineCommentToken).Append(" ").AppendLine(line);
}
}

_relationalCommandBuilder.AppendLine();
_relationalCommandBuilder
.AppendLines(_sqlGenerationHelper.GenerateComment(tag))
.AppendLine();
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/EFCore.Relational/Storage/ISqlGenerationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public interface ISqlGenerationHelper
/// </summary>
string BatchTerminator { get; }

/// <summary>
/// The default single-line comment prefix.
/// </summary>
string SingleLineCommentToken { get; }

/// <summary>
/// Generates a valid parameter name for the given candidate name.
/// </summary>
Expand Down Expand Up @@ -98,5 +103,12 @@ public interface ISqlGenerationHelper
/// <param name="name"> The identifier to delimit. </param>
/// <param name="schema"> The schema of the identifier. </param>
void DelimitIdentifier([NotNull] StringBuilder builder, [NotNull] string name, [CanBeNull] string schema);

/// <summary>
/// Generates a SQL comment.
/// </summary>
/// <param name="text"> The comment text. </param>
/// <returns> The generated SQL. </returns>
string GenerateComment([NotNull] string text);
}
}
28 changes: 28 additions & 0 deletions src/EFCore.Relational/Storage/RelationalSqlGenerationHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.IO;
using System.Text;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Utilities;
Expand Down Expand Up @@ -43,6 +44,11 @@ public RelationalSqlGenerationHelper([NotNull] RelationalSqlGenerationHelperDepe
/// </summary>
public virtual string BatchTerminator => string.Empty;

/// <summary>
/// The default single-line comment prefix.
/// </summary>
public virtual string SingleLineCommentToken => "--";

/// <summary>
/// Generates a valid parameter name for the given candidate name.
/// </summary>
Expand Down Expand Up @@ -159,5 +165,27 @@ public virtual void DelimitIdentifier(StringBuilder builder, string name, string

DelimitIdentifier(builder, name);
}

/// <summary>
/// Generates a SQL comment.
/// </summary>
/// <param name="text"> The comment text. </param>
/// <returns> The generated SQL. </returns>
public virtual string GenerateComment(string text)
{
Check.NotEmpty(text, nameof(text));

var builder = new StringBuilder();
using (var reader = new StringReader(text))
{
string line;
while ((line = reader.ReadLine()) != null)
{
builder.Append(SingleLineCommentToken).Append(" ").AppendLine(line);
}
}

return builder.ToString();
}
}
}
21 changes: 10 additions & 11 deletions src/EFCore.Sqlite.Core/Migrations/SqliteMigrationsSqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,9 @@ protected override void Generate(

using (builder.Indent())
{
CreateComment(builder, operation.Comment);
builder.AppendLine();
builder
.AppendLines(Dependencies.SqlGenerationHelper.GenerateComment(operation.Comment))
.AppendLine();
CreateTableColumns(operation, model, builder);
CreateTableConstraints(operation, model, builder);
builder.AppendLine();
Expand All @@ -375,6 +376,12 @@ protected override void Generate(
}
}

/// <summary>
/// Generates a SQL fragment for the column definitions in an <see cref="CreateTableOperation" />.
/// </summary>
/// <param name="operation"> The operation. </param>
/// <param name="model"> The target model which may be <c>null</c> if the operations exist without a model. </param>
/// <param name="builder"> The command builder to use to add the SQL fragment. </param>
protected override void CreateTableColumns(
CreateTableOperation operation,
IModel model,
Expand Down Expand Up @@ -409,7 +416,7 @@ private void CreateTableColumnsWithComments(

if (!string.IsNullOrEmpty(column.Comment))
{
CreateComment(builder, column.Comment);
builder.AppendLines(Dependencies.SqlGenerationHelper.GenerateComment(column.Comment));
}

ColumnDefinition(column, model, builder);
Expand All @@ -421,14 +428,6 @@ private void CreateTableColumnsWithComments(
}
}

private void CreateComment(MigrationCommandListBuilder builder, string comment)
{
foreach (var line in comment.Split(Environment.NewLine).Select(l => $"-- {l}"))
{
builder.AppendLine(line);
}
}

/// <summary>
/// Generates a SQL fragment for a column definition for the given column metadata.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,56 +563,6 @@ public virtual void CreateTableOperation_has_comment()
");
}

[ConditionalFact]
public virtual void CreateTableOperation_no_comments()
{
Generate(
new CreateTableOperation
{
Name = "People",
Columns =
{
new AddColumnOperation
{
Name = "Id",
Table = "People",
ClrType = typeof(int),
IsNullable = false,
},
new AddColumnOperation
{
Name = "UncommentedColumn1",
Table = "People",
ClrType = typeof(string),
IsNullable = false
},
new AddColumnOperation
{
Name = "UncommentedColumn2",
Table = "People",
ClrType = typeof(string),
IsNullable = false
},
new AddColumnOperation
{
Name = "UncommentedName",
Table = "People",
ClrType = typeof(string),
IsNullable = false,
}
}
});

AssertSql(
@"CREATE TABLE ""People"" (
""Id"" INTEGER NOT NULL,
""UncommentedColumn1"" TEXT NOT NULL,
""UncommentedColumn2"" TEXT NOT NULL,
""UncommentedName"" TEXT NOT NULL
);
");
}

[ConditionalFact]
public virtual void CreateTableOperation_has_multi_line_comment()
{
Expand Down

0 comments on commit 5e79127

Please sign in to comment.