FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
TestPgDb.php
1 <?php
2 /*
3 Copyright (C) 2014-2015, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
19 namespace Fossology\Lib\Test;
20 
21 // setup autoloading
22 require_once(dirname(dirname(dirname(dirname(__FILE__)))) . "/vendor/autoload.php");
23 require_once(__DIR__ . "/../../../testing/db/TestDbFactory.php");
24 
27 use Monolog\Logger;
28 
29 
30 class TestPgDb extends TestAbstractDb
31 {
33  private $dbName;
35  private $logFileName;
37  private $connection;
39  private $sys_conf;
40 
41  function __construct($dbName = null, $sysConf = null)
42  {
43  $dbName = strtolower($dbName);
44  $testDbFactory = new \TestDbFactory();
45  $this->sys_conf = $sysConf;
46  if (empty($this->sys_conf)) {
47  $this->sys_conf = $testDbFactory->setupTestDb($dbName);
48  $dbName = $testDbFactory->getDbName($this->sys_conf);
49  }
50  $this->dbName = $dbName;
51 
52  require_once (dirname(dirname(__FILE__)).'/common-db.php');
53  $this->connection = DBconnect($this->sys_conf);
54 
55  require (dirname(dirname(__FILE__)).'/common-container.php');
56  global $container;
57  $logger = new Logger('default'); // $container->get('logger');
58  $this->logFileName = dirname(dirname(dirname(dirname(dirname(__FILE__))))) . 'db.pg.log';
59  $logger->pushHandler(new StreamHandler($this->logFileName, Logger::DEBUG));
60 
61  $this->dbManager = $container->get('db.manager');
62  $postgres = new Postgres($this->connection);
63  $this->dbManager->setDriver($postgres);
64  $this->dbManager->queryOnce("DEALLOCATE ALL");
65  $this->dropAllTables();
66  $this->dropAllSequences();
67  }
68 
69  public function getFossSysConf()
70  {
71  return $this->sys_conf;
72  }
73 
74  private function dropAllTables()
75  {
76  $this->dbManager->prepare(__METHOD__.'.get',"SELECT table_name FROM information_schema.tables WHERE table_schema=$1 AND table_type=$2");
77  $res = $this->dbManager->execute(__METHOD__.'.get',array('public','BASE TABLE'));
78  $tableNames = $this->dbManager->fetchAll($res);
79  $this->dbManager->freeResult($res);
80  foreach ($tableNames as $row) {
81  $name = $row['table_name'];
82  $this->dbManager->queryOnce("DROP TABLE IF EXISTS $name CASCADE",$sqlLog=__METHOD__.".$name");
83  }
84  }
85 
86  private function dropAllSequences()
87  {
88  $this->dbManager->prepare($stmt=__METHOD__.'.get',
89  "SELECT sequence_name FROM information_schema.sequences WHERE sequence_schema=$1");
90  $res = $this->dbManager->execute($stmt,array('public'));
91  $tableNames = $this->dbManager->fetchAll($res);
92  $this->dbManager->freeResult($res);
93  foreach ($tableNames as $row) {
94  $name = $row['sequence_name'];
95  $this->dbManager->queryOnce("DROP SEQUENCE $name CASCADE",$sqlLog=__METHOD__.".$name");
96  }
97  }
98 
99  function __destruct()
100  {
101  $this->dbManager = null;
102  $this->connection = null;
103  }
104 
105  function fullDestruct()
106  {
107  pg_close($this->connection);
108  $GLOBALS['PG_CONN'] = false;
109  $testDbFactory = new \TestDbFactory();
110  $testDbFactory->purgeTestDb($this->sys_conf);
111  $this->dbManager = null;
112  }
113 
114  function isInFossyGroup()
115  {
116  $gid_array = posix_getgroups();
117  foreach ($gid_array as $gid) {
118  $gid_info = posix_getgrgid($gid);
119  if ($gid_info['name'] === 'fossy') {
120  return true;
121  }
122  }
123  $uid = posix_getuid();
124  $uid_info = posix_getpwuid($uid);
125  return ($uid_info['name'] !== 'root');
126  }
127 
132  public function createPlainTables($tableList, $invert=false)
133  {
134  $coreSchemaFile = $this->dirnameRec(__FILE__, 4) . '/www/ui/core-schema.dat';
135  $Schema = array();
136  require($coreSchemaFile);
137  foreach ($Schema['TABLE'] as $tableName=>$tableCols) {
138  if ($invert^!in_array($tableName, $tableList) || array_key_exists($tableName, $Schema['INHERITS'])) {
139  continue;
140  }
141  $this->dbManager->queryOnce("CREATE TABLE \"$tableName\" ()");
142  $sqlAddArray = array();
143  foreach ($tableCols as $attributes) {
144  $sqlAdd = preg_replace('/ DEFAULT .*/','',$attributes["ADD"]);
145  $sqlAddArray[] = $sqlAdd;
146  }
147  $this->dbManager->queryOnce(implode(";\n",$sqlAddArray));
148  }
149  }
150 
151  public function resetSequenceAsMaxOf($sequenceName, $tableName, $columnName)
152  {
153  $this->dbManager->queryOnce("SELECT setval('$sequenceName', (SELECT MAX($columnName) FROM $tableName))");
154  }
155 
160  public function createSequences($seqList, $invert=FALSE)
161  {
162  $this->applySchema('SEQUENCE', $seqList, $invert);
163  }
164 
169  public function createConstraints($cList, $invert=FALSE)
170  {
171  $this->applySchema('CONSTRAINT', $cList, $invert);
172  }
173 
177  public function createInheritedTables($tableList=array())
178  {
179  $table = 'license_candidate';
180  if ((empty($tableList) || in_array($table, $tableList)) && !$this->dbManager->existsTable($table)) {
181  $this->dbManager->queryOnce("CREATE TABLE $table (group_fk integer) INHERITS (license_ref)");
182  }
183  $coreSchemaFile = $this->dirnameRec(__FILE__, 4) . '/www/ui/core-schema.dat';
184  $Schema = array();
185  require($coreSchemaFile);
186  foreach ($Schema['INHERITS'] as $table=>$fromTable) {
187  if ($fromTable=='master_ars' || !empty($tableList) && !in_array($table, $tableList) ) {
188  continue;
189  }
190  if (!$this->dbManager->existsTable($table) && $this->dbManager->existsTable($fromTable)) {
191  $this->dbManager->queryOnce("CREATE TABLE \"$table\" () INHERITS (\"$fromTable\")");
192  }
193  }
194  }
195 
196  public function createInheritedArsTables($agents)
197  {
198  foreach ($agents as $agent) {
199  if (!$this->dbManager->existsTable($agent . '_ars')) {
200  $this->dbManager->queryOnce("create table " . $agent . "_ars() inherits(ars_master)");
201  }
202  }
203  }
204 }
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
createPlainTables($tableList, $invert=false)
Definition: TestPgDb.php:132
applySchema($type, $elementList, $invert=false)
createSequences($seqList, $invert=FALSE)
Definition: TestPgDb.php:160
createConstraints($cList, $invert=FALSE)
Definition: TestPgDb.php:169
createInheritedTables($tableList=array())
Definition: TestPgDb.php:177
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28