► スポイラーを表示
configs['type'])) {
// MySQLの場合
$this->dsn = 'mysql:';
if (! empty($this->configs['server'])) {
$this->dsn .= 'host=' . $this->checkVar($this->configs['server']);
}
if (! empty($this->configs['port'])) {
$this->dsn .= 'port=' . $this->checkVar($this->configs['port']);
}
if (! empty($this->configs['dbname'])) {
$this->dsn .= 'dbname=' . $this->checkVar($this->configs['dbname']);
}
if (! empty($this->configs['charset'])) {
$this->dsn .= 'charset=' . $this->checkVar($this->configs['charset']);
}
if (! empty($this->configs['socket'])) {
$this->dsn .= 'unix_socket=' . $this->checkVar($this->configs['socket']);
}
} elseif (preg_match('/^(PostgreSQL)+$/', $this->configs['type'])) {
// PostgreSQLの場合
$this->dsn = 'pgsql:';
if (! empty($this->configs['server'])) {
$this->dsn .= 'host=' . $this->checkVar($this->configs['server']);
}
if (! empty($this->configs['port'])) {
$this->dsn .= 'port=' . $this->checkVar($this->configs['port']);
}
if (! empty($this->configs['dbname'])) {
$this->dsn .= 'dbname=' . $this->checkVar($this->configs['dbname']);
}
if (! empty($this->configs['username'])) {
$this->dsn .= 'user=' . $this->checkVar($this->configs['username']);
}
if (! empty($this->configs['password'])) {
$this->dsn .= 'password=' . $this->checkVar($this->configs['password']);
}
} elseif (preg_match('/^(CUBRID)+$/', $this->configs['type'])) {
$this->dsn = 'cubrid:';
if (! empty($this->configs['server'])) {
$this->dsn .= 'host=' . $this->checkVar($this->configs['server']);
}
if (! empty($this->configs['port'])) {
$this->dsn .= 'port=' . $this->checkVar($this->configs['port']);
}
if (! empty($this->configs['dbname'])) {
$this->dsn .= 'dbname=' . $this->checkVar($this->configs['dbname']);
}
} elseif (preg_match('/^(SQLite 3)+$/', $this->configs['type'])) {
// SQLite 3の場合
$this->dsn = 'sqlite:';
if (! empty($this->configs['dbname'])) {
$this->dsn .= $this->checkVar($this->configs['dbname']);
}
} elseif (preg_match('/^(SQLite 2)+$/', $this->configs['type'])) {
// SQLite 2の場合
$this->dsn = 'sqlite2:';
if (! empty($this->configs['dbname'])) {
$this->dsn .= $this->checkVar($this->configs['dbname']);
}
} elseif (preg_match('/^(Oracle)+$/', $this->configs['type'])) {
// Oracleデータベースの場合
$this->dsn = 'oci:';
if (! empty($this->configs['dbname'])) {
$this->dsn .= 'dbname=' . $this->checkVar($this->configs['dbname']);
}
if (! empty($this->configs['charset'])) {
$this->dsn .= 'charset=' . $this->checkVar($this->configs['charset']);
}
}
return preg_replace('/;$/', '', $this->dsn);
}
/**
* データベースユーザー名を取得
* @return string
*/
public function buildUserName()
{
return (string)$this->configs['username'];
}
/**
* 変数にセットされているかチェック
* (buildDsnメソッド内で使用)
* @param $var
* @return string
*/
private function checkVar($var)
{
// 渡されたものが nullもしくは空っぽの場合
if (is_null($var) || empty($var)) {
return $var . '';
} elseif (isset($var)) {
return $var . ';';
}
return $var;
}
/**
* Unixソケットを設定する
* @param null $socket
* @return $this
*/
public function databaseSocket($socket = null)
{
if (! empty($socket)) {
$this->configs['socket'] = $socket;
}
return $this;
}
/**
* 文字コードを設定する
* @param null $charset
* @return $this
*/
public function databaseCharset($charset = null)
{
if (! empty($charset)) {
$this->configs['charset'] = $charset;
}
return $this;
}
/**
* データベース名を設定する
* @param null $dbname
* @return $this
*/
public function databaseName($dbname = null)
{
if (! empty($dbname)) {
$this->configs['dbname'] = $dbname;
}
return $this;
}
/**
* データベースのパスワードを設定する
* @param null $password
* @return $this
*/
public function databasePassword($password = null)
{
if (! empty($password)) {
$this->configs['password'] = $password;
}
return $this;
}
/**
* データベースユーザー名を設定する
* @param null $username
* @return $this
*/
public function databaseUserName($username = null)
{
if (! empty($username)) {
$this->configs['username'] = $username;
}
return $this;
}
/**
* データベースのポートを設定する
* @param null $port
* @return $this
*/
public function databasePort($port = null)
{
if (! empty($port)) {
$this->configs['port'] = $port;
}
return $this;
}
/**
* データベースサーバーを設定する
* @param null $server
* @return $this
*/
public function databaseServer($server = null)
{
if (! empty($server)) {
$this->configs['server'] = $server;
}
return $this;
}
/**
* データベースの種類を設定する
* @param null $type
* @return $this
*/
public function databaseType($type = null)
{
if (! empty($type)) {
$this->configs['type'] = $type;
}
return $this;
}
/**
* メソッドチェーンを作成する
* @return Config
*/
public static function create()
{
return new self;
}
}