Skip to content

Commit a235733

Browse files
author
Ghislain Fourny
committed
Add extra parameter.
1 parent 69b1a85 commit a235733

1 file changed

Lines changed: 36 additions & 3 deletions

File tree

src/jsoniq/session.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ def convert(self, value):
157157
else:
158158
raise ValueError("Cannot yet convert value of type " + str(type(value)) + " to a RumbleDB item. Please open an issue and we will look into it!")
159159

160+
def unbind(self, name: str):
161+
conf = self._jrumblesession.getConfiguration();
162+
if not name.startswith("$"):
163+
raise ValueError("Variable name must start with a dollar symbol ('$').")
164+
name = name[1:]
165+
conf.resetExternalVariableValue(name);
166+
160167
def bind(self, name: str, valueToBind):
161168
conf = self._jrumblesession.getConfiguration();
162169
if not name.startswith("$"):
@@ -177,7 +184,28 @@ def bind(self, name: str, valueToBind):
177184
elif isinstance(valueToBind, tuple):
178185
conf.setExternalVariableValue(name, self.convert(valueToBind))
179186
elif isinstance(valueToBind, list):
180-
raise ValueError("To avoid confusion, a sequence of items must be provided as a Python tuple, not as a Python list. Lists are mapped to single array items, while tuples are mapped to sequences of items. If you want to interpret the list as a sequence of items (one item for each list member), then you need to change this list to a tuple by wrapping it into a tuple() call. If you want to bind the variable to one array item, then you need to wrap the provided list inside a singleton tuple and try again, or you can also call bindOne() instead.")
187+
raise ValueError("""
188+
To avoid confusion, a sequence of items must be provided as a Python tuple, not as a Python list.
189+
Lists are mapped to single array items, while tuples are mapped to sequences of items.
190+
191+
If you want to interpret the list as a sequence of items (one item for each list member), then you need to convert it to a tuple.
192+
Example: [1,2,3] should then be rewritten as tuple([1,2,3]) for the sequence of three (integer) items 1, 2, and 3.
193+
194+
If you want to interpret the list as a sequence of one array item, then you need to create a singleton tuple.
195+
Example: [1,2,3] should then be rewritten as ([1,2,3],) for the sequence of one (array) item [1,2,3].
196+
""")
197+
elif isinstance(valueToBind, dict):
198+
conf.setExternalVariableValue(name, self.convert((valueToBind, )))
199+
elif isinstance(valueToBind, str):
200+
conf.setExternalVariableValue(name, self.convert((valueToBind, )))
201+
elif isinstance(valueToBind, int):
202+
conf.setExternalVariableValue(name, self.convert((valueToBind, )))
203+
elif isinstance(valueToBind, float):
204+
conf.setExternalVariableValue(name, self.convert((valueToBind, )))
205+
elif isinstance(valueToBind, bool):
206+
conf.setExternalVariableValue(name, self.convert((valueToBind, )))
207+
elif valueToBind is None:
208+
conf.setExternalVariableValue(name, self.convert((valueToBind, )))
181209
elif(hasattr(valueToBind, "_get_object_id")):
182210
conf.setExternalVariableValue(name, valueToBind);
183211
else:
@@ -198,9 +226,14 @@ def bindDataFrameAsVariable(self, name: str, df):
198226
conf.setExternalVariableValue(name, df._jdf);
199227
return self;
200228

201-
def jsoniq(self, str):
229+
def jsoniq(self, str, **kwargs):
230+
for key, value in kwargs.items():
231+
self.bind(f"${key}", value);
202232
sequence = self._jrumblesession.runQuery(str);
203-
return SequenceOfItems(sequence, self);
233+
seq = SequenceOfItems(sequence, self);
234+
for key, value in kwargs.items():
235+
self.unbind(f"${key}");
236+
return seq;
204237

205238
def __getattr__(self, item):
206239
return getattr(self._sparksession, item)

0 commit comments

Comments
 (0)