/** * Cria instância de objeto PDO * @param AtributosConexao $configuracoes * @return PDO */ protected function criarConexao(AtributosConexao $configuracoes) { try { $conexaoPDO = new \PDO($configuracoes->getDsn(), $configuracoes->getUser(), $configuracoes->getPassword()); $conexaoPDO->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); } catch (PDOException $exc) { throw new InfraEstruturaException($exc->getMessage(), $exc->getCode(), $exc->getPrevious()); } catch (\Exception $exc) { throw new InfraEstruturaException($exc->getMessage(), $exc->getCode(), $exc->getPrevious()); } return $conexaoPDO; }
/** * Fábrica que cria a conexão com o banco de dados. * * @param AtributosConexao $configuracoes * @return EntityManager Conexão de acesso ao banco de dados * @throws Exception */ public function criarConexao(AtributosConexao $configuracoes) { $config = Setup::createConfiguration(FALSE); $config->setAutoGenerateProxyClasses(TRUE); $config->setProxyDir(CAMINHO_APLICACAO_DOMINIO . DIRECTORY_SEPARATOR . "tmp" . DIRECTORY_SEPARATOR); $driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver(new \Doctrine\Common\Annotations\AnnotationReader(), array($configuracoes->getPath_model())); $config->setMetadataDriverImpl($driver); AnnotationRegistry::registerFile(CAMINHO_APLICACAO_DOMINIO . '/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'); $arrayConfigConnection = array('dbname' => $configuracoes->getDbname(), 'user' => $configuracoes->getUser(), 'password' => $configuracoes->getPassword(), 'host' => $configuracoes->getHost(), 'driver' => $configuracoes->getDbdriver(), 'charset' => $configuracoes->getCharset()); try { $entityManager = EntityManager::create($arrayConfigConnection, $config); $entityManager->getConnection()->connect(); return $entityManager; } catch (\Doctrine\ORM\ORMException $exc) { throw new InfraEstruturaException("Erro ao abrir conexão com banco de dados.", $exc->getCode()); } catch (\InvalidArgumentException $exc) { throw new InfraEstruturaException("Erro ao abrir conexão com banco de dados.", $exc->getCode()); } catch (\Exception $exc) { throw new InfraEstruturaException("Erro ao abrir conexão com banco de dados.", $exc->getCode()); } }
/** * Retorna uma string de conexao (DSN) * @param string $dbDriver Driver do BD * @return DSN string de conexao */ public function gerarDsn($dbDriver) { switch ($dbDriver) { case DBDriver::PDO_Mysql: return "mysql:host=" . parent::getHost() . ";port=" . $this->port . ";dbname=" . parent::getDbname() . ';charset=' . parent::getCharset(); break; case DBDriver::PDO_PGsql: break; default: return "mysql:host=" . parent::getHost() . ";port=" . $this->port . ";dbname=" . parent::getDbname() . ';charset=' . parent::getCharset(); break; } }