Esempio n. 1
0
    def __call__(self, value, length, cursor):
        from psycopg2ct.extensions import typecast

        s = value
        assert s[0] == "{" and s[-1] == "}"
        i = 1
        array = []
        stack = [array]
        value_length = len(s) - 1
        while i < value_length:
            if s[i] == '{':
                sub_array = []
                array.append(sub_array)
                stack.append(sub_array)
                array = sub_array
                i += 1
            elif s[i] == '}':
                stack.pop()
                array = stack[-1]
                i += 1
            elif s[i] in ', ':
                i += 1
            else:
                start = i

                # Number of quotes, this will always be 0 or 2 (int vs str)
                quotes = 0

                # Whether or not the next char should be escaped
                escape_char = False

                while i < value_length:
                    if s[i] == '"':
                        if not escape_char:
                            quotes += 1
                    elif s[i] == '\\':
                        escape_char = not escape_char
                    elif s[i] == '}' or s[i] == ',':
                        if not escape_char and quotes % 2 == 0:
                            break
                    i += 1

                if quotes:
                    start += 1
                    end = i - 1
                else:
                    end = i

                str_buf = s[start:end].replace(r'\\', '\\')
                val = typecast(self._caster, str_buf, end - start, cursor)
                array.append(val)
        return stack[-1]
Esempio n. 2
0
    def _build_row(self, row_num):
        n = self._nfields
        row = []
        for i in xrange(n):
            if libpq.PQgetisnull(self._pgres, row_num, i):
                val = None
            else:
                val = libpq.PQgetvalue(self._pgres, row_num, i)
                length = libpq.PQgetlength(self._pgres, row_num, i)
                val = extensions.typecast(self._casts[i], val, length, self)

            row.append(val)
        return tuple(row)