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