FOSSology  4.4.0
Open Source License Compliance by Open Source Software
TestLiteDb.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4  Author: Steffen Weber
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 use Monolog\Handler\StreamHandler;
15 use Monolog\Logger;
16 use SQLite3;
17 
19 {
21  private $dbFileName;
23  private $logFileName;
24 
25  function __construct($dbFileName = null)
26  {
27  if (! class_exists('Sqlite3')) {
28  throw new \Exception("Class SQLite3 not found");
29  }
30 
31  date_default_timezone_set("UTC");
32  if (! isset($dbFileName)) {
33  $dbFileName = ":memory:";
34  } else {
35  if (file_exists($dbFileName)) {
36  unlink($dbFileName);
37  }
38  }
39  $this->dbFileName = $dbFileName;
40 
41  require (dirname(dirname(__FILE__)).'/common-container.php');
42 
43  global $container;
44  $logger = $container->get('logger');
45  $this->logFileName = dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/db.sqlite.log';
46  $logger->pushHandler(new StreamHandler($this->logFileName, Logger::DEBUG));
47 
48  $sqlite3Connection = new SQLite3($this->dbFileName, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
49 
50  $liteDb = new SqliteE($sqlite3Connection);
51  $container->get('db.manager')->setDriver($liteDb);
52  $this->dbManager = $container->get('db.manager');
53  }
54 
55  function __destruct()
56  {
57  if (file_exists($this->logFileName)) {
58  unlink($this->logFileName);
59  }
60  $this->dbManager = null;
61  }
62 
67  public function createPlainTables($tableList, $invert=FALSE)
68  {
69  $coreSchemaFile = $this->dirnameRec(__FILE__, 4) . '/www/ui/core-schema.dat';
70  $Schema = array();
71  require($coreSchemaFile);
72  foreach ($Schema['TABLE'] as $tableName=>$tableCols) {
73  if ($invert^!in_array($tableName, $tableList) || array_key_exists($tableName, $Schema['INHERITS'])) {
74  continue;
75  }
76  $columns = array();
77  // $pattern = "ALTER TABLE \"license_ref\" ADD COLUMN \"rf_pk\" int8;"";
78  foreach ($tableCols as $attributes) {
79  $sql = preg_replace('/ DEFAULT .*/', '', $attributes["ADD"]);
80  $alterSql = explode('"', $sql);
81  $columns[$alterSql[3]] = "$alterSql[3] " . $alterSql[4];
82  }
83  $createSql = "CREATE TABLE $tableName (" . implode(',', $columns) . ')';
84  $this->dbManager->queryOnce($createSql);
85  }
86  }
87 
93  protected function queryConverter($sql)
94  {
95  $sql = str_replace(' false,', " 'false',", $sql);
96  $sql = str_replace(' true,', " 'true',", $sql);
97  $sql = str_replace(' false)', " 'false')", $sql);
98  $sql = str_replace(' true)', " 'true')", $sql);
99  return $sql;
100  }
101 }
queryConverter($sql)
convert sql string to something the drive understands
Definition: TestLiteDb.php:93
createPlainTables($tableList, $invert=FALSE)
Definition: TestLiteDb.php:67
int Test
Definition: util.c:20
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16