001package gudusoft.gsqlparser.stmt.teradata.utilities;
002
003
004public enum TeradataUtilityType {
005    BTEQ(".btq", "."),           // Basic Teradata Query
006    FASTEXPORT(".fexp", ""),     // FastExport
007    FASTLOAD(".fld", ""),        // FastLoad
008    MULTILOAD(".mld", "");       // MultiLoad
009
010    /**
011     * Each Teradata MultiLoad command must
012     *   • Begin on a new line
013     *   • Start with a period (.) character
014     *   • End with a semicolon (;) character
015     */
016
017     /**
018      * The following syntax rules apply when using Teradata FastLoad commands:
019      *   • Commands may begin with a period, but do not have to begin with a period.
020      *   • If there is no leading period, then there must be a semicolon at the end.
021      *   • If the command has a leading period, it must all be on one line. Commands that begin with a period cannot span multiple lines.
022      *   • Although a semicolon character should always be used to signify the end of a Teradata FastLoad command, it is not required for simple commands such as SHOW and SESSIONS.
023      *   • The ending semicolon character is optional for Teradata FastLoad commands that both begin with a period and are specified on a single line.
024      *   • Teradata SQL statements must not have a leading period and must always end with a semicolon.
025      *   • If the command contains unbalanced double or single quotes, the command must be started with a period and on a single line. It can terminate with semi-colon (;).
026      */
027
028     /**
029     * Each FastExport command must:
030     *   • Begin on a new line
031     *   • Start with a period (.) character
032     *   • End with a semicolon (;) character
033     * Each command can continue for as many lines as necessary, as long as it satisfies the beginning and
034     * ending requirements.
035     */
036
037
038    private final String fileExtension;
039    private final String commandPrefix;
040
041    TeradataUtilityType(String fileExtension, String commandPrefix) {
042        this.fileExtension = fileExtension;
043        this.commandPrefix = commandPrefix;
044    }
045
046    public String getFileExtension() {
047        return fileExtension;
048    }
049
050    public String getCommandPrefix() {
051        return commandPrefix;
052    }
053
054    public static TeradataUtilityType determineType(String command) {
055        if (command == null || command.trim().isEmpty()) {
056            return null;
057        }
058
059        String trimmedCommand = command.trim().toUpperCase();
060
061        // Check by command prefix
062        if (trimmedCommand.startsWith(".")) {
063            return BTEQ;
064        }
065
066        // Check by initialization commands
067        if (trimmedCommand.startsWith("BEGIN EXPORT")) {
068            return FASTEXPORT;
069        }
070        if (trimmedCommand.startsWith("BEGIN LOADING")) {
071            return FASTLOAD;
072        }
073        if (trimmedCommand.startsWith("BEGIN MLOAD") || 
074            trimmedCommand.startsWith("BEGIN IMPORT MLOAD") || 
075            trimmedCommand.startsWith("BEGIN DELETE MLOAD")) {
076            return MULTILOAD;
077        }
078
079        return null;  // Cannot determine type from command alone
080    }
081
082    public static TeradataUtilityType determineTypeFromFile(String filename) {
083        if (filename == null || filename.trim().isEmpty()) {
084            return null;
085        }
086
087        String lowerFilename = filename.toLowerCase();
088        for (TeradataUtilityType type : values()) {
089            if (lowerFilename.endsWith(type.getFileExtension())) {
090                return type;
091            }
092        }
093
094        return null;  // Unknown file extension
095    }
096}