FOSSology  4.4.0
Open Source License Compliance by Open Source Software
db.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
21 require_once (dirname(__FILE__) . '/../TestEnvironment.php');
22 require_once (dirname(__FILE__) . '/../../ui/common/common-ui.php');
23 
24 global $URL;
25 global $USER;
26 global $PASSWORD;
27 
28 class db {
29  private $_pg_conn;
30  private $pg_rows;
31  protected $pg_Error;
32  private $dbName;
33  private $dbUser;
34  private $dbPassword;
35  private $dbHost;
36  public $Debug;
37 
38  function __construct($options = NULL) {
39  global $URL;
40  global $USER;
41  global $PASSWORD;
42 
43  if (is_null($options)) {
44  //$this->dbHost = parse_url($URL, PHP_URL_HOST);
45  $this->dbHost = 'localhost';
46  //$this->dbName = $DBNAME;
47  $this->dbUser = $USER;
48  $this->dbPassword = $PASSWORD;
49 
50  $this->_docon();
51  }
52  else {
53  $this->_docon($options);
54  }
55  if(is_resource($this->_pg_conn)) {
56  $this->pg_Error = 0;
57  return (TRUE);
58  }
59  else {
60  $this->pg_ERROR = 1;
61  return (FALSE);
62  }
63  } // __construct
64 
65  public function get_pg_ERROR() {
66  return($this->pg_ERROR);
67  }
68 
79  public function connect($options = NULL) {
80  if (is_resource($this->_pg_conn)) {
81  return ($this->_pg_conn);
82  }
83  else {
84  $this->_docon($options);
85  return ($this->_pg_conn);
86  }
87  } // connect
88 
99  private function _docon($options = NULL) {
100  // fix the hardcode below, enhance create test env...
101  $dbname = 'fossology';
102 
103  if (is_null($options)) {
104  $this->_pg_conn = pg_pconnect("host=$this->dbHost dbname=$dbname " .
105  "user=$this->dbUser password=$this->dbPassword");
106  }
107  else {
108  $this->_pg_conn = pg_pconnect(str_replace(";", " ", $options));
109  }
110  $res = pg_last_error($this->_pg_conn);
111 
112  if(is_null($this->_pg_conn)) {
113  $this->pg_Error = TRUE;
114  print "DB: could not connect to the db, connection is NULL\n";
115  return(FALSE);
116  }
117 
118  if($this->_pg_conn === FALSE) {
119  $this->pg_Error = TRUE;
120  print "DB: could not connect to the db, connect is FALSE\n";
121  return(FALSE);
122  }
123  if (!isset ($this->_pg_conn)) {
124  $this->pg_Error = 1;
125  return (0);
126  }
127  $this->pg_Error = 0;
128  return (1);
129  }
139  public function dbQuery($Sql) {
140  /*
141  * sql query's can return False on error or NULL (no error, no
142  * results)
143  *
144  * This code is pretty much copied from Action in db-core. Look for
145  * areas where better error checking can be done (e.g. where the @
146  * is used)
147  */
148  $uid = posix_getuid();
149  $uidInfo = posix_getpwuid($uid);
150  $this->pg_rows = array ();
151  if (!$this->_pg_conn) {
152  return ($this->pg_rows); // think about this, is false better?
153  }
154  if (empty ($Sql)) {
155  return ($this->pg_rows); // same as above
156  }
157 
158  @ $result = pg_query($this->_pg_conn, $Sql);
159  DBCheckResult($result, $Sql, __FILE__, __LINE__);
160 
161  $this->Error = 0;
162  $this->pg_rows = pg_affected_rows($result);
163 
164  /* if the query returned nothing then just return*/
165  if (!isset ($result)) {
166  print "DB-Query: result not set!\n";
167  return;
168  }
169  @ $rows = pg_fetch_all($result);
170 
171  if (!is_array($rows)) {
172  $rows = array ();
173  }
174  //print "DB-QU: rows is\n"; print_r($rows) . "\n";
175  @ pg_free_result($result);
176  return $rows;
177  } // dbQuery
178 } // class db
Definition: db.php:28
connect($options=NULL)
Definition: db.php:79
_docon($options=NULL)
Definition: db.php:99
dbQuery($Sql)
Definition: db.php:139
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187