001/* 
002 * JKNIV, whinstone one contract to access your database.
003 * 
004 * Copyright (C) 2017, the original author or authors.
005 *
006 * This library is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 2.1 of the License.
010 * 
011 * This library is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with this library; if not, write to the Free Software Foundation, Inc., 
018 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
019 */
020package net.sf.jkniv.whinstone.types;
021
022import java.sql.Types;
023
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027/**
028 * <p>
029 * The class that defines the constants that are used to identify generic SQL
030 * types, called JDBC types. The int values are identical from java.sql.Types
031 * 
032 * @author Alisson Gomes
033 * @see java.sql.Types
034 * @since 0.6.0
035 */
036public enum JdbcType implements ColumnType
037{
038    /*
039JDBC Type       Java Type                       JDBC Type 
040CHAR            String                          CHAR, VARCHAR, or LONGVARCHAR
041VARCHAR         String                          CHAR, VARCHAR, or LONGVARCHAR
042LONGVARCHAR     String                          CHAR, VARCHAR, or LONGVARCHAR
043NUMERIC         java.math.BigDecimal            NUMERIC
044DECIMAL         java.math.BigDecimal            NUMERIC
045BIT             boolean                         BIT
046TINYINT         byte                            TINYINT
047SMALLINT        short                           SMALLINT
048INTEGER         int                             INTEGER
049BIGINT          long                            BIGINT
050REAL            float                           REAL
051FLOAT           double                          DOUBLE
052DOUBLE          double                          DOUBLE
053BINARY          byte[]                          BINARY, VARBINARY, or LONGVARBINARY
054VARBINARY       byte[]                          BINARY, VARBINARY, or LONGVARBINARY
055LONGVARBINARY   byte[]                          BINARY, VARBINARY, or LONGVARBINARY
056DATE            java.sql.Date                   DATE
057TIME            java.sql.Time                   DATE
058TIMESTAMP       java.sql.Timestamp              DATE
059CLOB            Clob                            CLOB
060BLOB            Blob                            BLOB
061ARRAY           Array                           ARRAY
062DISTINCT        mapping of underlying type      
063STRUCT          Struct                          STRUCT
064REF             Ref                             REF
065JAVA_OBJECT     underlying Java class           JAVA_OBJECT 
066     */
067    
068    //Types.BINARY 
069    //Types.DATALINK 
070    //Types.DECIMAL 
071    //Types.DISTINCT
072    //Types.JAVA_OBJECT 
073    //Types.LONGNVARCHAR 
074    //Types.LONGVARBINARY
075    //Types.LONGVARCHAR 
076    //Types.NUMERIC 
077    //Types.OTHER 
078    //Types.REF
079    //Types.REF_CURSOR 
080    //Types.ROWID 
081    //Types.SQLXML 
082    //Types.STRUCT 
083    //Types.TIME
084    //Types.TIME_WITH_TIMEZONE 
085    //Types.TIMESTAMP
086    //Types.TIMESTAMP_WITH_TIMEZONE    
087    /**
088     * <P>
089     * The constant in the Java programming language, sometimes referred to as a
090     * type code, that identifies the generic SQL type <code>BIT</code>.
091     */
092    BIT(Types.BIT),
093    
094    /**
095     * <P>
096     * The constant in the Java programming language, sometimes referred to as a
097     * type code, that identifies the generic SQL type <code>TINYINT</code>.
098     */
099    TINYINT(Types.TINYINT),
100    
101    /**
102     * <P>
103     * The constant in the Java programming language, sometimes referred to as a
104     * type code, that identifies the generic SQL type <code>SMALLINT</code>.
105     */
106    SMALLINT(Types.SMALLINT),
107    
108    /**
109     * <P>
110     * The constant in the Java programming language, sometimes referred to as a
111     * type code, that identifies the generic SQL type <code>INTEGER</code>.
112     */
113    INTEGER(Types.INTEGER),
114    
115    /**
116     * <P>
117     * The constant in the Java programming language, sometimes referred to as a
118     * type code, that identifies the generic SQL type <code>BIGINT</code>.
119     */
120    BIGINT(Types.BIGINT),
121    
122    /**
123     * <P>
124     * The constant in the Java programming language, sometimes referred to as a
125     * type code, that identifies the generic SQL type <code>FLOAT</code>.
126     */
127    FLOAT(Types.FLOAT),
128    
129    /**
130     * <P>
131     * The constant in the Java programming language, sometimes referred to as a
132     * type code, that identifies the generic SQL type <code>REAL</code>.
133     */
134    REAL(Types.REAL),
135    
136    /**
137     * <P>
138     * The constant in the Java programming language, sometimes referred to as a
139     * type code, that identifies the generic SQL type <code>DOUBLE</code>.
140     */
141    DOUBLE(Types.DOUBLE),
142    
143    /**
144     * <P>
145     * The constant in the Java programming language, sometimes referred to as a
146     * type code, that identifies the generic SQL type <code>NUMERIC</code>.
147     */
148    NUMERIC(Types.NUMERIC),
149    
150    /**
151     * <P>
152     * The constant in the Java programming language, sometimes referred to as a
153     * type code, that identifies the generic SQL type <code>DECIMAL</code>.
154     */
155    DECIMAL(Types.DECIMAL),
156    
157    /**
158     * <P>
159     * The constant in the Java programming language, sometimes referred to as a
160     * type code, that identifies the generic SQL type <code>CHAR</code>.
161     */
162    CHAR(Types.CHAR),
163    
164    /**
165     * <P>
166     * The constant in the Java programming language, sometimes referred to as a
167     * type code, that identifies the generic SQL type <code>VARCHAR</code>.
168     */
169    VARCHAR(Types.VARCHAR),
170    
171    /**
172     * <P>
173     * The constant in the Java programming language, sometimes referred to as a
174     * type code, that identifies the generic SQL type <code>LONGVARCHAR</code>.
175     */
176    LONGVARCHAR(Types.LONGVARCHAR),
177    
178    /**
179     * <P>
180     * The constant in the Java programming language, sometimes referred to as a
181     * type code, that identifies the generic SQL type <code>DATE</code>.
182     */
183    DATE(Types.DATE),
184    
185    /**
186     * <P>
187     * The constant in the Java programming language, sometimes referred to as a
188     * type code, that identifies the generic SQL type <code>TIME</code>.
189     */
190    TIME(Types.TIME),
191    
192    /**
193     * <P>
194     * The constant in the Java programming language, sometimes referred to as a
195     * type code, that identifies the generic SQL type <code>TIMESTAMP</code>.
196     */
197    TIMESTAMP(Types.TIMESTAMP),
198    
199    /**
200     * <P>
201     * The constant in the Java programming language, sometimes referred to as a
202     * type code, that identifies the generic SQL type <code>BINARY</code>.
203     */
204    BINARY(Types.BINARY),
205    
206    /**
207     * <P>
208     * The constant in the Java programming language, sometimes referred to as a
209     * type code, that identifies the generic SQL type <code>VARBINARY</code>.
210     */
211    VARBINARY(Types.VARBINARY),
212    
213    /**
214     * <P>
215     * The constant in the Java programming language, sometimes referred to as a
216     * type code, that identifies the generic SQL type
217     * <code>LONGVARBINARY</code>.
218     */
219    LONGVARBINARY(Types.LONGVARBINARY),
220    
221    /**
222     * <P>
223     * The constant in the Java programming language that identifies the generic
224     * SQL value <code>NULL</code>.
225     */
226    NULL(Types.NULL),
227    
228    /**
229     * The constant in the Java programming language that indicates that the SQL
230     * type is database-specific and gets mapped to a Java object that can be
231     * accessed via the methods <code>getObject</code> and
232     * <code>setObject</code>.
233     */
234    OTHER(Types.OTHER),
235    
236    /**
237     * The constant in the Java programming language, sometimes referred to as a
238     * type code, that identifies the generic SQL type <code>JAVA_OBJECT</code>.
239     * 
240     * @since 1.2
241     */
242    JAVA_OBJECT(Types.JAVA_OBJECT),
243    
244    /**
245     * The constant in the Java programming language, sometimes referred to as a
246     * type code, that identifies the generic SQL type <code>DISTINCT</code>.
247     * 
248     * @since 1.2
249     */
250    DISTINCT(Types.DISTINCT),
251    
252    /**
253     * The constant in the Java programming language, sometimes referred to as a
254     * type code, that identifies the generic SQL type <code>STRUCT</code>.
255     * 
256     * @since 1.2
257     */
258    STRUCT(Types.STRUCT),
259    
260    /**
261     * The constant in the Java programming language, sometimes referred to as a
262     * type code, that identifies the generic SQL type <code>ARRAY</code>.
263     * 
264     * @since 1.2
265     */
266    ARRAY(Types.ARRAY),
267    
268    /**
269     * The constant in the Java programming language, sometimes referred to as a
270     * type code, that identifies the generic SQL type <code>BLOB</code>.
271     * 
272     * @since 1.2
273     */
274    BLOB(Types.BLOB),
275    
276    /**
277     * The constant in the Java programming language, sometimes referred to as a
278     * type code, that identifies the generic SQL type <code>CLOB</code>.
279     * 
280     * @since 1.2
281     */
282    CLOB(Types.CLOB),
283    
284    /**
285     * The constant in the Java programming language, sometimes referred to as a
286     * type code, that identifies the generic SQL type <code>REF</code>.
287     * 
288     * @since 1.2
289     */
290    REF(Types.REF),
291    
292    /**
293     * The constant in the Java programming language, somtimes referred to as a
294     * type code, that identifies the generic SQL type <code>DATALINK</code>.
295     *
296     * @since 1.4
297     */
298    DATALINK(Types.DATALINK),
299    
300    /**
301     * The constant in the Java programming language, somtimes referred to as a
302     * type code, that identifies the generic SQL type <code>BOOLEAN</code>.
303     *
304     * @since 1.4
305     */
306    BOOLEAN(Types.BOOLEAN),
307    
308    // ------------------------- JDBC 4.0 -----------------------------------
309    
310    /**
311     * The constant in the Java programming language, sometimes referred to as a
312     * type code, that identifies the generic SQL type <code>ROWID</code>
313     *
314     * @since 1.6
315     */
316    ROWID(Types.ROWID),
317    
318    /**
319     * The constant in the Java programming language, sometimes referred to as a
320     * type code, that identifies the generic SQL type <code>NCHAR</code>
321     *
322     * @since 1.6
323     */
324    NCHAR(Types.NCHAR),
325    
326    /**
327     * The constant in the Java programming language, sometimes referred to as a
328     * type code, that identifies the generic SQL type <code>NVARCHAR</code>.
329     *
330     * @since 1.6
331     */
332    NVARCHAR(Types.NVARCHAR),
333    
334    /**
335     * The constant in the Java programming language, sometimes referred to as a
336     * type code, that identifies the generic SQL type <code>LONGNVARCHAR</code>
337     * .
338     *
339     * @since 1.6
340     */
341    LONGNVARCHAR(Types.LONGVARCHAR),
342    
343    /**
344     * The constant in the Java programming language, sometimes referred to as a
345     * type code, that identifies the generic SQL type <code>NCLOB</code>.
346     *
347     * @since 1.6
348     */
349    NCLOB(Types.NCLOB),
350    
351    /**
352     * The constant in the Java programming language, sometimes referred to as a
353     * type code, that identifies the generic SQL type <code>XML</code>.
354     *
355     * @since 1.6
356     */
357    SQLXML(2009),
358    
359    // --------------------------JDBC 4.2 -----------------------------
360    
361    /**
362     * The constant in the Java programming language, sometimes referred to as a
363     * type code, that identifies the generic SQL type {@code REF CURSOR}.
364     *
365     * @since 1.8
366     */
367    REF_CURSOR(2012),
368    
369    /**
370     * The constant in the Java programming language, sometimes referred to as a
371     * type code, that identifies the generic SQL type
372     * {@code TIME WITH TIMEZONE}.
373     *
374     * @since 1.8
375     */
376    TIME_WITH_TIMEZONE(2013),
377    
378    /**
379     * The constant in the Java programming language, sometimes referred to as a
380     * type code, that identifies the generic SQL type
381     * {@code TIMESTAMP WITH TIMEZONE}.
382     *
383     * @since 1.8
384     */
385    TIMESTAMP_WITH_TIMEZONE(2014);
386    
387    private final static Logger LOG = LoggerFactory.getLogger(JdbcType.class);
388    private int                 value;
389    
390    private JdbcType(int v)
391    {
392        this.value = v;
393    }
394    
395    public int value()
396    {
397        return value;
398    }
399    
400    @Override
401    public boolean isBinary()
402    {
403        return (this.value == Types.CLOB || this.value == Types.BLOB);
404    }
405    
406    @Override
407    public boolean isBlob()
408    {
409        // FIXME implements write BLOB to database
410        return (this.value == Types.BLOB);
411    }
412
413    @Override
414    public boolean isClob()
415    {
416        return (this.value == Types.CLOB);
417    }
418    
419    @Override
420    public boolean isDate()
421    {
422        return (this.value == Types.DATE);
423    }
424    
425    @Override
426    public boolean isTimestamp()
427    {
428        return (this.value == Types.TIMESTAMP);
429    }
430    
431    @Override
432    public boolean isTime()
433    {
434        return (this.value == Types.TIME);
435    }
436
437    public static ColumnType valueOf(int jdbcTypeValue)
438    {
439        JdbcType answer = null;
440        for(JdbcType jdbcType : JdbcType.values())
441        {
442            if (jdbcType.value() == jdbcTypeValue)
443            {
444                answer = jdbcType;
445                break;
446            }
447        }
448        return answer;
449    }
450}