phpvms/app/Console/Services/Database.php

51 lines
1016 B
PHP
Raw Normal View History

<?php
namespace App\Console\Services;
use Doctrine\DBAL\Driver\PDOException;
2018-02-21 12:33:09 +08:00
use PDO;
/**
* Class Database
* @package App\Console\Services
*/
class Database
{
/**
* Create the base connection DSN, optionally include the DB name
* @param $host
* @param $port
* @param null $name
* @return string
*/
public function createDsn($host, $port, $name=null)
{
$conn = config('database.default');
$dsn = "$conn:host=$host;port=$port";
if(filled($name)) {
$dsn .= ';dbname='.$name;
}
return $dsn;
}
/**
* @param $dsn
* @param $user
* @param $pass
* @return PDO
* @throws \PDOException
*/
public function createPDO($dsn, $user, $pass)
{
try {
$conn = new PDO($dsn, $user, $pass);
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
} catch (\PDOException $e) {
throw $e;
}
return $conn;
}
}