Changeset 803
- Timestamp:
- 03/07/07 21:47:04 (2 years ago)
- Files:
-
- pg8000/trunk/pg8000.py (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
pg8000/trunk/pg8000.py
r802 r803 266 266 267 267 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): 269 269 self.portal = portal 270 270 self.ps = ps … … 278 278 else: 279 279 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)) 281 281 self.out_fc = out_fc 282 282 … … 551 551 class Connection(object): 552 552 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 557 559 558 560 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)) 561 563 562 564 def _send(self, msg): 563 self. sock.send(msg.serialize())565 self._sock.send(msg.serialize()) 564 566 565 567 def _read_message(self): 566 bytes = self. sock.recv(5)568 bytes = self._sock.recv(5) 567 569 assert len(bytes) == 5 568 570 message_code = bytes[0] 569 571 data_len = struct.unpack("!i", bytes[1:])[0] - 4 570 bytes = self. sock.recv(data_len)572 bytes = self._sock.recv(data_len) 571 573 msg = Protocol.message_types[message_code].createFromData(bytes) 572 574 if isinstance(msg, Protocol.NoticeResponse): … … 578 580 def connect(self): 579 581 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" 582 584 583 585 def authenticate(self, user, **kwargs): … … 587 589 if isinstance(msg, Protocol.AuthenticationRequest): 588 590 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) 591 607 else: 592 608 raise InterfaceError("authentication method %s failed" % msg.__class__.__name__) … … 594 610 raise InternalError("StartupMessage was responded to with non-AuthenticationRequest msg %r" % msg) 595 611 596 def _waitForReady(self):597 while 1:598 msg = self._read_message()599 if isinstance(msg, Protocol.ReadyForQuery):600 self.state = "ready"601 break602 elif isinstance(msg, Protocol.ErrorResponse):603 raise msg.createException()604 605 612 def extended_query(self, portal, statement, qs, params): 606 613 self.verifyState("ready") 607 614 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)) 609 616 self._send(Protocol.DescribePortal(portal)) 610 617 self._send(Protocol.Flush()) … … 656 663 self._send(Protocol.ClosePortal(portal)) 657 664 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) 659 678 end_of_data = True 660 679 break … … 670 689 msg = self._read_message() 671 690 if isinstance(msg, Protocol.RowDescription): 672 self. state = "in_query"691 self._state = "in_query" 673 692 return msg 674 693 elif isinstance(msg, Protocol.ErrorResponse): … … 713 732 pg_type_id = staticmethod(pg_type_id) 714 733 715 def pg_value(v, fc ):734 def pg_value(v, fc, client_encoding): 716 735 typ = type(v) 717 736 data = Types.py_types.get(typ) … … 725 744 if func == None: 726 745 raise NotSupportedError("type %r, format code %r not converted" % (typ, fc)) 727 return func(v )746 return func(v, client_encoding) 728 747 pg_value = staticmethod(pg_value) 729 748 … … 755 774 return int(data) 756 775 757 def int4out(v):758 return str(v)759 760 def int4send(v):761 return struct.pack("!i", v)762 763 776 def timestamp_recv(data, description): 764 777 val = struct.unpack("!d", data)[0] … … 780 793 return decimal(data) 781 794 782 def numeric_out(v ):795 def numeric_out(v, ce): 783 796 return str(v) 784 797 … … 786 799 return unicode(data, "utf-8") 787 800 788 def varcharout(v ):789 return v.encode( "utf-8")801 def varcharout(v, ce): 802 return v.encode(ce) 790 803 791 804 py_types = {
