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