Skip to content

Commit 33ed270

Browse files
committed
Hstore query support
Fixes npgsql#212
1 parent 30cebf0 commit 33ed270

File tree

10 files changed

+1491
-4
lines changed

10 files changed

+1491
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
// ReSharper disable once CheckNamespace
2+
3+
namespace Microsoft.EntityFrameworkCore;
4+
5+
/// <summary>
6+
/// Provides extension methods supporting `hstore` function translation for PostgreSQL.
7+
/// </summary>
8+
public static class StringKeyValuePairDbFunctionsExtensions
9+
{
10+
/// <summary>
11+
/// Returns values associated with given keys, or NULL if not present.
12+
///
13+
/// Works with `hstore`, `json` and `jsonb` type columns
14+
/// <br/>
15+
/// SQL translation: input -> keys
16+
/// Note: input will automatically translate to `hstore` if `jsonb` or `json`
17+
/// </summary>
18+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
19+
/// <param name="input">The input hstore.</param>
20+
/// <param name="keys">The keys to return the values of from the input hstore.</param>
21+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
22+
public static List<string> ValuesForKeys(this DbFunctions _, IEnumerable<KeyValuePair<string, string>> input, IList<string> keys)
23+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(ValuesForKeys)));
24+
25+
/// <summary>
26+
/// Does dictionary contain all the specified keys?
27+
///
28+
/// Works with `hstore`, `json` and `jsonb` type columns
29+
/// <br/>
30+
/// SQL translation: input ?&amp; keys
31+
/// </summary>
32+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
33+
/// <param name="input">The input hstore.</param>
34+
/// <param name="keys">The keys to check if the input store contains all of.</param>
35+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
36+
public static bool ContainsAllKeys(this DbFunctions _, IEnumerable<KeyValuePair<string, string>> input, IList<string> keys)
37+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(ContainsAllKeys)));
38+
39+
/// <summary>
40+
/// Does dictionary contain any of the specified keys?
41+
///
42+
/// Works with `hstore`, `json` and `jsonb` type columns
43+
/// <br/>
44+
/// SQL translation: input ?| keys
45+
/// </summary>
46+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
47+
/// <param name="input">The input hstore.</param>
48+
/// <param name="keys">The keys to check if the input store contains any of.</param>
49+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
50+
public static bool ContainsAnyKeys(this DbFunctions _, IEnumerable<KeyValuePair<string, string>> input, IList<string> keys)
51+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(ContainsAnyKeys)));
52+
53+
/// <summary>
54+
/// Does left operand contain right?
55+
///
56+
/// Works with `hstore`, `json` and `jsonb` type columns
57+
/// <br/>
58+
/// SQL translation: left @> right
59+
/// </summary>
60+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
61+
/// <param name="left">The input left hstore.</param>
62+
/// <param name="right">The input right hstore.</param>
63+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
64+
public static bool Contains(this DbFunctions _, IEnumerable<KeyValuePair<string, string>> left, IEnumerable<KeyValuePair<string, string>> right)
65+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Contains)));
66+
67+
/// <summary>
68+
/// Is left operand contained by right?
69+
/// <br/>
70+
/// SQL translation: left &lt;@ right
71+
/// </summary>
72+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
73+
/// <param name="left">The input left hstore.</param>
74+
/// <param name="right">The input right hstore.</param>
75+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
76+
public static bool ContainedBy(this DbFunctions _, IEnumerable<KeyValuePair<string, string>> left, IEnumerable<KeyValuePair<string, string>> right)
77+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(ContainedBy)));
78+
79+
/// <summary>
80+
/// Deletes keys from input operand.
81+
///
82+
/// Works with `hstore`, `json` and `jsonb` type columns
83+
/// <br/>
84+
/// SQL translation: input - key
85+
/// </summary>
86+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
87+
/// <param name="input">The input hstore.</param>
88+
/// <param name="key">The key to remove.</param>
89+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
90+
public static T Remove<T>(this DbFunctions _, T input, string key)
91+
where T : IEnumerable<KeyValuePair<string, string>>
92+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Remove)));
93+
94+
/// <summary>
95+
/// Extracts a subset of a dictionary containing only the specified keys.
96+
///
97+
/// For `jsonb` and `json` input they will be automatically converted to `hstore` but
98+
/// the result will always be an `hstore` type
99+
/// <br/>
100+
/// SQL translation: slice(input, keys)
101+
/// </summary>
102+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
103+
/// <param name="input">The input hstore.</param>
104+
/// <param name="keys">The keys to extract the subset of.</param>
105+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
106+
public static T Slice<T>(this DbFunctions _, T input, IList<string> keys)
107+
where T : IEnumerable<KeyValuePair<string, string>>
108+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Slice)));
109+
110+
/// <summary>
111+
/// Converts hstore to an array of alternating keys and values.
112+
/// <br/>
113+
/// SQL translation: hstore_to_array(input)
114+
/// </summary>
115+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
116+
/// <param name="input">The input hstore.</param>
117+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
118+
public static List<string> ToKeysAndValues(this DbFunctions _, IEnumerable<KeyValuePair<string, string>> input)
119+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(ToKeysAndValues)));
120+
121+
/// <summary>
122+
/// Constructs an hstore from a key/value pair string array
123+
/// <br/>
124+
/// SQL translation: hstore(input)
125+
/// </summary>
126+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
127+
/// <param name="input">The input string array of key value pairs.</param>
128+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
129+
public static Dictionary<string, string> FromKeysAndValues(this DbFunctions _, IList<string> input)
130+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(FromKeysAndValues)));
131+
132+
/// <summary>
133+
/// Constructs an hstore from a string array of keys and a string array of values
134+
/// <br/>
135+
/// SQL translation: hstore(keys, values)
136+
/// </summary>
137+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
138+
/// <param name="keys">The input string array of keys.</param>
139+
/// <param name="values">The input string array of values.</param>
140+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
141+
public static Dictionary<string, string> FromKeysAndValues(this DbFunctions _, IList<string> keys, IList<string> values)
142+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(FromKeysAndValues)));
143+
144+
/// <summary>
145+
/// Converts an hstore to a `json` type `Dictionary&lt;string, string>`
146+
/// <br/>
147+
/// Can be used during a migration of changing a column's StoreType from 'hstore' to a 'json' with the `Using` clause
148+
/// SQL translation: hstore_to_json(input)
149+
/// </summary>
150+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
151+
/// <param name="input">The input hstore.</param>
152+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
153+
public static Dictionary<string, string> ToJson(this DbFunctions _, IEnumerable<KeyValuePair<string, string>> input)
154+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(ToJson)));
155+
156+
/// <summary>
157+
/// Converts a `json` type `IEnumerable&lt;KeyValuePair&lt;string, string>>` (i.e. Dictionary&lt;string, string>> or related type) to an hstore type `Dictionary&lt;string, string>>`
158+
/// <br/>
159+
/// Can be used during a migration of changing a column's StoreType from 'json' to 'hstore' with the `Using` clause
160+
/// SQL translation: select hstore(array_agg(key), array_agg(value)) FROM json_each_text(json)
161+
/// </summary>
162+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
163+
/// <param name="input">The input hstore.</param>
164+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
165+
public static Dictionary<string, string> FromJson(this DbFunctions _, IEnumerable<KeyValuePair<string, string>> input)
166+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(FromJson)));
167+
168+
/// <summary>
169+
/// Converts an hstore to a `jsonb` type `Dictionary&lt;string, string>`
170+
///
171+
/// Can be used during a migration of changing a column's StoreType from 'hstore' to 'jsonb' with the `Using` clause
172+
/// <br/>
173+
/// SQL translation: hstore_to_jsonb(input)
174+
/// </summary>
175+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
176+
/// <param name="input">The input hstore.</param>
177+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
178+
public static Dictionary<string, string> ToJsonb(this DbFunctions _, IEnumerable<KeyValuePair<string, string>> input)
179+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(ToJsonb)));
180+
181+
/// <summary>
182+
/// Converts a `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>>`
183+
/// <br/>
184+
/// Can be used during a migration of changing a column's StoreType from 'jsonb' to 'hstore' with the `Using` clause
185+
/// SQL translation: select hstore(array_agg(key), array_agg(value)) FROM jsonb_each_text(json)
186+
/// </summary>
187+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
188+
/// <param name="input">The input hstore.</param>
189+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
190+
public static Dictionary<string, string> FromJsonb(this DbFunctions _, IEnumerable<KeyValuePair<string, string>> input)
191+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(FromJsonb)));
192+
193+
/// <summary>
194+
/// 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.
195+
/// <br/>
196+
/// SQL translation: hstore_to_json_loose(input)
197+
/// </summary>
198+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
199+
/// <param name="input">The input hstore.</param>
200+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
201+
public static Dictionary<string, object?> ToJsonLoose(this DbFunctions _, IEnumerable<KeyValuePair<string, string>> input)
202+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(ToJsonLoose)));
203+
204+
/// <summary>
205+
/// 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.
206+
/// <br/>
207+
/// SQL translation: hstore_to_jsonb_loose(input)
208+
/// </summary>
209+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
210+
/// <param name="input">The input hstore.</param>
211+
/// <seealso href="https://www.postgresql.org/docs/current/hstore.html#HSTORE-OPS-FUNCS">PostgreSQL documentation for 'hstore' functions.</seealso>
212+
public static Dictionary<string, object?> ToJsonbLoose(this DbFunctions _, IEnumerable<KeyValuePair<string, string>> input)
213+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(ToJsonbLoose)));
214+
}

0 commit comments

Comments
 (0)