FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
db.php
1 <?php
2 
3 
4 /***********************************************************
5  Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License
9  version 2 as published by the Free Software Foundation.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License along
17  with this program; if not, write to the Free Software Foundation, Inc.,
18  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  ***********************************************************/
20 
34 require_once (dirname(__FILE__) . '/../TestEnvironment.php');
35 require_once (dirname(__FILE__) . '/../../ui/common/common-ui.php');
36 
37 global $URL;
38 global $USER;
39 global $PASSWORD;
40 
41 class db {
42  private $_pg_conn;
43  private $pg_rows;
44  protected $pg_Error;
45  private $dbName;
46  private $dbUser;
47  private $dbPassword;
48  private $dbHost;
49  public $Debug;
50 
51  function __construct($options = NULL) {
52  global $URL;
53  global $USER;
54  global $PASSWORD;
55 
56  if (is_null($options)) {
57  //$this->dbHost = parse_url($URL, PHP_URL_HOST);
58  $this->dbHost = 'localhost';
59  //$this->dbName = $DBNAME;
60  $this->dbUser = $USER;
61  $this->dbPassword = $PASSWORD;
62 
63  $this->_docon();
64  }
65  else {
66  $this->_docon($options);
67  }
68  if(is_resource($this->_pg_conn)) {
69  $this->pg_Error = 0;
70  return (TRUE);
71  }
72  else {
73  $this->pg_ERROR = 1;
74  return (FALSE);
75  }
76  } // __construct
77 
78  public function get_pg_ERROR() {
79  return($this->pg_ERROR);
80  }
81 
92  public function connect($options = NULL) {
93  if (is_resource($this->_pg_conn)) {
94  return ($this->_pg_conn);
95  }
96  else {
97  $this->_docon($options);
98  return ($this->_pg_conn);
99  }
100  } // connect
101 
112  private function _docon($options = NULL) {
113  // fix the hardcode below, enhance create test env...
114  $dbname = 'fossology';
115 
116  if (is_null($options)) {
117  $this->_pg_conn = pg_pconnect("host=$this->dbHost dbname=$dbname " .
118  "user=$this->dbUser password=$this->dbPassword");
119  }
120  else {
121  $this->_pg_conn = pg_pconnect(str_replace(";", " ", $options));
122  }
123  $res = pg_last_error($this->_pg_conn);
124 
125  if(is_null($this->_pg_conn)) {
126  $this->pg_Error = TRUE;
127  print "DB: could not connect to the db, connection is NULL\n";
128  return(FALSE);
129  }
130 
131  if($this->_pg_conn === FALSE) {
132  $this->pg_Error = TRUE;
133  print "DB: could not connect to the db, connect is FALSE\n";
134  return(FALSE);
135  }
136  if (!isset ($this->_pg_conn)) {
137  $this->pg_Error = 1;
138  return (0);
139  }
140  $this->pg_Error = 0;
141  return (1);
142  }
152  public function dbQuery($Sql) {
153  /*
154  * sql query's can return False on error or NULL (no error, no
155  * results)
156  *
157  * This code is pretty much copied from Action in db-core. Look for
158  * areas where better error checking can be done (e.g. where the @
159  * is used)
160  */
161  $uid = posix_getuid();
162  $uidInfo = posix_getpwuid($uid);
163  $this->pg_rows = array ();
164  if (!$this->_pg_conn) {
165  return ($this->pg_rows); // think about this, is false better?
166  }
167  if (empty ($Sql)) {
168  return ($this->pg_rows); // same as above
169  }
170 
171  @ $result = pg_query($this->_pg_conn, $Sql);
172  DBCheckResult($result, $Sql, __FILE__, __LINE__);
173 
174  $this->Error = 0;
175  $this->pg_rows = pg_affected_rows($result);
176 
177  /* if the query returned nothing then just return*/
178  if (!isset ($result)) {
179  print "DB-Query: result not set!\n";
180  return;
181  }
182  @ $rows = pg_fetch_all($result);
183 
184  if (!is_array($rows)) {
185  $rows = array ();
186  }
187  //print "DB-QU: rows is\n"; print_r($rows) . "\n";
188  @ pg_free_result($result);
189  return $rows;
190  } // dbQuery
191 } // class db
192 ?>
dbQuery($Sql)
Definition: db.php:152
Definition: db.php:41
_docon($options=NULL)
Definition: db.php:112
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:198
connect($options=NULL)
Definition: db.php:92