Skip to content

Commit

Permalink
[fix]pgsql在拼接 Upsert 语句时多输出了逗号。
Browse files Browse the repository at this point in the history
空数组时,使用 '{}' 代替 ARRAY[] 。用于抑制以下报错
42P18: cannot determine type of empty array
  • Loading branch information
Soar360 committed Sep 16, 2024
1 parent a15d532 commit a3a1506
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions XCode/DataAccessLayer/Database/PostgreSQL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public override String FormatValue(IDataColumn? column, Object? value)
}
if (isArrayField)
{
if (value is null) return isNullable ? "NULL" : "ARRAY[]";
if (value is null) return isNullable ? "NULL" : "'{}'";
var count = 0;
var builder = new StringBuilder();
builder.Append("ARRAY[");
Expand All @@ -131,8 +131,16 @@ public override String FormatValue(IDataColumn? column, Object? value)
builder.Append(',');
count++;
}
if (count != 0) builder.Length--;
builder.Append("]");
if (count != 0)
{
builder.Length--;
builder.Append("]");
}
else
{
builder.Clear();
builder.Append("'{}'");
}
return builder.ToString();
}
else
Expand Down Expand Up @@ -414,11 +422,11 @@ public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollectio
{
if (dc.Nullable)
{
setters.Add(String.Format("{0} = EXCLUDED.{0},", db.FormatName(dc)));
setters.Add(String.Format("{0} = EXCLUDED.{0}", db.FormatName(dc)));
}
else
{
setters.Add(String.Format("{0} = COALESCE(EXCLUDED.{0},{1}.{0}),", db.FormatName(dc), tb));
setters.Add(String.Format("{0} = COALESCE(EXCLUDED.{0},{1}.{0})", db.FormatName(dc), tb));
}
}
}
Expand All @@ -432,7 +440,7 @@ public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollectio

if (addColumns.Contains(dc.Name))
{
setters.Add(String.Format("{0} = EXCLUDED.{0} + {1}.{0},", db.FormatName(dc), tb));
setters.Add(String.Format("{0} = EXCLUDED.{0} + {1}.{0}", db.FormatName(dc), tb));
}
}
}
Expand Down

0 comments on commit a3a1506

Please sign in to comment.