Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
n/a
0 / 0
n/a
0 / 0
CRAP
n/a
0 / 0
Db
n/a
0 / 0
n/a
0 / 0
23
n/a
0 / 0
 startExecuteSqlFile
n/a
0 / 0
n/a
0 / 0
9
 executeSql
n/a
0 / 0
n/a
0 / 0
1
 startUsingDatabase
n/a
0 / 0
n/a
0 / 0
4
 startTestDataImport
n/a
0 / 0
n/a
0 / 0
1
 startConfigDataImport
n/a
0 / 0
n/a
0 / 0
1
 startMigrations
n/a
0 / 0
n/a
0 / 0
5
 executeTestData
n/a
0 / 0
n/a
0 / 0
2
1<?php
2
3namespace BO\Zmsdb\Cli;
4
5/**
6 * @codeCoverageIgnore
7 * @SuppressWarnings(Short)
8 */
9class Db
10{
11    public static $baseDSN = '';
12
13    public static function startExecuteSqlFile($file, $dbname = null, $verbose = true)
14    {
15        $pdo = self::startUsingDatabase($dbname, $verbose);
16        $startTime = microtime(true);
17
18        // Check if file is compressed or not
19        $isCompressed = (substr($file, -3) === '.gz');
20
21        if ($isCompressed) {
22            $sqlFile = gzopen($file, 'r');
23            $readFunction = 'gzgets';
24            $closeFunction = 'gzclose';
25        } else {
26            $sqlFile = fopen($file, 'r');
27            $readFunction = 'fgets';
28            $closeFunction = 'fclose';
29        }
30
31        if ($verbose) {
32            echo "Importing " . basename($file) . "\n";
33        }
34        $query = '';
35        while ($line = $readFunction($sqlFile)) {
36            $query .= $line;
37            if (preg_match('/;\s*$/', $line)) {
38                try {
39                    $pdo->exec($query);
40                    if ($verbose) {
41                        echo ".";
42                    }
43                    //echo "Successful:\n$query\n";
44                    $query = '';
45                } catch (\Exception $exception) {
46                    if ($verbose) {
47                        echo "Offending query: \n$query\n";
48                    }
49                    throw $exception;
50                }
51            }
52        }
53        $closeFunction($sqlFile);
54        $time = round(microtime(true) - $startTime, 3);
55        if ($verbose) {
56            echo "\nTook $time seconds\n";
57        }
58    }
59
60    public static function executeSql($query, $dbname = null)
61    {
62        $pdo = self::startUsingDatabase($dbname, false);
63        $pdo->exec($query);
64    }
65
66    public static function startUsingDatabase($dbname = null, $verbose = true): \BO\Zmsdb\Connection\Pdo
67    {
68        if (!self::$baseDSN) {
69            self::$baseDSN = \BO\Zmsdb\Connection\Select::$writeSourceName;
70        }
71        \BO\Zmsdb\Connection\Select::closeWriteConnection();
72        if ($dbname === null) {
73            $dbname =& \BO\Zmsdb\Connection\Select::$dbname_zms;
74            \BO\Zmsdb\Connection\Select::$writeSourceName = self::$baseDSN;
75        } else {
76            $dbname_zms =& \BO\Zmsdb\Connection\Select::$dbname_zms;
77            \BO\Zmsdb\Connection\Select::$writeSourceName =
78                preg_replace("#dbname=$dbname_zms.*?;#", "dbname=$dbname;", self::$baseDSN);
79        }
80
81        if ($verbose) {
82            error_log("Use Connection " . \BO\Zmsdb\Connection\Select::$writeSourceName);
83        }
84
85        $pdo = \BO\Zmsdb\Connection\Select::getWriteConnection();
86        return $pdo;
87    }
88
89    public static function startTestDataImport($fixtures, $filename = 'mysql_zmsbo.sql')
90    {
91        $dbname_zms =& \BO\Zmsdb\Connection\Select::$dbname_zms;
92
93        $pdo = self::startUsingDatabase('information_schema');
94        $pdo->exec("DROP DATABASE IF EXISTS `$dbname_zms`;");
95        $pdo->exec("CREATE DATABASE IF NOT EXISTS `$dbname_zms`;");
96
97        self::startExecuteSqlFile($fixtures . '/' . $filename);
98    }
99
100    public static function startConfigDataImport()
101    {
102        $defaults = new \BO\Zmsentities\Config();
103        (new \BO\Zmsdb\Config())->updateEntity($defaults);
104    }
105
106    public static function startMigrations($migrationList, $commit = true)
107    {
108        if (!is_array($migrationList)) {
109            $migrationList = glob($migrationList . '/*.sql');
110        }
111        sort($migrationList);
112        $pdo = self::startUsingDatabase();
113        $migrationsDoneList = $pdo->fetchPairs('SELECT filename, changeTimestamp FROM migrations');
114        $addedMigrations = 0;
115        foreach ($migrationList as $migrationFile) {
116            $migrationName = basename($migrationFile);
117            if (!array_key_exists($migrationName, $migrationsDoneList)) {
118                $addedMigrations++;
119                if (!$commit) {
120                    echo "$addedMigrations. Add migration $migrationName\n";
121                } else {
122                    self::startExecuteSqlFile($migrationFile);
123                    $pdo->prepare('INSERT INTO `migrations` SET `filename` = :filename')
124                        ->execute(['filename' => $migrationName]);
125                }
126            }
127        }
128        echo "\nFound " . count($migrationsDoneList) . " completed migrations and added $addedMigrations migrations.\n";
129        return $addedMigrations;
130    }
131
132    public static function executeTestData(string $testName, string $step)
133    {
134        $fixtures = realpath(__DIR__ . '/../../../tests/Zmsdb/fixtures/');
135        $sqlFile = $fixtures . '/' . $testName . '/' . $step . '.sql';
136
137        if (! file_exists($sqlFile)) {
138            return;
139        }
140
141        self::startExecuteSqlFile(
142            $sqlFile,
143            null,
144            false
145        );
146    }
147}