FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
TestLiteDb.php
1 <?php
2 /*
3 Copyright (C) 2014-2015, Siemens AG
4 Author: Steffen Weber
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
20 namespace Fossology\Lib\Test;
21 
22 // setup autoloading
23 require_once(dirname(dirname(dirname(dirname(__FILE__)))) . "/vendor/autoload.php");
24 
27 use Monolog\Logger;
28 use SQLite3;
29 
31 {
33  private $dbFileName;
35  private $logFileName;
36 
37  function __construct($dbFileName = null)
38  {
39  if (! class_exists('Sqlite3')) {
40  throw new \Exception("Class SQLite3 not found");
41  }
42 
43  date_default_timezone_set("UTC");
44  if (! isset($dbFileName)) {
45  $dbFileName = ":memory:";
46  } else {
47  if (file_exists($dbFileName)) {
48  unlink($dbFileName);
49  }
50  }
51  $this->dbFileName = $dbFileName;
52 
53  require (dirname(dirname(__FILE__)).'/common-container.php');
54 
55  global $container;
56  $logger = $container->get('logger');
57  $this->logFileName = dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/db.sqlite.log';
58  $logger->pushHandler(new StreamHandler($this->logFileName, Logger::DEBUG));
59 
60  $sqlite3Connection = new SQLite3($this->dbFileName, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
61 
62  $liteDb = new SqliteE($sqlite3Connection);
63  $container->get('db.manager')->setDriver($liteDb);
64  $this->dbManager = $container->get('db.manager');
65  }
66 
67  function __destruct()
68  {
69  if (file_exists($this->logFileName)) {
70  unlink($this->logFileName);
71  }
72  $this->dbManager = null;
73  }
74 
79  public function createPlainTables($tableList, $invert=FALSE)
80  {
81  $coreSchemaFile = $this->dirnameRec(__FILE__, 4) . '/www/ui/core-schema.dat';
82  $Schema = array();
83  require($coreSchemaFile);
84  foreach ($Schema['TABLE'] as $tableName=>$tableCols) {
85  if ($invert^!in_array($tableName, $tableList) || array_key_exists($tableName, $Schema['INHERITS'])) {
86  continue;
87  }
88  $columns = array();
89  // $pattern = "ALTER TABLE \"license_ref\" ADD COLUMN \"rf_pk\" int8;"";
90  foreach ($tableCols as $attributes) {
91  $sql = preg_replace('/ DEFAULT .*/', '', $attributes["ADD"]);
92  $alterSql = explode('"', $sql);
93  $columns[$alterSql[3]] = "$alterSql[3] " . $alterSql[4];
94  }
95  $createSql = "CREATE TABLE $tableName (" . implode(',', $columns) . ')';
96  $this->dbManager->queryOnce($createSql);
97  }
98  }
99 
105  protected function queryConverter($sql)
106  {
107  $sql = str_replace(' false,', " 'false',", $sql);
108  $sql = str_replace(' true,', " 'true',", $sql);
109  $sql = str_replace(' false)', " 'false')", $sql);
110  $sql = str_replace(' true)', " 'true')", $sql);
111  return $sql;
112  }
113 }
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
createPlainTables($tableList, $invert=FALSE)
Definition: TestLiteDb.php:79
queryConverter($sql)
convert sql string to something the drive understands
Definition: TestLiteDb.php:105