001    package hep.aida.ref.sql;
002    
003    // AIDA
004    import hep.aida.IBaseTupleColumn;
005    
006    // FreeHEP
007    import hep.aida.ref.tuple.FTupleColumn;
008    import org.freehep.util.Value;
009    
010    // Log4J
011    import org.apache.log4j.Logger;
012    
013    /** <code>SQLTupleColumn</code> extends {@link FTupleColumn}
014      * with SQL backend.
015      * <p><font color="#880088">
016      * <pre>
017      * $Log: SQLTupleColumn.java,v $
018      * Revision 1.8  2007/05/23 16:38:44  hrivnac
019      * logical connections for Plotter; better UML
020      *
021      * Revision 1.7  2005/09/02 14:10:31  hrivnac
022      * moved to JAS3 0.8.2
023      *
024      * Revision 1.6  2004/10/13 12:25:39  hrivnac
025      * defaults work fine
026      *
027      * Revision 1.5  2004/10/13 09:21:21  hrivnac
028      * defaults used
029      *
030      * Revision 1.4  2004/10/12 19:26:59  hrivnac
031      * MySQL Connector 3.0.14
032      *
033      * Revision 1.3  2004/05/22 15:12:59  hrivnac
034      * class id reformated
035      *
036      * Revision 1.2  2004/05/04 08:50:33  hrivnac
037      * can run with JAS3
038      *
039      * Revision 1.1  2004/05/03 19:22:30  hrivnac
040      * basic support for JAS3
041      *
042      * </pre>
043      * </font></p>
044      * @opt attributes
045      * @opt operations
046      * @opt types
047      * @opt visibility
048      * @version $Id: SQLTupleColumn.java,v 1.8 2007/05/23 16:38:44 hrivnac Exp $
049      * @author <a href="mailto:Julius.Hrivnac@cern.ch">J.Hrivnac</a> */
050    public class SQLTupleColumn implements IBaseTupleColumn, FTupleColumn {
051    
052      /** Create with reference to {@link SQLTuple}.
053        * @param tuple  The {@link SQLTuple} containing requested column.
054        * @param column The requested column number. */
055      public SQLTupleColumn(SQLTuple tuple, int column) {
056        _tuple = tuple;
057        _column = column;
058        }
059    
060      /** Give the name of che column.
061        * @return The column's name. */
062      public String name() {
063        return _tuple.columnName(_column);
064        }
065    
066      /** Give the type of the column.
067        * @return The column's type. */
068      public Class type() {
069        return _tuple.columnType(_column);
070        }
071    
072      /** Give the mean value stored in the column.
073        * @return The mean value. */
074      public double mean() {
075        return _tuple.columnMean(_column);
076        }
077    
078      /** Give the rms value stored in the column.
079        * @return The rms value. */
080      public double rms() {
081        return _tuple.columnRms(_column);
082        }
083    
084      /** Give the minimum value stored in the column.
085        * @return The minimum value. */
086      public double minimum() {
087        return _tuple.columnMin(_column);
088        }
089    
090      /** Give the maximum value stored in the column.
091        * @return The maximum value. */
092      public double maximum() {
093        return _tuple.columnMax(_column);
094        }
095    
096      /** Give the minimum value stored in the column.
097        * @param value The {@link Value} object in which the minimum value is passed. */
098      public void minValue(Value value) {
099        value.set(_tuple.columnMin(_column));
100        }
101    
102      /** Give the maximum value stored in the column.
103        * @param value The {@link Value} object in which the maximum value is passed. */
104      public void maxValue(Value value) {
105        value.set(_tuple.columnMax(_column));
106        }
107    
108      /** Give the mean value stored in the column.
109        * @param value The {@link Value} object in which the mean value is passed. */
110      public void meanValue(Value value) {
111        value.set(_tuple.columnMean(_column));
112        }
113    
114      /** Give the rms value stored in the column.
115        * @param value The {@link Value} object in which the rms value is passed. */
116      public void rmsValue(Value value) {
117        value.set(_tuple.columnRms(_column));
118        }
119    
120      /** Give the default value stored in the column.
121        * @param value The {@link Value} object in which the default value is passed. */
122      public void defaultValue(Value value) {
123        String defaultValue = _tuple.columnDefault(_column);
124        boolean isNotSet = (defaultValue == null);
125             if (_tuple.columnType(_column) == Integer.TYPE  ) value.set(isNotSet ? (int   )0 : Integer.parseInt(  defaultValue));
126        else if (_tuple.columnType(_column) == Short.TYPE    ) value.set(isNotSet ? (short )0 : Short.parseShort(  defaultValue));
127        else if (_tuple.columnType(_column) == Long.TYPE     ) value.set(isNotSet ? (long  )0 : Long.parseLong(    defaultValue));
128        else if (_tuple.columnType(_column) == Float.TYPE    ) value.set(isNotSet ? (float )0 : Float.parseFloat(  defaultValue));
129        else if (_tuple.columnType(_column) == Double.TYPE   ) value.set(isNotSet ? (double)0 : Double.parseDouble(defaultValue));
130        else if (_tuple.columnType(_column) == Boolean.TYPE  ) value.set(isNotSet ? false     : Boolean.valueOf(   defaultValue).booleanValue());
131        else if (_tuple.columnType(_column) == Byte.TYPE     ) value.set(isNotSet ? (byte  )0 : Byte.parseByte(    defaultValue));
132        else if (_tuple.columnType(_column) == Character.TYPE) value.set(isNotSet ? (char  )0 :                    defaultValue.charAt(0));
133        else                                                   value.set(isNotSet ? ""        :                    defaultValue);
134        }
135    
136      /** Check if the columns have default values.
137        * @return true. */
138      public boolean hasDefaultValue() {
139        return true;
140        }
141    
142      private SQLTuple _tuple;
143    
144      private int _column;
145    
146      /** Logging . */
147      private static Logger log = Logger.getLogger(SQLTupleColumn.class);
148    
149      }