Guest User

Untitled

a guest
Jan 16th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. <?php
  2. /**
  3. * Clase creada con la intencion de realizar pruebas de conexion
  4. * a base de datos Postgres
  5. *
  6. * User: Albert Eduardo Hidalgo Taveras
  7. * Date: 16/1/2019
  8. * Time: 11:16 AM
  9. */
  10.  
  11. namespace Core\Util\PgConecttionTest;
  12.  
  13.  
  14. class PgConectionTest
  15. {
  16. private $userName;
  17. private $password;
  18. private $host;
  19. private $dbName;
  20. private $schema;
  21. private $dbConection;
  22.  
  23. /**
  24. * @return mixed
  25. */
  26. public function getDbConection()
  27. {
  28. return $this->dbConection;
  29. }
  30.  
  31. /**
  32. * @param mixed $dbConection
  33. */
  34. public function setDbConection($dbConection): void
  35. {
  36. $this->dbConection = $dbConection;
  37. }
  38.  
  39. public function __construct($userName,$password,$host,$dbName,$schema)
  40. {
  41. $this->userName = $userName;
  42. $this->password = $password;
  43. $this->host = $host;
  44. $this->dbName = $dbName;
  45. $this->schema = $schema;
  46. }
  47.  
  48. /**
  49. * Verifica que las credenciales de conexion sean validas
  50. * @return string
  51. */
  52. public function validDataBaseCredentials(){
  53. $this->setDbConection(
  54. pg_connect ("host=$this->host dbname=$this->dbName user=$this->userName password=$this->password")
  55. );
  56. if($this->getDbConection()) {
  57. return 'OK';
  58. } else {
  59. return 'ERROR';
  60. }
  61. }
  62.  
  63. /**
  64. * Consulta el objeto pg_database para saber si
  65. * la base de datos existe
  66. * @return string
  67. */
  68. public function checkIsDBExists(){
  69. $result = pg_query_params($this->dbConection,
  70. 'SELECT exists(SELECT 1 FROM pg_database WHERE datname = $1)', array($this->dbName));
  71.  
  72. $result = pg_fetch_assoc($result);
  73. if($result['exists'] == 't'){
  74. return 'OK';
  75. }else{
  76. return 'NO EXITS';
  77. }
  78. }
  79.  
  80. /**
  81. * Verifica que un esquema exista
  82. * @return string
  83. */
  84. public function checkIsSchemaExists(){
  85. $result = pg_query_params($this->dbConection,
  86. 'SELECT exists(select schema_name FROM information_schema.schemata WHERE schema_name = $1);', array($this->schema));
  87.  
  88. $result = pg_fetch_assoc($result);
  89. if($result['exists'] == 't'){
  90. return 'OK';
  91. }else{
  92. return 'NO EXITS';
  93. }
  94. }
  95. }
Add Comment
Please, Sign In to add comment