001package gudusoft.gsqlparser.sqlenv;
002
003import gudusoft.gsqlparser.EDbVendor;
004
005import static gudusoft.gsqlparser.sqlenv.ESQLDataObjectType.*;
006
007/**
008 * base class of all the database object such as catalog, schema, table, column and etc
009 */
010public abstract class TSQLObject {
011    protected String name = null;
012    protected String nameKeepCase = null;
013
014    protected TSQLEnv sqlEnv;
015
016    /**
017     * SQL environment where this database object belonged to.
018     *
019     * @return SQL environment
020     */
021    public TSQLEnv getSqlEnv() {
022        return sqlEnv;
023    }
024
025
026    /**
027     * create a new database object and add to the SQL environment.
028     *
029     * @param sqlEnv SQL environment
030     * @param objectName object name
031     */
032    public TSQLObject(TSQLEnv sqlEnv, String objectName, ESQLDataObjectType dataObjectType){
033        this.sqlEnv = sqlEnv;
034        this.nameKeepCase = objectName;
035        this.name = normalizeIdentifier(objectName);
036        this.dataObjectType = dataObjectType;
037    }
038
039    /**
040     * name of this database object. With the case unchanged.
041     * @return database object name
042     */
043    public String getNameKeepCase() {
044        return nameKeepCase;
045    }
046
047    /**
048     * name of this database object. case of the name was normalized according to the database vendor's implementation.
049     * If the name was picked up from a SQL script, it's case maybe changed and not the same as it appeared in the SQL script.
050     *
051     * @return name
052     */
053    public String getName(){
054        return name;
055    }
056
057    public void setDataObjectType(ESQLDataObjectType dataObjectType) {
058        this.dataObjectType = dataObjectType;
059    }
060
061    protected ESQLDataObjectType dataObjectType = dotUnknown;
062
063    /**
064     * Type of this database object
065     *
066     * @return database object type
067     */
068    public ESQLDataObjectType getDataObjectType(){
069        return dataObjectType;
070    }
071
072    /**
073     * qualified name for database object such as: databaseName.schemaName.tableName
074     * @return qualified name
075     */
076    public String getQualifiedName(){
077        return "";
078    }
079
080
081    /**
082     * whether is this a delimited identifier.
083     *
084     * @param identifier name of a database object
085     * @return true if it's a delimited/quoted identifer.
086     */
087    public boolean isDelimitedIdentifier(String identifier){
088        return  isDelimitedIdentifier(this.sqlEnv,identifier);
089    }
090
091    /**
092     * Change the case of the name of database object.
093     *
094     * @param identifier database object name
095     * @return normalized database object name
096     */
097    public String normalizeIdentifier(String identifier){
098        return this.sqlEnv.normalizeIdentifier(this.dataObjectType,identifier);
099    }
100
101    /**
102     * compare the name of this object to objectname with carefully handle of the case [in]sensitive.
103     *
104     * @param objectName object name need to be compared
105     * @return a negative integer, zero, or a positive integer as the
106     *         specified String is greater than, equal to, or less than this object name
107     *
108     */
109    public int compareTo(String objectName){
110        return compareTo(this.sqlEnv,this.getDataObjectType(),this,objectName);
111    }
112
113    /**
114     * check whether a database object name is a delimited identifier or not based on the database.
115     *
116     * @param sqlEnv SQL environment
117     * @param identifier database object name
118     * @return true if it's delimited/quoted identifier
119     */
120    public static boolean isDelimitedIdentifier(TSQLEnv sqlEnv, String identifier){
121        return TSQLEnv.isDelimitedIdentifier(sqlEnv.getDBVendor(),identifier);
122    }
123
124
125    /**
126     * change the case of the name of the database object name, and remove the delimited char if it's a delimited identifier.
127     * <br>
128     * the case of database object name was changed when it's saved in INFORMATION_SCHEMA.
129     * In oracle, db2, it's changed to uppercase and in the PostgreSQL, it's changed to lowercase.
130     * case of the delimited identifier is unchanged when saved to the INFORMATION_SCHEMA.
131     * <br>So, the implementation is vendor dependent, this method will change the case of database object name accordingly
132     * based on the implementation of different database vendor.
133     *
134     * @param sqlEnv SQL environment
135     * @param sqlDataObjectType database object type, such as procedure, table, column
136     * @param identifier name of the database object
137     * @return normalized name of the database object
138     */
139//    public static String normalizeIdentifier(TSQLEnv sqlEnv, ESQLDataObjectType sqlDataObjectType, String identifier){
140//        //return normalizeIdentifier(sqlEnv.getDBVendor(),sqlDataObjectType,identifier);
141//        return sqlEnv.normalizeIdentifier(sqlDataObjectType,identifier);
142//    }
143
144//    public static String normalizeIdentifier(EDbVendor dbVendor, ESQLDataObjectType sqlDataObjectType, String identifier){
145//        return TSQLEnv.normalizeIdentifier(dbVendor,sqlDataObjectType,identifier);
146//    }
147
148
149    /**
150     * how to compare the name of database object is various from different databases due to the different implementation.
151     * We need to consider the database vendor, the database object type when comparing the database object name.
152     *
153     * @param sqlEnv  SQL environment
154     * @param sqlDataObjectType database object type
155     * @param targetDataObject instance of database object need to be compared
156     * @param objectName database object name
157     * @return
158     */
159    public static int compareTo(TSQLEnv sqlEnv, ESQLDataObjectType sqlDataObjectType, TSQLObject targetDataObject, String objectName){
160        return compareTo(sqlEnv.getDBVendor(),sqlDataObjectType,targetDataObject,objectName);
161    }
162
163    public static int compareTo(EDbVendor dbVendor, ESQLDataObjectType sqlDataObjectType, TSQLObject targetDataObject, String objectName){
164        return compareTo(dbVendor,sqlDataObjectType,targetDataObject.getName() ,objectName);
165    }
166
167    /**
168     * @deprecated since version 2.2.3.2(2021-05-10), replaced by @TSQLEnv.compareIdentifier()
169     *
170     * @param dbVendor
171     * @param sqlDataObjectType
172     * @param pTargetObjectName
173     * @param objectName
174     * @return
175     */
176    public static int compareTo(EDbVendor dbVendor, ESQLDataObjectType sqlDataObjectType, String pTargetObjectName, String objectName){
177
178        if (TSQLEnv.compareIdentifier(dbVendor,sqlDataObjectType,pTargetObjectName,objectName)) return 0;
179        else return -1;
180
181//        int ret = 0;
182//        String normalizedName = normalizeIdentifier(dbVendor, sqlDataObjectType,objectName);
183//        String targetObjectName = normalizeIdentifier(dbVendor, sqlDataObjectType,pTargetObjectName);
184//
185//        switch (dbVendor){
186//            case dbvmssql:
187//                ret =  targetObjectName.compareToIgnoreCase(normalizedName);
188//                break;
189//            case dbvmysql:
190//                if ((sqlDataObjectType == dotTable)||(sqlDataObjectType == dotTrigger)||(sqlDataObjectType == dotCatalog)){
191//                    ret = targetObjectName.compareTo(normalizedName);
192//                }else {
193//                    ret = targetObjectName.compareToIgnoreCase(normalizedName);
194//                }
195//                break;
196//            case dbvpostgresql:
197//                ret =  targetObjectName.compareTo(normalizedName);
198//                break;
199//            case dbvoracle:
200//                ret =  targetObjectName.compareTo(normalizedName);
201//                break;
202//            case dbvdb2:
203//                ret =  targetObjectName.compareTo(normalizedName);
204//                break;
205//            default:
206//                ret =  targetObjectName.compareTo(normalizedName);
207//                break;
208//        }
209//        return ret;
210    }
211
212}