FOSSology  4.6.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  pg_free_result($result);
135  return $ResArray;
136 }
137 
154 function DB2ValArray($Table, $ValCol, $Uniq=false, $Where="")
155 {
156  global $PG_CONN;
157 
158  $ResArray = array();
159 
160  if ($Uniq) {
161  $sql = "SELECT DISTINCT $ValCol from $Table $Where";
162  } else {
163  $sql = "SELECT $ValCol from $Table $Where";
164  }
165  $result = pg_query($PG_CONN, $sql);
166  DBCheckResult($result, $sql, __FILE__, __LINE__);
167 
168  $i = 0;
169  while ($row = pg_fetch_assoc($result)) {
170  $ResArray[$i] = $row[$ValCol];
171  ++ $i;
172  }
173  pg_free_result($result);
174  return $ResArray;
175 }
176 
177 
189 function DBCheckResult($result, $sql, $filenm, $lineno)
190 {
191  global $PG_CONN;
192 
193  if (! $result) {
194  echo "<hr>File: $filenm, Line number: $lineno<br>";
195  if (pg_connection_status($PG_CONN) === PGSQL_CONNECTION_OK) {
196  echo pg_last_error($PG_CONN);
197  } else {
198  echo "FATAL: DB connection lost.";
199  }
200  echo "<br> ".htmlspecialchars($sql);
201  debugbacktrace();
202  echo "<hr>";
203  exit(1);
204  }
205 }
206 
207 
216 function DB_TableExists($tableName)
217 {
218  global $PG_CONN;
219  global $SysConf;
220 
221  $sql = "select count(*) as count from information_schema.tables where "
222  . "table_catalog='{$SysConf['DBCONF']['dbname']}' and table_name='$tableName'";
223  $result = pg_query($PG_CONN, $sql);
224  DBCheckResult($result, $sql, __FILE__, __LINE__);
225  $row = pg_fetch_assoc($result);
226  $count = $row['count'];
227  pg_free_result($result);
228  return($count);
229 } /* DB_TableExists() */
230 
231 
242 function DB_ColExists($tableName, $colName, $DBName='fossology')
243 {
244  global $PG_CONN;
245 
246  $sql = "select count(*) as count from information_schema.columns where "
247  . "table_catalog='$DBName' and table_name='$tableName' and column_name='$colName'";
248  $result = pg_query($PG_CONN, $sql);
249  DBCheckResult($result, $sql, __FILE__, __LINE__);
250  $row = pg_fetch_assoc($result);
251  $count = $row['count'];
252  pg_free_result($result);
253  return($count);
254 } /* DB_ColExists() */
255 
256 
266 function DB_ConstraintExists($ConstraintName, $DBName='fossology')
267 {
268  global $PG_CONN;
269 
270  $sql = "select count(*) as count from information_schema.table_constraints "
271  . "where table_catalog='$DBName' and constraint_name='$ConstraintName' limit 1";
272  $result = pg_query($PG_CONN, $sql);
273  DBCheckResult($result, $sql, __FILE__, __LINE__);
274  $row = pg_fetch_assoc($result);
275  $count = $row['count'];
276  pg_free_result($result);
277  if ($count == 1) {
278  return true;
279  }
280  return False;
281 } /* DB_ColExists() */
282 
283 
295 function GetLastSeq($seqname, $tablename)
296 {
297  global $PG_CONN;
298 
299  $sql = "SELECT currval('$seqname') as mykey FROM $tablename";
300  $result = pg_query($PG_CONN, $sql);
301  DBCheckResult($result, $sql, __FILE__, __LINE__);
302  $row = pg_fetch_assoc($result);
303  $mykey = $row["mykey"];
304  pg_free_result($result);
305  return($mykey);
306 }
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:242
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:189
GetLastSeq($seqname, $tablename)
Get last sequence number.
Definition: common-db.php:295
DB_ConstraintExists($ConstraintName, $DBName='fossology')
Check if a constraint exists.
Definition: common-db.php:266
GetSingleRec($Table, $Where="")
Retrieve a single database record.
Definition: common-db.php:91
DB_TableExists($tableName)
Check if table exists.
Definition: common-db.php:216
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:154
debugbacktrace()
Debug back trace.
Definition: common-ui.php:77
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN