nikita-volkov / hasql

The fastest PostgreSQL libpq-based driver for Haskell
http://hackage.haskell.org/package/hasql
MIT License
519 stars 55 forks source link

ConnectionError & QueryError is not readable under unicode environment #150

Closed kalxd closed 2 years ago

kalxd commented 2 years ago

Hi All. I am using the lastest version hasql == 1.6.1.3, and trying to connect to a postgresql server in the local netowork.

{-# LANGUAGE OverloadedStrings #-}
module Main where

import Hasql.Connection (acquire, release)

main :: IO ()
main = do
    conn <- acquire "postgresql://username@192.168.1.10:5431/unknowndb"
    case conn of
        Right conn' -> release conn'
        Left e -> print e

where unknowndb is not exists. When running the code, it prints:

Just "\232\135\180\229\145\189\233\148\153\232\175\175:  \230\149\176\230\141\174\229\186\147 \"unknowndb\" \228\184\141\229\173\152\229\156\168\n"

so, is where a way to convert the ByteString to readable Text? thanks a lot.

nikita-volkov commented 2 years ago

You can use Data.Text.decodeUtf8'.

kalxd commented 2 years ago

You can use Data.Text.decodeUtf8'.

thanks for your reply. decodeUtf8' does not work either, it also prints:

{-# LANGUAGE OverloadedStrings #-}
module Main where

import Hasql.Connection (acquire, release)
import Data.Text.Encoding (decodeUtf8')

main :: IO ()
main = do
    conn <- acquire "postgresql://null@192.168.1.10:5431/sl"
    case conn of
        Right conn' -> release conn'
        Left e -> print $ decodeUtf8' <$> e

--- prints:
--- Just (Right "\33268\21629\38169\35823:  \25968\25454\24211 \"sl\" \19981\23384\22312\n")

psql could print chinese characters correctly:

$ psql -U null -h 192.168.1.10 -p 5431 -d sl
psql: 错误: 致命错误:  数据库 "sl" 不存在

it also says database "sl" not existed, my $LANG is zh_CN.UTF-8.

Now I have no idea how to solve it, could you give me some suggests?

kalxd commented 2 years ago

also the query SQL, Haskell not prints very well.

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
module Main where

import Hasql.Connection (acquire, release)
import qualified Hasql.TH as TH
import Data.Text.Encoding (decodeUtf8')
import Hasql.Statement (Statement)
import Hasql.Session (run, statement)
import Data.Int (Int64)
import Data.Text (Text)

stmt :: Statement () (Int64, Text)
stmt = [TH.singletonStatement| select 1 :: int8, '美林' :: text |]

main :: IO ()
main = do
    conn <- acquire "postgresql://null@192.168.1.10:5431"
    case conn of
        Right conn' -> do
            result <- run (statement undefined stmt) conn'
            release conn'
            print result
        Left e -> print $ decodeUtf8' <$> e
--- prints
--- Right (1,"\32654\26519")

How can I solve it? thanks a lot.

nikita-volkov commented 2 years ago

Use Data.Text.IO.putStrLn instead of print

kalxd commented 2 years ago

Use Data.Text.IO.putStrLn instead of print

Great, it works, thanks a lot !!