|
| 1 | +''' |
| 2 | +Copyright (c) 2014-2018, Bitnine Inc. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +''' |
| 16 | + |
| 17 | +import sys |
| 18 | + |
| 19 | +from psycopg2.extensions import ISQLQuote |
| 20 | +from psycopg2.extras import json |
| 21 | + |
| 22 | +# borrowed from simplejson's compat.py |
| 23 | +if sys.version_info[0] < 3: |
| 24 | + string_types = (basestring,) |
| 25 | + integer_types = (int, long) |
| 26 | + def dict_items(o): |
| 27 | + return o.iteritems() |
| 28 | +else: |
| 29 | + string_types = (str,) |
| 30 | + integer_types = (int,) |
| 31 | + def dict_items(o): |
| 32 | + return o.items() |
| 33 | + |
| 34 | +def quote_string(s): |
| 35 | + s = s[1:-1] |
| 36 | + s = "'" + s.replace("'", "''") + "'" |
| 37 | + return s |
| 38 | + |
| 39 | +class PropertyEncoder(object): |
| 40 | + def encode(self, o): |
| 41 | + chunks = self.iterencode(o) |
| 42 | + if not isinstance(chunks, (list, tuple)): |
| 43 | + chunks = list(chunks) |
| 44 | + return ''.join(chunks) |
| 45 | + |
| 46 | + def iterencode(self, o): |
| 47 | + markers = {} |
| 48 | + _iterencode = _make_iterencode(markers, json.dumps, quote_string) |
| 49 | + return _iterencode(o) |
| 50 | + |
| 51 | +def _make_iterencode(markers, _encoder, _quote_string, |
| 52 | + dict=dict, |
| 53 | + float=float, |
| 54 | + id=id, |
| 55 | + isinstance=isinstance, |
| 56 | + list=list, |
| 57 | + tuple=tuple, |
| 58 | + string_types=string_types, |
| 59 | + integer_types=integer_types, |
| 60 | + dict_items=dict_items): |
| 61 | + def _iterencode_list(o): |
| 62 | + if not o: |
| 63 | + yield '[]' |
| 64 | + return |
| 65 | + |
| 66 | + markerid = id(o) |
| 67 | + if markerid in markers: |
| 68 | + raise ValueError('Circular reference detected') |
| 69 | + markers[markerid] = o |
| 70 | + |
| 71 | + yield '[' |
| 72 | + first = True |
| 73 | + for e in o: |
| 74 | + if first: |
| 75 | + first = False |
| 76 | + else: |
| 77 | + yield ',' |
| 78 | + |
| 79 | + for chunk in _iterencode(e): |
| 80 | + yield chunk |
| 81 | + yield ']' |
| 82 | + |
| 83 | + del markers[markerid] |
| 84 | + |
| 85 | + def _iterencode_dict(o): |
| 86 | + if not o: |
| 87 | + yield '{}' |
| 88 | + return |
| 89 | + |
| 90 | + markerid = id(o) |
| 91 | + if markerid in markers: |
| 92 | + raise ValueError('Circular reference detected') |
| 93 | + markers[markerid] = o |
| 94 | + |
| 95 | + yield '{' |
| 96 | + first = True |
| 97 | + for k, v in dict_items(o): |
| 98 | + if isinstance(k, string_types): |
| 99 | + pass |
| 100 | + elif (k is True or k is False or k is None or |
| 101 | + isinstance(k, integer_types) or isinstance(k, float)): |
| 102 | + k = _encoder(k) |
| 103 | + else: |
| 104 | + raise TypeError('keys must be str, int, float, bool or None, ' |
| 105 | + 'not %s' % k.__class__.__name__) |
| 106 | + |
| 107 | + if first: |
| 108 | + first = False |
| 109 | + else: |
| 110 | + yield ',' |
| 111 | + |
| 112 | + yield _quote_string(_encoder(k)) |
| 113 | + yield ':' |
| 114 | + for chunk in _iterencode(v): |
| 115 | + yield chunk |
| 116 | + yield '}' |
| 117 | + |
| 118 | + del markers[markerid] |
| 119 | + |
| 120 | + def _iterencode(o): |
| 121 | + if isinstance(o, string_types): |
| 122 | + yield _quote_string(_encoder(o)) |
| 123 | + elif isinstance(o, (list, tuple)): |
| 124 | + for chunk in _iterencode_list(o): |
| 125 | + yield chunk |
| 126 | + elif isinstance(o, dict): |
| 127 | + for chunk in _iterencode_dict(o): |
| 128 | + yield chunk |
| 129 | + else: |
| 130 | + yield _encoder(o) |
| 131 | + |
| 132 | + return _iterencode |
| 133 | + |
| 134 | +_default_encoder = PropertyEncoder() |
| 135 | + |
| 136 | +class Property(object): |
| 137 | + def __init__(self, value): |
| 138 | + self.value = value |
| 139 | + |
| 140 | + def __conform__(self, proto): |
| 141 | + if proto is ISQLQuote: |
| 142 | + return self |
| 143 | + |
| 144 | + def prepare(self, conn): |
| 145 | + self._conn = conn |
| 146 | + |
| 147 | + def getquoted(self): |
| 148 | + return _default_encoder.encode(self.value) |
0 commit comments