mikecao / sparrow

A simple database toolkit for PHP
MIT License
289 stars 68 forks source link

Fix charset #27

Open kamov opened 7 years ago

kamov commented 7 years ago

Hi, can you please explain how to fix charset for different case? For example I am using mysql and sqlite, and also pdo in some project. I try to check what other suggest, but without success. Maybe you can make a method for handle this? THanks!!

kamov commented 7 years ago

strange, I have posted here more reply.. seem disappear..

kamov commented 7 years ago

I will write again my previous posts, there was many more info, so I will make this time short.

I would like to fix charset for all type of database engine so my application can be database agnostic.

I have change the setDb() in this way below for mysql, mysqli and pdomysql. But I am not sure how to do for others apart run query SET NAMES UTF8.


switch ($db['type']) {
                case 'mysqli':
                    $this->db = new mysqli(
                        $db['hostname'],
                        $db['username'],
                        $db['password'],
                        $db['database']
                    );

                    if ($this->db->connect_error) {
                        throw new Exception('Connection error: '.$this->db->connect_error);
                    }

                    /* Change character set */
                    if (! $this->db->set_charset($db['charset'])) {
                        throw new Exception("Error loading character set utf8: %s\n", $this->db->error);
                    }

                    break;

                case 'mysql':
                    $this->db = mysql_connect(
                        $db['hostname'],
                        $db['username'],
                        $db['password']
                    );

                    if (!$this->db) {
                        throw new Exception('Connection error: '.mysql_error());
                    }

                    /* Change character set */
                    mysql_set_charset($db['charset'], $this->db);

                    mysql_select_db($db['database'], $this->db);

                    break;

                case 'pgsql':
                    $str = sprintf(
                        'host=%s dbname=%s user=%s password=%s',
                        $db['hostname'],
                        $db['database'],
                        $db['username'],
                        $db['password']
                    );

                    $this->db = pg_connect($str);

                    // @TODO $this->db->("SET NAMES {$db['charset']}");

                    break;

                case 'sqlite':
                    $this->db = sqlite_open($db['database'], 0666, $error);

                    if (!$this->db) {
                        throw new Exception('Connection error: '.$error);
                    }

                    // @TODO $this->db->("SET NAMES {$db['charset']}");

                    break;

                case 'sqlite3':
                    $this->db = new SQLite3($db['database']);

                    break;

                case 'pdomysql':
                    $dsn = sprintf(
                        'mysql:host=%s;port=%d;dbname=%s;charset=%s',
                        $db['hostname'],
                        isset($db['port']) ? $db['port'] : 3306,
                        $db['database'],
                        $db['charset']
                    );

                    $this->db = new PDO($dsn, $db['username'], $db['password']);
                    $db['type'] = 'pdo';

                    // @TODO $this->db->("SET NAMES {$db['charset']}");

                    break;

                case 'pdopgsql':
                    $dsn = sprintf(
                        'pgsql:host=%s;port=%d;dbname=%s;user=%s;password=%s',
                        $db['hostname'],
                        isset($db['port']) ? $db['port'] : 5432,
                        $db['database'],
                        $db['username'],
                        $db['password']
                    );

                    $this->db = new PDO($dsn);
                    $db['type'] = 'pdo';

                    // @TODO $this->db->("SET NAMES {$db['charset']}");

                    break;

                case 'pdosqlite':
                    $this->db = new PDO('sqlite:/'.$db['database']);
                    $db['type'] = 'pdo';

                    // @TODO $this->db->("SET NAMES {$db['charset']}");

                    break;
            }

I hope you can integrate this in the library, so we can just run $db->setDb(array with all info charset included)

Thanks!

mikecao commented 7 years ago

I think it should be possible for the databases that support charset. I'll work on an enhancement.