FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
TestAbstractDb.php
1 <?php
2 /*
3 Copyright (C) 2015,2019 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 
25 
26 abstract class TestAbstractDb
27 {
29  protected $dbManager;
30 
31  protected function dirnameRec($path, $depth = 1)
32  {
33  for ($i = 0; $i < $depth; $i ++) {
34  $path = dirname($path);
35  }
36  return $path;
37  }
38 
43  abstract function createPlainTables($tableList, $invert=false);
44 
49  public function insertData($tableList, $invert=FALSE, $dataFile=null)
50  {
51  $testdataFile = $dataFile ?: dirname(__FILE__) . '/testdata.sql';
52  $testdata = file_get_contents($testdataFile);
53  $delimiter = 'INSERT INTO ';
54  $offset = strpos($testdata, $delimiter);
55  while (false !== $offset) {
56  $nextOffset = strpos($testdata, $delimiter, $offset + 1);
57  if (false === $nextOffset) {
58  $sql = substr($testdata, $offset);
59  } else {
60  $sql = substr($testdata, $offset, $nextOffset - $offset);
61  }
62  $table = array();
63  preg_match('/^INSERT INTO (?P<name>\w+) /', $sql, $table);
64  if (($invert ^ ! in_array($table['name'], $tableList))) {
65  $offset = $nextOffset;
66  continue;
67  }
68  $this->dbManager->queryOnce($this->queryConverter($sql));
69  $offset = $nextOffset;
70  }
71  }
72 
78  protected function queryConverter($sql)
79  {
80  return $sql;
81  }
82 
83  public function insertData_license_ref($limit=140)
84  {
85  $keysToBeChanged = array(
86  'rf_OSIapproved' => '"rf_OSIapproved"',
87  'rf_FSFfree'=> '"rf_FSFfree"',
88  'rf_GPLv2compatible' => '"rf_GPLv2compatible"',
89  'rf_GPLv3compatible'=> '"rf_GPLv3compatible"',
90  'rf_Fedora' => '"rf_Fedora"'
91  );
92 
94  $LIBEXECDIR = $this->dirnameRec(__FILE__, 5) . '/install/db';
95  $jsonData = json_decode(file_get_contents("$LIBEXECDIR/licenseRef.json"), true);
96  $statementName = __METHOD__.'.insertInToLicenseRef';
97  foreach ($jsonData as $licenseArrayKey => $licenseArray) {
98  $keys = strtr(implode(",", array_keys($licenseArray)), $keysToBeChanged);
99  $valuePlaceHolders = "$" . join(",$",range(1, count(array_keys($licenseArray))));
100  $SQL = "INSERT INTO license_ref ( $keys ) VALUES ($valuePlaceHolders);";
101  $this->dbManager->prepare($statementName, $SQL);
102  $this->dbManager->execute($statementName, array_values($licenseArray));
103  if ($licenseArrayKey >= $limit) {
104  break;
105  }
106  }
107  }
108 
114  protected function applySchema($type, $elementList, $invert=false)
115  {
116  $coreSchemaFile = $this->dirnameRec(__FILE__, 4) . '/www/ui/core-schema.dat';
117  $Schema = array();
118  require($coreSchemaFile);
119  foreach ($Schema[$type] as $viewName => $sql) {
120  if ($invert ^ ! in_array($viewName, $elementList)) {
121  continue;
122  }
123  $sqlCreate = is_array($sql) ? $sql['CREATE'] : $sql;
124  $this->dbManager->queryOnce($sqlCreate);
125  }
126  }
127 
132  public function createViews($viewList, $invert=FALSE)
133  {
134  $this->applySchema('VIEW', $viewList, $invert);
135  }
136 
141  public function alterTables($tableList, $invert=FALSE)
142  {
143  $coreSchemaFile = $this->dirnameRec(__FILE__, 4) . '/www/ui/core-schema.dat';
144  $Schema = array();
145  require($coreSchemaFile);
146  $attributeKey = "ALTER";
147  foreach ($Schema['TABLE'] as $tableName => $tableCols) {
148  if ($invert ^ ! in_array($tableName, $tableList)) {
149  continue;
150  }
151  foreach ($tableCols as $attributes) {
152  if (array_key_exists($attributeKey, $attributes)) {
153  $this->dbManager->queryOnce($attributes[$attributeKey]);
154  }
155  }
156  }
157  }
158 
159  public function &getDbManager()
160  {
161  return $this->dbManager;
162  }
163 }
applySchema($type, $elementList, $invert=false)
alterTables($tableList, $invert=FALSE)
queryConverter($sql)
convert sql string to something the drive understands
createViews($viewList, $invert=FALSE)
createPlainTables($tableList, $invert=false)
insertData($tableList, $invert=FALSE, $dataFile=null)
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28