KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > expression > expressionprimary > nullIffunction


1 package com.daffodilwoods.daffodildb.server.sql99.expression.expressionprimary;
2 import com.daffodilwoods.database.resource.*;
3 import com.daffodilwoods.daffodildb.server.sql99.token.*;
4 import com.daffodilwoods.daffodildb.server.sql99.utils._Reference;
5 import com.daffodilwoods.daffodildb.server.serversystem._ServerSession;
6 import com.daffodilwoods.daffodildb.server.sql99.common.ColumnDetails;
7 import com.daffodilwoods.daffodildb.server.sql99.common.ParameterInfo;
8 import com.daffodilwoods.daffodildb.server.sql99.utils.ByteComparison;
9 import com.daffodilwoods.daffodildb.server.sql99.expression.stringvalueexpression.*;
10 import com.daffodilwoods.daffodildb.server.sql99.expression.*;
11 import com.daffodilwoods.daffodildb.utils.field.*;
12 import com.daffodilwoods.daffodildb.server.sql99.common.*;
13 import com.daffodilwoods.daffodildb.server.sql99.expression.rowvalueexpression.*;
14 import com.daffodilwoods.daffodildb.utils.*;
15
16 public class nullIffunction extends AbstractGeneralFunction implements com.daffodilwoods.daffodildb.utils.parser.StatementExecuter ,generalvaluespecification {
17
18      public Srightparen_1874859514 _Srightparen_18748595140 ;
19      public valueexpression _valueexpression1 ;
20      public Scomma94843605 _Scomma948436052 ;
21          public valueexpression _valueexpression3 ;
22      public Sleftparen653880241 _Sleftparen6538802414 ;
23      public SNONRESERVEDWORD136444255 _SNONRESERVEDWORD1364442555 ;
24
25        private int resultType = -1;
26        private int type1 = -1;
27        private int type2 = -1;
28
29       /*
30        The NULLIF function compares two values and does the following.
31     Returns null if both values are the same.
32     Returns the first value if both values are different.
33         E.g.: Returning the credits available for customers. The query below will return null if the TOTAL_CREDITS column is equal to the CREDITS_USED column for a customer, else it will return the TOTAL_CREDITS value.
34
35         select customer_name, nullif(total_credit, credits_used)from customer_credits;
36
37         E.g.: The above example will return the same result as the statement below with CASE expression.
38         select customer_name, case when total_credits = credits_used then null
39          else total_credits
40          end
41         from customer_credits;
42        */

43
44       public Object JavaDoc run(Object JavaDoc object) throws com.daffodilwoods.database.resource.DException {
45       if (resultType == -1) {
46          resultType = getAppropriateDatatype(object);
47       }
48       FieldBase result1 = (FieldBase) _valueexpression3.run(object);
49       if (result1.isNull()) {
50         return new FieldStringLiteral(FieldUtility.NULLBUFFERRANGE, CHARACTER);
51       }
52       FieldBase result2 = (FieldBase) _valueexpression1.run(object);
53       if (result2.isNull()) {
54         return new FieldStringLiteral(FieldUtility.NULLBUFFERRANGE, CHARACTER);
55       }
56       type1 =getDataType(result1);
57       type2 =getDataType(result1);
58       try {
59         Check.checkForDatatype(type1, type2);
60       }
61       catch (DException ex) {
62         if (ex.getDseCode().equalsIgnoreCase("DSE514") ||
63             ex.getDseCode().equalsIgnoreCase("DSE6001")) {
64            throw new DException("DSE8100", new Object JavaDoc[] {StaticClass.getDataTypeName(type1),
65                                 StaticClass.getDataTypeName(type2)});
66         }
67         throw ex;
68       }
69         return getResult(result1 ,result2);
70    }
71
72    public ColumnDetails[] getChildColumnDetails() throws DException {
73       ColumnDetails[] cd1 = _valueexpression3.getColumnDetails();
74       ColumnDetails[] cd2 = _valueexpression1.getColumnDetails();
75       return GeneralPurposeStaticClass.getCombinedCDArray(cd1, cd2);
76    }
77
78
79    public AbstractRowValueExpression[] getChilds() {
80       AbstractRowValueExpression[] childs = new AbstractRowValueExpression[] { (AbstractRowValueExpression) (_valueexpression3), (AbstractRowValueExpression) (_valueexpression1)};
81       return childs;
82    }
83
84
85
86         public String JavaDoc toString() {
87     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
88     sb.append(" ");
89     sb.append(_SNONRESERVEDWORD1364442555);
90     sb.append(" ");
91     sb.append(_Sleftparen6538802414);
92     sb.append(" ");
93     sb.append(_valueexpression3);
94     sb.append(" ");
95     sb.append(_Scomma948436052);
96     sb.append(" ");
97     sb.append(_valueexpression1);
98     sb.append(" ");
99     sb.append(_Srightparen_18748595140);
100     return sb.toString();
101 }
102
103       public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
104           nullIffunction tempClass = new nullIffunction();
105           tempClass._Srightparen_18748595140 = (Srightparen_1874859514) _Srightparen_18748595140.clone();
106           tempClass._valueexpression1 = (valueexpression)_valueexpression1.clone();
107           tempClass._Scomma948436052 = (Scomma94843605) _Scomma948436052.clone();
108           tempClass._valueexpression3 = (valueexpression) _valueexpression3.clone();
109           tempClass._Sleftparen6538802414 = (Sleftparen653880241) _Sleftparen6538802414.clone();
110           tempClass._SNONRESERVEDWORD1364442555 = (SNONRESERVEDWORD136444255) _SNONRESERVEDWORD1364442555.clone();
111           return tempClass;
112        }
113
114        public ParameterInfo[] getParameterInfo() throws DException {
115         /* ParameterInfo[] parameterInfo1 = _valueexpression3.getParameterInfo();
116           ParameterInfo[] parameterInfo2 = _valueexpression1.getParameterInfo();
117           ParameterInfo[] parameterInfo3 = new ParameterInfo[parameterInfo1.length + parameterInfo2.length];
118           int index = 0;
119           for (; index < parameterInfo1.length; ++index) {
120              parameterInfo3[index] = parameterInfo1[index];
121           }
122           for (int i = 0; i < parameterInfo2.length; ++i, ++index) {
123              parameterInfo3[index] = parameterInfo2[i];
124           }
125           return parameterInfo3;*/

126          return new ParameterInfo[0];
127        }
128
129
130        public ByteComparison getByteComparison(Object JavaDoc object) throws DException {
131           int dt = resultType == -1 ? getAppropriateDatatype(object) : resultType;
132          ByteComparison byteComparison = new ByteComparison(false, new int[] {dt});
133          byteComparison.setSize(getColumnSize(object));
134          return byteComparison;
135        }
136
137
138        private int getAppropriateDatatype(Object JavaDoc object) throws DException {
139           type1 = getDataTypeForByte(_valueexpression3.getByteComparison(object));
140           type2 = getDataTypeForByte(_valueexpression1.getByteComparison(object));
141           try {
142              Check.checkForDatatype(type1, type2);
143           } catch (DException ex) {
144              if (ex.getDseCode().equalsIgnoreCase("DSE514") ||
145                  ex.getDseCode().equalsIgnoreCase("DSE6001")) {
146                 throw new DException("DSE8100", new Object JavaDoc[] {StaticClass.getDataTypeName(type1),
147                                      StaticClass.getDataTypeName(type2)});
148              }
149              throw ex;
150           }
151           return (type1 > type2) ? type1 : type2;
152        }
153
154     /* private FieldBase getResult(FieldBase expression1,FieldBase expression2, int type1,int type2) throws DException {
155         switch (type1) {
156           case BYTE:
157           case TINYINT:
158               switch(type2) {
159                 case BYTE:
160                 case TINYINT:
161                  if(expression1.equals(expression2)) {
162                   return new FieldStringLiteral(FieldUtility.NULLBUFFERRANGE, BYTE);
163                 } else {
164                   return new FieldStringLiteral((expression1.getObject()).toString(), BYTE);
165                 }
166                 default:
167                 throw new DException("DSE514", new Object[] {new Integer(type2)});
168               }
169           case SHORT:
170           case SMALLINT:
171             switch(type2) {
172               case SHORT:
173               case SMALLINT:
174                 if(expression1.equals(expression2)) {
175                  return new FieldLiteral(FieldUtility.NULLBUFFERRANGE, SHORT);
176                } else {
177                  return new FieldLiteral((expression1.getObject()).toString(), SHORT);
178                }
179                default:
180               throw new DException("DSE514", new Object[] {StaticClass.getDataTypeName(type2)});
181              }
182           case INTEGER:
183           case INT:
184             switch(type2) {
185               case INTEGER:
186               case INT:
187                if(expression1.equals(expression2)) {
188                 return new FieldLiteral(FieldUtility.NULLBUFFERRANGE, INTEGER);
189               } else {
190                 return new FieldLiteral((expression1.getObject()).toString(), INTEGER);
191               }
192               default:
193               throw new DException("DSE514", new Object[] {StaticClass.getDataTypeName(type2)});
194             }
195           case LONG:
196           case BIGINT:
197             switch(type2) {
198               case LONG:
199               case BIGINT:
200             if(expression1.equals(expression2)) {
201              return new FieldLiteral(FieldUtility.NULLBUFFERRANGE, LONG);
202            } else {
203              return new FieldLiteral((expression1.getObject()).toString(), LONG);
204            }
205            default:
206            throw new DException("DSE514", new Object[] {StaticClass.getDataTypeName(type2)});
207          }
208          case REAL:
209            switch(type2) {
210              case REAL:
211            if(expression1.equals(expression2)) {
212             return new FieldLiteral(FieldUtility.NULLBUFFERRANGE, FLOAT);
213           } else {
214             return new FieldLiteral((expression1.getObject()).toString(), FLOAT);
215           }
216           default:
217           throw new DException("DSE514", new Object[] {StaticClass.getDataTypeName(type2)});
218         }
219          case DOUBLE:
220          case FLOAT:
221          case DOUBLEPRECISION:
222            switch(type2) {
223              case DOUBLE:
224              case FLOAT:
225              case DOUBLEPRECISION:
226           if(expression1.equals(expression2)) {
227            return new FieldLiteral(FieldUtility.NULLBUFFERRANGE, DOUBLE);
228          } else {
229            return new FieldLiteral((expression1.getObject()).toString(), DOUBLE);
230          }
231          default:
232          throw new DException("DSE514", new Object[] {StaticClass.getDataTypeName(type2)});
233        }
234
235          case BIGDECIMAL:
236          case DEC:
237          case DECIMAL:
238          case NUMERIC:
239            switch(type2) {
240              case BIGDECIMAL:
241             case DEC:
242             case DECIMAL:
243             case NUMERIC:
244          if(expression1.equals(expression2)) {
245           return new FieldLiteral(FieldUtility.NULLBUFFERRANGE, BIGDECIMAL);
246           } else {
247            return new FieldLiteral((expression1.getObject()).toString(), BIGDECIMAL);
248           }
249           default:
250            throw new DException("DSE514", new Object[] {StaticClass.getDataTypeName(type2)});
251           }
252          case CHARACTER:
253          case VARCHAR:
254          case CHAR:
255          case CHARACTERVARYING:
256            switch(type2) {
257              case CHARACTER:
258             case VARCHAR:
259             case CHAR:
260             case CHARACTERVARYING:
261         if(expression1.equals(expression2)) {
262          return new FieldLiteral(FieldUtility.NULLBUFFERRANGE, CHARACTER);
263          } else {
264           return new FieldLiteral((expression1.getObject()).toString(), CHARACTER);
265          }
266          default:
267           throw new DException("DSE514", new Object[] {StaticClass.getDataTypeName(type2)});
268          }
269          case DATE:
270            switch(type2) {
271          case DATE:
272            if(expression1.equals(expression2)) {
273            return new FieldLiteral(FieldUtility.NULLBUFFERRANGE, DATE);
274           } else {
275           return new FieldLiteral((expression1.getObject()).toString(), DATE);
276           }
277           default:
278            throw new DException("DSE514", new Object[] {StaticClass.getDataTypeName(type2)});
279          }
280          case TIME:
281            switch(type2) {
282              case TIME:
283                if(expression1.equals(expression2)) {
284                  return new FieldLiteral(FieldUtility.NULLBUFFERRANGE, TIME);
285                } else {
286                  return new FieldLiteral((expression1.getObject()).toString(), TIME);
287                }
288              default:
289                throw new DException("DSE514", new Object[] {StaticClass.getDataTypeName(type2)});
290            }
291          case TIMESTAMP:
292            switch(type2) {
293             case TIMESTAMP:
294               if(expression1.equals(expression2)) {
295                 return new FieldLiteral(FieldUtility.NULLBUFFERRANGE, TIMESTAMP);
296               } else {
297                 return new FieldLiteral((expression1.getObject()).toString(), TIMESTAMP);
298               }
299             default:
300               throw new DException("DSE514", new Object[] {StaticClass.getDataTypeName(type2)});
301           }
302            default:
303               throw new DException("DSE514", new Object[] {StaticClass.getDataTypeName(type2)});
304         }
305      } */

