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 normalizeIdentifier(this.sqlEnv,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 } 142 143 public static String normalizeIdentifier(EDbVendor dbVendor, ESQLDataObjectType sqlDataObjectType, String identifier){ 144 return TSQLEnv.normalizeIdentifier(dbVendor,sqlDataObjectType,identifier); 145 } 146 147 148 /** 149 * how to compare the name of database object is various from different databases due to the different implementation. 150 * We need to consider the database vendor, the database object type when comparing the database object name. 151 * 152 * @param sqlEnv SQL environment 153 * @param sqlDataObjectType database object type 154 * @param targetDataObject instance of database object need to be compared 155 * @param objectName database object name 156 * @return 157 */ 158 public static int compareTo(TSQLEnv sqlEnv, ESQLDataObjectType sqlDataObjectType, TSQLObject targetDataObject, String objectName){ 159 return compareTo(sqlEnv.getDBVendor(),sqlDataObjectType,targetDataObject,objectName); 160 } 161 162 public static int compareTo(EDbVendor dbVendor, ESQLDataObjectType sqlDataObjectType, TSQLObject targetDataObject, String objectName){ 163 return compareTo(dbVendor,sqlDataObjectType,targetDataObject.getName() ,objectName); 164 } 165 166 /** 167 * @deprecated since version 2.2.3.2(2021-05-10), replaced by @TSQLEnv.compareIdentifier() 168 * 169 * @param dbVendor 170 * @param sqlDataObjectType 171 * @param pTargetObjectName 172 * @param objectName 173 * @return 174 */ 175 public static int compareTo(EDbVendor dbVendor, ESQLDataObjectType sqlDataObjectType, String pTargetObjectName, String objectName){ 176 177 if (TSQLEnv.compareIdentifier(dbVendor,sqlDataObjectType,pTargetObjectName,objectName)) return 0; 178 else return -1; 179 180// int ret = 0; 181// String normalizedName = normalizeIdentifier(dbVendor, sqlDataObjectType,objectName); 182// String targetObjectName = normalizeIdentifier(dbVendor, sqlDataObjectType,pTargetObjectName); 183// 184// switch (dbVendor){ 185// case dbvmssql: 186// ret = targetObjectName.compareToIgnoreCase(normalizedName); 187// break; 188// case dbvmysql: 189// if ((sqlDataObjectType == dotTable)||(sqlDataObjectType == dotTrigger)||(sqlDataObjectType == dotCatalog)){ 190// ret = targetObjectName.compareTo(normalizedName); 191// }else { 192// ret = targetObjectName.compareToIgnoreCase(normalizedName); 193// } 194// break; 195// case dbvpostgresql: 196// ret = targetObjectName.compareTo(normalizedName); 197// break; 198// case dbvoracle: 199// ret = targetObjectName.compareTo(normalizedName); 200// break; 201// case dbvdb2: 202// ret = targetObjectName.compareTo(normalizedName); 203// break; 204// default: 205// ret = targetObjectName.compareTo(normalizedName); 206// break; 207// } 208// return ret; 209 } 210 211}