|
14 | 14 |
|
15 | 15 | """High-level wrapper for datastore queries.""" |
16 | 16 |
|
| 17 | +from google.cloud.ndb import _exceptions |
| 18 | + |
17 | 19 |
|
18 | 20 | __all__ = [ |
19 | 21 | "Cursor", |
@@ -64,8 +66,66 @@ def __ne__(self, other): |
64 | 66 |
|
65 | 67 |
|
66 | 68 | class Parameter(ParameterizedThing): |
67 | | - def __init__(self, *args, **kwargs): |
68 | | - raise NotImplementedError |
| 69 | + """Represents a bound variable in a GQL query. |
| 70 | +
|
| 71 | + ``Parameter(1)`` corresponds to a slot labeled ``:1`` in a GQL query. |
| 72 | + ``Parameter('xyz')`` corresponds to a slot labeled ``:xyz``. |
| 73 | +
|
| 74 | + The value must be set (bound) separately by calling :meth:`set`. |
| 75 | +
|
| 76 | + Args: |
| 77 | + key (Union[str, int]): The parameter key. |
| 78 | +
|
| 79 | + Raises: |
| 80 | + TypeError: If the ``key`` is not a string or integer. |
| 81 | + """ |
| 82 | + |
| 83 | + def __init__(self, key): |
| 84 | + if not isinstance(key, (int, str, bytes)): |
| 85 | + raise TypeError( |
| 86 | + "Parameter key must be an integer or string, not {}".format( |
| 87 | + key |
| 88 | + ) |
| 89 | + ) |
| 90 | + self._key = key |
| 91 | + |
| 92 | + def __repr__(self): |
| 93 | + return "{}({!r})".format(self.__class__.__name__, self._key) |
| 94 | + |
| 95 | + def __eq__(self, other): |
| 96 | + if not isinstance(other, Parameter): |
| 97 | + return NotImplemented |
| 98 | + |
| 99 | + return self._key == other._key |
| 100 | + |
| 101 | + @property |
| 102 | + def key(self): |
| 103 | + """Retrieve the key.""" |
| 104 | + return self._key |
| 105 | + |
| 106 | + def resolve(self, bindings, used): |
| 107 | + """Resolve the current parameter from the parameter bindings. |
| 108 | +
|
| 109 | + Args: |
| 110 | + bindings (dict): A mapping of parameter bindings. |
| 111 | + used (Dict[Union[str, int], bool]): A mapping of already used |
| 112 | + parameters. This will be modified if the current parameter |
| 113 | + is in ``bindings``. |
| 114 | +
|
| 115 | + Returns: |
| 116 | + Any: The bound value for the current parameter. |
| 117 | +
|
| 118 | + Raises: |
| 119 | + .BadArgumentError: If the current parameter is not in ``bindings``. |
| 120 | + """ |
| 121 | + key = self._key |
| 122 | + if key not in bindings: |
| 123 | + raise _exceptions.BadArgumentError( |
| 124 | + "Parameter :{} is not bound.".format(key) |
| 125 | + ) |
| 126 | + value = bindings[key] |
| 127 | + used[key] = True |
| 128 | + return value |
69 | 129 |
|
70 | 130 |
|
71 | 131 | class ParameterizedFunction(ParameterizedThing): |
|
0 commit comments