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