001package gudusoft.gsqlparser;
002
003import java.util.ArrayList;
004import java.util.Arrays;
005import java.util.HashMap;
006import java.util.List;
007import java.util.Map;
008
009/**
010 * Database vendors supported:
011 *  <br>
012 * {@link #dbvmssql},{@link #dbvoracle},{@link #dbvmysql},{@link #dbvdb2},
013 * {@link #dbvsybase}, {@link #dbvinformix},{@link #dbvpostgresql},{@link #dbvteradata},
014 * {@link #dbvmdx},{@link #dbvnetezza},{@link #dbvhive},{@link #dbvgreenplum},{@link #dbvredshift}
015 * <br><br>
016 * No specific engine for Microsoft ACCESS {@link #dbvaccess}, use {@link #dbvmssql} instead.
017 * <br><br>
018 * Database engine not supported:
019 *<br>{@link #dbvgeneric},{@link #dbvfirebird},{@link #dbvansi},{@link #dbvodbc}
020 */
021public enum EDbVendor {
022    dbvaccess(new String[]{"access"}, "Microsoft Access", 0),
023    dbvansi(new String[]{"ansi"}, "ANSI SQL", 0),
024    dbvathena(new String[]{"athena"}, "AWS Athena", 1), //aws,2021/9/18
025    dbvazuresql(new String[]{"azuresql", "azure"}, "Azure SQL", 1),
026    dbvbigquery(new String[]{"bigquery"}, "Google BigQuery", 1),
027    dbvclickhouse(new String[]{"clickhouse"}, "ClickHouse", 1),
028    dbvcouchbase(new String[]{"couchbase"}, "Couchbase", 1),
029    dbvdax(new String[]{"dax"}, "AWS DAX", 0),
030    dbvdb2(new String[]{"db2"}, "IBM DB2", 1),
031    dbvdoris(new String[]{"doris"}, "Apache Doris", 1),
032    dbvexasol(new String[]{"exasol"}, "Exasol", 0),
033    dbvfirebird(new String[]{"firebird"}, "Firebird", 0),
034    dbvflink(new String[]{"flink"}, "Apache Flink SQL", 1),
035    dbvgeneric(new String[]{"generic"}, "Generic SQL", 0),
036    dbvgreenplum(new String[]{"greenplum"}, "Greenplum", 1),
037    dbvhana(new String[]{"hana"}, "SAP HANA", 1),
038    dbvhive(new String[]{"hive"}, "Apache Hive", 1),
039    dbvimpala(new String[]{"impala"}, "Apache Impala", 1),
040    dbvinformix(new String[]{"informix"}, "IBM Informix", 1),
041    dbvmdx(new String[]{"mdx"}, "MDX", 0),
042    dbvmysql(new String[]{"mysql"}, "MySQL", 1),
043    dbvmssql(new String[]{"mssql", "sqlserver"}, "Microsoft SQL Server", 1),
044    dbvnetezza(new String[]{"netezza"}, "IBM Netezza", 1),
045    dbvodbc(new String[]{"odbc"}, "ODBC", 1),
046    dbvopenedge(new String[]{"openedge"}, "Progress OpenEdge", 1),
047    dbvoracle(new String[]{"oracle"}, "Oracle", 1),
048    dbvpostgresql(new String[]{"postgresql", "postgres"}, "PostgreSQL", 1),
049    dbvpresto(new String[]{"presto"}, "Presto", 1), //2021/9/18
050    dbvredshift(new String[]{"redshift"}, "Amazon Redshift", 1),
051    dbvsnowflake(new String[]{"snowflake"}, "Snowflake", 1),
052    dbvsoql(new String[]{"soql"}, "Salesforce SOQL", 1),
053    dbvsparksql(new String[]{"sparksql", "spark"}, "Apache Spark SQL", 1),
054    dbvsqlite(new String[]{"sqlite"}, "SQLite", 1),
055    dbvstarrocks(new String[]{"starrocks"}, "StarRocks", 1),
056    dbvsybase(new String[]{"sybase"}, "Sybase", 1),
057    dbvteradata(new String[]{"teradata"}, "Teradata", 1),
058    dbvtrino(new String[]{"trino"}, "Trino", 1), //2021/9/18
059    dbvvertica(new String[]{"vertica"}, "Vertica", 1),
060    dbvdatabricks(new String[]{"databricks"}, "Databricks", 1), //2022/10/30
061    dbvgaussdb(new String[]{"gaussdb"}, "GaussDB", 1); //2023/9/28
062
063    private final String[] aliases;
064    private final String description;
065    private final int status; // 0 means not implemented, 1 means implemented
066    private static final Map<String, EDbVendor> aliasMap = new HashMap<>();
067
068    static {
069        for (EDbVendor vendor : values()) {
070            for (String alias : vendor.aliases) {
071                aliasMap.put(alias.toLowerCase(), vendor);
072            }
073        }
074    }
075
076    EDbVendor(String[] aliases, String description, int status) {
077        this.aliases = aliases;
078        this.description = description;
079        this.status = status;
080    }
081
082    /**
083     * Get the primary string representation of this database vendor
084     * @return the primary alias (first in the list)
085     */
086    public String getPrimaryAlias() {
087        return aliases[0];
088    }
089
090    /**
091     * Get all string representations of this database vendor
092     * @return array of all aliases
093     */
094    public String[] getAliases() {
095        return aliases;
096    }
097
098    /**
099     * Get the description of this database vendor
100     * @return the description
101     */
102    public String getDescription() {
103        return description;
104    }
105    
106    /**
107     * Get the implementation status of this database vendor
108     * @return 0 for not implemented, 1 for implemented
109     */
110    public int getStatus() {
111        return status;
112    }
113    
114    /**
115     * Check if this database vendor is implemented
116     * @return true if implemented, false otherwise
117     */
118    public boolean isImplemented() {
119        return status == 1;
120    }
121
122    /**
123     * Get the database vendor from a string representation
124     * @param alias the string representation
125     * @return the corresponding database vendor or null if not found
126     */
127    public static EDbVendor fromAlias(String alias) {
128        if (alias == null) {
129            return null;
130        }
131        return aliasMap.get(alias.toLowerCase());
132    }
133
134    /**
135     * Get the database vendor from a string representation, returning dbvoracle as default if not found
136     * @param alias the string representation
137     * @return the corresponding database vendor or dbvoracle if not found
138     */
139    public static EDbVendor valueOfWithDefault(String alias) {
140        EDbVendor vendor = fromAlias(alias);
141        return vendor != null ? vendor : dbvoracle;
142    }
143
144    /**
145     * Returns an array of strings representing all database vendors
146     * using their primary aliases (first alias), sorted alphabetically
147     * @return array of all primary database vendor aliases
148     */
149    public static String[] getAllVendorAliases() {
150        return getAllVendorAliases(false);
151    }
152    
153    /**
154     * Returns an array of strings representing database vendors
155     * using their primary aliases (first alias), sorted alphabetically
156     * @param implementedOnly if true, only implemented vendors are included
157     * @return array of primary database vendor aliases
158     */
159    public static String[] getAllVendorAliases(boolean implementedOnly) {
160        EDbVendor[] allVendors = values();
161        List<String> aliasList = new ArrayList<>();
162        
163        for (EDbVendor vendor : allVendors) {
164            if (!implementedOnly || vendor.isImplemented()) {
165                aliasList.add(vendor.getPrimaryAlias());
166            }
167        }
168        
169        String[] aliases = aliasList.toArray(new String[0]);
170        Arrays.sort(aliases);
171        return aliases;
172    }
173
174    /**
175     * Check if a string representation corresponds to this database vendor
176     * @param alias the string to check
177     * @return true if the string represents this vendor, false otherwise
178     */
179    public boolean matchesAlias(String alias) {
180        if (alias == null) {
181            return false;
182        }
183        return Arrays.asList(getAliases()).contains(alias.toLowerCase());
184    }
185}