Created
January 16, 2019 16:04
-
-
Save itsalb3rt/ab7b1964538f4e1f7a69d3b072585bf7 to your computer and use it in GitHub Desktop.
Clase test para probar la conexion a base de datos postgres
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment