001package gudusoft.gsqlparser.stmt; 002 003import gudusoft.gsqlparser.*; 004import gudusoft.gsqlparser.compiler.TFrame; 005import gudusoft.gsqlparser.compiler.TVariable; 006import gudusoft.gsqlparser.nodes.*; 007 008import static gudusoft.gsqlparser.EDeclareType.variable; 009 010 011/** 012 * Represents declare statement that used to decalre variable, constant, exception and subtype. 013 * Or Signifies that the statement is a pragma (compiler directive), including exception_init_pragma, 014 * autonomous_transaction_pragma, restrict_references_pragma, serially_resuable_pragma and pragma_timestamp 015 */ 016 017public class TVarDeclStmt extends TCustomSqlStatement { 018 019 public TVarDeclStmt(){ 020 this(EDbVendor.dbvoracle); 021 } 022 023 public TVarDeclStmt(EDbVendor dbvendor){ 024 super(dbvendor); 025 sqlstatementtype = ESqlStatementType.sstplsql_vardecl ; 026 } 027 028 void buildsql() { 029 } 030 031 void clear() { 032 } 033 034 String getasprettytext() { 035 return ""; 036 } 037 038 void iterate(TVisitorAbs pvisitor) { 039 } 040 041 private TObjectName elementName = null; 042 private TTypeName dataType = null; 043 private boolean NotNull = false; 044 private TExpression value = null; 045 046 047 private int howtoSetValue = TBaseType.howtoSetValue_none; 048 049 /** 050 * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#variable} 051 */ 052 public final static int whatDeclared_variable = 1; 053 /** 054 * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#constant} 055 */ 056 public final static int whatDeclared_constant = 2; 057 /** 058 * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#exception} 059 */ 060 public final static int whatDeclared_exception = 3; 061 /** 062 * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#subtype} 063 */ 064 public final static int whatDeclared_subtype = 4; 065 /** 066 * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#pragma_exception_init} 067 */ 068 public final static int whatDeclared_pragma_exception_init = 5; 069 /** 070 * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#pragma_autonomous_transaction} 071 */ 072 public final static int whatDeclared_pragma_autonomous_transaction = 6; 073 /** 074 * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#pragma_serially_reusable} 075 */ 076 public final static int whatDeclared_pragma_serially_reusable = 7; 077 /** 078 * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#pragma_timestamp} 079 */ 080 public final static int whatDeclared_pragma_timestamp = 8; 081 /** 082 * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#pragma_restrict_references} 083 */ 084 public final static int whatDeclared_pragma_restrict_references = 9; 085 /** 086 * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#pragma_interface} 087 */ 088 public final static int whatDeclared_pragma_interface = 10; 089 090 public void setWhatDeclared(int whatDeclared) { 091 this.whatDeclared = whatDeclared; 092 } 093 094 /** 095 * Indicates what's kind of element this delcare statement declared such as variable, constant, exception, subtype and various pragma clause. 096 * @return 097 */ 098 public int getWhatDeclared() { 099 return whatDeclared; 100 } 101 102 /** 103 * How deafult value of a variable or constant was set. 104 * <p> := | DEFAULT 105 * <p> := {@link TBaseType#howtoSetValue_assign} 106 * <p> or DEFAULT keyword {@link TBaseType#howtoSetValue_default}; 107 * @return 108 */ 109 110 public int getHowtoSetValue() { 111 return howtoSetValue; 112 } 113 114 /** 115 * @deprecated As of v1.4.8.8, replaced by {@link #getDefaultValue()} 116 */ 117 public TExpression getValue() { 118 return value; 119 } 120 121 /** 122 * Specifies that no element can have the value NULL. 123 * @return 124 */ 125 public boolean getNotNull() { 126 return NotNull; 127 } 128 129 private int whatDeclared = whatDeclared_variable; 130 private EDeclareType declareType = variable; 131 private TObjectName collateName; 132 private TExpression defaultValue; 133 134 public void setDefaultValue(TExpression defaultValue) { 135 this.defaultValue = defaultValue; 136 } 137 138 /** 139 * Default value of a variable or constant that declared. 140 */ 141 public TExpression getDefaultValue() { 142 143 return defaultValue; 144 } 145 146 public void setCollateName(TObjectName collateName) { 147 this.collateName = collateName; 148 } 149 150 public TObjectName getCollateName() { 151 152 return collateName; 153 } 154 155 public void setDeclareType(EDeclareType declareType) { 156 this.declareType = declareType; 157 } 158 159 public EDeclareType getDeclareType() { 160 161 return declareType; 162 } 163 164 private TObjectName exception_name = null; 165 private TExpression error_number = null; 166 167 public void setError_number(TExpression error_number) { 168 this.error_number = error_number; 169 } 170 171 public void setException_name(TObjectName exception_name) { 172 this.exception_name = exception_name; 173 } 174 175 /** 176 * Any valid Oracle Database error number. Used when this class represents exception_init_pragma. 177 * @return 178 */ 179 public TExpression getError_number() { 180 return error_number; 181 } 182 183 /** 184 * Name of a user-defined exception declared within the current scope. Used when this class represents exception_init_pragma. 185 * @return 186 */ 187 188 public TObjectName getException_name() { 189 return exception_name; 190 } 191 192 private TObjectName aliasItem; 193 194 public void setAliasItem(TObjectName aliasItem) { 195 this.aliasItem = aliasItem; 196 } 197 198 public TObjectName getAliasItem() { 199 200 return aliasItem; 201 } 202 203 public void init(Object arg1){ 204 if (arg1 instanceof EDeclareType){ 205 this.declareType = (EDeclareType)arg1; 206 }else{ 207 elementName = (TObjectName)arg1; 208 elementName.setObjectType(TObjectName.ttobjVariable); 209 } 210 } 211 212 public void init(Object arg1,Object arg2){ 213 init(arg1); 214 switch (declareType){ 215 case exception: 216 elementName = (TObjectName)arg2; 217 break; 218 default: 219 if (arg2 != null){ 220 dataType = (TTypeName)arg2; 221 } 222 break; 223 } 224 } 225 226 public void init(Object arg1,Object arg2,Object arg3,Object arg4){ 227 228 init(arg1,arg2); 229 NotNull = (arg3 != null); 230 231 if (arg4 != null){ //tdummy 232 if (arg4 instanceof TDummy){ 233 value = (TExpression)((TDummy)arg4).node1; 234 defaultValue = value; 235 howtoSetValue = ((TDummy)arg4).int1; 236 }else if (arg4 instanceof TExpression){ 237 defaultValue = (TExpression)arg4; 238 } 239 } 240 } 241 242 /** 243 * Datatype of declared element. 244 * @return 245 */ 246 public TTypeName getDataType() { 247 return dataType; 248 } 249 250 251 public int doParseStatement(TCustomSqlStatement psql) { 252 super.doParseStatement(psql); 253 if (elementName != null){ 254 TFrame stackFrame = getFrameStack().peek(); 255 stackFrame.getScope().addSymbol(new TVariable(elementName,this)); 256 } 257 if (value != null){ 258 value.doParse(this,ESqlClause.unknown); 259 } 260 261 if (defaultValue != null){ 262 defaultValue.doParse(this,ESqlClause.unknown); 263 } 264 return 0; 265 } 266 267 /** 268 * Name of element that declared. 269 * @return 270 */ 271 public TObjectName getElementName() { 272 return elementName; 273 } 274 275 public void accept(TParseTreeVisitor v){ 276 v.preVisit(this); 277 v.postVisit(this); 278 } 279 280 public void acceptChildren(TParseTreeVisitor v){ 281 v.preVisit(this); 282 v.postVisit(this); 283 } 284 285 public void setElementName(TObjectName elementName) { 286 this.elementName = elementName; 287 } 288 289 public void setDataType(TTypeName dataType) { 290 this.dataType = dataType; 291 } 292 293 public void setNotNull(boolean notNull) { 294 NotNull = notNull; 295 } 296 297 public void setValue(TExpression value) { 298 this.value = value; 299 } 300 301 public void setHowtoSetValue(int howtoSetValue) { 302 this.howtoSetValue = howtoSetValue; 303 } 304}