306
307
308       private FieldBase getResult(FieldBase expression1,FieldBase expression2) throws DException {
309         GetByteComparator gbc =new GetByteComparator();
310         if (gbc.objectComparator.compare(expression1,expression2)==0) {
311               return new FieldStringLiteral(FieldUtility.NULLBUFFERRANGE, resultType);
312               } else {
313                  return expression1;
314              }
315        }
316
317
318        public _Reference[] checkSemantic(_ServerSession parent) throws DException {
319           _Reference[] ref = super.checkSemantic(parent);
320           getAppropriateDatatype(parent);
321           return ref;
322        }
323
324
325        int getColumnSize(Object JavaDoc object) throws DException {
326   ColumnDetails[] columnDetails = getChildColumnDetails();
327   int type =columnDetails[0].getType();
328   if(type!=TypeConstants.CONSTANT) {
329     int datatype = columnDetails[0].getDatatype();
330      switch (datatype) {
331        case BYTE:
332        case TINYINT:
333        case INTEGER:
334        case INT:
335          return INTSIZE;
336        case REAL:
337          return FLOATSIZE;
338        case DOUBLE:
339        case FLOAT:
340        case DOUBLEPRECISION:
341         return DOUBLESIZE;
342        case LONG:
343        case BIGINT:
344          return LONGSIZE;
345        case SHORT:
346        case SMALLINT:
347          return SHORTSIZE;
348        case BIGDECIMAL:
349        case DEC:
350        case DECIMAL:
351        case NUMERIC:
352          if (columnDetails[0].getSize() != -5) {
353            return columnDetails[0].getSize();
354          }
355          break;
356        case DATE :
357         return DATESIZE;
358        case TIME :
359          return TIMESIZE;
360        case TIMESTAMP :
361          return TIMESTAMPSIZE;
362          default :
363          if (columnDetails[0].getSize() != -5) {
364            return columnDetails[0].getSize();
365          }
366         break;
367      }
368    } else if(columnDetails[0].getQuestion()) {
369        return 255;
370    } else {
371      FieldBase field = (FieldBase)_valueexpression3.run(object);
372      field.setDatatype(columnDetails[0].getDatatype());
373      return field.getLength();
374    }
375    return -1;
376    }
377
378
379 }
380
Popular Tags