A container for mapping python types to CQL string literals when working with non-prepared statements. The type :attr:`~.Encoder.mapping` can be directly customized by users.
| 48 | |
| 49 | |
| 50 | class Encoder(object): |
| 51 | """ |
| 52 | A container for mapping python types to CQL string literals when working |
| 53 | with non-prepared statements. The type :attr:`~.Encoder.mapping` can be |
| 54 | directly customized by users. |
| 55 | """ |
| 56 | |
| 57 | mapping = None |
| 58 | """ |
| 59 | A map of python types to encoder functions. |
| 60 | """ |
| 61 | |
| 62 | def __init__(self): |
| 63 | self.mapping = { |
| 64 | float: self.cql_encode_float, |
| 65 | Decimal: self.cql_encode_decimal, |
| 66 | bytearray: self.cql_encode_bytes, |
| 67 | str: self.cql_encode_str, |
| 68 | int: self.cql_encode_object, |
| 69 | UUID: self.cql_encode_object, |
| 70 | datetime.datetime: self.cql_encode_datetime, |
| 71 | datetime.date: self.cql_encode_date, |
| 72 | datetime.time: self.cql_encode_time, |
| 73 | Date: self.cql_encode_date_ext, |
| 74 | Time: self.cql_encode_time, |
| 75 | dict: self.cql_encode_map_collection, |
| 76 | OrderedDict: self.cql_encode_map_collection, |
| 77 | OrderedMap: self.cql_encode_map_collection, |
| 78 | OrderedMapSerializedKey: self.cql_encode_map_collection, |
| 79 | list: self.cql_encode_list_collection, |
| 80 | tuple: self.cql_encode_list_collection, # TODO: change to tuple in next major |
| 81 | set: self.cql_encode_set_collection, |
| 82 | sortedset: self.cql_encode_set_collection, |
| 83 | frozenset: self.cql_encode_set_collection, |
| 84 | types.GeneratorType: self.cql_encode_list_collection, |
| 85 | ValueSequence: self.cql_encode_sequence, |
| 86 | Point: self.cql_encode_str_quoted, |
| 87 | LineString: self.cql_encode_str_quoted, |
| 88 | Polygon: self.cql_encode_str_quoted |
| 89 | } |
| 90 | |
| 91 | self.mapping.update({ |
| 92 | memoryview: self.cql_encode_bytes, |
| 93 | bytes: self.cql_encode_bytes, |
| 94 | type(None): self.cql_encode_none, |
| 95 | ipaddress.IPv4Address: self.cql_encode_ipaddress, |
| 96 | ipaddress.IPv6Address: self.cql_encode_ipaddress |
| 97 | }) |
| 98 | |
| 99 | def cql_encode_none(self, val): |
| 100 | """ |
| 101 | Converts :const:`None` to the string 'NULL'. |
| 102 | """ |
| 103 | return 'NULL' |
| 104 | |
| 105 | def cql_encode_unicode(self, val): |
| 106 | """ |
| 107 | Converts :class:`unicode` objects to UTF-8 encoded strings with quote escaping. |
no outgoing calls