001: /*
002: * Copyright 2002 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: ByteMapping.java,v 1.5 2003/10/10 22:22:14 pierreg0 Exp $
009: */
010:
011: package com.triactive.jdo.store;
012:
013: import com.triactive.jdo.PersistenceManager;
014: import java.math.BigInteger;
015: import java.sql.PreparedStatement;
016: import java.sql.ResultSet;
017: import java.sql.SQLException;
018: import java.sql.Types;
019: import javax.jdo.JDODataStoreException;
020:
021: public class ByteMapping extends ColumnMapping {
022: public ByteMapping(DatabaseAdapter dba, Class type) {
023: super (dba, type);
024:
025: initTypeInfo();
026: }
027:
028: public ByteMapping(Column col) {
029: super (col);
030:
031: col.checkPrimitive();
032:
033: initTypeInfo();
034: }
035:
036: public ByteMapping(ClassBaseTable table, int relativeFieldNumber) {
037: this (table.newColumn(relativeFieldNumber));
038: }
039:
040: protected TypeInfo getTypeInfo() {
041: return dba.getTypeInfo(new int[] { Types.TINYINT,
042: Types.SMALLINT, Types.INTEGER, Types.DECIMAL });
043: }
044:
045: public void setByte(PersistenceManager pm, PreparedStatement ps,
046: int param, byte value) {
047: try {
048: ps.setByte(param, value);
049: } catch (SQLException e) {
050: throw dba.newDataStoreException(
051: "Can't set byte parameter: value = " + value, e);
052: }
053: }
054:
055: public byte getByte(PersistenceManager pm, ResultSet rs, int param) {
056: byte value;
057:
058: try {
059: value = rs.getByte(param);
060:
061: if (rs.wasNull())
062: throw new NullValueException(
063: "Illegal null value in column " + col);
064: } catch (SQLException e) {
065: throw dba.newDataStoreException(
066: "Can't get byte result: param = " + param, e);
067: }
068:
069: return value;
070: }
071:
072: public void setObject(PersistenceManager pm, PreparedStatement ps,
073: int param, Object value) {
074: try {
075: if (value == null)
076: ps.setNull(param, typeInfo.dataType);
077: else
078: ps.setByte(param, ((Byte) value).byteValue());
079: } catch (SQLException e) {
080: throw dba.newDataStoreException(
081: "Can't set Byte parameter: value = " + value, e);
082: }
083: }
084:
085: public Object getObject(PersistenceManager pm, ResultSet rs,
086: int param) {
087: Object value;
088:
089: try {
090: byte b = rs.getByte(param);
091: value = rs.wasNull() ? null : new Byte(b);
092: } catch (SQLException e) {
093: throw dba.newDataStoreException(
094: "Can't get Byte result: param = " + param, e);
095: }
096:
097: return value;
098: }
099:
100: public SQLExpression newSQLLiteral(QueryStatement qs, Object value) {
101: return new IntegerLiteral(qs, BigInteger.valueOf(((Byte) value)
102: .longValue()));
103: }
104:
105: public SQLExpression newSQLExpression(QueryStatement qs,
106: QueryStatement.QueryColumn qsc, String fieldName) {
107: return new NumericExpression(qs, qsc);
108: }
109: }
|