0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CentOSから名前付きインスタンスでdatabase名が日本語のSqlServerへ接続する

Last updated at Posted at 2018-01-06

かなり特殊な状況ではありますが...

環境

  • CentOS release 6.5 (Final)
  • PHP 5.6.31 (cli) (built: Jul 6 2017 08:16:47)
  • Laravel Framework version 5.3.31

freetdsのインストール

$ cat /etc/freetds.conf
.
.
.
[dbname]
host = 123.123.123.123
instance = INSTANCE_NAME

phpドライバのインストール

  • phpのインストールは他ドキュメント参照
  • ここではremiリポジトリを参照する
$ sudo yum install --enablerepo=remi-php56 php-mssql

laravelのconfig/database.php

.
.
        'SqlServer' => [
            'driver'   => 'sqlsrv',
            'host'     => 'dbname',
            'database' => '',
            'username' => 'username',
            'password' => 'password',
            'charset'  => 'utf8',
            'prefix'   => '',
        ],
  • hostにはfreetdsで指定したものを指定
  • databaseには空文字を指定
    • config/database.phpで日本語データベースを指定すると、文字コードの違いでうまくよみとってくれなかった

汎用ライブラリの用意

  • 自分の周辺ではSqlServerの文字コードがShift-JISであることが多く、文字コード周りを処理するために汎用ライブラリを作成
  • Eloquent Modelのみで実装することも可能だと思われる
<?php

namespace App\Service;

use DB;

class SqlServer
{
    protected static $connection = 'SqlServer';

    /**
     * execute select query. return results
     *
     * @param string $sql
     * @return array
     */
    public static function select($sql)
    {
        // クライアント、サーバの文字コードの設定次第では
        // クエリ送信前にも文字コード変換が必要な場合がある
        // static::u2s($sql);

        $rows = DB::connection(static::$connection)->select($sql);
        static::s2u($rows);

        return $rows;
    }

    /**
     * encoding utf8 to sjis
     *
     * @param string
     */
    public static function u2s(&$str)
    {
        static::encoding($str, 'sjis', 'utf8');
    }

    /**
     * encoding sjis to utf8
     *
     * @param string
     */
    public static function s2u(&$str)
    {
        static::encoding($str, 'utf8', 'sjis');
    }

    /**
     * encoding character or object
     *
     * @param mix string/array/object
     * @param string to_character
     * @param string from_character
     */
    public static function encoding(&$str, $to, $from)
    {
        if (is_object($str)) {
            $str = (array)$str;
        }

        if (is_array($str)) {
            array_walk_recursive($str, function(&$v) use ($to, $from) {
                static::encoding($v, $to, $from);
            });
        }
        else {
            if (! is_null($str)) {
                $str = mb_convert_encoding($str, $to, $from);
            }
        }
    }
}

クエリ実行

use App\Service\SqlServer;

$sql    = 'select * from [データベース].dbo.table';
$result = SqlServer::select($sql);
  • 日本語データベースはクエリ内で直接指定する。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?