bigpresh / Dancer-Plugin-Database

Dancer::Plugin::Database - easy database support for Dancer applications
http://search.cpan.org/dist/Dancer-Plugin-Database
37 stars 36 forks source link

MySQL additional DB utf-8 issue #58

Closed sime-k closed 9 years ago

sime-k commented 9 years ago

Utf-8 works with default database but when i use additional one it returns.

Example: config.yml

charset: "utf-8"

plugins:
    Database:
      driver: 'mysql'
      database: 'data'
      host: 'localhost'
      username: 'user'
      password: 'pass'
      connections:
        stats:
              driver: 'mysql'
              database: 'data'
              host: 'localhost'
              username: 'user'
              password: 'pass'    

data: {NAME=>ščćđ}

Dancer

    my $test = database->quick_select('RECORDINGS', {ID=>'174309663'});
    return to_json $test;
        # returns {NAME=>ščćđ}

    my $test = database('stats')->quick_select('RECORDINGS', {ID=>'174309663'});
    return to_json $test;
        # returns {NAME=>????}
gregapompe commented 9 years ago

For some reason named connections don't get charset from settings. So an elegant workaround is to add charset: utf-8 to connection. In your case:

charset: "utf-8"

plugins:
    Database:
      driver: 'mysql'
      database: 'data'
      host: 'localhost'
      username: 'user'
      password: 'pass'
      connections:
        stats:
              driver: 'mysql'
              database: 'data'
              host: 'localhost'
              username: 'user'
              password: 'pass' 
              charset: "utf-8"
sime-k commented 9 years ago

Thanks! :+1:

bigpresh commented 9 years ago

Hmm - whilst you can, indeed, pass the charset parameter in each named connection, it doesn't seem reasonable behaviour for it to be automatically enabled for the "main" connection, but not for named connections - that's bound to surprise others - so I'm going to re-open this as still being a bug that ought to be fixed.

ambs commented 9 years ago

Indeed. I tested locally and I did not have any problem. So, I would really like to see this reproduced so we can fix the issue.

esyphelon commented 9 years ago

Oops: Ignore this, I did not set up the table properly.

As another interesting note: This is what I get from the mysql command-line client when I query a table with the example string.

Note: I inserted the string twice, once with the client set to UTF8 and once without.

mysql> insert into test (name) values ('ščćđ') mysql> select * from test; +-------+ | name | +-------+ | š??? | | š??? | +-------+

esyphelon commented 9 years ago

Just FYI. Submitted PR #59 which handles charset inheritance of named connections.

ambs commented 9 years ago

I think this is solved, so closing issue. Please let me know if not. Best,