001: /*
002: * Copyright 2004 (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: ByteArrayMapping.java,v 1.1 2004/03/22 04:58:13 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.store;
012:
013: import com.triactive.jdo.PersistenceManager;
014: import java.sql.PreparedStatement;
015: import java.sql.ResultSet;
016: import java.sql.SQLException;
017: import java.sql.Types;
018: import javax.jdo.JDOUserException;
019:
020: public class ByteArrayMapping extends ColumnMapping {
021: public ByteArrayMapping(DatabaseAdapter dba, Class type) {
022: super (dba, type);
023:
024: initTypeInfo();
025: }
026:
027: public ByteArrayMapping(Column col) {
028: super (col);
029:
030: col.checkString();
031:
032: initTypeInfo();
033: }
034:
035: public ByteArrayMapping(ClassBaseTable table,
036: int relativeFieldNumber) {
037: this (table.newColumn(relativeFieldNumber));
038: }
039:
040: protected TypeInfo getTypeInfo() {
041: TypeInfo ti;
042:
043: if (col == null)
044: ti = dba.getTypeInfo(new int[] { Types.VARBINARY,
045: Types.LONGVARBINARY, Types.BLOB });
046: else {
047: switch (col.getLengthType()) {
048: case Column.FIXED_LENGTH:
049: ti = dba.getTypeInfo(new int[] { Types.BINARY,
050: Types.LONGVARBINARY, Types.BLOB });
051:
052: if (ti.dataType != Types.BINARY)
053: col.setUnlimitedLength();
054:
055: break;
056:
057: case Column.MAXIMUM_LENGTH:
058: ti = dba.getTypeInfo(new int[] { Types.VARBINARY,
059: Types.LONGVARBINARY, Types.BLOB });
060:
061: if (ti.dataType != Types.VARBINARY)
062: col.setUnlimitedLength();
063:
064: break;
065:
066: case Column.UNLIMITED_LENGTH:
067: default:
068: ti = dba.getTypeInfo(new int[] { Types.LONGVARBINARY,
069: Types.BLOB });
070: break;
071: }
072: }
073:
074: return ti;
075: }
076:
077: public void setObject(PersistenceManager pm, PreparedStatement ps,
078: int param, Object value) {
079: try {
080: if (value == null)
081: ps.setNull(param, typeInfo.dataType);
082: else
083: ps.setBytes(param, (byte[]) value);
084: } catch (SQLException e) {
085: throw dba.newDataStoreException(
086: "Can't set byte[] parameter: value = " + value, e);
087: }
088: }
089:
090: public Object getObject(PersistenceManager pm, ResultSet rs,
091: int param) {
092: Object value;
093:
094: try {
095: byte[] ba = rs.getBytes(param);
096: value = rs.wasNull() ? null : ba;
097: } catch (SQLException e) {
098: throw dba.newDataStoreException(
099: "Can't get byte[] result: param = " + param, e);
100: }
101:
102: return value;
103: }
104:
105: public SQLExpression newSQLLiteral(QueryStatement qs, Object value) {
106: return new BinaryLiteral(qs, ((byte[]) value));
107: }
108:
109: public SQLExpression newSQLExpression(QueryStatement qs,
110: QueryStatement.QueryColumn qsc, String fieldName) {
111: return new BinaryExpression(qs, qsc);
112: }
113: }
|