/*
 * Copyright 2014, Armenak Grigoryan, and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 */
package com.strider.datadefender;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import picocli.CommandLine.Option;

import lombok.Getter;

/**
 * Database configuration options for picocli.
 * 
 * @author Zaahid Bateson
 */
@Getter
public class DbConfig {

    private String vendor;

    @Option(names = { "--schema" }, description = "The schema to connect to", required = false)
    private String schema;

    private String url;

    @Option(
        names = { "--vendor" },
        description = "Database vendor, available options are: h2, mysql, mariadb, postgresql, sqlserver, oracle. "
            + "If not specified, vendor will attempt to be extracted from the datasource url for a jdbc scheme.",
        required = false
    )
    public void setVendor(String vendor) {
        if (!StringUtils.equalsAny(vendor, "h2", "mysql", "mariadb", "postgresql", "mssql", "sqlserver", "oracle")) {
            throw new IllegalArgumentException(
                "Invalid value for option '--vendor': Valid options are: "
                + "h2, mysql, postgresql, mssql and oracle."
            );
        }
        if (vendor.equals("mariadb")) {
            this.vendor = "mysql";
        } else if (vendor.equals("mssql")) {
            // old versions of DataDefender used the name 'mssql'
            this.vendor = "sqlserver";
        } else {
            this.vendor = vendor;
        }
    }

    @Option(names = { "--url" }, description = "The datasource URL", required = true)
    public void setUrl(String url) {
        Pattern p = Pattern.compile("\\s*jdbc:([^:]+):.*");
        if (vendor == null) {
            Matcher m = p.matcher(url);
            if (m.find()) {
                setVendor(m.group(1));
            }
        }
        this.url = url;
    }
}

@@ -742,16 +742,38 @@
 ender;%0A%0A
+import java.util.Map;%0A
 import j
@@ -832,53 +832,8 @@
 rn;%0A
-import org.apache.commons.lang3.StringUtils;%0A
 impo
@@ -1010,29 +1010,420 @@
 %7B%0A%0A    p
-rivate String
+ublic enum Vendor %7B%0A        H2, MYSQL, POSTGRESQL, SQLSERVER, ORACLE%0A    %7D%0A%0A    private static final Map%3CString, Vendor%3E VENDOR_MAP = Map.of(%0A        %22h2%22, Vendor.H2,%0A        %22mysql%22, Vendor.MYSQL,%0A        %22mariadb%22, Vendor.MYSQL,%0A        %22postgresql%22, Vendor.POSTGRESQL,%0A        %22sqlserver%22, Vendor.SQLSERVER,%0A        %22mssql%22, Vendor.SQLSERVER,%0A        %22oracle%22, Vendor.ORACLE%0A    );%0A%0A    private Vendor
  vendor;
@@ -1934,113 +1934,85 @@
     
-if (!
 String
-Utils.equalsAny(vendor, %22h2%22, %22mysql%22, %22mariadb%22, %22postgresql%22, %22mssql%22, %22sqlserver%22, %22oracle%22
+ c = vendor.trim().toLowerCase();%0A        if (!VENDOR_MAP.containsKey(c
 )) %7B
@@ -2164,16 +2164,25 @@
 , mysql,
+ mariadb,
  postgre
@@ -2186,21 +2186,25 @@
 gresql, 
-ms
 sql
+server
  and ora
@@ -2246,285 +2246,40 @@
     
-if (vendor.equals(%22mariadb%22)) %7B%0A            this.vendor = %22mysql%22;%0A        %7D else if (vendor.equals(%22mssql%22)) %7B%0A            // old versions of DataDefender used the name 'mssql'%0A            this.vendor = %22sqlserver%22;%0A        %7D else %7B%0A            this.vendor = vendor;%0A        %7D
+this.vendor = VENDOR_MAP.get(c);
 %0A   

Use enum for vendor types