Skip to content

Commit

Permalink
the tables and columns are now snake cased
Browse files Browse the repository at this point in the history
  • Loading branch information
runeanielsen committed Jan 29, 2025
1 parent 011169b commit b936de1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/CIM.PostgresImporter.CLI/PostgresImport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public static async Task<int> ImportAsync(
builder.UseNetTopologySuite();
using var dataSource = builder.Build();
using var connection = await dataSource.OpenConnectionAsync().ConfigureAwait(false);
var fields = string.Join(",", schemaType.Keys.Select(key => $"\"{key}\""));
var fields = string.Join(",", schemaType.Keys.Select(key => $"\"{key.ToSnakeCase()}\""));

var copySql = $"COPY \"{schemaName}\".\"{typeName}\" ({fields}) FROM STDIN (FORMAT BINARY)";
var copySql = $"COPY \"{schemaName}\".\"{typeName.ToSnakeCase()}\" ({fields}) FROM STDIN (FORMAT BINARY)";

var postgresqlBinaryWriter = await connection.BeginBinaryImportAsync(copySql).ConfigureAwait(false);

Expand Down
6 changes: 3 additions & 3 deletions src/CIM.PostgresImporter.CLI/PostgresSchemaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public static string Build(Schema schema, string schemaName)

private static string Build(SchemaType schemaType, string schemaName)
{
var columns = string.Join(",\n ", schemaType.Properties.Select(x => $"\"{x.Name}\" {ConvertInternalTypeToPostgresqlType(x.Type)}"));
var columns = string.Join(",\n ", schemaType.Properties.Select(x => $"\"{x.Name.ToSnakeCase()}\" {ConvertInternalTypeToPostgresqlType(x.Type)}"));
return @$"
CREATE TABLE ""{schemaName}"".""{schemaType.Name}"" (
CREATE TABLE ""{schemaName}"".""{schemaType.Name.ToSnakeCase()}"" (
{columns},
PRIMARY KEY (""mRID"")
PRIMARY KEY (""m_rid"")
);";
}

Expand Down
14 changes: 14 additions & 0 deletions src/CIM.PostgresImporter.CLI/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Globalization;
using System.Text.RegularExpressions;

namespace CIM.PostgresImporter.CLI;

internal static class StringExtensions
{
public static string ToSnakeCase(this string text)
{
#pragma warning disable CA1308 // We want lower case.
return Regex.Replace(text, "(?<=[a-z0-9])[A-Z]|(?<=[A-Z])[A-Z][a-z]", "_$0").ToLower(CultureInfo.InvariantCulture);
#pragma warning restore CA1308
}
}

0 comments on commit b936de1

Please sign in to comment.