FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
common-db.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  Copyright (C) 2011-2012 Hewlett-Packard Development Company, L.P.
4  Copyright (C) 2017, Siemens AG
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License version 2.1 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this library; if not, write to the Free Software Foundation, Inc.0
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 ***********************************************************/
19 
44 function DBconnect($sysconfdir, $options="", $exitOnFail=true)
45 {
46  global $PG_CONN;
47 
48  if (! empty($PG_CONN)) {
49  return $PG_CONN;
50  }
51 
52  $path="$sysconfdir/Db.conf";
53  if (empty($options)) {
54  $dbConf = file_get_contents($path);
55  if ($exitOnFail && (false === $dbConf)) {
56  $text = _("Could not connect to FOSSology database.");
57  echo "<h2>$text</h2>";
58  echo _('permission denied for configuration file');
59  exit();
60  }
61  if (false === $dbConf) {
62  $PG_CONN = false;
63  return;
64  }
65  $options = $dbConf;
66  }
67  if (! empty($options)) {
68  $PG_CONN = pg_connect(str_replace(";", " ", $options));
69  }
70 
71  if (! empty($PG_CONN)) {
72  /* success */
73  return $PG_CONN;
74  }
75 
76  if ($exitOnFail) {
77  $text = _("Could not connect to FOSSology database.");
78  echo "<h2>$text</h2>";
79  exit();
80  }
81  $PG_CONN = false;
82 }
83 
84 
102 function GetSingleRec($Table, $Where="")
103 {
104  global $PG_CONN;
105 
106  $sql = "SELECT * from $Table $Where limit 1";
107  $result = pg_query($PG_CONN, $sql);
108  DBCheckResult($result, $sql, __FILE__, __LINE__);
109 
110  $row = pg_fetch_assoc($result);
111  pg_free_result($result);
112  return $row;
113 }
114 
115 
132 function DB2KeyValArray($Table, $KeyCol, $ValCol, $Where="")
133 {
134  global $PG_CONN;
135 
136  $ResArray = array();
137 
138  $sql = "SELECT $KeyCol, $ValCol from $Table $Where";
139  $result = pg_query($PG_CONN, $sql);
140  DBCheckResult($result, $sql, __FILE__, __LINE__);
141 
142  while ($row = pg_fetch_assoc($result)) {
143  $ResArray[$row[$KeyCol]] = $row[$ValCol];
144  }
145  return $ResArray;
146 }
147 
164 function DB2ValArray($Table, $ValCol, $Uniq=false, $Where="")
165 {
166  global $PG_CONN;
167 
168  $ResArray = array();
169 
170  if ($Uniq) {
171  $sql = "SELECT DISTINCT $ValCol from $Table $Where";
172  } else {
173  $sql = "SELECT $ValCol from $Table $Where";
174  }
175  $result = pg_query($PG_CONN, $sql);
176  DBCheckResult($result, $sql, __FILE__, __LINE__);
177 
178  $i = 0;
179  while ($row = pg_fetch_assoc($result)) {
180  $ResArray[$i] = $row[$ValCol];
181  ++ $i;
182  }
183  return $ResArray;
184 }
185 
186 
198 function DBCheckResult($result, $sql, $filenm, $lineno)
199 {
200  global $PG_CONN;
201 
202  if (! $result) {
203  echo "<hr>File: $filenm, Line number: $lineno<br>";
204  if (pg_connection_status($PG_CONN) === PGSQL_CONNECTION_OK) {
205  echo pg_last_error($PG_CONN);
206  } else {
207  echo "FATAL: DB connection lost.";
208  }
209  echo "<br> $sql";
210  debugbacktrace();
211  echo "<hr>";
212  exit(1);
213  }
214 }
215 
216 
225 function DB_TableExists($tableName)
226 {
227  global $PG_CONN;
228  global $SysConf;
229 
230  $sql = "select count(*) as count from information_schema.tables where "
231  . "table_catalog='{$SysConf['DBCONF']['dbname']}' and table_name='$tableName'";
232  $result = pg_query($PG_CONN, $sql);
233  DBCheckResult($result, $sql, __FILE__, __LINE__);
234  $row = pg_fetch_assoc($result);
235  $count = $row['count'];
236  pg_free_result($result);
237  return($count);
238 } /* DB_TableExists() */
239 
240 
251 function DB_ColExists($tableName, $colName, $DBName='fossology')
252 {
253  global $PG_CONN;
254 
255  $sql = "select count(*) as count from information_schema.columns where "
256  . "table_catalog='$DBName' and table_name='$tableName' and column_name='$colName'";
257  $result = pg_query($PG_CONN, $sql);
258  DBCheckResult($result, $sql, __FILE__, __LINE__);
259  $row = pg_fetch_assoc($result);
260  $count = $row['count'];
261  pg_free_result($result);
262  return($count);
263 } /* DB_ColExists() */
264 
265 
275 function DB_ConstraintExists($ConstraintName, $DBName='fossology')
276 {
277  global $PG_CONN;
278 
279  $sql = "select count(*) as count from information_schema.table_constraints "
280  . "where table_catalog='$DBName' and constraint_name='$ConstraintName' limit 1";
281  $result = pg_query($PG_CONN, $sql);
282  DBCheckResult($result, $sql, __FILE__, __LINE__);
283  $row = pg_fetch_assoc($result);
284  $count = $row['count'];
285  pg_free_result($result);
286  if ($count == 1) {
287  return true;
288  }
289  return False;
290 } /* DB_ColExists() */
291 
292 
304 function GetLastSeq($seqname, $tablename)
305 {
306  global $PG_CONN;
307 
308  $sql = "SELECT currval('$seqname') as mykey FROM $tablename";
309  $result = pg_query($PG_CONN, $sql);
310  DBCheckResult($result, $sql, __FILE__, __LINE__);
311  $row = pg_fetch_assoc($result);
312  $mykey = $row["mykey"];
313  pg_free_result($result);
314  return($mykey);
315 }
DBconnect($sysconfdir, $options="", $exitOnFail=true)
Connect to database engine. This is a no-op if $PG_CONN already has a value.
Definition: common-db.php:44
GetLastSeq($seqname, $tablename)
Get last sequence number.
Definition: common-db.php:304
GetSingleRec($Table, $Where="")
Retrieve a single database record.
Definition: common-db.php:102
DB2ValArray($Table, $ValCol, $Uniq=false, $Where="")
Create an array by using table rows to source the values.
Definition: common-db.php:164
DB_TableExists($tableName)
Check if table exists.
Definition: common-db.php:225
DB_ConstraintExists($ConstraintName, $DBName='fossology')
Check if a constraint exists.
Definition: common-db.php:275
DB2KeyValArray($Table, $KeyCol, $ValCol, $Where="")
Create an associative array by using table rows to source the key/value pairs.
Definition: common-db.php:132
DB_ColExists($tableName, $colName, $DBName='fossology')
Check if a column exists.
Definition: common-db.php:251
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:198
debugbacktrace()
Debug back trace.
Definition: common-ui.php:86