001    /*
002    // $Id: Datatype.java 125 2008-11-02 07:43:12Z jhyde $
003    // This software is subject to the terms of the Common Public License
004    // Agreement, available at the following URL:
005    // http://www.opensource.org/licenses/cpl.html.
006    // Copyright (C) 2006-2008 Julian Hyde
007    // All Rights Reserved.
008    // You must accept the terms of that agreement to use this software.
009    */
010    package org.olap4j.metadata;
011    
012    import java.util.Map;
013    import java.util.HashMap;
014    
015    /**
016     * Enumeration of the allowable data types of a Property or Measure.
017     *
018     * <p>The values derive from the OLE DB specification, specifically a
019     * subset of the OLE DB Types Indicators returned by SQL Server.
020     *
021     * @author jhyde
022     * @version $Id: Datatype.java 125 2008-11-02 07:43:12Z jhyde $
023     * @since Aug 23, 2006
024     */
025    public enum Datatype {
026        /*
027        * The following values exactly match VARENUM
028        * in Automation and may be used in VARIANT.
029        */
030        INTEGER(3, "DBTYPE_I4", "A four-byte, signed integer: INTEGER"),
031    
032        DOUBLE(5, "DBTYPE_R8", "A double-precision floating-point value: Double"),
033    
034        CURRENCY(6, "DBTYPE_CY", "A currency value: LARGE_INTEGER, Currency is a fixed-point number with four digits to the right of the decimal point. It is stored in an eight-byte signed integer, scaled by 10,000."),
035    
036        BOOLEAN(11, "DBTYPE_BOOL", "A Boolean value stored in the same way as in Automation: VARIANT_BOOL; 0 means false and ~0 (bitwise, the value is not 0; that is, all bits are set to 1) means true."),
037    
038        /**
039         * Used by SQL Server for value.
040         */
041        VARIANT(12, "DBTYPE_VARIANT", "An Automation VARIANT"),
042    
043        /**
044         * Used by SQL Server for font size.
045         */
046        UNSIGNED_SHORT(18, "DBTYPE_UI2", "A two-byte, unsigned integer"),
047    
048        /**
049         * Used by SQL Server for colors, font flags and cell ordinal.
050         */
051        UNSIGNED_INTEGER(19, "DBTYPE_UI4", "A four-byte, unsigned integer"),
052    
053        /*
054        * The following values exactly match VARENUM
055        * in Automation but cannot be used in VARIANT.
056        */
057        LARGE_INTEGER(20, "DBTYPE_I8", "An eight-byte, signed integer: LARGE_INTEGER"),
058    
059        /*
060        * The following values are not in VARENUM in OLE.
061        */
062        STRING(130, "DBTYPE_WSTR", "A null-terminated Unicode character string: wchar_t[length]; If DBTYPE_WSTR is used by itself, the number of bytes allocated for the string, including the null-termination character, is specified by cbMaxLen in the DBBINDING structure. If DBTYPE_WSTR is combined with DBTYPE_BYREF, the number of bytes allocated for the string, including the null-termination character, is at least the length of the string plus two. In either case, the actual length of the string is determined from the bound length value. The maximum length of the string is the number of allocated bytes divided by sizeof(wchar_t) and truncated to the nearest integer.");
063    
064        private final int xmlaOrdinal;
065    
066        private static final Map<Integer, Datatype> xmlaMap =
067            new HashMap<Integer, Datatype>();
068    
069        static {
070            for (Datatype datatype : values()) {
071                xmlaMap.put(datatype.xmlaOrdinal, datatype);
072            }
073        }
074    
075        Datatype(
076            int xmlaOrdinal,
077            String dbTypeIndicator,
078            String description)
079        {
080            this.xmlaOrdinal = xmlaOrdinal;
081        }
082    
083        /**
084         * Looks up a Datatype by its XMLA ordinal.
085         *
086         * @param xmlaOrdinal Ordinal of a Datatype according to the XMLA
087         * specification.
088         *
089         * @return Datatype with the given ordinal, or null if there is no
090         * such Datatype
091         */
092        public static Datatype forXmlaOrdinal(int xmlaOrdinal) {
093            return xmlaMap.get(xmlaOrdinal);
094        }
095    
096    }
097    
098    // End Datatype.java