KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > expression > stringvalueexpression > repeatfunction


1 package com.daffodilwoods.daffodildb.server.sql99.expression.
2     stringvalueexpression;
3
4 import java.math.*;
5
6 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*;
7 import com.daffodilwoods.daffodildb.server.serversystem.*;
8 import com.daffodilwoods.daffodildb.server.sql99.common.*;
9 import com.daffodilwoods.daffodildb.server.sql99.expression.numericvalueexpression.*;
10 import com.daffodilwoods.daffodildb.server.sql99.expression.rowvalueexpression.*;
11 import com.daffodilwoods.daffodildb.server.sql99.token.*;
12 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
13 import com.daffodilwoods.daffodildb.utils.*;
14 import com.daffodilwoods.daffodildb.utils.field.*;
15 import com.daffodilwoods.database.resource.*;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Arrays JavaDoc;
18 import in.co.daffodil.db.jdbc.DatabaseProperties;
19
20 public class repeatfunction extends AbstractStringValueExpression implements charactervaluefunction {
21    public Srightparen_1874859514 _Srightparen_18748595140;
22    public numericvalueexpression _numericvalueexpression1;
23    public Scomma94843605 _Scomma948436052;
24    public charactervalueexpression _charactervalueexpression3;
25    public Sleftparen653880241 _Sleftparen6538802414;
26    public SNONRESERVEDWORD136444255 _SNONRESERVEDWORD1364442555;
27
28
29    public Object JavaDoc run(Object JavaDoc object) throws com.daffodilwoods.database.resource.
30
       DException {
31       FieldBase result1 = (FieldBase) _charactervalueexpression3.run(object);
32       FieldBase result2 = (FieldBase) _numericvalueexpression1.run(object);
33       if (result1.isNull() || result2.isNull()) {
34          return new FieldStringLiteral(FieldUtility.NULLBUFFERRANGE, CHARACTER);
35       }
36       return getResult(result1, result2);
37    }
38
39    public ColumnDetails[] getChildColumnDetails() throws DException {
40       return columnDetails;
41    }
42
43    public ParameterInfo[] getParameterInfo() throws DException {
44
45      ArrayList JavaDoc list=new ArrayList JavaDoc();
46      ParameterInfo[] param1 = _charactervalueexpression3.getParameterInfo();
47      for (int i = 0; i < param1.length; i++) {
48        if (param1[i].getQuestionMark()) {
49          param1[i].setDataType(Datatypes.CHARACTER);
50          param1[i].setName("REPEAT Arg1");
51          list.addAll(Arrays.asList(param1));
52        }
53      }
54      ParameterInfo[] param2 = _numericvalueexpression1.getParameterInfo();
55      for (int i = 0; i < param2.length; i++) {
56        if (param2[i].getQuestionMark()) {
57          param2[i].setDataType(Datatypes.INTEGER);
58          param2[i].setName("REPEAT Arg2");
59          list.addAll(Arrays.asList(param2));
60        }
61      }
62
63       ParameterInfo[] param3 = new ParameterInfo[list.size()];
64       list.toArray(param3);
65       for (int i = 0; i <param3.length; i++) {
66         if(param3[i].getQuestionMark())
67           return param3;
68       }
69       return getThisParameterInfo();
70    }
71
72    protected ParameterInfo[] getThisParameterInfo() throws DException {
73       ParameterInfo parameterInfo = new ParameterInfo();
74       parameterInfo.setName(toString());
75       parameterInfo.setDataType(Datatypes.CHARACTER);
76       return new ParameterInfo[] {parameterInfo};
77    }
78
79
80    protected String JavaDoc getNameOfColumn(ColumnDetails[] columnDetails) throws
81        DException {
82       String JavaDoc nameOfColumn = "";
83       for (int i = 0; i < columnDetails.length; ++i) {
84          nameOfColumn += columnDetails[i].getColumn();
85       }
86       return nameOfColumn;
87    }
88
89    /*private int getDataType(Object object) throws DException {
90      if (object instanceof Integer)
91        return INTEGER;
92      else if (object instanceof Float)
93        return FLOAT;
94      else if (object instanceof Double)
95        return DOUBLE;
96      else if (object instanceof Long)
97        return LONG;
98      else if (object instanceof String)
99        return CHARACTER;
100      else if (object instanceof Short)
101        return SHORT;
102      else if (object instanceof BigDecimal)
103        return BIGDECIMAL;
104      else if (object instanceof Byte)
105        return BYTE;
106      else
107        throw new DException("DSE419", new Object[] {"REPEAT"});
108       }*/

109
110    private Object JavaDoc getResult(FieldBase result, FieldBase countFB) throws
111        DException {
112       Object JavaDoc object = result.getObject();
113       int type1 = getDataType(result);
114       switch (type1) {
115          case CHARACTER:
116          case VARCHAR:
117          case CHAR:
118          case CHARACTERVARYING:
119             Object JavaDoc count = countFB.getObject();
120             int type2 = getDataType(countFB);
121             switch (type2) {
122                case BYTE:
123                case TINYINT:
124                case SHORT:
125                case SMALLINT:
126                case INTEGER:
127                case INT:
128                case LONG:
129                case BIGINT:
130                   return repeat( (String JavaDoc) object, ( (Number JavaDoc) count).longValue());
131                case REAL:
132                case DOUBLE:
133                case FLOAT:
134                case DOUBLEPRECISION:
135                case BIGDECIMAL:
136                case DEC:
137                case DECIMAL:
138                case NUMERIC:
139                   return repeat(object, (Number JavaDoc) count);
140                case CHARACTER:
141                case VARCHAR:
142                case CHAR:
143                case CHARACTERVARYING:
144                   String JavaDoc string = (String JavaDoc) object;
145                   Double JavaDoc d = null;
146                   try {
147                      d = new Double JavaDoc(string);
148                   } catch (Exception JavaDoc e) {
149                      throw new DException("DSE4104", new Object JavaDoc[] {"character", "int"});
150                   }
151                   return repeat(object, d);
152                default:
153                   throw new DException("DSE4108", new Object JavaDoc[] {StaticClass.getDataTypeName(type2), "repeat"});
154             }
155          default:
156             throw new DException("DSE4108", new Object JavaDoc[] {StaticClass.getDataTypeName(type1), "repeat"});
157       }
158    }
159
160    private Object JavaDoc repeat(Object JavaDoc object, Number JavaDoc count) throws DException {
161       if ( (new BigDecimal(count + "")).compareTo(new BigDecimal(0)) < 0) {
162          throw new DException("DSE8114", null);
163       }
164       if ( (new BigDecimal(count + "")).compareTo(new BigDecimal("" + Long.MAX_VALUE)) > 0) {
165          throw new DException("DSE8113", new Object JavaDoc[] {"repeat"});
166       }
167       return repeat( (String JavaDoc) object, count.longValue());
168    }
169
170    /*private Object getResult(FieldBase result, Number count) throws
171        DException {
172      int type = getDataType(result);
173      Object object = result.getObject();
174      switch (type) {
175        case CHARACTER :case VARCHAR: case CHAR: case CHARACTERVARYING:
176          return repeat((String) object, count);
177        default:
178          throw new DException("DSE4108",new Object[]{StaticClass.getDataTypeName(type),"repeat"});
179      }
180       }*/

181
182    private Object JavaDoc repeat(String JavaDoc string, long count) throws DException {
183       if (count < 0) {
184          throw new DException("DSE8114", new Object JavaDoc[] {"repeat"});
185       }
186       StringBuffer JavaDoc tempString = new StringBuffer JavaDoc(string);
187       while (count-- > 1) {
188          if(tempString.length()<=4192) {
189            tempString.append(string);
190          } else {
191            throw new DException("DSE8170", null);
192          }
193       }
194       return new FieldStringLiteral(tempString.toString(), Datatype.CHARACTER);
195    }
196
197    /*private String repeat(String string, int count) throws DException {
198      if (string == null || count <= 0)
199        return string;
200      String tempString = string;
201      while (count-- > 1) {
202        string = string.concat(tempString);
203      }
204      return string;
205       }*/

206
207    /* private Number getCount(FieldBase length) throws DException {
208       int type = getDataType(length);
209       Object object = length.getObject();
210       switch (type) {
211          case BYTE:
212          case TINYINT:
213          case INTEGER:
214          case INT:
215          case REAL:
216          case DOUBLE:
217          case FLOAT:
218          case DOUBLEPRECISION:
219          case LONG:
220          case BIGINT:
221          case SHORT:
222          case SMALLINT:
223          case BIGDECIMAL:
224          case DEC:
225          case DECIMAL:
226          case NUMERIC:
227             return (Number) object;
228          case CHARACTER:
229          case VARCHAR:
230          case CHAR:
231          case CHARACTERVARYING:
232             String string = (String) object;
233             Double d = null;
234             try {
235                d = new Double(string);
236             } catch (Exception e) {
237                throw new DException("DSE4104", new Object[] {"character", "int"});
238             }
239             return d;
240          default:
241             throw new DException("DSE4108", new Object[] {StaticClass.getDataTypeName(type), "repeat"});
242       }
243    }
244
245    private Integer getCount(FieldBase length) throws DException {
246     int type = getDataType(length);
247     Object object = length.getObject();
248     switch (type) {
249       case BYTE : case TINYINT :
250         Byte byteV = (Byte) object;
251         return new Integer(byteV.intValue());
252       case INTEGER : case INT :
253         Integer integer = (Integer) object;
254         return new Integer(integer.intValue());
255       case REAL:
256         Float floatV = (Float) object;
257         return new Integer(floatV.intValue());
258       case DOUBLE : case FLOAT : case DOUBLEPRECISION :
259         Double doubleV = (Double) object;
260         return new Integer(doubleV.intValue());
261       case LONG : case BIGINT :
262         Long longV = (Long) object;
263         return new Integer(longV.intValue());
264       case SHORT : case SMALLINT :
265         Short shortV = (Short) object;
266         return new Integer(shortV.intValue());
267       case BIGDECIMAL : case DEC : case DECIMAL : case NUMERIC :
268         BigDecimal bigDecimal = (BigDecimal) object;
269         return new Integer(bigDecimal.intValue());
270       case CHARACTER :case VARCHAR: case CHAR: case CHARACTERVARYING:
271         String string = (String) object;
272         Double d = null;
273         try {
274           d = new Double(string);
275         }
276         catch (Exception e) {
277           throw new DException("DSE4104",new Object[]{"character","int"});
278         }
279         return new Integer(d.intValue());
280       default:
281         throw new DException("DSE4108",new Object[]{StaticClass.getDataTypeName(type),"repeat"});
282     }
283     } */

284
285    public AbstractRowValueExpression[] getChilds() {
286       AbstractRowValueExpression[] childs = new AbstractRowValueExpression[] {
287           (AbstractRowValueExpression) (_charactervalueexpression3),
288           (AbstractRowValueExpression) (_numericvalueexpression1)};
289       return childs;
290
291    }
292
293    public String JavaDoc getType() throws DException {
294       return (String JavaDoc) _SNONRESERVEDWORD1364442555.run(null);
295    }
296
297    public String JavaDoc toString() {
298       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
299       sb.append(" ");
300       sb.append(_SNONRESERVEDWORD1364442555);
301       sb.append(" ");
302       sb.append(_Sleftparen6538802414);
303       sb.append(" ");
304       sb.append(_charactervalueexpression3);
305       sb.append(" ");
306       sb.append(_Scomma948436052);
307       sb.append(" ");
308       sb.append(_numericvalueexpression1);
309       sb.append(" ");
310       sb.append(_Srightparen_18748595140);
311       return sb.toString();
312    }
313
314    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
315       repeatfunction tempClass = new repeatfunction();
316       tempClass._Srightparen_18748595140 = (Srightparen_1874859514)
317           _Srightparen_18748595140.clone();
318       tempClass._numericvalueexpression1 = (numericvalueexpression)
319           _numericvalueexpression1.clone();
320       tempClass._Scomma948436052 = (Scomma94843605) _Scomma948436052.clone();
321       tempClass._charactervalueexpression3 = (charactervalueexpression)
322           _charactervalueexpression3.clone();
323       tempClass._Sleftparen6538802414 = (Sleftparen653880241)
324           _Sleftparen6538802414.clone();
325       tempClass._SNONRESERVEDWORD1364442555 = (SNONRESERVEDWORD136444255)
326           _SNONRESERVEDWORD1364442555.clone();
327       return tempClass;
328    }
329
330    public _Reference[] checkSemantic(_ServerSession parent) throws DException {
331       _Reference[] ref = super.checkSemantic(parent);
332       if(ref!=null) {
333           return ref;
334         }
335       int type = _charactervalueexpression3.getByteComparison(parent).getDataTypes()[0];
336       switch (type) {
337          case -1:
338          case CHARACTER:
339          case VARCHAR:
340          case CHAR:
341          case CHARACTERVARYING:
342             return ref;
343          default:
344             throw new DException("DSE4108", new Object JavaDoc[] {StaticClass.getDataTypeName(type), "repeat"});
345       }
346
347    }
348
349    public int getColumnSize(Object JavaDoc object) throws DException {
350      ColumnDetails[] columnDetails = getChildColumnDetails();
351      if (columnDetails[0].getQuestion()) {
352        return DatabaseProperties.maxCharLiteralLength;
353      }
354      else if (columnDetails[0].getType() != TypeConstants.CONSTANT ||
355               columnDetails[0].getSize() != -5) {
356        return columnDetails[0].getSize();
357      }
358      else {
359        FieldBase field = (FieldBase) _charactervalueexpression3.run(object);
360        field.setDatatype(columnDetails[0].getDatatype());
361        return field.getLength();
362      }
363
364    }
365
366
367
368 }
369
Popular Tags