FOSSology  4.4.0
Open Source License Compliance by Open Source Software
common-db.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2011-2012 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2017 Siemens AG
5 
6  SPDX-License-Identifier: LGPL-2.1-only
7 */
8 
33 function DBconnect($sysconfdir, $options="", $exitOnFail=true)
34 {
35  global $PG_CONN;
36 
37  if (! empty($PG_CONN)) {
38  return $PG_CONN;
39  }
40 
41  $path="$sysconfdir/Db.conf";
42  if (empty($options)) {
43  $dbConf = file_get_contents($path);
44  if ($exitOnFail && (false === $dbConf)) {
45  $text = _("Could not connect to FOSSology database.");
46  echo "<h2>$text</h2>";
47  echo _('permission denied for configuration file');
48  exit();
49  }
50  if (false === $dbConf) {
51  $PG_CONN = false;
52  return;
53  }
54  $options = $dbConf;
55  }
56  if (! empty($options)) {
57  $PG_CONN = pg_connect(str_replace(";", " ", $options));
58  }
59 
60  if (! empty($PG_CONN)) {
61  /* success */
62  return $PG_CONN;
63  }
64 
65  if ($exitOnFail) {
66  $text = _("Could not connect to FOSSology database.");
67  echo "<h2>$text</h2>";
68  exit();
69  }
70  $PG_CONN = false;
71 }
72 
73 
91 function GetSingleRec($Table, $Where="")
92 {
93  global $PG_CONN;
94 
95  $sql = "SELECT * from $Table $Where limit 1";
96  $result = pg_query($PG_CONN, $sql);
97  DBCheckResult($result, $sql, __FILE__, __LINE__);
98 
99  $row = pg_fetch_assoc($result);
100  pg_free_result($result);
101  return $row;
102 }
103 
104 
121 function DB2KeyValArray($Table, $KeyCol, $ValCol, $Where="")
122 {
123  global $PG_CONN;
124 
125  $ResArray = array();
126 
127  $sql = "SELECT $KeyCol, $ValCol from $Table $Where";
128  $result = pg_query($PG_CONN, $sql);
129  DBCheckResult($result, $sql, __FILE__, __LINE__);
130 
131  while ($row = pg_fetch_assoc($result)) {
132  $ResArray[$row[$KeyCol]] = $row[$ValCol];
133  }
134  return $ResArray;
135 }
136 
153 function DB2ValArray($Table, $ValCol, $Uniq=false, $Where="")
154 {
155  global $PG_CONN;
156 
157  $ResArray = array();
158 
159  if ($Uniq) {
160  $sql = "SELECT DISTINCT $ValCol from $Table $Where";
161  } else {
162  $sql = "SELECT $ValCol from $Table $Where";
163  }
164  $result = pg_query($PG_CONN, $sql);
165  DBCheckResult($result, $sql, __FILE__, __LINE__);
166 
167  $i = 0;
168  while ($row = pg_fetch_assoc($result)) {
169  $ResArray[$i] = $row[$ValCol];
170  ++ $i;
171  }
172  return $ResArray;
173 }
174 
175 
187 function DBCheckResult($result, $sql, $filenm, $lineno)
188 {
189  global $PG_CONN;
190 
191  if (! $result) {
192  echo "<hr>File: $filenm, Line number: $lineno<br>";
193  if (pg_connection_status($PG_CONN) === PGSQL_CONNECTION_OK) {
194  echo pg_last_error($PG_CONN);
195  } else {
196  echo "FATAL: DB connection lost.";
197  }
198  echo "<br> ".htmlspecialchars($sql);
199  debugbacktrace();
200  echo "<hr>";
201  exit(1);
202  }
203 }
204 
205 
214 function DB_TableExists($tableName)
215 {
216  global $PG_CONN;
217  global $SysConf;
218 
219  $sql = "select count(*) as count from information_schema.tables where "
220  . "table_catalog='{$SysConf['DBCONF']['dbname']}' and table_name='$tableName'";
221  $result = pg_query($PG_CONN, $sql);
222  DBCheckResult($result, $sql, __FILE__, __LINE__);
223  $row = pg_fetch_assoc($result);
224  $count = $row['count'];
225  pg_free_result($result);
226  return($count);
227 } /* DB_TableExists() */
228 
229 
240 function DB_ColExists($tableName, $colName, $DBName='fossology')
241 {
242  global $PG_CONN;
243 
244  $sql = "select count(*) as count from information_schema.columns where "
245  . "table_catalog='$DBName' and table_name='$tableName' and column_name='$colName'";
246  $result = pg_query($PG_CONN, $sql);
247  DBCheckResult($result, $sql, __FILE__, __LINE__);
248  $row = pg_fetch_assoc($result);
249  $count = $row['count'];
250  pg_free_result($result);
251  return($count);
252 } /* DB_ColExists() */
253 
254 
264 function DB_ConstraintExists($ConstraintName, $DBName='fossology')
265 {
266  global $PG_CONN;
267 
268  $sql = "select count(*) as count from information_schema.table_constraints "
269  . "where table_catalog='$DBName' and constraint_name='$ConstraintName' limit 1";
270  $result = pg_query($PG_CONN, $sql);
271  DBCheckResult($result, $sql, __FILE__, __LINE__);
272  $row = pg_fetch_assoc($result);
273  $count = $row['count'];
274  pg_free_result($result);
275  if ($count == 1) {
276  return true;
277  }
278  return False;
279 } /* DB_ColExists() */
280 
281 
293 function GetLastSeq($seqname, $tablename)
294 {
295  global $PG_CONN;
296 
297  $sql = "SELECT currval('$seqname') as mykey FROM $tablename";
298  $result = pg_query($PG_CONN, $sql);
299  DBCheckResult($result, $sql, __FILE__, __LINE__);
300  $row = pg_fetch_assoc($result);
301  $mykey = $row["mykey"];
302  pg_free_result($result);
303  return($mykey);
304 }
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:33
DB_ColExists($tableName, $colName, $DBName='fossology')
Check if a column exists.
Definition: common-db.php:240
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
GetLastSeq($seqname, $tablename)
Get last sequence number.
Definition: common-db.php:293
DB_ConstraintExists($ConstraintName, $DBName='fossology')
Check if a constraint exists.
Definition: common-db.php:264
GetSingleRec($Table, $Where="")
Retrieve a single database record.
Definition: common-db.php:91
DB_TableExists($tableName)
Check if table exists.
Definition: common-db.php:214
DB2KeyValArray($Table, $KeyCol, $ValCol, $Where="")
Create an associative array by using table rows to source the key/value pairs.
Definition: common-db.php:121
DB2ValArray($Table, $ValCol, $Uniq=false, $Where="")
Create an array by using table rows to source the values.
Definition: common-db.php:153
debugbacktrace()
Debug back trace.
Definition: common-ui.php:77
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN