FOSSology  4.4.0
Open Source License Compliance by Open Source Software
common-db.php File Reference

This file contains common database functions. More...

Go to the source code of this file.

Functions

 DBconnect ($sysconfdir, $options="", $exitOnFail=true)
 Connect to database engine. This is a no-op if $PG_CONN already has a value. More...
 
 GetSingleRec ($Table, $Where="")
 Retrieve a single database record. More...
 
 DB2KeyValArray ($Table, $KeyCol, $ValCol, $Where="")
 Create an associative array by using table rows to source the key/value pairs. More...
 
 DB2ValArray ($Table, $ValCol, $Uniq=false, $Where="")
 Create an array by using table rows to source the values. More...
 
 DBCheckResult ($result, $sql, $filenm, $lineno)
 Check the postgres result for unexpected errors. If found, treat them as fatal. More...
 
 DB_TableExists ($tableName)
 Check if table exists. More...
 
 DB_ColExists ($tableName, $colName, $DBName='fossology')
 Check if a column exists. More...
 
 DB_ConstraintExists ($ConstraintName, $DBName='fossology')
 Check if a constraint exists. More...
 
 GetLastSeq ($seqname, $tablename)
 Get last sequence number. More...
 

Detailed Description

This file contains common database functions.

Definition in file common-db.php.

Function Documentation

◆ DB2KeyValArray()

DB2KeyValArray (   $Table,
  $KeyCol,
  $ValCol,
  $Where = "" 
)

Create an associative array by using table rows to source the key/value pairs.

Parameters
string$Tabletablename
string$KeyColKey column name in $Table
string$ValColValue column name in $Table
string$WhereSQL where clause (optional) This can really be any clause following the table name in the sql
Returns
Array[Key] = Val for each row in the table. May be empty if no table rows or Where results in no rows.

Definition at line 121 of file common-db.php.

◆ DB2ValArray()

DB2ValArray (   $Table,
  $ValCol,
  $Uniq = false,
  $Where = "" 
)

Create an array by using table rows to source the values.

Parameters
string$Tabletablename
string$ValColValue column name in $Table
string$UniqSort out duplicates
string$WhereSQL where clause (optional) This can really be any clause following the table name in the sql
Returns
Array[Key] = Val for each row in the table. May be empty if no table rows or Where results in no rows.

Definition at line 153 of file common-db.php.

◆ DB_ColExists()

DB_ColExists (   $tableName,
  $colName,
  $DBName = 'fossology' 
)

Check if a column exists.

Note
This is postgresql specific.
Parameters
string$tableNameTable to check in
string$colNameColumn to check
string$DBNameDatabase name, default "fossology"
Returns
1 if column exists, 0 if not.

Definition at line 240 of file common-db.php.

◆ DB_ConstraintExists()

DB_ConstraintExists (   $ConstraintName,
  $DBName = 'fossology' 
)

Check if a constraint exists.

Note
This is postgresql specific.
Parameters
string$ConstraintNameConstraint to check
string$DBNameDatabase name, default "fossology"
Returns
True if constraint exists, False if not.

Definition at line 264 of file common-db.php.

◆ DB_TableExists()

DB_TableExists (   $tableName)

Check if table exists.

Note
This is postgresql specific.
Parameters
string$tableNameTable to check
Returns
1 if table exists, 0 if not.

Definition at line 214 of file common-db.php.

◆ DBCheckResult()

DBCheckResult (   $result,
  $sql,
  $filenm,
  $lineno 
)

Check the postgres result for unexpected errors. If found, treat them as fatal.

Parameters
$resultcommand result object
string$sqlSQL command (optional)
string$filenmFile name (FILE)
int$linenoLine number of the caller (LINE)
Returns
None, prints error, sql and line number, then exits(1)
Examples
/home/runner/work/fossology/fossology/src/www/ui/ui-picker.php.

Definition at line 187 of file common-db.php.

◆ DBconnect()

DBconnect (   $sysconfdir,
  $options = "",
  $exitOnFail = true 
)

Connect to database engine. This is a no-op if $PG_CONN already has a value.

Parameters
string$sysconfdirfossology configuration directory (location of Db.conf)
string$optionsan optional list of attributes for connecting to the database. E.g.: "dbname=text host=text user=text password=text"
bool$exitOnFailtrue (default) to print error and call exit on failure false to return $PG_CONN === false on failure

If $options is empty, then connection parameters will be read from Db.conf.

Returns
Success: $PG_CONN, the postgres connection object
Failure: Error message is printed

Definition at line 33 of file common-db.php.

◆ GetLastSeq()

GetLastSeq (   $seqname,
  $tablename 
)

Get last sequence number.

This is typically used to get the primary key of a newly inserted record. This must be called immediately after the insert.

Parameters
string$seqnameSequence Name of key just added
string$tablenameTable containing $seqname
Returns
Current sequence number (i.e. the primary key of the rec just added)

Definition at line 293 of file common-db.php.

◆ GetSingleRec()

GetSingleRec (   $Table,
  $Where = "" 
)

Retrieve a single database record.

This function does a:

"SELECT * from $Table $Where limit 1"

and returns the result as an associative array.

Parameters
string$TableTable name
string$WhereSQL where clause e.g. "where uploadtree_pk=2". Though a WHERE clause is the typical use, $Where can really be any options following the sql tablename.
Returns
Associative array for this record. May be empty if no record found.
Examples
/home/runner/work/fossology/fossology/src/www/ui/ui-picker.php.

Definition at line 91 of file common-db.php.