Merge pull request #747 from hd2y/master

修复 pgsql 中 hstore 中 value 错误赋值为 key 的问题,并允许 value 值为 NULL。
This commit is contained in:
2881099 2021-04-22 11:50:10 +08:00 committed by GitHub
commit dabc95ae32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 7 deletions

6
.gitignore vendored
View File

@ -244,4 +244,8 @@ ModelManifest.xml
.paket/paket.exe .paket/paket.exe
# FAKE - F# Make # FAKE - F# Make
.fake/ .fake/
# JetBrains Rider
.idea/
*.sln.iml

View File

@ -7,6 +7,7 @@ using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data.Common; using System.Data.Common;
using System.Linq;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
@ -65,12 +66,22 @@ namespace FreeSql.PostgreSQL
{ {
var pgdics = isdic ? param as Dictionary<string, string> : var pgdics = isdic ? param as Dictionary<string, string> :
param as IEnumerable<KeyValuePair<string, string>>; param as IEnumerable<KeyValuePair<string, string>>;
if (pgdics == null) return string.Concat("''::hstore");
var pghstore = new StringBuilder(); var pghstore = new StringBuilder("'");
pghstore.Append("'"); var pairs = pgdics.ToArray();
foreach (var dic in pgdics)
pghstore.Append("\"").Append(dic.Key.Replace("'", "''")).Append("\"=>") for (var i = 0; i < pairs.Length; i++)
.Append(dic.Key.Replace("'", "''")).Append(","); {
if (i != 0) pghstore.Append(",");
pghstore.AppendFormat("\"{0}\"=>", pairs[i].Key.Replace("'", "''"));
if (pairs[i].Value == null)
pghstore.Append("NULL");
else
pghstore.AppendFormat("\"{0}\"", pairs[i].Value.Replace("'", "''"));
}
return pghstore.Append("'::hstore"); return pghstore.Append("'::hstore");
} }
else if (param is IEnumerable) else if (param is IEnumerable)