FOSSology  4.4.0
Open Source License Compliance by Open Source Software
TestAbstractDb.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2015, 2019 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Lib\Test;
9 
10 // setup autoloading
11 require_once(dirname(dirname(dirname(dirname(__FILE__)))) . "/vendor/autoload.php");
12 
14 
15 abstract class TestAbstractDb
16 {
18  protected $dbManager;
19 
20  protected function dirnameRec($path, $depth = 1)
21  {
22  for ($i = 0; $i < $depth; $i ++) {
23  $path = dirname($path);
24  }
25  return $path;
26  }
27 
32  abstract function createPlainTables($tableList, $invert=false);
33 
38  public function insertData($tableList, $invert=FALSE, $dataFile=null)
39  {
40  $testdataFile = $dataFile ?: dirname(__FILE__) . '/testdata.sql';
41  $testdata = file_get_contents($testdataFile);
42  $delimiter = 'INSERT INTO ';
43  $offset = strpos($testdata, $delimiter);
44  while (false !== $offset) {
45  $nextOffset = strpos($testdata, $delimiter, $offset + 1);
46  if (false === $nextOffset) {
47  $sql = substr($testdata, $offset);
48  } else {
49  $sql = substr($testdata, $offset, $nextOffset - $offset);
50  }
51  $table = array();
52  preg_match('/^INSERT INTO (?P<name>\w+) /', $sql, $table);
53  if (($invert ^ ! in_array($table['name'], $tableList))) {
54  $offset = $nextOffset;
55  continue;
56  }
57  $this->dbManager->queryOnce($this->queryConverter($sql));
58  $offset = $nextOffset;
59  }
60  }
61 
67  protected function queryConverter($sql)
68  {
69  return $sql;
70  }
71 
72  public function insertData_license_ref($limit=140)
73  {
74  $keysToBeChanged = array(
75  'rf_OSIapproved' => '"rf_OSIapproved"',
76  'rf_FSFfree'=> '"rf_FSFfree"',
77  'rf_GPLv2compatible' => '"rf_GPLv2compatible"',
78  'rf_GPLv3compatible'=> '"rf_GPLv3compatible"',
79  'rf_Fedora' => '"rf_Fedora"'
80  );
81  $keysToReplicate = [
82  "rf_spdx_id" => "rf_shortname",
83  ];
84  $keysToRemove = [
85  "rf_spdx_compatible"
86  ];
87 
89  $LIBEXECDIR = $this->dirnameRec(__FILE__, 5) . '/install/db';
90  $jsonData = json_decode(file_get_contents("$LIBEXECDIR/licenseRef.json"), true);
91  $statementName = __METHOD__.'.insertInToLicenseRef';
92  foreach ($jsonData as $licenseArrayKey => $licenseArray) {
93  foreach ($keysToReplicate as $duplicateKey => $originalKey) {
94  if ($licenseArray['rf_spdx_compatible'] == 't') {
95  $licenseArray[$duplicateKey] = $licenseArray[$originalKey];
96  } else {
97  $licenseArray[$duplicateKey] = "";
98  }
99  }
100  foreach ($keysToRemove as $key) {
101  unset($licenseArray[$key]);
102  }
103  ksort($licenseArray);
104  $arrayKeys = array_keys($licenseArray);
105  $arrayValues = array_values($licenseArray);
106  $keys = strtr(implode(",", $arrayKeys), $keysToBeChanged);
107  $valuePlaceHolders = "$" . join(",$",range(1, count($arrayKeys)));
108  $md5PlaceHolder = "$". (count($arrayKeys) + 1);
109  $arrayValues[] = $licenseArray['rf_text'];
110  $SQL = "INSERT INTO license_ref ( $keys,rf_md5 ) " .
111  "VALUES ($valuePlaceHolders,md5($md5PlaceHolder));";
112  $this->dbManager->prepare($statementName, $SQL);
113  $this->dbManager->execute($statementName, $arrayValues);
114  if ($licenseArrayKey >= $limit) {
115  break;
116  }
117  }
118  }
119 
125  protected function applySchema($type, $elementList, $invert=false)
126  {
127  $coreSchemaFile = $this->dirnameRec(__FILE__, 4) . '/www/ui/core-schema.dat';
128  $Schema = array();
129  require($coreSchemaFile);
130  foreach ($Schema[$type] as $viewName => $sql) {
131  if ($invert ^ ! in_array($viewName, $elementList)) {
132  continue;
133  }
134  $sqlCreate = is_array($sql) ? $sql['CREATE'] : $sql;
135  $this->dbManager->queryOnce($sqlCreate);
136  }
137  }
138 
143  public function createViews($viewList, $invert=FALSE)
144  {
145  $this->applySchema('VIEW', $viewList, $invert);
146  }
147 
152  public function alterTables($tableList, $invert=FALSE)
153  {
154  $coreSchemaFile = $this->dirnameRec(__FILE__, 4) . '/www/ui/core-schema.dat';
155  $Schema = array();
156  require($coreSchemaFile);
157  $attributeKey = "ALTER";
158  foreach ($Schema['TABLE'] as $tableName => $tableCols) {
159  if ($invert ^ ! in_array($tableName, $tableList)) {
160  continue;
161  }
162  foreach ($tableCols as $attributes) {
163  if (array_key_exists($attributeKey, $attributes)) {
164  $this->dbManager->queryOnce($attributes[$attributeKey]);
165  }
166  }
167  }
168  }
169 
170  public function &getDbManager()
171  {
172  return $this->dbManager;
173  }
174 }
applySchema($type, $elementList, $invert=false)
createViews($viewList, $invert=FALSE)
alterTables($tableList, $invert=FALSE)
queryConverter($sql)
convert sql string to something the drive understands
createPlainTables($tableList, $invert=false)
insertData($tableList, $invert=FALSE, $dataFile=null)
int Test
Definition: util.c:20
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16