Skip to content

Commit

Permalink
Issue #26 UsePostgresQuoting method added
Browse files Browse the repository at this point in the history
Added a method UsePostgresQuoting for better case-sensitive /
case-insensitive handling. Thanks to @say25 for the input! Released as
Version 2.4.0.
  • Loading branch information
bytefish committed Aug 13, 2018
1 parent f8b1a90 commit 929b0dd
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 9 deletions.
Binary file not shown.
Binary file not shown.
Expand Up @@ -30,6 +30,7 @@ protected override void OnSetupInTransaction()
public void Test_Issue1_QuotingColumnsAndTableNameTest()
{
subject = new PostgreSQLCopyHelper<MixedCaseEntity>("sample", "MixedCaseEntity")
.UsePostgresQuoting()
.MapInteger("Property_One", x => x.Property_One)
.MapText("Property_Two", x => x.Property_Two);

Expand Down Expand Up @@ -75,6 +76,5 @@ private int CreateTable()

return sqlCommand.ExecuteNonQuery();
}

}
}
Expand Up @@ -30,6 +30,7 @@ protected override void OnSetupInTransaction()
public void Test_MixedCaseEntity_BulkInsert()
{
subject = new PostgreSQLCopyHelper<MixedCaseEntity>("sample", "\"MixedCaseEntity\"")
.UsePostgresQuoting()
.MapInteger("\"Property_One\"", x => x.Property_One)
.MapText("\"Property_Two\"", x => x.Property_Two);

Expand Down
Expand Up @@ -12,13 +12,13 @@ internal class TableDefinition

public string TableName { get; set; }

public string GetFullQualifiedTableName()
public string GetFullQualifiedTableName(bool usePostgresQuoting)
{
if (string.IsNullOrWhiteSpace(Schema))
{
return NpgsqlUtils.QuoteIdentifier(TableName);
return TableName.GetIdentifier(usePostgresQuoting);
}
return $"{NpgsqlUtils.QuoteIdentifier(Schema)}.{NpgsqlUtils.QuoteIdentifier(TableName)}";
return $"{Schema.GetIdentifier(usePostgresQuoting)}.{TableName.GetIdentifier(usePostgresQuoting)}";
}

public override string ToString()
Expand Down
Expand Up @@ -13,6 +13,8 @@ namespace PostgreSQLCopyHelper
{
public class PostgreSQLCopyHelper<TEntity> : IPostgreSQLCopyHelper<TEntity>
{
private bool usePostgresQuoting;

private readonly TableDefinition Table;

private readonly List<ColumnDefinition<TEntity>> Columns;
Expand All @@ -24,6 +26,8 @@ public PostgreSQLCopyHelper(string tableName)

public PostgreSQLCopyHelper(string schemaName, string tableName)
{
usePostgresQuoting = false;

Table = new TableDefinition
{
Schema = schemaName,
Expand All @@ -42,6 +46,13 @@ public void SaveAll(NpgsqlConnection connection, IEnumerable<TEntity> entities)
}
}

public PostgreSQLCopyHelper<TEntity> UsePostgresQuoting(bool enabled = true)
{
usePostgresQuoting = enabled;

return this;
}

public PostgreSQLCopyHelper<TEntity> Map<TProperty>(string columnName, Func<TEntity, TProperty> propertyGetter, NpgsqlDbType type)
{
return AddColumn(columnName, (writer, entity) => writer.Write(propertyGetter(entity), type));
Expand Down Expand Up @@ -91,10 +102,10 @@ private PostgreSQLCopyHelper<TEntity> AddColumn(string columnName, Action<Npgsql

private string GetCopyCommand()
{
var commaSeparatedColumns = string.Join(", ", Columns.Select(x => NpgsqlUtils.QuoteIdentifier(x.ColumnName)));
var commaSeparatedColumns = string.Join(", ", Columns.Select(x => x.ColumnName.GetIdentifier(usePostgresQuoting)));

return string.Format("COPY {0}({1}) FROM STDIN BINARY;",
Table.GetFullQualifiedTableName(),
Table.GetFullQualifiedTableName(usePostgresQuoting),
commaSeparatedColumns);
}
}
Expand Down
Expand Up @@ -12,10 +12,10 @@
<PackageLicenseUrl>https://opensource.org/licenses/MIT</PackageLicenseUrl>
<Copyright>Copyright © 2018 Philipp Wagner</Copyright>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<AssemblyVersion>2.3.1.0</AssemblyVersion>
<FileVersion>2.3.1.0</FileVersion>
<AssemblyVersion>2.4.0.0</AssemblyVersion>
<FileVersion>2.4.0.0</FileVersion>
<RepositoryType>git</RepositoryType>
<Version>2.3.1</Version>
<Version>2.4.0</Version>
<IncludeSource>True</IncludeSource>
<IncludeSymbols>True</IncludeSymbols>
</PropertyGroup>
Expand Down
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PostgreSQLCopyHelper.Utils
{
public static class StringExtensions
{
public static string GetIdentifier(this string identifier, bool usePostgresQuotes)
{
if (usePostgresQuotes)
{
return NpgsqlUtils.QuoteIdentifier(identifier);
}

return identifier;
}
}
}
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -90,3 +90,17 @@ private void WriteToDatabase(PostgreSQLCopyHelper<TestEntity> copyHelper, IEnume
}
```

## Case-Sensitive Identifiers ##

By default the library does not apply quotes to identifiers, such as Table Names and Column Names. If you want PostgreSQL-conform quoting for identifiers,
then use the ``UsePostgresQuoting`` method like this:

```
var copyHelper = new PostgreSQLCopyHelper<MixedCaseEntity>("sample", "MixedCaseEntity")
.UsePostgresQuoting()
.MapInteger("Property_One", x => x.Property_One)
.MapText("Property_Two", x => x.Property_Two);
```

``

0 comments on commit 929b0dd

Please sign in to comment.