Kirill Simonov avatar Kirill Simonov committed 301aec4

A patch for the case when the encoding of a postgresql database is set to
SQL_ASCII while the data is actually LATIN1-encoded.

Comments (0)

Files changed (3)

src/htsql/core/cmd/retrieve.py

                 connect = Connect()
                 connection = connect()
                 cursor = connection.cursor()
-                cursor.execute(plan.sql.encode('utf-8'))
+                cursor.execute(plan.sql.encode('latin1'))
                 records = []
                 for row in cursor:
                     values = []

src/htsql/core/tr/parse.py

         # A string literal.
         elif tokens.peek(StringToken):
             token = tokens.pop(StringToken)
+            try:
+                token.value.encode('latin1')
+            except UnicodeEncodeError:
+                raise ParseError("invalid literal encoding", token.mark)
             return StringSyntax(token.value, token.mark)
         # A numeric literal.
         elif tokens.peek(NumberToken):

src/htsql_pgsql/core/connect.py

             parameters['password'] = db.password
         connection = psycopg2.connect(**parameters)
 
-        # All queries are UTF-8 encoded strings regardless of the database
-        # encoding.
-        connection.set_client_encoding('UTF8')
+        connection.set_client_encoding('LATIN1')
 
         # Make TEXT values return as `unicode` objects.
         psycopg2.extensions.register_type(psycopg2.extensions.UNICODE,
     @staticmethod
     def convert(value):
         if isinstance(value, str):
-            value = value.decode('utf-8')
+            value = value.decode('latin1')
         return value
 
 
     @staticmethod
     def convert(value):
         if isinstance(value, str):
-            value = value.decode('utf-8')
+            value = value.decode('latin1')
         return value
 
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.