Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Clase creada con la intencion de realizar pruebas de conexion
- * a base de datos Postgres
- *
- * User: Albert Eduardo Hidalgo Taveras
- * Date: 16/1/2019
- * Time: 11:16 AM
- */
- namespace Core\Util\PgConecttionTest;
- class PgConectionTest
- {
- private $userName;
- private $password;
- private $host;
- private $dbName;
- private $schema;
- private $dbConection;
- /**
- * @return mixed
- */
- public function getDbConection()
- {
- return $this->dbConection;
- }
- /**
- * @param mixed $dbConection
- */
- public function setDbConection($dbConection): void
- {
- $this->dbConection = $dbConection;
- }
- public function __construct($userName,$password,$host,$dbName,$schema)
- {
- $this->userName = $userName;
- $this->password = $password;
- $this->host = $host;
- $this->dbName = $dbName;
- $this->schema = $schema;
- }
- /**
- * Verifica que las credenciales de conexion sean validas
- * @return string
- */
- public function validDataBaseCredentials(){
- $this->setDbConection(
- pg_connect ("host=$this->host dbname=$this->dbName user=$this->userName password=$this->password")
- );
- if($this->getDbConection()) {
- return 'OK';
- } else {
- return 'ERROR';
- }
- }
- /**
- * Consulta el objeto pg_database para saber si
- * la base de datos existe
- * @return string
- */
- public function checkIsDBExists(){
- $result = pg_query_params($this->dbConection,
- 'SELECT exists(SELECT 1 FROM pg_database WHERE datname = $1)', array($this->dbName));
- $result = pg_fetch_assoc($result);
- if($result['exists'] == 't'){
- return 'OK';
- }else{
- return 'NO EXITS';
- }
- }
- /**
- * Verifica que un esquema exista
- * @return string
- */
- public function checkIsSchemaExists(){
- $result = pg_query_params($this->dbConection,
- 'SELECT exists(select schema_name FROM information_schema.schemata WHERE schema_name = $1);', array($this->schema));
- $result = pg_fetch_assoc($result);
- if($result['exists'] == 't'){
- return 'OK';
- }else{
- return 'NO EXITS';
- }
- }
- }
Add Comment
Please, Sign In to add comment