Skip to content

Commit

Permalink
Improve as part generation for return values
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoraes committed Nov 26, 2024
1 parent 485489f commit 09f05e4
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/Npgsql.Age/NpgsqlAgeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Npgsql;
using Npgsql.TypeMapping;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace Npgsql.Age
{
Expand Down Expand Up @@ -117,24 +118,39 @@ internal static string GenerateAsPart(string cypher)
// Split the return values
var returnValues = match.Groups[1].Value.Split(',');

// Dictionary to track occurrences of column names
var columnNames = new Dictionary<string, int>();

// Generate the 'as (...)' part
var asPart = string.Join(", ", returnValues.Select((value, index) =>
{
var trimmedValue = value.Trim().TrimStart('$');
if (int.TryParse(trimmedValue, out _) || double.TryParse(trimmedValue, out _))
{
return $"num{index} agtype";
trimmedValue = $"num";
}
if (Regex.IsMatch(trimmedValue, @"\w+\(.*\)"))
{
var exprName = Regex.Match(trimmedValue, @"\w+").Value;
return $"{exprName} agtype"; // TODO: use index or something when there are multiple of the same $"{exprName}{index} agtype";
var exprName = Regex.Match(trimmedValue, @"\w+").Value; // TODO: use index or something when there are multiple of the same $"{exprName}{index} agtype";
trimmedValue = exprName;
}
if (trimmedValue.Any(char.IsUpper))
{
return $"\"{trimmedValue}\" agtype";
trimmedValue = $"\"{trimmedValue}\"";
}
var sanitizedValue = Regex.Replace(trimmedValue, @"[^\w]", "_");

// Check for duplicate column names
if (columnNames.ContainsKey(sanitizedValue))
{
columnNames[sanitizedValue]++;
sanitizedValue += columnNames[sanitizedValue].ToString();
}
else
{
columnNames[sanitizedValue] = 0;
}

return $"{sanitizedValue} agtype";
}));
return $"({asPart})";
Expand Down

0 comments on commit 09f05e4

Please sign in to comment.