Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
DatabaseUtility
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
3 / 3
8
100.00% covered (success)
100.00%
1 / 1
 isSqlite
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isSqliteMemoryMode
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
 getSqliteFilePath
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\Core\Database;
6
7use PeServer\Core\Text;
8use PeServer\Core\Throws\ArgumentException;
9use PeServer\Core\Throws\InvalidOperationException;
10
11/**
12 * DB処理的な共通処理。
13 */
14abstract class DatabaseUtility
15{
16    /**
17     * 接続設定は SQLite か。
18     * @param ConnectionSetting $connection 接続設定。
19     * @return bool SQLite か。
20     */
21    public static function isSqlite(ConnectionSetting $connection): bool
22    {
23        return Text::startsWith($connection->dsn, 'sqlite:', false);
24    }
25
26    /**
27     * 接続設定は SQLite のインメモリか。
28     * @param ConnectionSetting $connection 接続設定。
29     * @return bool インメモリか。
30     * @throws InvalidOperationException 接続設定は SQLite ではない。
31     */
32    public static function isSqliteMemoryMode(ConnectionSetting $connection): bool
33    {
34        if (!self::isSqlite($connection)) {
35            throw new InvalidOperationException();
36        }
37
38        return
39            Text::startsWith($connection->source, ':memory:', false)
40            ||
41            (
42                Text::contains($connection->source, '?', false)
43                &&
44                Text::contains($connection->source, 'mode=memory', false)
45            );
46    }
47
48    /**
49     * 接続設定から SQlite データベースファイルを取得する。
50     * @param ConnectionSetting $connection 接続設定。
51     * @return string データベースファイルパス。
52     * @throws ArgumentException 接続設定は SQLite ではない。
53     * @throws InvalidOperationException ファイルデータベースではない。
54     */
55    public static function getSqliteFilePath(ConnectionSetting $connection): string
56    {
57        if (!self::isSqlite($connection)) {
58            throw new ArgumentException();
59        }
60
61        if (self::isSqliteMemoryMode($connection)) {
62            throw new InvalidOperationException();
63        }
64
65        return $connection->source;
66    }
67
68    #endregion
69}