Changeset 803

Show
Ignore:
Timestamp:
03/07/07 21:47:04 (2 years ago)
Author:
mfenniak
Message:

add client encoding support

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • pg8000/trunk/pg8000.py

    r802 r803  
    266266 
    267267    class Bind(object): 
    268         def __init__(self, portal, ps, in_fc, params, out_fc): 
     268        def __init__(self, portal, ps, in_fc, params, out_fc, client_encoding): 
    269269            self.portal = portal 
    270270            self.ps = ps 
     
    278278                else: 
    279279                    fc = self.in_fc[i] 
    280                 self.params.append(Types.pg_value(params[i], fc)) 
     280                self.params.append(Types.pg_value(params[i], fc, client_encoding)) 
    281281            self.out_fc = out_fc 
    282282 
     
    551551    class Connection(object): 
    552552        def __init__(self, host=None, port=5432): 
    553             self.state = "unconnected" 
    554             self.host = host 
    555             self.port = port 
    556             self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
     553            self._state = "unconnected" 
     554            self._client_encoding = "ascii" 
     555            self._host = host 
     556            self._port = port 
     557            self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
     558            self._backend_key_data = None 
    557559 
    558560        def verifyState(self, state): 
    559             if self.state != state: 
    560                 raise InternalError("connection state must be %s, is %s" % (state, self.state)) 
     561            if self._state != state: 
     562                raise InternalError("connection state must be %s, is %s" % (state, self._state)) 
    561563 
    562564        def _send(self, msg): 
    563             self.sock.send(msg.serialize()) 
     565            self._sock.send(msg.serialize()) 
    564566 
    565567        def _read_message(self): 
    566             bytes = self.sock.recv(5) 
     568            bytes = self._sock.recv(5) 
    567569            assert len(bytes) == 5 
    568570            message_code = bytes[0] 
    569571            data_len = struct.unpack("!i", bytes[1:])[0] - 4 
    570             bytes = self.sock.recv(data_len) 
     572            bytes = self._sock.recv(data_len) 
    571573            msg = Protocol.message_types[message_code].createFromData(bytes) 
    572574            if isinstance(msg, Protocol.NoticeResponse): 
     
    578580        def connect(self): 
    579581            self.verifyState("unconnected") 
    580             self.sock.connect((self.host, self.port)) 
    581             self.state = "noauth" 
     582            self._sock.connect((self._host, self._port)) 
     583            self._state = "noauth" 
    582584 
    583585        def authenticate(self, user, **kwargs): 
     
    587589            if isinstance(msg, Protocol.AuthenticationRequest): 
    588590                if msg.ok(self, user, **kwargs): 
    589                     self.state = "auth" 
    590                     self._waitForReady() 
     591                    self._state = "auth" 
     592                    while 1: 
     593                        msg = self._read_message() 
     594                        if isinstance(msg, Protocol.ReadyForQuery): 
     595                            # done reading messages 
     596                            self._state = "ready" 
     597                            break 
     598                        elif isinstance(msg, Protocol.ParameterStatus): 
     599                            if msg.key == "client_encoding": 
     600                                self._client_encoding = msg.value 
     601                        elif isinstance(msg, Protocol.BackendKeyData): 
     602                            self._backend_key_data = msg 
     603                        elif isinstance(msg, Protocol.ErrorResponse): 
     604                            raise msg.createException() 
     605                        else: 
     606                            raise InternalError("unexpected msg %r" % msg) 
    591607                else: 
    592608                    raise InterfaceError("authentication method %s failed" % msg.__class__.__name__) 
     
    594610                raise InternalError("StartupMessage was responded to with non-AuthenticationRequest msg %r" % msg) 
    595611 
    596         def _waitForReady(self): 
    597             while 1: 
    598                 msg = self._read_message() 
    599                 if isinstance(msg, Protocol.ReadyForQuery): 
    600                     self.state = "ready" 
    601                     break 
    602                 elif isinstance(msg, Protocol.ErrorResponse): 
    603                     raise msg.createException() 
    604  
    605612        def extended_query(self, portal, statement, qs, params): 
    606613            self.verifyState("ready") 
    607614            self._send(Protocol.Parse(statement, qs, [type(x) for x in params])) 
    608             self._send(Protocol.Bind(portal, statement, (0,), params, (0,))) 
     615            self._send(Protocol.Bind(portal, statement, (0,), params, (0,), self._client_encoding)) 
    609616            self._send(Protocol.DescribePortal(portal)) 
    610617            self._send(Protocol.Flush()) 
     
    656663                    self._send(Protocol.ClosePortal(portal)) 
    657664                    self._send(Protocol.Sync()) 
    658                     self._waitForReady() 
     665                    while 1: 
     666                        msg = self._read_message() 
     667                        if isinstance(msg, Protocol.ReadyForQuery): 
     668                            # ready to move on with life... 
     669                            self._state = "ready" 
     670                            break 
     671                        elif isinstance(msg, Protocol.CloseComplete): 
     672                            # ok, great! 
     673                            pass 
     674                        elif isinstance(msg, Protocol.ErrorResponse): 
     675                            raise msg.createException() 
     676                        else: 
     677                            raise InternalError("unexpected response msg %r" % msg) 
    659678                    end_of_data = True 
    660679                    break 
     
    670689            msg = self._read_message() 
    671690            if isinstance(msg, Protocol.RowDescription): 
    672                 self.state = "in_query" 
     691                self._state = "in_query" 
    673692                return msg 
    674693            elif isinstance(msg, Protocol.ErrorResponse): 
     
    713732    pg_type_id = staticmethod(pg_type_id) 
    714733 
    715     def pg_value(v, fc): 
     734    def pg_value(v, fc, client_encoding): 
    716735        typ = type(v) 
    717736        data = Types.py_types.get(typ) 
     
    725744        if func == None: 
    726745            raise NotSupportedError("type %r, format code %r not converted" % (typ, fc)) 
    727         return func(v
     746        return func(v, client_encoding
    728747    pg_value = staticmethod(pg_value) 
    729748 
     
    755774        return int(data) 
    756775 
    757     def int4out(v): 
    758         return str(v) 
    759  
    760     def int4send(v): 
    761         return struct.pack("!i", v) 
    762  
    763776    def timestamp_recv(data, description): 
    764777        val = struct.unpack("!d", data)[0] 
     
    780793            return decimal(data) 
    781794 
    782     def numeric_out(v): 
     795    def numeric_out(v, ce): 
    783796        return str(v) 
    784797 
     
    786799        return unicode(data, "utf-8") 
    787800 
    788     def varcharout(v): 
    789         return v.encode("utf-8"
     801    def varcharout(v, ce): 
     802        return v.encode(ce
    790803 
    791804    py_types = {