Skip to content

Commit 866c295

Browse files
committed
Dictionary query support
Fixes npgsql#212
1 parent 303ffc3 commit 866c295

File tree

10 files changed

+2164
-4
lines changed

10 files changed

+2164
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// ReSharper disable once CheckNamespace
2+
3+
namespace Microsoft.EntityFrameworkCore;
4+
5+
/// <summary>
6+
/// Provides extension methods supporting `Dictionary&lt;TKey,TValue>` and `ImmutableDictionary&lt;TKey,TValue>` function translation for PostgreSQL for the `hstore`, `json` and `jsonb` store types.
7+
/// </summary>
8+
public static class NpgsqlDictionaryDbFunctionsExtensions
9+
{
10+
/// <summary>
11+
/// Deletes keys from input operand Dictionary. Returns the same store type as the provided input for `hstore` and `jsonb` columns.
12+
///
13+
/// Works with `hstore`, `json` and `jsonb` type columns
14+
/// <br/>
15+
/// SQL translation: input - key
16+
///
17+
/// Note: for `json` type columns, input will be cast to `jsonb` and will output `jsonb` which requires PostgreSQL 9.3
18+
/// </summary>
19+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
20+
/// <param name="input">The input dictionary.</param>
21+
/// <param name="key">The key to remove.</param>
22+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
23+
public static T Remove<T, TValue>(this DbFunctions _, T input, string key)
24+
where T : IEnumerable<KeyValuePair<string, TValue>>
25+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Remove)));
26+
27+
/// <summary>
28+
/// Converts string dictionary to an array of alternating keys and values.
29+
/// <br/>
30+
/// HStore SQL translation: hstore_to_array(input)
31+
/// </summary>
32+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
33+
/// <param name="input">The input hstore.</param>
34+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
35+
public static List<string> ToKeyValueList(this DbFunctions _, IEnumerable<KeyValuePair<string, string>> input)
36+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(ToKeyValueList)));
37+
38+
/// <summary>
39+
/// Constructs an hstore `Dictionary&lt;string, string>` from a key/value pair string array
40+
/// <br/>
41+
/// SQL translation: hstore(input)
42+
/// </summary>
43+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
44+
/// <param name="input">The input string array of key value pairs.</param>
45+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
46+
public static Dictionary<string, string> DictionaryFromKeyValueList(this DbFunctions _, IList<string> input)
47+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DictionaryFromKeyValueList)));
48+
49+
/// <summary>
50+
/// Constructs an hstore `Dictionary&lt;string, string>` from a string array of keys and a string array of values
51+
/// <br/>
52+
/// SQL translation: hstore(keys, values)
53+
/// </summary>
54+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
55+
/// <param name="keys">The input string array of keys.</param>
56+
/// <param name="values">The input string array of values.</param>
57+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
58+
public static Dictionary<string, string> DictionaryFromKeysAndValues(this DbFunctions _, IList<string> keys, IList<string> values)
59+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DictionaryFromKeysAndValues)));
60+
61+
/// <summary>
62+
/// Converts an `hstore` to a `json` value type `Dictionary&lt;string, object?>`, but attempts to distinguish numerical and boolean values so they are unquoted in the JSON.
63+
/// <br/>
64+
/// SQL translation: hstore_to_json_loose(input)
65+
/// </summary>
66+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
67+
/// <param name="input">The input hstore.</param>
68+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
69+
public static Dictionary<string, object?> ToJsonLoose(this DbFunctions _, IEnumerable<KeyValuePair<string, string>> input)
70+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(ToJsonLoose)));
71+
72+
/// <summary>
73+
/// Converts an `hstore` to a `jsonb` value type `Dictionary&lt;string, object?>`, but attempts to distinguish numerical and boolean values so they are unquoted in the JSON.
74+
/// <br/>
75+
/// SQL translation: hstore_to_jsonb_loose(input)
76+
/// </summary>
77+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
78+
/// <param name="input">The input hstore.</param>
79+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
80+
public static Dictionary<string, object?> ToJsonbLoose(this DbFunctions _, IEnumerable<KeyValuePair<string, string>> input)
81+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(ToJsonbLoose)));
82+
83+
/// <summary>
84+
/// Converts a `json` or `jsonb` type `IEnumerable&lt;KeyValuePair&lt;string, string>>` (i.e. Dictionary&lt;string, string>> or related type) to an hstore type `Dictionary&lt;string, string>>`
85+
/// <br/>
86+
/// Can be used during a migration of changing a column's StoreType from 'json' to 'hstore' with the `Using` clause
87+
///
88+
/// HStore SQL translation: input
89+
/// Json SQL translation: select hstore(array_agg(key), array_agg(value)) FROM json_each_text(input)
90+
/// Json SQL translation: select hstore(array_agg(key), array_agg(value)) FROM jsonb_each_text(input)
91+
/// </summary>
92+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
93+
/// <param name="input">The input hstore.</param>
94+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
95+
public static Dictionary<string, string> ToHstore(this DbFunctions _, IEnumerable<KeyValuePair<string, string>> input)
96+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(ToHstore)));
97+
98+
/// <summary>
99+
/// Converts an `jsonb` or `hstore` type value to a `jsonb` type `Dictionary&lt;TKey, TKey>`
100+
/// <br/>
101+
///
102+
/// Hstore SQL translation: hstore_to_json(input)
103+
/// Json SQL translation: input
104+
/// Jsonb SQL translation: input::json
105+
/// </summary>
106+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
107+
/// <param name="input">The input hstore.</param>
108+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
109+
public static Dictionary<string, TValue> ToJson<TValue>(this DbFunctions _, IEnumerable<KeyValuePair<string, TValue>> input)
110+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(ToJson)));
111+
112+
/// <summary>
113+
/// Converts an `hstore` or `json` type value to a `jsonb` type `Dictionary&lt;TKey, TValue>`
114+
///
115+
/// Can be used during a migration of changing a column's StoreType from 'hstore' to 'jsonb' with the `Using` clause
116+
/// <br/>
117+
/// HStore SQL translation: hstore_to_jsonb(input)
118+
/// Json SQL translation: input::jsonb
119+
/// Jsonb SQL translation: input
120+
/// </summary>
121+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
122+
/// <param name="input">The input hstore.</param>
123+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
124+
public static Dictionary<string, TValue> ToJsonb<TValue>(this DbFunctions _, IEnumerable<KeyValuePair<string, TValue>> input)
125+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(ToJsonb)));
126+
}

0 commit comments

Comments
 (0)