~sgdg/stado/stado25

« back to all changes in this revision

Viewing changes to src/org/postgresql/driver/core/types/PGByte.java

  • Committer: Jim Mlodgenski
  • Date: 2011-08-30 22:39:37 UTC
  • mfrom: (1.1.3 stado)
  • Revision ID: jim@cirrusql.com-20110830223937-25q231a31x0e08b4
Merge from Spatial branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 * Copyright (C) 2008 EnterpriseDB Corporation.
 
3
 * Copyright (C) 2011 Stado Global Development Group.
 
4
 *
 
5
 * This file is part of Stado.
 
6
 *
 
7
 * Stado is free software: you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation, either version 3 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * Stado is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with Stado.  If not, see <http://www.gnu.org/licenses/>.
 
19
 *
 
20
 * You can find Stado at http://www.stado.us
 
21
 *
 
22
 ****************************************************************************/
 
23
package org.postgresql.driver.core.types;
 
24
 
 
25
import java.math.BigDecimal;
 
26
import java.sql.Types;
 
27
 
 
28
import org.postgresql.driver.util.GT;
 
29
import org.postgresql.driver.util.PSQLException;
 
30
import org.postgresql.driver.util.PSQLState;
 
31
 
 
32
/**
 
33
 * @author davec
 
34
 *
 
35
 * TODO To change the template for this generated type comment go to
 
36
 * Window - Preferences - Java - Code Style - Code Templates
 
37
 */
 
38
public class PGByte implements PGType
 
39
{
 
40
 
 
41
    private Byte val;
 
42
    
 
43
    public PGByte( Byte x )
 
44
    {
 
45
        val = x;
 
46
    }
 
47
    /* (non-Javadoc)
 
48
     * @see org.postgresql.types.PGType#castToServerType(int)
 
49
     */
 
50
    public static PGType castToServerType(Byte val, int targetType) throws PSQLException
 
51
    {
 
52
        try
 
53
        {
 
54
            switch ( targetType )
 
55
            {
 
56
                    case Types.BIT:
 
57
                        return new PGBoolean( val.byteValue() == 0?Boolean.FALSE:Boolean.TRUE );
 
58
                    
 
59
                    case Types.SMALLINT:
 
60
                    case Types.TINYINT:
 
61
                        return new PGByte( val );
 
62
                    case Types.REAL:
 
63
                        return new PGFloat( new Float( val.floatValue() ) ); 
 
64
                    case Types.DOUBLE:
 
65
                    case Types.FLOAT:
 
66
                        return new PGDouble( new Double( val.doubleValue() ) );
 
67
                    case Types.NUMERIC:
 
68
                    case Types.DECIMAL:
 
69
                        return new PGBigDecimal( new BigDecimal(val.toString()) );
 
70
                    case Types.VARCHAR:
 
71
                    case Types.LONGVARCHAR:                
 
72
                        return new PGString ( val.toString() );
 
73
                    default:
 
74
                        return new PGUnknown(val);
 
75
            }
 
76
        }
 
77
        catch( Exception ex )
 
78
        {
 
79
            throw new PSQLException(GT.tr("Cannot convert an instance of {0} to type {1}", new Object[]{val.getClass().getName(),"Types.OTHER"}), PSQLState.INVALID_PARAMETER_TYPE, ex);
 
80
        }
 
81
    }
 
82
    
 
83
    public String toString()
 
84
    {
 
85
        return val.toString();
 
86
    }
 
87
}