PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/site/onyx2/modules/db/postgresql.class.php

http://xe8tmw7c.googlecode.com/
PHP | 168 lines | 116 code | 52 blank | 0 comment | 31 complexity | d89ab9eef292d888a09b6fb2b76a05f8 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. class BDD
  3. {
  4. var $connected;
  5. private $session;
  6. private $reponse;
  7. var $host;
  8. var $user;
  9. private $password;
  10. var $database;
  11. var $num_rows;
  12. var $nodb;
  13. function __construct($profile=NULL)
  14. {
  15. if($profile === FALSE) return FALSE;
  16. global $db_config;
  17. if(empty($profile))
  18. {
  19. if(!$db_config['profile']) return FALSE;
  20. $profile = &$db_config['profile'];
  21. }
  22. if(!ctype_alnum($profile)) trigger_error('Le nom du profil contient des caracteres illegaux',E_USER_ERROR);
  23. if($db_config['profile'])
  24. {
  25. require(ONYX.'db/'.$profile.'.profile.php');
  26. $db = &$___profile['db'];
  27. $host = &$___profile['host'];
  28. $user = &$___profile['user'];
  29. $pass = &$___profile['pass'];
  30. }
  31. if($db_config['crypt']) $pass = dbpass($pass,$db_config['crypt']);
  32. return $this->connexion($host,$user,$pass,$db);
  33. }
  34. function connexion($host,$user,$pass,$db=NULL)
  35. {
  36. if($this->session) $this->deconnexion();
  37. $this->reponse = NULL;
  38. $host = pg_escape_string($host);
  39. $user = pg_escape_string($user);
  40. $pass = pg_escape_string($pass);
  41. $db = pg_escape_string($db);
  42. $this->session = pg_connect("host='$host' port=5432 dbname='$db' user='$user' password='$pass'");
  43. if(!$this->session)
  44. {
  45. elog('Connexion impossible a la base de donnee : '.$this->erreur(),2);
  46. if(function_exists($this->nodb)) call_user_func($this->nodb);
  47. return FALSE;
  48. }
  49. pg_setclientencoding($this->session,'UTF8');
  50. $this->host = $host;
  51. $this->user = $user;
  52. $this->password = $pass;
  53. $this->database = $db;
  54. $this->connected = TRUE;
  55. }
  56. function reconnexion()
  57. {
  58. if(!empty($this->host) && !empty($this->user) && !empty($this->password) && !empty($this->database)) $this->connexion($this->host,$this->user,$this->password,$this->database);
  59. }
  60. function deconnexion()
  61. {
  62. if(!$this->session) return FALSE;
  63. $r = pg_close($this->session);
  64. $this->session = FALSE;
  65. $this->connected = FALSE;
  66. return $r;
  67. }
  68. function erreur()
  69. {
  70. if(!$this->session) return FALSE;
  71. return pg_last_error($this->session);
  72. }
  73. function db($db)
  74. {
  75. if(!$this->session) return FALSE;
  76. return $this->database = pg_query($this->session,"\\connect ".pg_escape_string($db)) ? $db : $this->database;
  77. }
  78. function escape(&$var)
  79. {
  80. if(!$this->session) return FALSE;
  81. $var = pg_escape_string($this->session,$var);
  82. return $var;
  83. }
  84. function query($query)
  85. {
  86. if(!$this->session) return FALSE;
  87. $this->reponse = pg_query($this->session,$query);
  88. global $db_config;
  89. if(!$this->reponse && $db_config['log']) elog('Erreur PostgreSQL: " '.$this->erreur().' ", avec la requ?系e: { '.$query.' }.',1);
  90. $this->num_rows = pg_num_rows($this->reponse);
  91. if($this->num_rows == 0) return NULL;
  92. elseif($this->num_rows >= 1)
  93. {
  94. for($i=0; $var = pg_fetch_assoc($this->reponse); $i++) $sortie[$i] = $var;
  95. return $sortie;
  96. }
  97. else return FALSE;
  98. }
  99. function unique_query($query)
  100. {
  101. if(!$this->session) return FALSE;
  102. $this->reponse = pg_query($this->session,$query);
  103. global $db_config;
  104. if(!$this->reponse && $db_config['log']) elog('Erreur PostgreSQL: " '.$this->erreur().' ", avec la requ?系e: { '.$query.' }.',1);
  105. $this->num_rows = pg_num_rows($this->reponse);
  106. if($this->num_rows == 0) return NULL;
  107. elseif($this->num_rows >= 1) return pg_fetch_assoc($this->reponse);
  108. else return FALSE;
  109. }
  110. function affected()
  111. {
  112. if(!$this->session) return FALSE;
  113. return pg_affected_rows($this->reponse);
  114. }
  115. }
  116. ?>