setVendor(String)   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 2
b 0
f 0
dl 0
loc 14
ccs 0
cts 5
cp 0
crap 6
rs 9.85
1
/*
2
 * Copyright 2014, Armenak Grigoryan, and individual contributors as indicated
3
 * by the @authors tag. See the copyright.txt in the distribution for a
4
 * full listing of individual contributors.
5
 *
6
 * This is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation; either version 2.1 of
9
 * the License, or (at your option) any later version.
10
 *
11
 * This software is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
 * Lesser General Public License for more details.
15
 */
16
package com.strider.datadefender;
17
18
import java.util.List;
19
import java.util.Map;
20
import java.util.regex.Matcher;
21
import java.util.regex.Pattern;
22
import picocli.CommandLine.Option;
23
24
import lombok.Getter;
25
26
/**
27
 * Database configuration options for picocli.
28
 * 
29
 * @author Zaahid Bateson
30
 */
31
@Getter
32
public class DbConfig {
33
34
    public enum Vendor {
35
        H2, MYSQL, POSTGRESQL, SQLSERVER, ORACLE;
36
    }
37
38
    private static final Map<String, Vendor> VENDOR_MAP = Map.of(
39
        "h2", Vendor.H2,
40
        "mysql", Vendor.MYSQL,
41
        "mariadb", Vendor.MYSQL,
42
        "postgresql", Vendor.POSTGRESQL,
43
        "sqlserver", Vendor.SQLSERVER,
44
        "mssql", Vendor.SQLSERVER,
45
        "oracle", Vendor.ORACLE
46
    );
47
48
    private Vendor vendor;
49
50
    @Option(names = { "-u", "--user" }, description = "The username to connect with")
51
    private String username;
52
    
53
    @Option(names = { "-p", "--password" }, description = "The password to connect with", arity = "0..1", interactive = true)
54
    private String password;
55
56
    @Option(names = { "--schema" }, description = "The schema to connect to")
57
    private String schema;
58
59
    @Option(names = { "--no-skip-empty-tables-metadata" }, description = "Include generating metadata for empty tables (defaults to skipping)", defaultValue = "true", negatable = true)
60
    private boolean skipEmptyTables = true;
61
62
    @Option(names = { "--include-table-pattern-metadata" }, description = "Pattern(s) matching table names to include for metadata analysis")
63
    private List<Pattern> includeTablePatterns;
64
    @Option(names = { "--exclude-table-pattern-metadata" }, description = "Pattern(s) matching table names to exclude for metadata analysis")
65
    private List<Pattern> excludeTablePatterns;
66
67
    private String url;
68
69
    @Option(
70
        names = { "--vendor" },
71
        description = "Database vendor, available options are: h2, mysql, mariadb, postgresql, sqlserver, oracle. "
72
            + "If not specified, vendor will attempt to be extracted from the datasource url for a jdbc scheme."
73
    )
74
    public void setVendor(String vendor) {
75
        String c = vendor.trim().toLowerCase();
76
        if (!VENDOR_MAP.containsKey(c)) {
77
            throw new IllegalArgumentException(
78
                "Invalid value for option '--vendor': Valid options are: "
79
                + "h2, mysql, mariadb, postgresql, sqlserver and oracle."
80
            );
81
        }
82
        this.vendor = VENDOR_MAP.get(c);
83
    }
84
85
    @Option(names = { "--url" }, description = "The datasource URL")
86
    public void setUrl(String url) {
87
        Pattern p = Pattern.compile("\\s*jdbc:([^:]+):.*");
88
        if (vendor == null) {
89
            Matcher m = p.matcher(url);
90
            if (m.find()) {
91
                setVendor(m.group(1));
92
            }
93
        }
94
        this.url = url;
95
    }
96
}
97