001package gudusoft.gsqlparser.nodes; 002/* 003 * Date: 2010-9-25 004 * Time: 17:18:03 005 */ 006 007 008import gudusoft.gsqlparser.*; 009 010import java.util.HashMap; 011import java.util.Map; 012 013/** 014 * datatype attributes supported in various databases. 015 * method {@link #getValue_identifier()} returns a identifier value, 016 * mehtod {@link #getValue_literal()} return a value in type {@link TConstant} 017 */ 018public class TDatatypeAttribute extends TParseTreeNode { 019 020 private static Map<String, EDataTypeAttribute> dataTypeAttributeMap = new HashMap<String, EDataTypeAttribute>( ); 021 022 public static EDataTypeAttribute searchDataTypeAttributeByName(TSourceToken st1, TSourceToken st2){ 023 024 String lcstr = st1.toString(); 025 if ((st1.tokencode == TBaseType.rrw_not)||(st1.tokencode == TBaseType.rw_not1) // not null, not casespecific 026 ||(st1.tokencode == TBaseType.rrw_with) // with default, with time zone 027 ){ 028 // data attribute include more than one keyword, concate the first 2 keywords 029 if (st2 != null){ 030 lcstr = lcstr+"_"+st2.toString(); 031 }else{ 032 lcstr = lcstr+"_"; 033 } 034 } 035 return searchDataTypeAttributeByName(lcstr); 036 } 037 038 public static EDataTypeAttribute searchDataTypeAttributeByName(String typenameStr){ 039 040 if (dataTypeAttributeMap.size() == 0){ 041 dataTypeAttributeMap.put("not_null", EDataTypeAttribute.not_null_t); 042 dataTypeAttributeMap.put("uppercase", EDataTypeAttribute.uppercase_t); 043 dataTypeAttributeMap.put("casespecific", EDataTypeAttribute.casespecific_t); 044 dataTypeAttributeMap.put("not_casespecific", EDataTypeAttribute.not_casespecific_t); 045 dataTypeAttributeMap.put("format", EDataTypeAttribute.format_t); 046 dataTypeAttributeMap.put("title", EDataTypeAttribute.title_t); 047 dataTypeAttributeMap.put("named", EDataTypeAttribute.named_t); 048 dataTypeAttributeMap.put("default", EDataTypeAttribute.default_t); 049 dataTypeAttributeMap.put("with_default", EDataTypeAttribute.with_default_t); 050 dataTypeAttributeMap.put("with_time", EDataTypeAttribute.with_time_zone_t); 051 dataTypeAttributeMap.put("character_set", EDataTypeAttribute.character_set_t); 052 dataTypeAttributeMap.put("uc", EDataTypeAttribute.uppercase_t); 053 dataTypeAttributeMap.put("cs", EDataTypeAttribute.casespecific_t); 054 dataTypeAttributeMap.put("not_cs", EDataTypeAttribute.not_cs_t); 055// dataTypeAttributeMap.put("as_transactiontime", EDataTypeAttribute.as_transactiontime_t); 056// dataTypeAttributeMap.put("as_validtime", EDataTypeAttribute.as_validtime_t); 057// dataTypeAttributeMap.put("as_deferred", EDataTypeAttribute.as_deferred_t); 058 } 059 060 return dataTypeAttributeMap.get(typenameStr.toLowerCase()); 061 } 062 063 064 private EDataTypeAttribute attributeType = EDataTypeAttribute.unknown_t; 065 066 public void setAttributeType(EDataTypeAttribute attributeType) { 067 this.attributeType = attributeType; 068 } 069 070 public EDataTypeAttribute getAttributeType() { 071 072 return attributeType; 073 } 074 075 private String attributeValue; 076 077 public String getAttributeValue() { 078 return attributeValue; 079 } 080 081 public void init(Object arg1){ 082 attributeType = (EDataTypeAttribute)arg1; 083 } 084 085 private TObjectName value_identifier; 086 private TConstant value_literal; 087 private TExpressionList value_list; 088 089 public TExpressionList getValue_list() { 090 return value_list; 091 } 092 093 public TConstant getValue_literal() { 094 return value_literal; 095 } 096 097 public TObjectName getValue_identifier() { 098 return value_identifier; 099 } 100 101 private TExpression defaultValue; 102 103 public TExpression getDefaultValue() { 104 return defaultValue; 105 } 106 107 private TObjectName namedName; 108 109 public TObjectName getNamedName() { 110 return namedName; 111 } 112 113 public void init(Object arg1, Object arg2){ 114 init(arg1); 115 switch (attributeType){ 116 case default_value_t: 117 this.defaultValue = (TExpression)arg2; 118 break; 119 case named_t: 120 this.namedName = (TObjectName)arg2; 121 this.namedName.setDbObjectType(EDbObjectType.column_alias); 122 break; 123 case format_t: 124 case title_t: 125 // FORMAT and TITLE clauses store a string constant 126 if (arg2 instanceof TConstant){ 127 value_literal = (TConstant)arg2; 128 attributeValue = value_literal.toString(); 129 } 130 break; 131 default: 132 // Generic handling for other cases 133 if (arg2 instanceof TObjectName) { 134 value_identifier = (TObjectName)arg2; 135 attributeValue = value_identifier.toString(); 136 } else if (arg2 instanceof TConstant){ 137 value_literal = (TConstant)arg2; 138 attributeValue = value_literal.toString(); 139 } else if (arg2 instanceof TExpressionList){ 140 value_list = (TExpressionList)arg2; 141 } 142 break; 143 } 144 } 145 146 public static void setDataTypeAttributeMap(Map<String, EDataTypeAttribute> dataTypeAttributeMap) { 147 TDatatypeAttribute.dataTypeAttributeMap = dataTypeAttributeMap; 148 } 149 150 public void setAttributeValue(String attributeValue) { 151 this.attributeValue = attributeValue; 152 } 153 154 public void setValue_identifier(TObjectName value_identifier) { 155 this.value_identifier = value_identifier; 156 } 157 158 public void setValue_literal(TConstant value_literal) { 159 this.value_literal = value_literal; 160 } 161 162 public void setValue_list(TExpressionList value_list) { 163 this.value_list = value_list; 164 } 165 166 public void accept(TParseTreeVisitor v){ 167 v.preVisit(this); 168 v.postVisit(this); 169 } 170 171 public void acceptChildren(TParseTreeVisitor v){ 172 v.preVisit(this); 173 v.postVisit(this); 174 } 175}